Weird syntax error

The code below fails with a syntax error that seems related to the creation of the anonymous function. I don’t see why it is a syntax error. Can anyone explain it?

let
    if 0 == 0
        1
    elseif 0 == 0 || !all(x -> true, [1]) 
        2
    else
        3
    end 
end

Obs: The code does not make sense. It is a minimal version of an error I am finding in another code.

1 Like

This can be reduced to

if false
elseif (true || all(x -> true, [1]))
    println("hi")
end

I tried changing some stuff like parentheses and ! around. To my eyes it seems to be a bug with the interaction of elseif, ||, and the all function.

1 Like

it has something to do with anonymous function

julia> if true
       elseif (0 == 0) || (any(iszero, [1]))
       end

works

1 Like

The minimal example from @adienes works in Julia 1.6 but fails in 1.7, so it’s probably a bug in Julia itself. Want to open an issue?

@rdeits, I have just downloaded the last Julia 1.6 (1.6.7) and tried @adienes minimal version there. It also fails. Which 1.6.X did you test?

seems like what’s necessary is:

  1. at least one anonymous function definition, and
  2. at least one short-circuiting operator && or ||, inside
  3. elseif

Causes syntax errors:

A.

if false
elseif false || (()->true)()
end

B.

if false
elseif false && (()->true)()
end

C.

if false
elseif false || false || (()->true)()
end

D.

if false
elseif (()->true)() || false
end

E.

if rand((false, true))
elseif rand((false, true)) || (()->true)()
end

F.

if true
elseif true || x->x
end

G.

if false
elseif false
elseif false || x->x
end

H.

if false
elseif (x->x) || x->x
end

If you pass the whole minimal example block of code into code_lowered, it correctly prints "hi". The error persists when using JuliaSyntax.jl btw.

Fascinating. It works for me in 1.6.2:

  | | |_| | | | (_| |  |  Version 1.6.2 (2021-07-14)
 _/ |\__'_|_|_|\__'_|  |  Official https://julialang.org/ release
|__/                   |

julia> if false
       elseif (true || all(x -> true, [1]))
           println("hi")
       end
hi

I hope you don’t mind that I stole your thunder, but I opened an issue for this bug here `elseif` raises an error when used with both short-circuiting evaluation and anonymous function · Issue #47410 · JuliaLang/julia · GitHub

2 Likes

Thanks @adienes. I have just subscribed to the bug report. Glad that my eyeballs were of some use.

3 Likes

This is fixed now by fix #47410, syntax error with anonfn inside `elseif` and short-circuit op by JeffBezanson · Pull Request #47499 · JuliaLang/julia · GitHub. Thanks for the reports and analysis!

1 Like