Commit Graph

3160 Commits

Author SHA1 Message Date
Slava Pestov
eabc8efb41 AST: Better cope with UnboundGenericType in TypeBase::getSuperclass()
Returning the unsubstituted superclass type is not correct,
because it may contain type parameters. Let's form a new
UnboundGenericType instead.

- Fixes https://github.com/swiftlang/swift/issues/82160.
- Fixes rdar://152989888.
2025-06-27 18:11:40 -04:00
Pavel Yaskevich
d8642fce19 [Tests] NFC: Update all of the test-cases improved by changes to generic argument mismatch handling
(cherry picked from commit 4132aa04f9)
2025-06-03 09:33:13 -07:00
Pavel Yaskevich
a6827c605d [CSSimplify] Detect when generic argument mismatch applies to argument and produce a tailed fix
The problem detection logic currently expects `generic argument #<N>`
location to always be associated with two generic types, but that
is not always the case, this locator element is sometimes used for
i.e. optional object types and pointer `Pointee` type when types
appear in argument positions. This needs to be handled specifically.

Resolves: rdar://82971941
(cherry picked from commit ded6158cc3)
2025-06-03 09:33:13 -07:00
Slava Pestov
7c4564cbdc Sema: Allow parameterized existential compositions 2025-05-23 14:20:35 -04:00
Rintaro Ishizaki
6d254b7fd9 Merge pull request #81653 from calda/cal--6.2-trailing-comma-fixes
[6.2] Add trailing comma support in cases missing from Swift 6.1
2025-05-21 14:00:18 -07:00
Andrew Trick
818b38bd00 Update test: protocols.swift for LifetimeDependence evaluation
Evaluating LifetimeDependence changes the order that the declarations are
diagnosed. Fix this test output by flipping the order of two declarations. The
new order actually makes more sense to me.

(cherry picked from commit c40fd2a0cce97d8983c9f23206ba89b4c06a4158)
2025-05-21 00:14:31 -07:00
Cal Stephens
e73beb435e Support trailing commas in types within expressions 2025-05-20 15:49:07 -07:00
Cal Stephens
f4bedbc720 Revert changes to trailing commas in attributes like @available 2025-05-20 15:49:03 -07:00
Cal Stephens
bad48874f0 Add trailing comma support in cases missing from Swift 6.1 2025-05-20 15:48:58 -07:00
Slava Pestov
2d76c38174 Merge pull request #80965 from slavapestov/fix-rdar149438520-6.2
[6.2] Sema: Fix case where witness thrown error type is a subtype of a type parameter
2025-04-25 21:52:52 -04:00
Slava Pestov
dc70a0e779 Sema: Fix case where witness thrown error type is a subtype of a type parameter
In b30006837e, I changed the `if`
condition here to check for the absence of type variables as well
as type parameters. This is incorrect; the type variables come up
in ValueWitnessRequest, and the type parameters come up in
associated type inference. We want the matching to be more lax
in the former case.

Fixes rdar://149438520.
2025-04-24 12:30:02 -04:00
Anthony Latsis
7b92a8f041 AST: Quote attributes more consistently in DiagnosticsParse.def 2025-04-23 19:18:11 +01:00
Anthony Latsis
5e41794680 AST: Quote attributes more consistently in DiagnosticsSema.def 2025-04-23 19:18:08 +01:00
Doug Gregor
3ccaffd07c Update tests for isolated conformances being enabled by default
(cherry picked from commit 3f1a6c84c2)
2025-04-14 16:38:26 -07:00
Doug Gregor
b182c96bd7 Print diagnostic group names by default
Print diagnostic groups as part of the LLVM printer in the same manner as the
Swift one does, always. Make `-print-diagnostic-groups` an inert option, since we
always print diagnostic group names with the `[#GroupName]` syntax.

As part of this, we no longer render the diagnostic group name as part
of the diagnostic *text*, instead leaving it up to the diagnostic
renderer to handle the category appropriately. Update all of the tests
that were depending on `-print-diagnostic-groups` putting it into the
text to instead use the `{{documentation-file=<file name>}}`
diagnostic verification syntax.
2025-03-29 15:40:56 -07:00
Doug Gregor
e88f8995e1 [Diagnostics] Eliminate educational notes in favor of diagnostic groups
We've been converging the implementations of educational notes and
diagnostic groups, where both provide category information in
diagnostics (e.g., `[#StrictMemorySafety]`) and corresponding
short-form documentation files. The diagnostic group model is more
useful in a few ways:

* It provides warnings-as-errors control for warnings in the group
* It is easier to associate a diagnostic with a group with
GROUPED_ERROR/GROUPED_WARNING than it is to have a separate diagnostic
ID -> mapping.
* It is easier to see our progress on diagnostic-group coverage
* It provides an easy name to use for diagnostic purposes.

Collapse the educational-notes infrastructure into diagnostic groups,
migrating all of the existing educational notes into new groups.
Simplify the code paths that dealt with multiple educational notes to
have a single, possibly-missing "category documentation URL", which is
how we're treating this.
2025-03-29 15:40:35 -07:00
Anthony Latsis
fffa8c2f51 Diag: Abstract away some calls to DeclAttribute::getAttrName 2025-03-28 02:01:27 +00:00
Doug Gregor
1b1f28decb Tighten up diagnostics wording a bit 2025-03-20 22:13:19 -07:00
Doug Gregor
9ea735b9ed Rework diagnostics for conformance isolation failures
A protocol conformance can be ill-formed due to isolation mismatches
between witnesses and requirements, or with associated conformances.
Previously, such failures would be emitted as a number of separate
errors (downgraded to warnings in Swift 5), one for each witness and
potentially an extra for associated conformances. The rest was a
potential flood of diagnostics that was hard to sort through.

Collect all of the isolation-related problems for a given conformance
together and produce a single error (downgraded to a warning when
appropriate) that describes the overall issue. That error will have up
to three notes suggesting specific courses of action:
* Isolating the conformance (when the experimental feature is enabled)
* Marking the witnesses as 'nonisolated' where needed
*

The diagnostic also has notes to point out the witnesses/associated
conformances that have isolation problems. There is a new educational
note that also describes these options.

We give the same treatment to missing 'distributed' on witnesses to a
distributed protocol.
2025-03-20 21:23:16 -07:00
Doug Gregor
cd99fb57fd [Diagnostics] Remove unhelpful notes from witness-isolation diagnostics
When diagnosing an isolation mismatch between a requirement and witness,
we would produce notes on the requirement itself suggesting the addition of
`async`. This is almost never what you want to do, and is often so far
away from the actual conforming type as to be useless. Remove this note,
and the non-function fallback that just points at the requirement, because
they are unhelpful.

This is staging for a rework of the way we deal with conformance-level
actor isolation problems.
2025-03-19 17:18:52 -07:00
Allan Shortlidge
0fd2f3fc1c Sema: Diagnose @_spi_available on declarations that cannot be unavailable.
The attribute makes the declaration unavailable from the perspective of clients
of the module's public interface and was creating a loophole that admitted
inappropriate unavailability.
2025-03-07 19:44:48 -08:00
Anthony Latsis
4a119a47db Merge pull request #79660 from AnthonyLatsis/thalassarche-melanophris
[NFC] test: Add regression test for #65533
2025-02-27 20:04:32 +00:00
Anthony Latsis
f56b53cab9 [NFC] test: Add regression test for #65533 2025-02-27 15:14:14 +00:00
Allan Shortlidge
62c8e72f23 AST: Centralize AvailabilityDomain lookup.
Implement lookup of availability domains for identifiers on
`AvailabilityDomainOrIdentifier`. Add a bit to that type which represents
whether or not lookup has already been attempted. This allows both
`AvailableAttr` and `AvailabilitySpec` to share a common implementation of
domain lookup.
2025-02-25 22:00:31 -08:00
Doug Gregor
d9ef00ce16 Look for keywords in inactive #if regions when checking for variable uses
Fixes issue #79555
2025-02-24 18:04:31 -08:00
Allan Shortlidge
902053341e Merge pull request #79423 from tshortli/case-iterable-invalid-available
AST: Type-check `@available` attributes before synthesizing `CaseIterable`
2025-02-16 17:58:10 -08:00
Allan Shortlidge
b6ee0796c8 AST: Type-check @available attributes before synthesizing CaseIterable.
An `AvailableAttr` written in source with an unrecognized availability domain
is now only marked invalid after type-checking the attribute. This resulted in a
regression where `CaseIterable` synthesis was blocked incorrectly under the
following very narrow circumstances:

1. Every `@available` attribute on the elements of the enum is invalid.
2. The module is being emitted and lazy type-checking is enabled.
3. The enum is public and the only top-level declaration in the file.

Type-checking the attribute was delayed just enough that it would not be
considered invalid by the type the `CaseIterable` conformance was being
synthesized, resulting in a spurious error.

There were zero tests exercising `CaseIterable` synthesis for enums with
elements that have availability requirements, so I added some.

Resolves rdar://144897917.
2025-02-16 12:20:48 -08:00
Allan Shortlidge
949a6c68d7 AST/Sema: Retire SemanticAvailableAttr::getVersionAvailability().
Query for availability constraints instead of calling getVersionAvailability().
2025-02-16 07:44:45 -08:00
Allan Shortlidge
3c8a57f86d AST: Use consolidated availability constraint query for diagnostics.
Switch to calling `swift::getAvailabilityConstraintsForDecl()` to get the
unsatisfied availability constraints that should be diagnosed.

This was intended to be NFC, but it turns out it fixed a bug in the recently
introduced objc_implementation_direct_to_storage.swift test. In the test,
the stored properties are as unavailable as the context that is accessing them
so the accesses should not be diagnosed. However, this test demonstrates a
bigger issue with `@objc @implementation`, which is that it allows the
implementations of Obj-C interfaces to be less available than the interface,
which effectively provides an availability checking loophole that can be used
to invoke unavailable code.
2025-02-16 07:44:45 -08:00
Anthony Latsis
2705212395 Merge pull request #79404 from AnthonyLatsis/fix-rdar141962317-6.2
Sema: Partially revert existential opening fix
2025-02-15 03:40:09 +00:00
Anthony Latsis
43e82b4f7e Sema: Partially revert existential opening fix
Selectively revert 36683a804c to resolve
a source compatibility regression. See inline comment for use case. We
are going to consider acknowledging this use case in the rules in a
future release.
2025-02-14 22:30:28 +00:00
Anthony Latsis
e8b393430f Merge pull request #78459 from AnthonyLatsis/tuber-magnatum-2
TypeCheckType: Unconditionally warn about missing existential `any` until Swift 7
2025-02-14 19:08:33 +00:00
Anthony Latsis
fe2408c0ab TypeCheckType: Unconditionally warn about missing existential any until Swift 7
https://github.com/swiftlang/swift/pull/72659 turned out to have some
source compatibility fallout that we need to fix. Instead of introducing
yet another brittle compatibility hack, stop emitting errors about a
missing `any` altogether until a future language mode.

Besides resolving the compatibility issue, this will encourage
developers to adopt any sooner and grant us ample time to gracefully
address any remaining bugs before the source compatibility burden
resurfaces.

A subsequent commit adds a diagnostic group that will allow users to
escalate these warnings to errors with `-Werror ExistentialAny`.
2025-02-12 21:20:44 +00:00
Anthony Latsis
02261dccc7 [NFC] TypeCheckType: Streamline logic in the any syntax checker 2025-02-12 21:20:44 +00:00
Rintaro Ishizaki
71b24665fa [ASTDumper] Dump DeclContext
* Include `DeclContext` of the node where possible
* Add 'default-with-decl-contexts' dump style that dumps the dect context
  hierarchy in addition to the AST
* Support `-dump-parse` with `-dump-ast-format json`
2025-02-12 10:53:33 -08:00
Slava Pestov
17d8c820dc Merge pull request #79220 from slavapestov/fix-rdar143950572
AST: Source range of FuncDecl/ConstructorDecl should include the thrown type
2025-02-10 13:49:37 -05:00
Slava Pestov
e9266c25d3 Sema: Fix accepts-invalid with throwing function types
We can't unconditionally skip the conformance check if the type contains type
parameters; instead, we only want to skip it in the structural resolution
stage. In interface resolution stage, we proceed by mapping the type into
the generic environment first.
2025-02-10 09:17:40 -05:00
Slava Pestov
8400b43388 Sema: Don't diagnose thrown error type that contains errors
If we have error types here, type resolution already diagnosed an
error; don't diagnose again.
2025-02-10 09:17:40 -05:00
Slava Pestov
a6a96da558 AST: Source range of FuncDecl/ConstructorDecl should include the thrown type
When a function declaration has a body, its source range ends at the
closing curly brace, so it includes the `throws(E)`. However, a
protocol requirement doesn't have a body, and due to an oversight,
getSourceRange() was never updated to include the extra tokens
that appear after `throws` when the function declares a thrown
error type. As a result, unqualified lookup would fail to find a
generic parameter type, if that happened to be the thrown type.

Fixes rdar://problem/143950572.
2025-02-10 09:17:40 -05:00
Allan Shortlidge
16de339051 AST: Enable -unavailable-code-optimization for zippered libraries.
Correct the determination of whether a declaration is unreachable at runtime
when compiling a zippered library.

Resolves rdar://125930716.
2025-02-09 11:20:48 -08:00
Becca Royal-Gordon
69a77d8f09 Merge pull request #79094 from beccadax/rdar143582383 2025-02-01 03:09:32 -08:00
Becca Royal-Gordon
956bd10be4 Prevent shadowing of unavailable member impls
Typically, access control denies access to member implementations, so the imported interface decl will be used instead. However, in contexts that permit direct access to stored properties—such as accessors, inits, and deinits—their member implementations are accessible; the compiler then relies on a shadowing rule favoring Swift decls over ObjC decls to eliminate the imported interface decl.

However, there are many rules that are higher-priority than the Swift vs. ObjC decls one. In particular, a recent change to availability checking in #77886 caused a higher-priority rule to begin eliminating member implementations which belonged to unavailable extensions. This caused regressions in projects using `@objc @implementation` with classes that are unavailable in Mac Catalyst.

Introduce a fairly high-priority shadowing rule that favors a member implementation over its interface when both are present (i.e. when direct access to storage is permitted).

Fixes rdar://143582383.
2025-01-31 21:01:06 -08:00
Slava Pestov
7c6c5fcd50 Sema: Fix generic type alias resolution edge case
If a type alias in generic context fixes all outer generic
parameters to concrete types, we allow the type alias to
be referenced without specifying the generic arguments of
its parent type.

However, we need to reduce the underlying type in case it
was written in terms of the (fully concrete) generic
parameters.

Fixes rdar://problem/143707820.
2025-01-30 15:34:10 -05:00
Allan Shortlidge
fdcf53cca6 Tests: Test conformance checker with witnesses in unavailable extensions.
A protocol conformance witness must be as available as its requirement. In
Swift 6.1 and earlier, the conformance checker failed to note that witnesses in
unavailable extensions are unavailable. That bug was fixed by a previous
change, but there was no test case covering it so the difference in behavior
was not acknowledged.

Related to rdar://143466010.
2025-01-23 21:13:24 -08:00
Anthony Latsis
6c9bb2a283 Merge pull request #78593 from AnthonyLatsis/cocos-nucifera
OpenedExistentials: Do not attempt to erase existential metatypes wit…
2025-01-19 03:35:22 +00:00
Anthony Latsis
e626887dcc Sema: Improve requirement indicator note for actor_isolated_witness
`x declared here` is not helpful and clear enough, especially when there
are other notes attached. Swap it for a new note that says
`requirement x declared here`.
2025-01-14 14:52:41 +00:00
Anthony Latsis
f85eb97337 ConformanceChecker: Make actor_isolated_witness call out the protocol 2025-01-14 14:49:22 +00:00
Anthony Latsis
75953d2b51 OpenedExistentials: Do not attempt to erase existential metatypes with invariant Self
The non-metatype case was never supported. The same should hold for the
existential metatype case, which used to miscompile and now crashes
because the invariant reference is deemed OK but the erasure expectedly
fails to handle it:

```swift
class C<T> {}
protocol P {
  associatedtype A

  func f() -> any P & C<A>
  func fMeta() -> any (P & C<A>).Type
}

do {
  let p: any P
  let _ = p.f() // error
  let _ = p.fMeta() // crash
}
```
2025-01-12 17:47:52 +00:00
Anthony Latsis
6612c9cf89 Sema: Subscript called with opened existential cannot produce lvalue if result is type-erased 2024-12-17 23:03:38 +00:00
Anthony Latsis
f43166a5d0 Merge pull request #77851 from AnthonyLatsis/day-2 2024-12-17 12:03:09 +00:00