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
@@ -20,7 +20,7 @@ auto DeclNameStack::NameContext::prev_inst_id() -> SemIR::InstId {
switch (state) {
case NameContext::State::Error:
// The name is invalid and a diagnostic has already been emitted.
return SemIR::InstId::Invalid;
return SemIR::InstId::None;
case NameContext::State::Empty:
CARBON_FATAL(
@@ -31,7 +31,7 @@ auto DeclNameStack::NameContext::prev_inst_id() -> SemIR::InstId {
return resolved_inst_id;
case NameContext::State::Unresolved:
return SemIR::InstId::Invalid;
return SemIR::InstId::None;
case NameContext::State::Poisoned:
CARBON_FATAL("Poisoned state should not call prev_inst_id()");
@@ -117,7 +117,7 @@ auto DeclNameStack::Restore(SuspendedName sus) -> void {
for (auto& suspended_scope : llvm::reverse(sus.scopes)) {
// Reattempt to resolve the definition of the specific. The generic might
// have been defined after we suspended this scope.
if (suspended_scope.entry.specific_id.is_valid()) {
if (suspended_scope.entry.specific_id.has_value()) {
ResolveSpecificDefinition(*context_, sus.name_context.loc_id,
suspended_scope.entry.specific_id);
}
@@ -133,7 +133,7 @@ auto DeclNameStack::AddName(NameContext name_context, SemIR::InstId target_id,
return;
case NameContext::State::Unresolved:
if (!name_context.parent_scope_id.is_valid()) {
if (!name_context.parent_scope_id.has_value()) {
context_->AddNameToLookup(name_context.unresolved_name_id, target_id,
name_context.initial_scope_index);
} else {
@@ -176,7 +176,7 @@ auto DeclNameStack::AddNameOrDiagnose(NameContext name_context,
SemIR::AccessKind access_kind) -> void {
if (name_context.state == DeclNameStack::NameContext::State::Poisoned) {
context_->DiagnosePoisonedName(target_id);
} else if (auto id = name_context.prev_inst_id(); id.is_valid()) {
} else if (auto id = name_context.prev_inst_id(); id.has_value()) {
context_->DiagnoseDuplicateName(target_id, id);
} else {
AddName(name_context, target_id, access_kind);
@@ -188,13 +188,13 @@ auto DeclNameStack::LookupOrAddName(NameContext name_context,
SemIR::AccessKind access_kind)
-> std::pair<SemIR::InstId, bool> {
if (name_context.state == NameContext::State::Poisoned) {
return {SemIR::InstId::Invalid, true};
return {SemIR::InstId::None, true};
}
if (auto id = name_context.prev_inst_id(); id.is_valid()) {
if (auto id = name_context.prev_inst_id(); id.has_value()) {
return {id, false};
}
AddName(name_context, target_id, access_kind);
return {SemIR::InstId::Invalid, false};
return {SemIR::InstId::None, false};
}
// Push a scope corresponding to a name qualifier. For example, for
@@ -211,7 +211,7 @@ static auto PushNameQualifierScope(Context& context, SemIRLoc loc,
context.scope_stack().PopIfEmpty();
// When declaring a member of a generic, resolve the self specific.
if (specific_id.is_valid()) {
if (specific_id.has_value()) {
ResolveSpecificDefinition(context, loc, specific_id);
}
@@ -237,7 +237,7 @@ auto DeclNameStack::ApplyNameQualifier(const NameComponent& name) -> void {
// Resolve the qualifier as a scope and enter the new scope.
auto [scope_id, specific_id] = ResolveAsScope(name_context, name);
if (scope_id.is_valid()) {
if (scope_id.has_value()) {
PushNameQualifierScope(*context_, name_context.loc_id,
name_context.resolved_inst_id, scope_id, specific_id,
context_->name_scopes().Get(scope_id).has_error());
@@ -268,7 +268,7 @@ auto DeclNameStack::ApplyAndLookupName(NameContext& name_context,
if (is_poisoned) {
name_context.unresolved_name_id = name_id;
name_context.state = NameContext::State::Poisoned;
} else if (!resolved_inst_id.is_valid()) {
} else if (!resolved_inst_id.has_value()) {
// Invalid indicates an unresolved name. Store it and return.
name_context.unresolved_name_id = name_id;
name_context.state = NameContext::State::Unresolved;
@@ -374,7 +374,7 @@ auto DeclNameStack::ResolveAsScope(const NameContext& name_context,
const NameComponent& name) const
-> std::pair<SemIR::NameScopeId, SemIR::SpecificId> {
constexpr std::pair<SemIR::NameScopeId, SemIR::SpecificId> InvalidResult = {
SemIR::NameScopeId::Invalid, SemIR::SpecificId::Invalid};
SemIR::NameScopeId::None, SemIR::SpecificId::None};
if (!CheckQualifierIsResolved(*context_, name_context)) {
return InvalidResult;
@@ -429,9 +429,9 @@ auto DeclNameStack::ResolveAsScope(const NameContext& name_context,
// This is specifically for qualified name handling.
if (!CheckRedeclParamsMatch(
*context_, new_params,
DeclParams(name_context.resolved_inst_id, Parse::NodeId::Invalid,
Parse::NodeId::Invalid, SemIR::InstBlockId::Invalid,
SemIR::InstBlockId::Invalid))) {
DeclParams(name_context.resolved_inst_id, Parse::NodeId::None,
Parse::NodeId::None, SemIR::InstBlockId::None,
SemIR::InstBlockId::None))) {
return InvalidResult;
}
if (scope.is_closed_import()) {
@@ -441,7 +441,7 @@ auto DeclNameStack::ResolveAsScope(const NameContext& name_context,
// be used as a name qualifier.
scope.set_is_closed_import(false);
}
return {scope_id, SemIR::SpecificId::Invalid};
return {scope_id, SemIR::SpecificId::None};
}
default: {
DiagnoseQualifiedDeclInNonScope(*context_, name_context.loc_id,