@@ -7866,6 +7866,12 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
78667866 let mut nonlocal_union_builder = UnionBuilder :: new ( db) ;
78677867 let mut found_some_definition = false ;
78687868 for ( enclosing_scope_file_id, _) in self . index . ancestor_scopes ( file_scope_id) . skip ( 1 ) {
7869+ // If the current enclosing scope is global, no place lookup is performed here,
7870+ // instead falling back to the module's explicit global lookup below.
7871+ if enclosing_scope_file_id == FileScopeId :: global ( ) {
7872+ break ;
7873+ }
7874+
78697875 // Class scopes are not visible to nested scopes, and we need to handle global
78707876 // scope differently (because an unbound name there falls back to builtins), so
78717877 // check only function-like scopes.
@@ -7958,11 +7964,6 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
79587964 if enclosing_place. as_symbol ( ) . is_some_and ( Symbol :: is_global) {
79597965 break ;
79607966 }
7961- // If the current enclosing scope is global, no place lookup is performed here,
7962- // instead falling back to the module's explicit global lookup below.
7963- if enclosing_scope_file_id == FileScopeId :: global ( ) && !found_some_definition {
7964- break ;
7965- }
79667967
79677968 let enclosing_scope_id = enclosing_scope_file_id. to_scope_id ( db, self . file ( ) ) ;
79687969
@@ -8023,11 +8024,10 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
80238024 file_scope_id,
80248025 ) {
80258026 EnclosingSnapshotResult :: FoundConstraint ( constraint) => {
8026- // The constraint should already have been added in the iteration over `ancestor_scopes` above.
8027- debug_assert ! ( constraint_keys. contains( & (
8027+ constraint_keys. push ( (
80288028 FileScopeId :: global ( ) ,
80298029 ConstraintKey :: NarrowingConstraint ( constraint) ,
8030- ) ) ) ;
8030+ ) ) ;
80318031 // Reaching here means that no bindings are found in any scope.
80328032 // Since `explicit_global_symbol` may return a cycle initial value, we return `Place::Undefined` here.
80338033 return Place :: Undefined . into ( ) ;
@@ -8040,10 +8040,10 @@ impl<'db, 'ast> TypeInferenceBuilder<'db, 'ast> {
80408040 & constraint_keys,
80418041 )
80428042 } ) ;
8043- debug_assert ! ( constraint_keys. contains ( & (
8043+ constraint_keys. push ( (
80448044 FileScopeId :: global ( ) ,
80458045 ConstraintKey :: NestedScope ( file_scope_id) ,
8046- ) ) ) ;
8046+ ) ) ;
80478047 return place. into ( ) ;
80488048 }
80498049 // There are no visible bindings / constraint here.
0 commit comments