Skip to content

Commit 08e56df

Browse files
committed
Fix up enum handling.
1 parent 395372b commit 08e56df

File tree

5 files changed

+37
-41
lines changed

5 files changed

+37
-41
lines changed

toolchain/check/convert.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1290,10 +1290,7 @@ static auto PerformCopy(Context& context, SemIR::InstId expr_id,
12901290
// type imported from C++. For now we fake it by providing a direct copy.
12911291
auto type_id = context.insts().Get(expr_id).type_id();
12921292
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});
1293+
return expr_id;
12971294
}
12981295

12991296
auto copy_id = BuildUnaryOperator(
@@ -1326,11 +1323,14 @@ static auto ConvertValueForCppThunkRef(Context& context, SemIR::InstId expr_id,
13261323

13271324
// Otherwise, we need a temporary to pass as the thunk argument. Create a copy
13281325
// and initialize a temporary from it.
1326+
auto temporary_id = AddInst<SemIR::TemporaryStorage>(
1327+
context, SemIR::LocId(expr_id), {.type_id = expr.type_id()});
13291328
PendingBlock target_block(&context);
1330-
auto temporary_id = target_block.AddInst<SemIR::TemporaryStorage>(
1331-
SemIR::LocId(expr_id), {.type_id = expr.type_id()});
13321329
expr_id = PerformCopy(context, expr_id, temporary_id, target_block, diagnose);
1333-
return MaterializeIfInitializing(context, expr_id);
1330+
return AddInstWithCleanup<SemIR::Temporary>(context, SemIR::LocId(expr_id),
1331+
{.type_id = expr.type_id(),
1332+
.storage_id = temporary_id,
1333+
.init_id = expr_id});
13341334
}
13351335

13361336
// Returns the Core interface name to use for a given kind of conversion.

toolchain/check/testdata/interop/cpp/enum/anonymous.carbon

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,8 @@ fn G() {
124124
// CHECK:STDOUT: %Cpp.ref.loc8_9: <namespace> = name_ref Cpp, imports.%Cpp [concrete = imports.%Cpp]
125125
// CHECK:STDOUT: %b.ref: %.4f0 = name_ref b, imports.%int_1.81a [concrete = constants.%int_1.81a]
126126
// CHECK:STDOUT: %.loc8_12.1: ref %.4f0 = temporary_storage
127-
// CHECK:STDOUT: %.loc8_12.2: init %.4f0 = initialize_from %b.ref to %.loc8_12.1 [concrete = constants.%int_1.81a]
128-
// CHECK:STDOUT: %.loc8_12.3: ref %.4f0 = temporary %.loc8_12.1, %.loc8_12.2
129-
// CHECK:STDOUT: %addr.loc8_14: %ptr.793 = addr_of %.loc8_12.3
127+
// CHECK:STDOUT: %.loc8_12.2: ref %.4f0 = temporary %.loc8_12.1, %b.ref
128+
// CHECK:STDOUT: %addr.loc8_14: %ptr.793 = addr_of %.loc8_12.2
130129
// CHECK:STDOUT: %F__carbon_thunk.call.loc8: init %empty_tuple.type = call imports.%F__carbon_thunk.decl.e1b8ec.1(%addr.loc8_14)
131130
// CHECK:STDOUT: %Cpp.ref.loc10_3: <namespace> = name_ref Cpp, imports.%Cpp [concrete = imports.%Cpp]
132131
// CHECK:STDOUT: %C.ref.loc10_6: type = name_ref C, imports.%C.decl [concrete = constants.%C]
@@ -143,24 +142,23 @@ fn G() {
143142
// CHECK:STDOUT: %e.ref: %.bb7 = name_ref e, imports.%int_1.1d6 [concrete = constants.%int_1.1d6]
144143
// CHECK:STDOUT: %addr.loc10_11.2: %ptr.d9e = addr_of %.loc10_11.3
145144
// CHECK:STDOUT: %.loc10_20.1: ref %.bb7 = temporary_storage
146-
// CHECK:STDOUT: %.loc10_20.2: init %.bb7 = initialize_from %e.ref to %.loc10_20.1 [concrete = constants.%int_1.1d6]
147-
// CHECK:STDOUT: %.loc10_20.3: ref %.bb7 = temporary %.loc10_20.1, %.loc10_20.2
148-
// CHECK:STDOUT: %addr.loc10_22: %ptr.73d = addr_of %.loc10_20.3
145+
// CHECK:STDOUT: %.loc10_20.2: ref %.bb7 = temporary %.loc10_20.1, %e.ref
146+
// CHECK:STDOUT: %addr.loc10_22: %ptr.73d = addr_of %.loc10_20.2
149147
// CHECK:STDOUT: %F__carbon_thunk.call.loc10: init %empty_tuple.type = call imports.%F__carbon_thunk.decl.e1b8ec.2(%addr.loc10_11.2, %addr.loc10_22)
150-
// CHECK:STDOUT: %T.as.Destroy.impl.Op.bound.loc10_20: <bound method> = bound_method %.loc10_20.3, constants.%T.as.Destroy.impl.Op.823
148+
// CHECK:STDOUT: %T.as.Destroy.impl.Op.bound.loc10_20: <bound method> = bound_method %.loc10_20.2, constants.%T.as.Destroy.impl.Op.823
151149
// CHECK:STDOUT: <elided>
152-
// CHECK:STDOUT: %bound_method.loc10_20: <bound method> = bound_method %.loc10_20.3, %T.as.Destroy.impl.Op.specific_fn.1
153-
// CHECK:STDOUT: %addr.loc10_20: %ptr.73d = addr_of %.loc10_20.3
150+
// CHECK:STDOUT: %bound_method.loc10_20: <bound method> = bound_method %.loc10_20.2, %T.as.Destroy.impl.Op.specific_fn.1
151+
// CHECK:STDOUT: %addr.loc10_20: %ptr.73d = addr_of %.loc10_20.2
154152
// CHECK:STDOUT: %T.as.Destroy.impl.Op.call.loc10_20: init %empty_tuple.type = call %bound_method.loc10_20(%addr.loc10_20)
155153
// CHECK:STDOUT: %T.as.Destroy.impl.Op.bound.loc10_11: <bound method> = bound_method %.loc10_11.3, constants.%T.as.Destroy.impl.Op.d4e
156154
// CHECK:STDOUT: <elided>
157155
// CHECK:STDOUT: %bound_method.loc10_11: <bound method> = bound_method %.loc10_11.3, %T.as.Destroy.impl.Op.specific_fn.2
158156
// CHECK:STDOUT: %addr.loc10_11.3: %ptr.d9e = addr_of %.loc10_11.3
159157
// CHECK:STDOUT: %T.as.Destroy.impl.Op.call.loc10_11: init %empty_tuple.type = call %bound_method.loc10_11(%addr.loc10_11.3)
160-
// CHECK:STDOUT: %T.as.Destroy.impl.Op.bound.loc8: <bound method> = bound_method %.loc8_12.3, constants.%T.as.Destroy.impl.Op.8db
158+
// CHECK:STDOUT: %T.as.Destroy.impl.Op.bound.loc8: <bound method> = bound_method %.loc8_12.2, constants.%T.as.Destroy.impl.Op.8db
161159
// CHECK:STDOUT: <elided>
162-
// CHECK:STDOUT: %bound_method.loc8: <bound method> = bound_method %.loc8_12.3, %T.as.Destroy.impl.Op.specific_fn.3
163-
// CHECK:STDOUT: %addr.loc8_12: %ptr.793 = addr_of %.loc8_12.3
160+
// CHECK:STDOUT: %bound_method.loc8: <bound method> = bound_method %.loc8_12.2, %T.as.Destroy.impl.Op.specific_fn.3
161+
// CHECK:STDOUT: %addr.loc8_12: %ptr.793 = addr_of %.loc8_12.2
164162
// CHECK:STDOUT: %T.as.Destroy.impl.Op.call.loc8: init %empty_tuple.type = call %bound_method.loc8(%addr.loc8_12)
165163
// CHECK:STDOUT: return
166164
// CHECK:STDOUT: }

toolchain/check/testdata/interop/cpp/enum/copy.carbon

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,9 +67,8 @@ fn F() {
6767
// CHECK:STDOUT: %Cpp.ref.loc8_21: <namespace> = name_ref Cpp, imports.%Cpp [concrete = imports.%Cpp]
6868
// CHECK:STDOUT: %Enum.ref.loc8_24: type = name_ref Enum, imports.%Enum.decl [concrete = constants.%Enum]
6969
// CHECK:STDOUT: %a.ref.loc8: %Enum = name_ref a, imports.%int_0 [concrete = constants.%int_0]
70-
// CHECK:STDOUT: %.loc8_29: init %Enum = initialize_from %a.ref.loc8 to %a.var [concrete = constants.%int_0]
71-
// CHECK:STDOUT: assign %a.var, %.loc8_29
72-
// CHECK:STDOUT: %.loc8_13: type = splice_block %Enum.ref.loc8_13 [concrete = constants.%Enum] {
70+
// CHECK:STDOUT: assign %a.var, %a.ref.loc8
71+
// CHECK:STDOUT: %.loc8: type = splice_block %Enum.ref.loc8_13 [concrete = constants.%Enum] {
7372
// CHECK:STDOUT: %Cpp.ref.loc8_10: <namespace> = name_ref Cpp, imports.%Cpp [concrete = imports.%Cpp]
7473
// CHECK:STDOUT: %Enum.ref.loc8_13: type = name_ref Enum, imports.%Enum.decl [concrete = constants.%Enum]
7574
// CHECK:STDOUT: }
@@ -78,8 +77,7 @@ fn F() {
7877
// CHECK:STDOUT: %Cpp.ref.loc10: <namespace> = name_ref Cpp, imports.%Cpp [concrete = imports.%Cpp]
7978
// CHECK:STDOUT: %Enum.ref.loc10: type = name_ref Enum, imports.%Enum.decl [concrete = constants.%Enum]
8079
// CHECK:STDOUT: %b.ref: %Enum = name_ref b, imports.%int_1 [concrete = constants.%int_1]
81-
// CHECK:STDOUT: %.loc10: init %Enum = initialize_from %b.ref to %a.ref.loc10 [concrete = constants.%int_1]
82-
// CHECK:STDOUT: assign %a.ref.loc10, %.loc10
80+
// CHECK:STDOUT: assign %a.ref.loc10, %b.ref
8381
// CHECK:STDOUT: %T.as.Destroy.impl.Op.bound: <bound method> = bound_method %a.var, constants.%T.as.Destroy.impl.Op.bbe
8482
// CHECK:STDOUT: <elided>
8583
// CHECK:STDOUT: %bound_method: <bound method> = bound_method %a.var, %T.as.Destroy.impl.Op.specific_fn

0 commit comments

Comments
 (0)