Skip to content

Commit d7e37c3

Browse files
committed
move-to-facet-valuetype
1 parent c4c8032 commit d7e37c3

File tree

4 files changed

+37
-25
lines changed

4 files changed

+37
-25
lines changed

toolchain/check/member_access.cpp

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -320,18 +320,8 @@ static auto LookupMemberNameInScope(Context& context, SemIR::LocId loc_id,
320320
if (auto assoc_type =
321321
context.types().TryGetAs<SemIR::AssociatedEntityType>(type_id)) {
322322
if (lookup_in_type_of_base) {
323+
base_id = MoveToFacetValue(context, base_id);
323324
SemIR::TypeId base_type_id = context.insts().Get(base_id).type_id();
324-
if (auto binding_type =
325-
context.types().TryGetAs<SemIR::SymbolicBindingType>(
326-
base_type_id)) {
327-
// Move from the type of a symbolic facet value up in typish-ness to its
328-
// FacetType to find the type to work with.
329-
//
330-
// TODO: Look in ScopeStack with the the entity_name_id to find the
331-
// facet value.
332-
base_id = binding_type->facet_value_inst_id;
333-
base_type_id = context.insts().Get(base_id).type_id();
334-
}
335325

336326
if (auto facet_type =
337327
context.types().TryGetAs<SemIR::FacetType>(base_type_id)) {

toolchain/check/name_lookup.cpp

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "toolchain/check/import.h"
1212
#include "toolchain/check/import_ref.h"
1313
#include "toolchain/check/member_access.h"
14+
#include "toolchain/check/type.h"
1415
#include "toolchain/check/type_completion.h"
1516
#include "toolchain/diagnostics/format_providers.h"
1617
#include "toolchain/sem_ir/generic.h"
@@ -272,22 +273,15 @@ auto AppendLookupScopesForConstant(Context& context, SemIR::LocId loc_id,
272273
llvm::SmallVector<LookupScope>* scopes)
273274
-> bool {
274275
auto base_id = context.constant_values().GetInstId(base_const_id);
275-
auto base = context.insts().Get(base_id);
276276

277-
if (auto base_as_binding_type = base.TryAs<SemIR::SymbolicBindingType>()) {
278-
// Move from the symbolic facet value up in typish-ness to its FacetType to
279-
// find a lookup scope.
280-
//
281-
// TODO: Look in ScopeStack with the the entity_name_id to find the
282-
// facet value.
283-
auto facet_type_type_id =
284-
context.insts()
285-
.Get(base_as_binding_type->facet_value_inst_id)
286-
.type_id();
287-
base_const_id = context.types().GetConstantId(facet_type_type_id);
288-
base_id = context.constant_values().GetInstId(base_const_id);
289-
base = context.insts().Get(base_id);
277+
// TODO: We only want to move from type to FacetType if the original type in
278+
// `base_const_id` is `(facet value) as type`. Why do we not want to do this
279+
// for other facet values that are not converted to type?
280+
if (context.insts().Get(base_id).type_id() == SemIR::TypeType::TypeId) {
281+
base_id = MoveToFacetType(context, base_id);
282+
base_const_id = context.constant_values().Get(base_id);
290283
}
284+
auto base = context.insts().Get(base_id);
291285

292286
if (auto base_as_namespace = base.TryAs<SemIR::Namespace>()) {
293287
scopes->push_back(

toolchain/check/type.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -302,4 +302,23 @@ auto GetCanonicalizedFacetTypeOrType(Context& context, SemIR::InstId inst_id)
302302
}
303303
}
304304

305+
auto MoveToFacetValue(Context& context, SemIR::InstId inst_id)
306+
-> SemIR::InstId {
307+
auto type_id = context.insts().Get(inst_id).type_id();
308+
if (context.types().Is<SemIR::SymbolicBindingType>(type_id)) {
309+
return GetCanonicalizedFacetOrTypeValue(context,
310+
context.types().GetInstId(type_id));
311+
}
312+
return inst_id;
313+
}
314+
315+
auto MoveToFacetType(Context& context, SemIR::InstId inst_id) -> SemIR::InstId {
316+
auto facet_value_id = GetCanonicalizedFacetOrTypeValue(context, inst_id);
317+
auto type_id = context.insts().Get(facet_value_id).type_id();
318+
if (context.types().Is<SemIR::FacetType>(type_id)) {
319+
return context.types().GetInstId(type_id);
320+
}
321+
return facet_value_id;
322+
}
323+
305324
} // namespace Carbon::Check

toolchain/check/type.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,15 @@ auto GetCanonicalizedFacetOrTypeValue(Context& context,
134134
auto GetCanonicalizedFacetTypeOrType(Context& context, SemIR::InstId inst_id)
135135
-> SemIR::TypeInstId;
136136

137+
// If `inst_id` has a type which is a facet value, return the canonicalized
138+
// facet value, otherwise return `inst_id` unchanged.
139+
auto MoveToFacetValue(Context& context, SemIR::InstId inst_id) -> SemIR::InstId;
140+
141+
// If `inst_id` is a facet value, return the canonicalized facet value's type
142+
// (a FacetType or a concrete type value), otherwise return `inst_id`
143+
// unchanged.
144+
auto MoveToFacetType(Context& context, SemIR::InstId inst_id) -> SemIR::InstId;
145+
137146
} // namespace Carbon::Check
138147

139148
#endif // CARBON_TOOLCHAIN_CHECK_TYPE_H_

0 commit comments

Comments
 (0)