Allow simple uses of Self in impls. (#3824)

Per #3714, some of the details here are not yet settled. In particular,
we might want `Self` to come into scope at the start of the definition,
not at the `as` keyword. However, this change allows us to accept the
uncontroversial examples.
This commit is contained in:
Richard Smith
2024-03-27 22:52:45 +00:00
committed by GitHub
parent a79027120a
commit e8cc089b2b
6 changed files with 240 additions and 71 deletions
+9 -2
View File
@@ -43,8 +43,13 @@ auto HandleTypeImplAs(Context& context, Parse::TypeImplAsId node_id) -> bool {
auto [self_node, self_id] = context.node_stack().PopExprWithNodeId();
auto self_type_id = ExprAsType(context, self_node, self_id);
context.node_stack().Push(node_id, self_type_id);
// TODO: `Self` should come into scope here, at least if it's not already in
// scope. Check the design for the latter case.
// Introduce `Self`. Note that we add this name lexically rather than adding
// to the `NameScopeId` of the `impl`, because this happens before we enter
// the `impl` scope or even identify which `impl` we're declaring.
// TODO: Revisit this once #3714 is resolved.
context.AddNameToLookup(SemIR::NameId::SelfType,
context.types().GetInstId(self_type_id));
return true;
}
@@ -85,6 +90,8 @@ auto HandleDefaultSelfImplAs(Context& context,
self_type_id = SemIR::TypeId::Error;
}
// There's no need to push `Self` into scope here, because we can find it in
// the enclosing class scope.
context.node_stack().Push(node_id, self_type_id);
return true;
}