Skip to content

Commit

Permalink
Merge pull request #37 from oxinabox/ox/disable
Browse files Browse the repository at this point in the history
Make identity op on julia v1.10
  • Loading branch information
oxinabox authored Mar 29, 2023
2 parents 6f6d911 + 3bb42aa commit 019aeb9
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 24 deletions.
2 changes: 0 additions & 2 deletions .github/workflows/JuliaNightly.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ on:
branches: [master]
tags: [v*]
pull_request:
schedule:
- cron: "0 0 * * *"

jobs:
test:
Expand Down
4 changes: 2 additions & 2 deletions Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Tricks"
uuid = "410a4b4d-49e4-4fbc-ab6d-cb71b17b3775"
authors = ["Lyndon White"]
version = "0.1.6"
authors = ["Frames White"]
version = "0.1.7"

[compat]
julia = "1.0"
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,14 @@
-->
[![Codecov](https://codecov.io/gh/oxinabox/Tricks.jl/branch/master/graph/badge.svg)](https://codecov.io/gh/oxinabox/Tricks.jl)


| ⚠️ Notice ⚠️ |
| --- |
| **Tricks.jl** is not required post-Julia v1.10.0-DEV.609. |
|The features of running `hasmethod` at compile-time are now built into the language. |
| It can still be used for compatibility with older versions of the language. |


Tricks.jl is an particularly ~evil~ cunning package that does tricks with the Julia edge system.

Currently it has the following tricks:
Expand Down
56 changes: 37 additions & 19 deletions src/Tricks.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,8 @@ export static_hasmethod, static_methods, static_method_count, compat_hasmethod,
_hasmethod_false(@nospecialize(f), @nospecialize(t)) = false
_hasmethod_true(@nospecialize(f), @nospecialize(t)) = true

"""
static_hasmethod(f, type_tuple::Type{<:Tuple)

Like `hasmethod` but runs at compile-time (and does not accept a worldage argument).
"""
@generated function static_hasmethod(@nospecialize(f), @nospecialize(t::Type{T}),) where {T<:Tuple}
@generated function _static_hasmethod(@nospecialize(f), @nospecialize(t::Type{T}),) where {T<:Tuple}
# The signature type:
method_insts = _method_instances(f, T)

Expand All @@ -39,7 +35,17 @@ Like `hasmethod` but runs at compile-time (and does not accept a worldage argume
return ci
end

"""
static_hasmethod(f, type_tuple::Type{<:Tuple)
Like `hasmethod` but runs at compile-time (and does not accept a worldage argument).
"""
const static_hasmethod = if VERSION >= v"1.10.0-DEV.609"
# Feature is now part of julia itself
hasmethod
else
_static_hasmethod
end

function create_codeinfo_with_returnvalue(argnames, spnames, sp, value)
expr = Expr(:lambda,
Expand All @@ -63,14 +69,19 @@ end
Like `methods` but runs at compile-time (and does not accept a worldage argument).
"""
static_methods(@nospecialize(f)) = static_methods(f, Tuple{Vararg{Any}})
@generated function static_methods(@nospecialize(f) , @nospecialize(_T::Type{T})) where {T <: Tuple}
list_of_methods = _methods(f, T)
ci = create_codeinfo_with_returnvalue([Symbol("#self#"), :f, :_T], [:T], (:T,), :($list_of_methods))

# Now we add the edges so if a method is defined this recompiles
ci.edges = _method_table_all_edges_all_methods(f, T)
return ci
const static_methods = if VERSION >= v"1.10.0-DEV.609"
# feature is now built in
methods
else
_static_methods(@nospecialize(f)) = static_methods(f, Tuple{Vararg{Any}})
@generated function _static_methods(@nospecialize(f) , @nospecialize(_T::Type{T})) where {T <: Tuple}
list_of_methods = _methods(f, T)
ci = create_codeinfo_with_returnvalue([Symbol("#self#"), :f, :_T], [:T], (:T,), :($list_of_methods))

# Now we add the edges so if a method is defined this recompiles
ci.edges = _method_table_all_edges_all_methods(f, T)
return ci
end
end

function _method_table_all_edges_all_methods(f, T)
Expand All @@ -95,13 +106,20 @@ end
Returns `length(methods(f, tt))` but runs at compile-time (and does not accept a worldage argument).
"""
static_method_count(@nospecialize(f)) = static_method_count(f, Tuple{Vararg{Any}})
@generated function static_method_count(@nospecialize(f) , @nospecialize(_T::Type{T})) where {T <: Tuple}
method_count = length(_methods(f, T))
ci = create_codeinfo_with_returnvalue([Symbol("#self#"), :f, :_T], [:T], (:T,), :($method_count))
if VERSION >= v"1.10.0-DEV.609"
# feature is now built in
function static_method_count(@nospecialize(f) , @nospecialize(_T::Type{T})) where {T <: Tuple}
return length(methods(f, _T))
end
else
@generated function static_method_count(@nospecialize(f) , @nospecialize(_T::Type{T})) where {T <: Tuple}
method_count = length(_methods(f, T))
ci = create_codeinfo_with_returnvalue([Symbol("#self#"), :f, :_T], [:T], (:T,), :($method_count))

# Now we add the edges so if a method is defined this recompiles
ci.edges = _method_table_all_edges_all_methods(f, T)
return ci
# Now we add the edges so if a method is defined this recompiles
ci.edges = _method_table_all_edges_all_methods(f, T)
return ci
end
end

@static if VERSION < v"1.3"
Expand Down
8 changes: 7 additions & 1 deletion test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,13 @@ VERSION >= v"1.3" && @testset "static_method_count" begin
# Code Generation
code_typed = (@code_typed static_method_count(f))
@test code_typed[2] === Int # return type
@test has_no_calls(code_typed[1].code)

if VERSION < v"1.10.0-DEV.609"
@test has_no_calls(code_typed[1].code)
else
# Actually does have calls on new version, because `methods` isn't constant folded right now
@test_broken has_no_calls(code_typed[1].code)
end

@testset "delete method" begin
i(::Int) = 1
Expand Down

2 comments on commit 019aeb9

@oxinabox
Copy link
Owner 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/80551

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 v0.1.7 -m "<description of version>" 019aeb9c9d471c09ddd753e3e89621a69aba9c69
git push origin v0.1.7

Please sign in to comment.