Skip to content

Commit 1becb0b

Browse files
committed
PerformanceDiagnostics: diagnose dynamic casts
Dynamic casts need metadata and therefore cannot be used in embedded swift. Fixes an internal IRGen error.
1 parent 3aa3d8e commit 1becb0b

File tree

3 files changed

+16
-0
lines changed

3 files changed

+16
-0
lines changed

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/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+
func callCastToExistential() {
21+
castToExistential(x: 42) // expected-note {{called from here}}
22+
}
23+

0 commit comments

Comments
 (0)