Commit Graph

901 Commits

Author SHA1 Message Date
Holly Borla
66e85721cb Merge pull request #30807 from hborla/property-wrapper-refactoring
[Property Wrappers] Refactor property wrappers so the synthesized backing init is only type checked once
2020-04-14 10:48:22 -07:00
Holly Borla
65105f3a26 [Property Wrappers] Introduce a new Expr node for the property wrapper
wrapped value placeholder in an init(wrappedValue:) call that was previously
injected as an OpaqueValueExpr. This commit also restores the old design of
OpaqueValueExpr.
2020-04-09 16:00:57 -07:00
Pavel Yaskevich
3accb65a5c [AST] Track whether key path expression has a leading dot
This is going to be useful to detect whether contextual root
is really expected during key path resolution in Sema.
2020-04-07 16:16:34 -07:00
Holly Borla
d22b984a93 [Expr] Allow OpaqueValueExpr to store an underlying expression 2020-04-03 13:47:56 -07:00
Robert Widmann
92c8a65f09 Drop references to name binding as a phase
A lot of places appear to mean "name lookup".  A few places meant "import resolution".
2020-03-29 18:51:09 -07:00
marcrasi
f6562d3a67 [AutoDiff upstream] differentiable function conversion pipeline (#30660)
Add the `@differentiable` function conversion pipeline:

- New expressions that convert between `@differentiable`,
  `@differentiable(linear)`, and non-`@differentiable` functions:
  - `DifferentiableFunction`
  - `LinearFunction`
  - `DifferentiableFunctionExtractOriginal`
  - `LinearFunctionExtractOriginal`
  - `LinearToDifferentiableFunction`
- All the AST handling (e.g. printing) necessary for those expressions.
- SILGen for those expressions.
- CSApply code that inserts these expressions to implicitly convert between
  the various function types.
- Sema tests for the implicit conversions.
- SILGen tests for the SILGen of these expressions.

Resolves TF-833.
2020-03-27 01:27:39 -07:00
Nathan Hawes
a368434432 [SourceKit/CodeFormat] Re-work and improve the indentation implementation.
This restructures the indentation logic around producing a single IndentContext
for the line being indented. An IndentContext has:
- a ContextLoc, which points to a source location to indent relative to,
- a Kind, indicating whether we should align with that location exactly, or
  with the start of the content on its containing line, and
- an IndentLevel with the relative number of levels to indent by.

It also improves the handling of:
- chained and nested parens, braces, square brackets and angle brackets, and
  how those interact with the exact alignment of parameters, call arguments,
  and tuple, array and dictionary elements.
- Indenting to the correct level after an incomplete expression, statement or
  decl.

Resolves:
rdar://problem/59135010
rdar://problem/25519439
rdar://problem/50137394
rdar://problem/48410444
rdar://problem/48643521
rdar://problem/42171947
rdar://problem/40130724
rdar://problem/41405163
rdar://problem/39367027
rdar://problem/36332430
rdar://problem/34464828
rdar://problem/33113738
rdar://problem/32314354
rdar://problem/30106520
rdar://problem/29773848
rdar://problem/27301544
rdar://problem/27776466
rdar://problem/27230819
rdar://problem/25490868
rdar://problem/23482354
rdar://problem/20193017
rdar://problem/47117735
rdar://problem/55950781
rdar://problem/55939440
rdar://problem/53247352
rdar://problem/54326612
rdar://problem/53131527
rdar://problem/48399673
rdar://problem/51361639
rdar://problem/58285950
rdar://problem/58286076
rdar://problem/53828204
rdar://problem/58286182
rdar://problem/58504167
rdar://problem/58286327
rdar://problem/53828026
rdar://problem/57623821
rdar://problem/56965360
rdar://problem/54470937
rdar://problem/55580761
rdar://problem/46928002
rdar://problem/35807378
rdar://problem/39397252
rdar://problem/26692035
rdar://problem/33760223
rdar://problem/48934744
rdar://problem/43315903
rdar://problem/24630624
2020-03-10 21:04:21 -07:00
Slava Pestov
90ee606de7 Sema: Refactor CSApply in preparation for curry thunks 2020-02-26 23:10:07 -05:00
Slava Pestov
783ea28f1a AST: Change AutoClosureExpr::isThunk() to getThunkType() 2020-02-26 23:09:54 -05:00
Brent Royal-Gordon
c504eb1b0a Warn if magic identifiers don’t match
When wrapping a function which is supposed to capture the caller’s location, there’s always a risk that the wrapper won’t capture the information the wrapped function wants; for instance, you might pass `(…, line, column)` where the callee expected `(…, column, line)`.

This commit emits a warning when a call passes an explicit argument to something that has a default argument, and that explicit argument is itself a parameter with a default argument, and both parameters use magic identifiers, but they use *different* magic identifiers.  This is partially in support of concise #file, but applies to all magic identifiers.

Fixes rdar://problem/58588633.
2020-01-21 14:28:16 -08:00
Brent Royal-Gordon
6c99bdac1e [NFC] Extract helper for making magic identifier strings 2020-01-21 14:28:16 -08:00
Doug Gregor
9853926cb2 [Function builders] Make sure we contextualize closures with builders applied.
We used to get this contextualization "for free" because closures that
had function builders applied to them would get translated into
single-expression closures. Now, we need to check for this explicitly.
2020-01-16 13:19:21 -08:00
Doug Gregor
c5a655e35b [Type checker] Fold more for-each type checking into the constraint solver.
The type checking of the for-each loop is split between the constraint
solver (which does most of the work) and the statement checker (which
updates the for-each loop AST). Move more of the work into the constraint
solver proper, so that the AST updates can happen in one place, making use
of the solution produced by the solver. This allows a few things, some of
which are short-term gains and others that are more future-facing:

* `TypeChecker::convertToType` has been removed, because we can now either
use the more general `typeCheckExpression` entry point or perform the
appropriate operation within the constraint system.
* Solving the constraint system ensures that everything related to the
for-each loop full checks out
* Additional refactoring will make it easier for the for-each loop to be
checked as part of a larger constraint system, e.g., for processing entire
closures or function bodies (that’s the futurist bit).
2020-01-02 09:55:39 -08:00
Slava Pestov
3149d6d62c IDE: Preparations for Sema building curry thunks 2019-12-19 23:51:53 -05:00
Frederick Kellison-Linn
71697c37ca Allow implicit self in escaping closures when self usage is unlikely to cause cycle (#23934)
* WIP implementation

* Cleanup implementation

* Install backedge rather than storing array reference

* Add diagnostics

* Add missing parameter to ResultFinderForTypeContext constructor

* Fix tests for correct fix-it language

* Change to solution without backedge, change lookup behavior

* Improve diagnostics for weak captures and captures under different names

* Remove ghosts of implementations past

* Address review comments

* Reorder member variable initialization

* Fix typos

* Exclude value types from explicit self requirements

* Add tests

* Add implementation for AST lookup

* Add tests

* Begin addressing review comments

* Re-enable AST scope lookup

* Add fixme

* Pull fix-its into a separate function

* Remove capturedSelfContext tracking from type property initializers

* Add const specifiers to arguments

* Address review comments

* Fix string literals

* Refactor implicit self diagnostics

* Add comment

* Remove trailing whitespace

* Add tests for capture list across multiple lines

* Add additional test

* Fix typo

* Remove use of ?: to fix linux build

* Remove second use of ?:

* Rework logic for finding nested self contexts
2019-12-20 02:38:41 +00:00
Robert Widmann
84ff97ac68 [NFC] Drop a dead overload of DynamicSubscriptExpr::create 2019-12-16 11:28:14 -08:00
Slava Pestov
7626f9de4f AST: Small cleanups
- Allow AbstractClosureExpr to be created with null body
- Split up ParameterList::CloneFlags::Inherited
2019-12-13 15:39:47 -05:00
Brent Royal-Gordon
6a8598a99c [NFC] Remove DeclNameRef staging calls 2019-12-11 00:55:18 -08:00
Brent Royal-Gordon
addbe3e5ed [NFC] Thread DeclNameRef through most of the compiler
This huge commit contains as many of the mechanical changes as possible.
2019-12-11 00:55:18 -08:00
Brent Royal-Gordon
1df792ae9f [NFC] Convert TypeRepr to use DeclName(Loc)?
Replaces `ComponentIdentTypeRepr::getIdentifier()` and `getIdLoc()` with `getNameRef()` and `getNameLoc()`, which use `DeclName` and `DeclNameRef` respectively.
2019-12-11 00:45:08 -08:00
Brent Royal-Gordon
a3035eb925 [NFC] Add helpers for unresolved AST node synthesis
This change adds UnresolvedDotExpr::createImplicit() and UnresolvedDeclRefExpr::createImplicit() helpers. These calls simplify several tedious bits of code synthesis that would otherwise become even more tedious with DeclNameRef in the picture.
2019-12-11 00:45:07 -08:00
Brent Royal-Gordon
63ec1cf5af Introduce a separate #filePath, remove -pound-file
This makes the path behavior more first-class. The feature is now hidden behind an experimental flag, -enable-experimental-concise-pound-file.
2019-12-04 16:35:13 -08:00
Hamish Knight
7e788b8ab9 [AST] Remove CallerDefaultArgumentExpr
Now that we use DefaultArgumentExpr for both kinds
of default arguments, this is no longer needed.
2019-11-20 15:07:33 -08:00
Hamish Knight
c667d2b361 Use DefaultArgumentExpr for caller-side defaults
This commit changes how we represent caller-side
default arguments within the AST. Instead of
directly inserting them into the call-site, use
a DefaultArgumentExpr to refer to them indirectly.

The main goal of this change is to make it such
that the expression type-checker no longer cares
about the difference between caller-side and
callee-side default arguments. In particular, it
no longer cares about whether a caller-side
default argument is well-formed when type-checking
an apply. This is important because any
conversions introduced by the default argument
shouldn't affect the score of the resulting
solution.

Instead, caller-side defaults are now lazily
type-checked when we want to emit them in SILGen.
This is done through introducing a request, and
adjusting the logic in SILGen to be more lenient
with ErrorExprs. Caller-side defaults in primary
files are still also currently checked as a part
of the declaration by `checkDefaultArguments`.

Resolves SR-11085.
Resolves rdar://problem/56144412.
2019-11-20 15:07:32 -08:00
Doug Gregor
24b47b5673 [Constraint solver] Sink Expr::getPreorderIndexMap() into its client. 2019-11-14 20:33:00 -08:00
Doug Gregor
01f203a54d [Constraint system] Sink Expr::getDepthMap() into its one client.
NFC. It'll be easier to refactor this when it's not an API on Expr.
2019-11-14 15:02:58 -08:00
Robert Widmann
301e042121 Add PreCheckFunctionBuilderRequest
Drop a cache out of the type checker.
2019-11-10 14:18:54 -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
Jordan Rose
8ff1dac381 [AST] Break some header dependencies for faster rebuilds (#27374)
DiagnosticEngine.h no longer depends on Attr.h.
Expr.h no longer depends on TypeRepr.h.

No functionality change.
2019-09-26 09:17:10 -07:00
David Ungar
0dbf7e6ab6 Parser changes for lazy ASTScopes 2019-08-21 09:45:45 -07:00
Xi Ge
0a4cdac2b7 AST: fix another issue of exponentially calculating source location. rdar://54276140 2019-08-16 17:02:00 -07:00
Pavel Yaskevich
b26272f191 Merge pull request #26606 from theblixguy/fix/SR-11016
[Typechecker] Fix fix-it location for missing try when called on optional protocol value
2019-08-16 12:18:53 -07:00
Suyash Srijan
5795f88ad4 [Sema] Change OpaqueValueExpr to take a SourceRange instead of just a single SourceLoc 2019-08-16 18:31:58 +01:00
Doug Gregor
3c69f6a305 [Constraint solver] Introduce one-way constraints.
Introduce the notion of "one-way" binding constraints of the form

  $T0 one-way bind to $T1

which treats the type variables $T0 and $T1 as independent up until
the point where $T1 simplifies down to a concrete type, at which point
$T0 will be bound to that concrete type. $T0 won't be bound in any
other way, so type information ends up being propagated right-to-left,
only. This allows a constraint system to be broken up in more
components that are solved independently. Specifically, the connected
components algorithm now proceeds as follows:

1. Compute connected components, excluding one-way constraints from
consideration.
2. Compute a directed graph amongst the components using only the
one-way constraints, where an edge A -> B indicates that the type
variables in component A need to be solved before those in component
B.
3. Using the directed graph, compute the set of components that need
to be solved before a given component.

To utilize this, implement a new kind of solver step that handles the
propagation of partial solutions across one-way constraints. This
introduces a new kind of "split" within a connected component, where
we collect each combination of partial solutions for the input
components and (separately) try to solve the constraints in this
component. Any correct solution from any of these attempts will then
be recorded as a (partial) solution for this component.

For example, consider:

  let _: Int8 = b ? Builtin.one_way(int8Or16(17)) :
  Builtin.one_way(int8Or16(42\
))

where int8Or16 is overloaded with types `(Int8) -> Int8` and
`(Int16) -> Int16`. There are two one-way components (`int8Or16(17)`)
and (`int8Or16(42)`), each of which can produce a value of type `Int8`
or `Int16`. Those two components will be solved independently, and the
partial solutions for each will be fed into the component that
evaluates the ternary operator. There are four ways to attempt that
evaluation:

```
  [Int8, Int8]
  [Int8, Int16]
  [Int16, Int8]
  [Int16, Int16]

To test this, introduce a new expression builtin `Builtin.one_way(x)` that
introduces a one-way expression constraint binding the result of the
expression 'x'. The builtin is meant to be used for testing purposes,
and the one-way constraint expression itself can be synthesized by the
type checker to introduce one-way constraints later on.

Of these two, there are only two (partial) solutions that can work at
all, because the types in the ternary operator need a common
supertype:

  [Int8, Int8]
  [Int16, Int16]

Therefore, these are the partial solutions that will be considered the
results of the component containing the ternary expression. Note that
only one of them meets the final constraint (convertibility to
`Int8`), so the expression is well-formed.

Part of rdar://problem/50150793.
2019-08-13 11:48:42 -07:00
Greg Titus
9107626ce8 Added comment explaining additional use of AutoClosureExpr. 2019-08-07 07:13:51 -07:00
Xi Ge
d9c4a77fb5 AST: avoid unnecessarily calling getStartLoc() to avoid exponential growth of the stack trace
Issuing multiple getStartLoc() from sub-expression can exponentially grow the stack trace.
When the expression under analysis is complex enough, this could be a user-noticeable hang.
This patch fixes UnresolvedDotExpr::getStartLoc() by 'refactoring' the result of SubExpr->getStartLoc()
to a local variable.

rdar://52982457
2019-07-23 16:22:55 -07:00
pschuh
26c4cccb4b Convert InterpolatedStringLiteralExpr to not use tc.callWitness(). (#26076)
For reference, all other callers of callWitness have been migrated.
2019-07-16 10:24:45 -07:00
Slava Pestov
5022d8be5d AST: Remove LiteralExpr::shallowClone()
At one point it was used to work around problems with re-type checking
literals in CSApply, but all of our tests appear to pass with this removed.
2019-07-12 19:07:46 -04:00
Slava Pestov
366f758d3c AST: Remove ObjectLiteralExpr::{get,set}SemanticExpr() 2019-07-12 14:10:47 -04: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
8ea650043d Merge pull request #25408 from pschuh/s-8
Convert DictionaryExpr to not use tc.callWitness() or generate a SemanticExpr.
2019-06-17 18:01:54 -04:00
David Ungar
06527db4ab Merge pull request #24594 from davidungar/A5-7-ASTOOScope-rebased
[NameLookup] ASTOOScope ontology
2019-06-14 08:03:09 -07:00
Parker Schuh
0b03721ce8 Convert DictionaryExpr to not use tc.callWitness() or generate a SemanticExpr.
For reference, everything else except string interpolation has been
migrated to this new form.
2019-06-13 15:58:03 -07:00
Parker Schuh
823ba0bb73 Convert ObjectLiteralExpr to not use tc.callWitness() or generate a SemanticExpr.
For reference, all other literal types except dictionaries and string
interpolation have been converted over to this form.
2019-06-11 02:47:32 -07:00
David Ungar
80ccbcf291 Simpler trailing angle bracket. 2019-05-29 13:02:42 -07:00
David Ungar
ed097f865c Compute instead of store trailing angle bracket location. 2019-05-29 11:50:11 -07:00
David Ungar
6164f5bd2a Add TrailingAngleBrackLoc to EditorPlaceholder for upcoming scope code. 2019-05-28 19:47:12 -07:00
David Ungar
663760e3b7 ASTOOScope ontology 2019-05-28 10:48:22 -07:00