Skip to content

Commit

Permalink
Add at-compat for new type declaration syntax and fix depwarn in tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
yuyichao committed Feb 11, 2017
1 parent 58c52dc commit c83a1b5
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 8 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ Currently, the `@compat` macro supports the following syntaxes:
* `@compat Array{<:Real}` and similar uses of `<:T` to define a set of parameterized types ([#20414]).
In 0.4 and 0.5, this only works for non-nested usages (e.g. you can't define `Array{<:Array{<:Real}}`).

* `@compat abstract type T end` and `@compat primitive type T 8 end`
to declare abstract and primitive types. [#20418]
This only works when `@compat` is applied directly on the declaration.

## Type Aliases

* In 0.5, `ASCIIString` and `ByteString` were deprecated, and `UTF8String` was renamed to the (now concrete) type `String`.
Expand Down
49 changes: 47 additions & 2 deletions src/Compat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -746,8 +746,53 @@ function _compat(ex::Symbol)
end
_compat(ex) = ex

macro compat(ex)
esc(_compat(ex))
function _get_typebody(ex::Expr)
args = ex.args
if ex.head !== :type || length(args) != 3 || args[1] !== true
throw(ArgumentError("Invalid usage of @compat: $ex"))
end
name = args[2]
if !isexpr(args[3], :block)
throw(ArgumentError("Invalid type declaration: $ex"))
end
body = (args[3]::Expr).args
filter!(body) do e
if isa(e, LineNumberNode) || isexpr(e, :line)
return false
end
return true
end
return name, body
end

function _compat_primitive(typedecl)
name, body = _get_typebody(typedecl)
if length(body) != 1
throw(ArgumentError("Invalid primitive type declaration: $typedecl"))
end
return Expr(:bitstype, body[1], name)
end

function _compat_abstract(typedecl)
name, body = _get_typebody(typedecl)
if length(body) != 0
throw(ArgumentError("Invalid abstract type declaration: $typedecl"))
end
return Expr(:abstract, name)
end

macro compat(ex...)
if VERSION < v"0.6.0-dev.2746" && length(ex) == 2 && ex[1] === :primitive
return esc(_compat_primitive(ex[2]))
elseif length(ex) != 1
throw(ArgumentError("@compat called with wrong number of arguments: $ex"))
elseif (VERSION < v"0.6.0-dev.2746" && isexpr(ex[1], :abstract) &&
length(ex[1].args) == 1 && isexpr(ex[1].args[1], :type))
# This can in principle be handled in nested case but we do not
# do that to be consistent with primitive types.
return esc(_compat_abstract(ex[1].args[1]))
end
esc(_compat(ex[1]))
end

export @compat, @inline, @noinline
Expand Down
23 changes: 17 additions & 6 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -742,7 +742,7 @@ mktempdir() do dir


verbose && println("$name read...")
@test @compat read(io()) == UInt8[convert(UInt8, _) for _ in text]
@test @compat read(io()) == UInt8[convert(UInt8, x) for x in text]

@test read(io()) == read(filename)

Expand Down Expand Up @@ -843,7 +843,7 @@ mktempdir() do dir
@test readstring("$filename.to") == text

verbose && println("$name write(::IOBuffer, ...)")
@compat to = IOBuffer(UInt8[convert(UInt8, _) for _ in text], false, true)
@compat to = IOBuffer(UInt8[convert(UInt8, x) for x in text], false, true)
write(to, io())
@test String(take!(to)) == text

Expand Down Expand Up @@ -1353,8 +1353,11 @@ end
end
@test do_boundscheck() == true

@test Compat.promote_eltype_op(@functorize(+), ones(2,2), 1) === Float64
@test Compat.promote_eltype_op(@functorize(*), ones(Int, 2), zeros(Int16,2)) === Int
if VERSION < v"0.6.0-dev.1886"
# `promote_eltype_op` is deprecated
@test Compat.promote_eltype_op(@functorize(+), ones(2,2), 1) === Float64
@test Compat.promote_eltype_op(@functorize(*), ones(Int, 2), zeros(Int16,2)) === Int
end

#Add test for Base.normalize and Base.normalize!
let
Expand Down Expand Up @@ -1622,7 +1625,7 @@ for x in (3.1, -17, 3//4, big(111.1), Inf)
end

# julia#20006
abstract AbstractFoo20006
@compat abstract type AbstractFoo20006 end
immutable ConcreteFoo20006{T<:Int} <: AbstractFoo20006 end
immutable ConcreteFoo20006N{T<:Int,N} <: AbstractFoo20006 end
typealias ConcreteFoo200061{T<:Int} ConcreteFoo20006N{T,1}
Expand Down Expand Up @@ -1751,4 +1754,12 @@ end
let x = [1,2,3]
@dotcompat f(x) = x^2
@test f(x) == [1,4,9]
end
end

# PR #20418
@compat abstract type Abstract20418{T} <: Ref{T} end
@test Compat.TypeUtils.isabstract(Abstract20418)
@compat primitive type Primitive20418{T} <: Ref{T} 16 end
@test !Compat.TypeUtils.isabstract(Primitive20418)
@test isbits(Primitive20418{Int})
@test sizeof(Primitive20418{Int}) == 2

0 comments on commit c83a1b5

Please sign in to comment.