Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect parsing of certain catch syntax #782

Open
kbarros opened this issue Nov 27, 2023 · 1 comment
Open

Incorrect parsing of certain catch syntax #782

kbarros opened this issue Nov 27, 2023 · 1 comment
Labels
bug Something isn't working CSTParser3

Comments

@kbarros
Copy link

kbarros commented Nov 27, 2023

Consider this code with a non-idiomatic catch statement:

code1 = raw"""
try
    error("Whoops")
catch (e)
    println("Found $e")
end
"""

Julia Formatter moves (e) inside the catch block:

using JuliaFormatter
code2 = format_text(code1)

@assert code2 == raw"""
try
    error("Whoops")
catch
    (e)
    println("Found $e")
end
"""

This transformation changes the meaning of the code:

eval(Meta.parse(code1)) # Found ErrorException("Whoops")
eval(Meta.parse(code2)) # ERROR: UndefVarError: `e` not defined

It seems the correct fix would be to remove the parentheses in catch e.

@domluna
Copy link
Owner

domluna commented Nov 27, 2023

the issue is with CSTParser

julia> x = CSTParser.parse(code1)
  1:62  try
  1:16   block
  1:16    call
  1:5      error
  6:13     STRING: Whoops
 17:16   FALSE:
 17:44   block
 17:24    brackets
 17:17     e
 25:44    call
 25:31     println
 32:41     string
 32:38      STRING: Found
 39:39      e

julia> x = CSTParser.parse(code2)
  1:60  try
  1:16   block
  1:16    call
  1:5      error
  6:13     STRING: Whoops
 17:22   e
 23:42   block
 23:42    call
 23:29     println
 30:39     string
 30:36      STRING: Found
 37:37      e

julia> Meta.parse(code1)
:(try
      #= none:2 =#
      error("Whoops")
  catch e
      #= none:4 =#
      println("Found $(e)")
  end)

julia> Meta.parse(code2)
:(try
      #= none:2 =#
      error("Whoops")
  catch e
      #= none:4 =#
      println("Found $(e)")
  end)

In the case of (e) it thinks that's the body of the catch block

code1 is the original one with catch (e), code2 is catch e

@domluna domluna added bug Something isn't working CSTParser3 labels Nov 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working CSTParser3
Projects
None yet
Development

No branches or pull requests

2 participants