Skip to content

Commit 2199152

Browse files
author
ematejska
authored
Merge pull request #13419 from slavapestov/weak-unowned-what-is-the-difference-4.1
Serialization: Fix crash when deserializing store_unowned SIL instruction [4.1]
2 parents 2752063 + 2674aef commit 2199152

File tree

2 files changed

+33
-7
lines changed

2 files changed

+33
-7
lines changed

lib/Serialization/DeserializeSIL.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1183,7 +1183,7 @@ bool SILDeserializer::readSILInstruction(SILFunction *Fn, SILBasicBlock *BB,
11831183
// FIXME: Why the arbitrary order difference in IRBuilder type argument?
11841184
ResultVal = Builder.createPartialApply(
11851185
Loc, FnVal, Substitutions, Args,
1186-
closureTy.getAs<SILFunctionType>()->getCalleeConvention());
1186+
closureTy.castTo<SILFunctionType>()->getCalleeConvention());
11871187
break;
11881188
}
11891189
case SILInstructionKind::BuiltinInst: {
@@ -1317,7 +1317,7 @@ bool SILDeserializer::readSILInstruction(SILFunction *Fn, SILBasicBlock *BB,
13171317
}
13181318
case SILInstructionKind::IntegerLiteralInst: {
13191319
auto Ty = MF->getType(TyID);
1320-
auto intTy = Ty->getAs<BuiltinIntegerType>();
1320+
auto intTy = Ty->castTo<BuiltinIntegerType>();
13211321
Identifier StringVal = MF->getIdentifier(ValID);
13221322
// Build APInt from string.
13231323
APInt value(intTy->getGreatestWidth(), StringVal.str(), 10);
@@ -1328,7 +1328,7 @@ bool SILDeserializer::readSILInstruction(SILFunction *Fn, SILBasicBlock *BB,
13281328
}
13291329
case SILInstructionKind::FloatLiteralInst: {
13301330
auto Ty = MF->getType(TyID);
1331-
auto floatTy = Ty->getAs<BuiltinFloatType>();
1331+
auto floatTy = Ty->castTo<BuiltinFloatType>();
13321332
Identifier StringVal = MF->getIdentifier(ValID);
13331333
// Build APInt from string.
13341334
APInt bits(floatTy->getBitWidth(), StringVal.str(), 16);
@@ -1558,7 +1558,7 @@ bool SILDeserializer::readSILInstruction(SILFunction *Fn, SILBasicBlock *BB,
15581558
case SILInstructionKind::StoreUnownedInst: {
15591559
auto Ty = MF->getType(TyID);
15601560
SILType addrType = getSILType(Ty, (SILValueCategory)TyCategory);
1561-
auto refType = addrType.getAs<WeakStorageType>();
1561+
auto refType = addrType.castTo<UnownedStorageType>();
15621562
auto ValType = SILType::getPrimitiveObjectType(refType.getReferentType());
15631563
bool isInit = (Attr > 0);
15641564
ResultVal = Builder.createStoreUnowned(Loc,
@@ -1570,7 +1570,7 @@ bool SILDeserializer::readSILInstruction(SILFunction *Fn, SILBasicBlock *BB,
15701570
case SILInstructionKind::StoreWeakInst: {
15711571
auto Ty = MF->getType(TyID);
15721572
SILType addrType = getSILType(Ty, (SILValueCategory)TyCategory);
1573-
auto refType = addrType.getAs<WeakStorageType>();
1573+
auto refType = addrType.castTo<WeakStorageType>();
15741574
auto ValType = SILType::getPrimitiveObjectType(refType.getReferentType());
15751575
bool isInit = (Attr > 0);
15761576
ResultVal = Builder.createStoreWeak(Loc,
@@ -1652,7 +1652,7 @@ bool SILDeserializer::readSILInstruction(SILFunction *Fn, SILBasicBlock *BB,
16521652
// Use OneTypeOneOperand layout where the field number is stored in TypeID.
16531653
auto Ty2 = MF->getType(TyID2);
16541654
SILType ST = getSILType(Ty2, (SILValueCategory)TyCategory2);
1655-
TupleType *TT = ST.getAs<TupleType>();
1655+
TupleType *TT = ST.castTo<TupleType>();
16561656

16571657
auto ResultTy = TT->getElement(TyID).getType();
16581658
switch (OpCode) {
@@ -1675,7 +1675,7 @@ bool SILDeserializer::readSILInstruction(SILFunction *Fn, SILBasicBlock *BB,
16751675
// Format: a type followed by a list of values. A value is expressed by
16761676
// 2 IDs: ValueID, ValueResultNumber.
16771677
auto Ty = MF->getType(TyID);
1678-
TupleType *TT = Ty->getAs<TupleType>();
1678+
TupleType *TT = Ty->castTo<TupleType>();
16791679
assert(TT && "Type of a TupleInst should be TupleType");
16801680
SmallVector<SILValue, 4> OpList;
16811681
for (unsigned I = 0, E = ListOfValues.size(); I < E; I++) {

test/Serialization/Inputs/def_basic.sil

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1253,6 +1253,31 @@ sil [serialized] [thunk] @a_regular_thunk : $@convention(thin) () -> () {
12531253
return %1 : $()
12541254
}
12551255

1256+
public class WeakUnownedTest {
1257+
@sil_stored public unowned var unownedVal: @sil_unowned AnyObject { get set }
1258+
@sil_stored public weak var weakVal: @sil_weak AnyObject? { get set }
1259+
public init(protoVal: AnyObject)
1260+
deinit
1261+
}
1262+
1263+
// CHECK-LABEL: sil public_external [serialized] @weak_unowned : $@convention(thin) (@owned WeakUnownedTest, @owned AnyObject) -> ()
1264+
sil [serialized] @weak_unowned : $@convention(thin) (@owned WeakUnownedTest, @owned AnyObject) -> () {
1265+
bb0(%0 : $WeakUnownedTest, %1 : $AnyObject):
1266+
%2 = ref_element_addr %0 : $WeakUnownedTest, #WeakUnownedTest.unownedVal
1267+
// CHECK: store_unowned
1268+
store_unowned %1 to %2 : $*@sil_unowned AnyObject
1269+
1270+
%3 = enum $Optional<AnyObject>, #Optional.some!enumelt.1, %1 : $AnyObject
1271+
%4 = ref_element_addr %0 : $WeakUnownedTest, #WeakUnownedTest.weakVal
1272+
// CHECK: store_weak
1273+
store_weak %3 to %4 : $*@sil_weak AnyObject?
1274+
1275+
%r = tuple ()
1276+
1277+
// CHECK: return
1278+
return %r : $()
1279+
}
1280+
12561281
public class Foo {
12571282
subscript (x: Int, y: Int) -> Int32 { get set }
12581283
var x: Int
@@ -1467,6 +1492,7 @@ bb0:
14671492
%150 = function_ref @bridge_object : $@convention(thin) (@owned Class1, Builtin.Word) -> ()
14681493
%151 = function_ref @a_reabstraction_thunk: $@convention(thin) () -> ()
14691494
%152 = function_ref @a_regular_thunk: $@convention(thin) () -> ()
1495+
%153 = function_ref @weak_unowned: $@convention(thin) (@owned WeakUnownedTest, @owned AnyObject) -> ()
14701496

14711497
%r = tuple ()
14721498
return %r : $()

0 commit comments

Comments
 (0)