Skip to content

Commit eb0dcc8

Browse files
authored
Import generic named constraints (#6376)
We add tests showing that `ImplStore::GetOrAddLookupBucket` is doing the wrong thing for impls of a named constraint, as the impl-file redeclarations of impls in the api file are not getting flagged as such. To do the right thing requires us to be able to get the constraint from a require declaration with the specific of the named constraint/interface applied, which is future work as described in the [open discussion notes](https://docs.google.com/document/d/1Yt-i5AmF76LSvD4TrWRIAE_92kii6j5yFiW-S7ahzlg/edit?tab=t.1ji9ixn9bbnn#heading=h.kijomnov90rz).
1 parent 57a2715 commit eb0dcc8

File tree

4 files changed

+915
-44
lines changed

4 files changed

+915
-44
lines changed

toolchain/check/import_ref.cpp

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -852,6 +852,11 @@ static auto GetLocalGenericId(ImportContext& context,
852852
.Get(interface_type.interface_id)
853853
.generic_id;
854854
}
855+
case CARBON_KIND(SemIR::GenericNamedConstraintType constraint_type): {
856+
return context.local_named_constraints()
857+
.Get(constraint_type.named_constraint_id)
858+
.generic_id;
859+
}
855860
case CARBON_KIND(SemIR::ImplDecl impl_decl): {
856861
return context.local_impls().Get(impl_decl.impl_id).generic_id;
857862
}
@@ -1070,6 +1075,11 @@ static auto GetLocalNameScopeIdImpl(ImportRefResolver& resolver,
10701075
case CARBON_KIND(SemIR::GenericInterfaceType inst): {
10711076
return resolver.local_interfaces().Get(inst.interface_id).scope_id;
10721077
}
1078+
case CARBON_KIND(SemIR::GenericNamedConstraintType inst): {
1079+
return resolver.local_named_constraints()
1080+
.Get(inst.named_constraint_id)
1081+
.scope_id;
1082+
}
10731083
default: {
10741084
break;
10751085
}
@@ -2272,6 +2282,24 @@ static auto TryResolveTypedInst(ImportRefResolver& resolver,
22722282
resolver.local_types().GetConstantId(interface_val.type_id()));
22732283
}
22742284

2285+
static auto TryResolveTypedInst(ImportRefResolver& resolver,
2286+
SemIR::GenericNamedConstraintType inst)
2287+
-> ResolveResult {
2288+
CARBON_CHECK(inst.type_id == SemIR::TypeType::TypeId);
2289+
auto constraint_val_id =
2290+
GetLocalConstantInstId(resolver, resolver.import_named_constraints()
2291+
.Get(inst.named_constraint_id)
2292+
.first_owning_decl_id);
2293+
if (resolver.HasNewWork()) {
2294+
return ResolveResult::Retry();
2295+
}
2296+
auto constraint_val = resolver.local_insts().Get(constraint_val_id);
2297+
CARBON_CHECK(resolver.local_types().Is<SemIR::GenericNamedConstraintType>(
2298+
constraint_val.type_id()));
2299+
return ResolveResult::Done(
2300+
resolver.local_types().GetConstantId(constraint_val.type_id()));
2301+
}
2302+
22752303
// Make a declaration of an impl. This is done as a separate step from
22762304
// importing the impl definition in order to resolve cycles.
22772305
static auto MakeImplDeclaration(ImportContext& context,
@@ -2775,7 +2803,7 @@ static auto TryResolveTypedInst(ImportRefResolver& resolver,
27752803
resolver, import_named_constraint.require_impls_block_id,
27762804
require_impls);
27772805
SetGenericData(resolver, import_named_constraint.generic_id,
2778-
import_named_constraint.generic_id, generic_data);
2806+
new_named_constraint.generic_id, generic_data);
27792807

27802808
if (import_named_constraint.is_complete()) {
27812809
CARBON_CHECK(self_param_id);
@@ -3522,6 +3550,9 @@ static auto TryResolveInstCanonical(ImportRefResolver& resolver,
35223550
case CARBON_KIND(SemIR::GenericInterfaceType inst): {
35233551
return TryResolveTypedInst(resolver, inst);
35243552
}
3553+
case CARBON_KIND(SemIR::GenericNamedConstraintType inst): {
3554+
return TryResolveTypedInst(resolver, inst);
3555+
}
35253556
case CARBON_KIND(SemIR::LookupImplWitness inst): {
35263557
return TryResolveTypedInst(resolver, inst);
35273558
}

toolchain/check/testdata/impl/impl_as_named_constraint.carbon

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,3 +133,18 @@ constraint C {
133133
// CHECK:STDERR: ^~~~~~~~~~~~~~
134134
// CHECK:STDERR:
135135
impl () as C {}
136+
137+
// --- todo_fail_duplicate_through_generic_constraint.carbon
138+
library "[[@TEST_NAME]]";
139+
140+
interface A(T:! type) {}
141+
142+
constraint B(X:! type, Y:! type) {
143+
extend require impls A(Y);
144+
}
145+
146+
// This should impl () as A({}).
147+
impl () as B((), {}) {}
148+
149+
// This should be considered a duplicate.
150+
impl () as A({}) {}

0 commit comments

Comments
 (0)