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

RFC / WIP: no longer warn in code_warntype for variables not used in the body #23280

Merged
merged 1 commit into from
Aug 21, 2017
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
25 changes: 23 additions & 2 deletions base/interactiveutil.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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, " <optimized out>")
end
print(emph_io, '\n')
end
Expand Down
5 changes: 5 additions & 0 deletions test/reflection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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{}, "<optimized out>")

module ImportIntrinsics15819
# Make sure changing the lookup path of an intrinsic doesn't break
# the heuristic for type instability warning.
Expand Down