Commit Graph

614 Commits

Author SHA1 Message Date
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
0877aa13dc Merge pull request #79322 from AnthonyLatsis/charcharadon-carcharias
TypeResolution: Stop resolving unqualified protocol type aliases to `DependentMemberType` in structural stage
2025-02-14 03:36:43 +00:00
Anthony Latsis
3222053810 AST: Add a diagnostic group for any syntax diagnostics
Allow users to escalate `any` syntax warnings to errors with
`-Werror ExistentialAny`.
2025-02-12 22:22:10 +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
251ac929be TypeResolution: Stop resolving unqualified protocol type aliases to DependentMemberType in structural stage
This workaround is no longer needed because `TypeAliasType` is now
modeled using generic arguments (as opposed to a substitution map).
Previously, computing the substitution map in `StructuralTypeRequest` to
construct the resulting `TypeAliasType` could cause a request cycle
around generic signature computation.
2025-02-12 17:10:27 +00:00
Slava Pestov
cda3cda132 Sema: Rework change recording in PotentialBindings::retract()
Instead of making an undo() do an infer(), let's record fine-grained
changes about what was retracted, and directly re-insert the same
elements into the data structures.
2025-02-04 20:26:12 -05:00
Anthony Latsis
35f1370f6e TypeCheckType: Rework IUO diagnostics using behavior limitation
Also stop suggesting a '?' fix-it for casts, where it is not likely to
be helpful because the common intention is either to force the optional
or declare an IUO.
2025-01-21 21:41:00 +00:00
Anthony Latsis
9dad512515 Merge pull request #78356 from AnthonyLatsis/tuber-melanosporum
Sema: Minor QoI improvements for existential `any` diagnosis
2025-01-02 11:58:34 +00:00
Anthony Latsis
56e2e19707 AST: Make HasSelfOrAssociatedTypeRequirementsRequest evaluate to false on cycle
Because we will have considered all the protocols in a cyclic hierarchy
by the time the cycle is hit.
2024-12-23 21:56:22 +00:00
Becca Royal-Gordon
73377fe6b4 [NFC-ish] Fix some crashes from early Decl::dump()
Changes dump output used in one test; otherwise NFC.
2024-12-19 15:47:07 -08:00
Allan Shortlidge
eafea5b21e Sema: Relax availability checking in universally unavailable contexts.
Recent refactoring fixed a bug that previously caused `f()` to be checked as if
it were unavailable only on macOS in the following example:

```
@available(macOS, unavailable)
struct Outer {
  @available(*, unavailable)
  func f() {
    someFunctionUnavailableOnMacOS()
  }
}
```

Unfortunately, fixing that bug made a different existing availability checking
rule more problematic. References to declarations that are unavailable on the
current platform have been diagnosed as unavailable even in contexts that are
universally unavailable. This long standing behavior is overly strict but it
rarely had consequences. However, now that the example above is modeled
correctly, this overly strict behavior is causing some source compatibility
issues. The easiest solution is to relax the overly strict checking.

Resolves rdar://141124478.
2024-12-09 17:15:15 -08:00
Slava Pestov
c0787f337f Sema: Fix soundness hole with variable initializers and opaque return types
Just because the type of the initializer expression is an opaque return type,
does not mean it is the opaque return type *for the variable being initialized*.

It looks like there is a bit of duplicated logic and layering violations going
on so I only fixed one caller of openOpaqueType(). This addresses the test case
in the issue. For the remaining calls I added FIXMEs to investigate what is
going on.

Fixes https://github.com/swiftlang/swift/issues/73245.
Fixes rdar://127180656.
2024-12-04 15:13:22 -05:00
Slava Pestov
eeab483cab Sema: Undo changes in chronological order in SolverTrail::undo() 2024-11-19 21:24:12 -05:00
Hamish Knight
b644cd87a9 [Sema] Ensure performStmtDiagnostics is called for CaseStmts
Previously we would check if we have a SwitchStmt,
and apply diagnostics such as `checkExistentialTypes`
to the CaseStmts individually. This however would
have been missed for `catch` statements. The change
to consistently call `performStmtDiagnostics` in
closures fixed this for `do-catch`'s in closures,
this commit fixes it for those outside of closures.
Because this is source breaking, the existential
diagnostic is downgraded to a warning until Swift
7 for catch statements specifically.

While here, also apply the ambiguous where clause
diagnostic to `catch` statements.
2024-11-12 18:26:54 +00:00
Slava Pestov
b5af518638 Merge pull request #77491 from slavapestov/fix-rdar139237671
Sema: Allow closure parameter lists to reference opaque parameter declarations
2024-11-08 19:31:43 -05:00
Slava Pestov
e9ff8ad889 Sema: Allow closure parameter lists to reference opaque parameter declarations
A function declaration cannot have an opaque parameter type appearing in
consuming position:

    func f(_: (some P) -> ()) {}

However, we should skip this check for a closure, because if the
closure's parameter list references an opaque parameter declaration,
it means something else: namely, the inferred type of the closure
refers to an opaque parameter from an outer scope. That's allowed.

This unnecessary prohibition has been there ever since the check was
added, but only for multi-statement closures, so nobody seemed to
notice.

When https://github.com/swiftlang/swift/pull/76473 made it so we always
call TypeChecker::checkParameterList(), this exposed the problem in a
single-expression closure in an existing project.

Fixes rdar://139237671.
2024-11-08 14:59:15 -05:00
Allan Shortlidge
09af72e168 Tests: Consolidate opaque result type availability tests. 2024-11-06 16:08:57 -08:00
Daniel Rodríguez Troitiño
ba68faaed5 [test] Mark tests that use experimental/upcoming features as such
Find all the usages of `--enable-experimental-feature` or
`--enable-upcoming-feature` in the tests and replace some of the
`REQUIRES: asserts` to use `REQUIRES: swift-feature-Foo` instead, which
should correctly apply to depending on the asserts/noasserts mode of the
toolchain for each feature.

Remove some comments that talked about enabling asserts since they don't
apply anymore (but I might had miss some).

All this was done with an automated script, so some formatting weirdness
might happen, but I hope I fixed most of those.

There might be some tests that were `REQUIRES: asserts` that might run
in `noasserts` toolchains now. This will normally be because their
feature went from experimental to upcoming/base and the tests were not
updated.
2024-11-02 11:46:46 -07:00
Hamish Knight
2d7500eda6 [AST] Remove ParenType
Today ParenType is used:

1. As the type of ParenExpr
2. As the payload type of an unlabeled single
   associated value enum case (and the type of
   ParenPattern).
3. As the type for an `(X)` TypeRepr

For 1, this leads to some odd behavior, e.g the
type of `(5.0 * 5).squareRoot()` is `(Double)`. For
2, we should be checking the arity of the enum case
constructor parameters and the presence of
ParenPattern respectively. Eventually we ought to
consider replacing Paren/TuplePattern with a
PatternList node, similar to ArgumentList.

3 is one case where it could be argued that there's
some utility in preserving the sugar of the type
that the user wrote. However it's really not clear
to me that this is particularly desirable since a
bunch of diagnostic logic is already stripping
ParenTypes. In cases where we care about how the
type was written in source, we really ought to be
consulting the TypeRepr.
2024-10-31 11:32:40 +00:00
Allan Shortlidge
f827e7c6d3 Tests: Remove -disable-availability-checking in variadic generics tests. 2024-10-20 15:37:15 -07:00
Hamish Knight
3367d8b3d1 [Sema] Remove workaround from TypeExpr::createForSpecializedDecl
This no longer seems to be necessary now that we
eagerly open UnboundGenericTypes in
`resolveTypeReferenceInExpression`.
2024-09-13 11:30:06 +01:00
Slava Pestov
21905afd85 AST: Simplify ProtocolCompositionType::getMinimalCanonicalType() and remove bogus assert
Fixes https://github.com/swiftlang/swift/issues/76164
2024-08-30 15:22:47 -04:00
Hamish Knight
3c16ecf568 [Sema] Remove preCheckExpression
There are only a couple of clients left using this,
migrate them onto `preCheckTarget`.
2024-08-28 15:09:40 +01:00
Allan Shortlidge
ad1acd7f4a Frontend: Allow -enable-experimental-feature to specify upcoming features.
During the lifecycle of a feature, it may start as an experimental feature and
then graduate to become an upcoming feature. To preserve compatibility with
projects that adopted the feature when it was experimental,
`-enable-experimental-feature` ought to be able to enable upcoming features,
too.

Since projects may use `-enable-experimental-feature` for compatibility with an
older toolchain that does not have the feature as an upcoming feature, there is
no warning when the flag is used to enable an upcoming feature.

Note that if the semantics of a feature change when it graduates from
experimental to upcoming, then the feature must be renamed so that projects
using the experimental feature have an opportunity opt-in to the new semantics
of the upcoming feature.

Resolves rdar://134276783.
2024-08-20 07:49:33 -07:00
Alex Hoppen
66104395d7 [Sema/SourceKit] Emit same diagnostics for missing protocol requirements on the command line and in SourceKit
Some editors use diagnostics from SourceKit to replace build issues. This causes issues if the diagnostics from SourceKit are formatted differently than the build issues. Make sure they are rendered the same way, removing most uses of `DiagnosticsEditorMode`.

To do so, always emit the `add stubs for conformance` note (which previously was only emitted in editor mode) and remove all `; add <something>` suffixes from notes that state which requirements are missing.

rdar://129283608
2024-08-07 14:01:30 -07:00
Anthony Latsis
d8e5733a90 [NFC] Merge test/type/explicit_existential & test/type/explicit_existential_swift6 2024-07-25 08:59:20 -07:00
Anthony Latsis
6e5d6f1b3b ExistentialTypeSyntaxChecker: Fix any fix-it for IUO 2024-06-04 02:13:08 +03:00
Anthony Latsis
704244b477 ExistentialTypeSyntaxChecker: Fix any fix-it for inverse constraints 2024-06-04 02:13:08 +03:00
Anthony Latsis
89f6d2705e ExistentialTypeSyntaxChecker: Fix any fix-it for compositions 2024-06-04 02:13:08 +03:00
Anthony Latsis
8955625c3b ExistentialTypeSyntaxChecker: Fix any fix-it for higher-order metatypes 2024-06-04 02:13:08 +03:00
Anthony Latsis
b90b564f61 ExistentialTypeSyntaxChecker: Look through inverses 2024-06-04 02:13:08 +03:00
Anthony Latsis
44e5286d55 ExistentialTypeSyntaxChecker: Look through any and some 2024-06-04 02:13:08 +03:00
Anthony Latsis
1b6efc12ea [NFC] Reorganize and extend any syntax enforcement tests 2024-06-04 02:13:04 +03:00
Kavon Farvardin
0420310623 NCGenerics: it's no longer "experimental"
resolves rdar://127701059
2024-05-08 10:49:12 -07:00
Rintaro Ishizaki
8c67d15a32 Merge pull request #72550 from fummicc1/diagnostics-init-msg-improvement
[AST] Improve diagnostic message about `constructor` expression
2024-03-28 15:51:46 -07:00
fummicc1
4dda7efc0f [AST] Improve diagnostics message for swift users.
Use `initializer` instead of `constructor` because it is more official word to express its meaning.
2024-03-27 12:32:25 +09:00
Pavel Yaskevich
25ce6604cd [CSDiagnostics] Add a diagnostic for superclass requirement failure in opaque return type 2024-03-26 12:07:14 -07:00
Pavel Yaskevich
08e93825d2 [CSDiagnostics] Add a diagnostic for same-type requirement failure in opaque return type
Follow-up to https://github.com/apple/swift/pull/72493
2024-03-26 11:11:30 -07:00
Pavel Yaskevich
e0da21b777 [Tests] NFC: Add a couple of tests for parametrized opaque result types 2024-03-21 11:38:52 -07:00
Kavon Farvardin
18f00668ce NCGenerics: fix ExistentialAny handling
The checking for ExistentialAny diagnostics needed to be updated to both
 handle the new Copyable/Escapable protocols that appear in the Any and
 AnyObject existential layouts.

resolves rdar://123332844
2024-02-27 14:15:34 -08:00
Slava Pestov
48f46f0e3c Sema: Redo the normalization in TypeResolver::resolveCompositionType() 2024-02-24 07:25:59 -05:00
Kavon Farvardin
f296d8e158 NCGenerics: mass XFAIL tests
It's easier to get a handle on regressions while working through
failures if the tests that are known to not pass are XFAIL'd for
NoncopyableGenerics.
2024-02-20 18:26:05 -05:00
Anthony Latsis
ef931342ad AST/ASTWalker: Refactor for recursive MemberTypeRepr representation 2024-02-09 17:33:21 +03:00
Slava Pestov
ea15d9f9b2 Stop passing -warn-redundant-requirements in tests 2024-02-02 14:57:19 -05:00
Slava Pestov
73c18fb668 Sema: Better distinguish scalar from variadic generic argument positions, and reject pack expansion types there
We used to disallow pack expansions in generic argument lists of
protocols, but allow them for all other nominal types. However,
we only actually checked that the pack expansions match up if
the type itself was variadic.

Fix this by repurposing Context::ProtocolGenericArgument into
Context::ScalarGenericArgument, and using that when the type does
not have a parameter pack.

Context::GenericArgument is now Context::VariadicGenericArgument,
and we only use it if the type has a parameter pack, in which case
we use the PackMatcher, so any pack expansions in the wrong place
are caught there.

Fixes rdar://116716014 and https://github.com/apple/swift/issues/69088.
2023-10-11 10:39:26 -04:00
Pavel Yaskevich
c5138a385a Merge pull request #68459 from mininny/error-on-inout-on-invalid-parameter
Fix inaccurate error on inout in wrong parameter type
2023-09-25 08:32:23 +01:00
Minhyuk Kim
c0eab4f551 Change error message to specify when inout usage is allowed 2023-09-24 23:18:27 +09:00
Kavon Farvardin
a69f9e434d experimental feature tests require asserts
When using an experimental feature that is not available
in production, a test needs to have `REQUIRES: asserts`
so that non-assert builds don't unexpectedly fail.
2023-09-23 19:52:45 -07:00
Minhyuk Kim
d9a03dda17 [Sema] Produce better diagnostics when using inout on parameter types that cannot be 'inout'. Resolves #68417 2023-09-23 22:15:13 +09:00
Slava Pestov
b6f8f81ec1 Sema: Don't allow PackExpansionType in the generic arguments of a ParameterizedProtocolType
We already had a separate TypeResolverContext::ProtocolGenericArgument that
did not allow PackExpansionTypes, but it was never used.

Fixes rdar://problem/115538574.
2023-09-19 23:03:59 -04:00