Commit Graph

2365 Commits

Author SHA1 Message Date
Hamish Knight
1f79cbdca1 [CS] Sink closure attribute/param checking into applySolution
The cases where CSApply fails should be fairly
limited these days, and there doesn't seem to be
any reason we shouldn't run these on failure anyway.
2024-09-17 12:35:11 +01:00
Hamish Knight
930dc18eb7 [CS] Avoid delaying application for multi-statement closures
We ought to be able to apply the solution to them
immediately now.
2024-09-17 12:35:11 +01:00
Hamish Knight
f0151c1d21 [CS] Delay type-checking of local decls
Delay `typeCheckDecl` for local decls until the
end of CSApply. This replaces the existing logic
for delaying type-checking for local functions.
2024-09-17 12:35:10 +01:00
Hamish Knight
d99d881af5 [CS] NFC: Factor out base class SyntacticElementTargetRewriter
Pass this instead of a function for rewriting
targets.
2024-09-17 12:35:10 +01:00
Hamish Knight
002c6d169b [CS] Remove some calls to setExprTypes
`rewriteTarget` already calls `setExprTypes` for
expressions, so these are redundant.
2024-09-17 12:35:10 +01:00
Doug Gregor
05e8140c6d Provide macro module name in MacroExpansionExpr creation
This properly passes the module name through from attached macros to
the freestanding macro that are used under-the-hood for type checking.
2024-09-16 16:44:17 -07:00
Hamish Knight
b1c90feb52 [CS] Sink closure property wrapper application into applySolution
We need to make sure property wrappers are
applied before `checkParameterList`.
2024-09-14 15:37:21 +01:00
Hamish Knight
2d6a38b2cd [CS] Call checkParameterList for single-expr closures
Previously we would only call this in the delayed
application logic, which is currently run for
multi-statement closures. I'm planning on
removing that code path, which uncovered this
issue.
2024-09-14 15:37:21 +01:00
Hamish Knight
ee0e408a8c [Sema] Remove separate closure type-checking logic
`participatesInInference` is now always true for
a non-empty body, remove it along with the separate
type-checking logic such that empty bodies are
type-checked together with the context.
2024-09-08 16:17:11 +01:00
Alejandro Alonso
45d7ea39a5 Merge pull request #75518 from Azoy/integer-generics
Implement Value generics
2024-09-05 15:33:46 -07:00
Alejandro Alonso
0df42e9841 Lower UDRE to TypeValue if it references a value generic 2024-09-04 15:13:29 -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
Slava Pestov
851a829063 Sema: Consolidate logic for opening existentials in OpenedExistentials.cpp 2024-09-04 14:57:38 -04:00
Pavel Yaskevich
44d83aee8a [CSApply] Start marking closures that require dynamic isolation checking
If a closure is passed as an argument to a not-fully concurrency
checked callee - require dynamic isolation checking.

This information is going to be used by SILGen to inject expected
executor precondition when necessary.
2024-08-22 09:57:17 -07:00
Pavel Yaskevich
a4d9b3b5b2 [AST] Add a bit to closure expr to indiciate whether it requires dynamic isolation checking
This is an important information for closures because the compiler
might need to emit dynamic actor isolation checks in some circumstances
(i.e. when a closure is isolated and passed to a not fully concurrency
checked API).
2024-08-22 09:57:06 -07:00
Pavel Yaskevich
3df5e9c997 Merge pull request #75991 from xedin/rename-ispassedtosendingparameter
[AST] NFC: Rename `ParameterInfoList::isPassedToSendableParameter` to…
2024-08-21 09:14:00 -07:00
Pavel Yaskevich
f64bf5ff95 [AST] NFC: Rename ParameterInfoList::isPassedToSendableParameter to isSendableParameter
Just like other members of that type, `isPassedToSendableParameter` bit
set tracks information per parameter. The "passed" bit is applicable
to the argument rather than the parameter itself so it should bit kept
on the `AbstractClosureExpr`.
2024-08-20 13:17:04 -07:00
Slava Pestov
0c2f28fd3d AST: Remove GenericSignature parameter from OpenedArchetypeType::get() 2024-08-20 12:15:27 -04:00
Slava Pestov
d4d85572ce Sema: typeEraseOpenedArchetypesWithRoot() => typeEraseOpenedArchetypesFromEnvironment() 2024-08-19 16:55:10 -04:00
Slava Pestov
76ec591fde AST: hasOpenedExistentialWithRoot() => hasLocalArchetypeFromEnvironment() 2024-08-19 16:55:10 -04:00
Slava Pestov
375363a473 AST: Move global conformance lookup entry points to ConformanceLookup.h 2024-08-08 23:35:58 -04:00
Pavel Yaskevich
38aa71de91 [CSApply] Allow marker existential to superclass conversions
If existential is a protocol composition type where all of the
protocols are `@_marker`, narrowly allow coercion to its superclass
bound (if it matches). Both types have the same representation
which makes it okay.

This is only a problem in Swift 5 mode without strict concurrency
checks. In this mode `@preconcurrency` stripping happens
outside of the solver which means that no conversion restrictions
are recorded for members that got `& Sendable` stripped from
their types.

For example:

```swift
struct S {
  @preconcurrency static let member: KeyPath<String, Int> & Sendable
}

func test() {
  _ = S.member
}
```

Since `member` is `@preconcurrency` its type would get concurrency
annotations stripped, which includes `& Sendable` which means that
the solver uses `KeyPath<String, Int>` type for the reference and
not the original `KeyPath<String, Int> & Sendable`, this is a problem
for `ExprRewritter::adjustTypeForDeclReference` because conversion
between existential and its superclass bound requires a constraint
restriction which won't be available in this case.

Resolves: rdar://132700409
2024-08-01 12:41:02 -07:00
Kavon Farvardin
998986caca Noncopyable: fix implicit conversion error
Implicit initializers internally have "Default" parameter ownership
specifiers, which can only happen for a noncopyable type if the decl is
synthesized, since ownership is required to be specified if it were
written in the source.

There's no reason to warn for such decls, since the user can't see it
anyway. So specifically target an explicitly written "borrowing" as
being confusing, since that's the only valid case that could happen
anyway.

resolves rdar://131546153
2024-07-22 15:57:34 -07:00
Pavel Yaskevich
59ec789768 [CSApply] Allow @dynamicMemberLookup subscripts to take sendable key path
The changes to support `& Sendable` composition with key path
types adjusted `buildKeyPathDynamicMemberArgExpr` to support
that but the use-site erroneously still cast index type or
`BoundGenericType` as before.

Resolves: https://github.com/swiftlang/swift/issues/75244
Resolves: rdar://131768785
2024-07-16 00:20:38 -07:00
Slava Pestov
86d567f95a AST: ModuleDecl::lookupConformance() is a static method 2024-07-06 12:05:47 -04:00
Slava Pestov
3fcda140bb AST: ModuleDecl::checkConformance() is a static method 2024-07-06 12:05:46 -04:00
Slava Pestov
fae01d9776 AST: Remove ModuleDecl parameter from more places 2024-07-06 12:05:46 -04:00
Michael Gottesman
5e27999b09 [sending] Rename Sema level APIs from isSendingParameter -> isPassedToSendingParameter.
This came up while I was talking with @xedin. This name makes
it really clear what we are trying to communicate to the author.
2024-06-21 02:24:03 -07:00
Michael Gottesman
3f39bdc1ed [sending] closure literals that are passed as sending parameters are now inferred to be nonisolated.
Consider the following piece of code and what the isolation is of the closure
literal passed to doSomething():

```swift
func doSomething(_ f: sending () -> ()) { ... }

@MyCustomActor
func foo() async {
  doSomething {
    // What is the isolation here?
  }
}
```

In this case, the isolation of the closure is @MyCustomActor. This is because
non-Sendable closures are by default isolated to their current context (in this
case @MyCustomActor since foo is @MyCustomActor isolated). This is a problem
since

1. Our closure is a synchronous function that does not have the ability to hop
to MyCustomActor to run said code. This could result in a concurrency hole
caused by running the closure in doSomething() without hopping to
MyCustomActor's executor.

2. In Region Based Isolation, a closure that is actor isolated cannot be sent,
so we would immediately hit a region isolation error.

To fix this issue, by default, if a closure literal is passed as a sending
parameter, we make its isolation nonisolated. This ensures that it is
disconnected and can be transferred safely.

In the case of an async closure literal, we follow the same semantics, but we
add an additional wrinkle: we keep support of inheritActorIsolation. If one
marks an async closure literal with inheritActorIsolation, we allow for it to be
passed as a sendable parameter since it is actually Sendable under the hood.
2024-06-21 02:24:03 -07:00
Tim Kientzle
1098054291 Merge branch 'main' into tbkka-assertions2 2024-06-18 17:52:00 -07:00
Slava Pestov
02e3b5a2e0 Sema: Remove unused variable 2024-06-11 09:01:39 -04:00
Tim Kientzle
1d961ba22d Add #include "swift/Basic/Assertions.h" to a lot of source files
Although I don't plan to bring over new assertions wholesale
into the current qualification branch, it's entirely possible
that various minor changes in main will use the new assertions;
having this basic support in the release branch will simplify that.
(This is why I'm adding the includes as a separate pass from
rewriting the individual assertions)
2024-06-05 19:37:30 -07:00
Kavon Farvardin
77e8fb0b74 NCGenerics: implicit casts require 'consume'
It's better if this is an error from the start.
2024-06-05 09:52:17 -07:00
Kavon Farvardin
e55e247f0c ConstraintSystem: clarify consuming conversions
There are a number of implicit conversions in Swift, such as to Optional
and to an existential, which are now possible for noncopyable types.

But all type casts are consuming operations for noncopyable types. So
it's confusing when a function that takes a borrowed argument of
optional type appears to be consuming:

```
func f(_ x: borrowing NC?) { ... }

let x = NC()
f(x)
f(x) // error!
```

So, rather than for people to write `x as T?` around all implicit
conversions, require them to write `consume x` around expressions
that will consume some lvalue. Since that makes it much more clear what
the consequences will be.

Expressions like `f(g())`, where you're passing an rvalue to the callee,
are not confusing. And those are exactly the expressions you're not
allowed to write `consume` for, anyway.

fixes rdar://127450418
2024-06-04 14:04:19 -07:00
Michael Gottesman
88729b9e34 [sending] Make {Transferring,Sending}ArgsAndResults a LANGUAGE_FEATURE instead of an UPCOMING_FEATURE.
TLDR: This makes it so that we always can parse sending/transferring but changes
the semantic language effects to be keyed on RegionBasedIsolation instead.

----

The key thing that makes this all work is that I changed all of the "special"
semantic changes originally triggered on *ArgsAndResults to now be triggered
based on RegionBasedIsolation being enabled. This makes a lot of sense since we
want these semantic changes specifically to be combined with the checkers that
RegionBasedIsolation turns on. As a result, even though this causes these two
features to always be enabled, we just parse it but we do not use it for
anything semantically.

rdar://128961672
2024-06-01 23:25:16 -07:00
Michael Gottesman
e3e78ad6bb [sending] Change the internals of sending to be based around 'sending' instead of 'transferring'.
We still only parse transferring... but this sets us up for adding the new
'sending' syntax by first validating that this internal change does not mess up
the current transferring impl since we want both to keep working for now.

rdar://128216574
2024-05-16 12:20:45 -07:00
Doug Gregor
2ffe9d556c [Type checker] Represent "initializable" let instance property references as lvalues
As we do with references to initializable local lets, teach the type
checker to produce ASTs where member references to initializable
instance property lets (e.g., within an initializer) treat the `let`
as an lvalue and then immediately load.

This modeling addresses a source compatibility issue uncovered in
swift-syntax, where code that *technically* has a
use-before-definition on a `let` property in an initializer wasn't
diagnosed as such because the loaded value wasn't actually used for
anything.
2024-05-15 10:33:54 -07:00
Doug Gregor
39f4e38027 Diagnose inout uses of 'lets' in constructors in the type checker.
Stored `let` properties of a struct, class, or actor permit
'inout' modification within the constructor body after they have been
initialized. Tentatively remove this rule, only allowing such `let`
properties to be initialized (assigned to) and not treated as `inout`.

Fixes rdar://127258363.
2024-05-14 15:59:50 -07:00
Slava Pestov
7e36060331 AST: Remove VarDecl::getOpenedElementEnvironment() 2024-05-10 17:21:17 -04:00
Joe Groff
d6e4b6bab8 Look through @Sendable conversions when considering C function pointer conversions.
Adding (or removing) `@Sendable` doesn't add context, so it shouldn't prevent
conversion to a C function pointer. Fix for rdar://127521718.
2024-05-03 17:47:53 -07:00
Pavel Yaskevich
7b8f76e3c4 Merge pull request #73007 from xedin/promote-dynamic-actor-isolation-to-upcoming-feature
[SE-0423] Promote `DynamicActorIsolation` to an upcoming feature and add a way to disable checks
2024-04-16 11:46:41 -07:00
Pavel Yaskevich
72eb8abadb [Sema/SILGen] Don't emit dynamic actor isolation checks when -disable-dynamic-actor-isolation flag is used 2024-04-15 09:19:22 -07:00
Alex Hoppen
078e1ea0cc [CodeCompletion] Fix a crash when completing the call pattern of a function that takes a parameter pack
The issue here was that we inferred the contextual type of the `Mixer` call to be the following, inferred from the result type of the partial call to the initializer of `Mixer`

```
(bound_generic_struct_type decl="swift_ide_test.(file).Mixer@/Users/alex/src/swift/test/IDE/complete_parameter_pack_as_call_argument.swift:3:8"
  (pack_type num_elements=1
    (pack_expansion_type
      (pattern=unresolved_type)
      (count=unresolved_type))))
```

Technically, the contextual type that we should have here should be `Any` (from `print`) but getting that information out of the constraint system turns out to be quite hard. https://github.com/apple/swift/pull/72568 makes some improvements in this area but in general the constraint system does not contain enough information to figure out the contextual type of an arbitrary expression.

The important thing right now is that the unresolved type in here trips the constraint system over when comparing types of code completion results with the contextual type. To prevent the crash for now, reset the expected call type if the computed type contains an unresolved type.

rdar://124166587

Co-authored-by: Pavel Yaskevich <pyaskevich@apple.com>
2024-04-12 14:03:57 -07:00
Slava Pestov
e7131cfbe2 Sema: Fix existential-metatype-to-Any conversion
We were incorrectly unwrapping too many levels of metatype, and
our ErasureExpr would end up with conformances for the instance
type and not the metatype itself.

In the old universe, this was not a problem, but now Any has two
protocol conformance members, so suddently this violated invariants.

This was a regression, introduced in 6027bf46a6.

Fixes rdar://125460667.
2024-04-01 23:05:22 -04:00
Konrad `ktoso` Malawski
6132386371 [Distributed] Complete handling of protocol calls and witnesses using adjusted mangling scheme (#72416) 2024-03-23 23:54:23 +09:00
Holly Borla
0a627bc44c Merge pull request #72324 from hborla/extract-function-isolation-expr
[Concurrency] Add a `.isolation` member on dynamically isolated function values.
2024-03-14 06:56:43 -07:00
Holly Borla
d9aa8697ab [Concurrency] Teach the constraint system about .isolation on dynamically
isolated function values.
2024-03-13 22:23:31 -07:00
Holly Borla
78384d596d [Concurrency] Add ExtractFunctionIsolationExpr to represent the isolation
of a dynamically isolated function value in the AST.
2024-03-13 19:55:15 -07:00
Michael Gottesman
acca7353db [transferring] Make async let take a transferring result if its result is non-Sendable.
Some notes:

1. If the result is non-Sendable and we didn't infer something that is
transferring, we still emit the current sema error that says that one cannot
assign a non-Sendable value to an async let.

2. When region isolation is enabled, but transferring args and results are
disabled, we leave the async let semantics alone. This means that the async let
closure is still @Sendable and one cannot pass in non-Sendable values to it.
2024-03-13 15:30:54 -07:00
Erik Eckstein
3c76464c1c rename withConcurrent -> withSendable
That was missed when "concurrent" was renamed to "sendable"
2024-03-13 09:58:31 +01:00