Skip to content

embedded: fix a compiler crash when using dynamic casts #73703

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ extension CheckedCastAddrBranchInst : OnoneSimplifyable {
return
}
if castWillSucceed {
replaceSuccess(context)
// TODO: handle cases where the operand address types are different.
if source.type == destination.type {
replaceSuccess(context)
}
} else {
replaceFailure(context)
}
Expand Down
2 changes: 2 additions & 0 deletions include/swift/AST/DiagnosticsSIL.def
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,8 @@ ERROR(embedded_swift_metatype,none,
"cannot use metatype in embedded Swift", ())
ERROR(embedded_swift_keypath,none,
"cannot use key path in embedded Swift", ())
ERROR(embedded_swift_dynamic_cast,none,
"cannot do dynamic casting in embedded Swift", ())
ERROR(embedded_swift_allocating_type,none,
"cannot use allocating type %0 in -no-allocations mode", (Type))
ERROR(embedded_swift_allocating,none,
Expand Down
4 changes: 4 additions & 0 deletions lib/SILOptimizer/Mandatory/PerformanceDiagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,10 @@ bool PerformanceDiagnostics::visitInst(SILInstruction *inst,
diagnose(loc, diag::embedded_swift_keypath);
return true;
}
if (isa<CheckedCastAddrBranchInst>(inst) || isa<UnconditionalCheckedCastAddrInst>(inst)) {
diagnose(loc, diag::embedded_swift_dynamic_cast);
return true;
}
if (!allowedMetadataUseInEmbeddedSwift(inst)) {
PrettyStackTracePerformanceDiagnostics stackTrace("metatype", inst);
if (impactType) {
Expand Down
11 changes: 11 additions & 0 deletions test/SILOptimizer/simplify_checked_cast_addr_br.sil
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,14 @@ bb2:
unreachable
}

// Check that we don't crash on this
sil @success_but_different_operand_type : $@convention(thin) (@in D) -> @out X {
bb0(%0 : $*X, %1 : $*D):
checked_cast_addr_br copy_on_success D in %1 : $*D to X in %0 : $*X, bb1, bb2
bb1:
%r = tuple()
return %r : $()
bb2:
unreachable
}

10 changes: 10 additions & 0 deletions test/embedded/metatypes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,13 @@ public func test() -> Int {
sink(t: metatype)
return 42
}

func castToExistential<T>(x: T) {
if x is any FixedWidthInteger { // expected-error {{cannot do dynamic casting in embedded Swift}}
}
}

public func callCastToExistential() {
castToExistential(x: 42) // expected-note {{called from here}}
}