Commit Graph

768 Commits

Author SHA1 Message Date
Holly Borla
21a86b5d2f [NFC][Property Wrappers] Split PropertyWrapperBackingPropertyInfoRequest into
two separate requests - one to synthesize the auxiliary declarations, and
another to compute how the backing storage is initialized.
2021-03-19 13:03:13 -07:00
Joe Groff
79fb05b362 Concurrency: Hop back to the previous executor after actor calls.
Tasks shouldn't normally hog the actor context indefinitely after making a call that's bound to
that actor, since that prevents the actor from potentially taking on other jobs it needs to
be able to address. Set up SILGen so that it saves the current executor (using a new runtime
entry point) and hops back to it after every actor call, not only ones where the caller context
is also actor-bound.

The added executor hopping here also exposed a bug in the runtime implementation while processing
DefaultActor jobs, where if an actor job returned to the processing loop having already yielded
the thread back to a generic executor, we would still attempt to make the actor give up the thread
again, corrupting its state.

rdar://71905765
2021-03-18 11:47:50 -07:00
Kavon Farvardin
2d0911cd97 implementation of async actor properties in SILGen
The strategy for implementing them is integrated with the
PathComponent infrastructure in SILGen in order to correctly
support mixtures of chained accesses and forced optionals, etc.

The actor isolation information is only piped into LValues from
the expressions that might be marked implicitly-async.
2021-03-04 18:37:32 -08:00
Kavon Farvardin
bf171edbc0 assert that some implicitly async property/subscript references have not made it to SILGen
For the dynamic versions of LookupExprs, we do not expect these to be
implicitly async. For the other cases, I simply haven't implemented them
yet.
2021-03-04 18:37:32 -08:00
Erik Eckstein
9055e93af9 SIL: some improvements/fixes around assign_by_wrapper
* Refactoring: replace "Destination" and the ownership qualifier by a single "Mode".  This represents much better the mode how the instruction is to be lowered. NFC
* Make assign_by_wrapper printable and parseable.
* Fix lowering of the assign modes for indirect results of the init-closure: The indirect result was initialized and not assigned to. The fix is to insert a destroy_addr before calling the init closure. This fixes a memory lifetime error and/or a memory leak. Found by inspection.
* Fix an iterator-invalidation crash in RawSILInstLowering
* Add tests for lowering assign_by_wrapper.
2021-03-03 18:57:02 +01:00
Holly Borla
9cdecf4703 [Property Wrappers] Compute generic wrapped-value and projected-value
initialization expressions for parameters with attached property wrappers.
2021-02-25 18:35:14 -08:00
Holly Borla
13692fefde [Property Wrappers] Store property wrapper "init from projection" expressions
in PropertyWrapperBackingPropertyInfo.
2021-02-25 18:35:14 -08:00
Erik Eckstein
ec64f2a255 SILLocation: replace CleanupLocation::get(loc) with CleanupLocation(loc)
No need to have a static get function - the constructor can be used directly.
NFC
2021-01-29 20:28:21 +01:00
Holly Borla
3640de3455 [Property Wrappers] Check for ignored assign_by_wrapper loads in
LifetimeChecker::shouldEmitError in order to avoid incorrectly
populating EmittedErrorLocs when no error is emitted.

This fixes a few corner cases where DI would error out or crash
while assigning to a wrapped property with a nonmutating setter.
2020-12-23 20:31:51 -05:00
Holly Borla
05686f7261 [SILGen] Always use assign_by_wrapper if a property wrapper assignment
is in an init or if the wrapped var is a local variable.
2020-12-23 11:00:47 -05:00
Holly Borla
a478cd72a0 [SILGen] When creating an assign_by_wrapper instruction, substitute
the setter type before attempting to emit a load in the case where
the setter is nonmutating and must capture self by value.
2020-12-23 10:59:37 -05:00
Doug Gregor
dab6fb7098 [Concurrency] Implement SIL generation for "async let".
Implement SIL generation for "async let" constructs, which involves:

1. Creating a child task future at the point of declaration of the "async let",
which runs the initializer in an async closure.
2. Entering a cleanup to destroy the child task.
3. Entering a cleanup to cancel the child task.
4. Waiting for the child task when any of the variables is reference.
5. Decomposing the result of the child task to write the results into the
appropriate variables.

Implements rdar://71123479.
2020-11-27 22:50:39 -08:00
Andrew Trick
6f2cda1390 Add AccessUseVisitor and cleanup related APIs.
Add AccesssedStorage::compute and computeInScope to mirror AccessPath.

Allow recovering the begin_access for Nested storage.

Adds AccessedStorage.visitRoots().
2020-10-16 15:00:10 -07:00
Holly Borla
763bcf9005 [SILGen] Don't use assign_by_wrapper for local property wrappers if
there is an initial wrapped value.
2020-10-01 20:11:40 -07:00
Holly Borla
4bb98baf13 [SILGen] Add a new CaptureEmission kind specifically for emitting
captured local variables for the assign_by_wrapper setter.

Since assign_by_wrapper will always be re-written to initialization
if the captured local variable is uninitialized, it's unnecessary
to mark the capture as an escape. This lets us support out-of-line
initialization for local property wrappers.
2020-09-30 10:40:44 -07:00
Holly Borla
0842b42127 [SILGen] Only use assign_by_wrapper for wrapped instance properties inside an
initializer, and for wrapped local variables.
2020-09-28 16:57:35 -07:00
Holly Borla
b33dbedd9b [SILGen] Teach SIlGen to emit local property wrappers 2020-09-28 16:57:35 -07:00
Holly Borla
a6c33a9611 [Property Wrappers] Use autoclosure information from CSApply to compute
`VarDecl::getPropertyWrapperInitValueInterfaceType`.
2020-07-13 17:22:29 -07:00
Arnold Schwaighofer
825a2a259b Mark non-foreign entry points of @objc dynamic methods in generic classes dynamically_replaceable
```
class Generic<T> {
  @objc dynamic func method() {}
}

extension Generic {
  @_dynamicReplacement(for:method())
  func replacement() {}
}
```

The standard mechanism of using Objective-C categories for dynamically
replacing @objc methods in generic classes does not work.

Instead we mark the native entry point as replaceable.

Because this affects all @objc methods in generic classes (whether there
is a replacement or not) by making the native entry point
`[dynamically_replaceable]` (regardless of optimization mode) we guard this by
the -enable-implicit-dynamic flag because we are late in the release cycle.

* Replace isNativeDynamic and isObjcDynamic by calls to shouldUse*Dispatch and
  shouldUse*Replacement
  This disambiguates between which dispatch method we should use at call
  sites and how these methods should  implement dynamic function
  replacement.

* Don't emit the method entry for @_dynamicReplacement(for:) of generic class
  methods
  There is not way to call this entry point since we can't generate an
  objective-c category for generic classes.

rdar://63679357
2020-06-09 09:23:29 -07:00
Arnold Schwaighofer
147144baa6 SIL: Thread type expansion context through to function convention apis
This became necessary after recent function type changes that keep
substituted generic function types abstract even after substitution to
correctly handle automatic opaque result type substitution.

Instead of performing the opaque result type substitution as part of
substituting the generic args the underlying type will now be reified as
part of looking at the parameter/return types which happens as part of
the function convention apis.

rdar://62560867
2020-05-04 13:53:30 -07:00
Artem Chikin
d04ae47801 Allow initializing a wrapped property with a nonmutating setter
This is achieved in 3 steps:
1. CSApply detects assignments to property wrappers inside constructors, and produces an `inout` expr instead of a `load`, which it normally would because nonmutating setters take the `self` by-value. This is necessary becasue the assign_by_wrapper instruction expects an address type for its $1 operand.
2. SILGenLValue now emits the assign_by_wrapper pattern for such setters, ignoring the fact that they capture `self` by value. It also introduces an additional load instruction for the setter patrial_apply because the setter signature still expects a value and we now have an address (because of (1)).
3. DefiniteInitialization specifically ignores load instructions used to produce a `self` value for a setter referenced on assign_by_wrapper because it will be deleted by lowering anyway.

Resolves rdar://problem/60600911 and rdar://problem/52280477
2020-04-30 14:49:22 -07:00
Anthony Latsis
74252028ca AST: Rename getFullName -> getName on ValueDecl & MissingMemberDecl 2020-04-23 05:16:55 +03:00
Anthony Latsis
c63b737e92 Collapse all indirect equivalents to ValueDecl::getBaseIdentifier 2020-03-29 00:36:01 +03:00
Roopesh Chander
b63e73e1eb [Property wrappers] Rename hasPropertyWrapper() for clarity
GetterSetterComponent::hasPropertyWrapper() now checks for more
stuff than just whether there's a property wrapper or not.

It's therefore renamed to canRewriteSetAsPropertyWrapperInit().
2020-03-20 12:21:50 -07:00
Roopesh Chander
8ee83fdf28 [Property wrappers] Avoid assign_by_wrapper when autoclosure is involved
When assigning to a wrapped variable in 'init()', if the property wrapper's
'wrappedValue:' parameter is an escaping autoclosure, the initializer and
setter have incompatible types -- the setter takes a value, and initializer
takes a closure returning that value.

An assign_by_wrapper with incompatible types causes SIL verification to fail.

This commit makes the SIL not use assign_by_wrapper in such cases, and use the
setter directly, resulting in a definitive initialization (DI) error instead.

    struct S {
      @Lazy var n: Int // Lazy.init(wrappedValue: @autoclosure ... )
      init() {
        n = 1 // error: 'self' used before all stored properties are initialized
      }
    }
2020-03-20 12:20:53 -07:00
Arnold Schwaighofer
57c6c59ad6 SILGen: Fix assign_by_wrapper of `@objc dynamic` properties
Insert a thunk. Objc method calls might need bridging.

rdar://60038084
2020-03-19 15:09:19 -07:00
Michael Gottesman
f3df836652 [ownership] Treat open_existential_box as an interior pointer producting instruction.
This means that it can only have a guaranteed object as an operandand that we
validate that all uses of the result address of open_existential_box occur only
within the lifetime of said object's borrow scope.
2020-03-15 23:21:45 -07:00
John McCall
585c28d0c3 Plumb a result SILType through SILGen's emitTransformedValue.
This fixes an immediate bug with subst-to-orig conversion of
parameter functions that I'm surprised isn't otherwise tested.
More importantly, it preserves valuable information that should
let us handle a much wider variety of variant representations
that aren't necessarily expressed in the AbstractionPattern.
2020-03-12 00:23:13 -04:00
Robert Widmann
3e4a820fb4 [SILGen] Handle Reabstraction and Substitution in assign_by_wrapper
Use the substitution map for the LValue component to properly handle the
case where we need to re-abstract the argument to the assign_by_wrapper
setter.

Resolves rdar://59553318
2020-02-18 14:59:03 -08:00
Robert Widmann
3b11c2b170 [SILGen] Materialize Arguments To Property Wrapper Setters If Needed
Codegen for the assign_by_wrapper instruction emits prepared arguments
directly into the initializer or accessor functions. This means it
misses out on the CC matching code that the general apply path uses.

In this particular case, an enum value was constructed within the
resilience boundary, but passed to the setter for a wrapper property
that expected it to be in memory.

Check the calling convention before we emit the assign_by_wrapper
instruction, and materialize an address for indirect CCs as required.

resolves rdar://59071930
2020-02-18 09:10:07 -08:00
Slava Pestov
38815ecf8d SILGen: Fix crash on assignment to static property wrapper through 'self'
Fixes <rdar://problem/59117087>.
2020-02-13 18:18:23 -05:00
Michael Gottesman
8f91b9e89a [ownership] Get rid of old entrypoint for creatingEndBorrow that takes the original value.
The original design was to make it so that end_borrow tied at the use level its
original/borrowed value. So we would have:

```
%borrowedVal = begin_borrow %original
...
end_borrow %borrowedVal from %original
```

In the end we decided not to use that design and instead just use:

```
%borrowedVal = begin_borrow %original
...
end_borrow %borrowedVal
```

In order to enable that transition, I left the old API for end_borrow that took
both original and borrowedVal and reimplemented it on top of the new API that
just took the borrowedVal (i.e. the original was just a dead arg).

Now given where we are in the development, it makes sense to get rid of that
transition API and move to just use the new API.
2020-02-02 12:57:58 -08:00
Arnold Schwaighofer
8ed908f923 Revert "[silgen] When SILGenLValue accesses ref_elt_addr, emit unsafe access for immutable or non accessing uses instead of not emitting any begin_access."
This reverts commit a3b68e6df5.

Speculative revert because I believe it is the cause of the failures on
the swift-master-source-compat-suite-enable-verify-exclusivity bot.

rdar://58529726
2020-01-15 10:12:23 -08:00
Michael Gottesman
a3b68e6df5 [silgen] When SILGenLValue accesses ref_elt_addr, emit unsafe access for immutable or non accessing uses instead of not emitting any begin_access. 2020-01-09 15:22:09 -08:00
Arnold Schwaighofer
33f4f57cc4 SILGen: Add TypeExpansionContext to SILGen 2019-11-11 14:21:52 -08:00
Michael Gottesman
7ee5ad7318 [sil] Rename {,Strong}Copy{Unowned,Unmanaged}. 2019-10-26 17:03:47 -07:00
Joe Groff
03c7919b4a SIL: Add fields to SILFunctionType for substituted function types.
https://forums.swift.org/t/improving-the-representation-of-polymorphic-interfaces-in-sil-with-substituted-function-types/29711

This prepares SIL to be able to more accurately preserve the calling convention of
polymorphic generic interfaces by letting the type system represent "substituted function types".
We add a couple of fields to SILFunctionType to support this:

- A substitution map, accessed by `getSubstitutions()`, which maps the generic signature
  of the function to its concrete implementation. This will allow, for instance, a protocol
  witness for a requirement of type `<Self: P> (Self, ...) -> ...` for a concrete conforming
  type `Foo` to express its type as `<Self: P> (Self, ...) -> ... for <Foo>`, preserving the relation
  to the protocol interface without relying on the pile of hacks that is the `witness_method`
  protocol.

- A bool for whether the generic signature of the function is "implied" by the substitutions.
  If true, the generic signature isn't really part of the calling convention of the function.
  This will allow closure types to distinguish a closure being passed to a generic function, like
  `<T, U> in (*T, *U) -> T for <Int, String>`, from the concrete type `(*Int, *String) -> Int`,
  which will make it easier for us to differentiate the representation of those as types, for
  instance by giving them different pointer authentication discriminators to harden arm64e
  code.

This patch is currently NFC, it just introduces the new APIs and takes a first pass at updating
code to use them. Much more work will need to be done once we start exercising these new
fields.

This does bifurcate some existing APIs:

- SILFunctionType now has two accessors to get its generic signature.
  `getSubstGenericSignature` gets the generic signature that is used to apply its
  substitution map, if any. `getInvocationGenericSignature` gets the generic signature
  used to invoke the function at apply sites. These differ if the generic signature is
  implied.
- SILParameterInfo and SILResultInfo values carry the unsubstituted types of the parameters
  and results of the function. They now have two APIs to get that type. `getInterfaceType`
  returns the unsubstituted type of the generic interface, and
  `getArgumentType`/`getReturnValueType` produce the substituted type that is used at
  apply sites.
2019-10-25 13:38:51 -07:00
Doug Gregor
43da5c9744 Remove dead flag disable-tsan-inout-instrumentation 2019-10-14 21:42:33 -07:00
Doug Gregor
f9f63bdf65 [DI] Don't allow assign_by_wrapper formation to break access control.
Fixes rdar://problem/54352235
2019-10-11 20:02:32 -07:00
Arnold Schwaighofer
e0ce37e0da SILGen: Fix setting variables with property wrappers marked objc dynamic
rdar://54181647
2019-10-03 07:57:23 -07:00
eeckstein
66a2f62a99 Merge pull request #27300 from eeckstein/fix-silgen-crash
SILGen: use the correct forwarding substitutions for the setter of an assign_by_wrapper.
2019-09-25 08:37:41 +02:00
Doug Gregor
8613b05445 [DI] Support definite initialization for composed property wrappers.
Use the newly-introduced property wrapper backing initializer function
in definite initialization (DI) to form the assign_by_wrapper
instruction, rather than forming a reference to the (only) property
wrapper's `init(wrappedValue:)`. This allows DI to work on properties
that have multiple, composed property wrappers applied to them.
2019-09-24 09:11:53 -07:00
Erik Eckstein
7d65012f27 SILGen: use the correct forwarding substitutions for the setter of an assign_by_wrapper.
Fixes a crash when a function, which assigns to a wrapped property has additional generic parameters.

https://bugs.swift.org/browse/SR-11484
rdar://problem/55442328
2019-09-24 09:41:35 +02:00
Doug Gregor
a7fce599f5 [SILGen] Properly substitute contextual archetypes when generating assign_by_wrapper
Fixes SR-11303 / rdar://problem/54311335
2019-09-19 13:03:27 -07:00
Slava Pestov
d434188157 SIL: Refactor TypeConverter to not require a SILModule
This mostly requires changing various entry points to pass around a
TypeConverter instead of a SILModule. I've left behind entry points
that take a SILModule for a few methods like SILType::subst() to
avoid creating even more churn.
2019-09-06 21:50:15 -04:00
Michael Gottesman
1cc3ed0b6f [gardening] Eliminate two unused variables. 2019-08-28 10:56:59 -07:00
Michael Gottesman
6a54531811 [silgen] Add SILGen support for emitting copy_unmanaged_value instead of unmanaged_to_ref + strong_retain.
This will ensure that the optimizer never eliminates the strong_retain. This
operation is meant to be unmanaged, we should respect the user's choice here
even in optimized builds.
2019-08-26 09:40:27 -07:00
Slava Pestov
2dbeeb0d3f AST: Make SubstFlags::UseErrorType the default behavior
We've fixed a number of bugs recently where callers did not expect
to get a null Type out of subst(). This occurs particularly often
in SourceKit, where the input AST is often invalid and the types
resulting from substitution are mostly used for display.

Let's fix all these potential problems in one fell swoop by changing
subst() to always return a Type, possibly one containing ErrorTypes.

Only a couple of places depended on the old behavior, and they were
easy enough to change from checking for a null Type to checking if
the result responds with true to hasError().

Also while we're at it, simplify a few call sites of subst().
2019-08-22 01:07:50 -04:00
Slava Pestov
0c5d52d860 AST: Introduce AbstractStorageDecl::get{Parsed,Opaque}Accessor()
Also, change visitOpaqueAccessors() to call getOpaqueAccessor() instead of
asserting if the expected accessor does not exist.
2019-08-02 19:34:43 -04:00
Slava Pestov
64c32c695b AST: Remove a few utility methods from AbstractStorageDecl
Since the return value of getAccessor() depends on mutable state, it
does not make sense in the request evaluator world. Let's begin by
removing some utility methods derived from getAccessor(), replacing
calls to them with calls to getAccessor().
2019-08-01 18:31:58 -04:00