Skip to content

StackOverflow while defining methods #36544

@baggepinnen

Description

@baggepinnen

On julia v1.5.0-rc1

I'm trying to define a bunch of methods using metaprogramming and I'm seeing a stack overflow error upon call to eval.

The following code manages to define the first method in the loop, but while evaling the second one the error is thrown

struct MyType{T,N}
    x::T
end

f(x::MyType) = x.x
f(x) = x

for XT in (:T, Symbol("MyType{T,N}")), YT in (:T, Symbol("MyType{T,N}")), ZT in (:T, Symbol("MyType{T,N}"))
    XT == YT == ZT == :T && continue
    ex = """
        function Base.muladd(x::$XT,y::$YT,z::$ZT) where {T,N}
            res = muladd(f(x),f(y),f(z))
            MyType{eltype(res),N}(res)
        end
    """
    println(ex)
    eval(Meta.parse(ex))
end

The reason I'm parsing a string and not building the expression directly is that I couldn't figure out how to interpolate Symbol("MyType{T,N}") into the expression, it just produced z::var"MyType{T,N}" instead of z::MyType{T,N}.

The output of the program (notice the print statement printing the expression) is

    function Base.muladd(x::T,y::T,z::MyType{T,N}) where {T,N}
        res = muladd(f(x),f(y),f(z))
        MyType{eltype(res),N}(res)
    end

    function Base.muladd(x::T,y::MyType{T,N},z::T) where {T,N}
        res = muladd(f(x),f(y),f(z))
        MyType{eltype(res),N}(res)
    end

ERROR: StackOverflowError:
Stacktrace:
 [1] top-level scope at none:1
 [2] eval at ./boot.jl:331 [inlined]
 [3] eval(::Expr) at ./client.jl:467
 [4] top-level scope at ./none:10

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugIndicates an unexpected problem or unintended behaviortypes and dispatchTypes, subtyping and method dispatch

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions