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:
Jon Ross-Perkins
2025-01-22 23:15:00 +00:00
committed by GitHub
parent b292943648
commit 6b5eb1a101
361 changed files with 2612 additions and 2624 deletions
+16 -16
View File
@@ -31,7 +31,7 @@ auto ScopeStack::Push(SemIR::InstId scope_inst_id, SemIR::NameScopeId scope_id,
// If this scope doesn't have a specific of its own, it lives in the enclosing
// scope's specific, if any.
auto enclosing_specific_id = specific_id;
if (!specific_id.is_valid() && !scope_stack_.empty()) {
if (!specific_id.has_value() && !scope_stack_.empty()) {
enclosing_specific_id = PeekSpecificId();
}
@@ -45,7 +45,7 @@ auto ScopeStack::Push(SemIR::InstId scope_inst_id, SemIR::NameScopeId scope_id,
compile_time_binding_stack_.all_values_size()),
.lexical_lookup_has_load_error =
LexicalLookupHasLoadError() || lexical_lookup_has_load_error});
if (scope_id.is_valid()) {
if (scope_id.has_value()) {
non_lexical_scope_stack_.push_back({.scope_index = next_scope_index_,
.name_scope_id = scope_id,
.specific_id = enclosing_specific_id});
@@ -53,7 +53,7 @@ auto ScopeStack::Push(SemIR::InstId scope_inst_id, SemIR::NameScopeId scope_id,
// For lexical lookups, unqualified lookup doesn't know how to find the
// associated specific, so if we start adding lexical scopes associated with
// specifics, we'll need to somehow track them in lookup.
CARBON_CHECK(!specific_id.is_valid(),
CARBON_CHECK(!specific_id.has_value(),
"Lexical scope should not have an associated specific.");
}
@@ -75,15 +75,15 @@ auto ScopeStack::Pop() -> void {
lexical_results.pop_back();
});
if (scope.scope_id.is_valid()) {
if (scope.scope_id.has_value()) {
CARBON_CHECK(non_lexical_scope_stack_.back().scope_index == scope.index);
non_lexical_scope_stack_.pop_back();
}
if (scope.has_returned_var) {
CARBON_CHECK(!return_scope_stack_.empty());
CARBON_CHECK(return_scope_stack_.back().returned_var.is_valid());
return_scope_stack_.back().returned_var = SemIR::InstId::Invalid;
CARBON_CHECK(return_scope_stack_.back().returned_var.has_value());
return_scope_stack_.back().returned_var = SemIR::InstId::None;
}
VerifyNextCompileTimeBindIndex("Pop", scope);
@@ -104,12 +104,12 @@ auto ScopeStack::LookupInLexicalScopesWithin(SemIR::NameId name_id,
-> SemIR::InstId {
auto& lexical_results = lexical_lookup_.Get(name_id);
if (lexical_results.empty()) {
return SemIR::InstId::Invalid;
return SemIR::InstId::None;
}
auto result = lexical_results.back();
if (result.scope_index < scope_index) {
return SemIR::InstId::Invalid;
return SemIR::InstId::None;
}
return result.inst_id;
@@ -125,7 +125,7 @@ auto ScopeStack::LookupInLexicalScopes(SemIR::NameId name_id)
// If we have no lexical results, check all non-lexical scopes.
if (lexical_results.empty()) {
return {LexicalLookupHasLoadError() ? SemIR::ErrorInst::SingletonInstId
: SemIR::InstId::Invalid,
: SemIR::InstId::None,
non_lexical_scope_stack_};
}
@@ -150,7 +150,7 @@ auto ScopeStack::LookupOrAddName(SemIR::NameId name_id, SemIR::InstId target_id,
// function, so it should be relatively rare, but it's still not necesasry to
// recompute this.
int scope_depth = scope_stack_.size() - 1;
if (scope_index.is_valid()) {
if (scope_index.has_value()) {
scope_depth =
std::lower_bound(scope_stack_.begin(), scope_stack_.end(), scope_index,
[](const ScopeStackEntry& entry, ScopeIndex index) {
@@ -178,31 +178,31 @@ auto ScopeStack::LookupOrAddName(SemIR::NameId name_id, SemIR::InstId target_id,
// Add a corresponding lexical lookup result.
lexical_results.push_back({.inst_id = target_id, .scope_index = scope_index});
return SemIR::InstId::Invalid;
return SemIR::InstId::None;
}
auto ScopeStack::SetReturnedVarOrGetExisting(SemIR::InstId inst_id)
-> SemIR::InstId {
CARBON_CHECK(!return_scope_stack_.empty(), "`returned var` in no function");
auto& returned_var = return_scope_stack_.back().returned_var;
if (returned_var.is_valid()) {
if (returned_var.has_value()) {
return returned_var;
}
returned_var = inst_id;
CARBON_CHECK(!scope_stack_.back().has_returned_var,
"Scope has returned var but none is set");
if (inst_id.is_valid()) {
if (inst_id.has_value()) {
scope_stack_.back().has_returned_var = true;
}
return SemIR::InstId::Invalid;
return SemIR::InstId::None;
}
auto ScopeStack::Suspend() -> SuspendedScope {
CARBON_CHECK(!scope_stack_.empty(), "No scope to suspend");
SuspendedScope result = {.entry = scope_stack_.pop_back_val(),
.suspended_items = {}};
if (result.entry.scope_id.is_valid()) {
if (result.entry.scope_id.has_value()) {
non_lexical_scope_stack_.pop_back();
}
@@ -246,7 +246,7 @@ auto ScopeStack::Restore(SuspendedScope scope) -> void {
VerifyNextCompileTimeBindIndex("Restore", scope.entry);
if (scope.entry.scope_id.is_valid()) {
if (scope.entry.scope_id.has_value()) {
non_lexical_scope_stack_.push_back(
{.scope_index = scope.entry.index,
.name_scope_id = scope.entry.scope_id,