Skip to content

[SR-5289] Teach Mirror how to handle unowned/unmanaged references #28823

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 27 commits into from
Dec 17, 2019

Conversation

tbkka
Copy link
Contributor

@tbkka tbkka commented Dec 17, 2019

This is the same as #28368 (which was reverted in #28821) except that it disables the new tests when testing against old versions of the standard library. (The functionality here is known to be broken on old standard libraries, so there's no point testing there.

Resolves SR-5289.

tbkka added 27 commits November 19, 2019 13:22
Test that we can reflect weak, unowned, and unmanaged references
to both Swift and Obj-C types (including existential references to
such types) that occur in both Swift class objects and in Swift
structs.

This includes the specific reported case (unowned reference to an
Obj-C object).
TypeReferenceOwnership here is a bitmap indicating which of
the various ownership modifiers (weak, unowned, unmanaged)
were present on the field declaration.  Making this a bitmap
ensures that we can accurately represent the data that may
have been provided but we have to be careful when we interpret
the result.

This changes the `isWeak`, `isUnowned`, `isUnmanged` checks to
only return true if exactly one bit is set.  Combinations of bits
will not be accepted.

Also, add `isStrong` to check for the default ownership with
no modifiers.
This involves refactoring how we handle reference ownership
when reflecting fields of struct and class objects:

* Move FieldType into ReflectionMirror.mm
  FieldType is really just an internal implementation detail
	of this one source file, so it does not belong in an ABI header.

* Use TypeReferenceOwnership directly to track field ownership
  This avoids bitwise copying of properties and localizes some
	of the knowledge about reference ownership

* Generate a top-level "copyFieldContents" from ReferenceStorage.def
  Adding new ownership types to ReferenceStorage.def will now
	automatically produce calls to `copy*FieldContents` - failure
	to provide a suitable implementation will fail the build.

* Implement `copyUnownedFieldContents` and `copyUnmanagedFieldContents`
…alIn`

Balanced calls here help ensure we use consistent memory management.
I had to fix this in order to test the Mirror support changes.

Weak properties are optionals, and reflection packages things
in Any containers, so it's hard to test reflecting weak
properties without being able to cast the result back
to the original type.
In particular, a null type at this point indicates that
the cast has already been checked and is known to be valid.
If this works, I should be able to factor out the common
unwrap logic here.
This reverts commit 0e86b31.

Trying to fix the casting inconsistencies is turning out
to be a lot more complex than I thought.  Let's roll back
to just the Mirror fix so that can be merged before I take
off on a lengthy tangent.
@tbkka tbkka requested review from mikeash and shahmishal December 17, 2019 01:55
@tbkka
Copy link
Contributor Author

tbkka commented Dec 17, 2019

@swift-ci Please test macOS

@tbkka
Copy link
Contributor Author

tbkka commented Dec 17, 2019

@swift-ci Please test Linux

@shahmishal
Copy link
Member

Thanks!

@tbkka tbkka merged commit fdb1926 into swiftlang:master Dec 17, 2019
@tbkka tbkka deleted the SR-5289 branch October 16, 2020 00:34
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.

2 participants