Skip to content

Commit 0ebb521

Browse files
committed
Deprecate expand(module, ex) to Meta.lower(module, ex).
1 parent 175f7a1 commit 0ebb521

19 files changed

+88
-86
lines changed

NEWS.md

+7-4
Original file line numberDiff line numberDiff line change
@@ -378,10 +378,13 @@ Deprecated or removed
378378
* The `bkfact`/`bkfact!` methods that accepted `uplo` and `issymmetric` symbols have been deprecated
379379
in favor of using `Hermitian` (or `Symmetric`) views ([#22605]).
380380

381-
* The function `current_module` is deprecated and replaced with `@__MODULE__` ([#22064]).
382-
This caused the deprecation of some reflection methods (such as `macroexpand` and `isconst`),
383-
which now require a module argument.
384-
And it caused the bugfix of other default arguments to use the Main module (including `whos`, `which`).
381+
* The function `current_module` is deprecated and replaced with `@__MODULE__`.
382+
This caused the deprecation of some reflection methods (such as `macroexpand` and
383+
`isconst`), which now require a module argument. And it caused the bugfix of other
384+
default arguments to use the Main module (including `whos`, `which`) ([#22064]).
385+
386+
* `expand(ex)` and `expand(module, ex)` have been deprecated in favor of
387+
`Meta.lower(module, ex)` ([#22064, #24278]).
385388

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

base/client.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ function eval_user_input(@nospecialize(ast), show_value)
153153
display_error(lasterr,bt)
154154
errcount, lasterr = 0, ()
155155
else
156-
ast = expand(Main, ast)
156+
ast = Meta.lower(Main, ast)
157157
value = eval(Main, ast)
158158
eval(Main, Expr(:body, Expr(:(=), :ans, QuoteNode(value)), Expr(:return, nothing)))
159159
if !(value === nothing) && show_value
@@ -210,7 +210,7 @@ function parse_input_line(s::String; filename::String="none")
210210
s, sizeof(s), filename, sizeof(filename))
211211
if ex isa Symbol && all(equalto('_'), string(ex))
212212
# remove with 0.7 deprecation
213-
expand(Main, ex) # to get possible warning about using _ as an rvalue
213+
Meta.lower(Main, ex) # to get possible warning about using _ as an rvalue
214214
end
215215
return ex
216216
end

base/deprecated.jl

+3-2
Original file line numberDiff line numberDiff line change
@@ -1254,9 +1254,10 @@ _current_module() = ccall(:jl_get_current_module, Ref{Module}, ())
12541254
depwarn("binding_module(symbol) is deprecated, use `binding_module(module, symbol)` instead.", :binding_module)
12551255
return binding_module(_current_module(), s)
12561256
end
1257+
export expand
12571258
@noinline function expand(@nospecialize(x))
1258-
depwarn("expand(x) is deprecated, use `expand(module, x)` instead.", :expand)
1259-
return expand(_current_module(), x)
1259+
depwarn("expand(x) is deprecated, use `Meta.lower(module, x)` instead.", :expand)
1260+
return Meta.lower(_current_module(), x)
12601261
end
12611262
@noinline function macroexpand(@nospecialize(x))
12621263
depwarn("macroexpand(x) is deprecated, use `macroexpand(module, x)` instead.", :macroexpand)

base/exports.jl

-1
Original file line numberDiff line numberDiff line change
@@ -936,7 +936,6 @@ export
936936

937937
# syntax
938938
esc,
939-
expand,
940939
gensym,
941940
macroexpand,
942941
@macroexpand1,

base/expr.jl

-9
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,6 @@ copy_exprargs(x::Array{Any,1}) = Any[copy_exprs(a) for a in x]
4545
==(x::Expr, y::Expr) = x.head === y.head && isequal(x.args, y.args)
4646
==(x::QuoteNode, y::QuoteNode) = isequal(x.value, y.value)
4747

48-
"""
49-
expand(m, x)
50-
51-
Takes the expression `x` and returns an equivalent expression in lowered form
52-
for executing in module `m`.
53-
See also [`code_lowered`](@ref).
54-
"""
55-
expand(m::Module, @nospecialize(x)) = ccall(:jl_expand, Any, (Any, Any), x, m)
56-
5748
"""
5849
macroexpand(m::Module, x; recursive=true)
5950

base/interactiveutil.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ function gen_call_with_extracted_types(__module__, fcn, ex0)
417417
if isa(ex0, Expr) && ex0.head == :macrocall # Make @edit @time 1+2 edit the macro by using the types of the *expressions*
418418
return Expr(:call, fcn, esc(ex0.args[1]), Tuple{#=__source__=#LineNumberNode, #=__module__=#Module, Any[ Core.Typeof(a) for a in ex0.args[3:end] ]...})
419419
end
420-
ex = expand(__module__, ex0)
420+
ex = Meta.lower(__module__, ex0)
421421
exret = Expr(:none)
422422
if !isa(ex, Expr)
423423
exret = Expr(:call, :error, "expression is not a function call or symbol")

base/meta.jl

+9
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,13 @@ macro dump(expr)
5656
dump(expr)
5757
end
5858

59+
"""
60+
lower(m, x)
61+
62+
Takes the expression `x` and returns an equivalent expression in lowered form
63+
for executing in module `m`.
64+
See also [`code_lowered`](@ref).
65+
"""
66+
lower(m::Module, @nospecialize(x)) = ccall(:jl_expand, Any, (Any, Any), x, m)
67+
5968
end # module

base/repl/REPLCompletions.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -348,7 +348,7 @@ function get_type(sym::Expr, fn::Module)
348348
# try to analyze nests of calls. if this fails, try using the expanded form.
349349
val, found = try_get_type(sym, fn)
350350
found && return val, found
351-
return try_get_type(expand(fn, sym), fn)
351+
return try_get_type(Meta.lower(fn, sym), fn)
352352
end
353353

354354
function get_type(sym, fn::Module)

doc/src/devdocs/eval.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ the expression. Macro expansion involves a handoff from [`eval()`](@ref) (in Jul
7878
function `jl_macroexpand()` (written in `flisp`) to the Julia macro itself (written in - what
7979
else - Julia) via `fl_invoke_julia_macro()`, and back.
8080

81-
Typically, macro expansion is invoked as a first step during a call to [`expand()`](@ref)/`jl_expand()`,
81+
Typically, macro expansion is invoked as a first step during a call to [`Meta.lower()`](@ref)/`jl_expand()`,
8282
although it can also be invoked directly by a call to [`macroexpand()`](@ref)/`jl_macroexpand()`.
8383

8484
## [Type Inference](@id dev-type-inference)

doc/src/devdocs/reflection.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,12 @@ julia> macroexpand(@__MODULE__, :(@edit println("")) )
9191
The functions `Base.Meta.show_sexpr` and [`dump`](@ref) are used to display S-expr style views
9292
and depth-nested detail views for any expression.
9393

94-
Finally, the [`expand`](@ref) function gives the `lowered` form of any expression and is of
94+
Finally, the [`Meta.lower`](@ref) function gives the `lowered` form of any expression and is of
9595
particular interest for understanding both macros and top-level statements such as function declarations
9696
and variable assignments:
9797

9898
```jldoctest
99-
julia> expand(@__MODULE__, :(f() = 1) )
99+
julia> Meta.lower(@__MODULE__, :(f() = 1) )
100100
:(begin
101101
$(Expr(:method, :f))
102102
$(Expr(:method, :f, :((Core.svec)((Core.svec)((Core.Typeof)(f)), (Core.svec)())), CodeInfo(:(begin

doc/src/stdlib/base.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -344,10 +344,10 @@ Base.@functionloc
344344
```@docs
345345
Base.gc
346346
Base.gc_enable
347+
Meta.lower
347348
Base.macroexpand
348349
Base.@macroexpand
349350
Base.@macroexpand1
350-
Base.expand
351351
Base.code_lowered
352352
Base.@code_lowered
353353
Base.code_typed

test/broadcast.jl

+6-6
Original file line numberDiff line numberDiff line change
@@ -318,12 +318,12 @@ end
318318

319319
# make sure scalars are inlined, which causes f.(x,scalar) to lower to a "thunk"
320320
import Base.Meta: isexpr
321-
@test isexpr(expand(Main, :(f.(x,y))), :call)
322-
@test isexpr(expand(Main, :(f.(x,1))), :thunk)
323-
@test isexpr(expand(Main, :(f.(x,1.0))), :thunk)
324-
@test isexpr(expand(Main, :(f.(x,$π))), :thunk)
325-
@test isexpr(expand(Main, :(f.(x,"hello"))), :thunk)
326-
@test isexpr(expand(Main, :(f.(x,$("hello")))), :thunk)
321+
@test isexpr(Meta.lower(Main, :(f.(x,y))), :call)
322+
@test isexpr(Meta.lower(Main, :(f.(x,1))), :thunk)
323+
@test isexpr(Meta.lower(Main, :(f.(x,1.0))), :thunk)
324+
@test isexpr(Meta.lower(Main, :(f.(x,$π))), :thunk)
325+
@test isexpr(Meta.lower(Main, :(f.(x,"hello"))), :thunk)
326+
@test isexpr(Meta.lower(Main, :(f.(x,$("hello")))), :thunk)
327327

328328
# PR #17623: Fused binary operators
329329
@test [true] .* [true] == [true]

test/ccall.jl

+12-12
Original file line numberDiff line numberDiff line change
@@ -1252,18 +1252,18 @@ end
12521252
f21104rt(Float64))
12531253

12541254
# test for malformed syntax errors
1255-
@test Expr(:error, "more arguments than types for ccall") == expand(@__MODULE__, :(ccall(:fn, A, (), x)))
1256-
@test Expr(:error, "more arguments than types for ccall") == expand(@__MODULE__, :(ccall(:fn, A, (B,), x, y)))
1257-
@test Expr(:error, "more arguments than types for ccall") == expand(@__MODULE__, :(ccall(:fn, A, (B,), x, y, z)))
1258-
@test Expr(:error, "more arguments than types for ccall") == expand(@__MODULE__, :(ccall(:fn, A, (B,), x, y)))
1259-
@test Expr(:error, "more arguments than types for ccall") == expand(@__MODULE__, :(ccall(:fn, A, (B,), x, y, z)))
1260-
@test Expr(:error, "more arguments than types for ccall") == expand(@__MODULE__, :(ccall(:fn, A, (B, C), x, y, z)))
1261-
@test Expr(:error, "more types than arguments for ccall") == expand(@__MODULE__, :(ccall(:fn, A, (B,),)))
1262-
@test Expr(:error, "more types than arguments for ccall") == expand(@__MODULE__, :(ccall(:fn, A, (B, C), )))
1263-
@test Expr(:error, "more types than arguments for ccall") == expand(@__MODULE__, :(ccall(:fn, A, (B..., C...), )))
1264-
@test Expr(:error, "only the trailing ccall argument type should have '...'") == expand(@__MODULE__, :(ccall(:fn, A, (B..., C...), x)))
1265-
@test Expr(:error, "only the trailing ccall argument type should have '...'") == expand(@__MODULE__, :(ccall(:fn, A, (B..., C...), x, y, z)))
1266-
@test Expr(:error, "more types than arguments for ccall") == expand(@__MODULE__, :(ccall(:fn, A, (B, C...), )))
1255+
@test Expr(:error, "more arguments than types for ccall") == Meta.lower(@__MODULE__, :(ccall(:fn, A, (), x)))
1256+
@test Expr(:error, "more arguments than types for ccall") == Meta.lower(@__MODULE__, :(ccall(:fn, A, (B,), x, y)))
1257+
@test Expr(:error, "more arguments than types for ccall") == Meta.lower(@__MODULE__, :(ccall(:fn, A, (B,), x, y, z)))
1258+
@test Expr(:error, "more arguments than types for ccall") == Meta.lower(@__MODULE__, :(ccall(:fn, A, (B,), x, y)))
1259+
@test Expr(:error, "more arguments than types for ccall") == Meta.lower(@__MODULE__, :(ccall(:fn, A, (B,), x, y, z)))
1260+
@test Expr(:error, "more arguments than types for ccall") == Meta.lower(@__MODULE__, :(ccall(:fn, A, (B, C), x, y, z)))
1261+
@test Expr(:error, "more types than arguments for ccall") == Meta.lower(@__MODULE__, :(ccall(:fn, A, (B,),)))
1262+
@test Expr(:error, "more types than arguments for ccall") == Meta.lower(@__MODULE__, :(ccall(:fn, A, (B, C), )))
1263+
@test Expr(:error, "more types than arguments for ccall") == Meta.lower(@__MODULE__, :(ccall(:fn, A, (B..., C...), )))
1264+
@test Expr(:error, "only the trailing ccall argument type should have '...'") == Meta.lower(@__MODULE__, :(ccall(:fn, A, (B..., C...), x)))
1265+
@test Expr(:error, "only the trailing ccall argument type should have '...'") == Meta.lower(@__MODULE__, :(ccall(:fn, A, (B..., C...), x, y, z)))
1266+
@test Expr(:error, "more types than arguments for ccall") == Meta.lower(@__MODULE__, :(ccall(:fn, A, (B, C...), )))
12671267

12681268
# cfunction on non-function singleton
12691269
struct CallableSingleton

test/codegen.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ end
6767
function test_jl_dump_compiles_toplevel_thunks()
6868
tfile = tempname()
6969
io = open(tfile, "w")
70-
topthunk = expand(Main, :(for i in 1:10; end))
70+
topthunk = Meta.lower(Main, :(for i in 1:10; end))
7171
ccall(:jl_dump_compiles, Void, (Ptr{Void},), io.handle)
7272
Core.eval(Main, topthunk)
7373
ccall(:jl_dump_compiles, Void, (Ptr{Void},), C_NULL)

test/goto.jl

+7-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
# This file is a part of Julia. License is MIT: https://julialang.org/license
22

33
# Basic goto tests
4-
expand(x) = Base.expand(@__MODULE__, x)
54

65
function goto_test1()
76
@goto a
@@ -15,10 +14,10 @@ end
1514
@test eval(:(@label a)) === nothing
1615

1716
@test Expr(:error, "label \"a\" referenced but not defined") ==
18-
expand(:(@goto a))
17+
Meta.lower(@__MODULE__, :(@goto a))
1918

2019
@test Expr(:error, "label \"a\" defined multiple times") ==
21-
expand(quote
20+
Meta.lower(@__MODULE__, quote
2221
function goto_test2()
2322
@goto a
2423
@label a
@@ -29,15 +28,15 @@ end
2928

3029

3130
@test Expr(:error, "label \"a\" referenced but not defined") ==
32-
expand(quote
31+
Meta.lower(@__MODULE__, quote
3332
function goto_test3()
3433
@goto a
3534
return
3635
end
3736
end)
3837

3938
@test Expr(:error, "misplaced label") ==
40-
expand(quote
39+
Meta.lower(@__MODULE__, quote
4140
function goto_test4()
4241
@goto a
4342
try
@@ -60,15 +59,15 @@ macro goto_test5_macro3()
6059
end
6160

6261
@test Expr(:error, "label \"a\" referenced but not defined") ==
63-
expand(quote
62+
Meta.lower(@__MODULE__, quote
6463
function goto_test5_1()
6564
@goto a
6665
@goto_test5_macro1
6766
return
6867
end
6968
end)
7069

71-
let e = expand(quote
70+
let e = Meta.lower(@__MODULE__, quote
7271
function goto_test5_2()
7372
@goto_test5_macro2
7473
@label a
@@ -89,7 +88,7 @@ end
8988

9089

9190
@test Expr(:error, "goto from a try/finally block is not permitted") ==
92-
expand(quote
91+
Meta.lower(@__MODULE__, quote
9392
function goto_test6()
9493
try
9594
@goto a

0 commit comments

Comments
 (0)