Skip to content

Commit

Permalink
Deprecate module_name to a general nameof function
Browse files Browse the repository at this point in the history
  • Loading branch information
ararslan committed Jan 22, 2018
1 parent 3d0ab52 commit db2148e
Show file tree
Hide file tree
Showing 23 changed files with 41 additions and 35 deletions.
5 changes: 4 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -964,6 +964,8 @@ Deprecated or removed

* `findin(a, b)` has been deprecated in favor of `findall(occursin(b), a)` ([#24673]).

* `module_name` has been deprecated in favor of a new, general `nameof` function ([#25622]).

* The module `Random.dSFMT` is renamed `Random.DSFMT` ([#25567]).

* `Random.RandomDevice(unlimited::Bool)` (on non-Windows systems) is deprecated in favor of
Expand Down Expand Up @@ -1239,5 +1241,6 @@ Command-line option changes
[#25532]: https://github.com/JuliaLang/julia/issues/25532
[#25545]: https://github.com/JuliaLang/julia/issues/25545
[#25616]: https://github.com/JuliaLang/julia/issues/25616
[#25622]: https://github.com/JuliaLang/julia/issues/25622
[#25634]: https://github.com/JuliaLang/julia/issues/25634
[#25654]: https://github.com/JuliaLang/julia/issues/25654
[#25654]: https://github.com/JuliaLang/julia/issues/25654
2 changes: 1 addition & 1 deletion base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ AbstractArray
convert(::Type{T}, a::T) where {T<:AbstractArray} = a
convert(::Type{T}, a::AbstractArray) where {T<:AbstractArray} = T(a)

if module_name(@__MODULE__) === :Base # avoid method overwrite
if nameof(@__MODULE__) === :Base # avoid method overwrite
# catch undefined constructors before the deprecation kicks in
# TODO: remove when deprecation is removed
function (::Type{T})(arg) where {T<:AbstractArray}
Expand Down
2 changes: 1 addition & 1 deletion base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -411,7 +411,7 @@ promote_rule(a::Type{Array{T,n}}, b::Type{Array{S,n}}) where {T,n,S} = el_same(p

## Constructors ##

if module_name(@__MODULE__) === :Base # avoid method overwrite
if nameof(@__MODULE__) === :Base # avoid method overwrite
# constructors should make copies
Array{T,N}(x::AbstractArray{S,N}) where {T,N,S} = copyto!(Array{T,N}(uninitialized, size(x)), x)
AbstractArray{T,N}(A::AbstractArray{S,N}) where {T,N,S} = copyto!(similar(A,T), A)
Expand Down
2 changes: 1 addition & 1 deletion base/bitarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ end
reinterpret(::Type{Bool}, B::BitArray, dims::NTuple{N,Int}) where {N} = reinterpret(B, dims)
reinterpret(B::BitArray, dims::NTuple{N,Int}) where {N} = reshape(B, dims)

if module_name(@__MODULE__) === :Base # avoid method overwrite
if nameof(@__MODULE__) === :Base # avoid method overwrite
(::Type{T})(x::T) where {T<:BitArray} = copy(x)
end

Expand Down
3 changes: 3 additions & 0 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1624,6 +1624,9 @@ export readandwrite
@deprecate function_module(f::Function) parentmodule(f) false
@deprecate function_module(f, t) parentmodule(f, t) false

# PR #25622
@deprecate module_name(m::Module) nameof(m)

# PR #25196
@deprecate_binding ObjectIdDict IdDict{Any,Any}

Expand Down
14 changes: 7 additions & 7 deletions base/docs/Docs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -457,22 +457,22 @@ end

uncurly(ex) = isexpr(ex, :curly) ? ex.args[1] : ex

namify(x) = nameof(x, isexpr(x, :macro))
namify(x) = astname(x, isexpr(x, :macro))

function nameof(x::Expr, ismacro)
function astname(x::Expr, ismacro)
if isexpr(x, :.)
ismacro ? macroname(x) : x
# Call overloading, e.g. `(a::A)(b) = b` or `function (a::A)(b) b end` should document `A(b)`
elseif (isexpr(x, :function) || isexpr(x, :(=))) && isexpr(x.args[1], :call) && isexpr(x.args[1].args[1], :(::))
return nameof(x.args[1].args[1].args[2], ismacro)
return astname(x.args[1].args[1].args[2], ismacro)
else
n = isexpr(x, (:module, :struct)) ? 2 : 1
nameof(x.args[n], ismacro)
astname(x.args[n], ismacro)
end
end
nameof(q::QuoteNode, ismacro) = nameof(q.value, ismacro)
nameof(s::Symbol, ismacro) = ismacro ? macroname(s) : s
nameof(other, ismacro) = other
astname(q::QuoteNode, ismacro) = astname(q.value, ismacro)
astname(s::Symbol, ismacro) = ismacro ? macroname(s) : s
astname(other, ismacro) = other

macroname(s::Symbol) = Symbol('@', s)
macroname(x::Expr) = Expr(x.head, x.args[1], macroname(x.args[end].value))
Expand Down
4 changes: 2 additions & 2 deletions base/docs/bindings.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ struct Binding
function Binding(m::Module, v::Symbol)
# Normalise the binding module for module symbols so that:
# Binding(Base, :Base) === Binding(Main, :Base)
m = module_name(m) === v ? parentmodule(m) : m
m = nameof(m) === v ? parentmodule(m) : m
new(Base.binding_module(m, v), v)
end
end
Expand Down Expand Up @@ -42,5 +42,5 @@ end
aliasof(b::Binding) = defined(b) ? (a = aliasof(resolve(b), b); defined(a) ? a : b) : b
aliasof(d::DataType, b) = Binding(d.name.module, d.name.name)
aliasof::Function, b) = (m = typeof(λ).name.mt; Binding(m.module, m.name))
aliasof(m::Module, b) = Binding(m, module_name(m))
aliasof(m::Module, b) = Binding(m, nameof(m))
aliasof(other, b) = b
2 changes: 1 addition & 1 deletion base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -792,7 +792,7 @@ export
hasmethod,
methods,
methodswith,
module_name,
nameof,
parentmodule,
names,
varinfo,
Expand Down
2 changes: 1 addition & 1 deletion base/int.jl
Original file line number Diff line number Diff line change
Expand Up @@ -450,7 +450,7 @@ end
# @doc isn't available when running in Core at this point.
# Tuple syntax for documention two function signatures at the same time
# doesn't work either at this point.
if module_name(@__MODULE__) === :Base
if nameof(@__MODULE__) === :Base
for fname in (:mod, :rem)
@eval @doc ("""
rem(x::Integer, T::Type{<:Integer}) -> T
Expand Down
2 changes: 1 addition & 1 deletion base/loading.jl
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ end

function PkgId(m::Module)
uuid = UUID(ccall(:jl_module_uuid, NTuple{2, UInt64}, (Any,), m))
name = String(module_name(m))
name = String(nameof(m))
UInt128(uuid) == 0 && return PkgId(name)
return PkgId(uuid, name)
end
Expand Down
2 changes: 1 addition & 1 deletion base/namedtuple.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# This file is a part of Julia. License is MIT: https://julialang.org/license

if module_name(@__MODULE__) === :Base
if nameof(@__MODULE__) === :Base

"""
NamedTuple{names,T}(args::Tuple)
Expand Down
8 changes: 4 additions & 4 deletions base/reflection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
# name and module reflection

"""
module_name(m::Module) -> Symbol
nameof(m::Module) -> Symbol
Get the name of a `Module` as a `Symbol`.
# Examples
```jldoctest
julia> module_name(Base)
julia> nameof(Base)
:Base
```
"""
module_name(m::Module) = ccall(:jl_module_name, Ref{Symbol}, (Any,), m)
nameof(m::Module) = ccall(:jl_module_name, Ref{Symbol}, (Any,), m)

"""
parentmodule(m::Module) -> Module
Expand Down Expand Up @@ -56,7 +56,7 @@ julia> fullname(Main)
```
"""
function fullname(m::Module)
mn = module_name(m)
mn = nameof(m)
if m === Main || m === Base || m === Core
return (mn,)
end
Expand Down
4 changes: 2 additions & 2 deletions base/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -584,7 +584,7 @@ end

function show(io::IO, m::Module)
if is_root_module(m)
print(io, module_name(m))
print(io, nameof(m))
else
print(io, join(fullname(m),"."))
end
Expand Down Expand Up @@ -1730,7 +1730,7 @@ function dumpsubtypes(io::IO, x::DataType, m::Module, n::Int, indent)
t = getfield(m, s)
if t === x || t === m
continue
elseif isa(t, Module) && module_name(t) === s && parentmodule(t) === m
elseif isa(t, Module) && nameof(t) === s && parentmodule(t) === m
# recurse into primary module bindings
dumpsubtypes(io, x, t, n, indent)
elseif isa(t, UnionAll) && directsubtype(t::UnionAll, x)
Expand Down
2 changes: 1 addition & 1 deletion base/stacktraces.jl
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ function from(frame::StackFrame, m::Module)
if finfo isa Core.MethodInstance
frame_m = finfo.def
isa(frame_m, Method) && (frame_m = frame_m.module)
result = module_name(frame_m) === module_name(m)
result = nameof(frame_m) === nameof(m)
end

return result
Expand Down
2 changes: 1 addition & 1 deletion base/tuple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ fill_to_length(t::Tuple{}, val, ::Val{2}) = (val, val)

# only define these in Base, to avoid overwriting the constructors
# NOTE: this means this constructor must be avoided in Core.Compiler!
if module_name(@__MODULE__) === :Base
if nameof(@__MODULE__) === :Base

(::Type{T})(x::Tuple) where {T<:Tuple} = convert(T, x) # still use `convert` for tuples

Expand Down
2 changes: 1 addition & 1 deletion doc/REQUIRE
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Compat 0.46.0 0.46.0+
DocStringExtensions 0.4.2 0.4.2+
DocStringExtensions 0.4.3 0.4.3+
Documenter 0.13.0 0.13.0+
2 changes: 1 addition & 1 deletion doc/src/base/base.md
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ Base.AsyncCondition(::Function)
## Reflection

```@docs
Base.module_name
Base.nameof(::Module)
Base.parentmodule
Base.@__MODULE__
Base.fullname
Expand Down
2 changes: 1 addition & 1 deletion examples/typetree.jl
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ function store_all_from(m::Module)
t = getfield(m, s)
if isa(t, Type) && t !== Union{}
store_type(Binding(m, s), t)
elseif isa(t, Module) && module_name(t) === s && parentmodule(t) === m && t !== m
elseif isa(t, Module) && nameof(t) === s && parentmodule(t) === m && t !== m
store_all_from(t)
end
end
Expand Down
2 changes: 1 addition & 1 deletion stdlib/REPL/src/REPLCompletions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ function complete_symbol(sym, ffunc)
# We will exclude the results that the user does not want, as well
# as excluding Main.Main.Main, etc., because that's most likely not what
# the user wants
p = s->(!Base.isdeprecated(mod, s) && s != module_name(mod) && ffunc(mod, s))
p = s->(!Base.isdeprecated(mod, s) && s != nameof(mod) && ffunc(mod, s))
# Looking for a binding in a module
if mod == context_module
# Also look in modules we got through `using`
Expand Down
2 changes: 1 addition & 1 deletion stdlib/Serialization/src/Serialization.jl
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ function serialize_mod_names(s::AbstractSerializer, m::Module)
serialize(s, Symbol(key.name))
else
serialize_mod_names(s, p)
serialize(s, module_name(m))
serialize(s, nameof(m))
end
end

Expand Down
4 changes: 2 additions & 2 deletions stdlib/Test/src/Test.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1288,7 +1288,7 @@ function detect_ambiguities(mods...;
continue
end
f = Base.unwrap_unionall(getfield(mod, n))
if recursive && isa(f, Module) && f !== mod && parentmodule(f) === mod && module_name(f) === n
if recursive && isa(f, Module) && f !== mod && parentmodule(f) === mod && nameof(f) === n
subambs = detect_ambiguities(f,
imported=imported, recursive=recursive, ambiguous_bottom=ambiguous_bottom)
union!(ambs, subambs)
Expand Down Expand Up @@ -1329,7 +1329,7 @@ function detect_unbound_args(mods...;
continue
end
f = Base.unwrap_unionall(getfield(mod, n))
if recursive && isa(f, Module) && parentmodule(f) === mod && module_name(f) === n
if recursive && isa(f, Module) && parentmodule(f) === mod && nameof(f) === n
subambs = detect_unbound_args(f, imported=imported, recursive=recursive)
union!(ambs, subambs)
elseif isa(f, DataType) && isdefined(f.name, :mt)
Expand Down
2 changes: 1 addition & 1 deletion test/numbers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2549,7 +2549,7 @@ function allsubtypes!(m::Module, x::DataType, sts::Set)
t = getfield(m, s)
if isa(t, Type) && t <: x && t != Union{}
push!(sts, t)
elseif isa(t, Module) && t !== m && module_name(t) === s && parentmodule(t) === m
elseif isa(t, Module) && t !== m && nameof(t) === s && parentmodule(t) === m
allsubtypes!(t, x, sts)
end
end
Expand Down
4 changes: 2 additions & 2 deletions test/reflection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ module TestModSub9475
let
@test Base.binding_module(@__MODULE__, :a9475) == @__MODULE__
@test Base.binding_module(@__MODULE__, :c7648) == TestMod7648
@test Base.module_name(@__MODULE__) == :TestModSub9475
@test Base.nameof(@__MODULE__) == :TestModSub9475
@test Base.fullname(@__MODULE__) == (curmod_name..., :TestMod7648, :TestModSub9475)
@test Base.parentmodule(@__MODULE__) == TestMod7648
end
Expand All @@ -241,7 +241,7 @@ using .TestModSub9475
let
@test Base.binding_module(@__MODULE__, :d7648) == @__MODULE__
@test Base.binding_module(@__MODULE__, :a9475) == TestModSub9475
@test Base.module_name(@__MODULE__) == :TestMod7648
@test Base.nameof(@__MODULE__) == :TestMod7648
@test Base.parentmodule(@__MODULE__) == curmod
end
end # module TestMod7648
Expand Down

0 comments on commit db2148e

Please sign in to comment.