mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-06 06:34:41 +01:00
Id::Invalid -> Id::None (#4834)
High level, replacing `Id::Invalid` with `Id::None` and `Id::is_valid` with `Id::has_value` for clarity, as discussed [here](https://discord.com/channels/655572317891461132/655578254970716160/1331664574545395794). The `IntId` refactoring is needed together with `AnyIdBase` because it's also used with `ValueStore`. Note, trying to be careful not to rewrite `EnumBase::InvalidIndex`, or `is_valid` in general (e.g., `IdKind::is_valid`). I've tried to sequence commits here: 1. Automatic replacements: - `((?:Id|Index)(?: |::|\(|Base(?:\(|::)))Invalid((?:Index)?\W)` -> `$1None$2` - `<invalid>` -> `<none>` - `InvalidNodeId` -> `NoneNodeId` - `/\*invalid\*/` -> `/*none*/` - `id((?:_|\(\))(?:\.|->))is_valid` -> `id$1has_value` 2. Manual edits: - In `int.h` and `int_test.cpp` - `IntT` has `is_value`, which I'm renaming to `is_embedded_value`. - Manual edits to comments in this file. - `AnyIdBase` and `IdBase` - Declaration of `is_valid` -> `has_value`, `InvalidIndex` -> `NoneIndex`. - In `ids.h` and `ids.cpp` - `is_valid` -> `has_value` - `// An explicitly invalid ID.` -> `// An ID with no value.`; similar for index - Various math on `InvalidIndex` -> `NoneIndex` - Various mentions of "valid" in comments - In `value_store.h`, for `IdT::Invalid`, plus one comment - In `impl.h` and `tokenized_buffer.h`, we had different initialization of `::None` values (versus `ids.h` syntax) that I fixed manually. - Spot checks to compile - Particularly where `is_valid` replacements didn't catch spots due to different naming. 3. Autoupdate tests 4. verbose.carbon (NOAUTOUPDATE) 5. Comment spot checks Note there are probably other mentions of "Invalid" that should be swept up, but I'd like to argue for merging and separating out remaining cleanup since this is so sweeping (and likely to hit merge conflicts from churn). We'll probably have lingering mentions of "invalid" for a bit regardless, just because there are uses of "invalid" in non-Id APIs.
This commit is contained in:
@@ -29,7 +29,7 @@ static auto GetPrettyName(Context& context, ParamPattern param_pattern)
|
||||
param_pattern.subpattern_id)) {
|
||||
return context.entity_names().Get(binding_pattern->entity_name_id).name_id;
|
||||
}
|
||||
return SemIR::NameId::Invalid;
|
||||
return SemIR::NameId::None;
|
||||
}
|
||||
|
||||
namespace {
|
||||
@@ -64,7 +64,7 @@ class MatchContext {
|
||||
// match operation is part of implementing the signature of the given
|
||||
// specific.
|
||||
explicit MatchContext(MatchKind kind, SemIR::SpecificId callee_specific_id =
|
||||
SemIR::SpecificId::Invalid)
|
||||
SemIR::SpecificId::None)
|
||||
: next_index_(0), kind_(kind), callee_specific_id_(callee_specific_id) {}
|
||||
|
||||
// Adds a work item to the stack.
|
||||
@@ -147,8 +147,8 @@ auto MatchContext::EmitPatternMatch(Context& context,
|
||||
// to avoid accidentally consuming it twice.
|
||||
auto [bind_name_id, type_expr_region_id] = std::exchange(
|
||||
context.bind_name_map().Lookup(entry.pattern_id).value(),
|
||||
{.bind_name_id = SemIR::InstId::Invalid,
|
||||
.type_expr_region_id = SemIR::ExprRegionId::Invalid});
|
||||
{.bind_name_id = SemIR::InstId::None,
|
||||
.type_expr_region_id = SemIR::ExprRegionId::None});
|
||||
context.InsertHere(type_expr_region_id);
|
||||
auto value_id = entry.scrutinee_id;
|
||||
switch (kind_) {
|
||||
@@ -161,7 +161,7 @@ auto MatchContext::EmitPatternMatch(Context& context,
|
||||
case MatchKind::Callee: {
|
||||
if (context.insts()
|
||||
.GetAs<SemIR::AnyParam>(value_id)
|
||||
.runtime_index.is_valid()) {
|
||||
.runtime_index.has_value()) {
|
||||
results_.push_back(value_id);
|
||||
}
|
||||
break;
|
||||
@@ -170,7 +170,7 @@ auto MatchContext::EmitPatternMatch(Context& context,
|
||||
CARBON_FATAL("Found binding pattern during caller pattern match");
|
||||
}
|
||||
auto bind_name = context.insts().GetAs<SemIR::AnyBindName>(bind_name_id);
|
||||
CARBON_CHECK(!bind_name.value_id.is_valid());
|
||||
CARBON_CHECK(!bind_name.value_id.has_value());
|
||||
bind_name.value_id = value_id;
|
||||
context.ReplaceInstBeforeConstantUse(bind_name_id, bind_name);
|
||||
context.inst_block_stack().AddInstId(bind_name_id);
|
||||
@@ -183,10 +183,10 @@ auto MatchContext::EmitPatternMatch(Context& context,
|
||||
// the caller side of the pattern, so we traverse without emitting any
|
||||
// insts.
|
||||
AddWork({.pattern_id = addr_pattern.inner_id,
|
||||
.scrutinee_id = SemIR::InstId::Invalid});
|
||||
.scrutinee_id = SemIR::InstId::None});
|
||||
break;
|
||||
}
|
||||
CARBON_CHECK(entry.scrutinee_id.is_valid());
|
||||
CARBON_CHECK(entry.scrutinee_id.has_value());
|
||||
auto scrutinee_ref_id =
|
||||
ConvertToValueOrRefExpr(context, entry.scrutinee_id);
|
||||
switch (SemIR::GetExprCategory(context.sem_ir(), scrutinee_ref_id)) {
|
||||
@@ -220,7 +220,7 @@ auto MatchContext::EmitPatternMatch(Context& context,
|
||||
results_.size(), param_pattern.runtime_index.index);
|
||||
switch (kind_) {
|
||||
case MatchKind::Caller: {
|
||||
CARBON_CHECK(entry.scrutinee_id.is_valid());
|
||||
CARBON_CHECK(entry.scrutinee_id.has_value());
|
||||
if (entry.scrutinee_id == SemIR::ErrorInst::SingletonInstId) {
|
||||
results_.push_back(SemIR::ErrorInst::SingletonInstId);
|
||||
} else {
|
||||
@@ -259,7 +259,7 @@ auto MatchContext::EmitPatternMatch(Context& context,
|
||||
case CARBON_KIND(SemIR::OutParamPattern param_pattern): {
|
||||
switch (kind_) {
|
||||
case MatchKind::Caller: {
|
||||
CARBON_CHECK(entry.scrutinee_id.is_valid());
|
||||
CARBON_CHECK(entry.scrutinee_id.has_value());
|
||||
CARBON_CHECK(context.insts().Get(entry.scrutinee_id).type_id() ==
|
||||
SemIR::GetTypeInSpecific(context.sem_ir(),
|
||||
callee_specific_id_,
|
||||
@@ -302,7 +302,7 @@ auto MatchContext::EmitPatternMatch(Context& context,
|
||||
bool already_in_lookup =
|
||||
context.scope_stack()
|
||||
.LookupOrAddName(SemIR::NameId::ReturnSlot, return_slot_id)
|
||||
.is_valid();
|
||||
.has_value();
|
||||
CARBON_CHECK(!already_in_lookup);
|
||||
results_.push_back(entry.scrutinee_id);
|
||||
break;
|
||||
@@ -315,7 +315,7 @@ auto MatchContext::EmitPatternMatch(Context& context,
|
||||
if (context.scope_stack().PeekIndex() == ScopeIndex::Package) {
|
||||
context.global_init().Resume();
|
||||
}
|
||||
if (entry.scrutinee_id.is_valid()) {
|
||||
if (entry.scrutinee_id.has_value()) {
|
||||
auto init_id =
|
||||
Initialize(context, pattern.loc_id, var_id, entry.scrutinee_id);
|
||||
// TODO: Consider using different instruction kinds for assignment
|
||||
@@ -341,33 +341,33 @@ auto CalleePatternMatch(Context& context,
|
||||
SemIR::InstBlockId param_patterns_id,
|
||||
SemIR::InstId return_slot_pattern_id)
|
||||
-> SemIR::InstBlockId {
|
||||
if (!return_slot_pattern_id.is_valid() && !param_patterns_id.is_valid() &&
|
||||
!implicit_param_patterns_id.is_valid()) {
|
||||
return SemIR::InstBlockId::Invalid;
|
||||
if (!return_slot_pattern_id.has_value() && !param_patterns_id.has_value() &&
|
||||
!implicit_param_patterns_id.has_value()) {
|
||||
return SemIR::InstBlockId::None;
|
||||
}
|
||||
|
||||
MatchContext match(MatchKind::Callee);
|
||||
|
||||
// We add work to the stack in reverse so that the results will be produced
|
||||
// in the original order.
|
||||
if (return_slot_pattern_id.is_valid()) {
|
||||
if (return_slot_pattern_id.has_value()) {
|
||||
match.AddWork({.pattern_id = return_slot_pattern_id,
|
||||
.scrutinee_id = SemIR::InstId::Invalid});
|
||||
.scrutinee_id = SemIR::InstId::None});
|
||||
}
|
||||
|
||||
if (param_patterns_id.is_valid()) {
|
||||
if (param_patterns_id.has_value()) {
|
||||
for (SemIR::InstId inst_id :
|
||||
llvm::reverse(context.inst_blocks().Get(param_patterns_id))) {
|
||||
match.AddWork(
|
||||
{.pattern_id = inst_id, .scrutinee_id = SemIR::InstId::Invalid});
|
||||
{.pattern_id = inst_id, .scrutinee_id = SemIR::InstId::None});
|
||||
}
|
||||
}
|
||||
|
||||
if (implicit_param_patterns_id.is_valid()) {
|
||||
if (implicit_param_patterns_id.has_value()) {
|
||||
for (SemIR::InstId inst_id :
|
||||
llvm::reverse(context.inst_blocks().Get(implicit_param_patterns_id))) {
|
||||
match.AddWork(
|
||||
{.pattern_id = inst_id, .scrutinee_id = SemIR::InstId::Invalid});
|
||||
{.pattern_id = inst_id, .scrutinee_id = SemIR::InstId::None});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -385,8 +385,8 @@ auto CallerPatternMatch(Context& context, SemIR::SpecificId specific_id,
|
||||
MatchContext match(MatchKind::Caller, specific_id);
|
||||
|
||||
// Track the return storage, if present.
|
||||
if (return_slot_arg_id.is_valid()) {
|
||||
CARBON_CHECK(return_slot_pattern_id.is_valid());
|
||||
if (return_slot_arg_id.has_value()) {
|
||||
CARBON_CHECK(return_slot_pattern_id.has_value());
|
||||
match.AddWork({.pattern_id = return_slot_pattern_id,
|
||||
.scrutinee_id = return_slot_arg_id});
|
||||
}
|
||||
@@ -397,7 +397,7 @@ auto CallerPatternMatch(Context& context, SemIR::SpecificId specific_id,
|
||||
auto runtime_index = SemIR::Function::GetParamPatternInfoFromPatternId(
|
||||
context.sem_ir(), param_pattern_id)
|
||||
.inst.runtime_index;
|
||||
if (!runtime_index.is_valid()) {
|
||||
if (!runtime_index.has_value()) {
|
||||
// Not a runtime parameter: we don't pass an argument.
|
||||
continue;
|
||||
}
|
||||
@@ -405,7 +405,7 @@ auto CallerPatternMatch(Context& context, SemIR::SpecificId specific_id,
|
||||
match.AddWork({.pattern_id = param_pattern_id, .scrutinee_id = arg_id});
|
||||
}
|
||||
|
||||
if (self_pattern_id.is_valid()) {
|
||||
if (self_pattern_id.has_value()) {
|
||||
match.AddWork({.pattern_id = self_pattern_id, .scrutinee_id = self_arg_id});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user