Skip to content

[SR-11574] Using try instead of throws should have a better diagnostic #53979

@beccadax

Description

@beccadax
Previous ID SR-11574
Radar None
Original Reporter @beccadax
Type Improvement
Status Closed
Resolution Done
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Improvement, DiagnosticsQoI, Parser, StarterBug
Assignee @vguerra
Priority Medium

md5: 4b1deee143c591cfa05263856f246b0c

Issue Description:

Suppose you absentmindedly type try in a function declaration when you meant to type throws. Swift's diagnostic in this situation is not very good:

Welcome to Apple Swift version 5.1.1 (swiftlang-1100.0.274.1 clang-1100.0.33.9).
Type :help for assistance.
  1> func foo() try {}
error: repl.swift:1:11: error: consecutive statements on a line must be separated by ';'
func foo() try {}
          ^
          ;

A message along the lines of "expected throwing specifier; did you mean throws?" with a fix-it would provide a much nicer experience.

To implement this, you would find the place(s) in the parser where we parse the "throws" keyword and attempt to parse the "try" keyword there if "throws" fails. If you parse it, you should emit the diagnostic and then have the compiler pretend that it parsed "throws" to recover from the error. We're already doing this for "throw", so you might just be able to add "try" to the same code path.

Metadata

Metadata

Assignees

Labels

compilerThe Swift compiler itselfdiagnostics QoIBug: Diagnostics Quality of Implementationgood first issueGood for newcomersimprovementparserArea → compiler: The legacy C++ parser

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions