mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 22:02:23 +01:00
In #7436 we stopped substituting `.Self` when collecting witnesses out of a facet type. While this was correct, it did not capture all the cases that need to avoid substituting `.Self`. And it poisoned the `IdentifiedFacetType` cache by not replacing `.Self` but storing the result in the cache. This led to incoherent behaviour, where the result of an impl lookup would change depending on which ones had been done previously. Now we use a flag to track for each `.Self` if we're currently type-checking inside the scope where it was introduced in a facet type. While inside that scope, identify should not replace the `.Self`. Any use of it should remain as-is since we don't yet know what value will replace it. We call this state "frozen" since it should not be modified by identify. This requires a substitution step when we leave the scope that introduced the `.Self`, to remove the flag. The flag is set in the `EntityName` of the `SymbolicBinding`, and is part of the canonical value, since `.Self` can become part of types, which are constants, and the flag needs to follow it for correct behaviour. We also have to ensure the flag is the same when doing comparison with constants from inside a facet type and constants from outside. For instance in `(Z where .Z1 = ()) where .Z2 = .Z1`, when we arrive at the second `.Z1` its `.Self` will be frozen, while the `.Z1 = ()` contains a non-frozen `.Self`. So we add the frozen flag to the first when storing it in `where_stack` in order to compare the constant values of the two `.Z1`. The `WhereExpr` requirement inst kinds now have an `InstConstantKind` of `AlwaysUnique` instead of `Never`. This allows us to add them to the usual InstBlocks, and in an `eval fn` body they have a constant value, so eval does not fail when trying to call that function. We have to be careful to not consider `AlwaysUnique` as being actually concrete though, since their constant value erases `.Self`-dependence. This allows us to stop special casing them when thawing the requirements block in a `WhereExpr`, and we can just thaw each `InstId` in the block in a straightforward manner. We add the new flag to the instruction's fingerprint and name in formatted semir.