Skip to content

Commit

Permalink
Deprecate expand(module, ex) to Meta.lower(module, ex).
Browse files Browse the repository at this point in the history
  • Loading branch information
Sacha0 committed Oct 22, 2017
1 parent 175f7a1 commit 687ba4d
Show file tree
Hide file tree
Showing 15 changed files with 79 additions and 77 deletions.
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,9 @@ Deprecated or removed
which now require a module argument.
And it caused the bugfix of other default arguments to use the Main module (including `whos`, `which`).

* `expand(ex)` and `expand(module, ex)` have been deprecated in favor of
`Meta.lower(ex)` ([#24278]).

* The `Operators` module is deprecated. Instead, import required operators explicitly
from `Base`, e.g. `import Base: +, -, *, /` ([#22251]).

Expand Down
4 changes: 2 additions & 2 deletions base/client.jl
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ function eval_user_input(@nospecialize(ast), show_value)
display_error(lasterr,bt)
errcount, lasterr = 0, ()
else
ast = expand(Main, ast)
ast = Meta.lower(Main, ast)
value = eval(Main, ast)
eval(Main, Expr(:body, Expr(:(=), :ans, QuoteNode(value)), Expr(:return, nothing)))
if !(value === nothing) && show_value
Expand Down Expand Up @@ -210,7 +210,7 @@ function parse_input_line(s::String; filename::String="none")
s, sizeof(s), filename, sizeof(filename))
if ex isa Symbol && all(equalto('_'), string(ex))
# remove with 0.7 deprecation
expand(Main, ex) # to get possible warning about using _ as an rvalue
Meta.lower(Main, ex) # to get possible warning about using _ as an rvalue
end
return ex
end
Expand Down
5 changes: 3 additions & 2 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1254,9 +1254,10 @@ _current_module() = ccall(:jl_get_current_module, Ref{Module}, ())
depwarn("binding_module(symbol) is deprecated, use `binding_module(module, symbol)` instead.", :binding_module)
return binding_module(_current_module(), s)
end
export expand
@noinline function expand(@nospecialize(x))
depwarn("expand(x) is deprecated, use `expand(module, x)` instead.", :expand)
return expand(_current_module(), x)
depwarn("expand(x) is deprecated, use `Meta.lower(module, x)` instead.", :expand)
return Meta.lower(_current_module(), x)
end
@noinline function macroexpand(@nospecialize(x))
depwarn("macroexpand(x) is deprecated, use `macroexpand(module, x)` instead.", :macroexpand)
Expand Down
1 change: 0 additions & 1 deletion base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -936,7 +936,6 @@ export

# syntax
esc,
expand,
gensym,
macroexpand,
@macroexpand1,
Expand Down
9 changes: 0 additions & 9 deletions base/expr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,6 @@ copy_exprargs(x::Array{Any,1}) = Any[copy_exprs(a) for a in x]
==(x::Expr, y::Expr) = x.head === y.head && isequal(x.args, y.args)
==(x::QuoteNode, y::QuoteNode) = isequal(x.value, y.value)

"""
expand(m, x)
Takes the expression `x` and returns an equivalent expression in lowered form
for executing in module `m`.
See also [`code_lowered`](@ref).
"""
expand(m::Module, @nospecialize(x)) = ccall(:jl_expand, Any, (Any, Any), x, m)

"""
macroexpand(m::Module, x; recursive=true)
Expand Down
2 changes: 1 addition & 1 deletion base/interactiveutil.jl
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,7 @@ function gen_call_with_extracted_types(__module__, fcn, ex0)
if isa(ex0, Expr) && ex0.head == :macrocall # Make @edit @time 1+2 edit the macro by using the types of the *expressions*
return Expr(:call, fcn, esc(ex0.args[1]), Tuple{#=__source__=#LineNumberNode, #=__module__=#Module, Any[ Core.Typeof(a) for a in ex0.args[3:end] ]...})
end
ex = expand(__module__, ex0)
ex = Meta.lower(__module__, ex0)
exret = Expr(:none)
if !isa(ex, Expr)
exret = Expr(:call, :error, "expression is not a function call or symbol")
Expand Down
9 changes: 9 additions & 0 deletions base/meta.jl
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,13 @@ macro dump(expr)
dump(expr)
end

"""
lower(m, x)
Takes the expression `x` and returns an equivalent expression in lowered form
for executing in module `m`.
See also [`code_lowered`](@ref).
"""
lower(m::Module, @nospecialize(x)) = ccall(:jl_expand, Any, (Any, Any), x, m)

end # module
12 changes: 6 additions & 6 deletions test/broadcast.jl
Original file line number Diff line number Diff line change
Expand Up @@ -318,12 +318,12 @@ end

# make sure scalars are inlined, which causes f.(x,scalar) to lower to a "thunk"
import Base.Meta: isexpr
@test isexpr(expand(Main, :(f.(x,y))), :call)
@test isexpr(expand(Main, :(f.(x,1))), :thunk)
@test isexpr(expand(Main, :(f.(x,1.0))), :thunk)
@test isexpr(expand(Main, :(f.(x,$π))), :thunk)
@test isexpr(expand(Main, :(f.(x,"hello"))), :thunk)
@test isexpr(expand(Main, :(f.(x,$("hello")))), :thunk)
@test isexpr(Meta.lower(Main, :(f.(x,y))), :call)
@test isexpr(Meta.lower(Main, :(f.(x,1))), :thunk)
@test isexpr(Meta.lower(Main, :(f.(x,1.0))), :thunk)
@test isexpr(Meta.lower(Main, :(f.(x,$π))), :thunk)
@test isexpr(Meta.lower(Main, :(f.(x,"hello"))), :thunk)
@test isexpr(Meta.lower(Main, :(f.(x,$("hello")))), :thunk)

# PR #17623: Fused binary operators
@test [true] .* [true] == [true]
Expand Down
24 changes: 12 additions & 12 deletions test/ccall.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1252,18 +1252,18 @@ end
f21104rt(Float64))

# test for malformed syntax errors
@test Expr(:error, "more arguments than types for ccall") == expand(@__MODULE__, :(ccall(:fn, A, (), x)))
@test Expr(:error, "more arguments than types for ccall") == expand(@__MODULE__, :(ccall(:fn, A, (B,), x, y)))
@test Expr(:error, "more arguments than types for ccall") == expand(@__MODULE__, :(ccall(:fn, A, (B,), x, y, z)))
@test Expr(:error, "more arguments than types for ccall") == expand(@__MODULE__, :(ccall(:fn, A, (B,), x, y)))
@test Expr(:error, "more arguments than types for ccall") == expand(@__MODULE__, :(ccall(:fn, A, (B,), x, y, z)))
@test Expr(:error, "more arguments than types for ccall") == expand(@__MODULE__, :(ccall(:fn, A, (B, C), x, y, z)))
@test Expr(:error, "more types than arguments for ccall") == expand(@__MODULE__, :(ccall(:fn, A, (B,),)))
@test Expr(:error, "more types than arguments for ccall") == expand(@__MODULE__, :(ccall(:fn, A, (B, C), )))
@test Expr(:error, "more types than arguments for ccall") == expand(@__MODULE__, :(ccall(:fn, A, (B..., C...), )))
@test Expr(:error, "only the trailing ccall argument type should have '...'") == expand(@__MODULE__, :(ccall(:fn, A, (B..., C...), x)))
@test Expr(:error, "only the trailing ccall argument type should have '...'") == expand(@__MODULE__, :(ccall(:fn, A, (B..., C...), x, y, z)))
@test Expr(:error, "more types than arguments for ccall") == expand(@__MODULE__, :(ccall(:fn, A, (B, C...), )))
@test Expr(:error, "more arguments than types for ccall") == Meta.lower(@__MODULE__, :(ccall(:fn, A, (), x)))
@test Expr(:error, "more arguments than types for ccall") == Meta.lower(@__MODULE__, :(ccall(:fn, A, (B,), x, y)))
@test Expr(:error, "more arguments than types for ccall") == Meta.lower(@__MODULE__, :(ccall(:fn, A, (B,), x, y, z)))
@test Expr(:error, "more arguments than types for ccall") == Meta.lower(@__MODULE__, :(ccall(:fn, A, (B,), x, y)))
@test Expr(:error, "more arguments than types for ccall") == Meta.lower(@__MODULE__, :(ccall(:fn, A, (B,), x, y, z)))
@test Expr(:error, "more arguments than types for ccall") == Meta.lower(@__MODULE__, :(ccall(:fn, A, (B, C), x, y, z)))
@test Expr(:error, "more types than arguments for ccall") == Meta.lower(@__MODULE__, :(ccall(:fn, A, (B,),)))
@test Expr(:error, "more types than arguments for ccall") == Meta.lower(@__MODULE__, :(ccall(:fn, A, (B, C), )))
@test Expr(:error, "more types than arguments for ccall") == Meta.lower(@__MODULE__, :(ccall(:fn, A, (B..., C...), )))
@test Expr(:error, "only the trailing ccall argument type should have '...'") == Meta.lower(@__MODULE__, :(ccall(:fn, A, (B..., C...), x)))
@test Expr(:error, "only the trailing ccall argument type should have '...'") == Meta.lower(@__MODULE__, :(ccall(:fn, A, (B..., C...), x, y, z)))
@test Expr(:error, "more types than arguments for ccall") == Meta.lower(@__MODULE__, :(ccall(:fn, A, (B, C...), )))

# cfunction on non-function singleton
struct CallableSingleton
Expand Down
2 changes: 1 addition & 1 deletion test/codegen.jl
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ end
function test_jl_dump_compiles_toplevel_thunks()
tfile = tempname()
io = open(tfile, "w")
topthunk = expand(Main, :(for i in 1:10; end))
topthunk = Meta.lower(Main, :(for i in 1:10; end))
ccall(:jl_dump_compiles, Void, (Ptr{Void},), io.handle)
Core.eval(Main, topthunk)
ccall(:jl_dump_compiles, Void, (Ptr{Void},), C_NULL)
Expand Down
15 changes: 7 additions & 8 deletions test/goto.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# This file is a part of Julia. License is MIT: https://julialang.org/license

# Basic goto tests
expand(x) = Base.expand(@__MODULE__, x)

function goto_test1()
@goto a
Expand All @@ -15,10 +14,10 @@ end
@test eval(:(@label a)) === nothing

@test Expr(:error, "label \"a\" referenced but not defined") ==
expand(:(@goto a))
Meta.lower(@__MODULE__, :(@goto a))

@test Expr(:error, "label \"a\" defined multiple times") ==
expand(quote
Meta.lower(@__MODULE__, quote
function goto_test2()
@goto a
@label a
Expand All @@ -29,15 +28,15 @@ end


@test Expr(:error, "label \"a\" referenced but not defined") ==
expand(quote
Meta.lower(@__MODULE__, quote
function goto_test3()
@goto a
return
end
end)

@test Expr(:error, "misplaced label") ==
expand(quote
Meta.lower(@__MODULE__, quote
function goto_test4()
@goto a
try
Expand All @@ -60,15 +59,15 @@ macro goto_test5_macro3()
end

@test Expr(:error, "label \"a\" referenced but not defined") ==
expand(quote
Meta.lower(@__MODULE__, quote
function goto_test5_1()
@goto a
@goto_test5_macro1
return
end
end)

let e = expand(quote
let e = Meta.lower(@__MODULE__, quote
function goto_test5_2()
@goto_test5_macro2
@label a
Expand All @@ -89,7 +88,7 @@ end


@test Expr(:error, "goto from a try/finally block is not permitted") ==
expand(quote
Meta.lower(@__MODULE__, quote
function goto_test6()
try
@goto a
Expand Down
Loading

0 comments on commit 687ba4d

Please sign in to comment.