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:
Jon Ross-Perkins
2025-04-15 22:40:29 +00:00
committed by GitHub
parent 838417e358
commit 4923445e3a
64 changed files with 489 additions and 539 deletions
+13 -14
View File
@@ -100,7 +100,7 @@ auto HandleParseNode(Context& context, Parse::DefaultSelfImplAsId node_id)
CARBON_DIAGNOSTIC(ImplAsOutsideClass, Error,
"`impl as` can only be used in a class");
context.emitter().Emit(node_id, ImplAsOutsideClass);
self_type_id = SemIR::ErrorInst::SingletonTypeId;
self_type_id = SemIR::ErrorInst::TypeId;
}
// Build the implicit access to the enclosing `Self`.
@@ -111,7 +111,7 @@ auto HandleParseNode(Context& context, Parse::DefaultSelfImplAsId node_id)
// handling of the `Self` expression.
auto self_inst_id = AddTypeInst(
context, node_id,
SemIR::NameRef{.type_id = SemIR::TypeType::SingletonTypeId,
SemIR::NameRef{.type_id = SemIR::TypeType::TypeId,
.name_id = SemIR::NameId::SelfType,
.value_id = context.types().GetInstId(self_type_id)});
@@ -184,7 +184,7 @@ static auto ExtendImpl(Context& context, Parse::NodeId extend_node,
}
const auto& impl = context.impls().Get(impl_id);
if (impl.witness_id == SemIR::ErrorInst::SingletonInstId) {
if (impl.witness_id == SemIR::ErrorInst::InstId) {
parent_scope.set_has_error();
} else {
bool is_complete = RequireCompleteType(
@@ -227,8 +227,7 @@ static auto PopImplIntroducerAndParamsAsNameComponent(
CARBON_CHECK(call_params_id == SemIR::InstBlockId::Empty ||
llvm::all_of(context.inst_blocks().Get(call_params_id),
[](SemIR::InstId inst_id) {
return inst_id ==
SemIR::ErrorInst::SingletonInstId;
return inst_id == SemIR::ErrorInst::InstId;
}));
}
@@ -321,7 +320,7 @@ static auto CheckConstraintIsInterface(Context& context,
SemIR::TypeInstId constraint_id)
-> SemIR::SpecificInterface {
auto facet_type_id = context.types().GetTypeIdForTypeInstId(constraint_id);
if (facet_type_id == SemIR::ErrorInst::SingletonTypeId) {
if (facet_type_id == SemIR::ErrorInst::TypeId) {
return SemIR::SpecificInterface::None;
}
auto facet_type = context.types().TryGetAs<SemIR::FacetType>(facet_type_id);
@@ -415,7 +414,7 @@ static auto BuildImplDecl(Context& context, Parse::AnyImplDeclId node_id,
impl_info.witness_id =
ImplWitnessForDeclaration(context, impl_info, is_definition);
} else {
impl_info.witness_id = SemIR::ErrorInst::SingletonInstId;
impl_info.witness_id = SemIR::ErrorInst::InstId;
// TODO: We might also want to mark that the name scope for the impl has
// an error -- at least once we start making name lookups within the impl
// also look into the facet (eg, so you can name associated constants from
@@ -435,14 +434,14 @@ static auto BuildImplDecl(Context& context, Parse::AnyImplDeclId node_id,
if (name.implicit_param_patterns_id.has_value()) {
for (auto inst_id :
context.inst_blocks().Get(name.implicit_param_patterns_id)) {
if (inst_id == SemIR::ErrorInst::SingletonInstId) {
if (inst_id == SemIR::ErrorInst::InstId) {
has_error_in_implicit_pattern = true;
break;
}
}
}
if (impl_info.generic_id.has_value() && !has_error_in_implicit_pattern &&
impl_info.witness_id != SemIR::ErrorInst::SingletonInstId) {
impl_info.witness_id != SemIR::ErrorInst::InstId) {
context.inst_block_stack().Push();
auto deduced_specific_id = DeduceImplArguments(
context, node_id,
@@ -471,7 +470,7 @@ static auto BuildImplDecl(Context& context, Parse::AnyImplDeclId node_id,
// diagnostics.
FillImplWitnessWithErrors(context, impl_info);
context.impls().Get(impl_decl.impl_id).witness_id =
SemIR::ErrorInst::SingletonInstId;
SemIR::ErrorInst::InstId;
}
}
} else {
@@ -484,13 +483,13 @@ static auto BuildImplDecl(Context& context, Parse::AnyImplDeclId node_id,
ReplaceInstBeforeConstantUse(context, impl_decl_id, impl_decl);
// For an `extend impl` declaration, mark the impl as extending this `impl`.
if (self_type_id != SemIR::ErrorInst::SingletonTypeId &&
if (self_type_id != SemIR::ErrorInst::TypeId &&
introducer.modifier_set.HasAnyOf(KeywordModifierSet::Extend)) {
auto extend_node = introducer.modifier_node_id(ModifierOrder::Decl);
if (impl_info.generic_id.has_value()) {
constraint_type_inst_id = AddTypeInst<SemIR::SpecificConstant>(
context, context.insts().GetLocId(constraint_type_inst_id),
{.type_id = SemIR::TypeType::SingletonTypeId,
{.type_id = SemIR::TypeType::TypeId,
.inst_id = constraint_type_inst_id,
.specific_id =
context.generics().GetSelfSpecific(impl_info.generic_id)});
@@ -501,7 +500,7 @@ static auto BuildImplDecl(Context& context, Parse::AnyImplDeclId node_id,
// Don't allow the invalid impl to be used.
FillImplWitnessWithErrors(context, impl_info);
context.impls().Get(impl_decl.impl_id).witness_id =
SemIR::ErrorInst::SingletonInstId;
SemIR::ErrorInst::InstId;
}
}
@@ -510,7 +509,7 @@ static auto BuildImplDecl(Context& context, Parse::AnyImplDeclId node_id,
// there is an error that would prevent the impl from being legal to define.
if (!is_definition && !invalid_redeclaration &&
context.impls().Get(impl_decl.impl_id).witness_id !=
SemIR::ErrorInst::SingletonInstId) {
SemIR::ErrorInst::InstId) {
context.definitions_required_by_decl().push_back(impl_decl_id);
}