Skip to content

Commit 323cb67

Browse files
authored
Merge pull request #73703 from eeckstein/fix-cast-simplification
embedded: fix a compiler crash when using dynamic casts
2 parents 31337dd + 369021f commit 323cb67

File tree

5 files changed

+31
-1
lines changed

5 files changed

+31
-1
lines changed

SwiftCompilerSources/Sources/Optimizer/InstructionSimplification/SimplifyCheckedCast.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ extension CheckedCastAddrBranchInst : OnoneSimplifyable {
1818
return
1919
}
2020
if castWillSucceed {
21-
replaceSuccess(context)
21+
// TODO: handle cases where the operand address types are different.
22+
if source.type == destination.type {
23+
replaceSuccess(context)
24+
}
2225
} else {
2326
replaceFailure(context)
2427
}

include/swift/AST/DiagnosticsSIL.def

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -390,6 +390,8 @@ ERROR(embedded_swift_metatype,none,
390390
"cannot use metatype in embedded Swift", ())
391391
ERROR(embedded_swift_keypath,none,
392392
"cannot use key path in embedded Swift", ())
393+
ERROR(embedded_swift_dynamic_cast,none,
394+
"cannot do dynamic casting in embedded Swift", ())
393395
ERROR(embedded_swift_allocating_type,none,
394396
"cannot use allocating type %0 in -no-allocations mode", (Type))
395397
ERROR(embedded_swift_allocating,none,

lib/SILOptimizer/Mandatory/PerformanceDiagnostics.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,6 +526,10 @@ bool PerformanceDiagnostics::visitInst(SILInstruction *inst,
526526
diagnose(loc, diag::embedded_swift_keypath);
527527
return true;
528528
}
529+
if (isa<CheckedCastAddrBranchInst>(inst) || isa<UnconditionalCheckedCastAddrInst>(inst)) {
530+
diagnose(loc, diag::embedded_swift_dynamic_cast);
531+
return true;
532+
}
529533
if (!allowedMetadataUseInEmbeddedSwift(inst)) {
530534
PrettyStackTracePerformanceDiagnostics stackTrace("metatype", inst);
531535
if (impactType) {

test/SILOptimizer/simplify_checked_cast_addr_br.sil

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,3 +129,14 @@ bb2:
129129
unreachable
130130
}
131131

132+
// Check that we don't crash on this
133+
sil @success_but_different_operand_type : $@convention(thin) (@in D) -> @out X {
134+
bb0(%0 : $*X, %1 : $*D):
135+
checked_cast_addr_br copy_on_success D in %1 : $*D to X in %0 : $*X, bb1, bb2
136+
bb1:
137+
%r = tuple()
138+
return %r : $()
139+
bb2:
140+
unreachable
141+
}
142+

test/embedded/metatypes.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,13 @@ public func test() -> Int {
1111
sink(t: metatype)
1212
return 42
1313
}
14+
15+
func castToExistential<T>(x: T) {
16+
if x is any FixedWidthInteger { // expected-error {{cannot do dynamic casting in embedded Swift}}
17+
}
18+
}
19+
20+
public func callCastToExistential() {
21+
castToExistential(x: 42) // expected-note {{called from here}}
22+
}
23+

0 commit comments

Comments
 (0)