Skip to content

Commit 549fbc0

Browse files
authored
Merge pull request #21121 from eeckstein/fix-box-mangling-5.0
[5.0] Mangling: mangle box field types as they are.
2 parents 0ce4afe + fc43e7a commit 549fbc0

File tree

2 files changed

+55
-11
lines changed

2 files changed

+55
-11
lines changed

lib/AST/ASTMangler.cpp

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -934,23 +934,27 @@ void ASTMangler::appendType(Type type) {
934934
case TypeKind::SILBox: {
935935
auto box = cast<SILBoxType>(tybase);
936936
auto layout = box->getLayout();
937-
SmallVector<TupleTypeElt, 4> fieldsList;
937+
bool firstField = true;
938938
for (auto &field : layout->getFields()) {
939-
auto fieldTy = field.getLoweredType();
940-
// Use the `inout` mangling to represent a mutable field.
941-
auto fieldFlag = ParameterTypeFlags().withInOut(field.isMutable());
942-
fieldsList.push_back(TupleTypeElt(fieldTy, Identifier(), fieldFlag));
939+
appendType(field.getLoweredType());
940+
if (field.isMutable()) {
941+
// Use the `inout` mangling to represent a mutable field.
942+
appendOperator("z");
943+
}
944+
appendListSeparator(firstField);
943945
}
944-
appendTypeList(TupleType::get(fieldsList, tybase->getASTContext())
945-
->getCanonicalType());
946+
if (firstField)
947+
appendOperator("y");
946948

947949
if (auto sig = layout->getGenericSignature()) {
948-
fieldsList.clear();
950+
bool firstType = true;
949951
for (Type type : box->getSubstitutions().getReplacementTypes()) {
950-
fieldsList.push_back(TupleTypeElt(type));
952+
appendType(type);
953+
appendListSeparator(firstType);
951954
}
952-
appendTypeList(TupleType::get(fieldsList, tybase->getASTContext())
953-
->getCanonicalType());
955+
if (firstType)
956+
appendOperator("y");
957+
954958
appendGenericSignature(sig);
955959
appendOperator("XX");
956960
} else {

test/SILOptimizer/closure_specialize.sil

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,46 @@ bb0(%0 : $Builtin.Int32, %1 : $<τ_0_0> { var τ_0_0 } <Builtin.Int32>):
478478
return %7 : $()
479479
}
480480

481+
// Check that we don't crash with this:
482+
// CHECK-LABEL: sil @test_box_with_named_elements_tuple
483+
sil @test_box_with_named_elements_tuple: $@convention(thin) () -> Builtin.Int32 {
484+
bb0:
485+
%0 = alloc_box ${ let (first: Builtin.Int32, second: Builtin.Int32) }
486+
%0p = project_box %0 : ${ let (first: Builtin.Int32, second: Builtin.Int32) }, 0
487+
%0a = tuple_element_addr %0p : $*(first: Builtin.Int32, second: Builtin.Int32), 0
488+
%0b = tuple_element_addr %0p : $*(first: Builtin.Int32, second: Builtin.Int32), 1
489+
%1 = integer_literal $Builtin.Int32, 0
490+
store %1 to %0a : $*Builtin.Int32
491+
store %1 to %0b : $*Builtin.Int32
492+
%4 = function_ref @closure_with_named_elements_tuple : $@convention(thin) (Builtin.Int32, @owned { let (first: Builtin.Int32, second: Builtin.Int32) }) -> ()
493+
strong_retain %0 : ${ let (first: Builtin.Int32, second: Builtin.Int32) }
494+
%6 = partial_apply %4(%0) : $@convention(thin) (Builtin.Int32, @owned { let (first: Builtin.Int32, second: Builtin.Int32) }) -> ()
495+
%7 = alloc_stack $Builtin.Int32
496+
%9 = integer_literal $Builtin.Int32, 1
497+
store %9 to %7 : $*Builtin.Int32
498+
%12 = function_ref @$s4main5inneryys5Int32Vz_yADctF: $@convention(thin) (@inout Builtin.Int32, @owned @callee_owned (Builtin.Int32) -> ()) -> ()
499+
strong_retain %6 : $@callee_owned (Builtin.Int32) -> ()
500+
%14 = apply %12(%7, %6) : $@convention(thin) (@inout Builtin.Int32, @owned @callee_owned (Builtin.Int32) -> ()) -> ()
501+
strong_release %6 : $@callee_owned (Builtin.Int32) -> ()
502+
%16 = tuple ()
503+
dealloc_stack %7 : $*Builtin.Int32
504+
%18 = load %0a : $*Builtin.Int32
505+
strong_release %0 : ${ let (first: Builtin.Int32, second: Builtin.Int32) }
506+
return %18 : $Builtin.Int32
507+
}
508+
509+
// CHECK-LABEL: sil shared @closure_with_named_elements_tuple
510+
sil shared @closure_with_named_elements_tuple : $@convention(thin) (Builtin.Int32, @owned { let (first: Builtin.Int32, second: Builtin.Int32) }) -> () {
511+
bb0(%0 : $Builtin.Int32, %1 : ${ let (first: Builtin.Int32, second: Builtin.Int32) }):
512+
%3 = project_box %1 : ${ let (first: Builtin.Int32, second: Builtin.Int32) }, 0
513+
%4 = tuple_element_addr %3 : $*(first: Builtin.Int32, second: Builtin.Int32), 0
514+
store %0 to %4 : $*Builtin.Int32
515+
strong_release %1 : ${ let (first: Builtin.Int32, second: Builtin.Int32) }
516+
%7 = tuple ()
517+
return %7 : $()
518+
}
519+
520+
481521
// The specialized function should always be a thin function, regardless of the
482522
// representation of the original function.
483523

0 commit comments

Comments
 (0)