Skip to content

Conversation

@beccadax
Copy link
Contributor

@beccadax beccadax commented Nov 3, 2020

This pull request implements the lookup behavior, including tailored diagnostics, for "module selectors", which allow references to declarations to be unambiguously prefixed with a module they can be found through:

class MyClass: ObjectiveC::NSObject {}  // OK, ObjectiveC defines NSObject
class MyClass: Foundation::NSObject {}  // OK, Foundation re-exports NSObject
class MyClass: Swift::NSObject {}       // error, Swift does not re-export or define NSObject
class MyClass: MyModule::NSObject {}    // error, MyModule does not re-export or define NSObject

Since SE-0491 has been approved and this PR is sufficient to make them functional, it also changes them from an experimental feature to an ordinary on-by-default language feature.

Parsing for this feature has already landed in #84362. Conditional emission into module interface files will land in a future PR.

This work was previously in PR #28834, which was closed during the master-to-main transition.

Fixes rdar://problem/19481048.

@beccadax beccadax changed the title Module selectors Implement experimental module selectors (MyMod::someName) feature Nov 3, 2020
@beccadax beccadax force-pushed the mod-squad-2 branch 3 times, most recently from eb8ecdf to b6d41f6 Compare July 21, 2021 20:18
@nkcsgexi
Copy link
Contributor

Woohoo! Thank you for reviving this, Becca!

@beccadax
Copy link
Contributor Author

@swift-ci test

@beccadax
Copy link
Contributor Author

@swift-ci test

Comment on lines +300 to +303
// Disable module selector filtering--UnqualifiedLookupRequest should have
// done it.
LookupResultBuilder builder(result, dc, /*moduleSelector=*/Identifier(),
options);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

@slavapestov This is the result of that review comment you made on the other PR.

@beccadax
Copy link
Contributor Author

@swift-ci please test

In this commit, this change affects certain diagnostics but doesn’t actually alter name lookup behavior yet.
Adds comments explaining why these conversions aren’t lossy.
Lookups like Builtin::Int64 were failing because BuiltinUnit rejected all unqualified lookups. Make it allow unqualified lookups with a module selector.
In code like the following:

```
protocol P { associatedtype A: Hashable }
protocol Q { associatedtype A: Comparable }

func fn<T: P & Q>(_: T) where T.A == Int { … }
```

`T.A` is actually the union of `P.A` and `Q.A`—it satisfies both associated types and has both of their constraints. This means it doesn’t actually make sense to apply a module selector to `A`—even if `P` and `Q` are in different modules, `T.A` always represents both of the declarations, not one or the other. We therefore now ban module selectors in this position, since they don’t actually jibe with the nature of a generic signature.

This justification technically doesn’t hold for *every* member type of a generic parameter—a member type can refer to a concrete typealias in a protocol extension, for instance—but in those situations, you can disambiguate (and add module selectors) by writing `P.A` or `Q.A` instead of `T.A`, so we’re not really worried about this limitation.
@beccadax
Copy link
Contributor Author

@swift-ci please smoke test

@beccadax beccadax enabled auto-merge October 28, 2025 17:39
@beccadax beccadax merged commit 3939650 into swiftlang:main Oct 28, 2025
3 checks passed
@nkcsgexi
Copy link
Contributor

🎉

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.

3 participants