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
+6 -9
View File
@@ -77,7 +77,7 @@ struct NoReturn {
};
// Constraint that a type is `bool`.
using Bool = BuiltinType<BoolType::SingletonInstId>;
using Bool = BuiltinType<BoolType::InstId>;
// Constraint that requires the type to be a sized integer type.
struct AnySizedInt {
@@ -92,8 +92,7 @@ struct AnyInt {
static auto Check(const File& sem_ir, ValidateState& state, TypeId type_id)
-> bool {
return AnySizedInt::Check(sem_ir, state, type_id) ||
BuiltinType<IntLiteralType::SingletonInstId>::Check(sem_ir, state,
type_id);
BuiltinType<IntLiteralType::InstId>::Check(sem_ir, state, type_id);
}
};
@@ -101,8 +100,7 @@ struct AnyInt {
struct AnyFloat {
static auto Check(const File& sem_ir, ValidateState& state, TypeId type_id)
-> bool {
if (BuiltinType<LegacyFloatType::SingletonInstId>::Check(sem_ir, state,
type_id)) {
if (BuiltinType<LegacyFloatType::InstId>::Check(sem_ir, state, type_id)) {
return true;
}
return sem_ir.types().Is<FloatType>(type_id);
@@ -110,18 +108,17 @@ struct AnyFloat {
};
// Constraint that requires the type to be the type type.
using Type = BuiltinType<TypeType::SingletonInstId>;
using Type = BuiltinType<TypeType::InstId>;
// Constraint that requires the type to be a type value, whose type is type
// type. Also accepts symbolic constant value types.
struct AnyType {
static auto Check(const File& sem_ir, ValidateState& state, TypeId type_id)
-> bool {
if (BuiltinType<TypeType::SingletonInstId>::Check(sem_ir, state, type_id)) {
if (BuiltinType<TypeType::InstId>::Check(sem_ir, state, type_id)) {
return true;
}
return sem_ir.types().GetAsInst(type_id).type_id() ==
TypeType::SingletonTypeId;
return sem_ir.types().GetAsInst(type_id).type_id() == TypeType::TypeId;
}
};