Skip to content

Commit

Permalink
pure: remove incorrect or unnecessary annotations and functions
Browse files Browse the repository at this point in the history
  • Loading branch information
vtjnash committed Jun 19, 2019
1 parent 508f5bf commit a23af5e
Show file tree
Hide file tree
Showing 7 changed files with 9 additions and 70 deletions.
6 changes: 1 addition & 5 deletions base/bool.jl
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,7 @@ julia> .![true false true]
0 1 0
```
"""
function !(x::Bool)
## We need a better heuristic to detect this automatically
@_pure_meta
return not_int(x)
end
!(x::Bool) = not_int(x)

(~)(x::Bool) = !x
(&)(x::Bool, y::Bool) = and_int(x, y)
Expand Down
4 changes: 2 additions & 2 deletions base/essentials.jl
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ tail(::Tuple{}) = throw(ArgumentError("Cannot call tail on an empty tuple."))
tuple_type_head(T::Type) = (@_pure_meta; fieldtype(T::Type{<:Tuple}, 1))

function tuple_type_tail(T::Type)
@_pure_meta
@_pure_meta # TODO: this method is wrong (and not @pure)
if isa(T, UnionAll)
return UnionAll(T.var, tuple_type_tail(T.body))
elseif isa(T, Union)
Expand Down Expand Up @@ -698,7 +698,7 @@ julia> f(Val(true))
struct Val{x}
end

Val(x) = (@_pure_meta; Val{x}())
Val(x) = Val{x}()

"""
invokelatest(f, args...; kwargs...)
Expand Down
4 changes: 2 additions & 2 deletions base/promotion.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
Return the closest common ancestor of `T` and `S`, i.e. the narrowest type from which
they both inherit.
"""
typejoin() = (@_pure_meta; Bottom)
typejoin(@nospecialize(t)) = (@_pure_meta; t)
typejoin() = Bottom
typejoin(@nospecialize(t)) = t
typejoin(@nospecialize(t), ts...) = (@_pure_meta; typejoin(t, typejoin(ts...)))
function typejoin(@nospecialize(a), @nospecialize(b))
@_pure_meta
Expand Down
32 changes: 3 additions & 29 deletions base/reflection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -538,31 +538,6 @@ struct type with no fields.
"""
issingletontype(@nospecialize(t)) = (@_pure_meta; isa(t, DataType) && isdefined(t, :instance))

"""
Base.parameter_upper_bound(t::UnionAll, idx)
Determine the upper bound of a type parameter in the underlying datatype.
This method should generally not be relied upon:
code instead should usually use static parameters in dispatch to extract these values.
# Examples
```jldoctest
julia> struct Foo{T<:AbstractFloat, N}
x::Tuple{T, N}
end
julia> Base.parameter_upper_bound(Foo, 1)
AbstractFloat
julia> Base.parameter_upper_bound(Foo, 2)
Any
```
"""
function parameter_upper_bound(t::UnionAll, idx)
@_pure_meta
return rewrap_unionall((unwrap_unionall(t)::DataType).parameters[idx], t)
end

"""
typeintersect(T, S)
Expand Down Expand Up @@ -727,13 +702,12 @@ julia> instances(Color)
function instances end

function to_tuple_type(@nospecialize(t))
@_pure_meta
if isa(t,Tuple) || isa(t,AbstractArray) || isa(t,SimpleVector)
if isa(t, Tuple) || isa(t, AbstractArray) || isa(t, SimpleVector)
t = Tuple{t...}
end
if isa(t,Type) && t<:Tuple
if isa(t, Type) && t <: Tuple
for p in unwrap_unionall(t).parameters
if !(isa(p,Type) || isa(p,TypeVar))
if !(isa(p, Type) || isa(p, TypeVar))
error("argument tuple type must contain only types")
end
end
Expand Down
1 change: 1 addition & 0 deletions base/tuple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ function _compute_eltype(t::Type{<:Tuple})
@_pure_meta
t isa Union && return promote_typejoin(eltype(t.a), eltype(t.b))
= unwrap_unionall(t)
# TODO: handle Union/UnionAll correctly here
r = Union{}
for ti in.parameters
r = promote_typejoin(r, rewrap_unionall(unwrapva(ti), t))
Expand Down
28 changes: 0 additions & 28 deletions test/compiler/inference.jl
Original file line number Diff line number Diff line change
Expand Up @@ -767,12 +767,6 @@ fUnionAll(::Type{T}) where {T} = Type{S} where S <: T
@inferred fUnionAll(Real) == Type{T} where T <: Real
@inferred fUnionAll(Rational{T} where T <: AbstractFloat) == Type{T} where T<:(Rational{S} where S <: AbstractFloat)

fComplicatedUnionAll(::Type{T}) where {T} = Type{Tuple{S,rand() >= 0.5 ? Int : Float64}} where S <: T
let pub = Base.parameter_upper_bound, x = fComplicatedUnionAll(Real)
@test pub(pub(x, 1), 1) == Real
@test pub(pub(x, 1), 2) == Int || pub(pub(x, 1), 2) == Float64
end

# issue #20733
# run this test in a separate process to avoid interfering with `getindex`
let def = "Base.getindex(t::NTuple{3,NTuple{2,Int}}, i::Int, j::Int, k::Int) = (t[1][i], t[2][j], t[3][k])"
Expand All @@ -788,28 +782,6 @@ end
f20267(x::T20267{T}, y::T) where (T) = f20267(Any[1][1], x.inds)
@test Base.return_types(f20267, (Any, Any)) == Any[Union{}]

# issue #20704
f20704(::Int) = 1
Base.@pure b20704(@nospecialize(x)) = f20704(x)
@test b20704(42) === 1
@test_throws MethodError b20704(42.0)

bb20704() = b20704(Any[1.0][1])
@test_throws MethodError bb20704()

v20704() = Val{b20704(Any[1.0][1])}
@test_throws MethodError v20704()
@test Base.return_types(v20704, ()) == Any[Type{Val{1}}]

Base.@pure g20704(::Int) = 1
h20704(@nospecialize(x)) = g20704(x)
@test g20704(1) === 1
@test_throws MethodError h20704(1.2)

Base.@pure c20704() = (f20704(1.0); 1)
d20704() = c20704()
@test_throws MethodError d20704()

Base.@pure function a20704(x)
rand()
42
Expand Down
4 changes: 0 additions & 4 deletions test/reflection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -568,10 +568,6 @@ end
@test !isstructtype(Int)
@test isstructtype(TLayout)

@test Base.parameter_upper_bound(ReflectionExample, 1) === AbstractFloat
@test Base.parameter_upper_bound(ReflectionExample, 2) === Any
@test Base.parameter_upper_bound(ReflectionExample{T, N} where T where N <: Real, 2) === Real

let
wrapperT(T) = Base.typename(T).wrapper
@test @inferred wrapperT(ReflectionExample{Float64, Int64}) == ReflectionExample
Expand Down

0 comments on commit a23af5e

Please sign in to comment.