diff --git a/toolchain/check/handle_impl.cpp b/toolchain/check/handle_impl.cpp index 15b899ee6a9c..9294707b266e 100644 --- a/toolchain/check/handle_impl.cpp +++ b/toolchain/check/handle_impl.cpp @@ -204,13 +204,18 @@ static auto PopImplIntroducerAndParamsAsNameComponent( .pattern_block_id = pattern_block_id}; } +struct BuildImplDeclResult { + SemIR::ImplId impl_id; + SemIR::InstId impl_decl_id; + SemIR::TypeInstId full_constraint_type_inst_id; + SemIR::IdentifiedFacetTypeId identified_id; +}; + // Build an ImplDecl describing the signature of an impl. This handles the // common logic shared by impl forward declarations and impl definitions. It // also sets the `definition_id` on the Impl structure. static auto BuildImplDecl(Context& context, Parse::AnyImplDeclId node_id, - bool has_definition) - -> std::tuple { + bool has_definition) -> BuildImplDeclResult { auto [constraint_node, constraint_id] = context.node_stack().PopExprWithNodeId(); auto [self_type_node, self_type_inst_id] = diff --git a/toolchain/check/handle_require.cpp b/toolchain/check/handle_require.cpp index a06d40c36fab..7fe593e36f70 100644 --- a/toolchain/check/handle_require.cpp +++ b/toolchain/check/handle_require.cpp @@ -245,7 +245,7 @@ static auto ValidateRequire(Context& context, SemIR::LocId full_require_loc_id, auto identified_facet_type_id = RequireIdentifiedFacetType( context, constraint_loc_id, self_type_id.AsConstantId(), - context.types().GetTypeInstId(constraint_type_id), [&](auto& builder) { + constraint_type_id, [&](auto& builder) { CARBON_DIAGNOSTIC( RequireImplsUnidentifiedFacetType, Context, "facet type {0} cannot be identified in `require` declaration", diff --git a/toolchain/check/handle_where.cpp b/toolchain/check/handle_where.cpp index 85f4b29d602c..197a2b5eb0c4 100644 --- a/toolchain/check/handle_where.cpp +++ b/toolchain/check/handle_where.cpp @@ -95,8 +95,7 @@ auto HandleParseNode(Context& context, Parse::WhereOperandId node_id) -> bool { // lookups into that interface with `.Self` so we identify it with `.Self`. auto identified_id = TryToIdentifyFacetType( context, node_id, context.constant_values().Get(period_self), - context.types().GetTypeInstId(period_self_type_id), - /*allow_partially_identified=*/false); + period_self_type_id, /*allow_partially_identified=*/false); if (identified_id.has_value()) { const auto& identified = context.identified_facet_types().Get(identified_id); diff --git a/toolchain/check/impl.cpp b/toolchain/check/impl.cpp index e70fe79faae4..6b9d9d23a0a4 100644 --- a/toolchain/check/impl.cpp +++ b/toolchain/check/impl.cpp @@ -995,7 +995,7 @@ auto CheckConstraintIsInterface(Context& context, SemIR::LocId loc_id, auto identified_id = RequireIdentifiedFacetType( context, SemIR::LocId(full_constraint_id), context.constant_values().Get(self_id), - context.constant_values().GetConstantTypeInstId(full_constraint_id), + context.types().GetTypeIdForTypeInstId(full_constraint_id), [&](auto& builder) { CARBON_DIAGNOSTIC(ImplOfUnidentifiedFacetType, Context, "facet type {0} cannot be identified in `impl as`", diff --git a/toolchain/check/impl_lookup.cpp b/toolchain/check/impl_lookup.cpp index 0a4bdb9a77cd..f1a61c9751ff 100644 --- a/toolchain/check/impl_lookup.cpp +++ b/toolchain/check/impl_lookup.cpp @@ -228,15 +228,16 @@ static auto GetRequiredImplsFromConstraint( SemIR::ConstantId query_self_const_id, SemIR::ConstantId query_facet_type_const_id, bool diagnose) -> std::optional> { - auto facet_type_inst_id = - context.types().GetTypeInstIdForTypeConstantId(query_facet_type_const_id); auto identified_id = RequireIdentifiedFacetType( - context, loc_id, query_self_const_id, facet_type_inst_id, + context, loc_id, query_self_const_id, + context.types().GetTypeIdForTypeConstantId(query_facet_type_const_id), [&](auto& builder) { CARBON_DIAGNOSTIC(ImplLookupInUnidentifiedFacetType, Context, - "facet type {0} can not be identified", InstIdAsType); + "facet type {0} can not be identified", + SemIR::TypeId); builder.Context(loc_id, ImplLookupInUnidentifiedFacetType, - facet_type_inst_id); + context.types().GetTypeIdForTypeConstantId( + query_facet_type_const_id)); }, diagnose); if (!identified_id.has_value()) { @@ -372,8 +373,7 @@ static auto CollectFacetWitnessSources( context.impl_lookup_no_symbolic_final_lookups(); ++no_symbolic_final_lookups; auto identified_id = TryToIdentifyFacetType( - context, loc_id, facet_const_id, - context.types().GetTypeInstId(type_id), allow_partially_identified); + context, loc_id, facet_const_id, type_id, allow_partially_identified); --no_symbolic_final_lookups; if (identified_id.has_value()) { @@ -452,7 +452,7 @@ static auto CollectFacetWitnessSources( // always have a FacetType in `facet_type_const_id`. auto identified_id = TryToIdentifyFacetType( context, loc_id, canon_self_const_id, - context.types().GetTypeInstIdForTypeConstantId(facet_type_const_id), + context.types().GetTypeIdForTypeConstantId(facet_type_const_id), /*allow_partially_identified=*/true); if (identified_id.has_value()) { witnesses.push_back({.facet_const_id = canon_self_const_id, @@ -1353,14 +1353,12 @@ auto MakeWitnessesForPeriodSelfTypeWithoutLookup(Context& context, context.constant_values().GetInst(period_self).type_id(); auto identified_period_self_type_id = RequireIdentifiedFacetType( - context, loc_id, facet_value, - context.types().GetTypeInstId(period_self_type_id), - [&](auto& /*builder*/) { - // The facet type of `.Self` may refer to generic interfaces that use - // `.Self` in their arguments. And when `.Self` is replaced by - // `facet_value`, we may fail with a monomorphization error. We pass it - // along without adding additional context. - }); + context, loc_id, facet_value, period_self_type_id, + // The facet type of `.Self` may refer to generic interfaces that use + // `.Self` in their arguments. And when `.Self` is replaced by + // `facet_value`, we may fail with a monomorphization error. We pass it + // along without adding additional context. + [&](auto& /*builder*/) {}); if (!identified_period_self_type_id.has_value()) { return SemIR::InstBlockIdOrError::MakeError(); } diff --git a/toolchain/check/type_completion.cpp b/toolchain/check/type_completion.cpp index ca4af457a3a1..a8e480c7dd16 100644 --- a/toolchain/check/type_completion.cpp +++ b/toolchain/check/type_completion.cpp @@ -962,16 +962,17 @@ auto RequireConcreteType(Context& context, SemIR::TypeId type_id, // error is encountered or diagnosed. static auto IdentifyFacetType(Context& context, SemIR::LocId loc_id, SemIR::ConstantId initial_self_const_id, - SemIR::TypeInstId facet_type_inst_id, + SemIR::ConstantId facet_type_const_id, bool allow_partially_identified, bool diagnose) -> SemIR::IdentifiedFacetTypeId { - if (facet_type_inst_id == SemIR::ErrorInst::InstId) { + if (facet_type_const_id == SemIR::ErrorInst::ConstantId) { return SemIR::IdentifiedFacetTypeId::None; } - auto declared_facet_type_id = context.insts() - .GetAs(facet_type_inst_id) - .declared_facet_type_id; + auto declared_facet_type_id = + context.constant_values() + .GetInstAs(facet_type_const_id) + .declared_facet_type_id; // While partially identified facet types end up in the store of // IdentifiedFacetTypes, we don't try to construct a key to look for them @@ -992,7 +993,7 @@ static auto IdentifyFacetType(Context& context, SemIR::LocId loc_id, auto subst_id = SubstPeriodSelfInFacetType( context, loc_id, context.constant_values().GetInstId(initial_self_const_id), - context.types().GetAsTypeInstId(facet_type_inst_id)); + context.types().GetTypeInstIdForTypeConstantId(facet_type_const_id)); declared_facet_type_id = context.insts() .GetAs(subst_id) .declared_facet_type_id; @@ -1251,22 +1252,24 @@ static auto IdentifyFacetType(Context& context, SemIR::LocId loc_id, auto TryToIdentifyFacetType(Context& context, SemIR::LocId loc_id, SemIR::ConstantId self_const_id, - SemIR::TypeInstId facet_type_inst_id, + SemIR::TypeId facet_type_type_id, bool allow_partially_identified) -> SemIR::IdentifiedFacetTypeId { - return IdentifyFacetType(context, loc_id, self_const_id, facet_type_inst_id, + return IdentifyFacetType(context, loc_id, self_const_id, + facet_type_type_id.AsConstantId(), allow_partially_identified, /*diagnose=*/false); } auto RequireIdentifiedFacetType(Context& context, SemIR::LocId loc_id, SemIR::ConstantId self_const_id, - SemIR::TypeInstId facet_type_inst_id, + SemIR::TypeId facet_type_type_id, DiagnosticContextFn diagnostic_context, bool diagnose) -> SemIR::IdentifiedFacetTypeId { CARBON_CHECK(diagnostic_context); Diagnostics::ContextScope scope(&context.emitter(), diagnostic_context); - return IdentifyFacetType(context, loc_id, self_const_id, facet_type_inst_id, + return IdentifyFacetType(context, loc_id, self_const_id, + facet_type_type_id.AsConstantId(), /*allow_partially_identified=*/false, diagnose); } diff --git a/toolchain/check/type_completion.h b/toolchain/check/type_completion.h index cd26f0c9701b..b2009b5c528d 100644 --- a/toolchain/check/type_completion.h +++ b/toolchain/check/type_completion.h @@ -77,7 +77,7 @@ auto RequireConcreteType(Context& context, SemIR::TypeId type_id, // the middle of being defined. auto TryToIdentifyFacetType(Context& context, SemIR::LocId loc_id, SemIR::ConstantId self_const_id, - SemIR::TypeInstId facet_type_inst_id, + SemIR::TypeId facet_type_type_id, bool allow_partially_identified) -> SemIR::IdentifiedFacetTypeId; @@ -96,7 +96,7 @@ auto TryToIdentifyFacetType(Context& context, SemIR::LocId loc_id, // TODO: Remove `diagnose` and split into `TryIdentifyFacetType`. auto RequireIdentifiedFacetType(Context& context, SemIR::LocId loc_id, SemIR::ConstantId self_const_id, - SemIR::TypeInstId facet_type_inst_id, + SemIR::TypeId facet_type_type_id, DiagnosticContextFn diagnostic_context, bool diagnose = true) -> SemIR::IdentifiedFacetTypeId;