Skip to content

stagedfunction-related compilation error when type inference fails #8504

Closed

Description

I've found a case where compiling a function that calls a stagedfunction fails, due to a failure in type inference. If one checks out #8501 (and preferably, inserts a println(P) here), here's a simple way to reproduce:

function as_sub(x::AbstractMatrix)
    y = similar(x, eltype(x), tuple(([size(x)...]*2)...))
    @show typeof(y)
    y = sub(y, 2:2:size(y,1), 2:2:size(y,2))
    for j=1:size(x,2)
        for i=1:size(x,1)
            y[i,j] = x[i,j]
        end
    end
    y
end

as_sub(eye(2))

This gives the following output:

julia> as_sub(eye(2))
Array{Float64,N}
Array{Float64,N}
Array{Float64,N}
ERROR: `ndims` has no method matching ndims(::Type{Any})
 in ndims at ./abstractarray.jl:22 (repeats 3 times)
 in setindex! at ./subarray2.jl:35

One can see that it fails at compile-time, not runtime: that @show typeof(y) never gets called.

A modified version of the function (which declares the type of y):

function as_sub1(x::AbstractMatrix)
    y = similar(x, eltype(x), tuple(([size(x)...]*2)...))::typeof(x)
    @show typeof(y)
    y = sub(y, 2:2:size(y,1), 2:2:size(y,2))
    for j=1:size(x,2)
        for i=1:size(x,1)
            y[i,j] = x[i,j]
        end
    end
    y
end

yields this:

julia> as_sub1(eye(2))
Array{Float64,2}
Array{Float64,2}
typeof(y) = Array{Float64,2}
2x2 SubArray{Float64,2,Array{Float64,2},(StepRange{Int64,Int64},StepRange{Int64,Int64})}:
 1.0  0.0
 0.0  1.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Assignees

No one assigned

    Labels

    bugIndicates an unexpected problem or unintended behaviorpriorityThis should be addressed urgently

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions