Bug 263215 - [1.5][compiler] I20090129-1200 generates suddenly an generics compile error that 3.5M4 didnt do
Summary: [1.5][compiler] I20090129-1200 generates suddenly an generics compile error ...
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.5   Edit
Hardware: PC Windows NT
: P3 normal (vote)
Target Milestone: 3.5 M6   Edit
Assignee: Philipe Mulet CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2009-02-02 05:18 EST by Johan Compagner CLA
Modified: 2011-01-25 11:09 EST (History)
2 users (show)

See Also:


Attachments
Proposed patch (17.79 KB, patch)
2009-02-03 04:33 EST, 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 Johan Compagner CLA 2009-02-02 05:18:58 EST
i have this class:

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class Test
{
	public static <T> T[] asArray(Iterator< ? extends T> it, Class<T> clazz)
	{
		List<T> lst = new ArrayList<T>();
		while (it.hasNext())
		{
			lst.add(it.next());
		}
		return lst.toArray((T[])java.lang.reflect.Array.newInstance(clazz, lst.size()));
	}

	public void test()
	{
		String[] asString = null;
		// eclipse 3.5M4 this worked in build  I20090129-1200 it doesnt anymore
		asString = Test.<String> asArray(getIterator(), String.class);

		// now i have to do this:
		Iterator<String> iterator = getIterator();
		asString = Test.<String> asArray(iterator, String.class);

		// this also works except if i have remove unnecessary cast enabled then the cast is removed and i get a compile error
		asString = Test.<String> asArray((Iterator<String>)getIterator(), String.class);
	}

	public Iterator getIterator()
	{
		return new Iterator()
		{
			public void remove(){}

			public Object next(){
				return null;
			}

			public boolean hasNext(){
				return false;
			}
		};
	}
}

the error is:
Type mismatch: cannot convert from Object[] to String[]	


I think the error now isnt right. It should see that it will return a string because it is type with Test.<String> it shouldnt look what goes into the method as arguments (that is just a warning that the iterator isnt generified)

also if this error was right. Then the cast shouldnt be removed by the unnecessary cast save action. Because it is needed.
Comment 1 Philipe Mulet CLA 2009-02-03 04:33:52 EST
Created attachment 124519 [details]
Proposed patch
Comment 2 Philipe Mulet CLA 2009-02-03 04:39:58 EST
This issue got introduced while fixing bug 258798.
For explicitly parameterized invocations, we should not apply 15.12.2.6 after unchecked conversions occurred in argument types.

Note that the true 15.12.2.6. mandates to do it, even if non generic invocation. This is being debated with the spec master; and until resolved, I agree that matching javac behavior is the best for now.

Added GenericTypeTest#test1444-1445.

Also note that javac is not producing the proper diagnostics for thrown exceptions (failing to substitute T with IOException) e.g.:

import java.io.IOException;
import java.util.List;
public class X {
	<T extends Throwable> X(List<T> lt) throws T { }
	<T extends Throwable> List<T> foo(List<T> t) throws T { return t; }

	static void bar(List l) {
		new X(l).foo(l);
	}
	static void baz(List l) throws IOException {
		new <IOException> X(l). <IOException> foo(l);
	}
	
	X(List l, long l2) throws IOException {
		<IOException> this(l);
	}

	static void baz2(List l) throws IOException {
		new <IOException> X(l){}. <IOException> foo(l);
	}
}
Comment 3 Philipe Mulet CLA 2009-02-03 04:40:17 EST
Released for 3.5M6
Comment 4 Olivier Thomann CLA 2011-01-25 11:09:27 EST
Verified.