Files
carbon-lang/toolchain/sem_ir/generic.cpp
Dana Jansens 24766e213e Defer evaluation of witnesses substituted while identifying a facet type (#7930)
When identifying a facet type, defer evaluation of LookupImplWitnesses
by redirecting them to evaluate into IdentifiedWitness. Then the caller
of identify will need to substitute the IdentifiedWitnesses, either with
witnesses of their own, or with LookupImplWitness to cause a lookup
operation.

This allows:
- An `impl as` on a named constraint can subst the witnesses in
designators from the named constraint with a witness to the impl itself.
This prevents them from looking for and finding some other overlapping
impl. (This PR)
- Impl lookup to collect rewrite/equality requirements from all named
constraints and then use the impls it has already found to evaluate any
designators in those requirements instead of performing impl lookup
again (creating a cycle). (Done in
https://github.com/carbon-language/carbon-lang/pull/7944)
- Similarly, an `impl as` can collect impls and equality requirements
from all named constraints and verify them correctly by replacing
witnesses that should refer to the impl with the correct witness instead
of performing a lookup and finding an overlapping impl. (Done in
https://github.com/carbon-language/carbon-lang/pull/7937)
- ImplWitnessAccess to be able to find rewrites from a named constraint,
not just from the initial DeclaredFacetType. (TBD)

To do this we need to introduce the concept of "identified" specifics.
In order to identify a facet type, we must construct a specific for each
require decl in a named constraint. These specifics will have
IdentifiedWitnesses inside them, but their args would otherwise match a
specific for the require decl constructed in another context. To avoid
polluting the canonical specific namespace, we introduce a second
namespace of specifics that are "identified" since they are created by
identifying.

As we have introduced a new global state that modifies eval behaviour,
we must disable that state when resolving a specific (as per
https://github.com/carbon-language/carbon-lang/pull/7923). But inside an
"identified" specific, we preserve the behaviour so that the witnesses
modified by identify (through SubstPeriodSelf or through evaluating a
require specific) have their evaluation deferred.
2026-10-14 14:12:56 +00:00

164 lines
6.6 KiB
C++

// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#include "toolchain/sem_ir/generic.h"
#include "toolchain/base/value_store_impl.h"
#include "toolchain/sem_ir/file.h"
#include "toolchain/sem_ir/typed_insts.h"
namespace Carbon::SemIR {
class SpecificStore::KeyContext : public TranslatingKeyContext<KeyContext> {
public:
// A lookup key for a specific.
struct Key {
GenericId generic_id;
InstBlockId args_id;
// Whether a specific was created by identifying a facet type, which changes
// the behaviour of eval while resolving the specific. So this is part of
// the specific's key in order to differentiate from specifics with the same
// arguments but which are not created by identifying a facet type, and thus
// may have different evaluated constants.
int32_t identified;
friend auto operator==(const Key&, const Key&) -> bool = default;
};
explicit KeyContext(const ValueStore* specifics) : specifics_(specifics) {}
auto TranslateKey(SpecificId id) const -> Key {
const auto& specific = specifics_->Get(id);
return {.generic_id = specific.generic_id,
.args_id = specific.args_id,
.identified = specific.identified};
}
private:
const ValueStore* specifics_;
};
auto SpecificStore::GetOrAdd(GenericId generic_id, InstBlockId args_id,
bool identified) -> SpecificId {
CARBON_CHECK(generic_id.has_value());
return lookup_table_
.Insert(
KeyContext::Key{.generic_id = generic_id,
.args_id = args_id,
.identified = identified},
[&] {
return specifics_.Add({.generic_id = generic_id,
.args_id = args_id,
.identified = identified});
},
KeyContext(&specifics_))
.key();
}
auto SpecificStore::CollectMemUsage(MemUsage& mem_usage,
llvm::StringRef label) const -> void {
mem_usage.Collect(MemUsage::ConcatLabel(label, "specifics_"), specifics_);
mem_usage.Collect(MemUsage::ConcatLabel(label, "lookup_table_"),
lookup_table_, KeyContext(&specifics_));
}
static auto GetConstantInSpecific(const File& specific_ir,
SpecificId specific_id, const File& const_ir,
ConstantId const_id)
-> std::pair<const File*, ConstantId> {
if (!const_id.is_symbolic()) {
// The constant does not depend on a generic parameter.
return {&const_ir, const_id};
}
const auto& symbolic =
const_ir.constant_values().GetSymbolicConstant(const_id);
if (!symbolic.generic_id.has_value()) {
// The constant is an unattached symbolic constant, not associated with some
// particular generic.
return {&const_ir, const_id};
}
if (!specific_id.has_value()) {
// We have a generic constant but no specific. We treat this as a request
// for the value that should be used within the generic itself, which is the
// unattached constant.
return {&const_ir, const_ir.constant_values().Get(symbolic.inst_id)};
}
const auto& specific = specific_ir.specifics().Get(specific_id);
// TODO: Enforce this check even if the generic and specific are in different
// IRs.
CARBON_CHECK(
&specific_ir != &const_ir || specific.generic_id == symbolic.generic_id,
"Given a specific for the wrong generic");
auto value_block_id = specific.GetValueBlock(symbolic.index.region());
if (!value_block_id.has_value()) {
// For the self specific, we can see queries before the definition is
// resolved. Return the unattached constant value.
CARBON_CHECK(
specific_ir.generics().GetSelfSpecific(
specific_ir.specifics().Get(specific_id).generic_id) == specific_id,
"Queried {0} {1} in {2} for generic {3} before it was resolved.",
symbolic.index, const_ir.insts().Get(symbolic.inst_id), specific_id,
specific_ir.insts().Get(
specific_ir.generics().Get(specific.generic_id).decl_id));
// TODO: Make sure this is the same value that we put in the self specific
// when it's resolved. Consider not building value blocks for a self
// specific.
return {&const_ir, const_ir.constant_values().Get(symbolic.inst_id)};
}
// TODO: Distinguish between parts of the instruction block that we've not
// reached yet during eval and values that evaluated successfully to
// ConstantId::NotConstant.
auto value_id =
specific_ir.inst_blocks().Get(value_block_id)[symbolic.index.index()];
return {&specific_ir, value_id.has_value()
? specific_ir.constant_values().Get(value_id)
: SemIR::ConstantId::NotConstant};
}
auto GetConstantValueInSpecific(const File& sem_ir, SpecificId specific_id,
InstId inst_id) -> ConstantId {
return GetConstantInSpecific(sem_ir, specific_id, sem_ir,
sem_ir.constant_values().GetAttached(inst_id))
.second;
}
auto GetConstantValueInSpecific(const File& specific_ir, SpecificId specific_id,
const File& inst_ir, InstId inst_id)
-> std::pair<const File*, ConstantId> {
return GetConstantInSpecific(specific_ir, specific_id, inst_ir,
inst_ir.constant_values().GetAttached(inst_id));
}
auto GetTypeOfInstInSpecific(const File& sem_ir, SpecificId specific_id,
InstId inst_id) -> TypeId {
auto type_id = sem_ir.insts().GetAttachedType(inst_id);
auto const_id = sem_ir.types().GetConstantId(type_id);
auto [_, specific_const_id] =
GetConstantInSpecific(sem_ir, specific_id, sem_ir, const_id);
return TypeId::ForTypeConstant(specific_const_id);
}
auto GetTypeOfInstInSpecific(const File& specific_ir, SpecificId specific_id,
const File& inst_ir, InstId inst_id)
-> std::pair<const File*, TypeId> {
auto type_id = inst_ir.insts().GetAttachedType(inst_id);
auto const_id = inst_ir.types().GetConstantId(type_id);
auto [result_ir, result_const_id] =
GetConstantInSpecific(specific_ir, specific_id, inst_ir, const_id);
return {result_ir, TypeId::ForTypeConstant(result_const_id)};
}
} // namespace Carbon::SemIR
namespace Carbon {
template class ValueStore<SemIR::GenericId, SemIR::Generic,
Tag<SemIR::CheckIRId>>;
template class ValueStore<SemIR::SpecificId, SemIR::Specific,
Tag<SemIR::CheckIRId>>;
} // namespace Carbon