Bug 159607

Summary: [1.5][compiler] M2: Unnecessary cast is necessary
Product: [Eclipse Project] JDT Reporter: David Saff <david>
Component: CoreAssignee: Philipe Mulet <philippe_mulet>
Status: VERIFIED FIXED QA Contact:
Severity: normal    
Priority: P3 CC: jcsahnwaldt, zorzella
Version: 3.3   
Target Milestone: 3.3 M3   
Hardware: PC   
OS: Windows XP   
Whiteboard:
Attachments:
Description Flags
Proposed patch none

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