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
+7 -8
View File
@@ -221,8 +221,7 @@ auto HandleParseNode(Context& context, Parse::PostfixOperatorStarId node_id)
auto inner_type = ExprAsType(context, node_id, value_id);
AddInstAndPush<SemIR::PointerType>(
context, node_id,
{.type_id = SemIR::TypeType::SingletonTypeId,
.pointee_id = inner_type.inst_id});
{.type_id = SemIR::TypeType::TypeId, .pointee_id = inner_type.inst_id});
return true;
}
@@ -239,13 +238,13 @@ auto HandleParseNode(Context& context, Parse::PrefixOperatorAmpId node_id)
CARBON_DIAGNOSTIC(AddrOfEphemeralRef, Error,
"cannot take the address of a temporary object");
context.emitter().Emit(TokenOnly(node_id), AddrOfEphemeralRef);
value_id = SemIR::ErrorInst::SingletonInstId;
value_id = SemIR::ErrorInst::InstId;
break;
default:
CARBON_DIAGNOSTIC(AddrOfNonRef, Error,
"cannot take the address of non-reference expression");
context.emitter().Emit(TokenOnly(node_id), AddrOfNonRef);
value_id = SemIR::ErrorInst::SingletonInstId;
value_id = SemIR::ErrorInst::InstId;
break;
}
// TODO: Preserve spelling of type of operand where possible.
@@ -276,9 +275,9 @@ auto HandleParseNode(Context& context, Parse::PrefixOperatorConstId node_id)
context.emitter().Emit(node_id, RepeatedConst);
}
auto inner_type = ExprAsType(context, node_id, value_id);
AddInstAndPush<SemIR::ConstType>(context, node_id,
{.type_id = SemIR::TypeType::SingletonTypeId,
.inner_id = inner_type.inst_id});
AddInstAndPush<SemIR::ConstType>(
context, node_id,
{.type_id = SemIR::TypeType::TypeId, .inner_id = inner_type.inst_id});
return true;
}
@@ -330,7 +329,7 @@ auto HandleParseNode(Context& context, Parse::PrefixOperatorStarId node_id)
TokenOnly(node_id), DerefOfNonPointer, not_pointer_type_id);
// TODO: Check for any facet here, rather than only a type.
if (not_pointer_type_id == SemIR::TypeType::SingletonTypeId) {
if (not_pointer_type_id == SemIR::TypeType::TypeId) {
CARBON_DIAGNOSTIC(
DerefOfType, Note,
"to form a pointer type, write the `*` after the pointee type");