Commit Graph

17 Commits

Author SHA1 Message Date
Kavon Farvardin
906a4cbcdb silgen: allow borrow of subscript for noncopyables
If a subscript uses a read accessor to yield a noncopyable value,
we'd emit an `end_apply` that's too tightly scoped to allow for
a subsequent borrowing access on the yielded value.

resolves rdar://159079818
2025-08-29 13:30:31 -07: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
Joe Groff
c6f981bad0 SILGen: Make FormalEvaluationScope destructor tolerate running from unreachable code.
This can happen now in a borrowing `switch` that nonlocal exits on all paths.
2024-02-27 20:54:26 -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
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
John McCall
cbd2cd77d8 Strengthen assertions around cleanup and formal evaluation scopes.
I haven't actually tested this separately from the changes to scope
usage that are coming in the follow-up, but it's logically somewhat
separate.
2018-11-03 01:00:17 -04:00
John McCall
3162b513a9 Strengthen some assertions with cleanup scopes; NFC. 2018-10-25 21:50:00 -07:00
John McCall
7a4aeed570 Implement generalized accessors using yield-once coroutines.
For now, the accessors have been underscored as `_read` and `_modify`.
I'll prepare an evolution proposal for this feature which should allow
us to remove the underscores or, y'know, rename them to `purple` and
`lettuce`.

`_read` accessors do not make any effort yet to avoid copying the
value being yielded.  I'll work on it in follow-up patches.

Opaque accesses to properties and subscripts defined with `_modify`
accessors will use an inefficient `materializeForSet` pattern that
materializes the value to a temporary instead of accessing it in-place.
That will be fixed by migrating to `modify` over `materializeForSet`,
which is next up after the `read` optimizations.

SIL ownership verification doesn't pass yet for the test cases here
because of a general fault in SILGen where borrows can outlive their
borrowed value due to being cleaned up on the general cleanup stack
when the borrowed value is cleaned up on the formal-access stack.
Michael, Andy, and I discussed various ways to fix this, but it seems
clear to me that it's not in any way specific to coroutine accesses.

rdar://35399664
2018-07-23 18:59:58 -04:00
Michael Gottesman
3e730d4fa6 [silgen] Rename {was,}InWritebackScope => {was,}InFormalEvaluationScope. 2017-09-06 16:31:20 -07:00
Michael Gottesman
159fe76f93 [gardening] Silence a warning in release builds. (#8700) 2017-04-11 21:15:10 -04:00
Michael Gottesman
84f5fe6a48 [silgen] Create FormalEvaluationScope::verify().
This method verifies that all non-dead cleanup handles that a formal evaluation
scope would pop have not been popped yet.

The body of the function is if-defed out when asserts are disabled.

rdar://31313534
2017-03-30 13:20:50 -07:00
Michael Gottesman
e52eea2199 [silgen] When a formal evaluation scope is in an inout conversion scope, exit early when popping.
This fixes a logic error. Specifically: When we create a formal evaluation scope
in an inout conversion scope, we set it as if it was already popped. In the case
when we pop the scope by hand, this causes a "don't pop this scope twice" assert
to fire.
2017-03-17 19:00:28 -07:00
Michael Gottesman
68c581f729 [gardening] As per discussion, begin standardizing in SILGen that the SILGenFunction variable is passed around as SGF.
The reason that this is being done is that:

1. SILGenFunction is passed around all throughout SILGen, including in between
APIs some of which call the SILGenFunction variable SGF and others that call it
gen.
2. Thus when one is debugging code in SILGen, one wastes time figuring out what
the variable name of SILGenFunction is in the current frame.

I did not do this by hand. I did this by:

1. Grepping for "SILGenFunction &gen".
2. By hand inspecting that the match was truly a SILGenFunction &gen site.
3. If so, use libclang tooling to rename the variable to SGF.

So I did not update any use sites.
2017-03-11 23:38:17 -08:00
Michael Gottesman
03a926b256 [silgen] When emitting lvalue gets into temporaries, cleanup the temporaries as early as possible.
Previously, we were emitting these cleanups at the end of the lexical scope
instead of at the end of the formal evaluation scope. This change ensures that
we always emit the cleanup immediately at the end of the formal evaluation
scope.

Previously in most cases we got away with this due to the +0 self
hack. Basically we would emit a get for a self parameter and then immediately
use that self parameter as a guaranteed parameter. Then the hack would insert
the destroy value forwarding the lexical scope level cleanup at the same time.

rdar://29791263
2017-02-17 23:26:13 -08:00
Michael Gottesman
78a514cdb1 [silgen] Use the name FormalAccess to refer to state related to accesses in a single formal evaluation context. 2017-02-17 19:19:35 -08:00
Michael Gottesman
58c3959e90 [silgen] Remove ManagedBorrowedValue in favor of the usage of FormalEvaluationScopes.
rdar://29791263
2017-02-15 15:28:14 -08:00
Michael Gottesman
9747214e92 [silgen] Refactor WritebackScope functionality into a new FormalEvaluationScope.
As per John, WritebackScope was always an unfortunate name. Generally these
scopes are meant for formal evaluations of inout parameters. The cases that I am
interested in generalizing them to be used for are borrows of the base of a
class that will then be used as an lvalue.

This also eliminates the out of line vector of lvalue writebacks.

rdar://29791263
2017-02-14 13:15:23 -08:00