Skip to content

Expression on same line of if condition is part of the true branch #52014

Open
@giordano

Description

@giordano
julia> if true println("hello")
           1
       end
hello
1

julia> Meta.@lower if true println("hello")
           1
       end
:($(Expr(:thunk, CodeInfo(
    @ none within `top-level scope`
1 ─     goto #3 if not true
    @ REPL[13]:1 within `top-level scope`
2println("hello")
│   @ REPL[13]:2 within `top-level scope`
└──     return 1
3return nothing
))))

The println("hello") expression becomes part of the body of the true branch. This "feature" is even more nasty when you do by mistake something like

if condition1 && condition2 condition3 && condition4

condition3 && condition4 aren't evaluated at all as part of the conditions of the if. This should arguably be a parser/syntax error. For example

julia> if true 1 2 3
           1
       end
ERROR: syntax: unexpected "2"
Stacktrace:
 [1] top-level scope
   @ none:1

2 is a syntax error, I don't see why 1 shouldn't.

Other weird interpretations of this "feature":

julia> if true :foo
           1
       end
ERROR: UndefVarError: `foo` not defined
Stacktrace:
 [1] top-level scope
   @ REPL[15]:1

julia> if true :2
           1
       end
ERROR: TypeError: non-boolean (UnitRange{Int64}) used in boolean context
Stacktrace:
 [1] top-level scope
   @ REPL[17]:1

Metadata

Metadata

Assignees

No one assigned

    Labels

    breakingThis change will break codeparserLanguage parsing and surface syntax

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions