Commit Graph

28234 Commits

Author SHA1 Message Date
Michael Gottesman
fb3c710212 [concurrency] Fix an off by one error in generic handling in local capture checking code.
Otherwise, we break memory safety by accessing a generic parameter that does not
exist resulting in undefined behavior.

rdar://150351351
2025-04-30 06:17:52 -07:00
Slava Pestov
6b4710ed22 Merge pull request #81142 from slavapestov/more-type-subst-cleanup
Clean up duplicated opened existential archetype handling in SIL and more
2025-04-30 02:42:57 -04:00
Slava Pestov
7b5b5e9602 Merge pull request #81171 from slavapestov/fix-issue-81036
AST: Use weighted reduction order for opaque return types
2025-04-30 01:53:29 -04:00
Slava Pestov
3c26321aa3 Merge pull request #81173 from slavapestov/unused-vars
Squash warnings
2025-04-30 01:48:33 -04:00
Slava Pestov
d2b0bf002a AST: Fix existential erasure of long member types
Suppose protocol P has a primary associated type A, and we have
a `any P<S>` value. We form the generalization signature <T>
with substitution map {T := S}, and the existential signature
<T, Self where T == Self.A>.

Now, if we call a protocol requirement that takes Self.A.A.A,
we see this is fixed concrete type, because the reduced type of
Self.A.A.A is T.A.A in the existential signature.

However, this type parameter is not formed from the
conformance requirements of the generalization signature
(there aren't any), so we cannot directly apply the outer
substitution map.

Instead, change the outer substitution conformance lookup
callback to check if the reduced type parameter is valid
in the generalization signature, and not just rooted in a
generic parameter of the generalization signature.

If it isn't, fall back to global conformance lookup.

A better fix would introduce new requirements into the
generalization signature to handle this, or store them
separately in the generic environment itself. But this is fine
for now.

- Fixes https://github.com/swiftlang/swift/issues/79763.
- Fixes rdar://problem/146111083.
2025-04-29 18:58:58 -04:00
Alejandro Alonso
e68b398d41 Rename isArrayType and split the InlineArray portion 2025-04-29 15:57:10 -07:00
Slava Pestov
fa6ec5a4a8 Sema: Fix unused variable warning 2025-04-29 13:57:18 -04:00
Slava Pestov
8ab5ca2a7a Sema: Use weighted reduction order for opaque return types
If the opaque generic signature has a same-type requirement between
an outer type parameter and an opaque type parameter, the former
should always precede the latter in the type parameter order, even
if it is longer. Achieve this by giving the innermost generic
parameters a non-zero weight in the opaque generic signature.

Now, when we map a type parameter into an opaque generic environment,
we correctly decide if it is represented by a type parameter of the
outer generic signature, in which case we apply the outer substitution
map instead of instantiating an archetype.

The included test case demonstrates the issue; we declare an opaque
return type `some P<T.A.A>`, so the opaque generic signature has a
requirement `T.A.A == <<some P>>.A`. Previously, the reduced type of
`<<some P>>.A` was `<<some P>>.A`, so it remained opaque; now we
apply outer substitutions to `T.A.A`.

- Fixes https://github.com/swiftlang/swift/issues/81036.
- Fixes rdar://problem/149871931.
2025-04-29 13:55:31 -04:00
Slava Pestov
115ba5c54f AST: Factor out GenericTypeParamType::withDepth() 2025-04-29 13:55:29 -04:00
Pavel Yaskevich
085078dd8a [Feature] Rename Feature APIs from adoption to migration 2025-04-28 11:52:46 -07:00
Slava Pestov
cf1572c65b AST: Add ASTContext::TheSelfType for convenience 2025-04-28 11:49:50 -04:00
Pavel Yaskevich
22a3ec0ee3 Merge pull request #78601 from stzn/fix-sending-typecheck
[Sema]Skip Sendable conformance check when `sending` are added to parameters or return types of an actor-isolated function
2025-04-26 18:55:56 -07:00
Doug Gregor
771cb35c23 Merge pull request #81120 from DougGregor/unsafe-call-effects
[Strict memory safety] Provide argument-specific diagnostics for calls
2025-04-26 07:41:38 -07:00
Doug Gregor
1b94c3b3d6 Always emit "unsafe does not cover any unsafe constructs" warning
Check for unsafe constructs in all modes, so that we can emit the
"unsafe does not cover any unsafe constructs" warning consistently.
One does not need to write "unsafe" outside of strict memory safety
mode, but if you do... it needs to cover unsafe behavior.
2025-04-25 23:22:09 -07:00
Doug Gregor
60f7d20ed2 [Effects checking] Handle optional function types for dynamic @objc calls
We've been missing this case forever, but it hadn't come up until
we performed "unsafe" checking on calls.
2025-04-25 21:54:39 -07:00
Doug Gregor
81b68e567b [Strict memory safety] Temporarily disable warning about stray "unsafe" in _SwiftifyImport expansions
We need to improve on the macro so it doesn't generate unnecessary
"unsafe" expressions.
2025-04-25 21:54:29 -07:00
Doug Gregor
ee9487b86f [Strict memory safety] Provide argument-specific diagnostics for calls
Similar to what we do for 'throws' checking, perform argument-specific
checking for unsafe call arguments. This provides more detailed failures:

```
example.swift:18:3: warning: expression uses unsafe constructs but is not
marked with 'unsafe' [#StrictMemorySafety]
16 |   x.f(a: 0, b: 17, c: nil)
17 |
18 |   x.f(a: 0, b: 17, c: &i)
   |   |                   `- note: argument 'c' in call to instance
method 'f' has unsafe type 'UnsafePointer<Int>?'
   |   `- warning: expression uses unsafe constructs but is not marked
with 'unsafe' [#StrictMemorySafety]
19 |   unsafeF()
20 | }
```

It also means that we won't complain for `nil` or `Optional.none`
arguments passed to unsafe types, which eliminates some false
positives, and won't complain about unsafe result types when there is
a call---because we'd still get complaints later about the
actually-unsafe bit, which is using those results.

Fixes rdar://149629670.
2025-04-25 21:54:19 -07:00
Doug Gregor
0b264e2d55 [Effects] Keep track of whether we looked through a 'self' application
When we look through a "self" application while decomposing a function
call in effects checking, keep track of whether we did so. Use this to
ensure that we get the right parameter types when looking at the
"self" application vs. the outer call.

This has always been wrong, but the async/throws effects don't ever
matter for the innermost application (i.e., for `x.m`), so the
inconsistency didn't come up.
2025-04-25 16:02:34 -07:00
Allan Shortlidge
039974ad17 AST: Fix a typo in missingImportsForDefiningModule(). 2025-04-25 08:05:32 -07:00
Anthony Latsis
925b72eaec Merge pull request #76337 from DePasqualeOrg/grammar-compound-modifiers
Grammatical corrections for compound modifiers
2025-04-25 13:50:52 +01:00
Slava Pestov
27d96fc1c9 Merge pull request #80963 from slavapestov/fix-rdar149438520
Sema: Fix case where witness thrown error type is a subtype of a type parameter
2025-04-24 16:55:41 -04:00
Hamish Knight
1af98f7d26 Merge pull request #81025 from hamishknight/add-null-check
[Sema] Add missing null check for Type
2025-04-24 20:36:17 +01:00
Slava Pestov
2d8da4b49d 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:28:56 -04:00
Anthony
c9b17383c8 Grammatical corrections for compound modifiers 2025-04-24 09:21:32 +02:00
Pavel Yaskevich
b484e9645d [Sema] ConstnessChecker: Look through function conversions while checking arguments
In strict concurrency mode some calls could reference a declaration that
is  wrapped in one or more function conversion expressions to apply
concurrency related attributes or erase them (such as `@Sendable` or
`@MainActor`). This shouldn't impact constness checking and the checker
should look through such conversions.

Resolves: rdar://148168219
2025-04-23 14:11:39 -07:00
Hamish Knight
54e0607091 [Sema] Add missing null check for Type
`getType` here can return a null type if the queried expression isn't
part of the solution, which can currently happen for code completion.
I'm working on a more principled fix for this, but until then this is
a low-risk fix that will unblock the stress tester and can be
cherry-picked to 6.2.

rdar://149759542
2025-04-23 12:58:28 +01:00
Allan Shortlidge
fe79c409ba Merge pull request #80972 from tshortli/member-import-visibility-cross-import-overlay
Sema: Improve MemberImportVisibility diagnostics for cross import overlays
2025-04-22 18:29:33 -07:00
stzn
55e8d5579f Change how to check null 2025-04-23 09:19:01 +09:00
stzn
c0e82eca51 Prevent casting null pointer 2025-04-22 20:55:50 +09:00
Allan Shortlidge
9e67cf00cf Sema: Improve MemberImportVisibility diagnostics for cross import overlays.
When `MemberImportVisibility` is enabled and a declaration from a cross import
overlay is diagnosed because it has not been imported, suggest imports of the
declaring and bystanding modules instead of the cross import overlay module
(which is an implementation detail).

Resolves rdar://149307959.
2025-04-21 16:51:39 -07:00
Pavel Yaskevich
c110941c27 [Frontend] Rename AsyncCallerExecution upcoming feature to NonisolatedNonsendingBeDefault 2025-04-21 13:59:29 -07:00
Pavel Yaskevich
047d644ab3 Merge pull request #79717 from xedin/objc-handlers-are-execution-caller
[Sema/SILGen] Import ObjC async functions as `nonisolated(nonsending)` by …
2025-04-21 00:20:26 -07:00
Doug Gregor
54e4400e92 Merge pull request #80933 from DougGregor/safe-nested-in-unsafe-fixes
[Strict memory safety] Improve handling of safe types nested within unsafe ones
2025-04-20 02:31:48 -07:00
Doug Gregor
b7d41a55a5 [Strict memory safety] Treat the implicit call to a 'defer' function as implicitly 'unsafe'
This eliminates stray warnings.
2025-04-19 21:57:12 -07:00
Doug Gregor
53ca902267 [Strict memory safety] Type alias types are only unsafe if their underlying type is 2025-04-19 10:38:07 -07:00
Doug Gregor
8ec52c825c [Strict memory safety] Nested types are safe/unsafe independent of their enclosing type
When determining whether a nested type is safe, don't consider whether
its enclosing type is safe. They're independent.
2025-04-19 09:49:22 -07:00
Doug Gregor
0405f61207 [Strict memory safety] Only diagnose unsafe types in the canonical type
Typealiases involving unsafe types that resolve to safe types
should not be diagnosed.
2025-04-18 18:43:27 -07:00
Becca Royal-Gordon
8f75878455 Forbid @_borrowed in @abi
It has indirect effects on the accessors, so it shouldn’t matter, but we can defensively redirect the query to the API counterpart anyway.

This was the last `InferredInABIAttr` attribute, so we can now remove all of the infrastructure involved in supporting attribute inference.
2025-04-18 14:50:01 -07:00
Becca Royal-Gordon
239831403c Forbid lazy with @abi
It’s not clear how `@abi` would apply to the auxiliary decl used for `lazy`.
2025-04-18 14:50:01 -07:00
Becca Royal-Gordon
f01c0a7580 Diagnose CustomAttrs as needed in @abi
CustomAttr backs four different features, each of which requires a different behavior in `@abi`:

• Global actors: Permitted (and permitted to vary) since they can affect mangling
• Result builders: Forbidden inside an `@abi` since they have no ABI impact
• Property wrappers: Forbidden both inside an `@abi` and on a decl with an `@abi` since it’s not clear how we would apply `@abi` to the auxiliary decls
• Attached macros: Forbidden inside an `@abi` since an ABI-only decl has no body, accessors, members, peers, extensions, or (currently) conformances

Implement these behaviors (outside of `ABIDeclChecker` since they can’t be described there).

Macro-related tests are not included in this commit; they require matching swift-syntax changes which are being negotiated.
2025-04-18 14:47:04 -07:00
Becca Royal-Gordon
9d63cf84c7 Tweak redecl checking’s handling of generated code
Macro expansions are now treated like a part of the source file they belong to, for purposes of the “second declaration is the one that’s diagnosed” rule. This helps stabilize a behavior that was easy to perturb.
2025-04-18 14:44:24 -07:00
Becca Royal-Gordon
d81ffe854f Typecheck ABI-only VarDecls
The decl checker was effectively not being run on these because we weren’t typechecking the PBD and typechecking the VarDecl itself is basically a no-op.
2025-04-18 14:44:24 -07:00
nate-chandler
c832696226 Merge pull request #80889 from nate-chandler/rdar149352777
[CoroutineAccessors] Require underscored override.
2025-04-18 11:25:52 -07:00
Pavel Yaskevich
c25d2fab50 [Concurrency] Infer isolation of methods that override caller isolated @objc async handlers to be `@concurrent
This is behavior pre-SE-0461 which is the safest option instead
of inferring `nonisolated(nonsending)` and changing the calling
convention and foregoing Sendable checking.
2025-04-18 09:17:44 -07:00
Alexis Laferrière
e55c9bf2f8 Merge pull request #80900 from xymus/cdecl-reason-language
Sema: Simplify tracking the foreign language for @cdecl
2025-04-18 09:09:13 -07:00
Pavel Yaskevich
b3e48b40ae Merge pull request #80737 from xedin/rdar-148996589
[Concurrency] Downgrade non-Sendable type captures to warnings if clo…
2025-04-18 09:07:18 -07:00
Hamish Knight
3c302349ee Merge pull request #80853 from hamishknight/macro-async-warning 2025-04-18 16:22:28 +01:00
Pavel Yaskevich
07bad98f6d [Sema/SILGen] Import ObjC async functions as nonisolated(nonsending) by default
These functions already have special code generation that keeps them
in the caller's isolation context, so there is no behavior change here.

Resolves: rdar://145672343
2025-04-17 21:56:54 -07:00
Alexis Laferrière
384b0302c2 Sema: Simplify tracking the foreign language of an @objc decl
Apply review comments from #80744.
2025-04-17 15:33:46 -07:00
Alexis Laferrière
405a84e7d6 Merge pull request #80744 from xymus/cdecl-global-function-checking
Sema: Intro experimental @cdecl and basic C compatibility check
2025-04-17 15:31:30 -07:00