Skip to content

Commit

Permalink
Fix importing of the complete_type_witness for a generic class. (#4600)
Browse files Browse the repository at this point in the history
We need to create an instruction on import to attach the generic
constant value to.
  • Loading branch information
zygoloid authored and bricknerb committed Nov 28, 2024
1 parent ed26db5 commit ea00ab6
Show file tree
Hide file tree
Showing 41 changed files with 1,434 additions and 1,183 deletions.
12 changes: 8 additions & 4 deletions toolchain/check/import_ref.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1501,11 +1501,10 @@ static auto TryResolveTypedInst(ImportRefResolver& resolver,
LoadLocalPatternConstantIds(resolver, import_class.param_patterns_id);
auto generic_data = GetLocalGenericData(resolver, import_class.generic_id);
auto self_const_id = GetLocalConstantId(resolver, import_class.self_type_id);
auto complete_type_witness_id =
auto complete_type_witness_const_id =
import_class.complete_type_witness_id.is_valid()
? GetLocalConstantInstId(resolver,
import_class.complete_type_witness_id)
: SemIR::InstId::Invalid;
? GetLocalConstantId(resolver, import_class.complete_type_witness_id)
: SemIR::ConstantId::Invalid;
auto base_id = import_class.base_id.is_valid()
? GetLocalConstantInstId(resolver, import_class.base_id)
: SemIR::InstId::Invalid;
Expand All @@ -1529,6 +1528,11 @@ static auto TryResolveTypedInst(ImportRefResolver& resolver,
resolver.local_context().GetTypeIdForTypeConstant(self_const_id);

if (import_class.is_defined()) {
auto complete_type_witness_id = AddLoadedImportRef(
resolver,
resolver.local_context().GetBuiltinType(
SemIR::BuiltinInstKind::WitnessType),
import_class.complete_type_witness_id, complete_type_witness_const_id);
AddClassDefinition(resolver, import_class, new_class,
complete_type_witness_id, base_id, adapt_id);
}
Expand Down
36 changes: 20 additions & 16 deletions toolchain/check/testdata/alias/no_prelude/export_name.carbon
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ var d: D* = &c;
// CHECK:STDOUT: imports {
// CHECK:STDOUT: %import_ref.1 = import_ref Main//base, inst+1, unloaded
// CHECK:STDOUT: %import_ref.2: type = import_ref Main//base, inst+7, loaded [template = constants.%C]
// CHECK:STDOUT: %import_ref.3 = import_ref Main//base, inst+2, unloaded
// CHECK:STDOUT: %import_ref.3: <witness> = import_ref Main//base, inst+4, loaded [template = constants.%.2]
// CHECK:STDOUT: %import_ref.4 = import_ref Main//base, inst+2, unloaded
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: file {
Expand All @@ -119,8 +120,8 @@ var d: D* = &c;
// CHECK:STDOUT:
// CHECK:STDOUT: class @C {
// CHECK:STDOUT: !members:
// CHECK:STDOUT: .Self = imports.%import_ref.3
// CHECK:STDOUT: complete_type_witness = constants.%.2
// CHECK:STDOUT: .Self = imports.%import_ref.4
// CHECK:STDOUT: complete_type_witness = imports.%import_ref.3
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: --- export_orig.carbon
Expand All @@ -134,7 +135,8 @@ var d: D* = &c;
// CHECK:STDOUT: imports {
// CHECK:STDOUT: %import_ref.1: type = import_ref Main//base, inst+1, loaded [template = constants.%C]
// CHECK:STDOUT: %import_ref.2 = import_ref Main//base, inst+7, unloaded
// CHECK:STDOUT: %import_ref.3 = import_ref Main//base, inst+2, unloaded
// CHECK:STDOUT: %import_ref.3: <witness> = import_ref Main//base, inst+4, loaded [template = constants.%.2]
// CHECK:STDOUT: %import_ref.4 = import_ref Main//base, inst+2, unloaded
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: file {
Expand All @@ -148,8 +150,8 @@ var d: D* = &c;
// CHECK:STDOUT:
// CHECK:STDOUT: class @C {
// CHECK:STDOUT: !members:
// CHECK:STDOUT: .Self = imports.%import_ref.3
// CHECK:STDOUT: complete_type_witness = constants.%.2
// CHECK:STDOUT: .Self = imports.%import_ref.4
// CHECK:STDOUT: complete_type_witness = imports.%import_ref.3
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: --- use_export.carbon
Expand All @@ -162,8 +164,9 @@ var d: D* = &c;
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: imports {
// CHECK:STDOUT: %import_ref.1: type = import_ref Main//export, inst+9, loaded [template = constants.%C]
// CHECK:STDOUT: %import_ref.2 = import_ref Main//export, inst+8, unloaded
// CHECK:STDOUT: %import_ref.1: type = import_ref Main//export, inst+10, loaded [template = constants.%C]
// CHECK:STDOUT: %import_ref.2: <witness> = import_ref Main//export, inst+8, loaded [template = constants.%.2]
// CHECK:STDOUT: %import_ref.3 = import_ref Main//export, inst+9, unloaded
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: file {
Expand All @@ -179,8 +182,8 @@ var d: D* = &c;
// CHECK:STDOUT:
// CHECK:STDOUT: class @C {
// CHECK:STDOUT: !members:
// CHECK:STDOUT: .Self = imports.%import_ref.2
// CHECK:STDOUT: complete_type_witness = constants.%.2
// CHECK:STDOUT: .Self = imports.%import_ref.3
// CHECK:STDOUT: complete_type_witness = imports.%import_ref.2
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: fn @__global_init() {
Expand All @@ -199,7 +202,7 @@ var d: D* = &c;
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: imports {
// CHECK:STDOUT: %import_ref = import_ref Main//export, inst+9, unloaded
// CHECK:STDOUT: %import_ref = import_ref Main//export, inst+10, unloaded
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: file {
Expand Down Expand Up @@ -231,9 +234,10 @@ var d: D* = &c;
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: imports {
// CHECK:STDOUT: %import_ref.1: type = import_ref Main//export, inst+9, loaded [template = constants.%C]
// CHECK:STDOUT: %import_ref.2: type = import_ref Main//export_orig, inst+9, loaded [template = constants.%C]
// CHECK:STDOUT: %import_ref.3 = import_ref Main//export_orig, inst+8, unloaded
// CHECK:STDOUT: %import_ref.1: type = import_ref Main//export, inst+10, loaded [template = constants.%C]
// CHECK:STDOUT: %import_ref.2: type = import_ref Main//export_orig, inst+10, loaded [template = constants.%C]
// CHECK:STDOUT: %import_ref.3: <witness> = import_ref Main//export_orig, inst+8, loaded [template = constants.%.2]
// CHECK:STDOUT: %import_ref.4 = import_ref Main//export_orig, inst+9, unloaded
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: file {
Expand All @@ -255,8 +259,8 @@ var d: D* = &c;
// CHECK:STDOUT:
// CHECK:STDOUT: class @C {
// CHECK:STDOUT: !members:
// CHECK:STDOUT: .Self = imports.%import_ref.3
// CHECK:STDOUT: complete_type_witness = constants.%.2
// CHECK:STDOUT: .Self = imports.%import_ref.4
// CHECK:STDOUT: complete_type_witness = imports.%import_ref.3
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: fn @__global_init() {
Expand Down
18 changes: 10 additions & 8 deletions toolchain/check/testdata/alias/no_prelude/import.carbon
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,8 @@ var c: () = a_alias_alias;
// CHECK:STDOUT: %import_ref.1 = import_ref Main//class1, inst+1, unloaded
// CHECK:STDOUT: %import_ref.2: type = import_ref Main//class1, inst+7, loaded [template = constants.%C]
// CHECK:STDOUT: %import_ref.3 = import_ref Main//class1, inst+12, unloaded
// CHECK:STDOUT: %import_ref.4 = import_ref Main//class1, inst+2, unloaded
// CHECK:STDOUT: %import_ref.4: <witness> = import_ref Main//class1, inst+4, loaded [template = constants.%.2]
// CHECK:STDOUT: %import_ref.5 = import_ref Main//class1, inst+2, unloaded
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: file {
Expand All @@ -133,8 +134,8 @@ var c: () = a_alias_alias;
// CHECK:STDOUT:
// CHECK:STDOUT: class @C {
// CHECK:STDOUT: !members:
// CHECK:STDOUT: .Self = imports.%import_ref.4
// CHECK:STDOUT: complete_type_witness = constants.%.2
// CHECK:STDOUT: .Self = imports.%import_ref.5
// CHECK:STDOUT: complete_type_witness = imports.%import_ref.4
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: --- class3.carbon
Expand All @@ -147,9 +148,10 @@ var c: () = a_alias_alias;
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: imports {
// CHECK:STDOUT: %import_ref.1: type = import_ref Main//class2, inst+11, loaded [template = constants.%C]
// CHECK:STDOUT: %import_ref.2 = import_ref Main//class2, inst+16, unloaded
// CHECK:STDOUT: %import_ref.3 = import_ref Main//class2, inst+9, unloaded
// CHECK:STDOUT: %import_ref.1: type = import_ref Main//class2, inst+12, loaded [template = constants.%C]
// CHECK:STDOUT: %import_ref.2 = import_ref Main//class2, inst+17, unloaded
// CHECK:STDOUT: %import_ref.3: <witness> = import_ref Main//class2, inst+9, loaded [template = constants.%.2]
// CHECK:STDOUT: %import_ref.4 = import_ref Main//class2, inst+10, unloaded
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: file {
Expand All @@ -167,8 +169,8 @@ var c: () = a_alias_alias;
// CHECK:STDOUT:
// CHECK:STDOUT: class @C {
// CHECK:STDOUT: !members:
// CHECK:STDOUT: .Self = imports.%import_ref.3
// CHECK:STDOUT: complete_type_witness = constants.%.2
// CHECK:STDOUT: .Self = imports.%import_ref.4
// CHECK:STDOUT: complete_type_witness = imports.%import_ref.3
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: --- var1.carbon
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ var inst: Test.A = {};
// CHECK:STDOUT: imports {
// CHECK:STDOUT: %import_ref.1 = import_ref Test//def, inst+1, unloaded
// CHECK:STDOUT: %import_ref.2: type = import_ref Test//def, inst+7, loaded [template = constants.%C]
// CHECK:STDOUT: %import_ref.3 = import_ref Test//def, inst+2, unloaded
// CHECK:STDOUT: %import_ref.3: <witness> = import_ref Test//def, inst+4, loaded [template = constants.%.2]
// CHECK:STDOUT: %import_ref.4 = import_ref Test//def, inst+2, unloaded
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: file {
Expand All @@ -108,8 +109,8 @@ var inst: Test.A = {};
// CHECK:STDOUT:
// CHECK:STDOUT: class @C {
// CHECK:STDOUT: !members:
// CHECK:STDOUT: .Self = imports.%import_ref.3
// CHECK:STDOUT: complete_type_witness = constants.%.2
// CHECK:STDOUT: .Self = imports.%import_ref.4
// CHECK:STDOUT: complete_type_witness = imports.%import_ref.3
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: fn @__global_init() {
Expand Down
17 changes: 9 additions & 8 deletions toolchain/check/testdata/alias/no_prelude/import_order.carbon
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,9 @@ var a_val: a = {.v = b_val.v};
// CHECK:STDOUT: %import_ref.3: type = import_ref Main//a, inst+14, loaded [template = constants.%C]
// CHECK:STDOUT: %import_ref.4: type = import_ref Main//a, inst+16, loaded [template = constants.%C]
// CHECK:STDOUT: %import_ref.5: type = import_ref Main//a, inst+18, loaded [template = constants.%C]
// CHECK:STDOUT: %import_ref.6 = import_ref Main//a, inst+2, unloaded
// CHECK:STDOUT: %import_ref.7: %.4 = import_ref Main//a, inst+7, loaded [template = %.1]
// CHECK:STDOUT: %import_ref.6: <witness> = import_ref Main//a, inst+9, loaded [template = constants.%.2]
// CHECK:STDOUT: %import_ref.7 = import_ref Main//a, inst+2, unloaded
// CHECK:STDOUT: %import_ref.8: %.4 = import_ref Main//a, inst+7, loaded [template = %.1]
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: file {
Expand Down Expand Up @@ -122,9 +123,9 @@ var a_val: a = {.v = b_val.v};
// CHECK:STDOUT:
// CHECK:STDOUT: class @C {
// CHECK:STDOUT: !members:
// CHECK:STDOUT: .Self = imports.%import_ref.6
// CHECK:STDOUT: .v = imports.%import_ref.7
// CHECK:STDOUT: complete_type_witness = constants.%.2
// CHECK:STDOUT: .Self = imports.%import_ref.7
// CHECK:STDOUT: .v = imports.%import_ref.8
// CHECK:STDOUT: complete_type_witness = imports.%import_ref.6
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: fn @__global_init() {
Expand All @@ -138,7 +139,7 @@ var a_val: a = {.v = b_val.v};
// CHECK:STDOUT: %.loc7_25: init %C = converted %.loc7_24.1, %.loc7_24.4 [template = constants.%struct]
// CHECK:STDOUT: assign file.%d_val.var, %.loc7_25
// CHECK:STDOUT: %d_val.ref: ref %C = name_ref d_val, file.%d_val
// CHECK:STDOUT: %v.ref.loc8: %.4 = name_ref v, imports.%import_ref.7 [template = imports.%.1]
// CHECK:STDOUT: %v.ref.loc8: %.4 = name_ref v, imports.%import_ref.8 [template = imports.%.1]
// CHECK:STDOUT: %.loc8_27.1: ref %empty_tuple.type = class_element_access %d_val.ref, element0
// CHECK:STDOUT: %.loc8_29.1: %.1 = struct_literal (%.loc8_27.1)
// CHECK:STDOUT: %.loc8_29.2: ref %empty_tuple.type = class_element_access file.%c_val.var, element0
Expand All @@ -148,7 +149,7 @@ var a_val: a = {.v = b_val.v};
// CHECK:STDOUT: %.loc8_30: init %C = converted %.loc8_29.1, %.loc8_29.4 [template = constants.%struct]
// CHECK:STDOUT: assign file.%c_val.var, %.loc8_30
// CHECK:STDOUT: %c_val.ref: ref %C = name_ref c_val, file.%c_val
// CHECK:STDOUT: %v.ref.loc9: %.4 = name_ref v, imports.%import_ref.7 [template = imports.%.1]
// CHECK:STDOUT: %v.ref.loc9: %.4 = name_ref v, imports.%import_ref.8 [template = imports.%.1]
// CHECK:STDOUT: %.loc9_27.1: ref %empty_tuple.type = class_element_access %c_val.ref, element0
// CHECK:STDOUT: %.loc9_29.1: %.1 = struct_literal (%.loc9_27.1)
// CHECK:STDOUT: %.loc9_29.2: ref %empty_tuple.type = class_element_access file.%b_val.var, element0
Expand All @@ -158,7 +159,7 @@ var a_val: a = {.v = b_val.v};
// CHECK:STDOUT: %.loc9_30: init %C = converted %.loc9_29.1, %.loc9_29.4 [template = constants.%struct]
// CHECK:STDOUT: assign file.%b_val.var, %.loc9_30
// CHECK:STDOUT: %b_val.ref: ref %C = name_ref b_val, file.%b_val
// CHECK:STDOUT: %v.ref.loc10: %.4 = name_ref v, imports.%import_ref.7 [template = imports.%.1]
// CHECK:STDOUT: %v.ref.loc10: %.4 = name_ref v, imports.%import_ref.8 [template = imports.%.1]
// CHECK:STDOUT: %.loc10_27.1: ref %empty_tuple.type = class_element_access %b_val.ref, element0
// CHECK:STDOUT: %.loc10_29.1: %.1 = struct_literal (%.loc10_27.1)
// CHECK:STDOUT: %.loc10_29.2: ref %empty_tuple.type = class_element_access file.%a_val.var, element0
Expand Down
21 changes: 12 additions & 9 deletions toolchain/check/testdata/class/cross_package_import.carbon
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,8 @@ var c: Other.C = {};
// CHECK:STDOUT: import Other//other_define
// CHECK:STDOUT: }
// CHECK:STDOUT: %import_ref.1: type = import_ref Other//other_define, inst+3, loaded [template = constants.%C]
// CHECK:STDOUT: %import_ref.2 = import_ref Other//other_define, inst+4, unloaded
// CHECK:STDOUT: %import_ref.2: <witness> = import_ref Other//other_define, inst+6, loaded [template = constants.%.2]
// CHECK:STDOUT: %import_ref.3 = import_ref Other//other_define, inst+4, unloaded
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: file {
Expand All @@ -221,8 +222,8 @@ var c: Other.C = {};
// CHECK:STDOUT:
// CHECK:STDOUT: class @C {
// CHECK:STDOUT: !members:
// CHECK:STDOUT: .Self = imports.%import_ref.2
// CHECK:STDOUT: complete_type_witness = constants.%.2
// CHECK:STDOUT: .Self = imports.%import_ref.3
// CHECK:STDOUT: complete_type_witness = imports.%import_ref.2
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: fn @__global_init() {
Expand Down Expand Up @@ -296,7 +297,8 @@ var c: Other.C = {};
// CHECK:STDOUT: import Other//other_extern
// CHECK:STDOUT: }
// CHECK:STDOUT: %import_ref.1: type = import_ref Other//other_define, inst+3, loaded [template = constants.%C]
// CHECK:STDOUT: %import_ref.2 = import_ref Other//other_define, inst+4, unloaded
// CHECK:STDOUT: %import_ref.2: <witness> = import_ref Other//other_define, inst+6, loaded [template = constants.%.2]
// CHECK:STDOUT: %import_ref.3 = import_ref Other//other_define, inst+4, unloaded
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: file {
Expand All @@ -315,8 +317,8 @@ var c: Other.C = {};
// CHECK:STDOUT:
// CHECK:STDOUT: class @C {
// CHECK:STDOUT: !members:
// CHECK:STDOUT: .Self = imports.%import_ref.2
// CHECK:STDOUT: complete_type_witness = constants.%.2
// CHECK:STDOUT: .Self = imports.%import_ref.3
// CHECK:STDOUT: complete_type_witness = imports.%import_ref.2
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: fn @__global_init() {
Expand Down Expand Up @@ -348,7 +350,8 @@ var c: Other.C = {};
// CHECK:STDOUT: import Other//other_conflict
// CHECK:STDOUT: }
// CHECK:STDOUT: %import_ref.1: type = import_ref Other//other_define, inst+3, loaded [template = constants.%C]
// CHECK:STDOUT: %import_ref.2 = import_ref Other//other_define, inst+4, unloaded
// CHECK:STDOUT: %import_ref.2: <witness> = import_ref Other//other_define, inst+6, loaded [template = constants.%.2]
// CHECK:STDOUT: %import_ref.3 = import_ref Other//other_define, inst+4, unloaded
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: file {
Expand All @@ -367,8 +370,8 @@ var c: Other.C = {};
// CHECK:STDOUT:
// CHECK:STDOUT: class @C {
// CHECK:STDOUT: !members:
// CHECK:STDOUT: .Self = imports.%import_ref.2
// CHECK:STDOUT: complete_type_witness = constants.%.2
// CHECK:STDOUT: .Self = imports.%import_ref.3
// CHECK:STDOUT: complete_type_witness = imports.%import_ref.2
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: fn @__global_init() {
Expand Down
7 changes: 4 additions & 3 deletions toolchain/check/testdata/class/fail_import_misuses.carbon
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ var a: Incomplete;
// CHECK:STDOUT: import Core//prelude
// CHECK:STDOUT: import Core//prelude/...
// CHECK:STDOUT: }
// CHECK:STDOUT: %import_ref.3 = import_ref Main//a, inst+4, unloaded
// CHECK:STDOUT: %import_ref.3: <witness> = import_ref Main//a, inst+6, loaded [template = constants.%.2]
// CHECK:STDOUT: %import_ref.4 = import_ref Main//a, inst+4, unloaded
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: file {
Expand All @@ -117,8 +118,8 @@ var a: Incomplete;
// CHECK:STDOUT:
// CHECK:STDOUT: class @Empty {
// CHECK:STDOUT: !members:
// CHECK:STDOUT: .Self = imports.%import_ref.3
// CHECK:STDOUT: complete_type_witness = constants.%.2
// CHECK:STDOUT: .Self = imports.%import_ref.4
// CHECK:STDOUT: complete_type_witness = imports.%import_ref.3
// CHECK:STDOUT: }
// CHECK:STDOUT:
// CHECK:STDOUT: class @.1 {
Expand Down
Loading

0 comments on commit ea00ab6

Please sign in to comment.