Skip to content

Commit

Permalink
Support @constprop (#752)
Browse files Browse the repository at this point in the history
Co-authored-by: Shuhei Kadowaki <aviatesk@gmail.com>
  • Loading branch information
timholy and aviatesk authored Sep 11, 2021
1 parent 6e04e3a commit d4e51c0
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
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.35.0"
version = "3.36.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 @@ -54,6 +54,8 @@ changes in `julia`.

## Supported features

* `Compat.@constprop :aggressive ex` and `Compat.@constprop :none ex` allow control over constant-propagation during inference on Julia versions that support this feature, and otherwise just pass back `ex`. ([#42125]) (since Compat 3.36)

* `Returns(value)` returns `value` for any arguments ([#39794]) (since Compat 3.35)

* The function `current_exceptions()` has been added to get the current
Expand Down Expand Up @@ -273,3 +275,4 @@ Note that you should specify the correct minimum version for `Compat` in the
[#41076]: https://github.com/JuliaLang/julia/pull/41076
[#34331]: https://github.com/JuliaLang/julia/pull/34331
[#39794]: https://github.com/JuliaLang/julia/pull/39794
[#42125]: https://github.com/JuliaLang/julia/pull/42125
24 changes: 24 additions & 0 deletions src/Compat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1121,6 +1121,30 @@ if VERSION < v"1.7.0-DEV.793"
end
end

# https://github.com/JuliaLang/julia/pull/42125
if !isdefined(Base, Symbol("@constprop"))
if isdefined(Base, Symbol("@aggressive_constprop"))
macro constprop(setting, ex)
if isa(setting, QuoteNode)
setting = setting.value
end
setting === :aggressive && return esc(:(Base.@aggressive_constprop $ex))
setting === :none && return esc(ex)
throw(ArgumentError("@constprop $setting not supported"))
end
else
macro constprop(setting, ex)
if isa(setting, QuoteNode)
setting = setting.value
end
setting === :aggressive || setting === :none || throw(ArgumentError("@constprop $setting not supported"))
return esc(ex)
end
end
else
using Base: @constprop
end

include("iterators.jl")
include("deprecated.jl")

Expand Down
14 changes: 13 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1158,4 +1158,16 @@ end
val = [1,2,3]
@test Returns(val)(1) === val
@test sprint(show, Returns(1.0)) == "Returns{Float64}(1.0)"
end
end

# https://github.com/JuliaLang/julia/pull/42125
@testset "@constprop" begin
Compat.@constprop :aggressive aggf(x) = Symbol(x)
Compat.@constprop :none nonef(x) = Symbol(x)
@test_throws Exception Meta.lower(@__MODULE__,
quote
Compat.@constprop :other brokenf(x) = Symbol(x)
end
)
@test aggf("hi") == nonef("hi") == :hi
end

2 comments on commit d4e51c0

@timholy
Copy link
Member Author

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/44707

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.36.0 -m "<description of version>" d4e51c0fae4ff03e06db0bad44da799f614424f7
git push origin v3.36.0

Please sign in to comment.