Commit Graph

937 Commits

Author SHA1 Message Date
Holly Borla
42020792c6 [Sema] Represent pack element references in pack expansion patterns as
PackElementExpr.
2022-12-14 20:45:52 -08:00
Holly Borla
f1fd9acb23 [AST] Add 'PackElementExpr' for explicit pack elements spelled with the 'each'
keyword.
2022-12-14 20:45:51 -08:00
Doug Gregor
b29fcb4e58 [Macros] Parse macro declarations fully, and treat them as normal declarations 2022-11-28 18:32:43 -08:00
Doug Gregor
787af41765 [Macros] Parse generic arguments in macro expansions. 2022-11-17 16:46:43 -08:00
Robert Widmann
91c262bcb7 Move TokenKinds.def.gyb to AST 2022-11-16 13:38:25 -08:00
Pavel Yaskevich
70072a07ba Merge pull request #62122 from LucianoPAlmeida/fix-literal-dictionary-int
[Sema] printConstExprValue should consider negative signal when print number literals
2022-11-16 08:59:23 -08:00
Doug Gregor
9b10b0bde2 [Macros] Add pretty stack traces for macro expansions. 2022-11-15 15:00:52 -08:00
Luciano Almeida
4fb0e1c1f1 [Sema] printConstExprValue should consider negative signal when print number literals 2022-11-15 18:50:26 -03:00
Holly Borla
7354ad30c3 [ConstraintSystem] Use opened element types for opaque values in pack expansions. 2022-10-24 18:11:00 -07:00
Holly Borla
7f43001682 [AST] Store opaque value bindings in PackExpansionExpr. 2022-10-22 13:41:48 -07:00
Richard Wei
56e7cce809 [Macros] Parse MacroExpansionExpr and MacroExpansionDecl
Introduce `MacroExpansionExpr` and `MacroExpansionDecl` and plumb it through. Parse them in roughly the same way we parse `ObjectLiteralExpr`.

The syntax is gated under `-enable-experimental-feature Macros`.
2022-10-21 01:50:35 -07:00
Slava Pestov
fff224e2e7 Sema: Fix opening an existential argument to a 'try self.init()' delegation
Fixes rdar://problem/101098777.
2022-10-18 12:10:24 -04:00
Slava Pestov
da8ae1d36d Sema: Remove PackExpr 2022-10-16 21:37:25 -04:00
Holly Borla
67fb143f0e [AST] Remove ReifyPackExpr. 2022-10-10 16:25:26 -07:00
Holly Borla
19688cc2b1 [AST] Add the skeleton for PackExpansionExpr. 2022-10-07 20:13:18 -07:00
Hamish Knight
bca941b152 [AST] NFC: Rename IfExpr -> TernaryExpr
This matches what we call it in SwiftSyntax, and
is just generally clearer.
2022-09-28 10:33:31 +01:00
Joe Groff
6463d96df0 Sema: Use AutoClosureExpr again in KeyPathExpr-as-function expansion
Remove the preallocated closure discriminator from KeyPathExpr and go back
to expanding them using an AutoClosureExpr inside of a CaptureListExpr now
that that's supported. This allows the discriminator to be assigned during
type checking without disturbing the indexing of explicit closure literals.
2022-09-23 10:26:18 -07:00
Joe Groff
a35dc481ee AST: Allow CaptureListExpr to hold any AbstractClosureExpr.
Semantically, the capture list binding behavior doesn't require the scope
to be an explicit closure, and forming new ClosureExprs during type-checking
is difficult because they have to have preassigned discriminators, unlike
AutoClosureExprs which get discriminators introduced by Sema itself. Allowing
CaptureListExpr to hold an AutoClosureExpr makes it easier to synthesize
CaptureListExprs during type checking, to represent expressions with closure
semantics that evaluate some parts of the expression eagerly in the surrounding
context.
2022-09-23 08:35:18 -07:00
Joe Groff
c6b6787f74 Sema: Simplify AST representation of key path literals as functions.
Previously, we would turn a key path literal like `\.foo` in function type
context into a double-wrapped closure like this:

```
  foo(\.x) // before type checking
  foo({ $kp$ in { $0[$kp$] } }(\.x)) // after type checking
```

in order to preserve the evaluation semantics of the key path literal. This
works but leads to some awkward raw SIL generated out of SILGen which misses
out on various SILGen peepholes and requires a fair number of passes to clean
up. The semantics can still be preserved with a single layer of closure, by
using a capture list:

```
  foo({[$kp$ = \.x] in $0[$kp$] }) // after type checking
```

which generates better natural code out of SILGen, and is also (IMO) easier
to understand on human inspection.

Changing the AST representation did lead to a change in code generation that
interfered with the efficacy of CapturePropagation of key path literals; for
key path literals used as nonescaping closures, a mark_dependence of the
nonescaping function value on the key path was left behind, leaving the key
path object alive. The dependence is severed by the specialization done in
the pass, so update the pass to eliminate the dependence.

Compared to the previous patch, this version removes the attempt to have
the type-checked function expression carry the noescape-ness of its context,
and allows for coerceToType to introduce a function conversion instead, since
that FunctionConversionExpr is apparently load-bearing for default argument
generators.
2022-09-22 12:00:22 -07:00
Hamish Knight
4716f61fba [AST] Introduce explicit actions for ASTWalker
Replace the use of bool and pointer returns for
`walkToXXXPre`/`walkToXXXPost`, and instead use
explicit actions such as `Action::Continue(E)`,
`Action::SkipChildren(E)`, and `Action::Stop()`.
There are also conditional variants, e.g
`Action::SkipChildrenIf`, `Action::VisitChildrenIf`,
and `Action::StopIf`.

There is still more work that can be done here, in
particular:

- SourceEntityWalker still needs to be migrated.
- Some uses of `return false` in pre-visitation
methods can likely now be replaced by
`Action::Stop`.
- We still use bool and pointer returns internally
within the ASTWalker traversal, which could likely
be improved.

But I'm leaving those as future work for now as
this patch is already large enough.
2022-09-13 10:35:29 +01:00
Alex Hoppen
442cf9bd4e [Sema] Add custom functions to ActorIsolationChecker to determine expr type and closure actor isolation
When we get rid of `LeaveClosureBodiesUnchecked` we no longer save closure types to the AST and thus also don’t save their actor isolation to the AST. Hence, we need to extract types and actor isolations of parent closures from the constraint system solution instead of the AST. This prepares `ActorIsolationChecker` to take custom functions to determine the type of an expression or the actor isolation of a closure.
2022-09-07 11:12:23 +02:00
Alex Hoppen
ec73a3a721 [AST] Move conversion of ClosureActorIsolation to ActorIsolation to its own function
Just a little bit of cleanup because this logic is really quite general and doesn't need to live in `getActorIsolationOfContext`.
2022-09-07 11:12:09 +02:00
Tony Allevato
9c84149b10 Merge pull request #60432 from allevato/index-expressible-by-literal
Index expressible-by-literal expressions.
2022-08-30 07:57:25 -07:00
Kavon Farvardin
6c24bc57cb [AST][SILGen] model ABI-safe casts of LValues
We needed a way to describe an ABI-safe cast of an address
representing an LValue to implement `@preconcurrency` and
its injection of casts during accesses of members.

This new AST node, `ABISafeConversionExpr` models what is
essentially an `unchecked_addr_cast` in SIL when accessing
the LVAlue.

As of now I simply implemented it and the verification of
the node for the concurrency needs to ensure that it's not
misused by accident. If it finds use outside of that,
feel free to update the verifier.
2022-08-29 20:58:26 -07:00
Tony Allevato
31d1b3a6d8 Index expressible-by-literal expressions.
When a value is initialized or coerced for a type that conforms to
one of the `ExpressibleBy*Literal` protocols (or
`ExpressibleByStringInterpolation`), this change records an implicit
call to the corresponding `init(...Literal:)` in the indexstore,
located at the beginning of the literal.
2022-08-27 20:06:13 -07:00
Luciano Almeida
1b8330778d Merge pull request #60479 from LucianoPAlmeida/dictionary-literal
[Sema] Warn about dictionary literal of dictionary type about duplicated keys
2022-08-16 08:30:49 -03:00
Luciano Almeida
1b7b6a33aa [NFC] Replace other usage of get literal expr description 2022-08-15 22:57:24 -03:00
Luciano Almeida
ee20fec382 [Sema] Warn about dictionary literal of dictionary type about duplicated keys 2022-08-15 22:54:55 -03:00
Hamish Knight
76931677a8 Merge pull request #60438 from hamishknight/factor-in-factor-out 2022-08-13 17:21:37 +01:00
Michael Gottesman
c10c0f6285 [move-function] Add a new context sensitive move expr.
It doesn't do anything yet.
2022-08-08 12:50:41 -07:00
Hamish Knight
6b9bcf3935 [AST] Change DotSyntaxCallExpr to take an Argument base
This allows us to more easily propagate inout
information to it, which will become a necessity
once InOutExpr is removed.
2022-08-08 15:11:00 +01:00
Pavel Yaskevich
3167182871 [AST] Add TypeJoin expression
This expression represents a variable and a set of expressions
that are type-checked together to form a type of the variable.
2022-08-02 11:03:27 -07:00
Doug Gregor
fdd619b2db Handle function conversions in concurrency checking. 2022-06-30 23:26:39 -07:00
Robert Widmann
9203d459b8 Add a test for a corner case missed by #41978.
A member reference to a function with a dynamic 'Self' result type
can introduce a covariant return expression into the AST. This is
exposed by the (already deeply cursed) -self method on NSObject(Protocol).
Add a regression test and said cursed member to the mock SDK.
2022-05-05 18:21:00 -07:00
Xi Ge
6206d7cc9d sema: accept an array literal of enum case references as compile-time constant 2022-03-24 21:44:09 -07:00
Robert Widmann
4efcb825f5 Unlock Opaque Types in Parameterized Protocols
Represent this in much the same way that collections do by creating an opaque value representing the source argument, and a conversion expression representing the destination argument.
2022-03-08 23:45:37 -08:00
Xi Ge
0992e97e5e ConstValues: record the values of const variable references in the side-car file 2022-02-21 19:22:25 -08:00
Xi Ge
d52714fce2 sema: keypath expression should be considered as compile-time constant 2022-02-18 20:50:54 -08:00
Richard Wei
6645f0d33f Merge pull request #40630 from rxwei/capture-inference 2021-12-22 14:44:34 -08:00
Richard Wei
1b3c0b7a73 [Regex] Infer capture types of regex literals.
When parsing a regular expression literal, accept a serialized capture structure from the regex parser. During type checking, decode it and form Swift types.

Examples:
```swift
'/(.)(.)/' // ==> `Regex<(Substring, Substring)>`
'/(?<label>.)(.)/' // ==> `Regex<(label: Substring, Substring)`
'/((.))*((.)?)/' //==> `Regex<([Substring], [Substring], Substring, Substring?)>`
```

Also:
- Fix a bug where a regex literal parsing error is not returning an error parser result.

Note:
- This needs to land after apple/swift-experimental-string-processing#92 and after `dev/4` tag has been created.
- See apple/swift-experimental-string-processing#92 for regex parser changes and the capture structure encoding.
- The `RegexLiteralParsingFn` `CaptureStructureOut` pointer type change from `char *` to `void *` will not break builds due to implicit pointer conversion (SE-0324) and unchanged ABI.

Resolves rdar://83253511.
2021-12-22 02:58:21 -08:00
Robert Widmann
e5bfda7c6e Merge pull request #40587 from CodaFi/substitute-teacher
Initial Semantics for Variadic Generics
2021-12-20 11:25:25 -08:00
Michael Ilseman
7bff9da67d Revert "Revert "Merge pull request #40595 from hamishknight/straw-bales"" 2021-12-19 10:08:48 -07:00
Arnold Schwaighofer
9511994e52 Revert "Merge pull request #40595 from hamishknight/straw-bales"
This reverts commit a67a0436f7, reversing
changes made to 9965df76d0.

This commit or the earlier commit this commit is based on (#40531) broke the
incremental bot.
2021-12-18 11:02:37 -08:00
Hamish Knight
128f5d4bc6 Update regex literal lexing and emission
Update the lexing implementation to defer to the
regex library, which will pass back the pointer
from to resume lexing, and update the emission to
call the new `Regex(_regexString:version:)`
overload, that will accept the regex string with
delimiters.

Because this uses the library's lexing
implementation, the delimiters are now `'/.../'`
and `'|...|'` instead of plain `'...'`.
2021-12-17 18:05:31 +00:00
Robert Widmann
8ac8633f9d [NFC] Hide VarargExpansionExpr's Constructor 2021-12-16 01:16:45 -08:00
Robert Widmann
3b66a31d5c Model Pack Expressions and Pack Reifications
Pack expressions take a series of argument values and bundle them together as a pack - much like how a tuple expression bundles argument expressions into a tuple.

Pack reification represents the operation that converts packs to tuples/scalar types in the AST. This is important since we want pack types in return positions to resolve to tuples contextually.
2021-12-16 00:39:33 -08:00
Doug Gregor
2b5bcc1062 Merge pull request #40544 from DougGregor/sr-15131-closure-effects
Extend "uses concurrency features" checks for closures currently being type checked
2021-12-14 11:02:03 -08:00
Doug Gregor
c3b6160af8 Generalize and cache the "closure effects" determined from the closure body.
Use this to enable better detection of async contexts when determining
whether to diagnose problems with concurrency.

Part of SR-15131 / rdar://problem/82535088.
2021-12-13 21:32:28 -08:00
Richard Wei
05363cd55a Regex literal runtime plumbing.
- Frontend: Implicitly import `_StringProcessing` when frontend flag `-enable-experimental-string-processing` is set.
- Type checker: Set a regex literal expression's type as `_StringProcessing.Regex<(Substring, DynamicCaptures)>`. `(Substring, DynamicCaptures)` is a temporary `Match` type that will help get us to an end-to-end working system. This will be replaced by actual type inference based a regex's pattern in a follow-up patch (soon).
- SILGen: Lower a regex literal expression to a call to `_StringProcessing.Regex.init(_regexString:)`.
- String processing runtime: Add `Regex`, `DynamicCaptures` (matching actual APIs in apple/swift-experimental-string-processing), and `Regex(_regexString:)`.

Upcoming:
- Build `_MatchingEngine` and `_StringProcessing` modules with sources from apple/swift-experimental-string-processing.
- Replace `DynamicCaptures` with inferred capture types.
2021-12-09 16:05:34 -08:00
Hamish Knight
23a8bf0bec Merge pull request #40367 from hamishknight/pitter-patter 2021-12-07 10:15:44 +00:00