Bug 159607 - [1.5][compiler] M2: Unnecessary cast is necessary
Summary: [1.5][compiler] M2: Unnecessary cast is necessary
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.3   Edit
Hardware: PC Windows XP
: P3 normal (vote)
Target Milestone: 3.3 M3   Edit
Assignee: Philipe Mulet CLA
QA Contact:
URL:
Whiteboard:
Keywords:
: 163889 170765 (view as bug list)
Depends on:
Blocks:
 
Reported: 2006-10-03 11:52 EDT by David Saff CLA
Modified: 2007-01-22 04:08 EST (History)
2 users (show)

See Also:


Attachments
Proposed patch (8.44 KB, patch)
2006-10-04 05:25 EDT, Philipe Mulet CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description David Saff CLA 2006-10-03 11:52:22 EDT
In the following code:

class WidgetChildren {
void addChildren(Widget w) {
	if (w instanceof Composite) {
		Composite composite = (Composite) w;
		addAll((Widget[]) composite.getChildren());
	}
}

void addAll(Widget... widgets) {
}
}

Eclipse flags the last line with the warning "Unnecessary cast from Control[] to Widget[]".  Removing the cast, however, results in the warning "The argument of type Control[] should explicitly be cast to Widget[] for the invocation of the varargs method addAll(Widget...) from type WidgetChildren. It could alternatively be cast to Widget for a varargs invocation"

Is it possible to remove one of these warnings?
Comment 1 Philipe Mulet CLA 2006-10-03 12:27:26 EDT
Which build ID are you using ?
Comment 2 David Saff CLA 2006-10-03 12:28:56 EDT
I20060922-0010
Comment 3 Philipe Mulet CLA 2006-10-03 12:41:11 EDT
Reproduced.
class X {
	void addChildren(Widget w) {
		if (w instanceof Composite) {
			Composite composite = (Composite) w;
			addAll((Widget[]) composite.getChildren());
			addAll(composite.getChildren());
		}
	}
	void addAll(Widget... widgets) {
	}
}

class Widget {}
class Control extends Widget {}
class Composite extends Control {
	Control[] getChildren() {
		return null;
	}
}
Comment 4 Philipe Mulet CLA 2006-10-03 12:42:13 EDT
We perform ok in following direct case:

class X {
	void addChildren(Widget w) {
		if (w instanceof Composite) {
			Composite composite = (Composite) w;
			addAll((Control[]) composite.getChildren());
			addAll(composite.getChildren());
		}
	}
	void addAll(Control... widgets) {
	}
}

class Widget {}
class Control extends Widget {}
class Composite extends Control {
	Control[] getChildren() {
		return null;
	}
}
Comment 5 Philipe Mulet CLA 2006-10-03 12:45:22 EDT
I would expect the offending cast to be unnecessary.
Comment 6 Philipe Mulet CLA 2006-10-03 12:47:35 EDT
And when unnecessary cast is removed, no varargs warning should occur either.
Comment 7 Philipe Mulet CLA 2006-10-04 05:25:37 EDT
Created attachment 51397 [details]
Proposed patch
Comment 8 Philipe Mulet CLA 2006-10-04 06:03:00 EDT
Fixed in HEAD
Released for 3.3M3

Added VarargsTest#test052-053
Comment 9 David Audel CLA 2006-10-30 11:56:33 EST
Verified for 3.3 M3 using build I20061030-0010
Comment 10 Philipe Mulet CLA 2006-11-09 04:40:41 EST
*** Bug 163889 has been marked as a duplicate of this bug. ***
Comment 11 Philipe Mulet CLA 2007-01-22 04:08:19 EST
*** Bug 170765 has been marked as a duplicate of this bug. ***