Skip to content

Commit 7e26d8b

Browse files
authored
Normalize global (x,y) syntax during Expr conversion (#303)
The reference parser accepts this syntax and normalizes it eagerly. So we should do the same rather than making it part of fuzzy Expr comparison later.
1 parent 219fa05 commit 7e26d8b

File tree

4 files changed

+15
-13
lines changed

4 files changed

+15
-13
lines changed

src/expr.jl

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -411,10 +411,16 @@ function _internal_node_to_Expr(source, srcrange, head, childranges, childheads,
411411
end
412412
end
413413
elseif k == K"local" || k === K"global"
414-
if length(args) == 1 && (a1 = args[1]; @isexpr(a1, :const))
415-
# Normalize `local const` to `const local`
416-
args[1] = Expr(headsym, (a1::Expr).args...)
417-
headsym = :const
414+
if length(args) == 1
415+
a1 = args[1]
416+
if @isexpr(a1, :const)
417+
# Normalize `local const` to `const local`
418+
args[1] = Expr(headsym, (a1::Expr).args...)
419+
headsym = :const
420+
elseif @isexpr(a1, :tuple)
421+
# Normalize `global (x, y)` to `global x, y`
422+
args = a1.args
423+
end
418424
end
419425
elseif k == K"return" && isempty(args)
420426
push!(args, nothing)

test/expr.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,10 @@
670670
@test parsestmt("const x = 1") == Expr(:const, Expr(:(=), :x, 1))
671671
@test parsestmt("global x ~ 1") == Expr(:global, Expr(:call, :~, :x, 1))
672672
@test parsestmt("global x += 1") == Expr(:global, Expr(:+=, :x, 1))
673+
674+
# Parsing of global/local with
675+
@test parsestmt("global (x,y)") == Expr(:global, :x, :y)
676+
@test parsestmt("local (x,y)") == Expr(:local, :x, :y)
673677
end
674678

675679
@testset "tuples" begin

test/test_utils.jl

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,10 +133,7 @@ function exprs_roughly_equal(fl_ex, ex)
133133
return false
134134
end
135135
h = ex.head
136-
if (h == :global || h == :local) && length(args) == 1 && Meta.isexpr(args[1], :tuple)
137-
# Allow invalid syntax like `global (x, y)`
138-
args = args[1].args
139-
elseif h == :function && Meta.isexpr(fl_args[1], :block)
136+
if h == :function && Meta.isexpr(fl_args[1], :block)
140137
blockargs = filter(x->!(x isa LineNumberNode), fl_args[1].args)
141138
posargs = blockargs[1:max(0, length(blockargs))]
142139
kwargs = blockargs[2:end]

test/test_utils_tests.jl

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
# Tests for the test_utils go here to allow the utils to be included on their
22
# own without invoking the tests.
33
@testset "Reference parser bugs" begin
4-
# `global (x,y)`
5-
@test exprs_roughly_equal(Expr(:global, :x, :y),
6-
Expr(:global, Expr(:tuple, :x, :y)))
7-
@test exprs_roughly_equal(Expr(:local, :x, :y),
8-
Expr(:local, Expr(:tuple, :x, :y)))
94
# `0x1.8p0f`
105
@test exprs_roughly_equal(1.5,
116
Expr(:call, :*, 1.5, :f))

0 commit comments

Comments
 (0)