Commit Graph

77 Commits

Author SHA1 Message Date
Pavel Yaskevich
4132aa04f9 [Tests] NFC: Update all of the test-cases improved by changes to generic argument mismatch handling 2025-06-03 00:49:06 -07:00
Hamish Knight
0883b4ba5d [Parse] Recover slightly better from bad shorthand if let
Instead of assuming that `if let <expr>` is meant
to be `if case <expr> = ...`, turn it into
`if let _ = <expr>`, which is consistent with
the fix-it we suggest.

This currently doesn't have much of an effect on
the diagnostics we produce, but will be important
once we start doing bidirectional inference for
ExprPatterns, as it avoids unhelpful diagnostics.
2023-03-23 11:46:22 +00:00
Anthony Latsis
6f58cd684b Gardening: Migrate test suite to GH issues: stmt 2022-09-06 15:08:48 +03:00
Alex Hoppen
bfc68f48e4 [Parser] When recovering from expression parsing don't stop at '{'
When recovering from a parser error in an expression, we resumed parsing at a '{'. I assume this was because we wanted to continue inside e.g. an if-body if parsing the condition failed, but it's actually causing more issue because when parsing e.g.

```swift
expr + has - error +

functionTakesClosure {
}
```

we continue parsing at the `{` of the trailing closure, which is a completely garbage location to continue parsing.

The motivating example for this change was (in a result builder)
```swift
Text("\(island.#^COMPLETE^#)")
takeTrailingClosure {}
```

Here `Text(…)` has an error (because it contains a code completion token) and thus we skip `takeTrailingClosure`, effectively parsing
```swift
Text(….) {}
```

which the type checker wasn’t very happy with and thus refused to provide code completion. With this change, we completely drop `takeTrailingClosure {}`. The type checker is a lot happier with that.
2022-04-07 09:19:22 +02:00
Cal Stephens
cc85edff22 Clean up implementation, add more tests 2022-03-23 17:43:03 -07:00
Cal Stephens
14076df392 Fix test failures 2022-03-16 16:08:26 -07:00
Cal Stephens
ac7529f5bd Improve diagnostic in cases like 'if let foo.bar' 2022-03-13 12:20:01 -07:00
Cal Stephens
38e2b70d5d Add tests 2022-01-05 06:25:37 -08:00
Minhyuk Kim
c3ab99a405 [Sema] Offer 'is' replacement for unused 'if let' expression when the operand is optional 2021-06-14 00:40:03 +09:00
Suyash Srijan
ea41caf182 [Test] Add a regression test for SR-13258 2020-07-20 16:41:36 +01:00
Varun Gandhi
a1716fe2a6 [Diagnostics] Update compiler diagnostics to use less jargon. (#31315)
Fixes rdar://problem/62375243.
2020-04-28 14:11:39 -07:00
Doug Gregor
30e8d1791e [Constraint system] Handle implicit "some" patterns implicitly, better.
We have two similar code paths here that should probably be unified. For
now, make sure the more-specific one for pattern matching kicks in first.

Fixes rdar://problem/59838566.
2020-02-27 16:25:02 -08:00
Doug Gregor
644ed76a01 [Constraint system] Prefer enum cases to static members when pattern matching. 2020-02-24 15:00:39 -08:00
Doug Gregor
f6f80befe8 [Constraint system] Handle implicit tupling during pattern matching.
SE-0110 strikes again!
2020-02-24 11:45:23 -08:00
Doug Gregor
dcf7ddeb3a [Constraint system] Generate constraints for EnumElement patterns.
Generate a complete set of constraints for EnumElement patterns, e.g.,

    case let .something(x, y)

Most of the complication here comes from the implicit injection of optionals,
e.g., this case can be matched to an optional of the enum type of which
`something` is a member. To effect this change, introduce a locator for
pattern matching and use it to permit implicit unwrapping during member
lookup without triggering an error.

Note also labels are dropped completely when performing the match,
because labels can be added or removed when pattern matching. Label
conflict are currently diagnosed as part of coercePatternToType, which
suffices so long as overloading cases based on argument labels is not
permitted.

The primary observable change from this commit is in diagnostics: rather
than diagnostics being triggered by `TypeChecker::coercePatternToType`,
diagnostics for matching failures here go through the diagnostics machinery
of the constraint solver. This is currently a regression, because
there are no custom diagnostics for pattern match failures within the
constraint system. This regression will be addressed in a subsequent
commit; for now, leave those tests failing.
2020-02-24 00:48:15 -08:00
Doug Gregor
be8b9e5ef4 [Constraint solver] Always produce optional types for '?' patterns. 2020-02-24 00:48:14 -08:00
Robert Widmann
0267384e11 Fixup SourceKit and Tests
Patch up all the places that are making a syntactic judgement about the
isInvalid() bit in a ValueDecl.  They may continue to use that query,
but most guard themselves on whether the interface type has been set.
2019-10-30 15:09:14 -07:00
Robert Widmann
742f6b2102 Drastically Simplify VarDecl Validation
This is an amalgam of simplifications to the way VarDecls are checked
and assigned interface types.

First, remove TypeCheckPattern's ability to assign the interface and
contextual types for a given var decl.  Instead, replace it with the
notion of a "naming pattern".  This is the pattern that semantically
binds a given VarDecl into scope, and whose type will be used to compute
the interface type. Note that not all VarDecls have a naming pattern
because they may not be canonical.

Second, remove VarDecl's separate contextual type member, and force the
contextual type to be computed the way it always was: by mapping the
interface type into the parent decl context.

Third, introduce a catch-all diagnostic to properly handle the change in
the way that circularity checking occurs.  This is also motivated by
TypeCheckPattern not being principled about which parts of the AST it
chooses to invalidate, especially the parent pattern and naming patterns
for a given VarDecl.  Once VarDecls are invalidated along with their
parent patterns, a large amount of this diagnostic churn can disappear.
Unfortunately, if this isn't here, we will fail to catch a number of
obviously circular cases and fail to emit a diagnostic.
2019-10-14 12:06:50 -07:00
Pavel Yaskevich
72b61f55bf [Diagnostics] Tailored diagnostic for "condition" expression
Since all condition expressions supposed to be convertible
to `Bool`, let's use that type as contextual and produce a
tailored diagnostic.
2019-09-20 12:37:35 -07:00
Rintaro Ishizaki
02769d77c2 [Parse] Factor out StmtConditionElement parsing 2017-12-22 18:28:04 +09:00
gregomni
ed09da5094 When parsing a statement that expects a brace item list and the "{" isn't where we expect, try to recover by skipping tokens on the same line until we find an open brace, and use that as
the item list if we find it. (While still erroring that we expected a brace on the first bad token.) Improves recovery in general and in SR-5943 in particular.
2017-09-25 07:29:35 -07:00
Rintaro Ishizaki
49d134f1a9 [Parse] Stop lying about QuestionLoc of implicit OptionalTypeRepr (#7384)
In conditional statement let/var patterns.
Set invalid location instead.
2017-02-11 13:39:51 +09:00
Jacob Bandes-Storch
c98e515734 [QoI] Improvements to function call & closure diagnostics (#7224) 2017-02-07 17:36:11 -08:00
Brian King
0c57aebfea Fix unit tests 2017-01-26 10:04:41 -05:00
David Farler
b7d17b25ba Rename -parse flag to -typecheck
A parse-only option is needed for parse performance tracking and the
current option also includes semantic analysis.
2016-11-28 10:50:55 -08:00
Jacob Bandes-Storch
14dc422483 [QoI] fix diagnosis of non-Optional enum used in optional pattern 2016-08-21 23:39:51 -07:00
Michael Ilseman
abea7199a7 [SE-0099 Restructuring Condition Clauses] Flip the switch (#3441)
Now that some time has passed, switch the warnings to errors,
completing SE-0099.
2016-07-27 15:00:10 -07:00
Chris Lattner
669f40aa00 Produce warnings for implicit optional promotions involving !== and ===, and for
pattern matches.  In the case of an 'if let' with an explicit type, produce a
Taylor'd diagnostic that rewrites the condition to the right type.

This wraps up:
<rdar://problem/27457457> [Type checker] Diagnose unsavory optional injections
2016-07-23 17:06:54 -07:00
Michael Gottesman
a047bb7564 Revert "Fix the build."
This reverts commit dc24c2bd34.

Turns out Chris fixed the build but when I was looking at the bots, his fix had
not been tested yet, so I thought the tree was still red and was trying to
revert to green.
2016-07-17 16:29:18 -07:00
Michael Gottesman
dc24c2bd34 Fix the build.
This reverts commit b4cba58330.
This reverts commit a602927c75.
This reverts commit 55fbe5a763.
2016-07-17 16:17:15 -07:00
Chris Lattner
55fbe5a763 Remove Boolean as a special, privileged type used by Sema, and instead
use the concrete Bool type.  This eliminates a bunch of complexity and
makes diagnostics more concrete.
2016-07-17 15:14:24 -07:00
practicalswift
35ee9c842a [gardening] Remove path to Ben Langmuir's home directory :-) 2016-07-07 19:50:09 +02:00
Ben Langmuir
75619c02c4 [CodeCompletion] Improve StmtCondition recovery for code-completion
In the condition expressions of if, while and guard statements we were
throwing away the AST if there was a parse error in the condition, or
the brace statement was missing.  This led to poor code-completion for
unresolved members (enums, options sets) because we couldn't find the
parent expression to type-check.

There are a few minor diagnostic changes because we now do more
type-checking of these conditions, particularly if they end up
containing an unused closure.

SR-2001
2016-07-06 14:08:53 -07:00
Chris Lattner
45f2cfaaa0 Implement SE-0099, but where the migration diagnostics are left as warnings
for now.  I'll upgrade them to errors in a week or two to give downstream
projects a chance to update.
2016-07-02 15:44:57 -07:00
John McCall
3fc2291733 Add basic typo correction for unqualified lookup.
There's a lot of room for better QoI / performance here.
2016-05-20 11:04:58 -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
Max Moiseev
a49dab6bf8 Merge remote-tracking branch 'origin/master' into swift-3-api-guidelines 2016-02-29 12:08:52 -08:00
Daniel Duan
780b58a9a5 [Parser] update tests for 'inout' syntax adjustment 2016-02-26 01:33:22 -08:00
Dmitri Gribenko
65d840c0ae stdlib: lowercase cases in Optional and ImplicitlyUnwrappedOptional 2016-02-18 00:40:33 -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
David Farler
3434f9642b Disallow 'var' pattern bindings in if, while, and guard statements
Make the following patterns illegal:

  if var x = ... {
    ...
  }

  guard var x = ... else {
    ...
  }

  while var x = ... {
    ...
  }

And provide a replacement fixit 'var' -> 'let'.

rdar://problem/23172698

Swift SVN r32855
2015-10-24 01:46:30 +00:00
Joe Groff
2cf81672c1 AST: Correct scoping of 'while' condition bindings.
Fixes rdar://problem/22210545.

Swift SVN r31945
2015-09-14 22:52:22 +00:00
Chris Lattner
ada5487153 add fixit tests to random other tests.
Swift SVN r31006
2015-08-04 20:35:36 +00:00
Jordan Rose
16735a36f6 Fix local variable lookup for names in if/while/guard conditions.
These names don't go into scope until after they've been bound. Not having
this broke "if let foo = foo" for shadowing-based unwrapping.

rdar://problem/21961391

Swift SVN r30539
2015-07-23 16:22:05 +00:00
Chris Lattner
3ad108b0be Reapply r29419:
Enhance fixItRemove() to be a bit more careful about what whitespace it leaves around: if the thing it is removing has leading and trailing whitespace already, this nukes an extra space to avoid leaving double spaces or incorrectly indented results.  

This includes an extra fix for looking off the start of a buffer, which extractText doesn't and can't handle.

This fixes <rdar://problem/21045509> Fixit deletes 'let' from non-binding 'if case let' statements, but leaves an extra space




Swift SVN r29449
2015-06-17 16:31:26 +00:00
Ted Kremenek
d13549e607 Revert "enhance fixItRemove() to be a bit more careful about what whitespace it leaves around:"
This was breaking the bots.

Swift SVN r29432
2015-06-17 02:20:52 +00:00
Chris Lattner
6b3167ab36 enhance fixItRemove() to be a bit more careful about what whitespace it leaves around:
if the thing it is removing has leading and trailing whitespace already, this nukes
an extra space to avoid leaving double spaces or incorrectly indented results.  This
fixes <rdar://problem/21045509> Fixit deletes 'let' from non-binding 'if case let' statements, but leaves an extra space



Swift SVN r29419
2015-06-17 00:55:59 +00:00
Chris Lattner
d5f68b478f Teach the recursive part of the diagnostics to handle the various things
that make vardecls and subscripts immutable.  This makes the indirect cases
a lot more specific ("this is a get-only property" instead of "this is 
immutable") and allows us to consolidate a bunch of code:

 2 files changed, 45 insertions(+), 119 deletions(-)




Swift SVN r28954
2015-05-23 04:53:08 +00:00
Chris Lattner
8fa4d7760b rework "resolvePattern" to perform name lookup of UnresolvedDeclRefExpr's during
resolution.  This allows us to produce better QoI and simplifies code.  This would
be a lot simpler if not for our weird representation of clang enum aliases. :-(


Swift SVN r28760
2015-05-19 06:23:23 +00:00
Chris Lattner
7059871abf Convert some 'var' bindings to 'let' when they are not mutated, some
var/let bindings to _ when they are never used, and use some values that
are only written.  This is a testsuite cleanup, NFC. More to come.


Swift SVN r28406
2015-05-11 00:20:55 +00:00