Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Comment on lines -66 to +83

Choose a reason for hiding this comment

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

methods isn't constant folded at compile-time even after the PR. It returns vector, which is currently not handled as constant within our compiler.

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