From 93b29d64606a2374bf2b3ccb23bda5bedf05b2d3 Mon Sep 17 00:00:00 2001 From: Jeff Bezanson Date: Wed, 27 Jun 2018 13:50:08 -0400 Subject: [PATCH] fix #27352, disallow `print`/`string` of `nothing` --- base/show.jl | 1 + test/show.jl | 10 ++++++++++ 2 files changed, 11 insertions(+) diff --git a/base/show.jl b/base/show.jl index ee90da6c2acc1..5785792e190ea 100644 --- a/base/show.jl +++ b/base/show.jl @@ -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))) 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)) diff --git a/test/show.jl b/test/show.jl index 2367ec56a4e93..22a5d27066b34 100644 --- a/test/show.jl +++ b/test/show.jl @@ -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`