Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

descend doesn't work with constructor for sumtype in Expronicon #470

Closed
Zentrik opened this issue Jun 17, 2023 · 3 comments · Fixed by timholy/CodeTracking.jl#120
Closed

Comments

@Zentrik
Copy link
Collaborator

Zentrik commented Jun 17, 2023

using Cthulhu
using Expronicon.ADT: @adt

@adt Material begin
    struct Lambertian
    end
end

@descend Material.Lambertian()

throws error

Expr
  head: Symbol $
  args: Array{Any}((1,))
    1: Symbol args
ERROR: unexpected argument $args
Stacktrace:
  [1] error(::String, ::Expr)
    @ Base .\error.jl:44
  [2] get_argname(ex::Any)
    @ CodeTracking C:\Users\gag\.julia\packages\CodeTracking\EChKz\src\utils.jl:145
  [3] get_argname(ex::Any)
    @ CodeTracking C:\Users\gag\.julia\packages\CodeTracking\EChKz\src\utils.jl:142
  [4] is_func_expr(ex::Any, meth::Method)
    @ CodeTracking C:\Users\gag\.julia\packages\CodeTracking\EChKz\src\utils.jl:128
  [5] definition(#unused#::Type{String}, method::Method)
    @ CodeTracking C:\Users\gag\.julia\packages\CodeTracking\EChKz\src\CodeTracking.jl:261
  [6] tsn_and_mappings(m::Method, src::Core.CodeInfo, rt::Any; warn::Bool, strip_macros::Bool, kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})      
    @ TypedSyntax C:\Users\gag\.julia\packages\TypedSyntax\fngAi\src\node.jl:29
  [7] get_typed_sourcetext(mi::Core.MethodInstance, src::Core.CodeInfo, rt::Any; warn::Bool)
    @ Cthulhu C:\Users\gag\.julia\packages\Cthulhu\vJrGb\src\reflection.jl:346
  [8] find_callsites(interp::Cthulhu.CthulhuInterpreter, CI::Core.CodeInfo, stmt_infos::Vector{Core.Compiler.CallInfo}, mi::Core.MethodInstance, slottypes::Vector{Any}, optimize::Bool, annotate_source::Bool)
    @ Cthulhu C:\Users\gag\.julia\packages\Cthulhu\vJrGb\src\reflection.jl:28
  [9] _descend(term::REPL.Terminals.TTYTerminal, interp::Cthulhu.CthulhuInterpreter, curs::Cthulhu.CthulhuCursor; override::Nothing, debuginfo::Symbol, optimize::Bool, interruptexc::Bool, iswarn::Bool, hide_type_stable::Bool, verbose::Nothing, remarks::Bool, with_effects::Bool, inline_cost::Bool, type_annotations::Bool, annotate_source::Bool)
    @ Cthulhu C:\Users\gag\.julia\packages\Cthulhu\vJrGb\src\Cthulhu.jl:458
 [10] _descend(term::REPL.Terminals.TTYTerminal, interp::Cthulhu.CthulhuInterpreter, mi::Core.MethodInstance; kwargs::Base.Pairs{Symbol, Bool, Tuple{Symbol}, NamedTuple{(:iswarn,), Tuple{Bool}}})
    @ Cthulhu C:\Users\gag\.julia\packages\Cthulhu\vJrGb\src\Cthulhu.jl:724
 [11] _descend(::REPL.Terminals.TTYTerminal, ::Any, ::Vararg{Any}; interp::Core.Compiler.NativeInterpreter, kwargs::Base.Pairs{Symbol, Bool, Tuple{Symbol}, NamedTuple{(:iswarn,), Tuple{Bool}}})
    @ Cthulhu C:\Users\gag\.julia\packages\Cthulhu\vJrGb\src\Cthulhu.jl:740
 [12] __descend_with_error_handling(args::Any; terminal::Any, kwargs::Base.Pairs{Symbol, V, Tuple{Vararg{Symbol, N}}, NamedTuple{names, T}} where {V, N, names, T<:Tuple{Vararg{Any, N}}})
    @ Cthulhu C:\Users\gag\.julia\packages\Cthulhu\vJrGb\src\Cthulhu.jl:207
 [13] _descend_with_error_handling(f::Any, argtypes::Any; kwargs::Base.Pairs{Symbol, Bool, Tuple{Symbol}, NamedTuple{(:iswarn,), Tuple{Bool}}})
    @ Cthulhu C:\Users\gag\.julia\packages\Cthulhu\vJrGb\src\Cthulhu.jl:196
 [14] descend_code_typed(::Any, ::Vararg{Any}; kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
    @ Cthulhu C:\Users\gag\.julia\packages\Cthulhu\vJrGb\src\Cthulhu.jl:154
 [15] descend_code_typed(::Any, ::Any)
    @ Cthulhu C:\Users\gag\.julia\packages\Cthulhu\vJrGb\src\Cthulhu.jl:154
 [16] top-level scope
    @ REPL[7]:1
timholy added a commit to timholy/CodeTracking.jl that referenced this issue Aug 20, 2023
@timholy
Copy link
Member

timholy commented Aug 20, 2023

@Zentrik, with timholy/CodeTracking.jl#120 I now get:

julia> @descend Material.Lambertian()
(var"##adt_type"::var"Material#Type")(args...) @ Main ~/.julia/packages/Expronicon/7EBrJ/src/adt/emit.jl:429
429 function ($adt_type::$(info.typename))($args::Tuple{}...; $kwargs...)::Material
430             isempty($args) || return $(def.name)($adt_type, $args...)
431             $(codegen_ast(kwarg_body))
432         end
Select a call to descend into or  to ascend. [q]uit. [b]ookmark.
Toggles: [w]arn, [h]ide type-stable statements, [t]ype annotations, [s]yntax highlight for Source/LLVM/Native.
Show: [S]ource code, [A]ST, [T]yped code, [L]LVM IR, [N]ative code
Actions: [E]dit source code, [R]evise and redisplay
 • %2 = NamedTuple()::Core.Const(NamedTuple())
   %3 = pairs(::NamedTuple{(), Tuple{}})::Core.Const(Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}())
   %5 = #_#3(::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}},::var"Material#Type")::Material

Obviously there's no useful matching that can be done here. Should we scan for $ and if found warn the user that the body appears to be generated by metaprogramming and that the "[T]yped" view may be more useful? Or is this obvious without the warning?

@Zentrik
Copy link
Collaborator Author

Zentrik commented Aug 20, 2023

I think a warning would be useful as it wasn't particularly obvious to me that this was metaprogramming. Is @benchmark sum($(rand(1000))) metaprogramming? Descending on f(x) = @benchmark sum($(rand(1000))) gives f(x::Int64)::Any = @benchmark sum($(rand(1000)::Vector{Float64})::Tuple{Vector{Float64}})::Any which seems like what we would want to see and so I don't think looking at the "[T]yped" view would be helpful.

@timholy
Copy link
Member

timholy commented Aug 20, 2023

I'd say it is metaprogramming via the @benchmark. In the case of Material.Lambertian(), we don't even have the names of the arguments that are used to define the method (in your example, we match neither f nor x). It's a bit of a scary case because we only have the line number to go on.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants