Uploaded image for project: 'Swift'
  1. Swift
  2. SR-11574

Using `try` instead of `throws` should have a better diagnostic

    XMLWordPrintable

    Details

      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.

        Attachments

          Activity

            People

            Assignee:
            vguerra Victor Guerra
            Reporter:
            beccadax Becca Royal-Gordon
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved: