Commit Graph

455 Commits

Author SHA1 Message Date
Chris Lattner
59185be7cd Add the ability to write an 'if case' condition. It allows an arbitrary refutable
pattern, and can be chained together in conditions just like our other 'if let' 
constructs.  This only adds functionality, it doesn't change anything yet.


Swift SVN r27932
2015-04-29 21:59:57 +00:00
John McCall
426510cd4e Create an ErrorExpr when expression parsing consumes tokens
but doesn't build anything.

I have no idea why this test case needed to change.

Swift SVN r27897
2015-04-29 00:23:15 +00:00
Chris Lattner
82ef514f99 Reimplement the AST, Sema, and SILGen of defer to be closure based.
Now we bind the defer body into a ClosureExpr and emit it at the point of
the defer.  At any exit points out of the controlled region, we emit a call 
to the closure.

This should cover any problems where expressions cannot be emitted multiple times.
However, this is dramatically more complex than the obvious implementation, so I 
hope this patch can be reverted.



Swift SVN r27767
2015-04-26 17:58:06 +00:00
Chris Lattner
ee96164996 implement parsing, AST, and basic Sema support for 'defer'.
SILGen support and diagnosing jumps out of a defer aren't done
yet.



Swift SVN r27759
2015-04-26 15:16:37 +00:00
Chris Lattner
f71a329afd fix <rdar://problem/17569958> #endif not parsed correctly when occurs as last line in file
Swift SVN r27753
2015-04-26 05:32:04 +00:00
Chris Lattner
a475a3326f implement <rdar://problem/19786845> Warn on "let" and "var" when no data is bound in a pattern
We warn like this:
t.swift:3:12: warning: 'let' pattern has no effect; sub-pattern didn't bind any variables
      case let .Bar: println("bar")
           ^~~ ~~~~



Swift SVN r27747
2015-04-26 04:38:13 +00:00
Chris Lattner
2f15b53fe8 Add support for 'catch {' as a synonym for "catch let error {".
Swift SVN r27702
2015-04-24 18:54:45 +00:00
Chris Willmore
c7c7388cf2 Change do-while to repeat-while.
Change all uses of "do { ... } while <cond>" to use "repeat" instead.
Rename DoWhileStmt to RepeatWhileStmt. Add diagnostic suggesting change
of 'do' to 'repeat' if a condition is found afterwards.

<rdar://problem/20336424> rename do/while loops to repeat/while & introduce "repeat <count> {}" loops

Swift SVN r27650
2015-04-23 22:48:31 +00:00
Chris Lattner
e45c8defce fix <rdar://problem/20489838> QoI: Nonsensical error and fixit if "let" is missing between 'if let ... where' clauses
by tweaking parsing logic to handle a common error case with a nice diagnostic + fixit.  We now produce:

x.swift:4:29: error: binding ended by previous 'where' clause; use 'let' to introduce a new one
if let y? = x where y == 0, z? = x where z == 0 {
                            ^
                            let 



Swift SVN r27452
2015-04-18 05:36:01 +00:00
Chris Lattner
a63120f11a fix <rdar://problem/20457938> typed pattern is not allowed on if/let condition
Swift SVN r27110
2015-04-08 00:28:29 +00:00
Chris Lattner
e62845b9da fix <rdar://problem/20426834> incorrect refutable pattern match issue
We only require one of the patterns in a multi-pattern PBD to be conditional
as part of the swift 1 migation.  Relax the requirements to allow unconditional
bindings next to conditional ones.  This required moving some logic from the parser
to sema time.



Swift SVN r26987
2015-04-04 20:36:16 +00:00
Chris Lattner
8a07a85a91 Progress towards let/else:
- Implement SILGen for conditional multi-pattern PBD's.
 - Have the type checker check where clauses on PBDs.
 - Change the AST to represent complex if/let PBD's with
   composed PBDs instead of breaking them down. For example, 
   represent:
     if let x? = foo(), y? = bar() where x == y {
   as a single PBD in a StmtCondition instead of representing
   it as three entries in the condition.

The later change is good for AST/source fidelity as well as providing
a cheap way to exercise all the logic I'm building.



Swift SVN r26959
2015-04-03 22:46:34 +00:00
John McCall
2560e62b66 Only allow 'throw' expressions at statement positions.
rdar://20385676

Swift SVN r26833
2015-04-01 22:30:53 +00:00
Chris Lattner
b6e13bd1fe - Diagnose conditional PBD's without an else, conditional ones that do have an else,
and refutable pattern bindings without an initializer.
- Enhance ASTDumper to dump where/else clauses on PBDs.
- Merge if/let conditional PBD logic into the mainline logic now that they are all
  potentially conditional (more simplifications coming for this)

add tests for the fixits, which exercise the earlier SourceRange enhancments, e.g.:

x.swift:3:5: error: refutable pattern match can fail; add an else {} to handle this condition
let o? = a
    ^~
           else {}




Swift SVN r26751
2015-03-31 05:31:47 +00:00
Chris Lattner
c6134d913f prune #include
Swift SVN r26705
2015-03-29 22:02:26 +00:00
Arnold Schwaighofer
b9f795699f Revert "add parser and AST representation support for where/else clauses on let/var decls."
It breaks the validation test suite.

This reverts commit r26692.

rdar://20339903

Swift SVN r26700
2015-03-29 13:56:32 +00:00
Chris Lattner
9c53b659ee add parser and AST representation support for where/else clauses on let/var decls.
Sema, silgen, and more tests coming later.


Swift SVN r26692
2015-03-29 06:20:24 +00:00
Chris Lattner
fa90fec707 Teach Pattern::forEachVariable how to handle dig into ExprPatterns
to find variables, enabling it to work with refutable patterns in
addition to nonrefutable ones.  This allows eliminating some special
case code in the parser, NFC.



Swift SVN r26686
2015-03-29 05:28:35 +00:00
Xi Ge
8a64deb828 [codecompletion] auto-completing the catch statement.
When the code completion token is seen after the catch keyword, a set of
visible error types are suggested as completion.

Swift SVN r26519
2015-03-25 04:16:57 +00:00
John McCall
5d2f32bf83 Disallow trailing closures in catch patterns and
fix an assertion with rethrows out of non-exhaustive
catch statements.

Swift SVN r26470
2015-03-24 01:34:50 +00:00
John McCall
a0a16d78d2 Implement the do/catch statement. Tests to follow.
This patch also introduces some SILGen infrastructure for
dividing the function into "ordinary" and "postmatter"
sections, with error-handling-like stuff going into the
final section.  Currently, this is largely undermined by
SILBuilder, but I'm going to fix that in a follow-up.

Swift SVN r26422
2015-03-23 02:08:26 +00:00
Chris Lattner
8521916b86 change PatternBindingDecl to be created with a static "create" method instead
of using its ctor directly.  NFC, this is in prep for other changes coming.


Swift SVN r26174
2015-03-16 00:44:52 +00:00
Chris Lattner
a0d02cdcdd Several minor changes:
- Strength reduce isAtStartOfBindingName() to just check for 
   identifier or _ and inline into its two callers.
 - Rename Token::isIdentifierOrNone to isIdentifierOrUnderscore.
 - Teach InVarOrLetPattern about matching patterns, so that the
   parser knows when it is parsing an expression as a matching
   pattern but is not yet inside a let/var pattern.
 - Use newfound knowledge of matching patterns to refine handling
   of unexpected let/var when parsing an expression, but not in a
   pattern context, slightly improving QoI in invalid cases.



Swift SVN r26172
2015-03-15 23:36:05 +00:00
Chris Lattner
c050e810c7 Fix: <rdar://problem/17462274> Is increment in for loop optional?
a trivial oversight in parenthesized c-style-for-loop parsing.



Swift SVN r26169
2015-03-15 23:03:35 +00:00
Chris Lattner
d7c26d8758 Speculatively land the grammar change for <rdar://problem/20167393> revise typed-pattern grammar to make refutable patterns a superset of irrefutable ones
This is still a subject of discussion on swift-dev, but it seems like clearly the right
way to go to me.  If it turns out that this isn't a good direction, I'll revert this and 
subsequent patches built on top of it.



Swift SVN r26168
2015-03-15 22:54:45 +00:00
Chris Lattner
bf73cc23f1 fix <rdar://problem/20167543> "for var x = ..." not parsed as a foreach loop
This was because the ambiguity between c-style and foreach loops wasn't being
properly handled.  Use the canParsePattern() logic to handle this in full 
generality.

Since that logic was unused, dust it off and clean it up a bit.  Similarly,
remove some old vestigates of default argument parsing in tuples and 
old-syntax array handling.



Swift SVN r26164
2015-03-15 21:43:04 +00:00
Chris Lattner
27ef444db5 Remove the "isLet" parameter from the pattern parsing logic. It was (almost)
duplicated by the InVarOrLetPattern state in the Parser object.  Beef 
InVarOrLetPattern up so that we can remove it.

NFC except that we now reject pointless let patterns in foreach loops,
similar to how we reject var patterns inside of let patterns.



Swift SVN r26163
2015-03-15 21:06:37 +00:00
Chris Lattner
20f8f09ea8 Land: <rdar://problem/19382905> improve 'if let' to support refutable patterns and untie it from optionals
This changes 'if let' conditions to take general refutable patterns, instead of
taking a irrefutable pattern and implicitly matching against an optional.

Where before you might have written:
  if let x = foo() {

you now need to write:
  if let x? = foo() {
    
The upshot of this is that you can write anything in an 'if let' that you can
write in a 'case let' in a switch statement, which is pretty general.

To aid with migration, this special cases certain really common patterns like
the above (and any other irrefutable cases, like "if let (a,b) = foo()", and
tells you where to insert the ?.  It also special cases type annotations like
"if let x : AnyObject = " since they are no longer allowed.

For transitional purposes, I have intentionally downgraded the most common
diagnostic into a warning instead of an error.  This means that you'll get:

t.swift:26:10: warning: condition requires a refutable pattern match; did you mean to match an optional?
if let a = f() {
       ^
        ?

I think this is important to stage in, because this is a pretty significant
source breaking change and not everyone internally may want to deal with it
at the same time.  I filed 20166013 to remember to upgrade this to an error.

In addition to being a nice user feature, this is a nice cleanup of the guts
of the compiler, since it eliminates the "isConditional()" bit from
PatternBindingDecl, along with the special case logic in the compiler to handle
it (which variously added and removed Optional around these things).




Swift SVN r26150
2015-03-15 07:06:22 +00:00
Chris Lattner
3e3f568179 By far, the most common use of Lexer::getLocForEndOfToken is in
conjunction with .fixItInsert().  As such, introduce a helper named
.fixItInsertAfter() that does what we all want.  Adopt this in various
places around the compiler.  NFC.



Swift SVN r26147
2015-03-15 05:30:04 +00:00
John McCall
0802e85975 Implement the naked 'do' statement.
For now, we assume that 'while' after the braces starts
a do/while rather than being an independent statement.
We should disambiguate this, or better, remove do/while.

Tests later.

Swift SVN r26079
2015-03-13 01:58:42 +00:00
Denis Vnukov
9e76e2ba50 Minor (swift migrator related fixes):
Corrected several places where compiler generated AST nodes were not properly 
marked as implicit.

For interpolated strings also fixed string segment locations and made sure 
the first and last segments are preserved in AST even if they are empty.



Swift SVN r25983
2015-03-11 18:08:36 +00:00
Xi Ge
c0bf1f54ff [CodeCompletion] Further support the context-sensitivity of
auto-completing @attributes. By delaying the handling of code completion token after the entire decl being parsed, we know
what are the targets of the attribute to finishe, thus, only suggesting those applicable attributes.

Swift SVN r25938
2015-03-10 18:40:39 +00:00
Chris Willmore
42dba24464 Don't allow '{' at start of if-condition
If '{' is encountered immediately after 'if', assume that the condition
is missing. Apply the same treatment to while, do-while, for-in, and
switch. This way we're not trying to re-parse, backtrack, repurpose
misparsed closure bodies, etc. in those cases. Users who want to write a
condition that starts with '{' can wrap it in parens.

Addressing feedback re <rdar://problem/18940198>.

Swift SVN r25747
2015-03-04 05:39:49 +00:00
Jordan Rose
c6bb14f71f [Parse] Use a clearer condition for inactive #if blocks.
Fix-up to r25567 suggested by Denis. No functionality change.

Swift SVN r25601
2015-02-27 16:51:01 +00:00
Jordan Rose
9b5134d10a [Parse] Don't add local types from inactive #if blocks to the master list.
Otherwise we'll get all the way to IRGen and then try to emit them. And try
to reference them in serialization.

rdar://problem/19935034

Swift SVN r25567
2015-02-26 23:37:20 +00:00
Chris Lattner
da1dfcd55c implement <rdar://problem/19150249> Allow labeled "break" from an "if" statement
This doesn't allow 'continue' out of an if statement for the same reason we don't
allow it on switch: we'd prefer people to write loops more explicitly.



Swift SVN r25565
2015-02-26 22:32:30 +00:00
Chris Lattner
58aadafe82 follow up to improve r25511.
Swift SVN r25525
2015-02-25 01:27:40 +00:00
Chris Lattner
02f5c325d4 fix <rdar://problem/19939746> Improve error recovery for malformed if statements
Swift SVN r25511
2015-02-24 20:39:06 +00:00
Chris Lattner
3357ee21f3 Revert r25417, r25408, and r25370. This adds back back the warning for
un-type-annotated AnyObject binding in "if let", and allows general 
patterns in if-let.

This also reverts some unrelated QoI improvements that went in with those
patches, but I'll add those back next.


Swift SVN r25507
2015-02-24 18:55:54 +00:00
Chris Willmore
e8c8cfaa3c When recovering from missing switch statement subject, discard
diagnostics that arose from attempting to parse the switch statement
body as an expression.

Swift SVN r25448
2015-02-21 00:23:10 +00:00
Chris Willmore
776c6a1bd2 Use RAII with DiagnosticTransaction instead of manually opening and
committing transactions.

Swift SVN r25440
2015-02-20 22:51:20 +00:00
Chris Willmore
9134d7032d When parsing if/while statements, if the condition turns out to be a
closure, reparse it as a brace statement instead of attempting to
repurpose the closure body. Suppress diagnostics from the initial
condition parse until we're committed to that parse.

<rdar://problem/18940198> Fuzzing Swift: performTypeChecking(...) crashes in ContextualizeClosures::walkToExprPre(...): Assertion failed: "Incorrect parent decl context for closure"

Swift SVN r25438
2015-02-20 22:06:35 +00:00
Chris Lattner
dc46dd1a5a improve diagnostic and add fixit for if/let pattern error, as recommended by Dmitri.
Swift SVN r25417
2015-02-20 04:49:31 +00:00
Chris Lattner
fee103c42c Reapply two tweaks to if/let handling:
two logically independent but related patches conflated together: 
 - Improve error recovery for malformed if/let conditions, particularly 
   when the user uses "," instead of &&. Add testcases for error recovery 
   requested by Jordan.

- Add a syntactic requirement that the pattern of an if/let condition be 
  a simple identifier or _. We allow slightly broader patterns here now, 
  but they will change in the future when refutable patterns are allowed. 
  It is best to be narrow and then open it up later so we can do a great
  job of QoI then.

This includes the changes to the stdlib directory that I forgot to commit
with the patch the previous time.



Swift SVN r25408
2015-02-20 02:15:25 +00:00
Erik Eckstein
a44814b776 Revert 25371: "[25371] two logically independent but related patches conflated together:"
It broke the stdlib build.



Swift SVN r25372
2015-02-18 17:30:31 +00:00
Chris Lattner
6ad3a975db two logically independent but related patches conflated together:
- Improve error recovery for malformed if/let conditions, particularly
  when the user uses "," instead of &&.  Add testcases for error recovery
  requested by Jordan.

- Add a syntactic requirement that the pattern of an if/let condition be
  a simple identifier or _.  We allow slightly broader patterns here now,
  but they will change in the future when refutable patterns are allowed.
  It is best to be narrow and then open it up later so we can do a great 
  job of QoI then.



Swift SVN r25371
2015-02-18 06:16:06 +00:00
Chris Lattner
3e723974cc Implement support for a leading boolean condition in an if-let condition,
resolving <rdar://problem/19797158> Swift 1.2's "if" has 2 behaviours. They could be unified.



Swift SVN r25369
2015-02-18 04:54:27 +00:00
Denis Vnukov
6776cb2e95 Fix for rdar://problem/19582877, Fuzzing Swift: Parser::parseTopLevel() crashes in swift::verify(...)
Brace statement created for wrapping IfConfig inside TopLevelCodeDecl does not have 
closing brace, so we should use the previous token’s location as right brace location.



Swift SVN r24797
2015-01-28 17:48:36 +00:00
Denis Vnukov
6d232296d4 Fix for rdar://problem/19540536, Fuzzing Swift: assertion in parser: Assertion failed: (Tok.getKind() == tok::kw_let || Tok.getKind() == tok::kw_var)
Added an error in c-style for statement parsing for cases when we don’t find ‘var’ or ‘let’ after we parsed declaration attributes.



Swift SVN r24761
2015-01-27 22:20:00 +00:00
Jordan Rose
36aab716da [Parse] Disallow computed accessors in C-style for-loop variables.
This improves recovery for "for var i {}".

Swift SVN r24662
2015-01-23 00:32:45 +00:00