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

String comparisons by === got broken in Julia 1.1.1/1.2 #32842

Closed
thautwarm opened this issue Aug 9, 2019 · 9 comments
Closed

String comparisons by === got broken in Julia 1.1.1/1.2 #32842

thautwarm opened this issue Aug 9, 2019 · 9 comments

Comments

@thautwarm
Copy link
Member

Ah, it's be noticed when @PallHaraldsson was testing Py2Jl.jl: JuliaCN/Py2Jl#5 (comment)

I positioned the unexpected behaviour and simplified it to code without package specific APIs:

let z = Dict{Symbol, Any}(:class=>"Module"), Main855_1218 = z
    function Main858_1221(Main856_1219::Dict)
        Main857_1220 = (get)(Main856_1219, :class) do
            nothing
        end
        @info :bug Main857_1220 typeof(Main857_1220) Main857_1220 === "Module"
        if Main857_1220 === "Module"
            1
        else
            nothing
        end
    end
    function Main858_1221(Main856_1219)
        nothing
    end
    Main854_1217 = Main858_1221(Main855_1218)
    if Main854_1217 === nothing
        throw("failed")
    else
        Main854_1217
    end
end
┌ Info: bug
│   Main857_1220 = "Module"typeof(Main857_1220) = String
└   Main857_1220 === "Module" = false
ERROR: "failed"
Stacktrace:
 [1] top-level scope at REPL[60]:19
@thautwarm
Copy link
Member Author

thautwarm commented Aug 9, 2019

P.S: Julia 1.1.0-rc2 works fine.

@bkamins
Copy link
Member

bkamins commented Aug 9, 2019

A minimal example of the problem seems to be:

julia> z1 = Dict{Symbol, Any}(:class=>"Module")
Dict{Symbol,Any} with 1 entry:
  :class => "Module"

julia> z2 = Dict(:class=>"Module")
Dict{Symbol,String} with 1 entry:
  :class => "Module"

julia> f(d) = d[:class] === "Module"
f (generic function with 1 method)

julia> g(d) = (d[:class]::String) === "Module"
g (generic function with 1 method)

julia> f(z1)
false

julia> f(z2)
true

julia> g(z1)
true

julia> g(z2)
true

@thautwarm
Copy link
Member Author

thautwarm commented Aug 9, 2019

It seems that there're 2 "Module" stored in distinct memory addresses. The functions seem to maintain its own constants?
I do think the constant strings with same literal values should be stored in the same place, you know since 1.0(or earlier) Julia strings cannot be modified.

@bkamins
Copy link
Member

bkamins commented Aug 9, 2019

The problem is the same with any wrapper type around a String that has Any eltype (Ref, vectors, etc.). It seems that the compiler assumes that heap-allocated immutable types can be compared by memory location without calling jl_egal.

@thautwarm
Copy link
Member Author

However we can store the same(literally) heap-allocated immutable data into a same place.

Only mutable literals(like [1, 2]) need to be allocated again and again in its each occurrence.

@chethega
Copy link
Contributor

chethega commented Aug 9, 2019

@bkamins I did not find a way of triggering the issue with non-String (e.g. non-bitstype immutable). Do you have an example?

For shorter reproduction:

julia> f(a,b)=@inbounds a[]===b
f (generic function with 1 method)

julia> f(Any["foo"], "foo")
false

The SSA-IR looks fine, but the llvm-IR looks broken.

@bkamins
Copy link
Member

bkamins commented Aug 9, 2019

@chethega - this is what I have also checked (SSA-IR vs llvm).

As far as I remember only String type in Base is a "primitive" immutable that is heap allocated (but core devs probably know better here).

@JeffBezanson
Copy link
Member

Probably fixed by #31585. In 1.2.

@thautwarm
Copy link
Member Author

Probably fixed by #31585. In 1.2.

Yes, I've just checked it. Thanks!

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

No branches or pull requests

4 participants