Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 386259 - Cast on MethodHandle#invoke(..) wrongly marked as unnecessary
Summary: Cast on MethodHandle#invoke(..) wrongly marked as unnecessary
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.7.1   Edit
Hardware: All All
: P3 major (vote)
Target Milestone: 3.8.1   Edit
Assignee: Srikanth Sankaran CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2012-07-30 13:41 EDT by Anders Hedström CLA
Modified: 2012-08-14 10:28 EDT (History)
6 users (show)

See Also:
jarthana: review+


Attachments
eclipse preferences dialog (69.26 KB, image/png)
2012-07-31 07:41 EDT, Anders Hedström CLA
no flags Details
eclipse editor before save (36.88 KB, image/png)
2012-07-31 07:42 EDT, Anders Hedström CLA
no flags Details
eclipse editor after save (37.56 KB, image/png)
2012-07-31 07:42 EDT, Anders Hedström CLA
no flags Details
Patch under test (5.19 KB, patch)
2012-08-08 03:47 EDT, Srikanth Sankaran CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Anders Hedström CLA 2012-07-30 13:41:15 EDT
Code example
------------

* before save:

MethodHandle mh = lookup.findVirtual(instance.getClass(), "toString", MethodType.methodType(String.class));

String actual = (String) mh.invoke(instance);

* after save:

MethodHandle mh = lookup.findVirtual(instance.getClass(), "toString", MethodType.methodType(String.class));

String actual = mh.invoke(instance);


This will cause a compilation error, mh.invoke returns an java.lang.Object and must be casted to String. When Save Action "remove unnecessary casts" is enabled the cast to String is removed even though the cast is necessary.
Comment 1 Dani Megert CLA 2012-07-31 07:16:26 EDT
I cannot reproduce the problem using R4.2. If you can still reproduce this, then please attach code that allows us to reproduce the problem.
Comment 2 Anders Hedström CLA 2012-07-31 07:41:24 EDT
Created attachment 219359 [details]
eclipse preferences dialog
Comment 3 Anders Hedström CLA 2012-07-31 07:42:28 EDT
Created attachment 219360 [details]
eclipse editor before save
Comment 4 Anders Hedström CLA 2012-07-31 07:42:56 EDT
Created attachment 219361 [details]
eclipse editor after save
Comment 5 Anders Hedström CLA 2012-07-31 07:43:30 EDT
(In reply to comment #1)
> I cannot reproduce the problem using R4.2. If you can still reproduce this,
> then please attach code that allows us to reproduce the problem.

I'm using OS X 10.8 and Eclipse Juno, Build id: 20120614-1722 and when I have "remove unnecessary casts" enabled for my save actions, the cast in the code below is removed when I save the file

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;

public class EclipseBug {
  public static void main(String[] args) throws Throwable {
    String str = "test";
    MethodHandle mh = MethodHandles.lookup().findVirtual(String.class, "toString", 
        MethodType.methodType(String.class));
    String actual = (String) mh.invoke(str);
    assert "test".equals(actual);
  }
}

Which in turn results in a compilation failure: "Type mismatch: cannot convert from Object to String"

I've attached a couple of screenshots.
Comment 6 Dani Megert CLA 2012-07-31 07:58:31 EDT
Thanks for the source example. I can reproduce it now.
This got broken in 3.7.1.
Comment 7 Dani Megert CLA 2012-07-31 07:59:17 EDT
JDT Core does not report the cast to be unnecessary.
Comment 8 Markus Keller CLA 2012-07-31 10:49:47 EDT
(In reply to comment #7)
> JDT Core does not report the cast to be unnecessary.

That's wrong. The option is just set to "ignore" by default.

The bug is that the compiler does report the cast as unnecessary, although it is not unnecessary in this specific example, since MethodHandle#invoke(..) is an @PolymorphicSignature method.

I think the fix can be implemented quite locally in CastExpression#resolveType(BlockScope) where there is already code that explicitly deals with polymorphic signature methods.

> This got broken in 3.7.1.
That's because Java7 support was only added there.
Comment 9 Dani Megert CLA 2012-07-31 11:09:01 EDT
(In reply to comment #8)
> (In reply to comment #7)
> > JDT Core does not report the cast to be unnecessary.
> 
> That's wrong. The option is just set to "ignore" by default.

I too used to my own preferences. Sorry about that.
Comment 10 Srikanth Sankaran CLA 2012-08-01 03:48:01 EDT
Tentatively target 3.8.1, May need resetting after analysis.
Comment 11 Srikanth Sankaran CLA 2012-08-08 03:47:49 EDT
Created attachment 219659 [details]
Patch under test
Comment 12 Srikanth Sankaran CLA 2012-08-08 03:48:59 EDT
Jay, thanks for a quick review of this patch.
Comment 14 Jay Arthanareeswaran CLA 2012-08-08 07:49:20 EDT
Patch looks good to me.
Comment 15 Srikanth Sankaran CLA 2012-08-08 08:30:06 EDT
Reran the tests on master and all are green.
Comment 16 Jay Arthanareeswaran CLA 2012-08-09 01:17:01 EDT
Verified for 4.3 M1 with build I20120808-2000
Comment 17 Stephan Herrmann CLA 2012-08-14 10:28:53 EDT
Verified for 3.8.1 with build M20120809-1200.