Skip to content

Calling promote_tuple_eltype() causes promote_type to change behavior #228

@rdeits

Description

@rdeits

Yeah, this is a weird one. Consider the following:

module X

using StaticArrays: SVector

struct Foo
    x::Int
end

struct Bar{S}
    x::Int
end

Base.convert(::Type{Foo}, b::Bar) = Foo(b.x)
Base.promote_rule(::Type{Bar{S1}}, ::Type{Bar{S2}}) where {S1, S2} = Foo

end

b1 = X.Bar{:x}(1)
b2 = X.Bar{:y}(2)

@show promote_type(typeof(b1), typeof(b2))

shows:

promote_type(typeof(b1), typeof(b2)) = X.Foo

Now try the same thing, but call the SVector constructor before looking at the result of promote_type:

module X

using StaticArrays: SVector

struct Foo
    x::Int
end

struct Bar{S}
    x::Int
end

Base.convert(::Type{Foo}, b::Bar) = Foo(b.x)
Base.promote_rule(::Type{Bar{S1}}, ::Type{Bar{S2}}) where {S1, S2} = Foo

end

b1 = X.Bar{:x}(1)
b2 = X.Bar{:y}(2)

StaticArrays.SVector(b1, b2)
@show promote_type(typeof(b1), typeof(b2))

shows:

promote_type(typeof(b1), typeof(b2)) = X.Bar

In the second example, promote_type gives the wrong result (Bar instead of Foo).

julia> versioninfo()
Julia Version 0.6.0-rc3.0
Commit ad290e93e4 (2017-06-07 11:53 UTC)
Platform Info:
  OS: macOS (x86_64-apple-darwin13.4.0)
  CPU: Intel(R) Core(TM) i7-2860QM CPU @ 2.50GHz
  WORD_SIZE: 64
  BLAS: libopenblas (USE64BITINT DYNAMIC_ARCH NO_AFFINITY Sandybridge)
  LAPACK: libopenblas64_
  LIBM: libopenlibm
  LLVM: libLLVM-3.9.1 (ORCJIT, sandybridge)

This seems like it might be a Julia bug, or it might be that StaticArrays is doing something bad with @pure. I'm using StaticArrays at 9ccda4e

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions