Bug 109098 - [1.5][compiler] Should insert generic cast in presence of multi-bounded types
Summary: [1.5][compiler] Should insert generic cast in presence of multi-bounded types
Status: RESOLVED WONTFIX
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.1   Edit
Hardware: PC Windows XP
: P3 normal (vote)
Target Milestone: 3.2 M2   Edit
Assignee: Philipe Mulet CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2005-09-08 16:05 EDT by Philipe Mulet CLA
Modified: 2005-09-27 04:35 EDT (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Philipe Mulet CLA 2005-09-08 16:05:13 EDT
Build 3.1.0

The spec says that generics require cast insertion in exactly 2 situations (see
discussion in 5.2 Assignment conversion, p. 95).
However, on following example, it seems a cast to Runnable needs to be inserted,
as does javac.

import java.io.Serializable;
public class X<T extends Serializable & Runnable> {
	void foo(T t) {
		t.run();
	}
}
Comment 1 Philipe Mulet CLA 2005-09-08 16:05:47 EDT
Actually, any expression yielding a type variable as its type (possibly
resulting from capture as well) is to be checked before used, in case some type
assumptions would need to be enforced.
e.g.
import java.io.Serializable;

public class X<T extends Serializable & Runnable, V extends T> {
	void foo(T t) {
		Runnable r1 = t;
		bar(t);
	}
	void bar(Runnable r) {}	
	public static void main(String[] args) {
		new X<A, A>().foo(new A());
	}
}
class A implements Serializable, Runnable {
	public void run() {
		System.out.println("AA");
	}
}
Comment 2 Philipe Mulet CLA 2005-09-08 16:06:43 EDT
Affected types are: type variables with multipe bounds (directly or indirectly),
captures and intersection types.
Comment 3 Philipe Mulet CLA 2005-09-09 13:26:47 EDT
Actually, maybe casts are not mandated. Bytecode verifiers seem to defer the
check until runtime (similar to standard situation arising from binary
compatibility problems).

So even though the bytecode looks troublesome, at runtime the proper object
types are available and no cast is required.
Comment 4 Philipe Mulet CLA 2005-09-09 13:27:37 EDT
Added GenericTypeTest#test821-test825
Comment 5 Philipe Mulet CLA 2005-09-27 04:35:24 EDT
Closing