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

0 votes
in Clojure by
closed by

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)

The above occurs because there is no Integer(byte) constructor, though it should match on Integer(int).

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).
closed with the note: Fixed as a side effect of CLJ-2843 in 1.11.3 and 1.12.0-alpha10

26 Answers

0 votes
by

Comment made by: importer

ataggart said: (link: [file:b6gDSUZOur36b9eJe5cbCb)]

0 votes
by

Comment made by: importer

ataggart said: Also fixes #446.

0 votes
by
0 votes
by

Comment made by: stu

The patch is causing a test failure

`

 [java] Exception in thread "main" java.lang.IllegalArgumentException: 
 More than one matching method found: equiv, compiling:(clojure/pprint/cl_format.clj:428)

`

Can you take a look?

0 votes
by

Comment made by: stu

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

`

0 votes
by

Comment made by: ataggart

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:

  • Moved reflection functionality from Compiler to Reflector.
  • Reflector supports finding overloaded methods by widening conversion, boxing conversion, and casting.
  • During compilation Reflector attempts to find best wildcard match.
  • Reflector refers to * when reflectively invoking methods and constructors.
  • Both Reflector and Compiler support variable arity java methods and constructor; backwards compatible with passing an array or nil in the vararg slot.
  • Added more informative error messages to Reflector.
  • Added tests to clojure.test-clojure.reflector.
  • Altered overloaded functions of clojure.lang.Numbers to service Object/double/long params; fixes some ambiguity issues and avoids unnecessary boxing in some cases.
  • Patch closes issues 380, 440, 445, 666, and possibly 259 (not enough detail provided).
0 votes
by

Comment made by: ataggart

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).

0 votes
by

Comment made by: ataggart

Updated patch to work with latest master branch.

0 votes
by

Comment made by: stu

patch appears to be missing test file clojure/test_clojure/reflector.clj.

0 votes
by

Comment made by: ataggart

Bit by git.

Patch corrected to contain clojure.test-clojure.reflector.

0 votes
by

Comment made by: stu

Rich: I verified that the patch applied but reviewed only briefly, knowing you will want to go over this one closely.

0 votes
by

Comment made by: stu

After applying this patch, I am getting method missing errors:

java.lang.NoSuchMethodError: clojure.lang.Numbers.lt(JLjava/lang/Object;)

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...

0 votes
by

Comment made by: ataggart

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.

0 votes
by

Comment made by: redinger

Please test patch

0 votes
by

Comment made by: redinger

FYI: Patch applies cleanly on master and all tests pass as of 4/21 (2011)

...