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
+21 -7
View File
@@ -304,20 +304,22 @@ auto Context::NoteUndefinedInterface(SemIR::InterfaceId interface_id,
}
}
auto Context::AddNameToLookup(SemIR::NameId name_id, SemIR::InstId target_id)
-> void {
if (auto existing = scope_stack().LookupOrAddName(name_id, target_id);
auto Context::AddNameToLookup(SemIR::NameId name_id, SemIR::InstId target_id,
ScopeIndex scope_index) -> void {
if (auto existing =
scope_stack().LookupOrAddName(name_id, target_id, scope_index);
existing.is_valid()) {
DiagnoseDuplicateName(target_id, existing);
}
}
auto Context::LookupNameInDecl(SemIR::LocId loc_id, SemIR::NameId name_id,
SemIR::NameScopeId scope_id)
SemIR::NameScopeId scope_id,
ScopeIndex scope_index)
-> std::pair<SemIR::InstId, bool> {
if (!scope_id.is_valid()) {
// Look for a name in the current scope only. There are two cases where the
// name would be in an outer scope:
// Look for a name in the specified scope or a scope nested within it only.
// There are two cases where the name would be in an outer scope:
//
// - The name is the sole component of the declared name:
//
@@ -339,7 +341,19 @@ auto Context::LookupNameInDecl(SemIR::LocId loc_id, SemIR::NameId name_id,
// In this case, we're not in the correct scope to define a member of
// class A, so we should reject, and we achieve this by not finding the
// name A from the outer scope.
return {scope_stack().LookupInCurrentScope(name_id), false};
//
// There is also one case where the name would be in an inner scope:
//
// - The name is redeclared by a parameter of the same entity:
//
// fn F() {
// class C(C:! type);
// }
//
// In this case, the class C is not a redeclaration of its parameter, but
// we find the parameter in order to diagnose a redeclaration error.
return {scope_stack().LookupInLexicalScopesWithin(name_id, scope_index),
false};
} else {
// We do not look into `extend`ed scopes here. A qualified name in a
// declaration must specify the exact scope in which the name was originally