Bug 156016 - Wrong error message for generics
Summary: Wrong error message for generics
Status: RESOLVED INVALID
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.2   Edit
Hardware: Macintosh Mac OS X - Carbon (unsup.)
: P3 minor (vote)
Target Milestone: 3.3 M2   Edit
Assignee: JDT-Core-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2006-09-01 19:29 EDT by Reinier Zwitserloot CLA
Modified: 2006-09-06 04:15 EDT (History)
0 users

See Also:


Attachments
screenshot showing all code and the wrong error message (34.42 KB, image/png)
2006-09-01 19:30 EDT, Reinier Zwitserloot CLA
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Reinier Zwitserloot CLA 2006-09-01 19:29:39 EDT
Following code:

import java.util.Arrays;
import java.util.List;

public class Test {
	public static <T extends Number> List<T> makeNumberList(T a, T b) {
		return Arrays.asList(a, b);
	}
	
	public static void main(String... args) {
		List<Number> name = makeNumberList(5, 5D);
	}
}

produces a (correct) error on the 'makeNumberList' call. However, the message you get as tooltip is wrong. The message is:

Type mismatch: cannot convert from List<Number&Comparable<?>> to  List<Number>

The actual message should be:
Type mismatch: cannot convert from List<? extends Number> to  List<Number>

I'm thoroughly confused by the <Number&Comparable<?>> thing. That doesn't look like legal java to me.

Attached: Screenshot.
Comment 1 Reinier Zwitserloot CLA 2006-09-01 19:30:34 EDT
Created attachment 49284 [details]
screenshot showing all code and the wrong error message
Comment 2 Philipe Mulet CLA 2006-09-05 13:09:49 EDT
This is an intersection type (JLS4.9), computed by the inference of the generic method invocation.

Will investigate if inference correctly produces it in this case.
Comment 3 Philipe Mulet CLA 2006-09-05 13:12:09 EDT
Verified, we produce the right type, following the inference rules described in JLS15.12.2.7.

fyi - javac 1.6b96 agrees with us:
X.java:10: incompatible types
found   : java.util.List<java.lang.Number&java.lang.Comparable<? extends java.lang.Number&java.lang.Comparable<?>>>
required: java.util.List<java.lang.Number>
                List<Number> name = makeNumberList(5, 5D);
Comment 4 Reinier Zwitserloot CLA 2006-09-05 13:49:30 EDT
Reviewed relevant section of language spec. Eclipse indeed produces the correct intersection type. Perhaps this bug can continue as a feature request; the error message (just like javac's own error message) is thoroughly confusing and suggests there is no actual problem. This code can be made to work either by letting makeNumberList return a List<Number> instead of a List<? extends Number>, or by assigning the result to a List<? extends Number> instead of a List<Number>.

In case of overlapping types but mismatched range perhaps the error message can be adjusted to:

Type Mismatch: Cannot convert from List<? extends Number> to List<Number>.

My apologies if such an error message isn't certifiably computable or potentially inappropriate. Generics can be quite confusing :-P


NB: Verified in eclipse 3.2 that suggested fixes to the sample code indeed make the error go away.
NB2: A quick fix is offered to translate the assignment type from List<Number> to List<? extends Number> (Eclipse3.2)- these are the types the error message could mention.
Comment 5 Philipe Mulet CLA 2006-09-06 04:13:54 EDT
The compiler must present the exact types involved. If not, it could add to confusion even more. But since no intersection type can be explicitly defined, I agree it doesn't help much, this is why our quickfix produces a wildcard instead. Note that the wildcard is not as good since it drops Comparable<?> capabilities.

Unless the spec was revised, there is little we can change in the compiler. Maybe provide some better explanation in the error message, suggesting to use a wildcard instead... might end up only making longer messages, still cryptic...
Comment 6 Philipe Mulet CLA 2006-09-06 04:15:18 EDT
Closing this defect, since we behave as expected.
Added GenericTypeTest#test1029