Skip to content

Commit

Permalink
Merge pull request #25622 from JuliaLang/aa/module-symbol
Browse files Browse the repository at this point in the history
Deprecate module_name in favor of nameof(::Module)
  • Loading branch information
JeffBezanson authored Jan 23, 2018
2 parents bca94e4 + b262a61 commit dcb0517
Show file tree
Hide file tree
Showing 25 changed files with 60 additions and 48 deletions.
7 changes: 6 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -964,6 +964,10 @@ 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. Similarly,
the unexported `Base.function_name` and `Base.datatype_name` have been deprecated in favor
of `nameof` methods ([#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 +1243,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
6 changes: 6 additions & 0 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1625,6 +1625,12 @@ 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)
@deprecate function_name(f::Function) nameof(f) false
@deprecate datatype_name(t::DataType) nameof(t) false
@deprecate datatype_name(t::UnionAll) nameof(t) false

# 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
23 changes: 12 additions & 11 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 Expand Up @@ -144,9 +144,10 @@ fieldnames(t::UnionAll) = fieldnames(unwrap_unionall(t))
fieldnames(t::Type{<:Tuple}) = Int[n for n in 1:fieldcount(t)]

"""
Base.datatype_name(t) -> Symbol
nameof(t::DataType) -> Symbol
Get the name of a (potentially UnionAll-wrapped) `DataType` (without its parent module) as a symbol.
Get the name of a (potentially `UnionAll`-wrapped) `DataType` (without its parent module)
as a symbol.
# Examples
```jldoctest
Expand All @@ -156,12 +157,12 @@ julia> module Foo
end
Foo
julia> Base.datatype_name(Foo.S{T} where T)
julia> nameof(Foo.S{T} where T)
:S
```
"""
datatype_name(t::DataType) = t.name.name
datatype_name(t::UnionAll) = datatype_name(unwrap_unionall(t))
nameof(t::DataType) = t.name.name
nameof(t::UnionAll) = nameof(unwrap_unionall(t))

"""
parentmodule(t::DataType) -> Module
Expand Down Expand Up @@ -1018,11 +1019,11 @@ end

# function reflection
"""
Base.function_name(f::Function) -> Symbol
nameof(f::Function) -> Symbol
Get the name of a generic `Function` as a symbol, or `:anonymous`.
"""
function_name(f::Function) = typeof(f).name.mt.name
nameof(f::Function) = typeof(f).name.mt.name

functionloc(m::Core.MethodInstance) = functionloc(m.def)

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+
6 changes: 3 additions & 3 deletions doc/src/base/base.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ Base.isimmutable
Base.isabstracttype
Base.isprimitivetype
Base.isstructtype
Base.datatype_name
Base.nameof(::DataType)
Base.fieldnames
Base.fieldname
```
Expand Down Expand Up @@ -332,14 +332,14 @@ Base.AsyncCondition(::Function)
## Reflection

```@docs
Base.module_name
Base.nameof(::Module)
Base.parentmodule
Base.@__MODULE__
Base.fullname
Base.names
Core.nfields
Base.isconst
Base.function_name
Base.nameof(::Function)
Base.functionloc(::Any, ::Any)
Base.functionloc(::Method)
Base.@functionloc
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/Dates/src/parse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ function character_codes(directives::SimpleVector)
return letters
end

genvar(t::DataType) = Symbol(lowercase(string(Base.datatype_name(t))))
genvar(t::DataType) = Symbol(lowercase(string(nameof(t))))

"""
tryparsenext_core(str::AbstractString, pos::Int, len::Int, df::DateFormat, raise=false)
Expand Down
2 changes: 1 addition & 1 deletion stdlib/REPL/src/LineEdit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ const COMMAND_GROUPS =

const COMMAND_GROUP = Dict(command=>group for (group, commands) in COMMAND_GROUPS for command in commands)
command_group(command::Symbol) = get(COMMAND_GROUP, command, :nogroup)
command_group(command::Function) = command_group(Base.function_name(command))
command_group(command::Function) = command_group(nameof(command))

# return true if command should keep active a region
function preserve_active(command::Symbol)
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
8 changes: 4 additions & 4 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 All @@ -266,14 +266,14 @@ let
using .TestMod7648
@test Base.binding_module(@__MODULE__, :a9475) == TestMod7648.TestModSub9475
@test Base.binding_module(@__MODULE__, :c7648) == TestMod7648
@test Base.function_name(foo7648) == :foo7648
@test nameof(foo7648) == :foo7648
@test parentmodule(foo7648, (Any,)) == TestMod7648
@test parentmodule(foo7648) == TestMod7648
@test parentmodule(foo7648_nomethods) == TestMod7648
@test parentmodule(foo9475, (Any,)) == TestMod7648.TestModSub9475
@test parentmodule(foo9475) == TestMod7648.TestModSub9475
@test parentmodule(Foo7648) == TestMod7648
@test Base.datatype_name(Foo7648) == :Foo7648
@test nameof(Foo7648) == :Foo7648
@test basename(functionloc(foo7648, (Any,))[1]) == "reflection.jl"
@test first(methods(TestMod7648.TestModSub9475.foo7648)) == @which foo7648(5)
@test TestMod7648 == @which foo7648
Expand Down

0 comments on commit dcb0517

Please sign in to comment.