Bug 154267 - [1.5][compiler] Inferred type less restrictive than declared does not give error
Summary: [1.5][compiler] Inferred type less restrictive than declared does not give error
Status: RESOLVED INVALID
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.2   Edit
Hardware: PC Windows XP
: P3 normal (vote)
Target Milestone: 3.3 RC1   Edit
Assignee: Philipe Mulet CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2006-08-17 13:54 EDT by Philip Smith CLA
Modified: 2007-05-07 11:55 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 Philip Smith CLA 2006-08-17 13:54:25 EDT
Code is allowed which is rejected by javac. A method with a bounded generic type argument is assigned to a result with an unbounded generic type. The inferred type for the generic type argument should be unbounded resulting in a compile error attempting to call the method.

import java.awt.Container;
import java.util.Collection;


public class Demo
{
    private <T extends Container> Collection<T> foo()
    {
        return null;
    }

    private void showProblem()
    {
        Collection<?> result = foo();
    }
}

/* Fails when compiled in javac:

Demo.java:14: incompatible types; inferred type argument(s) ? do not conform to
bounds of type variable(s) T
found   : <T>java.util.Collection<T>
required: java.util.Collection<?>
        Collection<?> result = foo();
                                  ^
1 error

*/
Comment 1 Philipe Mulet CLA 2007-05-07 11:01:55 EDT
This is a bug in Javac, it fails to infer that under-constrained T should be 'Container'. Note that in a little variable, it does fine.

import java.awt.Container;
import java.util.Collection;

public class X {
	private <T extends Container> Collection<T> foo() {
		return null;
	}

	private void showProblem() {
		Collection<?> result = (Collection<Container>) foo();
	}
}
Comment 2 Philipe Mulet CLA 2007-05-07 11:26:51 EDT
Previous comment should have said:
"... Note that with a small variation, it does fine."
Comment 3 Philipe Mulet CLA 2007-05-07 11:37:23 EDT
Note: the variation with extra cast is not strictly equivalent, as with the cast it does NOT consider the expected type (Collection<?>) into the inference stage (pass2).
Technically though, the expected type doesn't bring any useful information for inference, and should just end up with the same final result, i.e. T is underconstrained and should default to be Container.
Comment 4 Philipe Mulet CLA 2007-05-07 11:55:16 EDT
Inference from expected type with unbound wildcard (Collection<?>) is described in JLS 15.12.7, p.459.

Added GenericTypeTest#test1137.
Marking as invalid.