Skip to content

Commit

Permalink
[REPL] try macroexpand alone before lowering
Browse files Browse the repository at this point in the history
Calling macroexpand alone might lead to something simple that we can
handle, while lowering makes it more complicated and usually requires
inference to interpret it.
  • Loading branch information
vtjnash committed Apr 15, 2022
1 parent 7529dbd commit 95bec91
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions stdlib/REPL/src/REPLCompletions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,11 @@ function try_get_type(sym::Expr, fn::Module)
return try_get_type(Expr(:call, GlobalRef(Base, :getindex), sym.args...), fn)
elseif sym.head === :. && sym.args[2] isa QuoteNode # second check catches broadcasting
return try_get_type(Expr(:call, GlobalRef(Core, :getfield), sym.args...), fn)
elseif sym.head === :toplevel || sym.head === :block
isempty(sym.args) && return (nothing, true)
return try_get_type(sym.args[end], fn)
elseif sym.head === :escape || sym.head === :var"hygienic-scope"
return try_get_type(sym.args[1], fn)
end
return (Any, false)
end
Expand All @@ -495,15 +500,22 @@ function get_type(sym::Expr, fn::Module)
found || return Any, false
end
newsym = try
Meta.lower(fn, sym)
macroexpand(fn, sym; recursive=false)
catch e
e isa LoadError && return Any, false
# If e is not a LoadError then Meta.lower crashed in an unexpected way.
# Since this is not a specific to the user code but an internal error,
# rethrow the error to allow reporting it.
rethrow()
# user code failed in macroexpand (ignore it)
return Any, false
end
val, found = try_get_type(newsym, fn)
if !found
newsym = try
Meta.lower(fn, sym)
catch e
# user code failed in lowering (ignore it)
return Any, false
end
val, found = try_get_type(newsym, fn)
end
return try_get_type(newsym, fn)
return val, found
end

function get_type(sym, fn::Module)
Expand Down

0 comments on commit 95bec91

Please sign in to comment.