Commit Graph

28352 Commits

Author SHA1 Message Date
Doug Gregor
a84db8f87d Merge pull request #80198 from DougGregor/current-context-isolation-crash
Avoid crashing if a MacroExpansionExpr failed to produce an expression
2025-03-22 17:33:33 -07:00
Doug Gregor
083194923c Implement experimental feature InferIsolatedConformances
Introduce the experimental feature InferIsolatedConformances to align
with the upcoming feature proposed in SE-0470. This is a slight
generalization of the main-actor-specific inference that was already
in place for the default-main-actor mode from SE-0466. Note that, as
specified in SE-0470, InferIsolatedConformances is implied by the
default-main-actor mode.
2025-03-21 11:41:46 -07:00
Doug Gregor
2ce04930a4 Avoid crashing if a MacroExpansionExpr failed to produce an expression 2025-03-21 08:30:11 -07:00
Holly Borla
bac0a10ae2 [Macros] Update the name and argument list for the function body macro
that wraps a function or closure body in a new top-level task.
2025-03-21 06:21:45 -07:00
Holly Borla
be670da373 Merge pull request #79980 from hborla/closure-body-macro
[Macros] Implement support for function body macros on closures.
2025-03-21 06:19:36 -07:00
Doug Gregor
8fd02d7ccd Merge pull request #80190 from DougGregor/conformance-isolation-diagnostics
Rework diagnostics for conformance isolation failures
2025-03-21 02:54:53 -07: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
Gábor Horváth
ddd36a7cfc Merge pull request #79662 from swiftlang/gaborh/dynamic-self-frt
[cxx-interop] Interpret Self as a static shorthand for FRTs
2025-03-20 17:54:35 +00:00
Hamish Knight
77dd7e20a5 Merge pull request #80129 from hamishknight/rdar118642163 2025-03-20 10:02:56 +00:00
Doug Gregor
70bd52e796 Factor isolation-related diagnostics for conformances into a separate structure
Rather than emitting isolation-related diagnostics for conformances, such
as witnesses that have incompatible isolation or associated conformances
that are differently isolated, capture all of those diagnostics in a
single side table and diagnose them all together.

This refactoring doesn't change the way we actually diagnose the
issue. That comes next.
2025-03-19 21:23:16 -07:00
Amritpan Kaur
3c30d68d2e Merge pull request #78823 from amritpan/method-keypaths
[Sema/SILGen/IRGen] Implement method & initializer keypaths.
2025-03-19 18:59:17 -07:00
Anthony Latsis
487d3b4ae7 [NFC] Simplify some code using ValueDecl::getParameterList 2025-03-20 00:26:00 +00: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
Andrew Trick
5d2c829b6a LifetimeDependence: implement strict type checking
Rework the type checker to support completely checking lifetime dependence
requirements. Don't let anything through without the feature being enabled and
complete annotation or inference.

First, prevent lifetime dependencies from sneaking into source that does not
enable LifetimeDependence. This is essential for controlling the scope of the
feature.

Fixing this is disruptive because, ever since `~Escapable` was introduced we
have been declaring empty non-Escapable types without enabling
LifetimeDependence. Such as:

      struct Implicit_Init_Nonescapable : ~Escapable {}

Fixes: rdar://145979187 ([nonescapable] diagnose implicit non-Escapable
initializers as an error unless LifetimeDependence is enabled)

Various forms of unsupported 'inout' dependencies are now also caught by the
type checker.

Second, disable lifetime dependency inferrence except in unambiguous cases and
some implicitly generated cases.

Fixes: rdar://131176898 ([nonescapable] missing diagnostic for incorrectly inferred inherited dependence)

This is important to avoid source compatibility problems as inference rules
change. They will change as the proposal goes through review.

This fixes various latent missing dependency bugs.

Disable experimental lifetime dependence inference. Unambiguous lifetime
dependency candidates will still be inferred by default, without any frontend
options. Ambiguous candidates will, however, no longer be inferred unless
-Xfrontend -enable_experimental_lifetime_dependence_inference is enabled.

This all has to be done without breaking existing .swiftinterface files. So
backward compatibility logic is maintained.

Examples of inference rules that are no longer enabled by default:

1. do not infer a dependency on non-Escapable 'self' for methods with more than
zero parameters:

    extension NE: ~Escapable {
      /*@lifetime(self)*/ // ERROR: 'self' not inferred
      func method<Arg>(arg: Arg) -> NE { ... }
    }

2. Never infer a 'copy' dependency kind for explicit functions

    extension NE: ~Escapable {
      @lifetime(self) // ERROR: 'copy' not inferred
      func method() -> NE { ... }

      @lifetime(self) // ERROR: 'copy' not inferred
      var property : NE { /*@lifetime(self: newValue)*/ set { ... } }
    }
2025-03-19 11:59:04 -07:00
Anthony Latsis
3d3b1ca50a [NFC] AST: Turn getParameterList into a method on ValueDecl 2025-03-19 18:49:15 +00:00
Slava Pestov
dc41b21d88 Merge pull request #80117 from slavapestov/conformance-checker-debug
Sema: Add LLVM_DEBUG to WitnessChecker::findBestWitness()
2025-03-19 14:30:34 -04:00
Hamish Knight
d0e17494a6 [Sema] Add missing null check for getAttachedResultBuilder
I haven't been able to come up with a test case, but it seems like
it's possible for the attribute to be invalidated during the call
to `visitCustomAttr`, in which case `getAttachedResultBuilder` can
return `nullptr`.

rdar://118642163
2025-03-19 18:08:29 +00:00
Amritpan Kaur
98cd675eb9 Guard feature behind experimental flag. 2025-03-19 10:54:09 -07:00
Pavel Yaskevich
552106d536 Merge pull request #80081 from xedin/rdar-143161190
[CSSimplify] Open key path type before assignment to its record requirements
2025-03-19 09:50:11 -07:00
Amritpan Kaur
86d456e4eb [Sema] Block inout method arguments. 2025-03-19 08:56:03 -07:00
Amritpan Kaur
a96b780a28 [Sema] Diagnose method arg captures that are not Hashable/Equatable. 2025-03-19 08:56:03 -07:00
Amritpan Kaur
970159c09d [CSDiagnostics] Block async, throws and mutating methods. 2025-03-19 08:56:03 -07:00
Amritpan Kaur
7ff563eb0c [CSDiagnostics] Remove checks preventing method/initializer keypaths. 2025-03-19 08:56:03 -07:00
Amritpan Kaur
0e895c6a2b [CSApply] Solve keypath methods and initializers. 2025-03-19 08:56:03 -07:00
Amritpan Kaur
724e3f9910 [Constraint System] Evaluable hashability during constraint generation. 2025-03-19 08:56:03 -07:00
Amritpan Kaur
c059d1521d [ConstraintSystem] Evaluate capabilities for keypath type. 2025-03-19 08:56:03 -07:00
Amritpan Kaur
6af6374e39 [CSGen] Generate constraints for keypaths to methods. 2025-03-19 08:56:03 -07:00
Amritpan Kaur
0c614e09d1 [Expr/AST] Add unresolvedApply component to handle method arguments. 2025-03-19 08:56:03 -07:00
Amritpan Kaur
2583da94b2 [NFC] Generalize subscript index handling. 2025-03-19 08:53:18 -07:00
Amritpan Kaur
811d54901b [NFC] Rename unresolvedProperty to unresolvedMember
to generalize for both properties and method members.
2025-03-19 08:53:18 -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
Artem Chikin
88dec5199e [Dependency Scanning] Add support for placing explicitly-built SDK modules into a separate module cache
With '-sdk-module-cache-path', Swift textual interfaces found in the SDK will be built into a separate SDK-specific module cache.
Clang modules are not yet affected by this change, pending addition of the required API.
2025-03-19 09:17:04 -06:00
Slava Pestov
c598ec3f5c Sema: Add LLVM_DEBUG to WitnessChecker::findBestWitness() 2025-03-19 09:30:46 -04:00
Gábor Horváth
e5c5ec57d2 Update lib/Sema/TypeCheckType.cpp
Co-authored-by: Anthony Latsis <alatsis@apple.com>
2025-03-19 12:56:56 +00:00
Gabor Horvath
b632534528 [cxx-interop] Interpret Self as a static shorthand for FRTs
We do not have dynamic self metadata for foreign reference types.

rdar://145066864
2025-03-19 12:56:56 +00:00
Slava Pestov
6a3ead3a44 Merge pull request #80107 from slavapestov/for-abstract-conformance
Fix a few more callers of ProtocolConformanceRef::forAbstract() to pass in a subject type
2025-03-19 07:15:27 -04:00
Holly Borla
8ec70e2559 [Macros] Gate closure body macros behind an experimental feature flag. 2025-03-18 20:50:47 -07:00
Pavel Yaskevich
9cd3c8de86 [TypeCheckMacros] Never associate MacroExpansionExpr with a closure
The closures are type-checked after macros are expanded
which means that macro cannot reference any declarations
from inner or outer closures as its arguments.

For example:

`_: (Int) -> Void = { x in { @Macro(x) in ... }() }`

`x` is not going to be type-checked at macro expansion
time and cannot be referenced by `@Macro`.

Let's walk up declaration contexts until we find first
non-closure one. This means that we can support a local
declaration that is defined inside of a closure because
they are separately checked after outer ones are already
processed.
2025-03-18 20:50:47 -07:00
Pavel Yaskevich
42ccd182f6 [PreCheckTarget] Move closure body expansion into PreCheckTarget::walkToClosureExprPre
If closure body is expanded in expression context we need to do
some special handling to make sure that source ranges are not
completely broken.
2025-03-18 20:50:47 -07:00
Pavel Yaskevich
b7e4b4370e [Macros] A macro attached to a closure body covers a range after in and up to }
The macro is not allowed to write explicit type or `in` keyword.
2025-03-18 20:50:47 -07:00
Holly Borla
31d4f661fc [Macros] Support function body macros on closures. 2025-03-18 20:50:47 -07:00
Doug Gregor
a02633ad47 Merge pull request #80103 from DougGregor/strict-safety-more-minor-cleanups 2025-03-18 20:29:25 -07:00
Slava Pestov
f7b0b72766 Sema: Refactor TypeChecker::containsProtocol() a bit 2025-03-18 19:38:21 -04:00
Pavel Yaskevich
cfad698202 [CSDiagnostics] Tailor diagnostic for Escapable conformance mismatch in key path context 2025-03-18 14:38:40 -07:00
Doug Gregor
9570e1e3a7 [SE-0458] Add fix-it for removing unnecessary "unsafe" keywords 2025-03-18 13:57:16 -07:00
Pavel Yaskevich
6c2b12547b [CSDiagnostics] Tailor diagnostic for Copyable conformance mismatch in key path context 2025-03-18 13:46:56 -07:00
Doug Gregor
4742d2c5db [SE-0458] withoutActuallyEscaping is unsafe for @convention(block)
The implementation of `withoutActuallyEscaping` for `@convention(block)`
functions cannot verify at runtime that the function did not actually
escape. Diagnose this as unsafe code under strict memory safety checking.

Fixes rdar://139994149.
2025-03-18 13:26:47 -07:00
Pavel Yaskevich
9a8f8277f9 Merge pull request #80085 from xedin/fix-assignFixedType-use
[CSSimplify] NFC: `assignFixedType` no longer accepts a locator
2025-03-18 08:49:30 -07:00
Anthony Latsis
ad3a5adc6d Merge pull request #80008 from AnthonyLatsis/danaus-plexippus-4 2025-03-18 13:24:17 +00:00