Skip to content

Commit

Permalink
Fix type inference merge conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
dcreager committed Feb 10, 2025
1 parent e5eaab9 commit a3ea477
Showing 1 changed file with 28 additions and 1 deletion.
29 changes: 28 additions & 1 deletion crates/red_knot_python_semantic/src/types/infer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3356,19 +3356,44 @@ impl<'db> TypeInferenceBuilder<'db> {

// Walk up parent scopes looking for a possible enclosing scope that may have a
// definition of this name visible to us (would be `LOAD_DEREF` at runtime.)
let mut previous_enclosing_scope_id = None;
for (enclosing_scope_file_id, _) in self.index.ancestor_scopes(file_scope_id) {
// Class scopes are not visible to nested scopes, and we need to handle global
// scope differently (because an unbound name there falls back to builtins), so
// check only function-like scopes.
let enclosing_scope_id = enclosing_scope_file_id.to_scope_id(db, current_file);
if !enclosing_scope_id.is_function_like(db) {
previous_enclosing_scope_id = Some(enclosing_scope_id);
continue;
}

let enclosing_symbol_table = self.index.symbol_table(enclosing_scope_file_id);
let Some(enclosing_symbol) = enclosing_symbol_table.symbol_by_name(symbol_name)
let Some(enclosing_symbol_id) =
enclosing_symbol_table.symbol_id_by_name(symbol_name)
else {
previous_enclosing_scope_id = Some(enclosing_scope_id);
continue;
};

// Is the immediately previous enclosing scope eager within this one? If so, we
// need to look for the symbol at the point where the previous enclosing scope was
// defined, instead of at the end of the scope.
let previous_eager_scope_id = previous_enclosing_scope_id.and_then(|previous| {
previous.scoped_eager_nested_scope_id(db, enclosing_scope_id)
});
if let Some(previous_eager_scope_id) = previous_eager_scope_id {
let enclosing_scope_use_def = self.index.use_def_map(enclosing_scope_file_id);
if let Some(bindings_at_nested_scope_definition) = enclosing_scope_use_def
.bindings_at_eager_nested_scope_definition(
previous_eager_scope_id,
enclosing_symbol_id,
)
{
return symbol_from_bindings(db, bindings_at_nested_scope_definition);
}
}

let enclosing_symbol = enclosing_symbol_table.symbol(enclosing_symbol_id);
if enclosing_symbol.is_bound() {
// We can return early here, because the nearest function-like scope that
// defines a name must be the only source for the nonlocal reference (at
Expand All @@ -3377,6 +3402,8 @@ impl<'db> TypeInferenceBuilder<'db> {
// falling back to other scopes / globals / builtins.
return symbol(db, enclosing_scope_id, symbol_name);
}

previous_enclosing_scope_id = Some(enclosing_scope_id);
}

Symbol::Unbound
Expand Down

0 comments on commit a3ea477

Please sign in to comment.