Commit Graph

1015 Commits

Author SHA1 Message Date
Allan Shortlidge
e90530990a Parse/Sema: Move some AvailabilitySpec diagnostics from Parse to Sema.
Eventually, querying the `AvailabilityDomain` associated with an
`AvailabilitySpec` will require invoking a request that takes a `DeclContext`.
This means that any diagnostics related to the domain identified by an
`AvailabilitySpec` need to be emitted during type-checking rather than parsing.
This change migrates several `AvailabilitySpec` diagnostics from Parse to Sema
to unblock further work.
2025-02-19 11:40:56 -08:00
Pavel Yaskevich
ff5e2ddf3c [MiscDiagnostics] Look through ABISafe conversions while determining whether access is mutating or not
Otherwise `inout` uses of declarations that require ABI safe conversions
are going to be diagnosed as non-mutating.
2025-02-13 16:04:21 -08:00
Rintaro Ishizaki
73164a0ffd Merge pull request #79353 from rintaro/astgen-magic-ident-literal
[ASTGen] Generate MagicIdentifierLiteralExpr
2025-02-13 06:25:09 -08:00
Rintaro Ishizaki
563ddc47e2 [AST] Eliminate 'SYNTAX_KIND' from MagicIdentifierKinds.def
Nothing is using it.
2025-02-12 23:19:34 -08:00
Allan Shortlidge
670084a9ac AST: Retire PlatformVersionConstraintAvailabilitySpec. 2025-02-12 22:46:58 -08:00
Allan Shortlidge
6daea78ccf AST: Remove AvailabilitySpec.h include from Stmt.h.
Include it where it's actually used instead to improve compile times.
2025-02-11 20:03:01 -08:00
Cal Stephens
14632b7c3e Update tests after pulling fix from master 2025-02-10 08:13:27 -08:00
Cal Stephens
31528da906 Further simplify conditions 2025-02-10 06:29:27 -08:00
Cal Stephens
041c68519c Simplify diagnostics 2025-02-06 08:38:13 -08:00
Cal Stephens
684c908564 Prevent nested closure in 'extension Optional' from unexpectedly being allowed 2025-02-04 18:49:42 -08:00
Cal Stephens
be0e68eca3 Add more test cases, simplify logic 2025-02-02 19:28:18 -08:00
Cal Stephens
f2cda5efe3 Merge tag 'swift-DEVELOPMENT-SNAPSHOT-2025-01-31-a' of github.com:swiftlang/swift into cal--fix-nested-weak-self-issue
Tag build swift-DEVELOPMENT-SNAPSHOT-2025-01-31-a
2025-02-02 14:18:15 -08:00
Anthony Latsis
a84dfc8387 [Gardening] Fix some set but not used variables 2025-01-30 21:34:38 +00:00
Cal Stephens
8557e234ee Merge branch 'main' into cal--fix-nested-weak-self-issue 2025-01-18 18:36:24 -08:00
Cal Stephens
e6a2230fdf Fix issue where implicit self was unexpectedly not allowed in nested weak self closure in Swift 6 mode 2025-01-18 18:25:23 -08:00
Hamish Knight
34f809055a [Sema] Downgrade implicit self diag to warning for macro args
We previously missed diagnosing this for macro
args, fixing it turned out to be a bit more source
breaking than initially thought though, so downgrade
to a warning until Swift 7.

rdar://141963700
2025-01-10 14:36:29 +00:00
Hamish Knight
fd4c86f8f5 [Sema] NFC: Move walker out of diagnoseImplicitSelfUseInClosure
I need to add a method with a template, and C++
doesn't support that in local classes.
2025-01-10 14:36:29 +00:00
Pavel Yaskevich
93a8675e49 Merge pull request #76946 from pwongxy/diag-warn-on-for-var-loop
[Diagnostics] Improve warning suggestion for `var` in for loop
2024-12-10 18:01:25 -08:00
Peter Wong
c1473e84f9 [Diagnostics] Improve warning suggestion for var in for loop
When iterator consists of tuple of variable and iteration only mutates
the tuple partially, improve the warning message from "changing to 'let"
to "changing the pattern to '(..., case let, ...)"
2024-12-10 22:51:08 +08: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
Hamish Knight
b2dcf1ef05 Merge pull request #77534 from hamishknight/i-take-issue-with-your-argument
[Sema] Consistently run MiscDiagnostics on macro arguments
2024-11-12 10:42:10 +00:00
Hamish Knight
36a9628b9e Merge pull request #77537 from hamishknight/complete-func
[Completion] Type-check parent closures for local functions
2024-11-12 10:40:53 +00:00
Hamish Knight
f8ca3975fe Merge pull request #77526 from hamishknight/remove-dead-func
[Sema] NFC: Remove `diagnoseMoveOnlyPatternMatchSubject`
2024-11-11 20:00:13 +00:00
Hamish Knight
7beceb0e4b [AST] Generalize getInnermostClosureForSelfCapture
Really this applies to any capture, not just
`self`. Also refactor to make it clear that
parent closures and functions are really the only
cases that matter here.
2024-11-11 19:34:21 +00:00
Hamish Knight
146e95c76a [Sema] Use BaseDiagnosticWalker consistently in MiscDiagnostics
None of these walkers ought to be walking into
non-PatternBinding decls, this avoids duplicate
diagnostics in nested local decls in closures.
2024-11-11 19:29:59 +00:00
Hamish Knight
fab09c50a0 Merge pull request #77479 from hamishknight/macro-misc-diag-fixes
[Sema] A couple of fixes for MiscDiagnostics on macro expansions
2024-11-11 19:27:58 +00:00
Allan Shortlidge
600ed07c88 Merge pull request #77519 from tshortli/fix-conditional-opaque-type-miscompile
Sema: Fix opaque type accessors with inactive availability conditions
2024-11-11 09:22:09 -08:00
Hamish Knight
26b827de29 [Sema] NFC: Remove diagnoseMoveOnlyPatternMatchSubject
This appears to be a dead function now.
2024-11-11 16:09:52 +00:00
Allan Shortlidge
b97e27279e Sema: Fix opaque type accessors with inactive availability conditions.
Opaque type metadata accessor functions could be miscompiled for functions that
contain `if #available` checks for inactive platforms. For example, this
function will always return `A` when compiled for macOS, but the opaque type
accessor would instead return the type metadata for `B`:

```
func f() -> some P {
  if #available(iOS 99, *) {
    return A() // Returns an A on macOS
  } else {
    return B()
  }
}
```

Resolves rdar://139487970.
2024-11-10 09:23:39 -08:00
Allan Shortlidge
0d581c4261 NFC: Use VersionRange::all() to represent "always available".
It doesn't make sense to use `VersionRange::empty()` to represent "universally
available" since something that is available in an empty version range is
effectively never available.
2024-11-09 19:36:03 -08:00
Hamish Knight
6352b01635 [Sema] Avoid double-diagnosing in macro expansions
Avoid walking into macro expansions for
MiscDiagnostics, the expansions will instead be
visited when they're type-checked on their own.
2024-11-09 00:24:52 +00:00
Nate Chandler
da71271d8f [CoroutineAccessors] Synthesize default req impls.
When a protocol which has a read (or modify) requirement is built with
the CoroutineAccessors feature, it gains a read2 (or modify2,
respectively) requirement.  For this to be compatible with binaries
built without the feature, a default implementation for these new
requirements must be provided.  Cause these new accessor requirements to
have default implementations by returning `true` from
`doesAccessorHaveBody` when the context is a `ProtocolDecl` and the
relevant availability check passes.
2024-11-07 16:47:09 -08:00
Rintaro Ishizaki
d4db99ce9d [Parse] Remove unnecessary dependencies to Parser.h
C++ swift::Parser is going to be replaced with SwiftParser+ASTGen.
Direct dependencies to it should be removed. Before that, remove
unnecessary '#include "swift/Parse/Parser.h"' to clarify what actually
depends on 'swift::Parser'.

Split 'swift::parseDeclName()' et al. into the dedicated files.
2024-11-02 01:23:59 -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
Nate Chandler
091368ba21 [CoroutineAccessors] Added read.
The name is a placeholder for the mutating single-yield coroutine
accessor.
2024-09-26 18:10:39 -07:00
Nate Chandler
98a2e6a7df [CoroutineAccessors] Added modify.
The name is a placeholder for the mutating single-yield coroutine
accessor.
2024-09-26 18:10:39 -07:00
Kavon Farvardin
df73eecdeb Sema: generalize findSyntacticErrorForConsume
Since this function is being called from the constraint solver now, we
need to generalize the way it obtains the Type of an Expression, as the
expression itself may not know its own type, only the solver does.

resolves rdar://134371893 / https://github.com/swiftlang/swift/issues/75999
2024-09-24 13:59:29 -07:00
Hamish Knight
e8ef77d307 [Sema] NFC: Elaborate on a comment 2024-09-17 12:35:11 +01:00
Allan Shortlidge
c868378d96 ConstraintSystem: Use scoring to implement MemberImportVisibility.
Previously, the constraint solver would first attempt member lookup that
excluded members from transitively imported modules. If there were no viable
candidates, it would perform a second lookup that included the previously
excluded members, treating any candidates as unviable. This meant that if the
member reference did resolve to one of the unviable candidates the resulting
AST would be broken, which could cause unwanted knock-on diagnostics.

Now, members from transitively imported modules are always returned in the set
of viable candidates. However, scoring will always prioritize candidates from
directly imported modules over members from transitive imports. This solves the
ambiguities that `MemberImportVisibility` is designed to prevent. If the only
viable candidates are from transitively imported modules, though, then the
reference will be resolved successfully and diagnosed later in
`MiscDiagnostics.cpp`. The resulting AST will not contain any errors, which
ensures that necessary access levels can be computed correctly for the imports
suggested by `MemberImportVisibility` fix-its.

Resolves rdar://126637855.
2024-09-10 09:47:42 -07:00
Doug Gregor
42809af51e Merge pull request #76327 from DougGregor/suppress-warnings-in-inactive-regions-without-ifconfigdecl
Suppress certain warnings in inactive `#if` regions without relying on `IfConfigDecl`
2024-09-07 10:16:20 -07:00
Doug Gregor
4a4c0f68f1 Teach the "unused variable / value" diagnostics to use SwiftIfConfig
Rather than walking into the inactive regions of IfConfigDecls looking for
references to a declaration before we diagnose it, go to the syntax
tree and look through inactive *and unparsed* regions for identifier
tokens that match. If we find one, suppress the diagnostic.

This reduces our dependency on IfConfigDecl in the AST, and also makes
the same suppression work with code in unparsed regions that had no
representation in IfConfigDecl.
2024-09-06 22:14:52 -07:00
Hamish Knight
1caaeeb83e Merge pull request #76295 from hamishknight/capturent
Remove `shouldWalkCaptureInitializerExpressions`
2024-09-06 21:39:51 +01:00
Alejandro Alonso
45d7ea39a5 Merge pull request #75518 from Azoy/integer-generics
Implement Value generics
2024-09-05 15:33:46 -07:00
Hamish Knight
f5557c4c10 Remove shouldWalkCaptureInitializerExpressions
There doesn't seem to be any reason we can't
walk the PatternBindingDecls directly here.
2024-09-05 22:27:15 +01:00
Slava Pestov
1ff1b9479a AST: Pick off some usages of GenericTypeParamType::getDecl() 2024-09-04 15:13:46 -07:00
Alejandro Alonso
75c2cbf593 Implement value generics
Some requirement machine work

Rename requirement to Value

Rename more things to Value

Fix integer checking for requirement

some docs and parser changes

Minor fixes
2024-09-04 15:13:25 -07:00
Hamish Knight
9bd0d9b7e4 [Sema] Walk separately-checked closures normally in MiscDiagnostics
We no longer need to make any special affordances
for separately-checked closures, walk them normally.
2024-08-31 12:47:46 +01:00
Hamish Knight
cbeb4bfbc8 [Sema] Walk patterns for syntactic diagnostics
Use `SyntacticElementTarget::walk`, ensuring we
walk to any ExprPatterns.
2024-08-31 12:47:46 +01:00
Hamish Knight
89a3390aa1 [Sema] Move SingleValueStmtUsageChecker into pre-checking
These diagnostics are better suited for pre-checking
since we ought to be emitting them even if there's
some other error with the expression.
2024-08-30 18:55:48 +01:00
Slava Pestov
ae77d6f0c1 AST: Replace one-off predicates with SubstitutionMap::getRecursiveProperties() 2024-08-21 13:19:10 -04:00