Julia: misleading error message for misplaced = sign (disallow function definitions in argument position)

Created on 31 Jul 2016  路  7Comments  路  Source: JuliaLang/julia

A more complicated case of the example below caused me to spend a long time debugging a small typo. In the code below, there is a syntax error ('=' instead of ':') in the third line of the function. However, when this function is run in 0.4.6 (StrangeError.fn(1,2,3,4,5)), the following error message results. Note that the error message refers to the second line in the function, that is, the first place in the code where a + operation is used, not the line with the syntax error.

julia> StrangeError.fn(1,2,3,4,5)
ERROR: UndefVarError: + not defined
 in fn at C:\Users\vavasis\ownCloud\Documents\Katerina\cohesive\conic_jl\test_misplaced_eq.jl:4
module StrangeError

function fn(a,b,c,d,e)
    u = a + b
    c[a + b = c + d : e] = 0
    return u
end

end
error handling lowering

Most helpful comment

Your analysis is correct. If anything, I think we should further restrict where assignments are allowed to occur. In particular it would seem to be good to restrict function definitions to statement position.

All 7 comments

It seems this issue is a combination of several features of Julia:

  • as in #15483, infix operators can be defined by infix function definitions
  • a+b redefines + for all possible arguments since a,b are unqualified
  • a function definition can be used legally as a subscript since it is an expression
  • a function definition nested inside another function supersedes all other definitions of that function within the outer function even before it occurs.

So apparently this is not a bug in Julia, and yet some aspect of this code should really trigger a warning to help prevent Julia programmers from pulling their hair out (not a problem for me since I am already bald).

Your analysis is correct. If anything, I think we should further restrict where assignments are allowed to occur. In particular it would seem to be good to restrict function definitions to statement position.

What about this case:

function f(x)
    g(y) = x + y
end

Technically, that's not statement position.

Good point. Should be disallowed in argument position instead.

FWIW, error message is now: "ERROR: UndefRefError: access to undefined reference".

In response to @KristofferC 's "FWIW", my answer is: "not much!". For a more in-depth complaint about this, please see:

https://discourse.julialang.org/t/cannot-use-size-array-int-2-error-loaderror-undefreferror-access-to-undefined-reference/4138

Was this page helpful?
0 / 5 - 0 ratings