JuliaSyntax.jl
JuliaSyntax.jl copied to clipboard
Better diagnostic for block-like `begin ... end` in `typed_hcat`
Over at https://github.com/JuliaLang/julia/issues/46364 @eschnett observed
The array expression
[begin 1 end]creates a 1-element array. Thebegin...endblock 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 errorERROR: 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