Commit Graph

460 Commits

Author SHA1 Message Date
David Farler
39bfc123a3 Refactor: Rename Parser::consumeToken, consumeLoc. Add consumeToken API.
These APIs return SourceLocs, and eventually the Parser should consume
tokens, which now include source trivia such as whitespace and comments,
and package them into a purely syntactic tree.  Just a tiny step. NFC.
2016-11-15 16:11:57 -08:00
David Farler
d6e2b58382 Preserve whitespace and comments during lexing as Trivia
Store leading a trailing "trivia" around a token, such as whitespace,
comments, doc comments, and escaping backticks. These are syntactically
important for preserving formatting when printing ASTs but don't
semantically affect the program.

Tokens take all trailing trivia up to, but not including, the next
newline. This is important to maintain checks that statements without
semicolon separators start on a new line, among other things.

Trivia are now data attached to the ends of tokens, not tokens
themselves.

Create a new Syntax sublibrary for upcoming immutable, persistent,
thread-safe ASTs, which will contain only the syntactic information
about source structure, as well as for generating new source code, and
structural editing. Proactively move swift::Token into there.

Since this patch is getting a bit large, a token fuzzer which checks
for round-trip equivlence with the workflow:

fuzzer => token stream => file1
  => Lexer => token stream => file 2 => diff(file1, file2)

Will arrive in a subsequent commit.

This patch does not change the grammar.
2016-11-15 16:11:57 -08:00
Huon Wilson
fa56e7928d [AST] Remove redundant GenericSignatures.
The GenericSignature is now canonically available through the
GenericEnvironment.
2016-11-11 14:57:04 -08:00
Rintaro Ishizaki
2cd25c8b5e Merge pull request #5194 from rintaro/parselist-earlybailout
[Parse] Improve error handling in parseList
2016-10-14 23:22:12 +09:00
Rintaro Ishizaki
21513b8916 [Parse] Remove unecessary parameters from parseDeclAttributeList
`InParam` was not used at all.

`StopAtTypeAttributes`
As far as I understand, this option *was* merely for improving diagnostic QoI
for declarations like:
  func foo(@typeattr Arg) {}
to fix-it to:
  func foo(_: @typeattr Arg) {}

But, this causes the very loudy diagnostics for misplaced type attributes.
For example, on:
func foo(@convention(block) x: () -> CInt) {}

test.swift:1:10: error: expected parameter name followed by ':'
test.swift:1:10: error: expected ',' separator
test.swift:1:10: error: expected ')' in parameter
test.swift:1:9: note: to match this opening '('
test.swift:1:10: error: consecutive statements on a line must be separated by ';'
test.swift:1:11: error: attribute can only be applied to types, not declarations
test.swift:1:21: error: expected declaration
test.swift:1:44: error: statement cannot begin with a closure expression
test.swift:1:44: note: explicitly discard the result of the closure by assigning to '_'
test.swift:1:44: error: braced block of statements is an unused closure
test.swift:1:6: error: expected '{' in body of function declaration
test.swift:1:44: error: expression resolves to an unused function

Now, we emit more accurate diagnostic:
test.swift:1:11: error: attribute can only be applied to types, not declarations
func foo(@convention(block) x: () -> CInt) {}
          ^

Note that This causes small regression in diagnostics for bare type parameter
like `func foo(@convention(c) () -> CInt) {}`:

Before:
test.swift:1:10: error: unnamed parameters must be written with the empty name '_'
func foo(@convention(block) () -> CInt) {}
         ^
         _:

Now:
test.swift:1:11: error: attribute can only be applied to types, not declarations
func foo(@convention(block) () -> CInt) {}
          ^
test.swift:1:29: error: unnamed parameters must be written with the empty name '_'
func foo(@convention(block) () -> CInt) {}
                            ^
                            _:
2016-10-11 02:26:13 +09:00
Rintaro Ishizaki
7085e2a36c [Parse] Return error status if we cannot parse element in parameter list 2016-10-08 16:51:01 +09:00
Rintaro Ishizaki
2cac9c52ef [QoI] Improve fixit for missing parameter clause in initializer decl (#5042)
Fix `init {}` to `init() {}` instead of `init () {}`.
2016-09-29 11:02:33 +09:00
Doug Gregor
2b57b80030 [Parser] Make parameter parsing still provide useful source locations when it fails. 2016-09-15 09:16:46 -07:00
Rintaro Ishizaki
d75c4c4cbb [gardening][Parse] Remove 2 unused flags in Parser (#4743)
* [Parse] Remove unused GreaterThanIsOperatorRAII. NFC

Last usage was removed in 68af974227.

* [Parse] Remove unused ArgumentIsParameter flag. NFC

Last usage was removed in 4e4173f2e6
2016-09-13 21:54:03 -07:00
Doug Gregor
78b007f178 Always create a DefaultArgumentInitializer for a parameter with a default argument.
As with pattern binding initializer contexts, we were trying to
optimize away these contexts, leading to an unpredictable AST.
2016-09-02 13:51:00 -07:00
Doug Gregor
85537fd66b Murder ExprHandle in cold blood. NFC
ExprHandle is a relic from a horrible time when expressions made their
way into the type system via default arguments. It's been unnecessary
for a long time, so get rid of it.
2016-09-02 10:39:19 -07:00
Rintaro Ishizaki
ba982bc01c [Diag] Make fixItReplace slightly smart
When replace something with a punctuator, we often prefer adding spaces around it.
For instance,

   func foo(): bar {}
   // fix it
   func foo() -> bar {}

In this case we want to add a space before '->', but not after that.

With this change, we can simply `fixItReplace(ColonLoc, " -> ")`.
`fixItReplace()` automatically adjust the spaces around it.
2016-08-25 13:51:55 +09:00
Jacob Bandes-Storch
73830dfff9 [QoI] diagnose initializers written as typed patterns (SR-1461) 2016-08-22 08:59:45 -07:00
Jordan Rose
294bbd8e17 [Parse] Attach 'inout' to Swift 2 unparenthesized function params. (#4241)
This syntax is no longer permitted, but we need to parse it like Swift
2 did in order to produce the fix-it that matches Swift 2 behavior.

rdar://problem/26681485
2016-08-11 16:42:06 -07:00
Slava Pestov
522f4e4572 AST: Replace recently-added IsInOut bit with simpler check, NFC
Thanks to @lattner for the suggestion.
2016-08-05 14:27:16 -07:00
Slava Pestov
9a1fa52ff2 Sema: Fix bogus 'parameters may not have the 'var' specifier' diagnostic
If an inout parameter has an invalid type, we were unable to
distinguish it from a 'var' parameter, resulting in an invalid
diagnostic.

Fix this by adding a VarDecl::isInOut() flag, instead of
introspecting the type.
2016-08-01 23:56:11 -04:00
Harlan
162648d219 Add fixit for '-> throws' to 'throws ->' (#3335) 2016-07-05 11:29:55 -07:00
David Farler
414cfe7487 Minor changes to keyword-as-identifier diagnostics
- Remove stray newline
- Adjust wording when recommending backticks for a keyword identifier
- Provide fix-it when encountering a keyword as an identifier

rdar://problem/25761380
2016-05-10 14:49:21 -07:00
David Farler
b4dbdc49c2 Provide a more specific diag when using keywords as identifiers
When declaring a function like func repeat(){}, the diagnostic is
"expected an identifier" but 'repeat' looks like a reasonable
identifier at first glance, so actually say why it isn't.

rdar://problem/25761380
2016-05-10 11:08:13 -07:00
Manav Gabhawala
7928140f79 [SE-0046] Implements consistent function parameter labels by discarding extraneous parameter names and adding _ where necessary 2016-04-06 20:21:58 -04:00
Joe Groff
2afece3e7c Remove warning about explicit '_' arguments.
SE-0046 will make the '_' necessary to suppress the first keyword label. This warning interferes with writing future-proof code.
2016-03-31 17:06:22 -07:00
Manav Gabhawala
7862f104c9 [Parser] Cleans up parsing of parameter attributes. Implements SE-0053. Fixes SR-979, SR-1020 and cleans up implementation of SE-0003. Provides better fix-its and diagnostics for misplaced 'inout' and prohibits 'var' and 'let' from parameter attributes 2016-03-29 13:55:46 -04:00
Chris Lattner
79968ab7e9 simplify some code related to the inout move. 2016-02-26 11:30:47 -08:00
Daniel Duan
f48487d3fb [Parser] adjust inout declaration for type decoration
This commit implements [SE-0031](//github.com/apple/swift-evolution/blob/master/proposals/0031-adjusting-inout-declarations.md).
When `inout` appears before parameter name, the parser issues a warning and
suggests the correct alternate location for it.

`inout` prefixing the paramter type is now valid.
2016-02-26 01:33:19 -08:00
Daniel Duan
efe230774b [AST] rename some isXXX methods to getAsXXX
There's a group of methods in `DeclContext` with names that start with *is*,
such as `isClassOrClassExtensionContext()`. These names suggests a boolean
return value, while the methods actually return a type declaration. This
patch replaces the *is* prefix with *getAs* to better reflect their interface.
2016-02-11 16:23:40 -08:00
Chris Lattner
8cc0ab170a fix a potential problem that Jordan noticed by inspection, NFC on the testsuite. 2016-02-01 21:02:00 -08:00
Chris Lattner
94dd92fcb8 Fix compiler_crashers 22725 & 28236 by reworking parameter parsing error
recovery a bit.
2016-02-01 20:50:32 -08:00
Chris Lattner
3ae9de1861 remove some old code that isn't doing anything useful in the testsuite,
but regresses parser recovery with a forthcoming patch I'm working on.
2016-02-01 12:28:50 -08:00
Chris Lattner
9b503272a4 fix compiler_crashers/28232-swift-typechecker-typecheckfunctionbodyuntil.swift 2016-01-31 21:46:34 -08:00
David Farler
3f635d04c7 Reinstante var bindings in refutable patterns, except function parameters.
This reverts commits: b96e06da44,
                      8f2fbdc93a,
                      93b6962478,
                      64024118f4,
                      a759ca9141,
                      3434f9642b,
                      9f33429891,
                      47c043e8a6.

This commit leaves 'var' on function parameters as a warning to be
merged into Swift 2.2. For Swift 3, this will be an error, to be
converted in a follow-up.
2016-01-29 15:27:08 -08:00
Denis Vnukov
4f92a08987 Adding a location of the var/let/inout to ParamDecl 2016-01-27 13:43:42 -08:00
Dmitri Gribenko
d046d88821 Revert "[Parser] Fix-it for declaration attributes being applied to parameter types (SR-215)" 2016-01-25 16:55:19 -08:00
Joshua Garnham
ce69f1d260 Decl attr applied to type check now more rigorous
Checks decl options to ensure it can be applied to a parameter & handles no escape special case as it's not a type attribute unless in SIL mode
2016-01-24 20:34:08 +00:00
Chris Lattner
28b71e18b3 Fix two regressions on the validation tests. We're passing down default arg info when
parsing curried arguments in some cases, even though that makes no sense.  This will get
cleaned up when they are removed.
2016-01-23 09:20:45 -08:00
Chris Lattner
f0377bee52 Fix <rdar://problem/22753605> QoI: bad diagnostic when closure has default argument
Fix <rdar://problem/16812341> QoI: Poor error message when providing a default value for a subscript parameter

by emitting a more specific diagnostic about the cases that aren't allowed.
2016-01-22 23:25:47 -08:00
Chris Willmore
983a674e0c Make use of curried function declaration syntax an error.
<rdar://problem/23111018>
2016-01-20 21:57:38 -08:00
Chris Lattner
4db5b98535 fix SR-573 - Crash with invalid parameter declaration
This makes sure to diagnose the error of not having a type on a parameter
in the parser, which is what sema expects.
2016-01-18 09:03:58 -08:00
Chris Lattner
73ec7006ee Simplify mapParsedParameters/createParam by passing the ParsedParameter
into it, instead of passing a bunch of pieces into it.  NFC.
2016-01-18 09:03:58 -08:00
David Farler
a6f2530780 Revert "REVERTME: Temporarily make vars in refutable patterns a warning"
This reverts commit b96e06da44, making
vars in refutable patterns an error for Swift 3.

rdar://problem/23172698
2016-01-14 20:52:24 -08:00
Dmitri Gribenko
f40c2a5b7f Revert "[Parser] Fix-it for declaration attributes being applied to parameter types (SR-215)" 2016-01-12 17:57:56 -08:00
Joshua Garnham
9ced244564 Corrected check for declaration attribute 2016-01-12 21:54:35 +00:00
Joshua Garnham
6b7eb00be7 Removed reference to bug ID 2016-01-12 11:52:26 +00:00
Joshua Garnham
d231cb0321 Add check for decl attributes being applied to types in function declaration (fixes SR-215) 2016-01-12 11:49:45 +00:00
Chris Lattner
a20fa87712 Fix a bug that I noticed when doing the parameter rework, where we'd accidentally
accept closure arguments with API names (but only in a parenthesized parameter list).
While it could theoretically be interesting to support API names on closures, this
is never something we intended to support, and a lot of implementation work would be
necessary to make them correct.  Just correctly reject them even if parenthesized.
2016-01-09 21:20:13 -08:00
Chris Lattner
6afe77d597 Eliminate the Parameter type completely - now ParameterList is just
an overblown array of ParamDecl*'s that also keeps track of parenlocs
and has helper methods.
2016-01-03 14:45:38 -08:00
Chris Lattner
b170b700f8 move the rest of the state out of Parameter and into ParamDecl,
in prep for Parameter going away.  NFC.
2016-01-01 15:27:53 -08:00
Chris Lattner
4479b46ef0 move the TypeLoc for a parameter out of Parameter and onto ParamDecl. NFC. 2016-01-01 14:13:13 -08:00
Chris Lattner
5ce3de8dd6 remove & dial back three old bits of syntax auto-upgrading support:
1. Array type parsing for postfix array types Int[].  We now handle this
   in the parser, but remove the AST representation of this old form.  We
   also stop making vague promises about the future by saying that "fixed
   size arrays aren't supported... yet".  Removal of this fixes a compiler
   crasher too.

2. Remove the special case support for migrating @autoclosure from types
   to parameters, which was Swift 1.0/1.1 syntax.  The world has moved or
   we don't care anymore.

3. Remove upgrade support for # arguments (nee "backtick" arguments), which
   was a Swift 1.x'ism abolished in an effort to simplify method naming
   rules.

NFC on valid code.
2015-12-31 22:29:39 -08:00
Chris Lattner
cc3f173040 remove variadics, default args etc, from tuple patterns, and simplify
the Pattern::clone interface now that the complexity isn't needed.
This also removes support for serializing this state.
2015-12-31 20:12:12 -08:00
Chris Lattner
a30ae2bf55 Merge pull request #836 from zachpanz88/new-year
Update copyright date
2015-12-31 19:36:14 -08:00