Fix introduction of class and interface names in local scopes. (#4793)

When declaring a class (or interface), we create a scope that covers the
entire class declaration. If the class was declared in a lexical scope,
we would declare the class name in the innermost scope, which was the
class's own scope instead of the enclosing lexical scope.

Fix this by instead adding the name to the lexical scope at the start of
the class declaration, not the lexical scope created to hold the class.
For now, we reject if the class name would have been shadowed by a name
that has already been declared within its scope, such as a generic
parameter, so we only ever need to modify the end of the list of lexical
lookup results for the class name.

This appears to be sufficient to make local declarations and definitions
of classes and interfaces work properly throughout check, though testing
is pretty minimal so far.
This commit is contained in:
Richard Smith
2025-01-13 18:55:26 +00:00
committed by GitHub
parent bb6ffc3dbc
commit 0d70091bda
10 changed files with 485 additions and 65 deletions
+4 -2
View File
@@ -134,7 +134,8 @@ auto DeclNameStack::AddName(NameContext name_context, SemIR::InstId target_id,
case NameContext::State::Unresolved:
if (!name_context.parent_scope_id.is_valid()) {
context_->AddNameToLookup(name_context.unresolved_name_id, target_id);
context_->AddNameToLookup(name_context.unresolved_name_id, target_id,
name_context.initial_scope_index);
} else {
auto& name_scope =
context_->name_scopes().Get(name_context.parent_scope_id);
@@ -262,7 +263,8 @@ auto DeclNameStack::ApplyAndLookupName(NameContext& name_context,
// For identifier nodes, we need to perform a lookup on the identifier.
auto [resolved_inst_id, is_poisoned] = context_->LookupNameInDecl(
name_context.loc_id, name_id, name_context.parent_scope_id);
name_context.loc_id, name_id, name_context.parent_scope_id,
name_context.initial_scope_index);
if (is_poisoned) {
name_context.unresolved_name_id = name_id;
name_context.state = NameContext::State::Poisoned;