Skip to content

Commit f4e19f4

Browse files
authored
Change DeclNameStack::LookupOrAddName() to return SemIR::ScopeLookupResult instead of a pair (#4852)
This makes the lookup API more consistent and would make it easier to add poisoning location. Part of #4622.
1 parent 51d7e63 commit f4e19f4

File tree

5 files changed

+24
-19
lines changed

5 files changed

+24
-19
lines changed

toolchain/check/decl_name_stack.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -186,15 +186,15 @@ auto DeclNameStack::AddNameOrDiagnose(NameContext name_context,
186186
auto DeclNameStack::LookupOrAddName(NameContext name_context,
187187
SemIR::InstId target_id,
188188
SemIR::AccessKind access_kind)
189-
-> std::pair<SemIR::InstId, bool> {
189+
-> SemIR::ScopeLookupResult {
190190
if (name_context.state == NameContext::State::Poisoned) {
191-
return {SemIR::InstId::None, true};
191+
return SemIR::ScopeLookupResult::MakePoisoned();
192192
}
193193
if (auto id = name_context.prev_inst_id(); id.has_value()) {
194-
return {id, false};
194+
return SemIR::ScopeLookupResult::MakeFound(id, access_kind);
195195
}
196196
AddName(name_context, target_id, access_kind);
197-
return {SemIR::InstId::None, false};
197+
return SemIR::ScopeLookupResult::MakeNotFound();
198198
}
199199

200200
// Push a scope corresponding to a name qualifier. For example, for

toolchain/check/decl_name_stack.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -238,12 +238,10 @@ class DeclNameStack {
238238
SemIR::AccessKind access_kind) -> void;
239239

240240
// Adds a name to name lookup if neither already declared nor poisoned in this
241-
// scope. If declared, returns the existing `InstId` and false. If poisoned,
242-
// returns `None` and true.
243-
// TODO: Return the poisoning instruction if poisoned.
241+
// scope.
244242
auto LookupOrAddName(NameContext name_context, SemIR::InstId target_id,
245243
SemIR::AccessKind access_kind)
246-
-> std::pair<SemIR::InstId, bool>;
244+
-> SemIR::ScopeLookupResult;
247245

248246
private:
249247
// Returns a name context corresponding to an empty name.

toolchain/check/handle_class.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,18 +105,21 @@ static auto MergeOrAddName(Context& context, Parse::AnyClassDeclId node_id,
105105
SemIR::ClassDecl& class_decl,
106106
SemIR::Class& class_info, bool is_definition,
107107
SemIR::AccessKind access_kind) -> void {
108-
auto [prev_id, is_poisoned] = context.decl_name_stack().LookupOrAddName(
109-
name_context, class_decl_id, access_kind);
110-
if (is_poisoned) {
108+
SemIR::ScopeLookupResult lookup_result =
109+
context.decl_name_stack().LookupOrAddName(name_context, class_decl_id,
110+
access_kind);
111+
if (lookup_result.is_poisoned()) {
111112
// This is a declaration of a poisoned name.
112113
context.DiagnosePoisonedName(class_decl_id);
113114
return;
114115
}
115116

116-
if (!prev_id.has_value()) {
117+
if (!lookup_result.is_found()) {
117118
return;
118119
}
119120

121+
SemIR::InstId prev_id = lookup_result.target_inst_id();
122+
120123
auto prev_class_id = SemIR::ClassId::None;
121124
auto prev_import_ir_id = SemIR::ImportIRId::None;
122125
auto prev = context.insts().Get(prev_id);

toolchain/check/handle_interface.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,15 @@ static auto BuildInterfaceDecl(Context& context,
5959
SemIR::LibraryNameId::None)};
6060

6161
// Check whether this is a redeclaration.
62-
auto [existing_id, is_poisoned] = context.decl_name_stack().LookupOrAddName(
63-
name_context, interface_decl_id, introducer.modifier_set.GetAccessKind());
64-
if (is_poisoned) {
62+
SemIR::ScopeLookupResult lookup_result =
63+
context.decl_name_stack().LookupOrAddName(
64+
name_context, interface_decl_id,
65+
introducer.modifier_set.GetAccessKind());
66+
if (lookup_result.is_poisoned()) {
6567
// This is a declaration of a poisoned name.
6668
context.DiagnosePoisonedName(interface_decl_id);
67-
} else if (existing_id.has_value()) {
69+
} else if (lookup_result.is_found()) {
70+
SemIR::InstId existing_id = lookup_result.target_inst_id();
6871
if (auto existing_interface_decl =
6972
context.insts().Get(existing_id).TryAs<SemIR::InterfaceDecl>()) {
7073
auto existing_interface =

toolchain/check/handle_namespace.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,13 @@ auto HandleParseNode(Context& context, Parse::NamespaceId node_id) -> bool {
4242
auto namespace_id =
4343
context.AddPlaceholderInst(SemIR::LocIdAndInst(node_id, namespace_inst));
4444

45-
auto [existing_inst_id, is_poisoned] =
45+
SemIR::ScopeLookupResult lookup_result =
4646
context.decl_name_stack().LookupOrAddName(name_context, namespace_id,
4747
SemIR::AccessKind::Public);
48-
if (is_poisoned) {
48+
if (lookup_result.is_poisoned()) {
4949
context.DiagnosePoisonedName(namespace_id);
50-
} else if (existing_inst_id.has_value()) {
50+
} else if (lookup_result.is_found()) {
51+
SemIR::InstId existing_inst_id = lookup_result.target_inst_id();
5152
if (auto existing =
5253
context.insts().TryGetAs<SemIR::Namespace>(existing_inst_id)) {
5354
// If there's a name conflict with a namespace, "merge" by using the

0 commit comments

Comments
 (0)