Commit Graph

142 Commits

Author SHA1 Message Date
Slava Pestov
6701e5b39b Sema: Tighten ridiculous disambiguation hack for overloaded type declarations
We noticed that a project failed to build with prepared overloads enabled,
but built successfully with prepared overloads disabled.

It turns out that the code clearly should never have been accepted in the
first place, because the type checker was making an arbitrary choice between
two nominal type declarations with the same name.

Further inspection revealed this was because of a FIXME added in 2013 which
was far too broad. Tighten up the logic here to only disambiguate if at least
one of the two declarations is a type alias, and it has the same underlying
type as the other one. A choice between unrelated nominal type declarations
should always be ambiguous.

This still isn't perfect, because we might have two generic type aliases
that are referenced in both solutions with different substitution maps,
in which case we will still erroneously pick the first one. But this
is better than the old logic, at least.

Fixes rdar://165863775.
2025-12-09 22:48:12 -05:00
Kavon Farvardin
12cb3f9a9a Merge pull request #85704 from kavon/suppressed-assoc-types-with-defaults
Sema: introduce SuppressedAssociatedTypesWithDefaults
2025-12-03 23:07:40 -08:00
Kavon Farvardin
ab1d83a31b Sema: fix cycle involving protocol extensions
We've had a load-bearing optimization that avoids rebuilding
the generic signature of the protocol extension to avoid a
cycle involving a typealias in the extension that's mentioned
as a requirement in the protocol, for example:

```
public protocol P3 {
  associatedtype A
}

public protocol P4: P3 where A == B {}

extension P4 {
  public typealias B = String
}
```
What was happening is that we're inside a
StructuralRequirementsRequest, so we're performing TypeResolution
in the Structural stage. When we encounter this typealias that's
within a protocol extension, we'd end up accidentally requesting
the interface type of that alias through
`TypeChecker::substMemberTypeWithBase`
(via `aliasDecl->getUnderlyingType()`).

What we should do instead is call `aliasDecl->getStructuralType()`
and skip the substitution during that stage.

There happened to already be code doing this, but it was only
kicking in if the DeclContext `isa<ProtocolDecl>`, which excludes
extensions of a protocol. I see no reason why extensions of a
protocol should be excluded, so I assume it was unintentional.

Thus, the fix boils down to using `DeclContext::getSelfProtocolDecl`
which does include extensions of protocols. With this fix, the
optimization is no longer load-bearing on the example above.

See the history of this hack in f747121080
or rdar://problem/129540617
2025-12-03 08:03:19 -08:00
Becca Royal-Gordon
b9747b77da Don’t repeat lookup work that may not round-trip
When a DeclRefTypeRepr is bound to a known declaration, the exact DeclNameRef used to create it is erased. This means that we no longer know exactly which module selector was used in the source code, so repeating the lookup with the same instance might produce different results.

Avoid this problem by reusing the bound decl instead of trying to look up the same declaration again.

Fixes rdar://164647850.
2025-11-15 19:16:12 -08:00
Hamish Knight
f2b0b92dc1 [Sema] Avoid member attribute cycle in ResolveMacroRequest
If we have a custom attribute on a type that does a qualified lookup
into the same type, we need to be able to expand member attribute
macros for that type. As such, the check to see if we already have
a nominal for the attribute would hit a cycle. Limit this check such
that it only applies to local vars, which is the only case where it
actually matters.

rdar://163961797
2025-11-06 19:33:09 +00:00
Becca Royal-Gordon
393965090e Merge pull request #34556 from beccadax/mod-squad-2
[SE-0491] Implement lookup and diagnostics for module selectors (MyMod::someName)
2025-10-28 16:00:26 -07:00
Becca Royal-Gordon
e6d8b02626 Make module selectors non-experimental
Approved by SE-0491.
2025-10-24 16:23:50 -07:00
Becca Royal-Gordon
e3495f5fe7 Forbid module selectors on dependent member types
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.
2025-10-24 16:23:50 -07:00
Becca Royal-Gordon
ec92be4955 Support module selectors for cross-import overlays 2025-10-24 16:23:50 -07:00
Becca Royal-Gordon
42fdd1d30c Improve module selector macro diagnostics 2025-10-24 16:23:49 -07:00
Becca Royal-Gordon
b65e3c9c15 Improve module selector @_dynamicReplacement diagnostics 2025-10-24 16:23:49 -07:00
Becca Royal-Gordon
60cc537f70 Improve module selector constraint solver diagnostics 2025-10-24 16:23:49 -07:00
Becca Royal-Gordon
d1e391094d Improve module selector expr lookup diagnostics 2025-10-24 16:23:49 -07:00
Becca Royal-Gordon
7949224b69 Improve module selector type lookup diagnostics 2025-10-24 16:23:48 -07:00
Becca Royal-Gordon
ffaa35810c Handle lookup into Builtin with module selector
Lookups like Builtin::Int64 were failing because BuiltinUnit rejected all unqualified lookups. Make it allow unqualified lookups with a module selector.
2025-10-24 16:23:48 -07:00
Becca Royal-Gordon
7bf14de2e6 Handle module selectors with local vars right 2025-10-24 16:23:34 -07:00
Becca Royal-Gordon
b35aaef526 Handle module selectors in macro lookups 2025-10-24 16:23:34 -07:00
Becca Royal-Gordon
f43bd72207 Handle module selectors in UDRE lookups 2025-10-24 16:23:34 -07:00
Becca Royal-Gordon
2869b334ee Handle module selectors in unqualified lookup 2025-10-24 16:23:34 -07:00
Becca Royal-Gordon
d30b46efcd Root unqualified lookups with module selectors at file scope 2025-10-24 16:23:33 -07:00
Becca Royal-Gordon
1dfc0bff86 Handle module selectors in qualified lookup 2025-10-24 16:23:33 -07:00
Becca Royal-Gordon
64d8b70942 Make DeclNameRef able to store a module selector
In this commit, this change affects certain diagnostics but doesn’t actually alter name lookup behavior yet.
2025-10-24 16:23:33 -07:00
Allan Shortlidge
d76670723c Tests: Add a MemberImportVisibility test case for rdar://162130647. 2025-10-22 16:10:43 -07:00
Becca Royal-Gordon
1ecabaf4c7 [NFC] Add skeleton of module selector lookup test
The diagnostics in this test will evolve as we implement pieces of the feature.
2025-10-22 13:05:19 -07:00
Hamish Knight
c23de594fc Merge pull request #84981 from hamishknight/give-me-closure
[ASTScopes] Avoid relying on `in` SourceLoc for closures
2025-10-18 12:44:12 +01:00
Becca Royal-Gordon
d8d226c966 Merge pull request #84362 from beccadax/mod-squad-parse
[SE-0491] Parse and ASTGen module selectors
2025-10-17 23:34:11 -07:00
Hamish Knight
8232345b68 [ASTScopes] Avoid relying on in SourceLoc for closures
Missed this in my previous patch, avoid relying on the `in` location
for closures with parameters and capture lists. Instead, form scopes
that start at the first element of the body. This fixes a crasher
uncovered by the fuzzer.
2025-10-17 15:21:53 +01:00
Becca Royal-Gordon
f1db5d3a6a [NFC] Add tok::colon_colon to parser 2025-10-16 13:30:29 -07:00
Hamish Knight
157bba140b [test] Silence IRGen output from property_wrapper_and_macro.swift 2025-10-16 11:21:54 +01:00
Hamish Knight
4b41fe1142 [NameLookup] Prefer property wrapper over attached macro on local var
We don't properly support peer declarations in local contexts, as such
if we have both a property wrapper and macro with a given name on a
local var, let's prefer the property wrapper.

rdar://148782639
2025-10-13 13:37:29 +01:00
Hamish Knight
11d299c212 Merge pull request #84698 from hamishknight/carousel
[Evaluator] Avoid emitting duplicate "through reference here" notes
2025-10-07 20:14:52 +01:00
Hamish Knight
13023de4c4 [Evaluator] Avoid emitting duplicate "through reference here" notes
Filter out any duplicate notes to help cut down on the noise for
request cycle diagnostics. Some of the note locations here still aren't
great, but this at least stops us from repeating them for each
intermediate request.
2025-10-05 20:48:08 +01:00
Henrik G. Olsson
cbc0ec3b88 Add -verify-ignore-unrelated where necessary (NFC)
These are tests that fail in the next commit without this flag. This
does not add -verify-ignore-unrelated to all tests with -verify, only
the ones that would fail without it. This is NFC since this flag is
currently a no-op.
2025-10-04 14:19:52 -07:00
Allan Shortlidge
0fbf70e9d4 Merge pull request #84659 from tshortli/member-import-visibility-resolve-decl-ref-expr-fix
Sema: Fix a MemberImportVisibility regression in `resolveDeclRefExpr()`
2025-10-03 22:36:33 -07:00
Allan Shortlidge
8053120624 Sema: Fix a MemberImportVisibility regression in resolveDeclRefExpr().
The changes from https://github.com/swiftlang/swift/pull/84259/ caused a
regression in which declarations from an outer scope could be shadowed
inappropriately by member declarations from a module that has not been
imported. This fix addresses the issue by refactoring `resolveDeclRefExpr()` so
that it performs lookups in two passes. First, it attempts to resolve the decl
ref using the complete lookup algorithm and standard name lookup options, which
will ignore members from modules that haven't been imported if
`MemberImportVisibility` is enabled. Then, if no results were found it re-runs
the full lookup algorithm again with `NameLookupFlags::IgnoreMissingImports`
included to find additional results that ought to be diagnosed as unavailable.
This insures that the unavailable results are not considered until after the
main lookup has already failed.

Resolves rdar://161078015.
2025-10-03 17:33:56 -07:00
Doug Gregor
67c7fbc87b [SE-0495] Make sure we can find imported C functions for @c @implementation
@c @implementation relies on matching the original C declaration. The
lookup for the original C declaration was doing the wrong kind of
lookup, meaning that it could only find the C declaration if it came
through a bridging header, and not through a normal module import.
Using unqualified lookup here finds the name appropriately.

Clarify the diagnostics here as well to not talk about umbrella and
bridging headers.

Fixes rdar://161909754.
2025-10-03 12:03:12 -07:00
Allan Shortlidge
d8fac32dd8 Sema: Fix a crash in migrate mode for MemberImportVisibility.
`TypeChecker::resolveDeclRefExpr()` would leave error nodes in the AST when
performing fallback name lookups with `MemberImportVisibility`. When running in
migration mode for `MemberImportVisibility`, these error nodes would then cause
the compiler to either diagnose a missing error or to crash during SILGen.

Instead of restricting name lookup during `TypeChecker::resolveDeclRefExpr()`,
allow it to find candidates with missing imports and then diagnose them if
necessary before forming a resolve decl ref.

Resolves rdar://154361861.
2025-09-15 20:48:06 -07:00
Allan Shortlidge
d9c494efea Tests: Expand MemberImportVisibility tests. 2025-09-12 19:55:42 -07:00
Allan Shortlidge
0c5eac5c3e Sema: Downgrade missing import diagnostics for key paths.
For now, downgrade the `MemberImportVisibility` diagnostics introduced in
https://github.com/swiftlang/swift/pull/83934 to warnings.

Resolves rdar://160146503.
2025-09-08 16:15:49 -07:00
Allan Shortlidge
ea714d246f Sema: Diagnose key paths when MemberImportVisibility is enabled.
The diagnostic pass that checks whether expressions reference member
declarations that have not been imported failed to handle key path expressions.

Resolves rdar://159093481.
2025-08-27 09:44:38 -07:00
Kavon Farvardin
da53b24d76 test: @_inlineable -> @inlinable
The Swift 4 spelling only should remain in specific tests that ensure it's still accepted in that mode.
2025-08-12 14:32:24 -07:00
Allan Shortlidge
aca604660f AST/Sema: Make MemberImportVisibility a migratable feature.
The migration to `MemberImportVisibility` can be performed mechanically by
adding missing import declarations, so offer automatic migration for the
feature.

Resolves rdar://151931597.
2025-05-30 15:34:08 -07:00
Allan Shortlidge
6ea2dd8fac AST/Sema: Remove code adding @_spi to suggested import fix-its.
The was never invoked because inaccessibility due to SPI protection level is
always diagnosed before missing imports are diagnosed. The functionality could
therefore not be tested and should be removed.
2025-05-30 15:34:08 -07:00
Allan Shortlidge
dbb1d783d9 Sema: Omit internal from MemberImportVisibility fix-its when appropriate.
Omit an explicit access level from `MemberImportVisibility` fix-its under the
following conditions:

- `InternalImportsByDefault` is enabled.
- The required import needs an `internal` access level or lower.
- The module is not yet imported explicitly in any other file.

Resolves rdar://149577615.
2025-05-26 22:56:47 -07:00
Allan Shortlidge
426f48dcd4 Tests: Rename test cases associated with MemberImportVisibility. 2025-05-26 22:56:47 -07:00
Allan Shortlidge
78faa1aad2 Tests: Verify MemberImportVisibility honors -public-module-name. 2025-05-22 13:49:16 -07:00
Becca Royal-Gordon
c3a61bee0e Add experimental feature for module selector 2025-05-14 11:16:02 -07:00
Allan Shortlidge
f3ef07083b AST: Filter out some Obj-C overrides when MemberImportVisibility is enabled.
Unlike in Swift, Obj-C allows method overrides to be declared in extensions
(categories), even outside of the module that defines the type that is being
extended. When MemberImportVisibility is enabled, these overrides must be
filtered out to prevent them from hijacking name lookup and causing the
compiler to insist that the module that defines the extension be imported.

Resolves rdar://145329988.
2025-04-25 08:05:32 -07:00
Allan Shortlidge
a8ce5462a0 Revert "AST: Filter out some Obj-C overrides when MemberImportVisibility is enabled."
This reverts commit eafb84713e.
2025-04-21 17:04:11 -07:00
Allan Shortlidge
374fc90b42 Merge pull request #80563 from tshortli/member-import-visibility-objc-overloads-in-extensions
AST: Filter out some Obj-C overrides when MemberImportVisibility is enabled
2025-04-06 03:26:38 -07:00