[red-knot] fix eager nested scopes handling (#16916)

## Summary

From #16861, and the continuation of #16915.

This PR fixes the incorrect behavior of
`TypeInferenceBuilder::infer_name_load` in eager nested scopes.

And this PR closes #16341.

## Test Plan

New test cases are added in `annotations/deferred.md`.
This commit is contained in:
Shunsuke Shibayama 2025-03-29 00:11:56 +09:00 committed by GitHub
parent 64171744dc
commit aca6254e82
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
9 changed files with 242 additions and 64 deletions

View file

@ -256,11 +256,11 @@ impl<'db> SemanticIndexBuilder<'db> {
}
for nested_symbol in self.symbol_tables[popped_scope_id].symbols() {
// Skip this symbol if this enclosing scope doesn't contain any bindings for
// it, or if the nested scope _does_.
if nested_symbol.is_bound() {
continue;
}
// Skip this symbol if this enclosing scope doesn't contain any bindings for it.
// Note that even if this symbol is bound in the popped scope,
// it may refer to the enclosing scope bindings
// so we also need to snapshot the bindings of the enclosing scope.
let Some(enclosing_symbol_id) =
enclosing_symbol_table.symbol_id_by_name(nested_symbol.name())
else {