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
+19 -19
View File
@@ -98,7 +98,7 @@ static auto MergeFunctionRedecl(Context& context, SemIRLoc new_loc,
return false;
}
if (!prev_function.first_owning_decl_id.is_valid()) {
if (!prev_function.first_owning_decl_id.has_value()) {
prev_function.first_owning_decl_id = new_function.first_owning_decl_id;
}
if (new_is_definition) {
@@ -107,7 +107,7 @@ static auto MergeFunctionRedecl(Context& context, SemIRLoc new_loc,
prev_function.MergeDefinition(new_function);
prev_function.return_slot_pattern_id = new_function.return_slot_pattern_id;
}
if ((prev_import_ir_id.is_valid() && !new_is_import)) {
if ((prev_import_ir_id.has_value() && !new_is_import)) {
ReplacePrevInstForMerge(context, new_function.parent_scope_id,
prev_function.name_id,
new_function.first_owning_decl_id);
@@ -121,12 +121,12 @@ static auto TryMergeRedecl(Context& context, Parse::AnyFunctionDeclId node_id,
SemIR::FunctionDecl& function_decl,
SemIR::Function& function_info, bool is_definition)
-> void {
if (!prev_id.is_valid()) {
if (!prev_id.has_value()) {
return;
}
auto prev_function_id = SemIR::FunctionId::Invalid;
auto prev_import_ir_id = SemIR::ImportIRId::Invalid;
auto prev_function_id = SemIR::FunctionId::None;
auto prev_import_ir_id = SemIR::ImportIRId::None;
CARBON_KIND_SWITCH(context.insts().Get(prev_id)) {
case CARBON_KIND(SemIR::FunctionDecl function_decl): {
prev_function_id = function_decl.function_id;
@@ -157,7 +157,7 @@ static auto TryMergeRedecl(Context& context, Parse::AnyFunctionDeclId node_id,
break;
}
if (!prev_function_id.is_valid()) {
if (!prev_function_id.has_value()) {
context.DiagnoseDuplicateName(function_info.latest_decl_id(), prev_id);
return;
}
@@ -177,7 +177,7 @@ static auto BuildFunctionDecl(Context& context,
Parse::AnyFunctionDeclId node_id,
bool is_definition)
-> std::pair<SemIR::FunctionId, SemIR::InstId> {
auto return_slot_pattern_id = SemIR::InstId::Invalid;
auto return_slot_pattern_id = SemIR::InstId::None;
if (auto [return_node, maybe_return_slot_pattern_id] =
context.node_stack().PopWithNodeIdIf<Parse::NodeKind::ReturnType>();
maybe_return_slot_pattern_id) {
@@ -185,7 +185,7 @@ static auto BuildFunctionDecl(Context& context,
}
auto name = PopNameComponent(context, return_slot_pattern_id);
if (!name.param_patterns_id.is_valid()) {
if (!name.param_patterns_id.has_value()) {
context.TODO(node_id, "function with positional parameters");
name.param_patterns_id = SemIR::InstBlockId::Empty;
}
@@ -217,7 +217,7 @@ static auto BuildFunctionDecl(Context& context,
if (auto class_decl = parent_scope_inst->TryAs<SemIR::ClassDecl>()) {
virtual_class_info = &context.classes().Get(class_decl->class_id);
if (virtual_modifier == SemIR::Function::VirtualModifier::Impl &&
!virtual_class_info->base_id.is_valid()) {
!virtual_class_info->base_id.has_value()) {
CARBON_DIAGNOSTIC(ImplWithoutBase, Error, "impl without base class");
context.emitter().Build(node_id, ImplWithoutBase).Emit();
}
@@ -236,7 +236,7 @@ static auto BuildFunctionDecl(Context& context,
// Add the function declaration.
auto decl_block_id = context.inst_block_stack().Pop();
auto function_decl = SemIR::FunctionDecl{
SemIR::TypeId::Invalid, SemIR::FunctionId::Invalid, decl_block_id};
SemIR::TypeId::None, SemIR::FunctionId::None, decl_block_id};
auto decl_id =
context.AddPlaceholderInst(SemIR::LocIdAndInst(node_id, function_decl));
@@ -262,7 +262,7 @@ static auto BuildFunctionDecl(Context& context,
}
// Create a new function if this isn't a valid redeclaration.
if (!function_decl.function_id.is_valid()) {
if (!function_decl.function_id.has_value()) {
if (function_info.is_extern && context.IsImplFile()) {
DiagnoseExternRequiresDeclInApiFile(context, node_id);
}
@@ -291,7 +291,7 @@ static auto BuildFunctionDecl(Context& context,
// Check if we need to add this to name lookup, now that the function decl is
// done.
if (name_context.state != DeclNameStack::NameContext::State::Poisoned &&
!name_context.prev_inst_id().is_valid()) {
!name_context.prev_inst_id().has_value()) {
// At interface scope, a function declaration introduces an associated
// function.
auto lookup_result_id = decl_id;
@@ -310,10 +310,10 @@ static auto BuildFunctionDecl(Context& context,
if (SemIR::IsEntryPoint(context.sem_ir(), function_decl.function_id)) {
auto return_type_id = function_info.GetDeclaredReturnType(context.sem_ir());
// TODO: Update this once valid signatures for the entry point are decided.
if (function_info.implicit_param_patterns_id.is_valid() ||
!function_info.param_patterns_id.is_valid() ||
if (function_info.implicit_param_patterns_id.has_value() ||
!function_info.param_patterns_id.has_value() ||
!context.inst_blocks().Get(function_info.param_patterns_id).empty() ||
(return_type_id.is_valid() &&
(return_type_id.has_value() &&
return_type_id != context.GetTupleType({}) &&
// TODO: Decide on valid return types for `Main.Run`. Perhaps we should
// have an interface for this.
@@ -346,10 +346,10 @@ static auto CheckFunctionDefinitionSignature(Context& context,
context.inst_blocks().GetOrEmpty(function.call_params_id);
// Check the return type is complete.
if (function.return_slot_pattern_id.is_valid()) {
if (function.return_slot_pattern_id.has_value()) {
CheckFunctionReturnType(
context, context.insts().GetLocId(function.return_slot_pattern_id),
function, SemIR::SpecificId::Invalid);
function, SemIR::SpecificId::None);
params_to_complete = params_to_complete.drop_back();
}
@@ -432,7 +432,7 @@ auto HandleParseNode(Context& context, Parse::FunctionDefinitionId node_id)
if (context.is_current_position_reachable()) {
if (context.functions()
.Get(function_id)
.return_slot_pattern_id.is_valid()) {
.return_slot_pattern_id.has_value()) {
CARBON_DIAGNOSTIC(
MissingReturnStatement, Error,
"missing `return` at end of function with declared return type");
@@ -510,7 +510,7 @@ static auto IsValidBuiltinDeclaration(Context& context,
// Get the return type. This is `()` if none was specified.
auto return_type_id = function.GetDeclaredReturnType(context.sem_ir());
if (!return_type_id.is_valid()) {
if (!return_type_id.has_value()) {
return_type_id = context.GetTupleType({});
}