mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-05 22:02:55 +01:00
Track and don't replace active .Self (#7443)
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.
This commit is contained in:
@@ -29,6 +29,18 @@ static auto DumpNameIfValid(const File& file, NameId name_id) -> std::string {
|
||||
return out.TakeStr();
|
||||
}
|
||||
|
||||
static auto DumpNameOfEntityName(const File& file,
|
||||
const EntityName& entity_name) -> std::string {
|
||||
RawStringOstream out;
|
||||
out << " `";
|
||||
out << file.names().GetFormatted(entity_name.name_id);
|
||||
if (entity_name.is_frozen_period_self) {
|
||||
out << "[frozen]";
|
||||
}
|
||||
out << "`";
|
||||
return out.TakeStr();
|
||||
}
|
||||
|
||||
static auto DumpLocSummary(const File& file, LocId loc_id) -> std::string {
|
||||
RawStringOstream out;
|
||||
// TODO: If the canonical location is None but the original is an InstId,
|
||||
@@ -223,7 +235,7 @@ LLVM_DUMP_METHOD auto Dump(const File& file, EntityNameId entity_name_id)
|
||||
out << entity_name_id;
|
||||
if (entity_name_id.has_value()) {
|
||||
auto entity_name = file.entity_names().Get(entity_name_id);
|
||||
out << ": " << entity_name << DumpNameIfValid(file, entity_name.name_id);
|
||||
out << ": " << entity_name << DumpNameOfEntityName(file, entity_name);
|
||||
}
|
||||
return out.TakeStr();
|
||||
}
|
||||
@@ -384,8 +396,7 @@ static auto DumpInstCommonDetails(const File& file, const Inst& inst,
|
||||
if (inst.arg0_and_kind().kind() == IdKind::For<EntityNameId>) {
|
||||
auto entity_name_id = EntityNameId(inst.arg0());
|
||||
out << "\n - name:"
|
||||
<< DumpNameIfValid(file,
|
||||
file.entity_names().Get(entity_name_id).name_id);
|
||||
<< DumpNameOfEntityName(file, file.entity_names().Get(entity_name_id));
|
||||
}
|
||||
|
||||
if (inst.type_id().has_value()) {
|
||||
|
||||
Reference in New Issue
Block a user