Welcome! Please see the About page for a little more info on how this works.

0 votes
in Compiler by

The following minimal example shows the error:

(defn f ^double []) (f) => NullPointerException

When decompiling the function f I found the following return expression:

return null.doubleValue();

This happened in a Java interop scenario where the called Java method had no return value but was in the return position of the primitive Clojure function.
The compiler should check for null on compilation.

Another example - calling a method with void return as the last expression fails in a similar way:

(defn f ^double [^SomeClassToAvoidRuntimeReflection obj, x, y] (.someMethod obj, x, y)) (f obj, x, y) => NullPointerException

9 Answers

0 votes
by

Comment made by: alexmiller

What do you expect to happen in this case? You declared a function as returning a double but didn't return one.

0 votes
by

Comment made by: gv

Since this is only the minimal example the error is relatively easy to spot.
Consider the following small example with Java interop:

(defn f ^double [^SomeClassToAvoidRuntimeReflection obj, x, y] (.someMethod obj, x, y)) (f obj, x, y) => NullPointerException

In this example it is much harder to find the reason for the NPE because you'd first suspect obj to be null.

I expect a check in the compiler at the point where "return null.doubleValue();" is emitted, followed by an error message, e.g. "Primitive return value of type 'double' expected, but no value is returned.".

0 votes
by

Comment made by: wagjo

Your second example seems perfectly OK to me, compiler should not report any error and NPE check must be at runtime.

0 votes
by

Comment made by: gv

@Jozef: No, you are wrong. The compiler infers via reflection at compile time that the called method does not return a value and emits "return null.doubleValue()". So this can and should be reported as explicit error at compile time. I added a typehint to make it clear that there is no runtime reflection involved.
You would be right, if the compiler emitted something like "return somevar.doubleValue();" because then at compile time there is no knowledge about a possible "null" value.

0 votes
by

Comment made by: jafingerhut

Gunnar, in your example, is the method 'someMethod' declared to return void, or something else? Adding that info to your example might help clarify it.

0 votes
by

Comment made by: wagjo

Gunnar, the second example was ambiguous and strayed away the discussion. Anyway, whether returning wrong type is through the native method or not, it is a user error in the first place. Right now it is reported at runtime. For me this ticket should be a minor enhancement instead of defect.

0 votes
by

Comment made by: gv

Yes, the reason is a user error. But one that is harder to debug than necessary.
Also, it is clearly a defect since emitting 'null.doubleValue()' can not be considered as a valid compilation.

Andy, yes 'someMethod' is declared to return void. I'd edit the original ticket text to add the example and the java method return value information, but it seems jira does not let me.

0 votes
by

Comment made by: alexmiller

I added the second example (with clarifying void comment) to the description.

0 votes
by
Reference: https://clojure.atlassian.net/browse/CLJ-1432 (reported by alex+import)
...