Skip to content

Dynamic Casting: Properly unwrap existential metatype sources #34469

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 6 commits into from
Oct 29, 2020

Conversation

tbkka
Copy link
Contributor

@tbkka tbkka commented Oct 28, 2020

Existential metatypes are really just existentials that hold metatypes. As
such, they should be handled in the general casting logic in much the same way
as regular existentials: They should generally be ignored by most casting logic,
and unwrapped as necessary at the top level.

In particular, the previous code would fail to correctly handle the following
cast from an existential metatype (AnyObject.Type) to an existential
(AnyObject):

  class C {}
  let a = C.self as AnyObject.Type
  let b = a as! AnyObject

With the old code, b above would hold a reference to a __SwiftValue box
containing the type reference. The correct result would simply store the type
reference directly in b. These two are only really distinguishable in that
the correct form permits a === b to return true.

Fixes rdar://70582753

Existential metatypes are really just existentials that hold metatypes.  As
such, they should be handled in the general casting logic in much the same way
as regular existentials: They should generally be ignored by most casting logic,
and unwrapped as necessary at the top level.

In particular, the previous code would fail to correctly handle the following
cast from an existential metatype (`AnyObject.Type`) to an existential
(`AnyObject`):
```
  class C {}
  let a = C.self as AnyObject.Type
  let b = a as! AnyObject
```
With the old code, `b` above would hold a reference to a `__SwiftValue` box
containing the type reference.  The correct result would simply store the type
reference directly in `b`.  These two are only really distinguishable in that
the correct form permits `a === b` to return `true`.

Fixes rdar://70582753
@tbkka tbkka requested a review from mikeash October 28, 2020 01:00
@tbkka
Copy link
Contributor Author

tbkka commented Oct 28, 2020

@swift-ci Please test

@tbkka tbkka requested review from lorentey and eeckstein October 28, 2020 01:02
@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - e972e3e

@tbkka
Copy link
Contributor Author

tbkka commented Oct 28, 2020

@swift-ci Please test

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - 23d26d0

tbkka added 3 commits October 28, 2020 14:30
While I'm here, include some comments indicating the missing pieces still needed
for Linux support to work.  Basically, metatypes on Linux are not currently
fully compatible with reference-counted class pointers, which prevents us from
fully supporting metatype operations on Linux that we support on macOS.
Whitespace cleanup
@tbkka
Copy link
Contributor Author

tbkka commented Oct 28, 2020

@swift-ci Please test

@tbkka tbkka merged commit d92f1d5 into swiftlang:main Oct 29, 2020
ainu-bot added a commit to google/swift that referenced this pull request Oct 29, 2020
* 'main' of github.com:apple/swift: (22 commits)
  Dynamic Casting: Properly unwrap existential metatype sources (swiftlang#34469)
  Add -allow-critical-edges=false to copyforward_ossa.sil
  Add -allow-critical-edges flag.
  Sema: Check result builder availability
  Sema: Pass ExportContext by const reference where possible
  Sema: Put the AvailabilityContext inside the ExportContext
  Change interfaces to accept a FileCollectorBase
  [Parse] Remove unused declaration and diagnostic message
  [cxx-interop] Use regex instead of operand name to fix C++ constructor tests.
  [CodeCompletion] Fix a crash in collectPossibleReturnTypesFromContext
  [test] Update cross_langauge index test for mangling change
  Temporarily disable this test
  [Concurrency] SIL: add hop_to_executor instruction
  sil-opt: add an -enable-experimental-concurrency option.
  Sema: Don't visit elements of a BraceStmt twice when checking availability
  Sema: Rename AvailabilityWalker to ExprAvailabilityWalker
  Sema: Pull diagnoseDeclAvailability() out of AvailabilityWalker
  Sema: Rename some availability checking entry points
  SILGen: Stub out support for invoking foreign async methods.
  Add a mangling for completion block implementation functions.
  ...
@tbkka tbkka deleted the tbkka/existentialMetatypeCasting branch August 1, 2024 16:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants