Commit Graph

1015 Commits

Author SHA1 Message Date
Hamish Knight
84847bcd06 [Sema] Relax a check in VarDeclUsageChecker
We don't want to just check the first pattern, we're interested in
the first pattern that binds the given variable. That can be determined
by checking if it's canonical or not.
2025-09-09 13:48:40 +01:00
Hamish Knight
84befd43ab [AST] Make case body variables for CaseStmt non-optional
We don't really care about the distinction between empty and nil here.
2025-09-09 13:48:40 +01: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
Anthony Latsis
380736292e Sema: Fix -Wsometimes-uninitialized warning 2025-08-31 11:18:27 +01:00
Slava Pestov
a5a8cf4a36 Merge pull request #83858 from slavapestov/covariant-result-type-part-2
AST: Continue process of removing replaceCovariantResultType()
2025-08-28 10:07:13 -04:00
Slava Pestov
1716832a7a AST: Remove most usages of AbstractFunctionDecl::hasDynamicSelfResult() 2025-08-27 15:34:26 -04: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
Allan Shortlidge
9de88624b2 Sema: Diagnose availability of availability domains in if #available queries.
When emitting statement diagnostics for `if #available` queries, diagnose the
availability of the decls representing the referenced availability domains.
Among other things, this checks that the domains are sufficiently visible to be
used in the containing function body context.
2025-08-26 08:20:12 -07:00
fahadnayyar
720406fdd6 Diagnose unannotated C++ APIs returning SWIFT_SHARED_REFERENCE at Swift call sites (#83025)
This patch improves the warning for C++ APIs returning
`SWIFT_SHARED_REFERENCE` types but not annotated with either
`SWIFT_RETURNS_RETAINED` or `SWIFT_RETURNS_UNRETAINED` in the following
ways:

1. The warning for missing `SWIFT_RETURNS_(UN)RETAINED` annotations is
now emitted on Swift use sites, rather than while importing the API
(func/method decls).
- This logic is now implemented as a Misl Diagnostic in function
`diagnoseCxxFunctionCalls` in file lib/Sema/MiscDiagnostics.cpp.
- The warning is now triggered only when the API is actually used, which
reduces noise in large C++ headers.
- These warnings are still gated behind experimental-feature-flag `WarnUnannotatedReturnOfCxxFrt`

rdar://150800115
2025-08-25 14:44:25 -07:00
Slava Pestov
4146a7f935 Merge pull request #83736 from slavapestov/fix-rdar157329046
Sema: New opaque return type circularity check that doesn't trigger lazy type checking of bodies
2025-08-14 23:46:01 -04:00
Mykola (Nickolas) Pokhylets
31470bbd1e Merge pull request #82732 from nickolas-pohilets/mpokhylets/weak-let-feature
Wrap SE-0481 into an upcoming feature until source incompatibilities are resolved
2025-08-14 23:59:28 +02:00
Slava Pestov
e26034b7ac Sema: New opaque return type circularity check that doesn't trigger lazy type checking of bodies
Commit b70f8a82b1 introduced a usage of
ReplaceOpaqueTypesWithUnderlyingTypes in Sema. Previously this was only
called from SILGen.

The problem was that ReplaceOpaqueTypesWithUnderlyingTypes would call
getUniqueUnderlyingTypeSubstitutions(), which triggers a request to
type check the body of the referenced function.

While this didn't result in unnecessary type checking work, because
UniqueUnderlyingTypeSubstitutionsRequest::evaluate() would skip bodies
in secondary files, it did change declaration checking order.

The specific issue we saw was a bad interaction with associated type
inference and unqualified lookup in a WMO build, and a complete test
case is hard to reduce here.

However, no behavior change is intended with this change, modulo bugs
elsewhere related to declaration checking order, so I feel OK not adding
a test case.

I'll hopefully address the unqualified lookup issue exposed in the
radar soon; it has a reproducer independent of opaque return types.

Fixes rdar://157329046.
2025-08-14 17:01:47 -04:00
Allan Shortlidge
db117bfefe Sema: Don't apply access level fix-it to @dynamicMemberLookup subscript.
The adjusted access level for the subscript shouldn't be fixed automatically
since sometimes the diagnostic is a warning and other times automatically
applying the fix-it immediately causes other errors, which can be confusing.

Resolves rdar://158261884.
2025-08-13 23:13:07 -07:00
Allan Shortlidge
b4fb7c81c0 AST: Refactor AvailabilityQuery constructors.
Get rid of the boolean arguments for unavailability in AvailabilityQuery's
constructors and introduce a `asUnavailable()` modifier that can be used
instead in the contexts where unavailability is relevant.
2025-08-06 18:01:44 -07:00
Allan Shortlidge
78f50ba741 AST: Refactor ConditionallyAvailableSubstitutions to use AvailabilityQuery.
Conditionally available opaque return types should support availability
conditions that are evaluated in any availability domain. Update
`ConditionallyAvailableSubstitutions` to model its conditions with
`AvailabilityQuery` instead of assuming that conditions are always a single
version query for the current platform.
2025-08-06 18:01:10 -07:00
Mykola Pokhylets
4e233706b9 Updated code comment to avoid giving false promises about adoption strategy of the feature 2025-08-02 20:24:46 +02:00
Mykola Pokhylets
86100fe685 Created separated warning group for warnings about unnecessary mutable weak variables 2025-08-02 20:24:46 +02:00
Mykola Pokhylets
89f8f8b9bf Wrap only changes related to the closure frontend logic 2025-08-02 20:24:46 +02:00
Mykola Pokhylets
ae48446716 Wrap SE-0481 into an upcoming feature until source incompatibilities are resolved 2025-08-02 20:24:21 +02:00
Anthony Latsis
fec049e5e4 Address llvm::PointerUnion::{is,get} deprecations
These were deprecated in
https://github.com/llvm/llvm-project/pull/122623.
2025-07-29 18:37:48 +01:00
Slava Pestov
b70f8a82b1 Sema: Improve the infinite opaque return type check
Now look through other opaque return types that appear in the
underlying type. This catches various forms of recursion that
otherwise would cause a SILGen or SILOptimizer crash.

- Fixes rdar://82992151.
2025-07-18 20:03:03 -04:00
Anthony Latsis
d7ec3a34bb Merge pull request #83094 from swiftlang/jepa-main
Sema: Make `@concurrent` not imply `async` on closures
2025-07-17 01:54:04 +01:00
Anthony Latsis
58d5059617 Sema: Make @concurrent not imply async on closures
The present approach is not prudent because `@concurrent` synchronous
functions, a natural extension, are a likely-to-happen future direction,
whereas the current inference rule is entirely grounded on `@concurrent`
being exclusive to async functions.

If we were to ship this rule, we would have to keep the promise for
backwards compatibility when implementing the aforementioned future
direction, replacing one inconsistency with another, and possibly
introducing new bug-prone expression checking code.

```swift
func foo(_: () -> Void) {}
func foo(_: () async -> Void) {}

// In a future without this change and  `@concurrent` synchronous
// functions accepted, the first call resolves to the first overload,
// and the second call resolves to the second, despite `@concurrent` no
// longer implying `async`.
foo { }
foo { @concurrent in }
```

This change also drops the fix-it for removing `@concurrent` when used
on a synchronous closure. With the inference rule gone, and the
diagnosis delayed until after solution application, this error raises a
fairly balanced choice between removing the attribute and being
explicit about the effect, where a unilateral suggestion is quite
possibly more harmful than useful.
2025-07-16 20:46:29 +01:00
Allan Shortlidge
e03cf967c3 Sema: Diagnose invalid OS versions in if #available queries.
Part of rdar://155558161.
2025-07-14 16:03:52 -07:00
Allan Shortlidge
59d74fa1c7 Sema: Diagnose opaque types that depend on availability of a custom domain.
Serialization and IRGen don't yet support opaque return types that would depend
on querying availability of a custom domain so we need to reject this code to
avoid mis-compiling it.
2025-07-02 11:23:42 -07:00
Allan Shortlidge
4a76c04cf5 SILGen: Fix if #unavailable mis-compile for zippered libraries.
Inverted availability queries were mis-compiled for zippered libraries because
the code that emits calls to `isOSVersionAtLeastOrVariantVersionAtLeast()` was
not updated when the `if #unavailable` syntax was introduced (at that time
support for zippered libraries had not yet been upstreamed). The result of
these calls is now inverted when appropriate.

To make it easier to manage the growing complexity of supporting availability
queries, Sema now models the relevant information about an availability query
with the new `AvailabilityQuery` type. It encapsulates the domain for the
query, the result if it is known at compile time, and the version tuple
arguments to pass to a runtime invocation if applicable.

Resolves rdar://147929876.
2025-07-02 11:23:42 -07:00
Allan Shortlidge
b493b56e7e Sema: Remove OpaqueUnderlyingTypeChecker::AvailabilityContext.
`AvailabilityContext` is now a fundamental type used throughout the compiler,
so it's confusing for `OpaqueUnderlyingTypeChecker` to repurpose this name to
represent an `IfStmt`.

NFC.
2025-07-02 11:23:42 -07:00
Slava Pestov
ef5e2861a2 Sema: Fix crash on invalid code in diagnoseDictionaryLiteralDuplicateKeyEntries() 2025-06-17 09:52:03 -04:00
Anthony Latsis
f2e1420a90 Sema: Never record argument label mismatches for unlabeled trailing closures
Fixes a crash on invalid. The previous logic was causing a label
mismatch constraint fix to be recorded for an unlabeled trailing closure
argument matching a variadic paramater after a late recovery argument
claim in `matchCallArgumentsImpl`, because the recovery claiming skips
arguments matching defaulted parameters, but not variadic ones. We may
want to reconsider that last part, but currently it regresses the
quality of some diagnostics, and this is a targeted fix.

The previous behavior is fine because the diagnosis routine associate
with the constraint fix (`diagnoseArgumentLabelError`) skips unlabeled
trailing closures when tallying labeling issues — *unless* there are no
other issues and the tally is zero, which we assert it is not.

Fixes rdar://152313388.
2025-06-05 18:01:20 +01:00
Mykola (Nickolas) Pokhylets
87e536ebec Merge pull request #80440 from nickolas-pohilets/mpokhylets/weak-let 2025-06-05 09:59:33 +02: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
Mykola Pokhylets
847a7d247b Update comments 2025-05-28 21:25:37 +02:00
Mykola Pokhylets
de3a87c0ab Merge branch 'main' into mpokhylets/weak-let 2025-05-23 17:26:49 +02:00
Artem Chikin
d8176a7e89 [Compile Time Values] Add syntactic verification of valid expressions in '@const' contexts
Syntactically verify that initializer expressions of '@const' variables and argument expressions to '@const' parameters consist strictly of syntactically-verifiable set of basic values and operations
2025-05-20 09:38:36 -07:00
Mykola Pokhylets
b138c2cc35 Updated logic for diagnosting implicit self capture to handle immutable weak capture (minor source-breaking change) 2025-05-19 16:31:01 +02:00
Mykola Pokhylets
3e0de8672a Removed the feature and made changes unconditional 2025-05-15 10:52:09 +02:00
Mykola Pokhylets
81da20f63c Enable warnings about never mutated weak var. 2025-05-15 10:26:27 +02:00
Nate Cook
e68069f891 [stdlib] Allow a default for optional interpolations (#80547)
This adds an `appendInterpolation` overload to
`DefaultStringInterpolation` that includes a parameter for providing a
default string when the value to interpolate is `nil`. This allows this
kind of usage:

```swift
let age: Int? = nil
print("Your age is \(age, default: "timeless")")
// Prints "Your age is timeless"
```

The change includes an additional fixit when optional values are
interpolated, with a suggestion to use this `default:` parameter.
2025-05-07 12:47:02 -05:00
Hamish Knight
3c302349ee Merge pull request #80853 from hamishknight/macro-async-warning 2025-04-18 16:22:28 +01:00
Hamish Knight
14bd41159e NFC: Abstract away the use of '7' to represent the next language mode
Introduce `Version::getFutureMajorLanguageVersion` to make it easier
to find clients that will need to be updated once we have a new
language mode.
2025-04-16 19:22:52 +01:00
Nate Chandler
347689fb30 [NFC] AST: Extract helper method.
Several callers of `AbstractStorageDecl::getAccessStrategy` only cared
about whether the the access would be via physical storage.  Before
adding more arguments to `getAccessStrategy` for which such callers
would have to pass a sentinel value, add a convenience method for this.
2025-04-15 20:45:37 -07:00
Anthony Latsis
a0930dec31 AST: Cut down on DescriptiveDeclKind usage in DiagnosticsSema.def 2025-04-05 12:31:19 +01:00
Allan Shortlidge
cce02961f9 AST/SILGen: Make availability ranges stored by PoundAvailableInfo optional.
Rather than representing a missing availability range on `PoundAvailableInfo`
with a default-constructed `AvailabilityRange` (empty), store the ranges as
optionals instead. This allows an empty range to represent an availability
condition which is known to be false at compile time, which will be necessary
when generating SIL for `if #available` queries that check custom availability
domains.
2025-04-01 07:46:46 -07:00
Amritpan Kaur
970159c09d [CSDiagnostics] Block async, throws and mutating methods. 2025-03-19 08:56:03 -07:00
Amritpan Kaur
8f71f5c780 [NFC] Rename property to member to generalize
for both properties and method members.
2025-03-19 08:53:18 -07:00
Allan Shortlidge
cad1ee75d5 AST: Optimize the layout of AvailabilityRange.
`AvailabilityRange` is now being used as a currency type in more of the
compiler, and some of those uses are in permanent `ASTContext` allocations. The
class wraps the `VersionRange` utility, which is itself a wrapper around
`llvm::VersionTuple` with some additional storage for representing sentinel
values. Even though the two sentinel values can be be represented with just a
single bit of additional storage on top of the 16 bytes required to represent
`VersionTuple`, because of alignment requirements the sentinel values end up
bloating the layout of `VersionRange` by many bytes.

To make `AvailabilityRange` and `VersionRange` more efficient to store, we can
instead reserve two unlikely `llvm::VersionTuple` bit patterns as the sentinel
values instead. The values chosen are the same ones LLVM uses to represent
version tuple tombstones and empty keys in a `DenseMap`.
2025-03-05 07:15:15 -08:00
Allan Shortlidge
272b2b43de AST: Accept AvailabilityDomains as diagnostic arguments.
This simplifies the code to emit availabilty diagnostics and ensures that they
display domain names consistently. While updating existing diagnostics, improve
consistency along other dimensions as well.
2025-02-28 09:18:38 -08:00
Allan Shortlidge
aaa0e0a7b3 AST/Parse: Parse custom availability domain specs in if #available(...).
Delay resolution of availability domain identifiers parsed in availability
specifications until type-checking. This allows custom domain specifications to
be written in `if #available` queries.
2025-02-27 23:07:55 -08:00
Allan Shortlidge
db29a1c6f4 Parse/Sema: Move #available query wildcard diagnostics to Sema.
In order to unblock resolution of availability domains during type-checking
instead of parsing, diagnostics about missing or superfluous wildcards in
availability specification lists need to move to Sema.
2025-02-23 22:09:33 -08:00
Allan Shortlidge
eb6506a1ad AST: Introduce SemanticAvailabilitySpec.
It wraps an type-checked `AvailabilitySpec`, which guarantees that the spec has
a valid `AvailabilityDomain` associated with it. This will unblock moving
AvailabilitySpec domain resolution from parsing to sema.
2025-02-23 10:53:06 -08:00