mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-05 22:02:55 +01:00
Use GetConstantValueInSpecific to get the impl's specific interface after deduction (#7584)
During impl lookup, for each (generic) impl candidate, we form a specific for that impl by deducing its generic arguments. Then we compare the query interface against the impl's specific interface. That comparison needs the deduced arguments applied to the impl's specific interface. Previously we were doing this by getting the impl's constraint facet type with the impl's specific applied (via `GetConstantValueInSpecific()`) and then identifying that facet type with the impl's deduced self. Identify is a fairly expensive operation. It runs subst, trying to replace `.Self` references. It walks named constraints. It collects require declarations. We're looking at making it do _more_ in the future too, including rewrite constraint resolution and collecting rewrite and same-type constraints. For this reason we have a cache to make it cheap on the second run, but it's still a very heavyweight operation to involve in impl lookup, when all we want is to apply the impl's specific to its target interface. We almost have all the information we need to avoid the identification step. We have the impl's specific after deduction. And we have the SpecificInterface that the impl is targeting in the `Impl` struct. When we form the specific for the impl itself, we resolve the declaration block and form new constant values for all instructions in there, but that does not cover the SpecificInterface that we're storing in the `Impl` struct. So we add a new instruction to the impl's eval block, which will be symbolic when the impl is generic and the target interface depends on a generic parameter. And we store the `InstId` in the `Impl` struct. This allows us to gets its constant value later with the impl's specific applied. From that constant value we can then pull out the SpecificInterface that the impl is targeting.
This commit is contained in:
@@ -252,6 +252,28 @@ static auto BuildImplDecl(Context& context, Parse::AnyImplDeclId node_id,
|
||||
full_constraint_type_inst_id = SemIR::ErrorInst::TypeInstId;
|
||||
}
|
||||
|
||||
// Store an instruction in the decl's eval block that contains the target
|
||||
// interface's specific, whose constant value will be updated when specifics
|
||||
// are applied to the impl.
|
||||
//
|
||||
// We can use ImplSelfWitness for this because it contains a
|
||||
// SpecificInterfaceId operand, and it has a constant_kind of `Always` so it
|
||||
// never evaluates to some other type of inst.
|
||||
//
|
||||
// TODO: We could avoid the extra indirection through a SpecificInterfaceId if
|
||||
// we introduced a new instruction with a SpecificId operand instead of
|
||||
// reusing ImplSelfWitness for this.
|
||||
auto interface_inst_id =
|
||||
specific_interface.interface_id.has_value()
|
||||
? AddInst<SemIR::ImplSelfWitness>(
|
||||
context, node_id,
|
||||
{.type_id =
|
||||
GetSingletonType(context, SemIR::WitnessType::TypeInstId),
|
||||
.period_self = self_type_inst_id,
|
||||
.specific_interface_id =
|
||||
context.specific_interfaces().Add(specific_interface)})
|
||||
: SemIR::ErrorInst::InstId;
|
||||
|
||||
// Strip off anything on the RHS of `where`, as they are not part of the
|
||||
// constraint being implemented, they just represent requirements that must be
|
||||
// met when the impl is defined. This drops any `.Self` references from the
|
||||
@@ -282,7 +304,8 @@ static auto BuildImplDecl(Context& context, Parse::AnyImplDeclId node_id,
|
||||
.is_final = is_final,
|
||||
.self_id = self_type_inst_id,
|
||||
.constraint_id = extend_constraint_type_inst_id,
|
||||
.interface = specific_interface}};
|
||||
.interface = specific_interface,
|
||||
.interface_inst_id = interface_inst_id}};
|
||||
if (has_definition) {
|
||||
impl.definition_id = impl_decl_id;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user