Details

Description

Problem:
When making java calls (or inlined functions), if both args and param are primitive, no widening conversion is used to locate the proper overloaded method/constructor.

Examples:

user=> (Integer. (byte 0))
java.lang.IllegalArgumentException: No matching ctor found for class java.lang.Integer (NO_SOURCE_FILE:0)
</code></pre>
The above occurs because there is no Integer(byte) constructor, though it should match on Integer(int).
<pre><code>user=> (bit-shift-left (byte 1) 1)
Reflection warning, NO_SOURCE_PATH:3 - call to shiftLeft can't be resolved.
2

In the above, a call is made via reflection to Numbers.shiftLeft(Object, Object) and its associated auto-boxing, instead of directly to the perfectly adequate Numbers.shiftLeft(long, int).

Workarounds:
Explicitly casting to the formal type.

Ancillary benefits of fixing:
It would also reduce the amount of method overloading, e.g., RT.intCast(char), intCast(byte), intCast(short), could all be removed, since such calls would pass to RT.intCast(int).

The failing test happens when trying to find the correct equiv for signature (Number, long). Is the compiler wrong to propose this signature, or is the resolution method wrong in not having an answer? (It thinks two signatures are tied: (Object, long) and (Number, Number).)

Exception in thread "main" java.lang.IllegalArgumentException: More than one matching method found: equiv, compiling:(clojure/pprint/cl_format.clj:428)
at clojure.lang.Compiler.analyzeSeq(Compiler.java:6062)
at clojure.lang.Compiler.analyze(Compiler.java:5866)
at clojure.lang.Compiler.analyze(Compiler.java:5827)
at clojure.lang.Compiler.analyzeSeq(Compiler.java:6050)
at clojure.lang.Compiler.analyze(Compiler.java:5866)
at clojure.lang.Compiler.access$100(Compiler.java:35)
at clojure.lang.Compiler$LetExpr$Parser.parse(Compiler.java:5492)
at clojure.lang.Compiler.analyzeSeq(Compiler.java:6055)
at clojure.lang.Compiler.analyze(Compiler.java:5866)
at clojure.lang.Compiler.analyzeSeq(Compiler.java:6043)
at clojure.lang.Compiler.analyze(Compiler.java:5866)
at clojure.lang.Compiler.analyzeSeq(Compiler.java:6043)
at clojure.lang.Compiler.analyze(Compiler.java:5866)
at clojure.lang.Compiler.analyze(Compiler.java:5827)
at clojure.lang.Compiler$IfExpr$Parser.parse(Compiler.java:2372)
at clojure.lang.Compiler.analyzeSeq(Compiler.java:6055)
at clojure.lang.Compiler.analyze(Compiler.java:5866)
at clojure.lang.Compiler.analyzeSeq(Compiler.java:6043)
at clojure.lang.Compiler.analyze(Compiler.java:5866)
at clojure.lang.Compiler.analyze(Compiler.java:5827)
at clojure.lang.Compiler$InvokeExpr.parse(Compiler.java:3277)
at clojure.lang.Compiler.analyzeSeq(Compiler.java:6057)
at clojure.lang.Compiler.analyze(Compiler.java:5866)
at clojure.lang.Compiler.analyze(Compiler.java:5827)
at clojure.lang.Compiler$BodyExpr$Parser.parse(Compiler.java:5231)
at clojure.lang.Compiler$LetExpr$Parser.parse(Compiler.java:5527)
at clojure.lang.Compiler.analyzeSeq(Compiler.java:6055)
at clojure.lang.Compiler.analyze(Compiler.java:5866)
at clojure.lang.Compiler.analyzeSeq(Compiler.java:6043)
at clojure.lang.Compiler.analyze(Compiler.java:5866)
at clojure.lang.Compiler.analyze(Compiler.java:5827)
at clojure.lang.Compiler$BodyExpr$Parser.parse(Compiler.java:5231)
at clojure.lang.Compiler.analyzeSeq(Compiler.java:6055)
at clojure.lang.Compiler.analyze(Compiler.java:5866)
at clojure.lang.Compiler.analyze(Compiler.java:5827)
at clojure.lang.Compiler$IfExpr$Parser.parse(Compiler.java:2385)
at clojure.lang.Compiler.analyzeSeq(Compiler.java:6055)
at clojure.lang.Compiler.analyze(Compiler.java:5866)
at clojure.lang.Compiler.analyze(Compiler.java:5827)
at clojure.lang.Compiler$BodyExpr$Parser.parse(Compiler.java:5231)
at clojure.lang.Compiler$LetExpr$Parser.parse(Compiler.java:5527)
at clojure.lang.Compiler.analyzeSeq(Compiler.java:6055)
at clojure.lang.Compiler.analyze(Compiler.java:5866)
at clojure.lang.Compiler.analyzeSeq(Compiler.java:6043)
at clojure.lang.Compiler.analyze(Compiler.java:5866)
at clojure.lang.Compiler.analyze(Compiler.java:5827)
at clojure.lang.Compiler$IfExpr$Parser.parse(Compiler.java:2385)
at clojure.lang.Compiler.analyzeSeq(Compiler.java:6055)
at clojure.lang.Compiler.analyze(Compiler.java:5866)
at clojure.lang.Compiler.analyze(Compiler.java:5827)
at clojure.lang.Compiler$BodyExpr$Parser.parse(Compiler.java:5231)
at clojure.lang.Compiler$LetExpr$Parser.parse(Compiler.java:5527)
at clojure.lang.Compiler.analyzeSeq(Compiler.java:6055)
at clojure.lang.Compiler.analyze(Compiler.java:5866)
at clojure.lang.Compiler.analyzeSeq(Compiler.java:6043)
at clojure.lang.Compiler.analyze(Compiler.java:5866)
at clojure.lang.Compiler.analyze(Compiler.java:5827)
at clojure.lang.Compiler$BodyExpr$Parser.parse(Compiler.java:5231)
at clojure.lang.Compiler$FnMethod.parse(Compiler.java:4667)
at clojure.lang.Compiler$FnExpr.parse(Compiler.java:3397)
at clojure.lang.Compiler.analyzeSeq(Compiler.java:6053)
at clojure.lang.Compiler.analyze(Compiler.java:5866)
at clojure.lang.Compiler.analyzeSeq(Compiler.java:6043)
at clojure.lang.Compiler.analyze(Compiler.java:5866)
at clojure.lang.Compiler.access$100(Compiler.java:35)
at clojure.lang.Compiler$DefExpr$Parser.parse(Compiler.java:480)
at clojure.lang.Compiler.analyzeSeq(Compiler.java:6055)
at clojure.lang.Compiler.analyze(Compiler.java:5866)
at clojure.lang.Compiler.analyze(Compiler.java:5827)
at clojure.lang.Compiler.eval(Compiler.java:6114)
at clojure.lang.Compiler.load(Compiler.java:6545)
at clojure.lang.RT.loadResourceScript(RT.java:340)
at clojure.lang.RT.loadResourceScript(RT.java:331)
at clojure.lang.RT.load(RT.java:409)
at clojure.lang.RT.load(RT.java:381)
at clojure.core$load$fn__1427.invoke(core.clj:5308)
at clojure.core$load.doInvoke(core.clj:5307)
at clojure.lang.RestFn.invoke(RestFn.java:409)
at clojure.pprint$eval3969.invoke(pprint.clj:46)
at clojure.lang.Compiler.eval(Compiler.java:6110)
at clojure.lang.Compiler.load(Compiler.java:6545)
at clojure.lang.RT.loadResourceScript(RT.java:340)
at clojure.lang.RT.loadResourceScript(RT.java:331)
at clojure.lang.RT.load(RT.java:409)
at clojure.lang.RT.load(RT.java:381)
at clojure.core$load$fn__1427.invoke(core.clj:5308)
at clojure.core$load.doInvoke(core.clj:5307)
at clojure.lang.RestFn.invoke(RestFn.java:409)
at clojure.core$load_one.invoke(core.clj:5132)
at clojure.core$load_lib.doInvoke(core.clj:5169)
at clojure.lang.RestFn.applyTo(RestFn.java:143)
at sun.reflect.GeneratedMethodAccessor11.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at clojure.lang.Reflector.invokeMatchingMethod(Reflector.java:77)
at clojure.lang.Reflector.invokeInstanceMethod(Reflector.java:28)
at clojure.core$apply.invoke(core.clj:602)
at clojure.core$load_libs.doInvoke(core.clj:5203)
at clojure.lang.RestFn.applyTo(RestFn.java:138)
at sun.reflect.GeneratedMethodAccessor11.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at clojure.lang.Reflector.invokeMatchingMethod(Reflector.java:77)
at clojure.lang.Reflector.invokeInstanceMethod(Reflector.java:28)
at clojure.core$apply.invoke(core.clj:604)
at clojure.core$use.doInvoke(core.clj:5283)
at clojure.lang.RestFn.invoke(RestFn.java:409)
at clojure.main$repl.doInvoke(main.clj:196)
at clojure.lang.RestFn.invoke(RestFn.java:422)
at clojure.main$repl_opt.invoke(main.clj:267)
at clojure.main$main.doInvoke(main.clj:362)
at clojure.lang.RestFn.invoke(RestFn.java:409)
at clojure.lang.Var.invoke(Var.java:401)
at clojure.lang.AFn.applyToHelper(AFn.java:163)
at clojure.lang.Var.applyTo(Var.java:518)
at clojure.main.main(main.java:37)
Caused by: java.lang.IllegalArgumentException: More than one matching method found: equiv
at clojure.lang.Reflector.getMatchingParams(Reflector.java:639)
at clojure.lang.Reflector.getMatchingParams(Reflector.java:578)
at clojure.lang.Reflector.getMatchingMethod(Reflector.java:569)
at clojure.lang.Compiler$StaticMethodExpr.<init>(Compiler.java:1439)
at clojure.lang.Compiler$HostExpr$Parser.parse(Compiler.java:896)
at clojure.lang.Compiler.analyzeSeq(Compiler.java:6055)
... 115 more

Stuart Halloway
added a comment - 28/Jan/11 12:30 PM The failing test happens when trying to find the correct equiv for signature (Number, long). Is the compiler wrong to propose this signature, or is the resolution method wrong in not having an answer? (It thinks two signatures are tied: (Object, long) and (Number, Number).)

Exception in thread "main" java.lang.IllegalArgumentException: More than one matching method found: equiv, compiling:(clojure/pprint/cl_format.clj:428)
at clojure.lang.Compiler.analyzeSeq(Compiler.java:6062)
at clojure.lang.Compiler.analyze(Compiler.java:5866)
at clojure.lang.Compiler.analyze(Compiler.java:5827)
at clojure.lang.Compiler.analyzeSeq(Compiler.java:6050)
at clojure.lang.Compiler.analyze(Compiler.java:5866)
at clojure.lang.Compiler.access$100(Compiler.java:35)
at clojure.lang.Compiler$LetExpr$Parser.parse(Compiler.java:5492)
at clojure.lang.Compiler.analyzeSeq(Compiler.java:6055)
at clojure.lang.Compiler.analyze(Compiler.java:5866)
at clojure.lang.Compiler.analyzeSeq(Compiler.java:6043)
at clojure.lang.Compiler.analyze(Compiler.java:5866)
at clojure.lang.Compiler.analyzeSeq(Compiler.java:6043)
at clojure.lang.Compiler.analyze(Compiler.java:5866)
at clojure.lang.Compiler.analyze(Compiler.java:5827)
at clojure.lang.Compiler$IfExpr$Parser.parse(Compiler.java:2372)
at clojure.lang.Compiler.analyzeSeq(Compiler.java:6055)
at clojure.lang.Compiler.analyze(Compiler.java:5866)
at clojure.lang.Compiler.analyzeSeq(Compiler.java:6043)
at clojure.lang.Compiler.analyze(Compiler.java:5866)
at clojure.lang.Compiler.analyze(Compiler.java:5827)
at clojure.lang.Compiler$InvokeExpr.parse(Compiler.java:3277)
at clojure.lang.Compiler.analyzeSeq(Compiler.java:6057)
at clojure.lang.Compiler.analyze(Compiler.java:5866)
at clojure.lang.Compiler.analyze(Compiler.java:5827)
at clojure.lang.Compiler$BodyExpr$Parser.parse(Compiler.java:5231)
at clojure.lang.Compiler$LetExpr$Parser.parse(Compiler.java:5527)
at clojure.lang.Compiler.analyzeSeq(Compiler.java:6055)
at clojure.lang.Compiler.analyze(Compiler.java:5866)
at clojure.lang.Compiler.analyzeSeq(Compiler.java:6043)
at clojure.lang.Compiler.analyze(Compiler.java:5866)
at clojure.lang.Compiler.analyze(Compiler.java:5827)
at clojure.lang.Compiler$BodyExpr$Parser.parse(Compiler.java:5231)
at clojure.lang.Compiler.analyzeSeq(Compiler.java:6055)
at clojure.lang.Compiler.analyze(Compiler.java:5866)
at clojure.lang.Compiler.analyze(Compiler.java:5827)
at clojure.lang.Compiler$IfExpr$Parser.parse(Compiler.java:2385)
at clojure.lang.Compiler.analyzeSeq(Compiler.java:6055)
at clojure.lang.Compiler.analyze(Compiler.java:5866)
at clojure.lang.Compiler.analyze(Compiler.java:5827)
at clojure.lang.Compiler$BodyExpr$Parser.parse(Compiler.java:5231)
at clojure.lang.Compiler$LetExpr$Parser.parse(Compiler.java:5527)
at clojure.lang.Compiler.analyzeSeq(Compiler.java:6055)
at clojure.lang.Compiler.analyze(Compiler.java:5866)
at clojure.lang.Compiler.analyzeSeq(Compiler.java:6043)
at clojure.lang.Compiler.analyze(Compiler.java:5866)
at clojure.lang.Compiler.analyze(Compiler.java:5827)
at clojure.lang.Compiler$IfExpr$Parser.parse(Compiler.java:2385)
at clojure.lang.Compiler.analyzeSeq(Compiler.java:6055)
at clojure.lang.Compiler.analyze(Compiler.java:5866)
at clojure.lang.Compiler.analyze(Compiler.java:5827)
at clojure.lang.Compiler$BodyExpr$Parser.parse(Compiler.java:5231)
at clojure.lang.Compiler$LetExpr$Parser.parse(Compiler.java:5527)
at clojure.lang.Compiler.analyzeSeq(Compiler.java:6055)
at clojure.lang.Compiler.analyze(Compiler.java:5866)
at clojure.lang.Compiler.analyzeSeq(Compiler.java:6043)
at clojure.lang.Compiler.analyze(Compiler.java:5866)
at clojure.lang.Compiler.analyze(Compiler.java:5827)
at clojure.lang.Compiler$BodyExpr$Parser.parse(Compiler.java:5231)
at clojure.lang.Compiler$FnMethod.parse(Compiler.java:4667)
at clojure.lang.Compiler$FnExpr.parse(Compiler.java:3397)
at clojure.lang.Compiler.analyzeSeq(Compiler.java:6053)
at clojure.lang.Compiler.analyze(Compiler.java:5866)
at clojure.lang.Compiler.analyzeSeq(Compiler.java:6043)
at clojure.lang.Compiler.analyze(Compiler.java:5866)
at clojure.lang.Compiler.access$100(Compiler.java:35)
at clojure.lang.Compiler$DefExpr$Parser.parse(Compiler.java:480)
at clojure.lang.Compiler.analyzeSeq(Compiler.java:6055)
at clojure.lang.Compiler.analyze(Compiler.java:5866)
at clojure.lang.Compiler.analyze(Compiler.java:5827)
at clojure.lang.Compiler.eval(Compiler.java:6114)
at clojure.lang.Compiler.load(Compiler.java:6545)
at clojure.lang.RT.loadResourceScript(RT.java:340)
at clojure.lang.RT.loadResourceScript(RT.java:331)
at clojure.lang.RT.load(RT.java:409)
at clojure.lang.RT.load(RT.java:381)
at clojure.core$load$fn__1427.invoke(core.clj:5308)
at clojure.core$load.doInvoke(core.clj:5307)
at clojure.lang.RestFn.invoke(RestFn.java:409)
at clojure.pprint$eval3969.invoke(pprint.clj:46)
at clojure.lang.Compiler.eval(Compiler.java:6110)
at clojure.lang.Compiler.load(Compiler.java:6545)
at clojure.lang.RT.loadResourceScript(RT.java:340)
at clojure.lang.RT.loadResourceScript(RT.java:331)
at clojure.lang.RT.load(RT.java:409)
at clojure.lang.RT.load(RT.java:381)
at clojure.core$load$fn__1427.invoke(core.clj:5308)
at clojure.core$load.doInvoke(core.clj:5307)
at clojure.lang.RestFn.invoke(RestFn.java:409)
at clojure.core$load_one.invoke(core.clj:5132)
at clojure.core$load_lib.doInvoke(core.clj:5169)
at clojure.lang.RestFn.applyTo(RestFn.java:143)
at sun.reflect.GeneratedMethodAccessor11.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at clojure.lang.Reflector.invokeMatchingMethod(Reflector.java:77)
at clojure.lang.Reflector.invokeInstanceMethod(Reflector.java:28)
at clojure.core$apply.invoke(core.clj:602)
at clojure.core$load_libs.doInvoke(core.clj:5203)
at clojure.lang.RestFn.applyTo(RestFn.java:138)
at sun.reflect.GeneratedMethodAccessor11.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at clojure.lang.Reflector.invokeMatchingMethod(Reflector.java:77)
at clojure.lang.Reflector.invokeInstanceMethod(Reflector.java:28)
at clojure.core$apply.invoke(core.clj:604)
at clojure.core$use.doInvoke(core.clj:5283)
at clojure.lang.RestFn.invoke(RestFn.java:409)
at clojure.main$repl.doInvoke(main.clj:196)
at clojure.lang.RestFn.invoke(RestFn.java:422)
at clojure.main$repl_opt.invoke(main.clj:267)
at clojure.main$main.doInvoke(main.clj:362)
at clojure.lang.RestFn.invoke(RestFn.java:409)
at clojure.lang.Var.invoke(Var.java:401)
at clojure.lang.AFn.applyToHelper(AFn.java:163)
at clojure.lang.Var.applyTo(Var.java:518)
at clojure.main.main(main.java:37)
Caused by: java.lang.IllegalArgumentException: More than one matching method found: equiv
at clojure.lang.Reflector.getMatchingParams(Reflector.java:639)
at clojure.lang.Reflector.getMatchingParams(Reflector.java:578)
at clojure.lang.Reflector.getMatchingMethod(Reflector.java:569)
at clojure.lang.Compiler$StaticMethodExpr.<init>(Compiler.java:1439)
at clojure.lang.Compiler$HostExpr$Parser.parse(Compiler.java:896)
at clojure.lang.Compiler.analyzeSeq(Compiler.java:6055)
... 115 more

Alexander Taggart
added a comment - 08/Feb/11 6:27 PM - edited In working on implementing support for vararg methods, I found a number of flaws with the previous solutions. Please disregard them.
I've attached a single patch (reflector-compiler-numbers.diff) which is a rather substantial overhaul of the Reflector code, with some enhancements to the Compiler and Numbers code.
The patch notes:

Updated patch to fix a bug where a concrete class with multiple identical Methods (e.g., one from an interface, another from an abstract class) would result in ambiguity. Now resolved by arbitrary selection (this is what the original code did as well albeit not explicitly).

Alexander Taggart
added a comment - 10/Feb/11 7:35 PM Updated patch to fix a bug where a concrete class with multiple identical Methods (e.g., one from an interface, another from an abstract class) would result in ambiguity. Now resolved by arbitrary selection (this is what the original code did as well albeit not explicitly).

but only when using compiled code, e.g. the same code works in the REPL and then fails after compilation. Haven't been able to isolate an example that I can share here yet, but hoping this will cause someone to have an "a, hah" moment...

but only when using compiled code, e.g. the same code works in the REPL and then fails after compilation. Haven't been able to isolate an example that I can share here yet, but hoping this will cause someone to have an "a, hah" moment...

The patch now contains only the minimal changes needed to support widening conversion. Cleanup of Numbers overloads, etc., can wait until this patch gets applied. The vararg support is in a separate patch on CLJ-440.

Alexander Taggart
added a comment - 02/Apr/11 12:55 PM The patch now contains only the minimal changes needed to support widening conversion. Cleanup of Numbers overloads, etc., can wait until this patch gets applied. The vararg support is in a separate patch on CLJ-440.

To better facilitate understanding of the changes, I've broken them up into two patches, each with a number of isolable, incremental commits:

reorg-reflector.patch: Moves the reflection/invocation code from Compiler to Reflector, and eliminates redundant code. The result is a single code base for resolving methods/constructors, which will allow for altering that mechanism without excess external coordination. This contains no behaviour changes.

prim-conversion.patch: Depends on the above. Alters the method/constructor resolution process:

more consistent with java resolution, especially when calling pre-1.5 APIs

adds support for widening conversion of primitive numerics of the same category (this is more strict than java, and more clojuresque)

adds support for wildcard matches at compile-time (i.e., you don't need to type-hint every arg to avoid reflection).

Alexander Taggart
added a comment - 28/Apr/11 1:19 PM - edited To better facilitate understanding of the changes, I've broken them up into two patches, each with a number of isolable, incremental commits:
reorg-reflector.patch: Moves the reflection/invocation code from Compiler to Reflector, and eliminates redundant code. The result is a single code base for resolving methods/constructors, which will allow for altering that mechanism without excess external coordination. This contains no behaviour changes.prim-conversion.patch: Depends on the above. Alters the method/constructor resolution process:

more consistent with java resolution, especially when calling pre-1.5 APIs

adds support for widening conversion of primitive numerics of the same category (this is more strict than java, and more clojuresque)

adds support for wildcard matches at compile-time (i.e., you don't need to type-hint every arg to avoid reflection).

Again, not sure if this is any help, but I've tested starting from Clojure head as of Feb 20, 2012, applying clj-792-reorg-reflector-patch2.txt attached to CLJ-792, and then applying clj-445-prim-conversion-update-2-patch.txt attached to this ticket, and the result compiles and passes all but 2 tests. I don't know whether those failures are easy to fix or not, or whether issues may have been introduced with these patches.

Andy Fingerhut
added a comment - 20/Feb/12 2:04 PM Again, not sure if this is any help, but I've tested starting from Clojure head as of Feb 20, 2012, applying clj-792-reorg-reflector-patch2.txt attached to CLJ-792, and then applying clj-445-prim-conversion-update-2-patch.txt attached to this ticket, and the result compiles and passes all but 2 tests. I don't know whether those failures are easy to fix or not, or whether issues may have been introduced with these patches.