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

UnionAll type where T<:... conflict with type T leads to syntax error "invalid type parameter name 'SSAValue'" #53940

Closed
quachpas opened this issue Apr 3, 2024 · 4 comments
Labels
bug Indicates an unexpected problem or unintended behavior compiler:lowering Syntax lowering (compiler front end, 2nd stage)

Comments

@quachpas
Copy link

quachpas commented Apr 3, 2024

When an UnionAll type with a nonunique name is specified, a syntax error "invalid type parameter name "SSAValue"" is raised.

MWE (error): Foo{T<:Integer, U<:AbstractArray{T} where T<:Integer}
julia> struct Foo{T<:Integer, U<:AbstractArray{T} where T<:Integer}
             a::T
             b::U
         end
ERROR: syntax: invalid type parameter name "SSAValue(404)" around REPL[21]:2
Stacktrace:
 [1] top-level scope
   @ REPL[21]:1
 [2] top-level scope
   @ ~/.julia/juliaup/julia-1.10.2+0.x64.linux.gnu/share/julia/stdlib/v1.10/REPL/src/REPL.jl:1428

When it is a simple type, the nested type T is identified as the first one T<:Integer.

Without UnionAll (no error): Bar{T<:Integer, U<:AbstractArray{T}}
julia> struct Bar{T<:Integer, U<:AbstractArray{T}}
             a::T
             b::U
         end

julia> Bar(1, [1])
Bar{Int64, Vector{Int64}}(1, [1])


julia> Bar(1, [Int32(1])
ERROR: MethodError: no method matching Bar(::Int64, ::Vector{Int32})

Closest candidates are:
  Bar(::T, ::U) where {T<:Integer, U<:(AbstractArray{T})}
   @ Main REPL[45]:2

Stacktrace:
 [1] top-level scope
   @ REPL[64]:1
 [2] top-level scope
   @ ~/.julia/juliaup/julia-1.10.2+0.x64.linux.gnu/share/julia/stdlib/v1.10/REPL/src/REPL.jl:1428

With a simple UnionAll type, everything works as expected

Without conflicting name (no error): FooBar{U<:AbstractArray{T} where T<:Integer}
struct FooBar{U<:AbstractArray{T} where T<:Integer}
                      b::U
                  end

julia> FooBar([1])
FooBar{Vector{Int64}}([1])

julia> FooBar([Int32(1)])
FooBar{Vector{Int32}}(Int32[1])

Below are existing errors for different, but similar situations

function static parameter name not unique: struct BarFoo{T<:Integer, T<:AbstractFloat}
julia> struct BarFoo{T<:Integer, T<:AbstractFloat}
             a::T
         end
ERROR: syntax: function static parameter name not unique: "T" around REPL[22]:2
Stacktrace:
 [1] top-level scope
   @ REPL[22]:1
 [2] top-level scope
   @ ~/.julia/juliaup/julia-1.10.2+0.x64.linux.gnu/share/julia/stdlib/v1.10/REPL/src/REPL.jl:1428
function argument and static parameter name not distinct: foo(A::A) where A<:Integer = A()
julia> foo(A::A) where A<:Integer = A()
ERROR: syntax: function argument and static parameter name not distinct: "A" around REPL[13]:1
Stacktrace:
 [1] top-level scope
   @ REPL[13]:1
 [2] top-level scope
   @ ~/.julia/juliaup/julia-1.10.2+0.x64.linux.gnu/share/julia/stdlib/v1.10/REPL/src/REPL.jl:1428
versioninfo
julia> versioninfo()
Julia Version 1.10.2
Commit bd47eca2c8a (2024-03-01 10:14 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: Linux (x86_64-linux-gnu)
  CPU: 8 × 11th Gen Intel(R) Core(TM) i7-1165G7 @ 2.80GHz
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-15.0.7 (ORCJIT, tigerlake)
Threads: 1 default, 0 interactive, 1 GC (on 8 virtual cores)

Thanks!

@Keno Keno added bug Indicates an unexpected problem or unintended behavior compiler:lowering Syntax lowering (compiler front end, 2nd stage) labels Apr 3, 2024
@Keno
Copy link
Member

Keno commented Apr 3, 2024

``

julia> struct Foo{T<:Integer, U<:AbstractArray{T} where T<:Integer}
             a::T
             b::U
         end
ERROR: syntax: invalid type parameter name "SSAValue(404)" around REPL[21]:2
Stacktrace:
 [1] top-level scope
   @ REPL[21]:1
 [2] top-level scope
   @ ~/.julia/juliaup/julia-1.10.2+0.x64.linux.gnu/share/julia/stdlib/v1.10/REPL/src/REPL.jl:1428

Looks like a bug where lowering doesn't recognize that where introduces local bindings and tries to use the tparam instead.

All other errors are intentional.

@quachpas
Copy link
Author

quachpas commented Apr 3, 2024

All other errors are intentional.

Yes! Sorry for the confusion, the other errors were added for context. I was not sure whether this particular situation should raise an error or not?
Given that Bar{T<:Integer, U<:AbstractArray{T}} works as expected, then the first situation looks ambiguous to me, e.g. should Foo{T<:Integer, U<:AbstractArray{T} where T<:AbstractFloat} work?

@Keno
Copy link
Member

Keno commented Apr 3, 2024

should Foo{T<:Integer, U<:AbstractArray{T} where T<:AbstractFloat} work?

Yes, where should shadow the outer definition of T.

@mlechu
Copy link
Member

mlechu commented Mar 20, 2025

Appears to have been fixed by #57317

@vtjnash vtjnash closed this as completed Mar 20, 2025
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 compiler:lowering Syntax lowering (compiler front end, 2nd stage)
Projects
None yet
Development

No branches or pull requests

4 participants