Skip to content

Commit 434b133

Browse files
committed
replace current_module() with @__MODULE__
passes macros a __module__ argument, like the __source__ argument, allowing them to inspect and configure themselves based on their evaluation context remove current_module dependence from `include`: instead, we use the same trick as for `eval` (they are basically the same function after all) to create a module-relative function in each non-bare module now puts Module in MethodInstance.def for toplevel thunks to make it convenient to pass around this contextual information
1 parent 9e3318c commit 434b133

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

76 files changed

+1106
-940
lines changed

Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,7 @@ $(build_private_libdir)/%.$(SHLIB_EXT): $(build_private_libdir)/%.o
186186

187187
CORE_SRCS := $(addprefix $(JULIAHOME)/, \
188188
base/boot.jl base/coreimg.jl \
189+
base/docs/core.jl \
189190
base/abstractarray.jl \
190191
base/array.jl \
191192
base/bool.jl \

base/Enums.jl

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,34 +48,34 @@ julia> f(apple)
4848
`BaseType`, which defaults to `Int32`, must be a primitive subtype of Integer. Member values can be converted between
4949
the enum type and `BaseType`. `read` and `write` perform these conversions automatically.
5050
"""
51-
macro enum(T,syms...)
51+
macro enum(T, syms...)
5252
if isempty(syms)
5353
throw(ArgumentError("no arguments given for Enum $T"))
5454
end
5555
basetype = Int32
5656
typename = T
57-
if isa(T,Expr) && T.head == :(::) && length(T.args) == 2 && isa(T.args[1], Symbol)
57+
if isa(T, Expr) && T.head == :(::) && length(T.args) == 2 && isa(T.args[1], Symbol)
5858
typename = T.args[1]
59-
basetype = eval(current_module(),T.args[2])
59+
basetype = eval(__module__, T.args[2])
6060
if !isa(basetype, DataType) || !(basetype <: Integer) || !isbits(basetype)
6161
throw(ArgumentError("invalid base type for Enum $typename, $T=::$basetype; base type must be an integer primitive type"))
6262
end
63-
elseif !isa(T,Symbol)
63+
elseif !isa(T, Symbol)
6464
throw(ArgumentError("invalid type expression for enum $T"))
6565
end
6666
vals = Vector{Tuple{Symbol,Integer}}(0)
6767
lo = hi = 0
6868
i = zero(basetype)
6969
hasexpr = false
7070
for s in syms
71-
if isa(s,Symbol)
71+
if isa(s, Symbol)
7272
if i == typemin(basetype) && !isempty(vals)
7373
throw(ArgumentError("overflow in value \"$s\" of Enum $typename"))
7474
end
75-
elseif isa(s,Expr) &&
75+
elseif isa(s, Expr) &&
7676
(s.head == :(=) || s.head == :kw) &&
77-
length(s.args) == 2 && isa(s.args[1],Symbol)
78-
i = eval(current_module(),s.args[2]) # allow exprs, e.g. uint128"1"
77+
length(s.args) == 2 && isa(s.args[1], Symbol)
78+
i = eval(__module__, s.args[2]) # allow exprs, e.g. uint128"1"
7979
if !isa(i, Integer)
8080
throw(ArgumentError("invalid value for Enum $typename, $s=$i; values must be integers"))
8181
end
@@ -143,7 +143,7 @@ macro enum(T,syms...)
143143
end
144144
end
145145
end
146-
if isa(typename,Symbol)
146+
if isa(typename, Symbol)
147147
for (sym,i) in vals
148148
push!(blk.args, :(const $(esc(sym)) = $(esc(typename))($i)))
149149
end

base/REPLCompletions.jl

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -292,9 +292,10 @@ function get_type_call(expr::Expr)
292292
return_type === nothing && return (Any, false)
293293
return (return_type, true)
294294
end
295-
# Returns the return type. example: get_type(:(Base.strip("",' ')),Main) returns (String,true)
296-
function get_type(sym::Expr, fn)
297-
sym=expand(sym)
295+
296+
# Returns the return type. example: get_type(:(Base.strip("", ' ')), Main) returns (String, true)
297+
function get_type(sym::Expr, fn::Module)
298+
sym = expand(fn, sym)
298299
val, found = get_value(sym, fn)
299300
found && return Base.typesof(val).parameters[1], found
300301
if sym.head === :call
@@ -310,10 +311,11 @@ function get_type(sym::Expr, fn)
310311
end
311312
return (Any, false)
312313
end
313-
function get_type(sym, fn)
314+
function get_type(sym, fn::Module)
314315
val, found = get_value(sym, fn)
315316
return found ? Base.typesof(val).parameters[1] : Any, found
316317
end
318+
317319
# Method completion on function call expression that look like :(max(1))
318320
function complete_methods(ex_org::Expr)
319321
args_ex = Any[]

base/boot.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ String(s::String) = s # no constructor yet
230230
# This should always be inlined
231231
getptls() = ccall(:jl_get_ptls_states, Ptr{Void}, ())
232232

233-
include(fname::String) = ccall(:jl_load_, Any, (Any,), fname)
233+
include(m::Module, fname::String) = ccall(:jl_load_, Any, (Any, Any), m, fname)
234234

235235
eval(e::ANY) = eval(Main, e)
236236
eval(m::Module, e::ANY) = ccall(:jl_toplevel_eval_in, Any, (Any, Any), m, e)
@@ -337,15 +337,15 @@ end
337337

338338
# docsystem basics
339339
macro doc(x...)
340-
atdoc(x...)
340+
atdoc(__source__, __module__, x...)
341341
end
342342
macro __doc__(x)
343343
Expr(:escape, Expr(:block, Expr(:meta, :doc), x))
344344
end
345345
macro doc_str(s)
346346
Expr(:escape, s)
347347
end
348-
atdoc = (str, expr) -> Expr(:escape, expr)
348+
atdoc = (source, mod, str, expr) -> Expr(:escape, expr)
349349
atdoc!(λ) = global atdoc = λ
350350

351351

@@ -382,4 +382,4 @@ show(a::ANY) = show(STDOUT, a)
382382
print(a::ANY...) = print(STDOUT, a...)
383383
println(a::ANY...) = println(STDOUT, a...)
384384

385-
ccall(:jl_set_istopmod, Void, (Bool,), true)
385+
ccall(:jl_set_istopmod, Void, (Any, Bool), Core, true)

base/client.jl

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ function eval_user_input(ast::ANY, show_value)
153153
display_error(lasterr,bt)
154154
errcount, lasterr = 0, ()
155155
else
156-
ast = expand(ast)
156+
ast = expand(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 === :_
212212
# remove with 0.6 deprecation
213-
expand(ex) # to get possible warning about using _ as an rvalue
213+
expand(Main, ex) # to get possible warning about using _ as an rvalue
214214
end
215215
return ex
216216
end
@@ -243,7 +243,7 @@ function incomplete_tag(ex::Expr)
243243
end
244244

245245
# try to include() a file, ignoring if not found
246-
try_include(path::AbstractString) = isfile(path) && include(path)
246+
try_include(mod::Module, path::AbstractString) = isfile(path) && include(mod, path)
247247

248248
function process_options(opts::JLOptions)
249249
if !isempty(ARGS)
@@ -279,7 +279,7 @@ function process_options(opts::JLOptions)
279279
# load file immediately on all processors
280280
if opts.load != C_NULL
281281
@sync for p in procs()
282-
@async remotecall_fetch(include, p, unsafe_string(opts.load))
282+
@async remotecall_fetch(include, p, Main, unsafe_string(opts.load))
283283
end
284284
end
285285
# eval expression
@@ -304,7 +304,7 @@ function process_options(opts::JLOptions)
304304
if !is_interactive
305305
ccall(:jl_exit_on_sigint, Void, (Cint,), 1)
306306
end
307-
include(PROGRAM_FILE)
307+
include(Main, PROGRAM_FILE)
308308
end
309309
break
310310
end
@@ -315,12 +315,13 @@ end
315315
function load_juliarc()
316316
# If the user built us with a specific Base.SYSCONFDIR, check that location first for a juliarc.jl file
317317
# If it is not found, then continue on to the relative path based on JULIA_HOME
318-
if !isempty(Base.SYSCONFDIR) && isfile(joinpath(JULIA_HOME,Base.SYSCONFDIR,"julia","juliarc.jl"))
319-
include(abspath(JULIA_HOME,Base.SYSCONFDIR,"julia","juliarc.jl"))
318+
if !isempty(Base.SYSCONFDIR) && isfile(joinpath(JULIA_HOME, Base.SYSCONFDIR, "julia", "juliarc.jl"))
319+
include(Main, abspath(JULIA_HOME, Base.SYSCONFDIR, "julia", "juliarc.jl"))
320320
else
321-
try_include(abspath(JULIA_HOME,"..","etc","julia","juliarc.jl"))
321+
try_include(Main, abspath(JULIA_HOME, "..", "etc", "julia", "juliarc.jl"))
322322
end
323-
try_include(abspath(homedir(),".juliarc.jl"))
323+
try_include(Main, abspath(homedir(), ".juliarc.jl"))
324+
nothing
324325
end
325326

326327
function load_machine_file(path::AbstractString)
@@ -363,12 +364,13 @@ function __atreplinit(repl)
363364
end
364365
end
365366
end
366-
_atreplinit(repl) = @eval Main $__atreplinit($repl)
367+
_atreplinit(repl) = invokelatest(__atreplinit, repl)
367368

368369
function _start()
369370
empty!(ARGS)
370371
append!(ARGS, Core.ARGS)
371372
opts = JLOptions()
373+
@eval Main include(x) = $include(Main, x)
372374
try
373375
(quiet,repl,startup,color_set,history_file) = process_options(opts)
374376

base/coreimg.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ Main.Core.eval(Main.Core, :(baremodule Inference
44
using Core.Intrinsics
55
import Core: print, println, show, write, unsafe_write, STDOUT, STDERR
66

7-
ccall(:jl_set_istopmod, Void, (Bool,), false)
7+
ccall(:jl_set_istopmod, Void, (Any, Bool), Inference, false)
88

99
eval(x) = Core.eval(Inference, x)
1010
eval(m, x) = Core.eval(m, x)
1111

12-
const include = Core.include
12+
include(x) = Core.include(Inference, x)
13+
include(mod, x) = Core.include(mod, x)
14+
1315
# conditional to allow redefining Core.Inference after base exists
1416
isdefined(Main, :Base) || ((::Type{T})(arg) where {T} = convert(T, arg)::T)
1517

base/deprecated.jl

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -103,14 +103,13 @@ function firstcaller(bt::Array{Ptr{Void},1}, funcsyms)
103103
return lkup
104104
end
105105

106-
deprecate(s::Symbol) = deprecate(current_module(), s)
107106
deprecate(m::Module, s::Symbol) = ccall(:jl_deprecate_binding, Void, (Any, Any), m, s)
108107

109108
macro deprecate_binding(old, new, export_old=true)
110-
Expr(:toplevel,
109+
return Expr(:toplevel,
111110
export_old ? Expr(:export, esc(old)) : nothing,
112111
Expr(:const, Expr(:(=), esc(old), esc(new))),
113-
Expr(:call, :deprecate, Expr(:quote, old)))
112+
Expr(:call, :deprecate, __module__, Expr(:quote, old)))
114113
end
115114

116115
# BEGIN 0.6-alpha deprecations (delete when 0.6 is released)
@@ -630,7 +629,7 @@ for (Bsig, A1sig, A2sig, gbb, funcname) in
630629
func = @get! cache f gen_broadcast_function_sparse($gbb, f, ($A1sig) <: SparseMatrixCSC)
631630
# need eval because func was just created by gen_broadcast_function_sparse
632631
# TODO: convert this to a generated function
633-
eval(current_module(), Expr(:body, Expr(:return, Expr(:call, QuoteNode(func), QuoteNode(B), QuoteNode(A1), QuoteNode(A2)))))
632+
eval(_current_module(), Expr(:body, Expr(:return, Expr(:call, QuoteNode(func), QuoteNode(B), QuoteNode(A1), QuoteNode(A2)))))
634633
return B
635634
end
636635
end # let broadcast_cache
@@ -1348,6 +1347,43 @@ end
13481347
@deprecate srand(filename::AbstractString, n::Integer=4) srand(read!(filename, Array{UInt32}(Int(n))))
13491348
@deprecate MersenneTwister(filename::AbstractString) srand(MersenneTwister(0), read!(filename, Array{UInt32}(Int(4))))
13501349

1350+
_current_module() = ccall(:jl_get_current_module, Ref{Module}, ())
1351+
@noinline function binding_module(s::Symbol)
1352+
depwarn("binding_module(symbol) is deprecated, use `binding_module(__module__, symbol)` instead.", :binding_module)
1353+
return binding_module(_current_module(), s)
1354+
end
1355+
@noinline function expand(x::ANY)
1356+
depwarn("expand(x) is deprecated, use `expand(__module__, x)` instead.", :expand)
1357+
return expand(_current_module(), x)
1358+
end
1359+
@noinline function macroexpand(x::ANY)
1360+
depwarn("macroexpand(x) is deprecated, use `macroexpand(__module__, x)` instead.", :macroexpand)
1361+
return macroexpand(_current_module(), x)
1362+
end
1363+
@noinline function isconst(s::Symbol)
1364+
depwarn("isconst(symbol) is deprecated, use `isconst(__module__, symbol)` instead.", :isconst)
1365+
return isconst(_current_module(), s)
1366+
end
1367+
@noinline function include_string(txt::AbstractString, fname::AbstractString)
1368+
depwarn("include_string(string, fname) is deprecated, use `include_string(__module__, string, fname)` instead.", :include_string)
1369+
return include_string(_current_module(), txt, fname)
1370+
end
1371+
1372+
"""
1373+
current_module() -> Module
1374+
1375+
Get the *dynamically* current `Module`, which is the `Module` code is currently being read
1376+
from. In general, this is not the same as the module containing the call to this function.
1377+
1378+
DEPRECATED: use @__MODULE__ instead
1379+
"""
1380+
@noinline function current_module()
1381+
depwarn("current_module() is deprecated, use `@__MODULE__` instead.", :current_module)
1382+
return _current_module()
1383+
end
1384+
export current_module
1385+
1386+
13511387
# END 0.7 deprecations
13521388

13531389
# BEGIN 1.0 deprecations

0 commit comments

Comments
 (0)