Skip to content

Parsing breakage for anonymous function body starting with parenthesis #60335

@yuyichao

Description

@yuyichao

On current master, if an anonymous function's body starts with parenthesis, then parsing treats the parenthesis body as a function argument and treats the actual argument list as (invalid) function name.

julia> function(a)
           (a)
       end
ERROR: ParseError:
# Error @ REPL[1]:1:9
function(a)
#       └─┘ ── Invalid signature in function definition
Stacktrace:
 [1] top-level scope
   @ REPL:1

This used to work just fine on older versions.

Note that this parsing was and is still new-line sensitive,

Old:

julia> function (a)(a)
       end
a (generic function with 1 method)

julia> function (a::Int)(a)
       end
ERROR: syntax: function argument name not unique: "a" around REPL[1]:1
Stacktrace:
 [1] top-level scope
   @ REPL[1]:1

julia> function (a)
           (a)
       end
#5 (generic function with 1 method)

julia> function (a::Int)
           (a)
       end
#8 (generic function with 1 method)

New:

julia> function (a)(a)
       end
a (generic function with 1 method)

julia> function (a::Int)(a)
       end
ERROR: syntax: function argument name not unique: "a" around REPL[4]:1
Stacktrace:
 [1] top-level scope
   @ REPL[4]:1

julia> function (a)
           (a)
       end
ERROR: ParseError:
# Error @ REPL[5]:1:10
function (a)
#        └─┘ ── Invalid signature in function definition
Stacktrace:
 [1] top-level scope
   @ REPL:1

julia> function (a::Int)
           (a)
       end
ERROR: ParseError:
# Error @ REPL[6]:1:10
function (a::Int)
#        └──────┘ ── Invalid signature in function definition
Stacktrace:
 [1] top-level scope
   @ REPL:1

This works on f5f4c01 but stops working on b9f40be so I assume it's caused by #59870 .

Not sure if the change was intentional but it does break a few packages.

https://github.com/bramtayl/Unzip.jl/blob/c239114b4b7faf71dab924c1832a11eaa0312d22/src/Unzip.jl#L144
https://github.com/FluxML/Zygote.jl/blob/35acbf3ef2b3ab7fbc5ba41cac04eab26ffe1759/src/lib/array.jl#L609
https://github.com/QuantumBFS/Yao.jl/blob/1f35b0320c2c6b4dd4d7e1343f94704486877e54/lib/YaoBlocks/src/autodiff/chainrules_patch.jl#L81
https://github.com/JuliaImages/ImageSegmentation.jl/blob/c6ba02673db9f29095d2d9915f21566d2d9f9830/src/core.jl#L352

Metadata

Metadata

Assignees

No one assigned

    Labels

    parserLanguage parsing and surface syntaxregressionRegression in behavior compared to a previous version

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions