Skip to content

Commit 9d06ac8

Browse files
committed
[cast-optimizer] Do not try to optimized a bridged cast if it involves an unbound generic type.
This fixes a compiler crash reported in rdar://21866854 Swift SVN r30515
1 parent a20bfb0 commit 9d06ac8

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

lib/SILPasses/Utils/Local.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1293,6 +1293,10 @@ optimizeBridgedCasts(SILInstruction *Inst,
12931293
!source.getStructOrBoundGenericStruct()))
12941294
return nullptr;
12951295

1296+
// Casts involving non-bound generic types cannot be optimized.
1297+
if (source->hasArchetype() || target->hasArchetype())
1298+
return nullptr;
1299+
12961300
auto BridgedTargetTy = getCastFromObjC(M, source, target);
12971301
if (!BridgedTargetTy)
12981302
return nullptr;

test/SILPasses/bridged_casts_folding.swift

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,44 @@ public func testCondCastSwiftToNSSetString() -> NSSet? {
620620
return setOpt
621621
}
622622

623+
// Casts involving generics cannot be optimized.
624+
625+
// CHECK-LABEL: sil [noinline] @_TF21bridged_casts_folding25testForcedCastFromGenericurFq_CSo8NSString
626+
// CHECK: unconditional_checked
627+
// CHECK: return
628+
@inline(never)
629+
public func testForcedCastFromGeneric<T>(x: T) -> NSString {
630+
var set: NSString = x as! NSString
631+
return set
632+
}
633+
634+
// CHECK-LABEL: sil [noinline] @_TF21bridged_casts_folding23testForcedCastToGenericurFq_q_
635+
// CHECK: unconditional_checked
636+
// CHECK: return
637+
@inline(never)
638+
public func testForcedCastToGeneric<T>(x: T) -> T {
639+
var set: T = nsString as! T
640+
return set
641+
}
642+
643+
// CHECK-LABEL: sil [noinline] @_TF21bridged_casts_folding23testCondCastFromGenericurFq_GSqCSo8NSString_
644+
// CHECK: checked_cast_addr_br
645+
// CHECK: return
646+
@inline(never)
647+
public func testCondCastFromGeneric<T>(x: T) -> NSString? {
648+
var setOpt: NSString? = x as? NSString
649+
return setOpt
650+
}
651+
652+
// CHECK-LABEL: sil [noinline] @_TF21bridged_casts_folding21testCondCastToGenericurFq_GSqq__
653+
// CHECK: checked_cast_addr_br
654+
// CHECK: return
655+
@inline(never)
656+
public func testCondCastToGeneric<T>(x: T) -> T? {
657+
var setOpt: T? = nsString as? T
658+
return setOpt
659+
}
660+
623661

624662
// Run-time tests
625663

0 commit comments

Comments
 (0)