Skip to content

Commit

Permalink
Add code_warntype
Browse files Browse the repository at this point in the history
  • Loading branch information
timholy committed Dec 30, 2014
1 parent 141bc79 commit f658f78
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 8 deletions.
2 changes: 2 additions & 0 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1069,6 +1069,7 @@ export
current_module,
edit,
code_typed,
code_warntype,
code_lowered,
code_llvm,
code_native,
Expand Down Expand Up @@ -1375,6 +1376,7 @@ export
@edit,
@less,
@code_typed,
@code_warntype,
@code_lowered,
@code_llvm,
@code_native,
Expand Down
25 changes: 24 additions & 1 deletion base/interactiveutil.jl
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,29 @@ function which(f, t::(Type...))
ms[1]
end

# displaying type-ambiguity warnings

function code_warntype(f, t::(Type...))
global show_expr_type_colorize
state = show_expr_type_colorize::Bool
ct = code_typed(f, t)
show_expr_type_colorize::Bool = true
for ast in ct
println(STDOUT, "Variables:")
vars = ast.args[2][2]
for v in vars
print(STDOUT, " ", v[1])
show_expr_type(STDOUT, v[2])
print(STDOUT, '\n')
end
print(STDOUT, "\nBody:\n ")
show_unquoted(STDOUT, ast.args[3], 2)
print(STDOUT, '\n')
end
show_expr_type_colorize::Bool = false
nothing
end

typesof(args...) = map(a->(isa(a,Type) ? Type{a} : typeof(a)), args)

function gen_call_with_extracted_types(fcn, ex0)
Expand Down Expand Up @@ -248,7 +271,7 @@ function gen_call_with_extracted_types(fcn, ex0)
exret
end

for fname in [:which, :less, :edit, :code_typed, :code_lowered, :code_llvm, :code_native]
for fname in [:which, :less, :edit, :code_typed, :code_warntype, :code_lowered, :code_llvm, :code_native]
@eval begin
macro ($fname)(ex0)
gen_call_with_extracted_types($(Expr(:quote,fname)), ex0)
Expand Down
52 changes: 45 additions & 7 deletions base/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -290,15 +290,20 @@ unquoted(ex::Expr) = ex.args[1]
## AST printing helpers ##

const indent_width = 4
show_expr_type_colorize = false

function show_expr_type(io::IO, ty)
if !is(ty, Any)
if is(ty, Function)
print(io, "::F")
elseif is(ty, IntrinsicFunction)
print(io, "::I")
if is(ty, Function)
print(io, "::F")
elseif is(ty, IntrinsicFunction)
print(io, "::I")
else
if show_expr_type_colorize::Bool && !isleaftype(ty)
print_with_color(:red, io, "::$ty")
else
print(io, "::$ty")
if !is(ty, Any)
print(io, "::$ty")
end
end
end
end
Expand Down Expand Up @@ -409,6 +414,9 @@ end
# TODO: implement interpolated strings
function show_unquoted(io::IO, ex::Expr, indent::Int, prec::Int)
head, args, nargs = ex.head, ex.args, length(ex.args)
show_type = true
global show_expr_type_colorize
state = show_expr_type_colorize::Bool

# dot (i.e. "x.y")
if is(head, :(.))
Expand Down Expand Up @@ -460,6 +468,10 @@ function show_unquoted(io::IO, ex::Expr, indent::Int, prec::Int)
func_prec = operator_precedence(func)
func_args = args[2:end]

if in(ex.args[1], (:box, TopNode(:box), :throw)) || ismodulecall(ex)
show_expr_type_colorize::Bool = show_type = false
end

# scalar multiplication (i.e. "100x")
if (func == :(*) && length(func_args)==2 &&
isa(func_args[1], Real) && isa(func_args[2], Symbol))
Expand Down Expand Up @@ -579,6 +591,7 @@ function show_unquoted(io::IO, ex::Expr, indent::Int, prec::Int)
show_list(io, args, ' ', indent)

elseif is(head, :line) && 1 <= nargs <= 2
show_type = false
show_linenumber(io, args...)

elseif is(head, :if) && nargs == 3 # if/else
Expand Down Expand Up @@ -660,6 +673,7 @@ function show_unquoted(io::IO, ex::Expr, indent::Int, prec::Int)

# print anything else as "Expr(head, args...)"
else
show_expr_type_colorize::Bool = show_type = false
print(io, "\$(Expr(")
show(io, ex.head)
for arg in args
Expand All @@ -669,7 +683,31 @@ function show_unquoted(io::IO, ex::Expr, indent::Int, prec::Int)
print(io, "))")
end

show_expr_type(io, ex.typ)
if ex.head == :(=)
show_type = show_type_assignment(ex.args[2])
elseif in(ex.head, (:boundscheck, :gotoifnot, :return))
show_type = false
end

if show_type
show_expr_type(io, ex.typ)
end

show_expr_type_colorize::Bool = state
end

show_type_assignment(::Number) = false
show_type_assignment(::(Number...)) = false
show_type_assignment(::Expr) = false
show_type_assignment(::SymbolNode) = false
show_type_assignment(::LambdaStaticData) = false
show_type_assignment(a) = true

function ismodulecall(ex::Expr)
ex.head == :call && ex.args[1] == TopNode(:getfield) &&
isa(ex.args[2], Symbol) &&
isdefined(current_module(), ex.args[2]) &&
isa(getfield(current_module(), ex.args[2]), Module)
end

# dump & xdump - structured tree representation like R's str()
Expand Down

0 comments on commit f658f78

Please sign in to comment.