Bug 464352 - [1.8][compiler] "Unnecessary cast" problems in lambda expressions for necessary casts
Summary: [1.8][compiler] "Unnecessary cast" problems in lambda expressions for necessa...
Status: NEW
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 4.5   Edit
Hardware: PC Windows 7
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: JDT-Core-Inbox CLA
QA Contact:
URL:
Whiteboard: stalebug
Keywords:
: 466341 (view as bug list)
Depends on:
Blocks:
 
Reported: 2015-04-10 01:37 EDT by Sasikanth Bharadwaj CLA
Modified: 2023-10-18 17:16 EDT (History)
6 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Sasikanth Bharadwaj CLA 2015-04-10 01:37:45 EDT
The following code results in an "Unnecessary Cast" being reported although removing the cast creates a compile error

//---
import java.util.List;
import java.util.function.Function;

public class Test {
	<T extends Number> List<T> apply (Function<T, T> func) {
		return null;
	}
	<E> E identity(E a) {
		return a;
	}	
	void test() {
		Integer i = apply (x -> identity((Integer)x)).get(0);
	}
}
//---

Created from bug 461706 comment 6.
Comment 1 Sasikanth Bharadwaj CLA 2015-04-10 01:39:48 EDT
Pasting relevant portions of the comment here
> We still report as unnecessary (it is a cast Integer -> Integer!), but removing
> the cast creates a compile error!!
> 
> A fully precise analysis seems to require to rerun type inference with the cast
> removed, because the cast may have been necessary to create the situation where
> it is deemed unnecessary.

> One solution would be to make a weaker warning "cast may be unnecessary", so
> that UI can indeed trigger the rerun of inference to find out if it is really
> unnecessary. The weaker form would be reported for any type that was "touched"
> by type inference (not sure we can easily detect his, yet, but that's a
> technical problem not a conceptual one).
> 
> Or, UI could refuse to remove any casts inside a lambda, when running in cleanup
> mode.
>
Comment 2 Stephan Herrmann CLA 2015-05-05 08:57:52 EDT
*** Bug 466341 has been marked as a duplicate of this bug. ***
Comment 3 Stephan Herrmann CLA 2015-05-05 08:58:56 EDT
The cluster "unnecessary cast" calls for some action in 4.6.
Comment 4 Sasikanth Bharadwaj CLA 2016-04-04 04:21:02 EDT
Didn't get to it during 4.6, will take this up on priority for 4.7
Comment 5 Sasikanth Bharadwaj CLA 2017-05-17 05:37:06 EDT
Moving out of 4.7
Comment 6 Manoj N Palat CLA 2018-08-16 00:30:19 EDT
Bulk move out of 4.9
Comment 7 Mat Gessel CLA 2019-11-04 23:42:58 EST
Still happens in 2019-09 R (4.13.0) • JDT 3.18.100.v20190916-1045 • tried ecj 1.8, 12.
Comment 8 Jay Arthanareeswaran CLA 2019-11-05 01:16:23 EST
Sasi is no longer with the team. So, this one is up for grabs if anyone is interested.
Comment 9 Eclipse Genie CLA 2021-10-27 11:44:38 EDT
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet.

If you have further information on the current state of the bug, please add it. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.

--
The automated Eclipse Genie.
Comment 10 Eclipse Genie CLA 2023-10-18 17:16:29 EDT
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet.

If you have further information on the current state of the bug, please add it. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.

--
The automated Eclipse Genie.