Commit Graph

6754 Commits

Author SHA1 Message Date
Adrian Prantl
7092e50e05 Merge pull request #66479 from adrian-prantl/110329894
Avoid emitting variable debug info for closure captures.  … @adrian-prantl
2023-06-09 09:21:31 -07:00
Adrian Prantl
3a97766bb3 Avoid emitting variable debug info for closure captures.
Variable debug info is triggered by pattern bindings, however, inside a closure
capture list, this should be avoided by setting the appropriate flag in the
initializer object.

rdar://110329894
2023-06-08 16:33:14 -07:00
Joe Groff
ca7b8e2890 Merge pull request #66445 from jckarter/noncopyable-computed-property-address-only-base
SILGen: Don't copy a borrowed noncopyable address-only base of a computed property access.
2023-06-08 13:42:41 -07:00
Joe Groff
f06621907e SILGen: Don't copy a borrowed noncopyable address-only base of a computed property access.
We can probably avoid this copy in more circumstances, but make the change only for
noncopyable types for now, since that's the case where it's most semantically apparent.
rdar://109161396
2023-06-08 08:36:04 -07:00
Pavel Yaskevich
274a43b040 Merge pull request #66283 from xedin/init-accessors
[Parse/Sema/SIL] Implementation of init accessors feature (under a flag)
2023-06-07 08:16:35 -07:00
Doug Gregor
5ea559435d Merge pull request #66387 from DougGregor/silgen-freestanding-local-vars 2023-06-07 06:49:23 -07:00
Pavel Yaskevich
ad95e911a8 [Sema/SIL] InitAccessors: Incorporate init accessors into memberwise initializers
Properties with init accessors are used in place of properties they
are supposed to initialize in order of stored properties.
2023-06-06 18:59:46 -07:00
Pavel Yaskevich
7b140f89b9 [SILGen] InitAccessors: AssignOrInit instruction should reference init accessor directly
Previously init accessor has been partially applied just like a setter but
that is no loger valid now that it operates on individual field references.
2023-06-06 18:59:13 -07:00
Pavel Yaskevich
6c0e3914b8 [SILGen] InitAccessor: Emit init accessor function
Emit all initializes/accesses properties as arguments
in custom prolog and map them back to the originating
property declarations.
2023-06-06 18:59:13 -07:00
Pavel Yaskevich
5bac6e8538 [SILGen] NFC: Generalize isCurrentFunction{{ReadAccess, InitAccessor}} functions 2023-06-06 18:59:13 -07:00
Pavel Yaskevich
00a9ccfcc3 [SILGen] InitAccessors: Remap property references into argument references
A reference to an instance property in init accessor body
has to be remapped into an argument reference because all
of the properties from initialized/accesses lists are passed
to init accessors individually via arguments.
2023-06-06 18:59:13 -07:00
Pavel Yaskevich
d670067c65 [SILGen] InitAccessors: Add a way to check whether property is redirected to argument
Instance properties referenced by init accessors are mapped to synthesized
arguments.
2023-06-06 18:59:13 -07:00
Holly Borla
7512f6e366 [SIL] Add an assignment mode to AssignOrInitInst that will be set by DI. 2023-06-06 18:59:13 -07:00
Holly Borla
eb3ed2b374 [SILGen] Emit AssignOrInitInst for assignment to a computed property with
an init accessor inside of an initializer.
2023-06-06 18:59:13 -07:00
Holly Borla
684ef9c482 [AST] Add a new accessor kind for init accessors. 2023-06-06 18:57:31 -07:00
Michael Gottesman
29672c503a Merge pull request #66381 from gottesmm/noimplicitcopy-borrow-consuming
[borrowing/consuming] Make borrowing and consuming parameters no implicit copy.
2023-06-06 21:41:18 -04:00
Michael Gottesman
59c8cff917 [borrowing] Add support for borrowing/consuming copyable types to be a noimplicitcopy type.
rdar://108383660
2023-06-06 18:12:29 -04:00
Doug Gregor
0caa4531a6 [Macros] Teach SILGen to visit declartions produced by freestanding macros
Fixes a crash where local variables introduced by a freestanding
declaration macro would not get SIL emitted for them, rdar://109721114.
2023-06-06 14:33:11 -07:00
Andrew Trick
2f200a6caa [move-only] Fix drop_deinit OSSA lowering
drop_deinit ultimately only affects the semantics of its
destroy_value. Avoid generating releases for destroys in which the
deinit has been dropped. Instead, individually release the members.
2023-06-06 09:17:53 -07:00
Andrew Trick
6b53365cd0 [move-only] Comment emitMoveOnlyMemberDestruction drop_deinit 2023-06-06 09:17:53 -07:00
Meghana Gupta
ed78fe06d7 Merge pull request #66304 from meg-gupta/fixcreateallocboxpr
Remove #ifndef on skipVarDeclAssert in SILBuilder::createAllocBox signature
2023-06-04 23:02:17 -07:00
Joe Groff
3582691e65 Merge pull request #66302 from jckarter/noncopyable-switch-require-consume
Require `switch` on a noncopyable-type binding to be explicitly `consumed`.
2023-06-03 12:08:26 -07:00
Joe Groff
359e045192 Require switch on a noncopyable-type binding to be explicitly consume-d.
Pattern matching as currently implemented is consuming, but that's not
necessarily what we want to be the default behavior when borrowing pattern
matching is implemented. When a binding of noncopyable type is pattern-matched,
require it to be annotated with the `consume` operator explicitly. That way,
when we introduce borrowing pattern matching later, we have the option to make
`switch x` do the right thing without subtly changing the behavior of existing
code. rdar://110073984
2023-06-02 18:14:37 -07:00
Meghana Gupta
f08720b04e Remove #ifndef on skipVarDeclAssert in SILBuilder::createAllocBox signature 2023-06-02 11:57:17 -07:00
Slava Pestov
fd81f8135d Merge pull request #66294 from slavapestov/pack-element-type-part-2
PackElementType changes, part 2
2023-06-02 07:32:54 -04:00
Slava Pestov
8a6fddfc74 Merge pull request #66290 from slavapestov/class-allocating-init-with-pack
SILGen: Fix emission of class allocating init with pack expansion parameters
2023-06-01 22:42:42 -04:00
Slava Pestov
f61d8c5573 SILGen: Simplify getSubstitutionsForPropertyInitializer() 2023-06-01 22:36:24 -04:00
Slava Pestov
a1582341a5 SILGen: Fix emission of class allocating init with pack expansion parameters
Fixes rdar://110099644.
2023-06-01 19:20:11 -04:00
Michael Gottesman
e09888923d Merge pull request #66255 from gottesmm/pr-ec03fa3a5af8403c86b22c7f8f8cf568642efa0a
[move-only] Fix class setters of address only move only types.
2023-06-01 15:07:29 -07:00
Adrian Prantl
958a1577b5 Ensure calls to getters have a source location.
Calls to getters are implicit because the compiler inserts them on a property
access, but the location is useful in backtraces so it should be preserved.

rdar://109123395
2023-05-31 17:41:26 -07:00
Michael Gottesman
42b4b9a652 [move-only] Fix class setters of address only move only types.
We were not properly propagating along cleanups in SILGenProlog. I went through
SILGenProlog and fixed this.

rdar://109287977
2023-05-31 14:24:40 -07:00
Michael Gottesman
1dfd8d6a88 Merge pull request #66094 from gottesmm/pr-9cc91f1b6b3ff60686adb825c4a444221c3d798a
[move-only] Teach SILGenApply how to emit subscripts with borrowed base values.
2023-05-28 19:25:34 -07:00
Michael Gottesman
8579c1924d [move-only] Make sure that we serialize deinits and if we are not able to see the value witness function, call it via the value witness function.
Some notes:

1. I put in both a swiftpm like test case and a library evolution test case. I
also updated the moveonly_deinit serialization swift test to show that we
actually serialize the deinit.

2. I changed when we emit the deinit table to only be when we have a type with
an actual value type destructor. Notably this doesn't include classes today so
as a side-effect, we no longer attempt to devirtualize moveonly class deinits.
This doesn't affect anything we are trying to actually do since we do not
support noncopyable classes today. With that in mind, I changed one test that
was showing that deinit devirtualization worked to use a struct with deinit
instead of a class.

rdar://109679168
2023-05-27 22:07:04 -07:00
Michael Gottesman
2b785e4a98 [move-only] Teach SILGen how to recognize a borrowed read subscript when the type also has a modify.
The form of the AST changes slightly when a type has a read and a modify.
Specifically, we now have a load on the subscript and an inout_expr on the base.
I dealt with this by making the inout_expr something that when we look for
storage we look through and by tweaking the load lookthrough code.
2023-05-26 15:31:35 -07:00
Michael Gottesman
7b66c70bda [move-only] Restrict 508bf8ae21 so it only applies if a subscript has a _read accessor.
We want the result of getters to still be separate values.
2023-05-26 12:34:53 -07:00
Michael Gottesman
5d9ab63dbb [move-only] When we emit a noncopyable temporary allocation, always attach a mark_must_check [consumable_and_assignable].
Most of the time SILGen already emits these correctly without having extra
copies, but in certain situations SILGen will emit copies that we need the move
checker to eliminate (e.x.: when we generate a yield). An additional benefit is
that this also will catch places where the frontend makes a mistake.

This also removes a bunch of "copy of noncopyable" types error that showed up in
the implicit compiler generated modify.
2023-05-25 14:57:55 -07:00
Michael Gottesman
508bf8ae21 [move-only] Teach SILGenApply how to emit subscripts with borrowed base values.
I also added a bunch of tests that showed the behavior of subscripts/other accessors with the following combinations of semantics:

1. get only.
2. get/set.
3. get/modify.
4. read/set.
5. read/modify.

rdar://109746476
2023-05-25 14:57:32 -07:00
Joe Groff
9db359985d Emit updated debug info when inout parameters are consumed and reinitialized.
Change SILGen to emit the `debug_value` instruction using the original inout
parameter address, instead of the `mark_must_check` inserted for move-only
parameters, because code in the MoveOnlyAddressChecker did not expect to
find the debug_value anywhere but on the original address. Update move-only
diagnostics so that they pick up the declaration name for a memory location
from any debug_value instruction if there are more than one. rdar://109740281
2023-05-24 12:15:53 -07:00
Michael Gottesman
767fdfc66f Merge pull request #66084 from gottesmm/pr-295915eed5caee4c789ab3a532ffac4379ba1778
[move-only] Fix emission of addressonly noncopyable setter new values.
2023-05-23 13:41:47 -07:00
Michael Gottesman
82c645db21 [move-only] Fix emission of addressonly noncopyable setter new values.
NOTE: This does not affect normal parameters since normal parameters that are
noncopyable never have default access semantics since the user is forced to
specify either borrow or consume. This is incontrast to implicit parameters like
the newValue of a setter.

rdar://109726282
2023-05-23 10:56:45 -07:00
Michael Gottesman
67fcb1c86e [consume-operator] Change consume so that its result is not considered an lvalue.
Before this change it was possible to:

1. Call mutating methods on a consume result.
2. assign into a consume (e.x.: var x = ...; (consume x) = value.

From an implementation perspective, this involved just taking the logic I
already used for the CopyExpr and reusing it for ConsumeExpr with some small
tweaks.

rdar://109479440
2023-05-17 22:42:42 -07:00
Michael Gottesman
fd25cf379a Rename MoveExpr -> ConsumeExpr to reflect the final name.
NFC.
2023-05-17 22:42:42 -07:00
Michael Gottesman
e6f1691122 [copy-operator] Add support for the copy operator in preparation for making consuming and borrowing no implicit copy.
Some notes:

1. I implemented this as a contextual keyword that can only apply directly to
lvalues. This ensures that we can still call functions called copy, define
variables named copy, etc. I added tests for both the c++ and swift-syntax based
parsers to validate this. So there shouldn't be any source breaks.

2. I did a little bit of type checker work to ensure that we do not treat
copy_expr's result as an lvalue. Otherwise, one could call mutating functions on
it or assign to it, which we do not want since the result of copy_value is

3. As expected, by creating a specific expr, I was able to have much greater
control of the SILGen codegen and thus eliminate extraneous copies and other
weirdness than if we used a function and had to go through SILGenApply.

rdar://101862423
2023-05-17 22:42:42 -07:00
Michael Gottesman
5b5b55f675 Merge pull request #65842 from gottesmm/more-resilient-stuff
[move-only] Batched resilient changes
2023-05-11 15:48:27 -07:00
Michael Gottesman
81a09b384e When using a store_borrow for borrowed things, make sure to only store_borrow if we are using lowered addresses.
When opaque values are enabled, we do not need this store_borrow. This was
caught by the following tests:

  Swift(macosx-x86_64) :: SILGen/opaque_values_silgen.swift
  Swift(macosx-x86_64) :: SILGen/opaque_values_silgen_resilient.swift
2023-05-11 12:44:04 -07:00
Michael Gottesman
59fdfaddd1 [move-only] Spill noncopyable types that are objects using store_borrow if we call a resilient function that takes it in_guaranteed.
This ensures that given a class that contains a noncopyable type that contains
another noncopyable type:

```
@_moveOnly struct S2 {}
@_moveOnly struct S { var s2: S2 }
class C { var s: S }
```

if we call a resilient function that takes C.S.S2:

```
borrowVal(c.s.s2)
```

we properly spill s2 onto the stack using a store_borrow.

Why Do This?
------------

Currently SILGenLValue treats ref_element_addr as a base that it needs to load
from for both copyable and non-copyable types. We keep a separation of concerns
and require emission of resilient functions to handle these loaded values. For
copyable types this means copying the value and storing it into a temporary
stack allocation. For noncopyable types, we never actually implemented this so
we would hit an error in SILGenApply telling us that our resilient function
expected an address argument, but we are passing an object.

To work around this, I updated how we emit borrowed lvalue arguments to in this
case to spill the value into a temporary allocation using a store_borrow. I also
included a test that validates that we properly have a read exclusivity scope
around the original loaded from memory for the entire call site so even though
we are performing a load_borrow and then spilling it, we still have read
exclusivity to the original memory for the entire region meaning that we still
preserve the semantics.

rdar://109171001
2023-05-10 14:12:34 -07:00
Michael Gottesman
12e1db4f1e [move-only] Ensure that resilient deinits take their self argument as an address argument instead of an object.
The signature was already correct, just the logic in the deinit code assumed
that we would always have an object.

rdar://109170069
2023-05-10 14:11:55 -07:00
Adrian Prantl
b74cdf19e8 Derive the SILDebugScope for a variable declaration from its owning ASTScope.
The previous code made the assumption that the ASTScope for a variable
declaration should be the one of the declaration's source location. That is not
necessarily the case, in some cases it should be an ancestor scope. This patch
introduces a map from ValueDecl -> ASTScope that is derived from querying each
ASTScope for its locals, which matches also what happens in name lookup.  This
patch also fixes the nesting of SILDebugScopes created for guard statement
bodies, which are incorrectly nested in the ASTScope hierarchy.

rdar://108940570
2023-05-10 12:29:42 -07:00
Kavon Farvardin
3e4bc82aa8 rename _forget to discard; deprecate _forget
SE-390 concluded with choosing the keyword discard rather than forget for
the statement that disables the deinit of a noncopyable type. This commit
adds parsing support for `discard self` and adds a deprecation warning for
`_forget self`.

rdar://108859077
2023-05-08 21:42:19 -07:00
Adrian Prantl
e0206daf01 Merge pull request #65741 from adrian-prantl/108618562
Fix SILDebugScopes for closures expanded from an expression macro.
2023-05-07 09:10:25 -07:00