mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-11-15 14:48:10 +00:00
A `where` expression can have symbolic references to `.Self` inside it, but those references do not represent symbolic instructions that should be part of an enclosing generic eval block. They are internal details of the facet type, and as such they are PeriodSelfSymbolic, not Symbolic. The WhereExpr evaluation is careful to produce a Symbolic constant value only if it has symbolic dependencies on the LHS of the `where`, which avoids leaking the symbolic nature of `.Self` into he facet type's constant value. But until now, those PeriodSelfSymbolic insts could still leak into the enclosing eval block if they ended up in an operand of a Call expression. A compile-time Call expr evaluates to a TemplateSymbolic phase constant if it's not concrete, which includes PeriodSelfSymbolic, because we don't want to evaluate the Call expr until the `.Self` gets replaced during identify. But that means the Call constant value, and its operands including PeriodSelfSymbolic constants, all end up in the eval block. We fix this by making Call evaluate to a PeriodSelfSymbolic phase if its input phase is PeriodSelfSymbolic. This keeps the RHS of the `where` expression self-contained. If the facet type is concrete, then it doesn't add any instructions to the enclosing eval block (which may not exist at all in a non-generic context). Regardless, when we identify the facet type and replace `.Self`, the dependent Call expression will be rebuilt as well and it will evaluate as appropriate. We also enclose the `where` expression in its own generic scope. This means that instructions which are not part of the facet type do not end up in an eval block. They may be symbolic instructions, and they will not get updated by a specific when it's applied the enclosing generic anymore. But since they are not part of the facet type their substitution is not required. The new test had an incorrectly elided SpecificImplFunction instruction in the resulting semir, as SubstPeriodSelf was using the location of the input canonical instructions as the location for the results which resulted in an instruction without a location. We fix that by using `loc_id_` in the subst rebuild. Formatting now saw the initialization of a value from an `eval fn` call to a function in a witness table, so it needed to be taught to look through ImplWitnessAssociatedConstant (which is what we find in the witness table).