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

"Slicing" due to type unstable parametric type construction #30335

Closed
jagot opened this issue Dec 10, 2018 · 1 comment
Closed

"Slicing" due to type unstable parametric type construction #30335

jagot opened this issue Dec 10, 2018 · 1 comment
Assignees
Labels
bug Indicates an unexpected problem or unintended behavior types and dispatch Types, subtyping and method dispatch

Comments

@jagot
Copy link
Contributor

jagot commented Dec 10, 2018

I'm seeing something that reminds me a little bit about type slicing in C++; I'm trying to generate term symbols (https://en.wikipedia.org/wiki/Term_symbol) from their string representation. I have this type

struct Term{I<:Integer,R<:Rational{I},LT<:Union{I,R}}
    L::LT
    S::R
    parity::I
end

The creation of a Term from a string is accomplished with (input error checks removed)

spectroscopic="spdfghiklmnoqrtuv"

function term_string(s::AbstractString)
    m = match(r"([0-9]+)([A-Z]|\[[0-9/]+\])([oe ]{0,1})", s)
    L = lowercase(m[2])
    L = if L[1] == '['
        L = strip(L, ['[',']'])
        if occursin("/", L)
            Ls = split(L, "/")
            Rational(parse(Int, Ls[1]),parse(Int, Ls[2]))
        else
            parse(Int, L)
        end
    else
        findfirst(L, spectroscopic)[1]-1
    end
    S = (parse(Int, m[1]) - 1)//2
    Term(L, S, m[3] == "o" ? -1 : 1)
end

Input 1S should yield Term.L=0, 1[5] => Term.L=5, 1[3/2] => Term.L=3//2 (rational). The last one however, results in

Term{Int64,Rational{Int64},Int64}
  L: Int64 3
  S: Rational{Int64}
    num: Int64 2
    den: Int64 0
  parity: Int64 1

where it can be seen that the type of L was not correctly inferred, and 3//2 was split such that the numerator ended up in L and the denominator in S.num, etc.

This works as it should on Julia v1.0.2, but not on nightly 1bd316b. I can make it work on nightly by either

  • declaring const spectroscopic="spdfghiklmnoqrtuv" (which is the sane thing to do anyway), or
  • replacing the last line in term_string with Term{Int,Rational{Int},typeof(L)}(L, S, m[3] == "o" ? -1 : 1)

I tried to bisect using the following scripts:

#!/bin/bash
make || make clean && make || make clean && exit 125 # If build fails, we cannot test it
./julia term-test.jl || exit 1

term-test.jl:

using Test

struct Term{I<:Integer,R<:Rational{I},LT<:Union{I,R}}
    L::LT
    S::R
    parity::I
end

spectroscopic = "spdfghiklmnoqrtuv"

function term_string(s::AbstractString)
    m = match(r"([0-9]+)([A-Z]|\[[0-9/]+\])([oe ]{0,1})", s)
    println(m)
    L = lowercase(m[2])
    L = if L[1] == '['
        L = strip(L, ['[',']'])
        if occursin("/", L)
            Ls = split(L, "/")
            Rational(parse(Int, Ls[1]),parse(Int, Ls[2]))
        else
            parse(Int, L)
        end
    else
        findfirst(L, spectroscopic)[1]-1
    end
    S = (parse(Int, m[1]) - 1)//2
    Term(L, S, m[3] == "o" ? -1 : 1)
end

@test term_string("1S") == Term(0, 0//1, 1)
@test term_string("1[5]") == Term(5, 0//1, 1)
@test term_string("1[3/2]") == Term(3//2, 0//1, 1)

but I end up with the unsatisfactory result

There are only 'skip'ped commits left to test.
The first bad commit could be any of:
1bd316b972cb4ef83b9b8e79f435cc4d59029a93
bd21aa75eb4c0e56ef870f6bffea3281c724cbb1
We cannot bisect more!
bisect run cannot continue any more
@JeffBezanson JeffBezanson self-assigned this Dec 10, 2018
@JeffBezanson JeffBezanson added the types and dispatch Types, subtyping and method dispatch label Dec 10, 2018
@JeffBezanson
Copy link
Member

Thanks for trying the bisect. I can reduce this to the following:

julia> typeintersect(Tuple{Any,Rational{Int},Int},
                     Tuple{LT,R,I} where LT<:Union{I, R} where R<:Rational{I} where I<:Integer)
Tuple{Int64,Rational{Int64},Int64}

reminds me a little bit about type slicing in C++

Noooo this is just a garden variety compiler bug :)

@JeffBezanson JeffBezanson added the bug Indicates an unexpected problem or unintended behavior label Dec 10, 2018
@KristofferC KristofferC mentioned this issue Dec 12, 2018
52 tasks
KristofferC pushed a commit that referenced this issue Dec 20, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Indicates an unexpected problem or unintended behavior types and dispatch Types, subtyping and method dispatch
Projects
None yet
Development

No branches or pull requests

2 participants