Set the value of .Self in a where to that of the outer .Self. (#2344)

Prior to this change, declarations like `let N:! X(.Self) where .(X(.Self).Y) == 5;` have the surprising behavior of the two `.Self` expressions resolving to two different symbolic values. Fix this by forcing the inner one to have the same symbolic value as the outer one, albeit with a different type.
This commit is contained in:
Richard Smith
2022-10-25 11:59:42 -07:00
committed by GitHub
parent c68cced1fb
commit bc7bf325d6
8 changed files with 69 additions and 44 deletions
+8
View File
@@ -239,6 +239,14 @@ static auto ResolveNames(Expression& expression,
auto& where = cast<WhereExpression>(expression);
CARBON_RETURN_IF_ERROR(
ResolveNames(where.self_binding().type(), enclosing_scope));
// If we're already in a `.Self` context, remember it so that we can
// reuse its value for the inner `.Self`.
if (auto enclosing_dot_self =
enclosing_scope.Resolve(".Self", where.source_loc());
enclosing_dot_self.ok()) {
where.set_enclosing_dot_self(
&cast<GenericBinding>(enclosing_dot_self->base()));
}
// Introduce `.Self` into scope on the right of the `where` keyword.
StaticScope where_scope;
where_scope.AddParent(&enclosing_scope);