Introduce typed-inst accessors for ConstantValueStore (#6980)

Add `InstIs`, `GetInstAs`, and `TryGetInstAs` which act on the
underlying constant instruction in a constant value, to save an explicit
call to `GetInstId`.

```carbon
context.insts().GetAs<InstT>(context.constant_values().GetInstId(const_id))
```
can now be written as simply
```carbon
context.constant_values().GetInstAs<InstT>(const_id)
```

For future work, we might provide `GetInst()` so that
`context.insts().Get(context.constant_values().GetInstId(const_id)` can
be shortened also.
This commit is contained in:
Dana Jansens
2026-03-27 21:41:04 +00:00
committed by GitHub
parent 899e54de36
commit 5503f643c6
15 changed files with 99 additions and 100 deletions
+3 -6
View File
@@ -729,11 +729,8 @@ auto CheckRequireDeclsSatisfied(Context& context, SemIR::LocId loc_id,
// requires LookupImplWitness to return a partial result, or take a
// diagnostic lambda or something.
if (!result.has_value()) {
auto facet_type_inst_id =
context.constant_values().GetInstId(facet_type_const_id);
if (!result.has_error_value() &&
facet_type_inst_id != SemIR::ErrorInst::InstId) {
facet_type_const_id != SemIR::ErrorInst::ConstantId) {
CARBON_DIAGNOSTIC(RequireImplsNotImplemented, Error,
"interface `{0}` being implemented requires that {1} "
"implements {2}",
@@ -742,8 +739,8 @@ auto CheckRequireDeclsSatisfied(Context& context, SemIR::LocId loc_id,
context.emitter().Emit(
loc_id, RequireImplsNotImplemented, impl.interface,
context.types().GetTypeIdForTypeConstantId(self_const_id),
context.insts()
.GetAs<SemIR::FacetType>(facet_type_inst_id)
context.constant_values()
.GetInstAs<SemIR::FacetType>(facet_type_const_id)
.facet_type_id);
}
}