JuliaSyntax.jl icon indicating copy to clipboard operation
JuliaSyntax.jl copied to clipboard

Better diagnostic for block-like `begin ... end` in `typed_hcat`

Open c42f opened this issue 3 years ago • 0 comments

Over at https://github.com/JuliaLang/julia/issues/46364 @eschnett observed

The array expression [begin 1 end] creates a 1-element array. The begin...end block is not really necessary here, but can be convenient if the expression is much more complicated, e.g. a comprehension.

The typed array expression Int[begin 1 end] leads to the parsing error ERROR: syntax: unexpected "end".

This being due to the ambiguity of whether begin or end in the first slot inside a[] should be treated as block keywords or as the first/last indices of the array:

julia> dump(:(a[end 1]))
Expr
  head: Symbol typed_hcat
  args: Array{Any}((3,))
    1: Symbol a
    2: Symbol end
    3: Int64 1

julia> dump(:(a[1 end]))
ERROR: syntax: unexpected "end"
Stacktrace:
 [1] top-level scope
   @ none:1

Over in that issue, it was suggested that the parser could explain the issue and suggest using let instead of begin, which seems like a good option.

See also: https://github.com/JuliaLang/JuliaSyntax.jl/pull/81

c42f avatar Sep 05 '22 04:09 c42f