Skip to content

[Clang importer] Don't bridge blocks to Swift functions in ObjC generic args #17247

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
Jun 15, 2018

Conversation

DougGregor
Copy link
Member

While the compiler can bridge C block types to Swift function types,
the Swift runtime cannot. Don't bridge block types to Swift function
types in Objective-C generic arguments, so

NSArray

will get imported as

[@convention(block) (...) -> Whatever]

rather than

[(...) -> Whatever]

Fixes rdar://problem/40879067 in a fairly narrow way; the Clang
importer's approach to adjusting types based on context needs a
cleanup, but this is the safe, localized fix suitable for 4.2.

@DougGregor DougGregor requested a review from jrose-apple June 15, 2018 18:52
@DougGregor
Copy link
Member Author

@swift-ci please smoke test

…ic args.

While the compiler can bridge C block types to Swift function types,
the Swift runtime cannot. Don't bridge block types to Swift function
types in Objective-C generic arguments, so

  NSArray<some-block-type>

will get imported as

  [@convention(block) (...) -> Whatever]

rather than

  [(...) -> Whatever]

Fixes rdar://problem/40879067 in a fairly narrow way; the Clang
importer's approach to adjusting types based on context needs a
cleanup, but this is the safe, localized fix suitable for 4.2.
@DougGregor DougGregor force-pushed the import-objc-generic-of-block branch from 3718cdc to d529002 Compare June 15, 2018 20:04
@DougGregor
Copy link
Member Author

@swift-ci please smoke test and merge

Copy link
Contributor

@jrose-apple jrose-apple left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for tracking this down! I think there might be an SR about it too.

/// \brief Import the type of a literal value that can be bridged.
BridgedValue,
/// \brief Import the type of an Objective-C generic argument.
ObjCGenericArgument,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like this isn't just any generic argument, since most of the time we'd import Foo<NSString *> * as Foo<NSString> and not Foo<String>. "ObjCCollectionElement", maybe?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point!

if (einfo.getRepresentation() != FunctionTypeRepresentation::Swift) {
einfo = einfo.withRepresentation(FunctionTypeRepresentation::Swift);
if (einfo.getRepresentation() != requiredFunctionTypeRepr) {
einfo = einfo.withRepresentation(requiredFunctionTypeRepr);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since @convention(block) is the default, it seems simple to just change the condition here: canBridgeTypes(importKind) && importKind != ImportTypeKind::ObjCGenericArgument.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the rathole I went down before, because the way in which we pass down the desired "bridgeability" of the imported type doesn't account for blocks, which are bridgeable in some positions but not others. The problem with simply trying to exclude ObjCGenericArgument here is that the mappedType we get here has already been given the Swift function type representation (because we're importing a typedef with "full bridgeability"), and we no longer have the block type around.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, typedefs. In that case, can you update the comment above?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah.

…llectionElement.

This better describes what we're importing. Also note why we need to
reset to the block representation.
@DougGregor
Copy link
Member Author

@swift-ci please smoke test

1 similar comment
@DougGregor
Copy link
Member Author

@swift-ci please smoke test

@@ -1287,10 +1287,24 @@ static ImportedType adjustTypeForConcreteImport(
// we would prefer to instead use the default Swift convention.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I meant this comment, which refers to @convention(block).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Grrrr. That comment above is a lie. I'll do a separate comment-only PR for that

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