-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Make NS_TYPED_ENUMS ObjectiveCBridgeable when they wrap an object #15270
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
@import Foundation; | ||
|
||
#define GET_UNTYPED_AND_OPAQUE(Name) \ | ||
static NSArray * _Nonnull getUntypedNSArrayOf##Name##s() { \ | ||
return getTypedNSArrayOf##Name##Wrappers(); \ | ||
} \ | ||
static id _Nonnull getOpaqueNSArrayOf##Name##s() { \ | ||
return getTypedNSArrayOf##Name##Wrappers(); \ | ||
} | ||
#define CHECK_UNTYPED_AND_OPAQUE(Name) \ | ||
static BOOL checkUntypedNSArrayOf##Name##s(NSArray * _Nonnull array) { \ | ||
return checkTypedNSArrayOf##Name##Wrappers(array); \ | ||
} \ | ||
static BOOL checkOpaqueNSArrayOf##Name##s(id _Nonnull array) { \ | ||
return checkTypedNSArrayOf##Name##Wrappers(array); \ | ||
} | ||
|
||
typedef NSString *StringWrapper NS_TYPED_EXTENSIBLE_ENUM; | ||
static NSArray<StringWrapper> * _Nonnull getTypedNSArrayOfStringWrappers() { | ||
return @[@"abc", @"def"]; | ||
} | ||
static BOOL | ||
checkTypedNSArrayOfStringWrappers(NSArray<StringWrapper> * _Nonnull array) { | ||
return [array isEqual:getTypedNSArrayOfStringWrappers()]; | ||
} | ||
GET_UNTYPED_AND_OPAQUE(String) | ||
CHECK_UNTYPED_AND_OPAQUE(String) | ||
|
||
|
||
typedef id <NSCopying, NSCoding> CopyingAndCodingWrapper | ||
NS_TYPED_EXTENSIBLE_ENUM; | ||
static NSArray<CopyingAndCodingWrapper> * _Nonnull | ||
getTypedNSArrayOfCopyingAndCodingWrappers() { | ||
return @[@"abc", @[]]; | ||
} | ||
static BOOL checkTypedNSArrayOfCopyingAndCodingWrappers( | ||
NSArray<CopyingAndCodingWrapper> * _Nonnull array) { | ||
return [array isEqual:getTypedNSArrayOfCopyingAndCodingWrappers()]; | ||
} | ||
GET_UNTYPED_AND_OPAQUE(CopyingAndCoding) | ||
CHECK_UNTYPED_AND_OPAQUE(CopyingAndCoding) | ||
|
||
typedef id <NSCopying> CopyingWrapper NS_TYPED_EXTENSIBLE_ENUM; | ||
static NSArray<CopyingWrapper> * _Nonnull getTypedNSArrayOfCopyingWrappers() { | ||
return getTypedNSArrayOfCopyingAndCodingWrappers(); | ||
} | ||
static BOOL | ||
checkTypedNSArrayOfCopyingWrappers(NSArray<CopyingWrapper> * _Nonnull array) { | ||
return [array isEqual:getTypedNSArrayOfCopyingWrappers()]; | ||
} | ||
GET_UNTYPED_AND_OPAQUE(Copying) | ||
CHECK_UNTYPED_AND_OPAQUE(Copying) | ||
|
||
typedef NSObject *ObjectWrapper NS_TYPED_EXTENSIBLE_ENUM; | ||
static NSArray<ObjectWrapper> * _Nonnull getTypedNSArrayOfObjectWrappers() { | ||
return (NSArray<ObjectWrapper> *)getTypedNSArrayOfCopyingAndCodingWrappers(); | ||
} | ||
static BOOL | ||
checkTypedNSArrayOfObjectWrappers(NSArray<ObjectWrapper> * _Nonnull array) { | ||
return [array isEqual:getTypedNSArrayOfObjectWrappers()]; | ||
} | ||
GET_UNTYPED_AND_OPAQUE(Object) | ||
CHECK_UNTYPED_AND_OPAQUE(Object) | ||
|
||
typedef NSError *ErrorWrapper NS_TYPED_EXTENSIBLE_ENUM; | ||
static NSArray<ErrorWrapper> * _Nonnull getTypedNSArrayOfErrorWrappers() { | ||
return @[[NSError errorWithDomain:@"x" code:11 userInfo:nil], | ||
[NSError errorWithDomain:@"x" code:22 userInfo:nil]]; | ||
} | ||
static BOOL | ||
checkTypedNSArrayOfErrorWrappers(NSArray<ErrorWrapper> * _Nonnull array) { | ||
return [[array valueForKey:@"code"] isEqual:@[@11, @22]] && | ||
NSNotFound == [array indexOfObjectPassingTest:^BOOL(ErrorWrapper error, | ||
NSUInteger idx, | ||
BOOL *stop) { | ||
return ![error isKindOfClass:[NSError class]]; | ||
}]; | ||
} | ||
GET_UNTYPED_AND_OPAQUE(Error) | ||
CHECK_UNTYPED_AND_OPAQUE(Error) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't trust the compiler to get this right consistently, because a
typealias
with the same name as an associated type, but in a constrained extension, isn't something modeled well in the conformance checker. You're already adding a synthesizedtypealias
in the importer, so why do you need this?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the one case the importer is skipping; I didn't want to have to resolve the
_ObjectiveCType
of theRawValue
, especially when compiling an overlay where we may not have actually determined it yet. How bad do you think this is? (Note that it's not necessarily the original type; aswift_wrapper
of anint
is bridged to NSNumber.)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking the that the
swift_bridge
attribute had all of the information, but of course it doesn't forNSNumber
orNSValue
. Slightly concerned that we'll be unable to form this type witness if the Clang importer imports one of these types after the type checker is gone, but I supposed #15254 should make that no longer an issue.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think if that happens we're hosed anyway for all the default value requirements, so I'm not going to worry about it. :-)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Associated type witnesses get used in
Type::subst
calls a lot, and we've had a number of compiler crashes due to missing associated type witnesses in the importer. Value witnesses are less often used---only really by the specializer or (in this case) the SILGen bridging code---and hasn't been as much of an issue. Anyway, I'm okay with this solution.