Skip to content

[Clang Importer] Do not rely on being able to always import Foundation on-demand #34331

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
Nov 2, 2020

Conversation

artemcm
Copy link
Contributor

@artemcm artemcm commented Oct 16, 2020

When importing Clang types.
This is not an option with Explicit Module Builds. If the module being built does not (directly or transitively) depend on Foundation, then attempting to load Foundation will produce an error because implicit module loading is no longer allowed.

This change addresses a small number of cases where ClangImporter relies on being able to load Foundation on-demand:

  • When importing a single Decl from a clang module, we check whether it has certain conformances by checking all extensions of the NominalTypeDecl of the Decl in question, to see if any of the extensions contain the conformance we are looking for, but we only check extensions whose parent module is either the original module of the NominalTypeDecl or the overlay module of the NominalTypeDecl or Foundation. It seems that we do not need to actually import Foundation here, just checking the module Identifier should be sufficient.
  • In maybeImportNSErrorOutParameter, change the behavior to have an exit condition based on whether Foundation can be imported, before attempting to load it.
  • When checking whether or not we are allowed to bridge an Objective-C type, it also looks sufficient to query whether or not Foundation can be imported, without loading it.

Resolves rdar://70381338

@artemcm
Copy link
Contributor Author

artemcm commented Oct 16, 2020

@swift-ci please test

@artemcm
Copy link
Contributor Author

artemcm commented Oct 16, 2020

@swift-ci please smoke test

@artemcm artemcm marked this pull request as ready for review October 16, 2020 18:36
@artemcm artemcm requested review from slavapestov and beccadax and removed request for slavapestov October 16, 2020 18:36
@artemcm
Copy link
Contributor Author

artemcm commented Oct 16, 2020

@brentdax, @slavapestov, I don't have as deep of an understanding to know what I may be breaking here, really curious to hear what you think.

@artemcm
Copy link
Contributor Author

artemcm commented Oct 17, 2020

@swift-ci please test

@artemcm
Copy link
Contributor Author

artemcm commented Oct 19, 2020

@swift-ci Please Test Source Compatibility

4 similar comments
@artemcm
Copy link
Contributor Author

artemcm commented Oct 20, 2020

@swift-ci Please Test Source Compatibility

@artemcm
Copy link
Contributor Author

artemcm commented Oct 21, 2020

@swift-ci Please Test Source Compatibility

@artemcm
Copy link
Contributor Author

artemcm commented Oct 22, 2020

@swift-ci Please Test Source Compatibility

@artemcm
Copy link
Contributor Author

artemcm commented Oct 22, 2020

@swift-ci Please Test Source Compatibility

@artemcm
Copy link
Contributor Author

artemcm commented Oct 23, 2020

@swift-ci Please Test Source Compatibility Debug

2 similar comments
@artemcm
Copy link
Contributor Author

artemcm commented Oct 23, 2020

@swift-ci Please Test Source Compatibility Debug

@artemcm
Copy link
Contributor Author

artemcm commented Oct 23, 2020

@swift-ci Please Test Source Compatibility Debug

Copy link
Contributor

@beccadax beccadax left a comment

Choose a reason for hiding this comment

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

I think this will be okay, but I wouldn't be surprised if we see regressions in some weird edge cases where ClangImporter implicitly loading Foundation turns out to have been load-bearing. We'll need to keep an eye out for that.

@@ -5583,8 +5583,7 @@ namespace {
}

static bool conformsToProtocolInOriginalModule(NominalTypeDecl *nominal,
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: Now that we're not passing in Foundation, this should probably be called something like conformsToProtocolInFoundationModule().

@artemcm
Copy link
Contributor Author

artemcm commented Oct 26, 2020

@swift-ci please smoke test

@artemcm
Copy link
Contributor Author

artemcm commented Oct 26, 2020

@swift-ci please smoke test Windows platform

@artemcm
Copy link
Contributor Author

artemcm commented Oct 27, 2020

@swift-ci please smoke test

2 similar comments
@artemcm
Copy link
Contributor Author

artemcm commented Oct 27, 2020

@swift-ci please smoke test

@artemcm
Copy link
Contributor Author

artemcm commented Oct 28, 2020

@swift-ci please smoke test

@artemcm
Copy link
Contributor Author

artemcm commented Oct 28, 2020

@swift-ci please smoke test macOS platform

…n on-demand

When importing Clang types.
This is not an option with Explicit Module Builds. If the module being built does not (directly or transitively) depend on `Foundation`, then attempting to load it will produce an error because Implicit module loading is no longer allowed..

This change addresses a small number of cases where ClangImporter relies on being able to load `Foundation` on-demand:
- When importing a single Decl from a clang module, we check whether it has certain conformances by checking all extensions of the NominalTypeDecl of the Decl in question, to see if any of the extensions contain the conformance we are looking for, but we only check extensions whose parent module is either the original module of the NominalTypeDecl or the overlay module of the NominalTypeDecl or Foundation. It seems that we do not need to actually import `Foundation` here, just checking the module Identifier should be sufficient.
- In `maybeImportNSErrorOutParameter`, change the behavior to have an exit condition based on whether `Foundation` can be imported, before attempting to load it.
- When checking whether or not we are allowed to bridge an Objective-C type, it also looks sufficient the query whether or not `Foundation` *can* be imported, without loading it.
@artemcm
Copy link
Contributor Author

artemcm commented Oct 28, 2020

It looks like it wasn't load-bearing when I put up the PR, but became so since then for these tests:

Swift(macosx-x86_64) :: Interop/Cxx/class/constructors-objc-silgen.swift
Swift(macosx-x86_64) :: Interop/Cxx/class/constructors-objc-irgen.swift

@artemcm artemcm force-pushed the NoFoundationOnExplicitImportSoil branch from a30cb65 to e98dbc8 Compare October 29, 2020 17:37
@artemcm
Copy link
Contributor Author

artemcm commented Oct 29, 2020

@swift-ci please smoke test

@artemcm
Copy link
Contributor Author

artemcm commented Oct 29, 2020

@swift-ci please smoke test macOS platform

4 similar comments
@artemcm
Copy link
Contributor Author

artemcm commented Oct 29, 2020

@swift-ci please smoke test macOS platform

@artemcm
Copy link
Contributor Author

artemcm commented Oct 29, 2020

@swift-ci please smoke test macOS platform

@artemcm
Copy link
Contributor Author

artemcm commented Oct 30, 2020

@swift-ci please smoke test macOS platform

@artemcm
Copy link
Contributor Author

artemcm commented Oct 30, 2020

@swift-ci please smoke test macOS platform

@artemcm
Copy link
Contributor Author

artemcm commented Nov 2, 2020

@swift-ci please smoke test

1 similar comment
@artemcm
Copy link
Contributor Author

artemcm commented Nov 2, 2020

@swift-ci please smoke test

@artemcm
Copy link
Contributor Author

artemcm commented Nov 2, 2020

@swift-ci please smoke test Windows platform

1 similar comment
@artemcm
Copy link
Contributor Author

artemcm commented Nov 2, 2020

@swift-ci please smoke test Windows platform

@artemcm artemcm merged commit 3b54549 into swiftlang:main Nov 2, 2020
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