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

fix #35134, regression in printing nested quote Exprs #35479

Merged
merged 1 commit into from
Apr 18, 2020
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
34 changes: 15 additions & 19 deletions base/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1207,13 +1207,7 @@ function show_unquoted_expr_fallback(io::IO, ex::Expr, indent::Int, quote_level:
show(io, ex.head)
for arg in ex.args
print(io, ", ")
if isa(arg, Expr)
print(io, ":(")
show_unquoted(io, arg, indent, 0, quote_level+1)
print(io, ")")
else
show(io, arg)
end
show(io, arg)
end
print(io, "))")
end
Expand Down Expand Up @@ -1600,17 +1594,19 @@ function show_unquoted(io::IO, ex::Expr, indent::Int, prec::Int, quote_level::In

elseif head === :quote && nargs == 1 && isa(args[1], Symbol)
show_unquoted_quote_expr(IOContext(io, beginsym=>false), args[1]::Symbol, indent, 0, quote_level+1)
elseif head === :quote && nargs == 1 && is_expr(args[1], :block)
show_block(IOContext(io, beginsym=>false), "quote", Expr(:quote, args[1].args...), indent,
quote_level+1)
print(io, "end")
elseif head === :quote && nargs == 1
print(io, ":(")
show_unquoted(IOContext(io, beginsym=>false), args[1], indent+2, 0, quote_level+1)
print(io, ")")
elseif head === :quote
show_block(IOContext(io, beginsym=>false), "quote", ex, indent, quote_level+1)
print(io, "end")
elseif head === :quote && !get(io, :unquote_fallback, true)
if nargs == 1 && is_expr(args[1], :block)
show_block(IOContext(io, beginsym=>false), "quote", Expr(:quote, args[1].args...), indent,
quote_level+1)
print(io, "end")
elseif nargs == 1
print(io, ":(")
show_unquoted(IOContext(io, beginsym=>false), args[1], indent+2, 0, quote_level+1)
print(io, ")")
else
show_block(IOContext(io, beginsym=>false), "quote", ex, indent, quote_level+1)
print(io, "end")
end

elseif head === :gotoifnot && nargs == 2 && isa(args[2], Int)
print(io, "unless ")
Expand Down Expand Up @@ -1645,7 +1641,7 @@ function show_unquoted(io::IO, ex::Expr, indent::Int, prec::Int, quote_level::In
if head === :$
quote_level -= 1
end
if head === :$ && quote_level < 0 && get(io, :unquote_fallback, true)
if head === :$ && get(io, :unquote_fallback, true)
unhandled = true
else
print(io, head)
Expand Down
16 changes: 8 additions & 8 deletions doc/src/manual/metaprogramming.md
Original file line number Diff line number Diff line change
Expand Up @@ -261,10 +261,10 @@ julia> x = :(1 + 2);
julia> e = quote quote $x end end
quote
#= none:1 =#
quote
#= none:1 =#
$x
end
$(Expr(:quote, quote
#= none:1 =#
$(Expr(:$, :x))
end))
end
```

Expand All @@ -289,10 +289,10 @@ This is done with multiple `$`s:
julia> e = quote quote $$x end end
quote
#= none:1 =#
quote
#= none:1 =#
$(1 + 2)
end
$(Expr(:quote, quote
#= none:1 =#
$(Expr(:$, :(1 + 2)))
end))
end
```

Expand Down
92 changes: 12 additions & 80 deletions test/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -891,8 +891,8 @@ test_mt(show_f5, "show_f5(A::AbstractArray{T,N}, indices::Vararg{$Int,N})")
@test_repr repr(Expr(:quote, Expr(:typed_hcat, Expr(:$, :a), 1)))
@test_repr "Expr(:quote, Expr(:typed_vcat, Expr(:\$, :a), 1))"
@test_repr "Expr(:quote, Expr(:typed_hcat, Expr(:\$, :a), 1))"
@test repr(Expr(:quote, Expr(:typed_vcat, Expr(:$, :a), 1))) == ":(:(\$a[1;]))"
@test repr(Expr(:quote, Expr(:typed_hcat, Expr(:$, :a), 1))) == ":(:(\$a[1]))"
@test_repr repr(Expr(:quote, Expr(:typed_vcat, Expr(:$, :a), 1)))
@test_repr repr(Expr(:quote, Expr(:typed_hcat, Expr(:$, :a), 1)))

# Printing of :(function f end)
@test sprint(show, :(function f end)) == ":(function f end)"
Expand All @@ -914,64 +914,6 @@ end"""
\$a + \$b
end
end"""
@test repr(Meta.parse(
"""macro m(a, b)
quote
\$a + \$b
end
end""")) ==
"""
:(macro m(a, b)
#= none:1 =#
#= none:2 =#
quote
#= none:3 =#
\$a + \$b
end
end)"""
@test repr(Base.remove_linenums!(Meta.parse(
"""macro m(a, b)
quote
\$a + \$b
end
end"""))) ==
"""
:(macro m(a, b)
quote
\$a + \$b
end
end)"""
@test repr(Meta.parse(
"""macro m(a, b)
:(\$a + \$b)
end""")) ==
":(macro m(a, b)\n #= none:1 =#\n #= none:2 =#\n :(\$a + \$b)\n end)"
@test repr(Expr(:macro, Expr(:call, :m, :x), Expr(:quote, Expr(:call, :+, Expr(:($), :x), 1)))) ==
":(macro m(x)\n :(\$x + 1)\n end)"

# nested quotes and interpolations
@test repr(Meta.parse(
"""quote
quote
\$\$x
end
end""")) ==
"""
:(quote
#= none:2 =#
quote
#= none:3 =#
\$\$x
end
end)"""
@weak_test_repr """
quote
#= none:2 =#
quote
#= none:3 =#
\$\$x
end
end"""

# fallback printing + nested quotes and unquotes
@weak_test_repr repr(Expr(:block, LineNumberNode(0, :none),
Expand All @@ -984,26 +926,15 @@ end"""
@test_repr "Expr(:exotic_head, Expr(:call, :+, 1, \$\$y))"
@test_repr ":(Expr(:exotic_head, Expr(:call, :+, 1, \$y)))"
@test_repr ":(:(Expr(:exotic_head, Expr(:call, :+, 1, \$\$y))))"
@test repr(Expr(:block, LineNumberNode(0, :none),
Expr(:exotic_head, Expr(:$, :x)))) ==
"""
quote
#= none:0 =#
\$(Expr(:exotic_head, :(\$x)))
end"""
@test repr(Expr(:exotic_head, Expr(:call, :+, 1, :(Expr(:$, :y))))) ==
":(\$(Expr(:exotic_head, :(1 + Expr(:\$, :y)))))"
@test repr(Expr(:exotic_head, Expr(:call, :+, 1, Expr(:quote, Expr(:$, :y))))) ==
":(\$(Expr(:exotic_head, :(1 + :(\$y)))))"
@test repr(Expr(:quote, Expr(:exotic_head, Expr(:call, :+, 1, Expr(:$, :y))))) ==
":(:(\$(Expr(:exotic_head, :(1 + \$y)))))"
@test repr(Expr(:block, Expr(:(=), :y, 2),
Expr(:quote, Expr(:exotic_head,
Expr(:call, :+, 1, Expr(:$, :y)))))) ==
"""
quote
y = 2
:(\$(Expr(:exotic_head, :(1 + \$y))))
\$(Expr(:quote, :(\$(Expr(:exotic_head, :(1 + \$(Expr(:\$, :y))))))))
end"""
@test repr(eval(Expr(:block, Expr(:(=), :y, 2),
Expr(:quote, Expr(:exotic_head,
Expand All @@ -1013,20 +944,20 @@ end"""
# nested quotes and blocks
@test_repr "Expr(:quote, Expr(:block, :a, :b))"
@weak_test_repr repr(Expr(:quote, Expr(:block, LineNumberNode(0, :none), :a, LineNumberNode(0, :none), :b)))
@test repr(Expr(:quote, Expr(:block, :a, :b))) ==
@test_broken repr(Expr(:quote, Expr(:block, :a, :b))) ==
":(quote
a
b
end)"
@test_repr "Expr(:quote, Expr(:block, :a))"
@weak_test_repr repr(Expr(:quote, Expr(:block, LineNumberNode(0, :none), :a)))
@test repr(Expr(:quote, Expr(:block, :a))) ==
@test_broken repr(Expr(:quote, Expr(:block, :a))) ==
":(quote
a
end)"
@test_repr "Expr(:quote, Expr(:block, :(a + b)))"
@weak_test_repr repr(Expr(:quote, Expr(:block, LineNumberNode(0, :none), :(a + b))))
@test repr(Expr(:quote, Expr(:block, :(a + b)))) ==
@test_broken repr(Expr(:quote, Expr(:block, :(a + b)))) ==
":(quote
a + b
end)"
Expand All @@ -1037,8 +968,9 @@ end"""
@test_repr ":(QuoteNode(\$x))"
@test_repr ":(:(QuoteNode(\$\$x)))"
@test repr(QuoteNode(Expr(:$, :x))) == ":(\$(QuoteNode(:(\$(Expr(:\$, :x))))))"
@test repr(QuoteNode(Expr(:quote, Expr(:$, :x)))) == ":(\$(QuoteNode(:(:(\$x)))))"
@test repr(Expr(:quote, QuoteNode(Expr(:$, :x)))) == ":(:(\$(QuoteNode(:(\$(Expr(:\$, :x)))))))"
@test repr(QuoteNode(Expr(:quote, Expr(:$, :x)))) == ":(\$(QuoteNode(:(\$(Expr(:quote, :(\$(Expr(:\$, :x)))))))))"
@test repr(Expr(:quote, QuoteNode(Expr(:$, :x)))) == ":(\$(Expr(:quote, :(\$(QuoteNode(:(\$(Expr(:\$, :x)))))))))"
@test repr(Expr(:quote, Expr(:quote, Expr(:foo)))) == ":(\$(Expr(:quote, :(\$(Expr(:quote, :(\$(Expr(:foo)))))))))"

# unquoting
@test_repr "\$y"
Expand All @@ -1061,13 +993,13 @@ y856739 = 2
x856739 = :y856739
z856739 = [:a, :b]
@test_repr repr(:(:(f($$x856739))))
@test repr(:(:(f($$x856739)))) == ":(:(f(\$y856739)))"
@test_broken repr(:(:(f($$x856739)))) == ":(:(f(\$y856739)))"
@test repr(eval(:(:(f($$x856739))))) == ":(f(2))"
@test_repr repr(:(:(f($x856739))))
@test repr(:(:(f($x856739)))) == ":(:(f(\$x856739)))"
@test_broken repr(:(:(f($x856739)))) == ":(:(f(\$x856739)))"
@test repr(eval(:(:(f($x856739))))) == ":(f(y856739))"
@test_repr repr(:(:(f($(($z856739)...)))))
@test repr(:(:(f($(($z856739)...))))) == ":(:(f(\$([:a, :b]...))))"
@test_broken repr(:(:(f($(($z856739)...))))) == ":(:(f(\$([:a, :b]...))))"
@test repr(eval(:(:(f($(($z856739)...)))))) == ":(f(a, b))"

# string interpolation, if this is what the comment in test_rep function
Expand Down