😤 Multi-line expressions aren't fully computed

One concept of Common Lisp that I somewhat miss in Julia is the concept of compiler hints. In CL if you write some code that the compiler can prove to do nothing (i.e. is removed by dead code elimination) then you get a note from the compiler warning you that it removed some code you wrote. This feature would probably completly eliminate mistakes like OP’s and also be correct in the (contrived) examples where + x actually has side effects.

I think there could be more nice opportunities for compiler hints in Julia. One idea I have would be a way of annotating a function with @typestable or something to indicate that you want it to be typestable. In case it is not (for some args), the compiler could write you nice notes explaining why it is not typestable.

Maybe once JuliaLowering.jl is out, one could think about realising compiler hints :slight_smile:

3 Likes

Although technically not compiler hints, @MilesCranmer’s DispatchDoctor.jl exports the macro @stable which goes in that direction.

2 Likes

This is a job for a linter. People often default to putting it on the compiler because AOT compilers tend to do this job, too, but we have JIT compilation and it happens distinctly from any syntax errors or warnings. Even our precompilation approaches don’t do anything close to warning us of valid yet suspicious code like for index in 1:length(array), they just cache compiled code. This sort of thing can be handled by a linter option that warns us as we’re writing, and we could turn it off if we’re writing stuff like:

c = a+b
-c |> mycache

Something else to point out, you want the linter option to ignore array literals. Leading + aren’t strange in arithmetic expressions, and spaces or newlines between full expressions are very unambiguous separators in matrix syntax.

julia> [1+20
       +100]
2-element Vector{Int64}:
  21
 100

julia> [1+20+
       100]
1-element Vector{Int64}:
 121

julia> [1+20 +100]
1×2 Matrix{Int64}:
 21  100

julia> [1+20+ 100]
1-element Vector{Int64}:
 121
2 Likes