Skip to content

Commit 3bd7252

Browse files
authored
Clean up obsolete import handling in class/function (#4857)
Noticed because the `new_loc.inst_id` use would be invalid as-is
1 parent ad0a47d commit 3bd7252

File tree

2 files changed

+15
-17
lines changed

2 files changed

+15
-17
lines changed

toolchain/check/handle_class.cpp

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#include "toolchain/check/merge.h"
1515
#include "toolchain/check/modifiers.h"
1616
#include "toolchain/check/name_component.h"
17+
#include "toolchain/parse/node_ids.h"
1718
#include "toolchain/sem_ir/function.h"
1819
#include "toolchain/sem_ir/ids.h"
1920
#include "toolchain/sem_ir/inst.h"
@@ -53,9 +54,8 @@ auto HandleParseNode(Context& context, Parse::ClassIntroducerId node_id)
5354
//
5455
// If merging is successful, returns true and may update the previous class.
5556
// Otherwise, returns false. Prints a diagnostic when appropriate.
56-
static auto MergeClassRedecl(Context& context, SemIRLoc new_loc,
57-
SemIR::Class& new_class, bool new_is_import,
58-
bool new_is_definition,
57+
static auto MergeClassRedecl(Context& context, Parse::AnyClassDeclId node_id,
58+
SemIR::Class& new_class, bool new_is_definition,
5959
SemIR::ClassId prev_class_id,
6060
SemIR::ImportIRId prev_import_ir_id) -> bool {
6161
auto& prev_class = context.classes().Get(prev_class_id);
@@ -69,7 +69,7 @@ static auto MergeClassRedecl(Context& context, SemIRLoc new_loc,
6969

7070
DiagnoseIfInvalidRedecl(
7171
context, Lex::TokenKind::Class, prev_class.name_id,
72-
RedeclInfo(new_class, new_loc, new_is_definition),
72+
RedeclInfo(new_class, node_id, new_is_definition),
7373
RedeclInfo(prev_class, prev_loc, prev_class.has_definition_started()),
7474
prev_import_ir_id);
7575

@@ -87,12 +87,11 @@ static auto MergeClassRedecl(Context& context, SemIRLoc new_loc,
8787
prev_class.complete_type_witness_id = new_class.complete_type_witness_id;
8888
}
8989

90-
if ((prev_import_ir_id.has_value() && !new_is_import) ||
90+
if (prev_import_ir_id.has_value() ||
9191
(prev_class.is_extern && !new_class.is_extern)) {
9292
prev_class.first_owning_decl_id = new_class.first_owning_decl_id;
93-
ReplacePrevInstForMerge(
94-
context, new_class.parent_scope_id, prev_class.name_id,
95-
new_is_import ? new_loc.inst_id : new_class.first_owning_decl_id);
93+
ReplacePrevInstForMerge(context, new_class.parent_scope_id,
94+
prev_class.name_id, new_class.first_owning_decl_id);
9695
}
9796
return true;
9897
}
@@ -165,9 +164,8 @@ static auto MergeOrAddName(Context& context, Parse::AnyClassDeclId node_id,
165164

166165
// TODO: Fix `extern` logic. It doesn't work correctly, but doesn't seem worth
167166
// ripping out because existing code may incrementally help.
168-
if (MergeClassRedecl(context, node_id, class_info,
169-
/*new_is_import=*/false, is_definition, prev_class_id,
170-
prev_import_ir_id)) {
167+
if (MergeClassRedecl(context, node_id, class_info, is_definition,
168+
prev_class_id, prev_import_ir_id)) {
171169
// When merging, use the existing entity rather than adding a new one.
172170
class_decl.class_id = prev_class_id;
173171
class_decl.type_id = prev.type_id();

toolchain/check/handle_function.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,10 @@ static auto DiagnoseModifiers(Context& context, DeclIntroducerState& introducer,
7777
//
7878
// If merging is successful, returns true and may update the previous function.
7979
// Otherwise, returns false. Prints a diagnostic when appropriate.
80-
static auto MergeFunctionRedecl(Context& context, SemIRLoc new_loc,
80+
static auto MergeFunctionRedecl(Context& context,
81+
Parse::AnyFunctionDeclId node_id,
8182
SemIR::Function& new_function,
82-
bool new_is_import, bool new_is_definition,
83+
bool new_is_definition,
8384
SemIR::FunctionId prev_function_id,
8485
SemIR::ImportIRId prev_import_ir_id) -> bool {
8586
auto& prev_function = context.functions().Get(prev_function_id);
@@ -90,7 +91,7 @@ static auto MergeFunctionRedecl(Context& context, SemIRLoc new_loc,
9091

9192
DiagnoseIfInvalidRedecl(
9293
context, Lex::TokenKind::Fn, prev_function.name_id,
93-
RedeclInfo(new_function, new_loc, new_is_definition),
94+
RedeclInfo(new_function, node_id, new_is_definition),
9495
RedeclInfo(prev_function, prev_function.latest_decl_id(),
9596
prev_function.has_definition_started()),
9697
prev_import_ir_id);
@@ -107,7 +108,7 @@ static auto MergeFunctionRedecl(Context& context, SemIRLoc new_loc,
107108
prev_function.MergeDefinition(new_function);
108109
prev_function.return_slot_pattern_id = new_function.return_slot_pattern_id;
109110
}
110-
if ((prev_import_ir_id.has_value() && !new_is_import)) {
111+
if (prev_import_ir_id.has_value()) {
111112
ReplacePrevInstForMerge(context, new_function.parent_scope_id,
112113
prev_function.name_id,
113114
new_function.first_owning_decl_id);
@@ -162,8 +163,7 @@ static auto TryMergeRedecl(Context& context, Parse::AnyFunctionDeclId node_id,
162163
return;
163164
}
164165

165-
if (MergeFunctionRedecl(context, node_id, function_info,
166-
/*new_is_import=*/false, is_definition,
166+
if (MergeFunctionRedecl(context, node_id, function_info, is_definition,
167167
prev_function_id, prev_import_ir_id)) {
168168
// When merging, use the existing function rather than adding a new one.
169169
function_decl.function_id = prev_function_id;

0 commit comments

Comments
 (0)