Commit Graph

6568 Commits

Author SHA1 Message Date
Michael Gottesman
6058b1d9bd [silgen] Change SILGen to emit ignored_user for emitIgnoredExpr and black hole initialization. 2025-01-22 21:12:36 -08:00
Joe Groff
01faf1ff98 Merge pull request #78572 from jckarter/synchronize-accessor-check-for-borrow
SILGen: Coordinate access strategy determination with actual codegen in `findStorageReferenceExprForMoveOnly`.
2025-01-13 09:57:12 -08:00
Alejandro Alonso
09d122af7c Merge pull request #76438 from Azoy/vector
[stdlib] Slab
2025-01-12 10:36:25 -08:00
Doug Gregor
ede7bfe1e8 Merge pull request #78554 from DougGregor/unsafe-effect 2025-01-10 23:51:29 -08:00
Andrew Trick
fe075dcb29 Fix SILGenFunction::emitValueConstructor for library evolution.
Always call createMarkUnresolvedNonCopyableValueInst for a constructor
with move-only 'self'. Handle 'self' that is either returned by value
or as an indirect result

Fixes rdar://142690658 (In ~Copyable public struct,
an init with COW type param causes compiler error)
2025-01-10 18:05:05 -08:00
Joe Groff
23747bbb8e SILGen: Coordinate access strategy determination with actual codegen in findStorageReferenceExprForMoveOnly.
Looking at the AST-level `getReadImpl` doesn't always correspond to what
accessor SILGen prefers to use, due to resilience, ABI rules, and other
concerns. In findStorageReferenceExprForMoveOnly, when determining whether
a storage reference is borrowable, use the same logic as SILGenLValue actually
uses to determine what storage or accessor access strategy to use.

Fixes rdar://142509673
2025-01-10 16:12:09 -08:00
Doug Gregor
8bb5bbedbc Implement an unsafe expression to cover uses of unsafe constructs
Introduce an `unsafe` expression akin to `try` and `await` that notes
that there are unsafe constructs in the expression to the right-hand
side. Extend the effects checker to also check for unsafety along with
throwing and async operations. This will result in diagnostics like
the following:

    10 |   func sum() -> Int {
    11 |     withUnsafeBufferPointer { buffer in
    12 |       let value = buffer[0]
       |                   |     `- note: reference to unsafe subscript 'subscript(_:)'
       |                   |- warning: expression uses unsafe constructs but is not marked with 'unsafe'
       |                   `- note: reference to parameter 'buffer' involves unsafe type 'UnsafeBufferPointer<Int>'
    13 |       tryWithP(X())
    14 |       return fastAdd(buffer.baseAddress, buffer.count)

These will come with a Fix-It that inserts `unsafe` into the proper
place. There's also a warning that appears when `unsafe` doesn't cover
any unsafe code, making it easier to clean up extraneous `unsafe`.

This approach requires that `@unsafe` be present on any declaration
that involves unsafe constructs within its signature. Outside of the
signature, the `unsafe` expression is used to identify unsafe code.
2025-01-10 10:39:14 -08:00
Andrew Trick
03ae24daa9 Fix SILGenFunction::emitBasicProlog for @lifetime with default args.
LifetimeDependencies were being read from the wrong DeclContext.

Fixes rdar://142451893 (Crash during AST -> SIL lowering)
2025-01-09 22:14:37 -08:00
Alejandro Alonso
f76d841540 Rename to Slab 2025-01-09 10:39:45 -08:00
Alejandro Alonso
0f30cdfb31 Allow Builtin.emplace to have typed throws 2025-01-08 10:37:16 -08:00
Alejandro Alonso
61702fb813 Implement Vector literals 2025-01-08 10:35:55 -08:00
Joe Groff
bc405c5f0a Merge pull request #78457 from jckarter/addressable-for-depedencies-2
`@_addressableForDependencies` fixes
2025-01-08 07:49:56 -08:00
Joe Groff
d9ee3f4de8 Make behavior changes for addressable types unconditional.
With the other fixes, it is now possible to build the stdlib without conditionalizing these
behaviors. This will allow libraries to adopt addressability as an experimental feature
without breaking ABI when interacting with other code.
2025-01-07 14:59:46 -08:00
Joe Groff
f9d9d75768 SILGen: Change codegen for loading from no-implicit-copy lvalues.
Previously, we would emit this as

  %1 = load [copy] %address
  %2 = moveonly_wrapper_to_copyable [owned] %1

which is difficult for move-only checking to handle, since %2 looks like a
consume of %1, making it difficult to determine %1's true lifetime. Change
this to

  %1 = moveonly_wrapper_to_copyable_addr %address
  %2 = load [copy] %address

which is handled better by move-only checking, improving the accuracy of
existing move-checking as well as fixing a spurious diagnostic when
indirect parameters get passed as by-value arguments to other functions.
2025-01-06 21:34:56 -08:00
Allan Shortlidge
d0f63a0753 AST: Split Availability.h into multiple headers.
Put AvailabilityRange into its own header with very few dependencies so that it
can be included freely in other headers that need to use it as a complete type.

NFC.
2025-01-03 18:36:04 -08:00
Joe Groff
a25edb2185 Make Builtin.addressForBorrow work with addressable parameters. 2025-01-02 21:33:55 -08:00
Joe Groff
5c4406b5e8 Add an @_addressableForDependencies type attribute.
This attribute makes it so that a parameter of the annotated type, as well as
any type structurally containing that type as a field, becomes passed as
if `@_addressable` if the return value of the function has a dependency on
the parameter. This allows nonescapable values to take interior pointers into
such types.
2025-01-02 21:33:51 -08:00
Michael Gottesman
56f38c4172 [concurrency] Add support in SILGen/SIL for emitting and calling implicit leading parameters for CallerInheritingIsolation isolation.
This does not change region isolation yet to recognize these as effectively
nonisolated.
2025-01-02 13:18:54 -08:00
Michael Gottesman
5d4239af57 [concurrency] Add new isolation kind CallerIsolationInheriting.
Right now it is basically a version of nonisolated beyond a few simple cases
like constructors/destructors where we are pretty sure we want to not support
this.

This is part of my bringup strategy for changing nonisolated/unspecified to be
caller isolation inheriting.
2025-01-02 13:18:30 -08:00
Michael Gottesman
a15f52009e [gardening] Use a higher level method that describes what a piece of code is doing instead of source inlining it. 2025-01-02 13:18:30 -08:00
Michael Gottesman
b2a4f7e3cc [sil] Add to SIL and SILGen the ability to emit/represent implicit parameters.
I need this today to add the implicit isolated parameter... but I can imagine us
adding more implicit parameters in the future, so it makes sense to formalize it
so it is easier to do in the future.
2025-01-02 13:18:11 -08:00
Pavel Yaskevich
636525ebd9 Merge pull request #78171 from xedin/rdar-140300022
[TypeChecker/SILGen] Allow `any Sendable` to match `Any` while matching generic arguments
2024-12-17 20:24:34 -08:00
Andrew Trick
b56a787c74 SILGen: emit mark_dependence for unsafeAddress
Fixes a correctness issue with unsafe addressors: `unsafeAddress` and
`unsafeMutableAddress`. Previously, the resulting `Unsafe[Mutable]Pointer` did
not depend on `self`, meaning that the compiler is allowed to destroy `self`
before any uses of the pointer. This happens to be valid for
`UnsafePointer.pointee` because, in that case, `self` does not have a lifetime
anyway; the correctness burden was on the programmer to use
`withExtendedLifetime` around all uses of `self`.

Now, unsafe addressors can be used for arbitrary `Self` types.

This also enables lifetime dependence diagnostics when the addressor points to a
`~Escapable` type.

Addressors can now be used as an implementation of borrowed properties.
2024-12-17 09:53:02 -08:00
Konrad `ktoso` Malawski
daf06c7f05 diagnose and dont crash when unable to find swift_task_deinitOnExecutor 2024-12-12 16:41:03 +09:00
Konrad `ktoso` Malawski
8ff2babfde Report fatal error when missing swift_task_deinitOnExecutor 2024-12-12 16:41:03 +09:00
Pavel Yaskevich
c4e4d4d6da [SIGen] UnsafeCastExpr: Use forwarding cast instead of bitcast
Forwarding cast handles ownership correctly and doesn't disable
optimizations unlike bitcast.
2024-12-11 17:19:20 -08:00
Pavel Yaskevich
cc13060f8c [SILGenBuilder] NFC: Add createUncheckedForwardingCast that works with ManagedValue 2024-12-11 17:19:20 -08:00
Pavel Yaskevich
5e08f7e257 [CSApply/SILGen] Implement any Sendable to Any erasure for generic arguments
In non-strict concurrency mode when `@preconcurrency` declarations
are involved `any Sendable` should be treated as `Any` in generic
argument positions to support passing types that (partially) adopted
concurrency annotations to types that haven't yet done so.
2024-12-11 17:19:14 -08:00
Joe Groff
3c0b08dbcb Prototype an @_addressable attribute that puts an argument at a stable address.
Many APIs using nonescapable types would like to vend interior pointers to their
parameter bindings, but this isn't normally always possible because of representation
changes the caller may do around the call, such as moving the value in or out of memory,
bridging or reabstracting it, etc. `@_addressable` forces the corresponding parameter
to be passed indirectly in memory, in its maximally-abstracted representation.
[TODO] If return values have a lifetime dependency on this parameter, the caller must
keep this in-memory representation alive for the duration of the dependent value's
lifetime.
2024-12-03 20:39:23 -08:00
Kuba Mracek
6f4ae28520 [ASTMangler] Pass ASTContext to all instantiations of ASTMangler 2024-12-02 15:01:04 -08:00
Michael Gottesman
e6b4e0f9f1 Merge pull request #77709 from gottesmm/pr-6feaf0c91a7d95d75b36d32cc91a32150d992162
[region-isolation] Some initial NFCI refactoring commits before adding experimental support for inheriting isolation to nonisolated functions
2024-11-19 22:22:50 -08:00
Michael Gottesman
d541190a5a [silgen] Refactor out how we compute the actor isolation for a SILFunction so we can reuse it in other contexts.
I also want to extend it and did not want to have to copy/paste this code into
multiple places.

The small test tweak occurs since I changed the initializer SILGen emission code
to set the declref field of SILFunctions to the actual decl ref which we did not
before. So we got a more specific diagnostic.
2024-11-19 12:47:45 -08:00
Slava Pestov
47156e006b AST: Introduce ProtocolConformanceRef::forAbstract() 2024-11-16 16:16:06 -05:00
Slava Pestov
35b78c950b SILGen: Remove usage of weird ProtocolConformanceRef() constructor 2024-11-16 16:16:06 -05:00
Doug Gregor
c5de02f60e Merge pull request #77628 from DougGregor/clang-importer-parse-request
[Clang importer] Use ParseSourceFileRequest for parsing swift_attr attributes
2024-11-16 01:12:27 -08:00
Michael Gottesman
ebcab8f2ee [silgen] Change ArgEmitter to use an OptionSet before I add another option.
NFCI.
2024-11-15 15:14:32 -08:00
Slava Pestov
7c21789c88 SILGen: Unwrap inherited conformance when emitting witness thunk
I'm about to fix RequirementEnvironment to correctly use
inherited conformances.
2024-11-15 17:26:35 -05:00
Slava Pestov
53e52abce1 SILGen: Fix invalid substitution map formation in coroutine witness thunk emission
If the conformance generic signature fixes all generic parameters,
F.getForwardingSubstitutionMap() is empty. Instead, map the
replacement types of the substitution map into the generic
environment earlier, before we strip off a fully-concrete generic
signature.
2024-11-15 17:26:34 -05:00
Slava Pestov
4e17871537 SIL: Fix formatting 2024-11-15 17:26:34 -05:00
Slava Pestov
59b1f1125a SILGen: Fix a spot where we passed in the wrong conformances
The conforming type didn't match the conformances, which is going to
be flagged by SubstitutionMap::verify().
2024-11-15 17:26:34 -05:00
Pavel Yaskevich
7bbb63a4f6 Merge pull request #77631 from xedin/rdar-134442168
[SILGen] ResultPlan: Make sure that checked continuations are destroye…
2024-11-15 09:03:44 -08:00
Doug Gregor
24a12ebc34 Renable GeneratedSourceInfo::Attribute to GeneratedSourceInfo::AttributeFromClang 2024-11-15 09:02:49 -08:00
Pavel Yaskevich
6fef6f5c92 [SILGen] ResultPlan: Make sure that checked continuations are destroyed after use
Currently nothing destroys the injected checked continuations which
means that their "canaries" leak.

Resolves: rdar://134442168
2024-11-14 20:17:19 -08:00
Doug Gregor
989c73d014 Ensure that buffers containing Clang swift_attr attributes are parsed as attributes
Previously, they were being parsed as top-level code, which would cause
errors because there are no definitions. Introduce a new
GeneratedSourceInfo kind to mark the purpose of these buffers so the
parser can handle them appropriately.
2024-11-13 21:19:37 -08:00
John McCall
e99ec88ce3 Use common code paths for constructor concurrency prologs if we're not in
the async self-isolated actor initializer case.

Fixes rdar://138394497, a bug where we didn't set up isolation correctly for
an async parameter-isolated initializer, but also probably a non-trivial number
of other latent differences between initializers and normal functions.
2024-11-13 17:10:32 -05:00
Nate Chandler
da71271d8f [CoroutineAccessors] Synthesize default req impls.
When a protocol which has a read (or modify) requirement is built with
the CoroutineAccessors feature, it gains a read2 (or modify2,
respectively) requirement.  For this to be compatible with binaries
built without the feature, a default implementation for these new
requirements must be provided.  Cause these new accessor requirements to
have default implementations by returning `true` from
`doesAccessorHaveBody` when the context is a `ProtocolDecl` and the
relevant availability check passes.
2024-11-07 16:47:09 -08:00
Nate Chandler
f6e145e995 [NFC] Remove dead branch.
The variable `storage` was defined as `nullptr` a couple lines above and
then is check for null, a spurious check.  Eliminate that
nullcheck--it's always true.
2024-11-06 20:52:21 -08:00
Nate Chandler
f1f0ccdeff [NFC] Improved predicate names. 2024-11-06 20:52:21 -08:00
Michael Gottesman
1046a03fb4 [concurrency] When calling an imported async objc function using continuations, use merge_isolation_region to tie the block storage and the resume buffer into one region.
This ensures that if the block has an @out return value that the return value is
considered to be affected by the actual call of the block. Before b/c we
smuggled the value through a Sendable continuation, we would lose the connection
in between the block and that result.

rdar://131422332
2024-11-01 12:11:44 -07:00
Michael Gottesman
dddfdc891f [region-isolation] Codegen unsafe continuation result using calls to foreign APIs in the same manner as with checked continuation.
The reason why I am doing this is that the unlike the codegen for checked
continuation, the codegen for unchecked continuation uses a sendable value
instead of Any as the block storage which prevents me from being able to create
a dependency from a non-Sendable @out parameter to the block.

By changing to use Any consistently, we are able to take advantage of Any not
being sendable to properly propagate this information.
2024-11-01 11:25:17 -07:00