Commit Graph

37 Commits

Author SHA1 Message Date
Doug Gregor
48db8767b5 Suppress the warning for inactive try/throw in do..catch
In the warning about having no try/throw within the body of a do..catch,
replace the walk of the inactive clauses of IfConfigDecl with a syntactic
check of inactive and unparsed regions to look for 'try' and 'throw'
keywords.

This both eliminates a dependency on IfConfigDecl and expands the
usefulness of this warning suppression to unparsed code.
2024-09-06 22:38:04 -07:00
Doug Gregor
be6393b7ca Use any Error for caught error type of an exhaustive, non-throwing do..catch
Prior to the introduction of typed throws, a `do..catch` always had a caught
error type of `any Error`, even if there were no throwing sites within
the `do` body. With typed throws, such a `do..catch` would have a
caught error type of `Never`, because it never throws. Unfortunately,
this causes code like the following to produce an error, because
`Never` cannot be pattern-matched to `HomeworkError.forgot`:

    func test() {
      do {
        try nonthrowing()
      } catch HomeworkError.forgot {
      } catch {
      }
    }

For source-compatibility reasons, adjust the caught error type to
`any Error` in this case, so we continue to accept the code above.
Don't do this adjustment under `FullTypedThrows`, because it's only
there for compatibility.

Fixes rdar://121526201.
2024-01-24 14:42:56 -08:00
Doug Gregor
ea836389f7 Suppress warnings about Never error types in catch clauses
Enabling typed throws has introduced some spurious warnings about error
values of `Never` type on top of the custom "unreachable" diagnostics
for catch clauses, so suppress the new warnings---they aren't helpful.
2024-01-04 09:37:35 -08:00
Anthony Latsis
ff7f117025 Gardening: Migrate test suite to GH issues: Parse 2022-09-02 01:44:24 +03:00
Evan Wilde
69cb29e758 Fix await in effect with async
Typing `func foo() await {` is something that folks do from time-to-time.
The old error message was unintuitive and suggested adding a semicolon
between the parenthesis and the await, then proceeded to complain about
the opening brace. This wasn't very clear about what the error actually
was.

Pulling this in line with `try`, `throw`, and `throws`, this patch
suggests replacing `await` with `async` when in the function effect
position, and provides a nice fix-it.
2022-03-05 15:53:17 -08:00
Rintaro Ishizaki
31595e3b2b [Parse] Adjust diagnostics message for duplicated effects specifiers 2020-12-14 14:58:30 -08:00
Rintaro Ishizaki
c72f9e5c92 [Parse] Adjust diagnostics for effects specifiers in closure signature 2020-12-14 12:43:50 -08:00
Doug Gregor
5b5b2c462c Fix tests for slightly altered Fix-Its for throws/rethrows parsing. 2020-07-28 09:38:35 -07:00
Owen Voorhees
43e2d107e1 [SE-0276] Implement multi-pattern catch clauses
Like switch cases, a catch clause may now include a comma-
separated list of patterns. The body will be executed if any
one of those patterns is matched.

This patch replaces `CatchStmt` with `CaseStmt` as the children
of `DoCatchStmt` in the AST. This necessitates a number of changes
throughout the compiler, including:
- Parser & libsyntax support for the new syntax and AST structure
- Typechecking of multi-pattern catches, including those which
  contain bindings.
- SILGen support
- Code completion updates
- Profiler updates
- Name lookup changes
2020-04-04 09:28:26 -07:00
Victor Guerra
459181178e [Diagnostics] Add a fix-it for try instead of throws in function decl… (#27647)
* [Diagnostics] Add a fix-it for try instead of throws in function decls and types.

Sometimes one would misstype `throws` for `try` so we provide a fix-it for that.
It might happen in two cases:

In function declarations:
```swift
func foo() try {}
```

In function types:
```swift
let f = () try -> Int
```

Resolves SR-11574.
2019-10-17 01:19:28 +02:00
Tapan Thaker
07e69409da [Diagnostics] Add a fix-it for misplaced throws in function types (#25306)
Adds a diagnostic error for misplaced throws in function types. For e.g:

```swift
let fn: () -> throws Void  // expected-error{{'throws' may only occur before '->'}} {{12-12=throws }} {{15-22=}}
```

Resolves SR-10703.
2019-06-07 19:07:49 -07:00
Hamish Knight
18e5cc6e62 Move a test from Parse to decl
We're testing a Sema diagnostic, not a Parse diagnostic.
2018-06-06 13:28:02 +01:00
Doug Gregor
493d4da667 Warn on “near-misses” when defaults are used for protocol witnesses.
When a particular nominal type or extension thereof declares conformance
to a protocol, check whether that type or extension contains any members
that *nearly* match a defaulted requirement (i.e., a requirement that 
is satisfied by something in a protocol extension), but didn’t match
for some reason and weren’t used to satisfy any other requirement of
that protocol. It’s intended to catch subtle mistakes where a default
gets picked instead of the intended member.

This is a generalization of the code we’ve had for @objc optional
requirements for a long time.

Fixes rdar://problem/24714887.
2017-10-26 17:04:46 -07: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
Pavel Yaskevich
b7ab7491e6 [QoI] Provide fix-it for missing "try" when calling throwing function
When calling a throwing function without 'try', let's suggest multiple
possibilities of note + fix-it for user to choose from.

Resolves: rdar://problem/33040113
2017-07-09 19:22:43 -07:00
Jacob Bandes-Storch
c98e515734 [QoI] Improvements to function call & closure diagnostics (#7224) 2017-02-07 17:36:11 -08:00
Rintaro Ishizaki
2a2da1f7e7 [Parse] Add FIXME for bad recovery for incomplete function type 2016-11-30 23:09:38 +09: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
Doug Gregor
823c24b355 [SE-0112] Rename ErrorProtocol to Error.
This is bullet (5) of the proposed solution in SE-0112, and the last
major piece to be implemented.
2016-07-12 10:53:52 -07:00
Harlan
162648d219 Add fixit for '-> throws' to 'throws ->' (#3335) 2016-07-05 11:29:55 -07:00
Trent Nadeau
0cc851568a Updated tests to use @discardableResult and _ = . 2016-05-11 22:53:38 -04:00
Chris Lattner
2c81c8a114 add some parens to the testsuite, NFC. 2016-05-05 23:19:08 -07:00
Chris Lattner
8746676616 Move @noescape and @autoclosure to their new places in various tests, NFC. 2016-04-15 16:05:35 -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
9a018bd77d Merge remote-tracking branch 'origin/master' into swift-3-api-guidelines 2016-01-20 14:38:22 -08:00
Chris Lattner
2f5af054bf Don't complain about unreachable catch blocks that are due to platform
specific #if code.  This has no known ticket filed for them, Dmitri
mentioned this on swift-dev.
2016-01-18 23:05:06 -08:00
Dmitri Gribenko
feacbc4433 Rename ErrorType to ErrorProtocol 2015-12-09 17:12:19 -08:00
John McCall
a20bfb0ada Correct 'throw' to 'throws' in function signatures and types.
rdar://21328447

Swift SVN r30514
2015-07-22 23:30:13 +00:00
John McCall
bc3b47b98a Infer the return type of a closure to be () if it contains no
return statements, or a return statement with no operand.

Also, fix a special-case diagnostic about converting a return
expression to (1) only apply to converting the actual return
expression, not an arbitrary sub-expression, and (2) use the
actual operand and return types, not the drilled-down types
that caused the failure.

Swift SVN r30420
2015-07-20 21:52:18 +00:00
Chris Lattner
4b4f909d1a Several tangled up changes:
- Enable 'catch is NSError' and 'catch let e as NSError {' patterns to
   a) work, and b) be considered to be exhaustive catches.  This enables
   people to catch an error and *use* it as an NSError directly, instead
   of having to do boiler-platey cases.  This is particularly important
   for the migrator.

 - Do not warn about non-noop coersion casts (like "_ as NSError" when
   matching an ErrorType), since they provide useful type adjustment to
   the subpattern.  Still warn on noop ones.

 - Simplify CatchStmt::isSyntacticallyExhaustive to use 
   Pattern::isRefutablePattern.  Add a FIXME, because the parser is guiding
   closure "throws" inference before the pattern is type checked, which means
   that it is incorrect (but only in subtle cases).

 - When diagnosing pointless 'as' patterns like:

  switch 4 {
  case _ as Int: break 

say "'as' test is always true" instead of "'is' test is always true".




Swift SVN r28774
2015-05-19 17:32:32 +00:00
Chris Lattner
678b99f347 Fix <rdar://problem/20985280> QoI: improve diagnostic on improper pattern match on type
Where before we would diagnose something like "catch MyType {" with two errors (one of
which complaining about a ~= the user didn't write) and two notes, we now produce:

 error: 'is' keyword required to pattern match against type name

with a fixit hint to insert the 'is'.



Swift SVN r28761
2015-05-19 06:25:50 +00:00
Chris Willmore
6951e460f0 If 'throws' is written after the result type in a function decl, suggest
moving it to before the arrow.

<rdar://problem/20857518> QoI for "throws" in the wrong place

Swift SVN r28492
2015-05-12 22:50:42 +00:00
Ted Kremenek
9f9bb725cf Rename '_ErrorType' to 'ErrorType'.
Swift SVN r28293
2015-05-07 21:59:29 +00:00
John McCall
71198a2869 Test error-coverage diagnostics more completely.
Fix an assert-on-valid caused by a broken getSourceRange()
implementation and a missing diagnostic caused by a broken
walker implementation.

Swift SVN r28142
2015-05-05 00:30:51 +00:00
John McCall
fdcecfcfb7 Move error-handling diagnostics to Sema and check try coverage.
Needs better test-case coverage.

Swift SVN r27898
2015-04-29 00:49:40 +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
John McCall
2560e62b66 Only allow 'throw' expressions at statement positions.
rdar://20385676

Swift SVN r26833
2015-04-01 22:30:53 +00:00