Skip to content
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

[SR-12970] Performance: no way to do an unchecked downcast to a protocol #55416

Open
dabrahams opened this issue Jun 10, 2020 · 1 comment
Open
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself

Comments

@dabrahams
Copy link
Collaborator

Previous ID SR-12970
Radar None
Original Reporter @dabrahams
Type Bug
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug
Assignee None
Priority Medium

md5: 47b36d8cf8bb851c24cd50a27faae59e

Issue Description:

The standard library provides unsafeDowncast to go from a base class to one of its derived classes without incurring the cost of dynamic checking in release mode, but there's no way to do the same for casting to a protocol.

We don't technically need a language or library extension to enable this. The code here expresses the intent, but the dynamic checks in testMe and testMe2 are not optimized away.

class X {}
protocol P: AnyObject {  }
class Y: X, P {  }

func testMe(x: X) -> P {
  (x as? P).unsafelyUnwrapped
}

protocol Q {  }
protocol R {  }
func testMe2(q: Q) -> R {
  (q as? R).unsafelyUnwrapped
}
@dabrahams
Copy link
Collaborator Author

/cc @eeckstein

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself
Projects
None yet
Development

No branches or pull requests

1 participant