mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-05 22:02:55 +01:00
Drop Singleton from ErrorInst::SingletonInstId and similar (#5304)
We frequently want to operate on singletons. Per discussion, drop `Singleton` to make the code shorter. This started off as wanting to write `inst_id.is_error()`, but the dependency relationship between ids.h and singleton_insts.h would require some kind of delayed evaluation to allow the implementation to remain in headers (which I suspect is helpful to have for inlining). I could have added something like `IsErrorInst`, forward declared in ids.h and defined in singleton_insts.h (which would always be included by typed_insts.h), but the template approach felt like a decent balance between (a) removing the boilerplate `::SingletonInstId`, (b) understandability, (c) still visually mirroring if we immediately return a singleton, and (d) flexibility for more than just `ErrorInst`. But TBH I'd probably still have written `is_error()` if it didn't require addressing the cross-header cycle. Then I tried `SemIR::InstId::Is<SemIR::ErrorInst>`, which generally worked with types but generated the complaint that it didn't shorten *all* singleton uses. So pulling back on `::Is`, and instead just dropping `Singleton`.
This commit is contained in:
+17
-18
@@ -56,7 +56,7 @@ static auto CheckAssociatedFunctionImplementation(
|
||||
interface_function_type.function_id);
|
||||
builder.Emit();
|
||||
|
||||
return SemIR::ErrorInst::SingletonInstId;
|
||||
return SemIR::ErrorInst::InstId;
|
||||
}
|
||||
|
||||
auto impl_enclosing_specific_id =
|
||||
@@ -80,7 +80,7 @@ static auto CheckAssociatedFunctionImplementation(
|
||||
context.functions().Get(interface_function_type.function_id),
|
||||
interface_function_specific_id, /*check_syntax=*/false,
|
||||
/*check_self=*/true)) {
|
||||
return SemIR::ErrorInst::SingletonInstId;
|
||||
return SemIR::ErrorInst::InstId;
|
||||
}
|
||||
return impl_decl_id;
|
||||
}
|
||||
@@ -91,9 +91,9 @@ auto ImplWitnessForDeclaration(Context& context, const SemIR::Impl& impl,
|
||||
CARBON_CHECK(!impl.has_definition_started());
|
||||
|
||||
auto self_type_id = context.types().GetTypeIdForTypeInstId(impl.self_id);
|
||||
if (self_type_id == SemIR::ErrorInst::SingletonTypeId) {
|
||||
if (self_type_id == SemIR::ErrorInst::TypeId) {
|
||||
// When 'impl as' is invalid, the self type is an error.
|
||||
return SemIR::ErrorInst::SingletonInstId;
|
||||
return SemIR::ErrorInst::InstId;
|
||||
}
|
||||
|
||||
return InitialFacetTypeImplWitness(
|
||||
@@ -105,7 +105,7 @@ auto ImplWitnessForDeclaration(Context& context, const SemIR::Impl& impl,
|
||||
auto ImplWitnessStartDefinition(Context& context, SemIR::Impl& impl) -> void {
|
||||
CARBON_CHECK(impl.is_being_defined());
|
||||
CARBON_CHECK(impl.witness_id.has_value());
|
||||
if (impl.witness_id == SemIR::ErrorInst::SingletonInstId) {
|
||||
if (impl.witness_id == SemIR::ErrorInst::InstId) {
|
||||
return;
|
||||
}
|
||||
auto witness = context.insts().GetAs<SemIR::ImplWitness>(impl.witness_id);
|
||||
@@ -143,8 +143,7 @@ auto ImplWitnessStartDefinition(Context& context, SemIR::Impl& impl) -> void {
|
||||
CARBON_CHECK(decl_id.has_value(), "Non-constant associated entity");
|
||||
if (auto decl =
|
||||
context.insts().TryGetAs<SemIR::AssociatedConstantDecl>(decl_id)) {
|
||||
if (witness_value ==
|
||||
SemIR::ImplWitnessTablePlaceholder::SingletonInstId) {
|
||||
if (witness_value == SemIR::ImplWitnessTablePlaceholder::InstId) {
|
||||
CARBON_DIAGNOSTIC(ImplAssociatedConstantNeedsValue, Error,
|
||||
"associated constant {0} not given a value in impl "
|
||||
"of interface {1}",
|
||||
@@ -160,7 +159,7 @@ auto ImplWitnessStartDefinition(Context& context, SemIR::Impl& impl) -> void {
|
||||
.Note(assoc_entity, AssociatedConstantHere)
|
||||
.Emit();
|
||||
|
||||
witness_value = SemIR::ErrorInst::SingletonInstId;
|
||||
witness_value = SemIR::ErrorInst::InstId;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -171,7 +170,7 @@ auto ImplWitnessStartDefinition(Context& context, SemIR::Impl& impl) -> void {
|
||||
auto FinishImplWitness(Context& context, SemIR::Impl& impl) -> void {
|
||||
CARBON_CHECK(impl.is_being_defined());
|
||||
CARBON_CHECK(impl.witness_id.has_value());
|
||||
if (impl.witness_id == SemIR::ErrorInst::SingletonInstId) {
|
||||
if (impl.witness_id == SemIR::ErrorInst::InstId) {
|
||||
return;
|
||||
}
|
||||
auto witness = context.insts().GetAs<SemIR::ImplWitness>(impl.witness_id);
|
||||
@@ -195,8 +194,8 @@ auto FinishImplWitness(Context& context, SemIR::Impl& impl) -> void {
|
||||
auto decl = context.insts().Get(decl_id);
|
||||
CARBON_KIND_SWITCH(decl) {
|
||||
case CARBON_KIND(SemIR::StructValue struct_value): {
|
||||
if (struct_value.type_id == SemIR::ErrorInst::SingletonTypeId) {
|
||||
witness_value = SemIR::ErrorInst::SingletonInstId;
|
||||
if (struct_value.type_id == SemIR::ErrorInst::TypeId) {
|
||||
witness_value = SemIR::ErrorInst::InstId;
|
||||
break;
|
||||
}
|
||||
auto type_inst = context.types().GetAsInst(struct_value.type_id);
|
||||
@@ -224,7 +223,7 @@ auto FinishImplWitness(Context& context, SemIR::Impl& impl) -> void {
|
||||
NoteAssociatedFunction(context, builder, fn_type->function_id);
|
||||
builder.Emit();
|
||||
|
||||
witness_value = SemIR::ErrorInst::SingletonInstId;
|
||||
witness_value = SemIR::ErrorInst::InstId;
|
||||
}
|
||||
break;
|
||||
}
|
||||
@@ -233,9 +232,9 @@ auto FinishImplWitness(Context& context, SemIR::Impl& impl) -> void {
|
||||
break;
|
||||
}
|
||||
default:
|
||||
CARBON_CHECK(decl_id == SemIR::ErrorInst::SingletonInstId,
|
||||
CARBON_CHECK(decl_id == SemIR::ErrorInst::InstId,
|
||||
"Unexpected kind of associated entity {0}", decl);
|
||||
witness_value = SemIR::ErrorInst::SingletonInstId;
|
||||
witness_value = SemIR::ErrorInst::InstId;
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -245,15 +244,15 @@ auto FinishImplWitness(Context& context, SemIR::Impl& impl) -> void {
|
||||
|
||||
auto FillImplWitnessWithErrors(Context& context, SemIR::Impl& impl) -> void {
|
||||
if (impl.witness_id.has_value() &&
|
||||
impl.witness_id != SemIR::ErrorInst::SingletonInstId) {
|
||||
impl.witness_id != SemIR::ErrorInst::InstId) {
|
||||
auto witness = context.insts().GetAs<SemIR::ImplWitness>(impl.witness_id);
|
||||
auto witness_table = context.insts().GetAs<SemIR::ImplWitnessTable>(
|
||||
witness.witness_table_id);
|
||||
auto witness_block =
|
||||
context.inst_blocks().GetMutable(witness_table.elements_id);
|
||||
for (auto& elem : witness_block) {
|
||||
if (elem == SemIR::ImplWitnessTablePlaceholder::SingletonInstId) {
|
||||
elem = SemIR::ErrorInst::SingletonInstId;
|
||||
if (elem == SemIR::ImplWitnessTablePlaceholder::InstId) {
|
||||
elem = SemIR::ErrorInst::InstId;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -261,7 +260,7 @@ auto FillImplWitnessWithErrors(Context& context, SemIR::Impl& impl) -> void {
|
||||
|
||||
auto AssignImplIdInWitness(Context& context, SemIR::ImplId impl_id,
|
||||
SemIR::InstId witness_id) -> void {
|
||||
if (witness_id == SemIR::ErrorInst::SingletonInstId) {
|
||||
if (witness_id == SemIR::ErrorInst::InstId) {
|
||||
return;
|
||||
}
|
||||
auto witness = context.insts().GetAs<SemIR::ImplWitness>(witness_id);
|
||||
|
||||
Reference in New Issue
Block a user