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 #27352, disallow print/string of nothing #27829

Merged
merged 1 commit into from
Jun 28, 2018
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
2 changes: 1 addition & 1 deletion base/compiler/ssair/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ function print_node(io::IO, idx::Int, @nospecialize(stmt), used, argnames, maxsi
Base.print(io, join(String[sprint(io->print_ssa(io, arg, argnames)) for arg in stmt.args], ", "))
Base.print(io, ")")
else
Base.print(io, stmt)
Base.show(io, stmt)
end
end

Expand Down
1 change: 1 addition & 0 deletions base/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -557,6 +557,7 @@ function show(io::IO, tn::Core.TypeName)
end

show(io::IO, ::Nothing) = print(io, "nothing")
print(io::IO, ::Nothing) = throw(MethodError(print, (io, nothing)))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think almost any other error type would be better. As it is, this will make us print:
“No method matching print(nothing). Did you mean print(nothing) instead?”

show(io::IO, b::Bool) = print(io, b ? "true" : "false")
show(io::IO, n::Signed) = (write(io, string(n)); nothing)
show(io::IO, n::Unsigned) = print(io, "0x", string(n, pad = sizeof(n)<<1, base = 16))
Expand Down
10 changes: 10 additions & 0 deletions test/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1248,3 +1248,13 @@ h_line() = f_line()
│╻╷ f_line
││╻ g_line
││╻ g_line""")

# issue #27352
@test_throws MethodError print(nothing)
@test_throws MethodError print(stdout, nothing)
@test_throws MethodError string(nothing)
@test_throws MethodError string(1, "", nothing)
@test_throws MethodError let x = nothing; "x = $x" end
@test let x = nothing; "x = $(repr(x))" end == "x = nothing"
@test_throws MethodError `/bin/foo $nothing`
@test_throws MethodError `$nothing`