From fdcc6e486d4e619ba515da442de422b393cb3cbe Mon Sep 17 00:00:00 2001 From: Sai Asish Y Date: Sat, 18 Jul 2026 04:48:11 -0700 Subject: [PATCH] Fix MethodError in ifelse when both branches are constants Signed-off-by: Sai Asish Y --- src/overloads/ifelse_global.jl | 1 + test/test_gradient.jl | 1 + 2 files changed, 2 insertions(+) diff --git a/src/overloads/ifelse_global.jl b/src/overloads/ifelse_global.jl index e50e19a2..8e8bb480 100644 --- a/src/overloads/ifelse_global.jl +++ b/src/overloads/ifelse_global.jl @@ -33,6 +33,7 @@ end output_union(tx::AbstractTracer, y) = tx output_union(x, ty::AbstractTracer) = ty +output_union(x, y) = x # neither branch is a tracer, so the result carries no dependency ## output union on AbstractArray outputs # TODO: add test diff --git a/test/test_gradient.jl b/test/test_gradient.jl index a1be9c1e..d41e5aa8 100644 --- a/test/test_gradient.jl +++ b/test/test_gradient.jl @@ -128,6 +128,7 @@ T = DEFAULT_GRADIENT_TRACER [1 1 1 1] @test J(x -> ifelse(x[2] < x[3], x[1] + x[2], 1.0), [1 2 3 4]) == [1 1 0 0] @test J(x -> ifelse(x[2] < x[3], 1.0, x[3] * x[4]), [1 2 3 4]) == [0 0 1 1] + @test J(x -> ifelse(x[2] < x[3], 1.0, 0.0) * x[1], [1 2 3 4]) == [1 0 0 0] function f_ampgo07(x) return (x[1] <= 0) * convert(eltype(x), Inf) +