JuliaLang / julia

The Julia Programming Language
https://julialang.org/
MIT License
45.52k stars 5.46k forks source link

Undefined typevar when it is clearly defined #55933

Open wsmoses opened 6 hours ago

wsmoses commented 6 hours ago

On Julia 1.10.5

julia> T = (Vector{Tuple{Code}} where {Code<:Integer})
Array{Tuple{Code}, 1} where Code<:Integer

julia> eltype(T)
Any

julia> @inline ptreltype(::Type{Array{T,N}}) where {T,N} = T
ptreltype (generic function with 1 method)

julia> ptreltype(T)
ERROR: UndefVarError: `T` not defined
Stacktrace:
 [1] ptreltype(::Type{Array{Tuple{Code}, 1} where Code<:Integer})
   @ Main ./REPL[6]:1
 [2] top-level scope
   @ REPL[7]:1

Not entirely sure what I would expect here (actually I would expect a methoderror), but definitely not this.

cc @mofeing

timholy commented 6 hours ago

Seems reasonable to me? This is where @isdefined comes in to play.

wsmoses commented 5 hours ago

Howso?

Consider this code which similarly crashes. I would expect this to return nothing.

julia> T = (Vector{Tuple{Code}} where {Code<:Integer})
Array{Tuple{Code}, 1} where Code<:Integer

julia> @inline ptreltype(x) = nothing
ptreltype (generic function with 1 method)

julia> @inline ptreltype(::Type{Array{T,N}}) where {T,N} = T
ptreltype (generic function with 2 methods)

julia> ptreltype(T)
ERROR: UndefVarError: `T` not defined
Stacktrace:
 [1] ptreltype(::Type{Array{Tuple{Code}, 1} where Code<:Integer})
   @ Main ./REPL[3]:1
 [2] top-level scope
   @ REPL[4]:1
wsmoses commented 5 hours ago

cc @vchuravy @gbaraldi

vtjnash commented 4 hours ago

It appears to be fixed in v1.11

$ julia +v1.10
               _
   _       _ _(_)_     |  Documentation: https://docs.julialang.org
  (_)     | (_) (_)    |
   _ _   _| |_  __ _   |  Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 1.10.0 (2023-12-25)
 _/ |\__'_|_|_|\__'_|  |  
|__/                   |

julia> Type{Vector{Tuple{Code}} where {Code}} <: Type{Vector{T}} where T
true

julia> Type{Vector{Tuple{Code}} where {Code<:Integer}} <: Type{Vector{T}} where T
true

julia> Type{Vector{Tuple{Code}} where {Code<:Integer}} <: Type{Array{T, N}} where {T, N}
true

julia> typeintersect(Type{Vector{Tuple{Code}} where {Code}}, Type{Vector{T}} where T)
Type{Array{Tuple{Code}, 1} where Code}

julia> typeintersect(Type{Vector{Tuple{Code}} where {Code}}, Type{Array{T,N}} where {T,N})
Type{Array{Tuple{Code}, 1} where Code}

julia> typeintersect(Type{Vector{Tuple{Code}} where {Code<:Integer}}, Type{Array{T,N}} where {T,N})
Type{Array{Tuple{Code}, 1} where Code<:Integer}

$ julia +v1.11
               _
   _       _ _(_)_     |  Documentation: https://docs.julialang.org
  (_)     | (_) (_)    |
   _ _   _| |_  __ _   |  Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` |  |
  | | |_| | | | (_| |  |  Version 1.11.0-rc2.54 (2024-08-19)
 _/ |\__'_|_|_|\__'_|  |  backports-release-1.11/8c60a17508d* (fork: 359 commits, 226 days)
|__/                   |

julia> Type{Vector{Tuple{Code}} where {Code}} <: Type{Vector{T}} where T
false

julia> Type{Vector{Tuple{Code}} where {Code<:Integer}} <: Type{Vector{T}} where T
false

julia> Type{Vector{Tuple{Code}} where {Code<:Integer}} <: Type{Array{T, N}} where {T, N}
false

julia> typeintersect(Type{Vector{Tuple{Code}} where {Code}}, Type{Vector{T}} where T)
Union{}

julia> typeintersect(Type{Vector{Tuple{Code}} where {Code}}, Type{Array{T,N}} where {T,N})
Union{}

julia> typeintersect(Type{Vector{Tuple{Code}} where {Code<:Integer}}, Type{Array{T,N}} where {T,N})
Union{}
wsmoses commented 4 hours ago

Any chance whatever the fix was can get backported?