Commit Graph

61 Commits

Author SHA1 Message Date
Slava Pestov
4c5bda2465 AST: Don't trigger source location deserialization in request evaluator
Deserializing a source location will compute the declaration's USR, which
might trigger another request cycle, etc.

Tentative fix for rdar://problem/80546848, but I don't have a test case.
2021-07-19 21:45:21 -04:00
Holly Borla
a15f4233a2 [Property Wrappers] Implement implementation-detail property wrappers for
parameters.
2021-03-31 09:29:46 -07:00
Doug Gregor
52096a640e SE-0302: Rename ConcurrentValue/@concurrent to Sendable/@Sendable. 2021-03-18 23:48:21 -07:00
Slava Pestov
b6381d4549 Sema: Ban forward references of captured values from 'defer' body
While 'defer' is implemented as a local function, it doesn't
behave as one. In particular, since SILGen runs it after
destroying all local bindings that appear after the 'defer'
definition, the body of a 'defer' cannot forward reference
captured bindings the way that local functions can.

Note that I had to remove a SILGen test case for an older,
related issue. The new diagnostic in Sema catches these cases
earlier.

Fixes rdar://problem/75088379.
2021-03-15 17:17:30 -04:00
Holly Borla
31f7b1ac75 [ConstraintSystem] Change the type of an unapplied function reference
when it has property wrapper parameters.

The property wrapper type will be replaced with either the wrapped-value
or projected-value type, depending on the argument label/parameter name,
and CSApply will build a thunk to construct the property wrapper and call
the function.
2021-02-25 18:35:13 -08:00
Doug Gregor
a554ad632b [Concurrency] Diagnose mutating accesses to locals from concurrent code.
Replace the existing warning about any access to a local variable from
concurrently-executing code with a more tailored error:
concurrently-executing code may read a mutable varable, but cannot
modify it. This is safe so long as we either always do by-value
captures in concurrent closures or we ensure that no mutation of that
variable can occur after the point of capture.

We'll follow up with one of those. For now... be careful out there.

Since we're promoting this to an error, narrow it down to concurrent
closures and local functions, dropping the assumption that escaping
closures "may execute concurrently."
2021-01-29 14:27:24 -08:00
Hamish Knight
a7a8d9fd26 [AST] Remove NewBodyKind default
A bunch of callers were accidentally misusing it.
2020-09-04 13:24:34 -07:00
Doug Gregor
cc17aef8c5 [Statement checker] Remove redundant state.
Remove the `TheFunc` state, which is used only in a few places and can
be trivially recomputed.
2020-08-02 10:29:42 -07:00
Doug Gregor
7eae5f2f0d [AST] Remove now-unused ClosureExpr::setSingleExpressionBody(). 2020-06-04 23:24:06 -07:00
Doug Gregor
af048d6953 [Constraint system] Move solution application for closures to CSClosure.
Slim down CSApply.cpp by moving the logic for applying a solution to a
closure into CSClosure.cpp. Also, eliminate duplicated logic for applying
function builders to the body of a closure or function. This should
not change semantics at all.
2020-05-30 22:44:30 -07:00
Doug Gregor
ac24491dae [Constraint system] Generalize function builder application APIs.
Teach the constraint system to handle matching a function builder to a
function as well as a closure.
2020-01-08 16:19:44 -08:00
Doug Gregor
fffe1291fa [Constraint system] Generalize record of transformed function builders. 2020-01-06 11:39:11 -08: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
Hamish Knight
9061d3da71 CaptureInfo no longer needs to be passed by reference
Now that it's only a word in size, it can be passed
by value.
2019-10-13 12:10:26 -07:00
Robert Widmann
5a8d0744c3 [NFC] Adopt TypeBase-isms for GenericSignature
Structurally prevent a number of common anti-patterns involving generic
signatures by separating the interface into GenericSignature and the
implementation into GenericSignatureBase.  In particular, this allows
the comparison operators to be deleted which forces callers to
canonicalize the signature or ask to compare pointers explicitly.
2019-09-30 14:04:36 -07:00
nate-chandler
5cddc40335 Diagnose missing expr in non-void single-expr func. [52025782] (#25802)
* Diagnose missing expr in non-void single-expr func. [52025782]

After SE-0255, compiling

```
func foo() -> Int {
    return
}
```

would result in a diagnostic without source location:

```
<unknown>:0: error: cannot convert return expression of type '()' to
```

Now, it results in

```
filename.swift:8:5: error: non-void function should return a value
    return
    ^
```

as it did prior to SE-0255.

To achieve that, during type checking for statements, when the
StmtChecker visits return statements, check whether we are within a
non-void-returning, single-expression function and that that single
expression is an implicit empty tuple.  (An empty implicit tuple is
added as the result of a resultless return statement that appears as the
only element in a single-expression function.)

To facilitate that, `hasSingleExpressionBody` and
`getSingleExpressionBody` was added to `AnyFunctionRef` and added
`getSingleExpressionBody` to `AbstractClosureExpr` (which already had
`hasSingleExpressionBody`).

rdar://problem/52025782
2019-07-02 09:17:37 -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
44142cdb62 SILGen: Diagnose captures before definitions
The new analysis simply checks if the captured value has been defined
yet; instead of asserting if it hasn't, we can now emit a diagnostic
using the source location tracked in the capture list.

Fixes <https://bugs.swift.org/browse/SR-4812>, <rdar://problem/40600800>.
2019-06-27 20:59:22 -04: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
John McCall
b80618fc80 Replace materializeForSet with the modify coroutine.
Most of this patch is just removing special cases for materializeForSet
or other fairly mechanical replacements.  Unfortunately, the rest is
still a fairly big change, and not one that can be easily split apart
because of the quite reasonable reliance on metaprogramming throughout
the compiler.  And, of course, there are a bunch of test updates that
have to be sync'ed with the actual change to code-generation.

This is SR-7134.
2018-08-27 03:24:43 -04:00
John McCall
1c99f31f20 Basic motions towards moving yields into AnyFunctionType.
Still not intending to do this anytime soon.
2018-08-16 02:13:54 -04: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
Slava Pestov
90cd772228 AST: Remove getParameterLists() and friends from AbstractFunctionDecl subclasses
Now, an AbstractFunctionDecl always stores a single parameter list.

Furthermore, ConstructorDecl and DestructorDecl always store a
ParamDecl for 'self'.

FuncDecl only has a 'self' if it is a member of a nominal type or
extension, so we tail-allocate the storage for it.
2018-07-22 20:56:56 -07:00
David Zarzycki
1958b8f4bf [AST] NFC: Simplify AnyFunctionRef::isKnownNoEscape()
This removes a defer body hack and an assumption that non-defer functions are always escaping.
2017-10-05 10:24:11 -04:00
Joe Groff
a7f23f5019 SIL: Canonicalize capture types with the AST function's generic signature.
When lowering closures, we avoid capturing the enclosing generic context when possible. However, the generic context may still be necessary to canonicalize types mentioned inside the closure, such as when an associated type is referred to that is same-typed to a concrete type. Fixes rdar://problem/30254048.
2017-02-03 12:08:57 -08:00
Hugh Bellamy
201ad5d594 Prefix COMPILER_IS_MSVC with SWIFT_ 2017-01-16 22:09:53 +00:00
Hugh Bellamy
cf777d04f5 Introduce and use COMPILER_IS_MSVC 2017-01-15 15:17:18 +00:00
Hugh Bellamy
7a5ef4bdd1 Support building swift/AST with MSVC on Windows 2017-01-09 09:05:06 +00:00
practicalswift
6d1ae2a39c [gardening] 2016 → 2017 2017-01-06 16:41:22 +01:00
Slava Pestov
fb0f372e94 AST: Move mapType{In,OutOf}Context() out of ArchetypeBuilder and clean up headers
- The DeclContext versions of these methods have equivalents
  on the DeclContext class; use them instead.

- The GenericEnvironment versions of these methods are now
  static methods on the GenericEnvironment class. Note that
  these are not made redundant by the instance methods on
  GenericEnvironment, since the static methods can also be
  called with a null GenericEnvironment, in which case they
  just assert that the type is fully concrete.

- Remove some unnecessary #includes of ArchetypeBuilder.h
  and GenericEnvironment.h. Now changes to these files
  result in a lot less recompilation.
2016-12-18 19:55:41 -08:00
Joe Groff
4444d83756 SIL: Lower captures to boxes with an appropriate generic context.
Officially kick SILBoxType over to be "nominal" in its layout, with generic layouts structurally parameterized only by formal types. Change SIL to lower a capture to a nongeneric box when possible, or a box capturing the enclosing generic context when necessary.
2016-12-16 20:23:25 -08:00
Slava Pestov
b4d11338ec AST: Push ValueDecl::{has,get,set}Type() down to VarDecl
After recent changes, this asserts on all decls that are not VarDecls,
so we can just enforce that statically now. Interestingly, this turns
up some dead code which would have asserted immediately if called.

Also, replace AnyFunctionRef::getType() with
AnyFunctionRef::getInterfaceType(), since the old
AnyFunctionRef::getType() would just assert when called on
a Decl.
2016-12-01 19:28:13 -08:00
Slava Pestov
1657060d1e AST: hasType() => hasInterfaceType() 2016-11-29 03:05:30 -07:00
Slava Pestov
f803d8f12c AST: Remove AbstractFunctionDecl::getBodyResultType()
This is made redundant by the existing getResultInterfaceType()
accessor.
2016-11-29 03:05:23 -07: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
Doug Gregor
50341da32b Use "TypeBase::hasError()" rather than "is<ErrorType>()" where needed.
In most places where we were checking "is<ErrorType>()", we now mean
"any error occurred". The few exceptions are in associated type
inference, code completion, and expression diagnostics, where we might
still work with partial errors.
2016-10-07 10:58:23 -07:00
Joe Groff
038c16be62 Contextualize capture list decls to enclosing autoclosures.
Without this fix, we would accidentally consider the autoclosure to capture the capture list decl, even though it's natively within the autoclosure's context. Fixes SR-848.
2016-04-27 13:51:06 -07:00
Joe Groff
f4765f676c Sema: Allow ObjC generic extensions to perform @objc operations involving generics.
Though the generic type information isn't present, it isn't necessary if we're just invoking other operations from Objective-C. This should allow an extension to use the generic class's own API to some degree, as it would if defined on the nongeneric form.
2016-04-16 08:27:32 -07:00
Chris Lattner
a30ae2bf55 Merge pull request #836 from zachpanz88/new-year
Update copyright date
2015-12-31 19:36:14 -08:00
Chris Lattner
7daaa22d93 Completely reimplement/redesign the AST representation of parameters.
Parameters (to methods, initializers, accessors, subscripts, etc) have always been represented
as Pattern's (of a particular sort), stemming from an early design direction that was abandoned.
Being built on top of patterns leads to patterns being overly complicated (e.g. tuple patterns
have to have varargs and default parameters) and make working on parameter lists complicated
and error prone.  This might have been ok in 2015, but there is no way we can live like this in
2016.

Instead of using Patterns, carve out a new ParameterList and Parameter type to represent all the
parameter specific stuff.  This simplifies many things and allows a lot of simplifications.
Unfortunately, I wasn't able to do this very incrementally, so this is a huge patch.  The good
news is that it erases a ton of code, and the technical debt that went with it.  Ignoring test
suite changes, we have:
   77 files changed, 2359 insertions(+), 3221 deletions(-)

This patch also makes a bunch of wierd things dead, but I'll sweep those out in follow-on
patches.

Fixes <rdar://problem/22846558> No code completions in Foo( when Foo has error type
Fixes <rdar://problem/24026538> Slight regression in generated header, which I filed to go with 3a23d75.

Fixes an overloading bug involving default arguments and curried functions (see the diff to
Constraints/diagnostics.swift, which we now correctly accept).

Fixes cases where problems with parameters would get emitted multiple times, e.g. in the
test/Parse/subscripting.swift testcase.

The source range for ParamDecl now includes its type, which permutes some of the IDE / SourceModel tests
(for the better, I think).

Eliminates the bogus "type annotation missing in pattern" error message when a type isn't
specified for a parameter (see test/decl/func/functions.swift).

This now consistently parenthesizes argument lists in function types, which leads to many diffs in the
SILGen tests among others.

This does break the "sibling indentation" test in SourceKit/CodeFormat/indent-sibling.swift, and
I haven't been able to figure it out.  Given that this is experimental functionality anyway,
I'm just XFAILing the test for now.  i'll look at it separately from this mongo diff.
2015-12-31 19:24:46 -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
Joe Groff
7ebe973bb4 AST: Preserve hack to consider 'defer' function bodies to be @noescape.
Using a func decl lost this, which leads to some undesirable behavior when mutating values in 'defer' blocks, such as losing inout deshadowing.

Swift SVN r30788
2015-07-30 05:28:09 +00:00
John McCall
bc3b47b98a Infer the return type of a closure to be () if it contains no
return statements, or a return statement with no operand.

Also, fix a special-case diagnostic about converting a return
expression to (1) only apply to converting the actual return
expression, not an arbitrary sub-expression, and (2) use the
actual operand and return types, not the drilled-down types
that caused the failure.

Swift SVN r30420
2015-07-20 21:52:18 +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
Joe Groff
94be8424ff Sema: Type checking for C function pointers.
Since a function pointer doesn't carry any context, we can only form a C function pointer from a static reference to a global function or a context-free local function or closure. (Or maybe a static function applied to its metatype, but that's not handled here yet.) As a placeholder for a to-be-bikeshedded surface syntax, let the existing @cc(cdecl) attribute appear in source text when the -enable-c-function-pointers frontend flag is passed.

Swift SVN r25308
2015-02-16 00:41:53 +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
Joe Groff
3e9d7c3076 Add accessors to AnyFunctionRef to the underlying function decl or closure expr.
Swift SVN r17280
2014-05-02 22:04:43 +00:00
Chris Lattner
108910c410 remove a now-dead function.
Swift SVN r13913
2014-02-14 18:13:28 +00:00
Chris Lattner
9ee7d99a46 switch a few things off of argument patterns, onto body patterns.
Swift SVN r13891
2014-02-14 01:33:14 +00:00