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-6706] Swift should complain about weak references to classes that don't support them #17792

Merged
merged 4 commits into from Jun 11, 2019

Conversation

ChristopherRogers
Copy link
Contributor

This pull requests adds diagnostics similar to Clang when attempting to create weak or unowned references of types that do not support them (NSFont, NSWindow, etc.). This is indicated with the objc_arc_weak_reference_unavailable Clang attribute.

I thought about limiting this to only run on macOS since in practice this only affects macOS targets. (The only classes affected are from the macOS SDK). I've left it for completion's sake since someone could add the attribute to their own Objective-C class if they so choose..

Resolves SR-6706.

ClassDecl *underlyingClass = underlyingType->getClassOrBoundGenericClass();
while (underlyingClass != nullptr) {
if (auto clangDecl = underlyingClass->getClangDecl()) {
if (auto underlyingObjCDecl = cast<clang::ObjCInterfaceDecl>(clangDecl)) {
Copy link
Member

Choose a reason for hiding this comment

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

Can this cast ever fail, eg if you have a CF type or DispatchQueue or something?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah, I didn't think of that. Looks like it would fail then. My original intention was to use dyn_cast just in case some new language bridge was added (C++? lol). Thanks.

About CF types & Dispatch objects, isArcWeakrefUnavailable() is only defined on clang::ObjCInterfaceDecl so I assume the attribute it tests for cannot be added to anything else--or at least Clang doesn't support it at the moment.

@jrose-apple
Copy link
Contributor

@ChristopherRogers talked to me about this at try! Swift San Jose today. Sorry for losing the PR for almost a year! To avoid having Sema reason about Clang declarations, I suggested adding a bit to ClassDecl to track this, which ClangImporter can set and Sema can check.

@ChristopherRogers
Copy link
Contributor Author

Some notes:

  • At first I thought maybe I should just set the bit correctly in the constructor but that triggered an assertion so I assume that was the wrong way to go about it.
  • I usually like avoiding negatives for boolean names to avoid potential double negatives but in this case it seems that there's a convention of having the default always be zero so I prioritized that (and the wording wasn't that awful).
  • I changed the diagnostic message (which was based on the Clang message) to use the word "reference" instead of "variable".

I think I've addressed everything. Let me know if you have any other concerns or nitpicks. Thank you for your help yesterday!

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, @ChristopherRogers! I still think it'd be a good idea to check an Objective-C subclass of NoWeakClass as well, but this looks good regardless.

/// references.
///
/// Note that this is true if this class or any of its ancestor classes
/// are marked incompatible.
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice, this does make sense to package up in the accessor.

@jrose-apple
Copy link
Contributor

@swift-ci Please test

@jrose-apple
Copy link
Contributor

And while I don't see how it could cause a problem, let's make sure…

@swift-ci Please test source compatibility

@swift-ci

This comment has been minimized.

@jrose-apple jrose-apple self-assigned this Jun 10, 2019
@swift-ci

This comment has been minimized.

@ChristopherRogers
Copy link
Contributor Author

I had forgotten to require objc_interop for the test file so it was failing on Linux. The macOS compatibility suite had some error with a ClockKit module not being found, but the previous build job had the same error as well.

@jrose-apple
Copy link
Contributor

@swift-ci Please test

@jrose-apple
Copy link
Contributor

jrose-apple commented Jun 10, 2019

I think this is still going to fail but I want to at least see the proper failures.

@swift-ci Please test source compatibility

@swift-ci

This comment has been minimized.

@swift-ci

This comment has been minimized.

@jrose-apple
Copy link
Contributor

Source compat failures match those on master. Thanks, @ChristopherRogers!

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.

None yet

4 participants