Skip to content

Commit 395372b

Browse files
committed
Mostly fix lowering. Enums are still broken.
1 parent 83865f2 commit 395372b

32 files changed

+949
-1095
lines changed

toolchain/check/convert.cpp

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1282,16 +1282,21 @@ static auto IsCppEnum(Context& context, SemIR::TypeId type_id) -> bool {
12821282
}
12831283

12841284
// Given a value expression, form a corresponding initializer that copies from
1285-
// that value, if it is possible to do so.
1286-
static auto PerformCopy(Context& context, SemIR::InstId expr_id, bool diagnose)
1287-
-> SemIR::InstId {
1285+
// that value to the specified target, if it is possible to do so.
1286+
static auto PerformCopy(Context& context, SemIR::InstId expr_id,
1287+
SemIR::InstId target_id, PendingBlock& target_block,
1288+
bool diagnose) -> SemIR::InstId {
12881289
// TODO: We don't have a mechanism yet to generate `Copy` impls for each enum
12891290
// type imported from C++. For now we fake it by providing a direct copy.
1290-
if (IsCppEnum(context, context.insts().Get(expr_id).type_id())) {
1291-
return CopyValueToTemporary(context, expr_id);
1291+
auto type_id = context.insts().Get(expr_id).type_id();
1292+
if (IsCppEnum(context, type_id)) {
1293+
target_block.InsertHere();
1294+
return AddInst<SemIR::InitializeFrom>(
1295+
context, SemIR::LocId(expr_id),
1296+
{.type_id = type_id, .src_id = expr_id, .dest_id = target_id});
12921297
}
12931298

1294-
return BuildUnaryOperator(
1299+
auto copy_id = BuildUnaryOperator(
12951300
context, SemIR::LocId(expr_id), {"Copy"}, expr_id, [&] {
12961301
if (!diagnose) {
12971302
return context.emitter().BuildSuppressed();
@@ -1300,6 +1305,8 @@ static auto PerformCopy(Context& context, SemIR::InstId expr_id, bool diagnose)
13001305
"cannot copy value of type {0}", TypeOfInstId);
13011306
return context.emitter().Build(expr_id, CopyOfUncopyableType, expr_id);
13021307
});
1308+
MarkInitializerFor(context.sem_ir(), copy_id, target_id, target_block);
1309+
return copy_id;
13031310
}
13041311

13051312
// Convert a value expression so that it can be used to initialize a C++ thunk
@@ -1319,7 +1326,10 @@ static auto ConvertValueForCppThunkRef(Context& context, SemIR::InstId expr_id,
13191326

13201327
// Otherwise, we need a temporary to pass as the thunk argument. Create a copy
13211328
// and initialize a temporary from it.
1322-
expr_id = PerformCopy(context, expr_id, diagnose);
1329+
PendingBlock target_block(&context);
1330+
auto temporary_id = target_block.AddInst<SemIR::TemporaryStorage>(
1331+
SemIR::LocId(expr_id), {.type_id = expr.type_id()});
1332+
expr_id = PerformCopy(context, expr_id, temporary_id, target_block, diagnose);
13231333
return MaterializeIfInitializing(context, expr_id);
13241334
}
13251335

@@ -1577,7 +1587,8 @@ auto Convert(Context& context, SemIR::LocId loc_id, SemIR::InstId expr_id,
15771587

15781588
// When initializing from a value, perform a copy.
15791589
if (target.is_initializer()) {
1580-
expr_id = PerformCopy(context, expr_id, target.diagnose);
1590+
expr_id = PerformCopy(context, expr_id, target.init_id,
1591+
*target.init_block, target.diagnose);
15811592
}
15821593

15831594
// When initializing a C++ thunk parameter, form a reference, creating a

toolchain/check/testdata/class/generic/basic.carbon

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -268,8 +268,8 @@ class Declaration(T:! type);
268268
// CHECK:STDOUT: %require_complete.loc11: <witness> = require_complete_type %T.as_type.loc10_32.1 [symbolic = %require_complete.loc11 (constants.%require_complete.ecc)]
269269
// CHECK:STDOUT: %Copy.lookup_impl_witness: <witness> = lookup_impl_witness %T, @Copy [symbolic = %Copy.lookup_impl_witness (constants.%Copy.lookup_impl_witness.3ba)]
270270
// CHECK:STDOUT: %Copy.facet: %Copy.type = facet_value %T.as_type.loc10_32.1, (%Copy.lookup_impl_witness) [symbolic = %Copy.facet (constants.%Copy.facet.72e)]
271-
// CHECK:STDOUT: %.loc11_16.5: type = fn_type_with_self_type constants.%Copy.Op.type, %Copy.facet [symbolic = %.loc11_16.5 (constants.%.671)]
272-
// CHECK:STDOUT: %impl.elem0.loc11_16.2: @Class.GetValue.%.loc11_16.5 (%.671) = impl_witness_access %Copy.lookup_impl_witness, element0 [symbolic = %impl.elem0.loc11_16.2 (constants.%impl.elem0.56e)]
271+
// CHECK:STDOUT: %.loc11_16.4: type = fn_type_with_self_type constants.%Copy.Op.type, %Copy.facet [symbolic = %.loc11_16.4 (constants.%.671)]
272+
// CHECK:STDOUT: %impl.elem0.loc11_16.2: @Class.GetValue.%.loc11_16.4 (%.671) = impl_witness_access %Copy.lookup_impl_witness, element0 [symbolic = %impl.elem0.loc11_16.2 (constants.%impl.elem0.56e)]
273273
// CHECK:STDOUT: %specific_impl_fn.loc11_16.2: <specific function> = specific_impl_function %impl.elem0.loc11_16.2, @Copy.Op(%Copy.facet) [symbolic = %specific_impl_fn.loc11_16.2 (constants.%specific_impl_fn.fb7)]
274274
// CHECK:STDOUT:
275275
// CHECK:STDOUT: fn(%self.param: @Class.GetValue.%Class (%Class)) -> %return.param: @Class.GetValue.%T.as_type.loc10_32.1 (%T.as_type) {
@@ -278,13 +278,13 @@ class Declaration(T:! type);
278278
// CHECK:STDOUT: %k.ref: @Class.GetValue.%Class.elem (%Class.elem) = name_ref k, @Class.%.loc14_8 [concrete = @Class.%.loc14_8]
279279
// CHECK:STDOUT: %.loc11_16.1: ref @Class.GetValue.%T.as_type.loc10_32.1 (%T.as_type) = class_element_access %self.ref, element0
280280
// CHECK:STDOUT: %.loc11_16.2: @Class.GetValue.%T.as_type.loc10_32.1 (%T.as_type) = bind_value %.loc11_16.1
281-
// CHECK:STDOUT: %impl.elem0.loc11_16.1: @Class.GetValue.%.loc11_16.5 (%.671) = impl_witness_access constants.%Copy.lookup_impl_witness.3ba, element0 [symbolic = %impl.elem0.loc11_16.2 (constants.%impl.elem0.56e)]
281+
// CHECK:STDOUT: %impl.elem0.loc11_16.1: @Class.GetValue.%.loc11_16.4 (%.671) = impl_witness_access constants.%Copy.lookup_impl_witness.3ba, element0 [symbolic = %impl.elem0.loc11_16.2 (constants.%impl.elem0.56e)]
282282
// CHECK:STDOUT: %bound_method.loc11_16.1: <bound method> = bound_method %.loc11_16.2, %impl.elem0.loc11_16.1
283283
// CHECK:STDOUT: %specific_impl_fn.loc11_16.1: <specific function> = specific_impl_function %impl.elem0.loc11_16.1, @Copy.Op(constants.%Copy.facet.72e) [symbolic = %specific_impl_fn.loc11_16.2 (constants.%specific_impl_fn.fb7)]
284284
// CHECK:STDOUT: %bound_method.loc11_16.2: <bound method> = bound_method %.loc11_16.2, %specific_impl_fn.loc11_16.1
285-
// CHECK:STDOUT: %.loc11_16.3: ref @Class.GetValue.%T.as_type.loc10_32.1 (%T.as_type) = temporary_storage
286-
// CHECK:STDOUT: %.loc11_16.4: init @Class.GetValue.%T.as_type.loc10_32.1 (%T.as_type) = call %bound_method.loc11_16.2(%.loc11_16.2) to %.loc11_16.3
287-
// CHECK:STDOUT: return %.loc11_16.4 to %return
285+
// CHECK:STDOUT: %.loc10_29: ref @Class.GetValue.%T.as_type.loc10_32.1 (%T.as_type) = splice_block %return {}
286+
// CHECK:STDOUT: %.loc11_16.3: init @Class.GetValue.%T.as_type.loc10_32.1 (%T.as_type) = call %bound_method.loc11_16.2(%.loc11_16.2) to %.loc10_29
287+
// CHECK:STDOUT: return %.loc11_16.3 to %return
288288
// CHECK:STDOUT: }
289289
// CHECK:STDOUT: }
290290
// CHECK:STDOUT:

toolchain/check/testdata/class/generic/field.carbon

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -306,8 +306,8 @@ fn H(U:! Core.Copy, c: Class(U)) -> U {
306306
// CHECK:STDOUT: %Class.elem: type = unbound_element_type %Class.loc13_31.1, %T.as_type.loc13_31.1 [symbolic = %Class.elem (constants.%Class.elem.73af90.1)]
307307
// CHECK:STDOUT: %Copy.lookup_impl_witness: <witness> = lookup_impl_witness %T.loc13_6.1, @Copy [symbolic = %Copy.lookup_impl_witness (constants.%Copy.lookup_impl_witness.3ba2b3.1)]
308308
// CHECK:STDOUT: %Copy.facet: %Copy.type = facet_value %T.as_type.loc13_31.1, (%Copy.lookup_impl_witness) [symbolic = %Copy.facet (constants.%Copy.facet.72ea01.1)]
309-
// CHECK:STDOUT: %.loc14_11.5: type = fn_type_with_self_type constants.%Copy.Op.type, %Copy.facet [symbolic = %.loc14_11.5 (constants.%.671c3a.1)]
310-
// CHECK:STDOUT: %impl.elem0.loc14_11.2: @G.%.loc14_11.5 (%.671c3a.1) = impl_witness_access %Copy.lookup_impl_witness, element0 [symbolic = %impl.elem0.loc14_11.2 (constants.%impl.elem0.56eb4a.1)]
309+
// CHECK:STDOUT: %.loc14_11.4: type = fn_type_with_self_type constants.%Copy.Op.type, %Copy.facet [symbolic = %.loc14_11.4 (constants.%.671c3a.1)]
310+
// CHECK:STDOUT: %impl.elem0.loc14_11.2: @G.%.loc14_11.4 (%.671c3a.1) = impl_witness_access %Copy.lookup_impl_witness, element0 [symbolic = %impl.elem0.loc14_11.2 (constants.%impl.elem0.56eb4a.1)]
311311
// CHECK:STDOUT: %specific_impl_fn.loc14_11.2: <specific function> = specific_impl_function %impl.elem0.loc14_11.2, @Copy.Op(%Copy.facet) [symbolic = %specific_impl_fn.loc14_11.2 (constants.%specific_impl_fn.fb70c0.1)]
312312
// CHECK:STDOUT:
313313
// CHECK:STDOUT: fn(%c.param: @G.%Class.loc13_31.1 (%Class.b1d614.1)) -> %return.param: @G.%T.as_type.loc13_31.1 (%T.as_type) {
@@ -316,13 +316,13 @@ fn H(U:! Core.Copy, c: Class(U)) -> U {
316316
// CHECK:STDOUT: %x.ref: @G.%Class.elem (%Class.elem.73af90.1) = name_ref x, @Class.%.loc6 [concrete = @Class.%.loc6]
317317
// CHECK:STDOUT: %.loc14_11.1: ref @G.%T.as_type.loc13_31.1 (%T.as_type) = class_element_access %c.ref, element0
318318
// CHECK:STDOUT: %.loc14_11.2: @G.%T.as_type.loc13_31.1 (%T.as_type) = bind_value %.loc14_11.1
319-
// CHECK:STDOUT: %impl.elem0.loc14_11.1: @G.%.loc14_11.5 (%.671c3a.1) = impl_witness_access constants.%Copy.lookup_impl_witness.3ba2b3.1, element0 [symbolic = %impl.elem0.loc14_11.2 (constants.%impl.elem0.56eb4a.1)]
319+
// CHECK:STDOUT: %impl.elem0.loc14_11.1: @G.%.loc14_11.4 (%.671c3a.1) = impl_witness_access constants.%Copy.lookup_impl_witness.3ba2b3.1, element0 [symbolic = %impl.elem0.loc14_11.2 (constants.%impl.elem0.56eb4a.1)]
320320
// CHECK:STDOUT: %bound_method.loc14_11.1: <bound method> = bound_method %.loc14_11.2, %impl.elem0.loc14_11.1
321321
// CHECK:STDOUT: %specific_impl_fn.loc14_11.1: <specific function> = specific_impl_function %impl.elem0.loc14_11.1, @Copy.Op(constants.%Copy.facet.72ea01.1) [symbolic = %specific_impl_fn.loc14_11.2 (constants.%specific_impl_fn.fb70c0.1)]
322322
// CHECK:STDOUT: %bound_method.loc14_11.2: <bound method> = bound_method %.loc14_11.2, %specific_impl_fn.loc14_11.1
323-
// CHECK:STDOUT: %.loc14_11.3: ref @G.%T.as_type.loc13_31.1 (%T.as_type) = temporary_storage
324-
// CHECK:STDOUT: %.loc14_11.4: init @G.%T.as_type.loc13_31.1 (%T.as_type) = call %bound_method.loc14_11.2(%.loc14_11.2) to %.loc14_11.3
325-
// CHECK:STDOUT: return %.loc14_11.4 to %return
323+
// CHECK:STDOUT: %.loc13_34: ref @G.%T.as_type.loc13_31.1 (%T.as_type) = splice_block %return {}
324+
// CHECK:STDOUT: %.loc14_11.3: init @G.%T.as_type.loc13_31.1 (%T.as_type) = call %bound_method.loc14_11.2(%.loc14_11.2) to %.loc13_34
325+
// CHECK:STDOUT: return %.loc14_11.3 to %return
326326
// CHECK:STDOUT: }
327327
// CHECK:STDOUT: }
328328
// CHECK:STDOUT:
@@ -339,8 +339,8 @@ fn H(U:! Core.Copy, c: Class(U)) -> U {
339339
// CHECK:STDOUT: %Class.elem: type = unbound_element_type %Class.loc17_31.1, %U.as_type.loc17_31.1 [symbolic = %Class.elem (constants.%Class.elem.73af90.2)]
340340
// CHECK:STDOUT: %Copy.lookup_impl_witness: <witness> = lookup_impl_witness %U.loc17_6.1, @Copy [symbolic = %Copy.lookup_impl_witness (constants.%Copy.lookup_impl_witness.3ba2b3.2)]
341341
// CHECK:STDOUT: %Copy.facet: %Copy.type = facet_value %U.as_type.loc17_31.1, (%Copy.lookup_impl_witness) [symbolic = %Copy.facet (constants.%Copy.facet.72ea01.2)]
342-
// CHECK:STDOUT: %.loc18_11.5: type = fn_type_with_self_type constants.%Copy.Op.type, %Copy.facet [symbolic = %.loc18_11.5 (constants.%.671c3a.2)]
343-
// CHECK:STDOUT: %impl.elem0.loc18_11.2: @H.%.loc18_11.5 (%.671c3a.2) = impl_witness_access %Copy.lookup_impl_witness, element0 [symbolic = %impl.elem0.loc18_11.2 (constants.%impl.elem0.56eb4a.2)]
342+
// CHECK:STDOUT: %.loc18_11.4: type = fn_type_with_self_type constants.%Copy.Op.type, %Copy.facet [symbolic = %.loc18_11.4 (constants.%.671c3a.2)]
343+
// CHECK:STDOUT: %impl.elem0.loc18_11.2: @H.%.loc18_11.4 (%.671c3a.2) = impl_witness_access %Copy.lookup_impl_witness, element0 [symbolic = %impl.elem0.loc18_11.2 (constants.%impl.elem0.56eb4a.2)]
344344
// CHECK:STDOUT: %specific_impl_fn.loc18_11.2: <specific function> = specific_impl_function %impl.elem0.loc18_11.2, @Copy.Op(%Copy.facet) [symbolic = %specific_impl_fn.loc18_11.2 (constants.%specific_impl_fn.fb70c0.2)]
345345
// CHECK:STDOUT:
346346
// CHECK:STDOUT: fn(%c.param: @H.%Class.loc17_31.1 (%Class.b1d614.2)) -> %return.param: @H.%U.as_type.loc17_31.1 (%U.as_type.565) {
@@ -349,13 +349,13 @@ fn H(U:! Core.Copy, c: Class(U)) -> U {
349349
// CHECK:STDOUT: %x.ref: @H.%Class.elem (%Class.elem.73af90.2) = name_ref x, @Class.%.loc6 [concrete = @Class.%.loc6]
350350
// CHECK:STDOUT: %.loc18_11.1: ref @H.%U.as_type.loc17_31.1 (%U.as_type.565) = class_element_access %c.ref, element0
351351
// CHECK:STDOUT: %.loc18_11.2: @H.%U.as_type.loc17_31.1 (%U.as_type.565) = bind_value %.loc18_11.1
352-
// CHECK:STDOUT: %impl.elem0.loc18_11.1: @H.%.loc18_11.5 (%.671c3a.2) = impl_witness_access constants.%Copy.lookup_impl_witness.3ba2b3.2, element0 [symbolic = %impl.elem0.loc18_11.2 (constants.%impl.elem0.56eb4a.2)]
352+
// CHECK:STDOUT: %impl.elem0.loc18_11.1: @H.%.loc18_11.4 (%.671c3a.2) = impl_witness_access constants.%Copy.lookup_impl_witness.3ba2b3.2, element0 [symbolic = %impl.elem0.loc18_11.2 (constants.%impl.elem0.56eb4a.2)]
353353
// CHECK:STDOUT: %bound_method.loc18_11.1: <bound method> = bound_method %.loc18_11.2, %impl.elem0.loc18_11.1
354354
// CHECK:STDOUT: %specific_impl_fn.loc18_11.1: <specific function> = specific_impl_function %impl.elem0.loc18_11.1, @Copy.Op(constants.%Copy.facet.72ea01.2) [symbolic = %specific_impl_fn.loc18_11.2 (constants.%specific_impl_fn.fb70c0.2)]
355355
// CHECK:STDOUT: %bound_method.loc18_11.2: <bound method> = bound_method %.loc18_11.2, %specific_impl_fn.loc18_11.1
356-
// CHECK:STDOUT: %.loc18_11.3: ref @H.%U.as_type.loc17_31.1 (%U.as_type.565) = temporary_storage
357-
// CHECK:STDOUT: %.loc18_11.4: init @H.%U.as_type.loc17_31.1 (%U.as_type.565) = call %bound_method.loc18_11.2(%.loc18_11.2) to %.loc18_11.3
358-
// CHECK:STDOUT: return %.loc18_11.4 to %return
356+
// CHECK:STDOUT: %.loc17_34: ref @H.%U.as_type.loc17_31.1 (%U.as_type.565) = splice_block %return {}
357+
// CHECK:STDOUT: %.loc18_11.3: init @H.%U.as_type.loc17_31.1 (%U.as_type.565) = call %bound_method.loc18_11.2(%.loc18_11.2) to %.loc17_34
358+
// CHECK:STDOUT: return %.loc18_11.3 to %return
359359
// CHECK:STDOUT: }
360360
// CHECK:STDOUT: }
361361
// CHECK:STDOUT:

0 commit comments

Comments
 (0)