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

Normalize global (x,y) syntax during Expr conversion #303

Merged
merged 1 commit into from
Jun 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions src/expr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -411,10 +411,16 @@ function _internal_node_to_Expr(source, srcrange, head, childranges, childheads,
end
end
elseif k == K"local" || k === K"global"
if length(args) == 1 && (a1 = args[1]; @isexpr(a1, :const))
# Normalize `local const` to `const local`
args[1] = Expr(headsym, (a1::Expr).args...)
headsym = :const
if length(args) == 1
a1 = args[1]
if @isexpr(a1, :const)
# Normalize `local const` to `const local`
args[1] = Expr(headsym, (a1::Expr).args...)
headsym = :const
elseif @isexpr(a1, :tuple)
# Normalize `global (x, y)` to `global x, y`
args = a1.args
end
end
elseif k == K"return" && isempty(args)
push!(args, nothing)
Expand Down
4 changes: 4 additions & 0 deletions test/expr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,10 @@
@test parsestmt("const x = 1") == Expr(:const, Expr(:(=), :x, 1))
@test parsestmt("global x ~ 1") == Expr(:global, Expr(:call, :~, :x, 1))
@test parsestmt("global x += 1") == Expr(:global, Expr(:+=, :x, 1))

# Parsing of global/local with
@test parsestmt("global (x,y)") == Expr(:global, :x, :y)
@test parsestmt("local (x,y)") == Expr(:local, :x, :y)
end

@testset "tuples" begin
Expand Down
5 changes: 1 addition & 4 deletions test/test_utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,7 @@ function exprs_roughly_equal(fl_ex, ex)
return false
end
h = ex.head
if (h == :global || h == :local) && length(args) == 1 && Meta.isexpr(args[1], :tuple)
# Allow invalid syntax like `global (x, y)`
args = args[1].args
elseif h == :function && Meta.isexpr(fl_args[1], :block)
if h == :function && Meta.isexpr(fl_args[1], :block)
blockargs = filter(x->!(x isa LineNumberNode), fl_args[1].args)
posargs = blockargs[1:max(0, length(blockargs))]
kwargs = blockargs[2:end]
Expand Down
5 changes: 0 additions & 5 deletions test/test_utils_tests.jl
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
# Tests for the test_utils go here to allow the utils to be included on their
# own without invoking the tests.
@testset "Reference parser bugs" begin
# `global (x,y)`
@test exprs_roughly_equal(Expr(:global, :x, :y),
Expr(:global, Expr(:tuple, :x, :y)))
@test exprs_roughly_equal(Expr(:local, :x, :y),
Expr(:local, Expr(:tuple, :x, :y)))
# `0x1.8p0f`
@test exprs_roughly_equal(1.5,
Expr(:call, :*, 1.5, :f))
Expand Down