oxinabox / Tricks.jl

Cunning tricks though the julia compiler internals
MIT License
85 stars 9 forks source link

Reproducible case of `static_methods` not working #39

Open gafter opened 1 year ago

gafter commented 1 year ago
nealgafter@NGafters-MBP A.jl % find . -type f -print | xargs -I {} sh -c 'echo "##### {} #####" ; cat {}'
##### ./Manifest.toml #####
# This file is machine-generated - editing it directly is not advised

julia_version = "1.10.0-DEV"
manifest_format = "2.0"
project_hash = "00db49d65728c871988860fc32dad0ecfd594cba"

[[deps.Tricks]]
git-tree-sha1 = "aadb748be58b492045b4f56166b5188aa63ce549"
uuid = "410a4b4d-49e4-4fbc-ab6d-cb71b17b3775"
version = "0.1.7"
##### ./test/runtests.jl #####
using A: enumerate_ctors_generated

struct Foo
    x
    y
end

foo = Foo(1, 2.0)

println(enumerate_ctors_generated(foo))
##### ./Project.toml #####
name = "A"
uuid = "d236873b-e432-73ab-a13d-4d3ddb9f1c9d"
authors = ["Neal Gafter <neal@gafter.com> and contributors"]
version = "0.0.1"

[deps]
Tricks = "410a4b4d-49e4-4fbc-ab6d-cb71b17b3775"

[compat]
julia = "1.10"
##### ./src/A.jl #####
module A

using Tricks

function enumerate_ctors_normal(node_type::Type{T}) where { T }
    Tricks.static_methods(node_type)
end

@generated function enumerate_ctors_generated(node::T) where { T }
    # Since this is a @generated function, node is actually the type of the argument.
    node_type = node
    enumerate_ctors_normal(node_type)
end

end # module A
nothing
nealgafter@NGafters-MBP A.jl % julia-nightly --project -e 'using Pkg; Pkg.test("A")'
     Testing A
      Status `/private/var/folders/86/5tmxf105651_3frbpzlmng400000gn/T/jl_u1IZQU/Project.toml`
  [d236873b] A v0.0.1 `~/Repos/tmp/A.jl`
  [410a4b4d] Tricks v0.1.7
      Status `/private/var/folders/86/5tmxf105651_3frbpzlmng400000gn/T/jl_u1IZQU/Manifest.toml`
  [d236873b] A v0.0.1 `~/Repos/tmp/A.jl`
  [410a4b4d] Tricks v0.1.7
     Testing Running tests...
ERROR: LoadError: TypeError: in typeassert, expected Vector, got a value of type Nothing
Stacktrace:
 [1] methods(f::Any, t::Any, mod::Nothing)
   @ Base ./reflection.jl:1060
 [2] methods(f::Type{Foo}, mod::Nothing) (repeats 2 times)
   @ Base ./reflection.jl:1082 [inlined]
 [3] enumerate_ctors_normal(node_type::Type{Foo})
   @ A ~/Repos/tmp/A.jl/src/A.jl:6
 [4] #s1#1
   @ A ~/Repos/tmp/A.jl/src/A.jl:12 [inlined]
 [5] var"#s1#1"(T::Any, ::Any, node::Any)
   @ A ./none:0
 [6] (::Core.GeneratedFunctionStub)(::UInt64, ::LineNumberNode, ::Any, ::Vararg{Any})
   @ Core ./boot.jl:598
 [7] top-level scope
   @ ~/Repos/tmp/A.jl/test/runtests.jl:10
 [8] include(fname::String)
   @ Base.MainInclude ./client.jl:478
 [9] top-level scope
   @ none:6
in expression starting at /Users/nealgafter/Repos/tmp/A.jl/test/runtests.jl:10
ERROR: Package A errored during testing
Stacktrace:
 [1] pkgerror(msg::String)
   @ Pkg.Types /Applications/Julia-1.10.app/Contents/Resources/julia/share/julia/stdlib/v1.10/Pkg/src/Types.jl:69
 [2] test(ctx::Pkg.Types.Context, pkgs::Vector{Pkg.Types.PackageSpec}; coverage::Bool, julia_args::Cmd, test_args::Cmd, test_fn::Nothing, force_latest_compatible_version::Bool, allow_earlier_backwards_compatible_versions::Bool, allow_reresolve::Bool)
   @ Pkg.Operations /Applications/Julia-1.10.app/Contents/Resources/julia/share/julia/stdlib/v1.10/Pkg/src/Operations.jl:2014
 [3] test(ctx::Pkg.Types.Context, pkgs::Vector{…}; coverage::Bool, test_fn::Nothing, julia_args::Cmd, test_args::Cmd, force_latest_compatible_version::Bool, allow_earlier_backwards_compatible_versions::Bool, allow_reresolve::Bool, kwargs::Base.Pairs{…})
   @ Pkg.API /Applications/Julia-1.10.app/Contents/Resources/julia/share/julia/stdlib/v1.10/Pkg/src/API.jl:441
 [4] test(pkgs::Vector{Pkg.Types.PackageSpec}; io::Base.TTY, kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, @NamedTuple{}})
   @ Pkg.API /Applications/Julia-1.10.app/Contents/Resources/julia/share/julia/stdlib/v1.10/Pkg/src/API.jl:156
 [5] test(pkgs::Vector{Pkg.Types.PackageSpec})
   @ Pkg.API /Applications/Julia-1.10.app/Contents/Resources/julia/share/julia/stdlib/v1.10/Pkg/src/API.jl:145
 [6] test(pkgs::Vector{String}; kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, @NamedTuple{}})
   @ Pkg.API /Applications/Julia-1.10.app/Contents/Resources/julia/share/julia/stdlib/v1.10/Pkg/src/API.jl:144 [inlined]
 [7] test(pkg::String; kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, @NamedTuple{}})
   @ Pkg.API /Applications/Julia-1.10.app/Contents/Resources/julia/share/julia/stdlib/v1.10/Pkg/src/API.jl:143 [inlined]
 [8] top-level scope
   @ none:1
Some type information was truncated. Use `show(err)` to see complete types.

See also https://github.com/JuliaLang/julia/issues/49982

oxinabox commented 1 year ago

hmm we may need to rollback the changes to static_methods since julia 1.10 only actually changed hasmethod.

oxinabox commented 1 year ago

On current nightly, with Trickes v0.1.8 this no longer erros

It gives

 julia> println(enumerate_ctors_generated(foo))
# 0 methods for type constructor