Commit Graph

2434 Commits

Author SHA1 Message Date
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
Pavel Yaskevich
e2c94ad3a7 Merge pull request #71896 from xedin/rdar-123638701
[CSApply] Optional chaining forces key path component to be r-value
2024-02-27 09:32:28 -08:00
Pavel Yaskevich
7ad80b8ce0 [CSApply] Optional chaining forces key path component to be r-value
Key path components with optional chaining unlike other places
don't propagate l-value through optionals.

Resolves: rdar://123638701
2024-02-26 13:10:46 -08:00
Ben Barham
f292ec9784 Use the new template deduction guides rather than makeArrayRef
LLVM has removed `make*ArrayRef`, migrate all references to their
constructor equivalent.
2024-02-23 20:04:51 -08:00
Ben Barham
ef8825bfe6 Migrate llvm::Optional to std::optional
LLVM has removed llvm::Optional, move over to std::optional. Also
clang-format to fix up all the renamed #includes.
2024-02-21 11:20:06 -08:00
Pavel Yaskevich
26a4077d20 Merge pull request #71737 from Jumhyn/fix-keypath-subtype-solution-application
Fix solution application for keypath function subtype conversions
2024-02-21 10:25:39 -08:00
Freddy Kellison-Linn
2b2f780a21 Fix solution application for keypath function subtype conversions
In #39612 we added subtyping for keypaths-as-functions, but during application
the implementation naively coerced the keypath expression itself to the
inferred supertype, resulting in erroneous results. This patch updates the
solution application logic to build the keypath-function conversion expression
based entirely on the 'natural' keypath type, only converting to the inferred
supertype at the end via the usual coerceToType machinery for function
conversions.
2024-02-19 15:41:15 -08:00
Slava Pestov
8f939c4a34 AST: Remove unused computation from IsBindableVisitor 2024-02-19 14:53:07 -06:00
Pavel Yaskevich
808c42b5b4 Merge pull request #71585 from xedin/fix-printobjc-protocol-requirements-checking
[PrintObjC] Fix `ReferencedTypeFinder::isConstrained` to filter out i…
2024-02-14 09:20:28 -08:00
Pavel Yaskevich
14e07566ed [CSApply] NFC: Simplify isOpenedAnyObject to check existential type 2024-02-13 13:43:07 -08:00
Pavel Yaskevich
0db22316a2 [ConstraintSystem] Record conformances synthesized for ad-hoc distributed requirements 2024-02-12 14:26:30 -08:00
Pavel Yaskevich
6afa7328d9 [CSApply] Produce an abstract conformance for Res parameter of DistributedActorSystem.remoteCall witness 2024-02-12 14:26:08 -08:00
Pavel Yaskevich
29a086d8ec [ConstraintSystem] NFC: Pass an underlying declaration to Solution::computeSubstitutions
This is going to be used to determine whether the substitutions are
computed for `DistributedActorSystem::remoteCall` and adjust the
generic signature with witness conformance requirements.
2024-02-12 13:23:51 -08:00
Kavon Farvardin
da8de569dc NCGenerics: remove ArcheType::hasRequirements
The IDE's use isn't correct under noncopyable generics and only the
constraint solver uses it. It's a misleading method now that nearly
every archetype has Copyable/Escapable requirements.
2024-02-11 12:54:35 -08:00
Slava Pestov
983dc250ad Sema: Replace a checkConformance() call with lookupConformance() 2024-02-10 09:36:37 -05:00
Slava Pestov
6027bf46a6 AST: Simplify collectExistentialConformances() 2024-02-10 09:36:37 -05:00
Hamish Knight
1003b2f30b [CS] Remove CTP_ImpliedReturnStmt
Unify with `CTP_ReturnStmt`, and have the
SyntacticElementTarget carry the ReturnStmt for
regular type-checking, which we can use to record
implied returns.
2024-02-07 18:14:23 +00:00
Hamish Knight
61a4148925 [CS] Generalize implied result handling
Track the implied result exprs in the constraint
system, and allow arbitrary propagation of
implied results down if/switch expression
branches. This is required for allowing implied
results in non-single-expression closures.
2024-02-07 18:14:22 +00:00
Hamish Knight
16cfca4186 [ASTWalker] NFC: Rename SkipChildren -> SkipNode
This better describes what the action currently
does, and allows us to re-introduce `SkipChildren`
with the correct behavior.
2024-02-05 15:27:25 +00:00
Frederick Kellison-Linn
0735629c8d Merge pull request #39612 from Jumhyn/keypath-function-conversion
[ConstraintSystem] Allow function-function conversions for keypath literals
2024-02-03 10:26:13 -08:00
Pavel Yaskevich
9d0797a10b Merge pull request #71248 from xedin/init_accessors-nonmutating-big-struct
[CSApply] Teach `adjustSelfTypeForMember` about init accessors
2024-02-02 15:23:19 -08:00
Pavel Yaskevich
53b2d86830 [CSApply] Determine whether to emit runtime isolation check based on Module::isConcurrencyChecked API 2024-02-01 14:14:31 -08:00
Pavel Yaskevich
db7959668e [Frontend] NFC: Rename PreconcurrencyConformances to DynamicActorIsolation 2024-02-01 13:28:25 -08:00
Pavel Yaskevich
68baadc253 [SILGen] Emit runtime actor isolation checks for @objc thunks of closures 2024-02-01 13:28:25 -08:00
Pavel Yaskevich
aff92488ef [CSApply] Use ActorIsolationErasure conversion in argument positions that require runtime check
Passing an isolated function value into an API that doesn't have full
static concurrency checking is unsafe and requires runtime checks
to make sure that function is always called in the expected context.
2024-02-01 13:28:25 -08:00
Hamish Knight
deaf2dd372 [CS] Unify ReturnStmt handling
Unify the implementation between single-expression
and multi-statement closures. Because we're now
storing a contextual type for single expression
closure returns, update a code completion test to
bring its behavior inline with the multi-statement
case.
2024-01-31 20:26:20 +00:00
Pavel Yaskevich
4242ac8c31 [CSApply] Teach adjustSelfTypeForMember about init accessors
`adjustSelfTypeForMember` shouldn't load base if member reference
is a potential init accessor use, the proper use of `self` would
be determined during lowering of the `assign_or_init` instruction
and defensive load for `nonmutating` sets is unnecessary in this
case.
2024-01-30 11:09:15 -08:00
Hamish Knight
0a4c029cfc [AST] Introduce UnreachableExpr
This models the conversion from an uninhabited
value to any type, and allows us to get rid of
a couple of places where we'd attempt to drop
the return statement instead.
2024-01-30 14:08:54 +00:00
Hamish Knight
9b64990d24 [AST] Remove the "single expression body" bit
Remove this bit from function decls and closures.
Instead, for closures, infer it from the presence
of a single return or single expression AST node
in the body, which ought to be equivalent, and
automatically takes result builders into
consideration. We can also completely drop this
query from AbstractFunctionDecl, replacing it
instead with a bit on ReturnStmt.
2024-01-30 14:08:54 +00:00
Doug Gregor
35582370d4 Partial actor isolation checking for #isolation
This isn't done very well, and is expected to be replaced, but it
allows us to avoid bogus Sendable diagnostics for iteration over
async sequences.
2024-01-25 16:04:48 -08:00
Doug Gregor
bb7a563e6c Switch async for-each loop over to _nextElement and drop @rethrows.
This couples together several changes to move entirely from
`@rethrows` over to typed throws:

* Use the `Failure` type to determine whether an async for-each loop
will throw, rather than depending on rethrows checking

* Introduce a special carve-out for `rethrows` functions that have a
generic requirement on an `AsyncSequence` or `AsyncIteratorProtocol`,
which uses that requirement's `Failure` type as potentially being part
of the thrown error type. This allows existing generic functions like
the following to continue to work:

    func f<S: AsyncSequence>(_: S) rethrows

* Switch SIL generation for the async for-each loop from the prior
`next()` over to the typed-throws version `_nextElement`.

* Remove `@rethrows` from `AsyncSequence` and `AsyncIteratorProtocol`
entirely. We are now fully dependent on typed throws.
2024-01-25 16:04:43 -08:00
Frederick Kellison-Linn
0d79f4523a Allow function-function conversions for keypath literals
Remove keypath subtype asserts; always use cached root type

Add tests for keypaths converted to funcs with inout param

Add unit test for overload selection
2024-01-23 08:51:44 -05:00