Refactor CheckIsAllowedRedecl and stop function definition merging (#4800)

Rename `CheckIsAllowedRedecl` to `DiagnoseIfInvalidRedecl` to try to
better document behavior, and clean up comments.

This extends the no-merge-if-defined behavior to functions. It was
already the case for class/interface, and just added for impl, so if
anything functions were now inconsistent. I was kind of tempted to make
a helper for it, but I didn't think of a great structure/name to get
there: `DiagnoseRedef` isn't always called when it's a redefinition, for
example due to `extern` diagnostics, it's hard to combine.

Cleans up `is_defined` calls to rely more on `has_definition_started`,
removing some code paths that are unused since definitions aren't
merged.
This commit is contained in:
Jon Ross-Perkins
2025-01-14 21:13:21 +00:00
committed by GitHub
parent 5b70a3ea91
commit d958caaff3
15 changed files with 194 additions and 141 deletions
+7 -8
View File
@@ -65,13 +65,13 @@ static auto MergeClassRedecl(Context& context, SemIRLoc new_loc,
return false;
}
CheckIsAllowedRedecl(
DiagnoseIfInvalidRedecl(
context, Lex::TokenKind::Class, prev_class.name_id,
RedeclInfo(new_class, new_loc, new_is_definition),
RedeclInfo(prev_class, prev_loc, prev_class.is_defined()),
RedeclInfo(prev_class, prev_loc, prev_class.has_definition_started()),
prev_import_ir_id);
if (new_is_definition && prev_class.is_defined()) {
if (new_is_definition && prev_class.has_definition_started()) {
// Don't attempt to merge multiple definitions.
return false;
}
@@ -280,11 +280,10 @@ auto HandleParseNode(Context& context, Parse::ClassDefinitionStartId node_id)
auto& class_info = context.classes().Get(class_id);
// Track that this declaration is the definition.
if (!class_info.is_defined()) {
class_info.definition_id = class_decl_id;
class_info.scope_id = context.name_scopes().Add(
class_decl_id, SemIR::NameId::Invalid, class_info.parent_scope_id);
}
CARBON_CHECK(!class_info.has_definition_started());
class_info.definition_id = class_decl_id;
class_info.scope_id = context.name_scopes().Add(
class_decl_id, SemIR::NameId::Invalid, class_info.parent_scope_id);
// Enter the class scope.
context.scope_stack().Push(