Skip to content

Commit

Permalink
Add 2 arg @inferred (#706)
Browse files Browse the repository at this point in the history
* Add 2 arg @inferred

* Update version to 3.12.0

* Update src/Compat.jl

Use using instead of import

Co-authored-by: Curtis Vogt <[email protected]>

Co-authored-by: Curtis Vogt <[email protected]>
  • Loading branch information
fchorney and omus authored Jun 11, 2020
1 parent b32879e commit 62a7453
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name = "Compat"
uuid = "34da2185-b29b-5c13-b0c7-acf172513d20"
version = "3.11.0"
version = "3.12.0"

[deps]
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ Please check the list below for the specific syntax you need.

## Supported features

* `@inferred [AllowedType] f(x)` is defined ([#27516]). (since Compat 3.12.0)

* Search a character in a string with `findfirst`, `findnext`, `findlast` and `findprev` ([#31664]). (since Compat 3.12.0)

* `∘(f) = f` is defined ([#34251]). (since Compat 3.11.0)
Expand Down Expand Up @@ -160,3 +162,4 @@ Note that you should specify the correct minimum version for `Compat` in the
[#30268]: https://github.com/JuliaLang/julia/pull/30268
[#34251]: https://github.com/JuliaLang/julia/pull/34251
[#35577]: https://github.com/JuliaLang/julia/pull/35577
[#27516]: https://github.com/JuliaLang/julia/pull/27516
50 changes: 50 additions & 0 deletions src/Compat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,56 @@ if VERSION < v"1.5.0-DEV.681"
Base.union(r::Base.OneTo, s::Base.OneTo) = Base.OneTo(max(r.stop,s.stop))
end

# https://github.com/JuliaLang/julia/pull/27516
if VERSION < v"1.2.0-DEV.77"
import Test: @inferred
using Core.Compiler: typesubtract

macro inferred(allow, ex)
_inferred(ex, __module__, allow)
end

function _inferred(ex, mod, allow = :(Union{}))
if Meta.isexpr(ex, :ref)
ex = Expr(:call, :getindex, ex.args...)
end
Meta.isexpr(ex, :call)|| error("@inferred requires a call expression")
farg = ex.args[1]
if isa(farg, Symbol) && first(string(farg)) == '.'
farg = Symbol(string(farg)[2:end])
ex = Expr(:call, GlobalRef(Base.Test, :_materialize_broadcasted),
farg, ex.args[2:end]...)
end
Base.remove_linenums!(quote
let
allow = $(esc(allow))
allow isa Type || throw(ArgumentError("@inferred requires a type as second argument"))
$(if any(a->(Meta.isexpr(a, :kw) || Meta.isexpr(a, :parameters)), ex.args)
# Has keywords
args = gensym()
kwargs = gensym()
quote
$(esc(args)), $(esc(kwargs)), result = $(esc(Expr(:call, _args_and_call, ex.args[2:end]..., ex.args[1])))
inftypes = $(gen_call_with_extracted_types(mod, Base.return_types, :($(ex.args[1])($(args)...; $(kwargs)...))))
end
else
# No keywords
quote
args = ($([esc(ex.args[i]) for i = 2:length(ex.args)]...),)
result = $(esc(ex.args[1]))(args...)
inftypes = Base.return_types($(esc(ex.args[1])), Base.typesof(args...))
end
end)
@assert length(inftypes) == 1
rettype = result isa Type ? Type{result} : typeof(result)
rettype <: allow || rettype == typesubtract(inftypes[1], allow) || error("return type $rettype does not match inferred return type $(inftypes[1])")
result
end
end)
end
#export @inferred
end

include("deprecated.jl")

end # module Compat
9 changes: 9 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -455,4 +455,13 @@ end
@test union(Base.OneTo(3), Base.OneTo(4)) === Base.OneTo(4)
end

# https://github.com/JuliaLang/julia/pull/27516
@testset "two arg @inferred" begin
g(a) = a < 10 ? missing : 1
@test ismissing(g(9))
@test g(10) == 1
@inferred Missing g(9)
@inferred Missing g(10)
end

nothing

2 comments on commit 62a7453

@omus
Copy link
Member

@omus omus commented on 62a7453 Jun 11, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/16216

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v3.12.0 -m "<description of version>" 62a745327b6330024b6a3a4d6e5c1a6d325a894d
git push origin v3.12.0

Please sign in to comment.