Skip to content

Commit 079796e

Browse files
authored
Merge pull request #13418 from slavapestov/weak-unowned-what-is-the-difference
Serialization: Fix crash when deserializing store_unowned SIL instruction
2 parents a24121a + 22edb62 commit 079796e

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
@@ -1187,7 +1187,7 @@ bool SILDeserializer::readSILInstruction(SILFunction *Fn, SILBasicBlock *BB,
11871187
// FIXME: Why the arbitrary order difference in IRBuilder type argument?
11881188
ResultVal = Builder.createPartialApply(
11891189
Loc, FnVal, Substitutions, Args,
1190-
closureTy.getAs<SILFunctionType>()->getCalleeConvention());
1190+
closureTy.castTo<SILFunctionType>()->getCalleeConvention());
11911191
break;
11921192
}
11931193
case SILInstructionKind::BuiltinInst: {
@@ -1321,7 +1321,7 @@ bool SILDeserializer::readSILInstruction(SILFunction *Fn, SILBasicBlock *BB,
13211321
}
13221322
case SILInstructionKind::IntegerLiteralInst: {
13231323
auto Ty = MF->getType(TyID);
1324-
auto intTy = Ty->getAs<BuiltinIntegerType>();
1324+
auto intTy = Ty->castTo<BuiltinIntegerType>();
13251325
Identifier StringVal = MF->getIdentifier(ValID);
13261326
// Build APInt from string.
13271327
APInt value(intTy->getGreatestWidth(), StringVal.str(), 10);
@@ -1332,7 +1332,7 @@ bool SILDeserializer::readSILInstruction(SILFunction *Fn, SILBasicBlock *BB,
13321332
}
13331333
case SILInstructionKind::FloatLiteralInst: {
13341334
auto Ty = MF->getType(TyID);
1335-
auto floatTy = Ty->getAs<BuiltinFloatType>();
1335+
auto floatTy = Ty->castTo<BuiltinFloatType>();
13361336
Identifier StringVal = MF->getIdentifier(ValID);
13371337
// Build APInt from string.
13381338
APInt bits(floatTy->getBitWidth(), StringVal.str(), 16);
@@ -1562,7 +1562,7 @@ bool SILDeserializer::readSILInstruction(SILFunction *Fn, SILBasicBlock *BB,
15621562
case SILInstructionKind::StoreUnownedInst: {
15631563
auto Ty = MF->getType(TyID);
15641564
SILType addrType = getSILType(Ty, (SILValueCategory)TyCategory);
1565-
auto refType = addrType.getAs<WeakStorageType>();
1565+
auto refType = addrType.castTo<UnownedStorageType>();
15661566
auto ValType = SILType::getPrimitiveObjectType(refType.getReferentType());
15671567
bool isInit = (Attr > 0);
15681568
ResultVal = Builder.createStoreUnowned(Loc,
@@ -1574,7 +1574,7 @@ bool SILDeserializer::readSILInstruction(SILFunction *Fn, SILBasicBlock *BB,
15741574
case SILInstructionKind::StoreWeakInst: {
15751575
auto Ty = MF->getType(TyID);
15761576
SILType addrType = getSILType(Ty, (SILValueCategory)TyCategory);
1577-
auto refType = addrType.getAs<WeakStorageType>();
1577+
auto refType = addrType.castTo<WeakStorageType>();
15781578
auto ValType = SILType::getPrimitiveObjectType(refType.getReferentType());
15791579
bool isInit = (Attr > 0);
15801580
ResultVal = Builder.createStoreWeak(Loc,
@@ -1656,7 +1656,7 @@ bool SILDeserializer::readSILInstruction(SILFunction *Fn, SILBasicBlock *BB,
16561656
// Use OneTypeOneOperand layout where the field number is stored in TypeID.
16571657
auto Ty2 = MF->getType(TyID2);
16581658
SILType ST = getSILType(Ty2, (SILValueCategory)TyCategory2);
1659-
TupleType *TT = ST.getAs<TupleType>();
1659+
TupleType *TT = ST.castTo<TupleType>();
16601660

16611661
auto ResultTy = TT->getElement(TyID).getType();
16621662
switch (OpCode) {
@@ -1679,7 +1679,7 @@ bool SILDeserializer::readSILInstruction(SILFunction *Fn, SILBasicBlock *BB,
16791679
// Format: a type followed by a list of values. A value is expressed by
16801680
// 2 IDs: ValueID, ValueResultNumber.
16811681
auto Ty = MF->getType(TyID);
1682-
TupleType *TT = Ty->getAs<TupleType>();
1682+
TupleType *TT = Ty->castTo<TupleType>();
16831683
assert(TT && "Type of a TupleInst should be TupleType");
16841684
SmallVector<SILValue, 4> OpList;
16851685
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)