Commit Graph

1184 Commits

Author SHA1 Message Date
Doug Gregor
af6b30a195 [Code completion] Add code completion support for macro expansions. 2022-11-28 18:33:10 -08:00
Erik Eckstein
ab1b343dad use new llvm::Optional API
`getValue` -> `value`
`getValueOr` -> `value_or`
`hasValue` -> `has_value`
`map` -> `transform`

The old API will be deprecated in the rebranch.
To avoid merge conflicts, use the new API already in the main branch.

rdar://102362022
2022-11-21 19:44:24 +01:00
Doug Gregor
787af41765 [Macros] Parse generic arguments in macro expansions. 2022-11-17 16:46:43 -08:00
Robert Widmann
4c162b2aeb Delete libSyntax 2022-11-16 14:52:28 -08:00
Robert Widmann
530d937879 Remove SyntaxContext and Parser Affordances 2022-11-16 13:24:21 -08:00
Doug Gregor
d6d2318dc9 [Macros] Handle macro expansion for function-like macros. 2022-10-27 17:15:30 -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
swift-ci
754c0ceab1 Merge pull request #61480 from atrick/fix-move-keyword
Disable '_move' contextual keyword absent -enable-experimental-move-only
2022-10-07 11:18:07 -07:00
Alex Hoppen
e006c24e82 [libSyntax] Adjustments because nodes can have unexpected nodes at the end 2022-10-07 14:22:16 +02:00
Andrew Trick
19175926da Disable '_move' contextual keyword absent -enable-experimental-move-only
Fixes rdar://100872195 ( error: 'move' can only be applied to lvalues
  error: Can not use feature when experimental move only is disabled!)

Identifiers with a single underscore are not reserved for use by the
language implementation. It is perfectly valid for a library to define
its own '_move'.

The contextual _move keyword should only be parse when it is followed by an lvalue, so should *not* conflict with user-defined '_move' functions.
https://github.com/apple/swift-evolution/blob/main/proposals/0366-move-function.md#source-compatibility
2022-10-06 16:33:26 -07:00
Doug Gregor
18f70dd25e Merge pull request #61386 from DougGregor/key-path-parsing-new-parser
Switch to the "old" key path expression syntax nodes.
2022-09-30 16:14:51 -07:00
Doug Gregor
fbb9822913 Switch to the "old" key path expression syntax nodes.
The new Swift parser uses a revamped set of syntax nodes for key paths
that better match the language. However, the C++ parser does not
produce anything like these syntax nodes, so switch to an "old"
keypath node style for now.
2022-09-30 11:53:48 -07:00
Anthony Latsis
18156d1177 Merge pull request #61347 from AnthonyLatsis/migrate-compiler-to-gh-issues
Gardening: Migrate compiler sources to GitHub issues
2022-09-30 10:48:52 +03:00
Anthony Latsis
2843e0c871 Gardening: Migrate compiler sources to GitHub issues 2022-09-29 23:58:55 +03: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
d4b690cfd3 Merge pull request #61302 from jckarter/single-closure-for-all-static-partial-applications
Sema: Form all static member partial applications with one closure
2022-09-26 19:52:20 -07:00
Luciano Almeida
52e2b3dc24 [Parse] Suggest platform correction based on known platforms edit distance 2022-09-26 19:44:04 -03: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
a6a486c60b Merge pull request #61253 from jckarter/key-path-literal-as-function-ast-optimization-2
Sema: Simplify AST representation of key path literals as functions. [take 2]
2022-09-22 15:59:41 -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
Artem Chikin
b8c1b4b3d0 Revert "Sema: Simplify AST representation of key path literals as functions." 2022-09-22 09:54:22 -07:00
Allan Shortlidge
f1ecb4c215 Parse: Offer a fix-it when platform is specified with the wrong case in availability attributes and queries.
Resolves rdar://100195680
2022-09-21 16:21:36 -07:00
Joe Groff
57553878f8 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.
2022-09-21 09:40:30 -07:00
Allan Shortlidge
06595f6597 Merge pull request #60852 from tshortli/parse-has-symbol
AST: Parse `#_hasSymbol`
2022-09-12 09:32:52 -07:00
Slava Pestov
da76e1ee71 Sema: Allow one-element tuples when -enable-experimental-variadic-generics is on 2022-09-10 00:26:43 -04:00
Allan Shortlidge
f5f1d3c028 AST: Parse #_hasSymbol
Introduce the compiler directive `#_hasSymbol` which will be used to detect whether weakly linked symbols are present at runtime. It is intended for use in combination with `@_weakLinked import` or `-weak-link-at-target`.

```
if #_hasSymbol(foo(_:)) {
  foo(42)
}
```

Parsing only; SILGen is coming in a later commit.

Resolves rdar://99342017
2022-09-08 17:15:29 -07:00
Luciano Almeida
5170a2e494 [Parse] Handle underscored integer in #sourceLocation line directive 2022-08-29 22:53:40 -03:00
Rintaro Ishizaki
ed34dfc637 [Syntax] Introduce UnresolvedIsExpr/UnresolvedAsExpr
SequenceExprSyntax should have odd number elements. Previously 'a as b'
was parsed like:
```
(sequence_expr
  (identifier_expr "a"),
  (as_expr
    'as'
    (typeidentifier "b")))
```
So it had even number elements. Now it's parsed
```
(sequence_expr
  (identifier_expr "a"),
  (unresolved_as_expr 'as')
  (type_expr
    (typeidentifier "b")))
```
2022-08-23 13:03:18 -07:00
Rintaro Ishizaki
1eed8675f7 [Parse] Parser update for UnresolvedTernaryExprSyntax 2022-08-18 10:37:18 -07:00
Rintaro Ishizaki
a673043737 Terminology change: 'garbage' -> 'unexpected'
There are no "garbage" characters in Swift code. They are just
"unexpected."
2022-08-15 14:32:28 -07: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
Alex Hoppen
c2695f0ffc [libSyntax] Allow adding garbage nodes in between any two children of a syntax node
When the source code is invalid, this allows us to represent tokens that could not be used to form a valid syntax tree with more fidelity.

This commit does not start using GarbageNodes yet, it just sets everything up for them.
2022-08-04 09:20:31 +02:00
Alex Hoppen
83380f2682 [SourceKit] Register await as a contextual keyword
`await` in an expresssion should be marked as a contextual keyword, not as an identifier

Resolves apple/sourcekit-lsp#591
2022-07-25 10:26:44 +02:00
Hamish Knight
515945fc6d [Parse] Avoid skipping bodies with /.../ regex literals
While skipping, if we encounter a token that looks
like it could be the start of a `/.../` regex
literal, fall back to parsing the function or type
body normally, as such a token could become a
regex literal. As such, it could treat `{` and
`}` as literal, or otherwise have contents that
would be lexically invalid Swift.

To avoid falling back in too many cases, we apply
the existing regex literal heuristics. Cases that
pass the heuristic fall back to regular parsing.
Cases that fail the heuristic are further checked
to make sure they wouldn't contain an unbalanced
`{` or `}`, but otherwise are allowed to be
skipped. This allows us to continue skipping for
most occurrences of infix and prefix `/`.

This is meant as a lower risk workaround to fix the
the issue, we ought to go back to handling regex
literals in the lexer.

Resolves rdar://95354010
2022-06-22 20:05:21 +01:00
Doug Gregor
3cf576da23 Refactor bare slash regex literals as a future feature 2022-05-30 08:33:46 -07:00
Doug Gregor
2fbe202146 Introduce FUTURE_FEATURE definitions for features staged in Swift 6.
Using the same feature set logic as experimental features, provide
feature names for "future" features, which are changes that will
become available with Swift 6. Use the feature check when determining
whether to implementation the feature instead of a language version
check, and map existing flags for these features (when available) over
to the feature set.

As an internal implementation detail, this makes it easier to reason
about when specific features are enabled (or not). If we decide to go
with piecemeal adoption support for features, it can provide an
alternative path to enabling features that feeds this mechanism.
2022-05-30 08:32:56 -07:00
Hamish Knight
9c62319be1 [Parse] Lenient prefix / parsing
Treat a prefix operator containing `/` the same as
the unapplied infix operator case, where we
tentatively lex. This means that we bail if there
is no closing `/` or the starting character is
invalid. This leaves binary operator containing
`/` in expression position as the last place where
we know that we definitely have a regex literal.
2022-05-12 11:49:10 +01:00
Hamish Knight
ba28b6a19b [Parse] Split prefix operators from regex in the lexer
Teach the lexer not to consider `/` an operator
character when attempting to re-lex a regex
literal. This allows us to split off a prefix
operator.

Previously this was done after-the-fact in the
parser, but that didn't cover the unapplied infix
operator case, and didn't form a `tok::amp_prefix`
for `foo(&/.../)`, which led to a suboptimal
diagnostic.

This also now means we'll split an operator for
cases such as `foo(!/^/)` rather than treating it
as an unapplied infix operator.

rdar://92469917
2022-04-29 10:53:56 +01:00
Josh Soref
4721852fcb Spelling parse (#42469)
* spelling: appear

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: availability

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: available

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: coerce

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: collection

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: condition

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: conditional

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: delimiter

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: derived

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: diagnostics

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: disambiguation

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: dropped

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: escaped

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: existence

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: expression

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: expressions

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: extended

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: furthermore

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: identifier

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: indentation

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: inspect

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: miscellaneous

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: multiline

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: offset

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: passthrough

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: precede

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: prefix

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: receiver

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: reference

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: registered

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: representing

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: returned

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: sequence

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: should

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: successfully

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: that

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: the

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: trivia

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: unsupported

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

* spelling: whitespace

Signed-off-by: Josh Soref <jsoref@users.noreply.github.com>

Co-authored-by: Josh Soref <jsoref@users.noreply.github.com>
2022-04-21 09:31:40 -07:00
Luciano Almeida
90c5dfa9ed [Parse] Track whether an let _: pattern is an async let pattern 2022-04-17 14:06:39 -03:00
Hamish Knight
f1a799037e [Parse] Introduce /.../ regex literals
Start parsing regex literals with `/.../`
delimiters.

rdar://83253726
2022-04-12 16:03:49 +01:00
Evan Wilde
4c7e4615d9 Improve captured field diagnostics
This patch improves the error message emitted when the capture list
contains an item that is a sub-field of a struct/class/etc....

If the closure capture did not include `weak` at the beginning, the
presence of a period would cause the if-chain to fall through the
identifier checking, resulting in an error message about expecting a
`weak` keyword. Instead, I've opted to accept the period at that stage
of parsing so that we can fall through to a better error message.

For the following code
```
{ [self.field] in ... }
```
instead of emitting
`expected 'weak', 'unowned', or no specifier in capture list`,
we now emit
`fields may only be captured by assigning to a specific name`
with a fix-it that changes the code to
```
{ [ field = self.field ] in ... }
```
2022-04-01 18:23:00 -07:00
Alex Hoppen
7f082e1c28 [Parser] Ignore generic arguments after a code completion token 2022-03-23 13:03:56 +01:00
Holly Borla
1acc9c3798 [Parser] Don't parse 'any identifier' as a type when the identifier
appears on the next line.
2022-01-26 11:06:28 -08:00
Holly Borla
bee6adf605 [Parser] Parse (any P).self 2022-01-20 21:49:56 -08:00
Hamish Knight
37f16520e6 Prototype regex literal AST and emission
With `-enable-experimental-string-processing`,
start lexing `'` delimiters as regex literals (this
is just a placeholder delimiter for now). The
contents of which gets passed to the libswift
library, which can return an error string to be
emitted, or null for success.

The libswift side isn't yet hooked up to the Swift
regex parser, so for now just emit a dummy
diagnostic for regexes starting with quantifiers.

If successful, build an AST node which will be
emitted as an implicit call to an
`init(_regexString:)` initializer of an in-scope
`Regex` decl (which will eventually be a known
stdlib decl).
2021-12-06 21:16:14 +00:00
Hamish Knight
76c6254779 [AST] Refactor KeyPathExpr constructors
Now that the CSApply just uses components, we can
better split up the key path constructors to either
accept a set of resolved components, or a parsed
root or path.
2021-11-15 12:25:18 +00:00
Alex Hoppen
b888dc0e40 [Parser] Don't modify the current token kind when cutting off parsing
Previously, when we reached the maximum nesting level, we changed the current token’s kind to an EOF token. A lot of places in the parser are not set up to expect this token change. The intended workaround was to check whether pushing a structure marker failed (which would change the token kind) and bail out parsing if this happened. This was fragile and caused assertion failures in assert builds.

Instead of changing the current token’s kind, and failing to push the structure marker, let the lexer know that it should cut off lexing, essentially making the input buffer stop at the current position. The parser will continue to consume its current token (`Parser.Tok`) and the next token that’s already lexed in the lexer (`Lexer.NextToken`) before reaching the emulated EOF token. Thus two more tokens are parsed than before, but that shouldn’t make much of a difference.
2021-11-09 12:28:10 +01:00
Hamish Knight
21e686e478 [Parse] Remove dead condition
We used to avoid parsing labels for yield stmts
by passing in `SyntaxKind::YieldStmt` and relying
on this condition. However we no longer do so, we
pass `SyntaxKind::ExprList` instead.
2021-09-30 11:00:28 +01:00
Robert Widmann
12b7d90c93 Merge pull request #39443 from Huddie/remove-dyanmic-type-diagnostic
Remove dynamic type diagnostic suggesting using type(of: instead
2021-09-24 13:22:02 -07:00