Commit Graph

41 Commits

Author SHA1 Message Date
Doug Gregor
ed6dccf12c Diagnose captured of non-sendable metatypes crossing isolation boundaries
Keep track of all of the type parameters and archetypes that are captured
by a local function or closure. Use that information to diagnose cases
where a non-Sendable metatype crosses an isolation boundary.
2025-02-13 17:07:09 -08:00
Slava Pestov
52e908363f Sema: Collect PackElementExpr captures in CaptureInfoRequest 2024-05-30 16:39:49 -04:00
Slava Pestov
670391a6c1 AST: Store captured PackElementExprs in CaptureInfo 2024-05-30 14:10:20 -04:00
Slava Pestov
97d95218f8 AST: Record captured generic environments in CaptureInfo 2024-04-30 21:38:15 -04:00
Slava Pestov
55ff73f205 AST: Stronger assertions around CaptureInfo 2024-04-19 17:59:58 -04:00
Slava Pestov
d222470a0b AST: Remove unused methods from CaptureInfo 2024-04-18 22:47:23 -04:00
Michael Gottesman
a375692176 Wordsmith a comment. 2024-03-01 12:58:58 -08:00
Michael Gottesman
f48bd90a9b [ast] Fix a series of potential nullptr violations in CaptureInfo.
Specifically, CaptureInfo previously as a pattern in CaptureInfo.cpp would
attempt to ascertain if a CapturedValue was a local capture by checking the
CapturedValue's decl. Unfortunately, when we have captured dynamic self
metadata, we do not even have a getDecl() and should return false.

I fixed this by adding a new API to CaptureValue that checks if it has a decl
before checking if the decl is a local capture. This avoids this problem.
2024-03-01 12:51:04 -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
Michael Gottesman
4768d017b8 [ast] Hide CapturedValue(OpaqueValueExpr *) so that it only can be used by TypeLowering.
The reason why I am doing this is before this commit despite the fact that
CapturedValue was only used by TypeLowering, this constructor was exposed to the
entire rest of the compiler. This made it so that other code (like the
AbstractClosureExpr::getIsolationCrossing() that I added in the previous series
of commits) would have to handle that API even though there was nothing to
handle just in case someone added something in the future.

Rather than create such a burden on the rest of the compiler, in this commit, we
instead hide said constructor and make it only accessible from
TypeLowering. This creates a barrier from new uses appearing in AST and make it
reasonable for code in the AST that will never see things from TypeLowering
(like the ACE API I mentioned above) just assert on that case without needing to
worry about additional uses cropping in easily by mistake.
2024-01-26 13:13:03 -08:00
Evan Wilde
f3ff561c6f [NFC] add llvm namespace to Optional and None
This is phase-1 of switching from llvm::Optional to std::optional in the
next rebranch. llvm::Optional was removed from upstream LLVM, so we need
to migrate off rather soon. On Darwin, std::optional, and llvm::Optional
have the same layout, so we don't need to be as concerned about ABI
beyond the name mangling. `llvm::Optional` is only returned from one
function in
```
getStandardTypeSubst(StringRef TypeName,
                     bool allowConcurrencyManglings);
```
It's the return value, so it should not impact the mangling of the
function, and the layout is the same as `std::optional`, so it should be
mostly okay. This function doesn't appear to have users, and the ABI was
already broken 2 years ago for concurrency and no one seemed to notice
so this should be "okay".

I'm doing the migration incrementally so that folks working on main can
cherry-pick back to the release/5.9 branch. Once 5.9 is done and locked
away, then we can go through and finish the replacement. Since `None`
and `Optional` show up in contexts where they are not `llvm::None` and
`llvm::Optional`, I'm preparing the work now by going through and
removing the namespace unwrapping and making the `llvm` namespace
explicit. This should make it fairly mechanical to go through and
replace llvm::Optional with std::optional, and llvm::None with
std::nullopt. It's also a change that can be brought onto the
release/5.9 with minimal impact. This should be an NFC change.
2023-06-27 09:03:52 -07:00
Doug Gregor
3f5ee47db8 Consistently handle 'self' captures 2021-12-10 16:21:02 -08:00
Doug Gregor
13de9d6a5f Properly compute actor isolation of local functions with captures.
Non-Sendable local functions can be actor-isolated if they capture an
isolated parameter. Treat them as such. Fixes rdar://83090397.
2021-12-10 15:10:43 -08:00
Varun Gandhi
ea92df04e1 [NFC] Remove redundant includes for <vector>. 2020-05-31 13:05:02 -07:00
Slava Pestov
107154381b AST: Remove control characters from CaptureInfo.h 2019-12-03 20:51:52 -05:00
Brent Royal-Gordon
99faa033fc [NFC] Standardize dump() methods in frontend
By convention, most structs and classes in the Swift compiler include a `dump()` method which prints debugging information. This method is meant to be called only from the debugger, but this means they’re often unused and may be eliminated from optimized binaries. On the other hand, some parts of the compiler call `dump()` methods directly despite them being intended as a pure debugging aid. clang supports attributes which can be used to avoid these problems, but they’re used very inconsistently across the compiler.

This commit adds `SWIFT_DEBUG_DUMP` and `SWIFT_DEBUG_DUMPER(<name>(<params>))` macros to declare `dump()` methods with the appropriate set of attributes and adopts this macro throughout the frontend. It does not pervasively adopt this macro in SILGen, SILOptimizer, or IRGen; these components use `dump()` methods in a different way where they’re frequently called from debugging code. Nor does it adopt it in runtime components like swiftRuntime and swiftReflection, because I’m a bit worried about size.

Despite the large number of files and lines affected, this change is NFC.
2019-10-31 18:37:42 -07:00
Jordan Rose
27e881d97e [AST] Shrink CaptureInfo down to a PointerIntPair (#27261)
If there are any actual captures, a CaptureInfoStorage will be
allocated in the ASTContext, matching the previous behavior of
allocating the array of captures there.

No functionality change. I /tried/ to change the functionality to
assert everywhere capture info was used but hadn't explicitly been
computed, but it turns out we do that /all over the place/ for any
declaration that's been synthesized, imported, or deserialized. I
left in some groundwork for someone to make this more explicit (or
requestify it) in the future.
2019-09-20 17:01:44 -07:00
Slava Pestov
aa6a55d9ad AST: Various getCaptureInfo() methods are now const; add setCaptureInfo() methods 2019-06-28 21:53:44 -04:00
Slava Pestov
62a3d1c81a Sema: Track source locations for captures
This will be used to emit diagnostics in SILGen when a value is
captured before being defined.
2019-06-27 20:59:22 -04:00
Slava Pestov
397b620501 AST: CapturedValue is never used as a DenseMap key 2019-06-27 20:59:22 -04:00
Doug Gregor
2e9f8cf981 Capture a placeholder opaque value expression when needed. 2019-04-23 11:32:28 -07:00
Adrian Prantl
ff63eaea6f Remove \brief commands from doxygen comments.
We've been running doxygen with the autobrief option for a couple of
years now. This makes the \brief markers into our comments
redundant. Since they are a visual distraction and we don't want to
encourage more \brief markers in new code either, this patch removes
them all.

Patch produced by

      for i in $(git grep -l '\\brief'); do perl -pi -e 's/\\brief //g' $i & done
2018-12-04 15:45:04 -08:00
gregomni
16a0fe7167 Move merging of CapturedValue flags into the class itself so that it'll be more clear that dealing with this will be required if more flags are added in the future. And mergeFlags correctly on the dynamic self capture. 2018-08-06 11:41:28 -07:00
practicalswift
6d1ae2a39c [gardening] 2016 → 2017 2017-01-06 16:41:22 +01:00
practicalswift
797b80765f [gardening] Use the correct base URL (https://swift.org) in references to the Swift website
Remove all references to the old non-TLS enabled base URL (http://swift.org)
2016-11-20 17:36:03 +01:00
Slava Pestov
c870052520 Sema: Give capture analysis ability to sniff out uses of dynamic 'Self'
This also adds some tests for the existing generic parameter
capture logic, which was only tested as part of SILGen tests
until now.

Also, move capture analysis into a new TypeCheckCaptures.cpp file.
2016-06-27 18:37:52 -07:00
Jordan Rose
91b72d3802 Fold the rest of PointerLikeTypeTraitsFwdDecl.h into TypeAlignments.h.
TypeAlignments.h predates this whole mess; it was used for types with
stronger alignment in PointerLikeTypeTraits than the old default of
"2 by fiat and assumption". All remaining forward-declared types are
AST types, so fold them into TypeAlignments.h.

(The one exception is SILTypeList.h, but that's already gone on master.)

To avoid future ODR issues, explicitly include TypeAlignments.h into
every header that defines a type it forward-declares.

I wish we could use partial specialization to provide PointerLikeTypeTraits
for all derived classes of Decl, TypeBase, etc, but that's not something
you can do in C++ if you don't control the traits class.
2016-02-06 11:22:28 -08:00
Michael Gottesman
3556db7484 Add in explicit specializations for forward declared pointers that we use. All of the explicit specializations are in the new header "PointerLikeTypeTraitsFwdDecl.h" 2016-02-06 11:22:28 -08:00
Michael Gottesman
30c5e8702d Respond to changes in SetVector in r253439. 2016-02-06 11:22:25 -08:00
Zach Panzarino
e3a4147ac9 Update copyright date 2015-12-31 23:28:40 +00:00
Slava Pestov
533f42dd2f Closures and local functions only capture generic parameters if necessary
The CaptureInfo computed by Sema now records if the body of the
function uses any generic parameters from the outer context.
SIL type lowering only adds a generic signature if this is the
case, instead of unconditionally.

This might yield a marginal performance improvement in some cases,
but more interestingly will allow @convention(c) conversions from
generic context.

Swift SVN r32161
2015-09-22 21:08:28 +00:00
Jordan Rose
772a124b63 Compute captures after all bodies have been type-checked, as a separate pass.
We already have the restriction that captures can't be computed until
everything is type-checked, but previously we tried to compute captures
/immediately/ after a closure was type-checked. Unfortunately, we either
type-checked too early (before @noescape was propagated onto multi-statement
closures) or too late (trying to compute autoclosure captures at the point
the autoclosure was introduced).

Now, all closure captures are computed after type-checking, and local
function captures as well. They also more consistently reuse the capture
list of nested closures/functions. Because captures can be computed on
demand, there is now a flag on CaptureInfo for whether we've computed
captures yet. Note that some functions will never have captures computed,
namely those that are not in a local context.

rdar://problem/19956242

Swift SVN r25776
2015-03-05 02:33:37 +00:00
Ted Kremenek
9e7471d68b Revert "Revert "Rework processing of @noescape closures a bit to fix rdar://19981118:""
Turns out llvm::DataLayoutPass is used in other places, so the bots are still unhappy.
Re-applying the original change so we can fix the problem holistically.

Swift SVN r25761
2015-03-04 20:15:45 +00:00
Ted Kremenek
6781ac7bec Revert "Rework processing of @noescape closures a bit to fix rdar://19981118:"
This is breaking the testing bot because DataLayoutPass was just removed from LLVM trunk.
Chris is the best one to fix this change, but we need to get the bots green.

Swift SVN r25760
2015-03-04 20:03:33 +00:00
Chris Lattner
ab43e444d6 Rework processing of @noescape closures a bit to fix rdar://19981118:
- Have Sema, not SILGen decide if a vardecl can be captured by address
  instead of by-box.  This is a non-local property that is best computed
  during capture set formation.  Sema captures this as a bit on the new
  CapturedValue entry.
- Rework some diagnostic emission to centralize a class of noescape 
  diagnostics in capture set calculation.  Previously, funcdecl closures
  produced their diagnostics there, but ClosureExprs produced them in
  MiscDiagnostics (NFC for this part).

This fixes <rdar://problem/19981118> Swift 1.2 beta 2: Closures nested in @noescape closures copy, rather than reference, captured vars.



Swift SVN r25759
2015-03-04 19:30:36 +00:00
Chris Lattner
3489840b8d rework our representation of capture lists to compute the "isDirect" bit up front
when computing the list.  This simplifies getLocalCaptures to *just* filter out
global captures, and paves the way for other enhancements.  NFC.


Swift SVN r25739
2015-03-04 01:56:48 +00:00
Chris Lattner
58e0efc345 Enhance getDeclCaptureKind() to take the closure that is capturing the
given decl, and plumb the information down to it.  NFC since the argument
is currently dead.



Swift SVN r24132
2014-12-23 23:08:38 +00:00
Chris Lattner
bc005219d1 Implement most of support for properties in function scope, part of rdar://15922884.
Most of the complexity here is teaching SILGen how to handle closed-over direct 
accesses to observing properties, since all of the getter/setter/willSet/didSet
members of the property are actually full closures when in a function body.

We generate correct but really terrible code here, since the setter captures the
willset/didset members themselves.  I'm not worrying about the performance of 
this construct though, functionality is what matters.


Swift SVN r13778
2014-02-11 06:23:14 +00:00
Chris Lattner
74bf9c98c6 change the CaptureInfo::getLocalCaptures() method to return a SmallVector
by reference, avoiding allocating heap memory.



Swift SVN r8275
2013-09-16 15:25:48 +00:00
Dmitri Hrybenko
5ac875e5dc Add CaptureInfo class that manages capture lists
Swift SVN r8234
2013-09-14 00:22:44 +00:00