JuliaLang / julia

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

Nested tuple construction hangs forever in 1.10 and earlier #56052

Open luchr opened 1 month ago

luchr commented 1 month ago

Take the following test file nestedtuples.jl:

function please_return()
  tup = ()
  for _ in 1:2
    tup = (tup, ())
  end
  return tup
end

@show please_return()

The function please_return does not terminate (julia hangs forever) for version 1.10.5. It does terminate and returns the expected result in version 1.11.0 though.

Output:

julia> versioninfo()
Julia Version 1.10.5
Commit 6f3fdf7b362 (2024-08-27 14:19 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: Linux (x86_64-linux-gnu)
  CPU: 16 × 12th Gen Intel(R) Core(TM) i5-1240P
  WORD_SIZE: 64
  LIBM: libopenlibm
  LLVM: libLLVM-15.0.7 (ORCJIT, alderlake)
Threads: 1 default, 0 interactive, 1 GC (on 16 virtual cores)

julia> include("./nestedtuples.jl")

<<<hangs forever>>>

For comparison the output of 1.11:

julia> versioninfo()
Julia Version 1.11.0
Commit 501a4f25c2b (2024-10-07 11:40 UTC)
Build Info:
  Official https://julialang.org/ release
Platform Info:
  OS: Linux (x86_64-linux-gnu)
  CPU: 16 × 12th Gen Intel(R) Core(TM) i5-1240P
  WORD_SIZE: 64
  LLVM: libLLVM-16.0.6 (ORCJIT, alderlake)
Threads: 1 default, 0 interactive, 1 GC (on 16 virtual cores)

julia> include("./nestedtuples.jl")
please_return() = (((), ()), ())
(((), ()), ())

Is this a known bug/feature?

oscardssmith commented 1 month ago

This appears to be a bug in tmerge that goes back at least as far as 1.6. Good to see that it's fixed in 1.11.

oscardssmith commented 1 month ago

specifically, the bug is that

T = Tuple{}
T_old = Union{}
while T != T_old
    T_old, T = T, Core.Compiler.tmerge(T, Tuple{T, Tuple{}})
end

is an infinite loop prior to 1.11

oscardssmith commented 1 month ago

This was likely fixed by some combination of https://github.com/JuliaLang/julia/pull/55757, https://github.com/JuliaLang/julia/pull/50927, and https://github.com/JuliaLang/julia/pull/53876. @Keno, @vtjnash any opinions on the backport-ability of these?