From 5c0c3923389df75bbf529c4a78976ee1f961fadd Mon Sep 17 00:00:00 2001 From: Kristoffer Carlsson Date: Wed, 16 Aug 2017 23:21:12 +0200 Subject: [PATCH] no longer warn in code_warntype for unused variables --- base/interactiveutil.jl | 25 +++++++++++++++++++++++-- test/reflection.jl | 5 +++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/base/interactiveutil.jl b/base/interactiveutil.jl index 4ad8d2795ad6f..1e50a94bebbf5 100644 --- a/base/interactiveutil.jl +++ b/base/interactiveutil.jl @@ -347,14 +347,35 @@ problematic for performance, so the results need to be used judiciously. See [`@code_warntype`](@ref man-code-warntype) for more information. """ function code_warntype(io::IO, f, @nospecialize(t)) + function slots_used(ci, slotnames) + used = falses(length(slotnames)) + scan_exprs!(used, ci.code) + return used + end + + function scan_exprs!(used, exprs) + for ex in exprs + if isa(ex, Slot) + used[ex.id] = true + elseif isa(ex, Expr) + scan_exprs!(used, ex.args) + end + end + end + emph_io = IOContext(io, :TYPEEMPHASIZE => true) for (src, rettype) in code_typed(f, t) println(emph_io, "Variables:") slotnames = sourceinfo_slotnames(src) + used_slotids = slots_used(src, slotnames) for i = 1:length(slotnames) print(emph_io, " ", slotnames[i]) - if isa(src.slottypes, Array) - show_expr_type(emph_io, src.slottypes[i], true) + if used_slotids[i] + if isa(src.slottypes, Array) + show_expr_type(emph_io, src.slottypes[i], true) + end + else + print(emph_io, " ") end print(emph_io, '\n') end diff --git a/test/reflection.jl b/test/reflection.jl index b77e7d4e778a8..45ae7bf78a654 100644 --- a/test/reflection.jl +++ b/test/reflection.jl @@ -92,6 +92,11 @@ show(iob, expand(Main, :(x -> x^2))) str = String(take!(iob)) @test isempty(search(str, tag)) +# Make sure non used variables are not emphasized +has_unused() = (a = rand(5)) +@test !warntype_hastag(has_unused, Tuple{}, tag) +@test warntype_hastag(has_unused, Tuple{}, "") + module ImportIntrinsics15819 # Make sure changing the lookup path of an intrinsic doesn't break # the heuristic for type instability warning.