type-intersection env may fail merging of multiple value matches #41096
Closed
Description
opened on Jun 5, 2021
When I was developing my own package, I came across a weird bug of Julia on the control flow using the if
-else
statement. The minimal reproducible example is as follows:
struct Modulate{M<:Union{Function, Val{true}, Val{false}}, id}
modulate::M
Modulate(id::Symbol, modulate::Function) = new{typeof(modulate), id}(modulate)
Modulate(id::Symbol, modulate::Bool=true) = new{Val{modulate}, id}(modulate|>Val)
end
@inline ismodulatable(modulate::Modulate) = ismodulatable(typeof(modulate))
@inline ismodulatable(::Type{<:Modulate{Val{B}}}) where B = B
@inline ismodulatable(::Type{<:Modulate{<:Function}}) = true
mutable struct Term{I, M<:Modulate}
modulate::M
Term{I}(modulate::Modulate) where I = new{I, typeof(modulate)}(modulate)
end
@inline ismodulatable(term::Term) = ismodulatable(typeof(term))
@inline ismodulatable(::Type{<:Term{I, M} where I}) where M = ismodulatable(M)
function newexpand(gen, name::Symbol)
flag = ismodulatable(getfield(gen, name))
println(flag)
if flag
println("flow for true.")
else
println("flow for false.")
end
end
t = Term{:t}(Modulate(:t, false))
μ = Term{:μ}(Modulate(:μ, false))
U = Term{:U}(Modulate(:U, false))
newexpand((t=t, μ=μ, U=U), :U)
In my computer, the output of the above script is
false
flow for true.
Apparently, something is wrong. However, if the last line of the above script is replaced with the following
newexpand((t=t, U=U), :U)
The result is correct as expected:
false
flow for false.
In fact, even for
newexpand((t=t, μ=t, U=U), :U)
The code also works fine:
false
flow for false.
My Julia version is
Julia Version 1.6.0
Commit f9720dc2eb (2021-03-24 12:55 UTC)
Platform Info:
OS: Windows (x86_64-w64-mingw32)
CPU: AMD Ryzen 7 4800HS with Radeon Graphics
WORD_SIZE: 64
LIBM: libopenlibm
LLVM: libLLVM-11.0.1 (ORCJIT, znver2)
Activity