Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 472420

Summary: [1.8][null] Bogus error/warning when DefaultLocation.TYPE_PARAMETER is added to NonNullByDefault
Product: [Eclipse Project] JDT Reporter: Clovis Seragiotto <clovis.seragiotto>
Component: CoreAssignee: Till Brychcy <register.eclipse>
Status: CLOSED DUPLICATE QA Contact:
Severity: normal    
Priority: P3 CC: register.eclipse, stephan.herrmann
Version: 4.5   
Target Milestone: 4.7 M4   
Hardware: PC   
OS: Windows 7   
See Also: https://bugs.eclipse.org/bugs/show_bug.cgi?id=501564
Whiteboard:

Description Clovis Seragiotto CLA 2015-07-11 08:35:09 EDT
This class compiles without errors or warnings...

@NonNullByDefault
public interface Foo<@NonNull A> { // #
    static <@NonNull B> Foo<B> newInstance(B b) { // *
        throw new UnsupportedOperationException("for " + b);
    }
}

... while for the following class...

/**
 * @param <A> Comment
 */
@NonNullByDefault({PARAMETER, RETURN_TYPE, FIELD, TYPE_BOUND, TYPE_ARGUMENT, TYPE_PARAMETER})
public interface Foo<A> {
    static <B> Foo<B> newInstance(B b) {
        throw new UnsupportedOperationException("for " + b);
    }
}

... the compiler complains:
1) The type 'B' is not a valid substitute for the type parameter '@NonNull A' (line *)
2) Unused type parameter A (line #)

The quick fixes for 2 are also invalid:
- remove unused type parameter
- document type parameter to avoid 'unused' warning
Comment 1 Stephan Herrmann CLA 2015-07-18 06:46:48 EDT
reproduced, thanks.
Comment 2 Stephan Herrmann CLA 2016-03-25 10:30:56 EDT
Too much on my plate for 4.6. Bulk deferral to 4.7
Comment 3 Clovis Seragiotto CLA 2016-09-16 04:54:45 EDT
Another simple test case:

import java.util.EnumMap;

import org.eclipse.jdt.annotation.*;

@NonNullByDefault(DefaultLocation.TYPE_PARAMETER)
public class Foo<E extends Enum<E>> {
    Foo(Class<E> c) {
        new EnumMap<>(c);
    }
}

Error (only if null analysis is on): Cannot infer type arguments for EnumMap<>
Comment 4 Clovis Seragiotto CLA 2016-09-16 05:03:37 EDT
… and also:

import java.util.function.Function;
import org.eclipse.jdt.annotation.*;

@NonNullByDefault(DefaultLocation.TYPE_PARAMETER)
public class Foo<E extends Enum<E>> {
    Foo(Class<E> c) {
        Function<E, String> f = Enum::name; // unexpected error: The type Enum does not define name(E) that is applicable here
        Function<E, String> g = (E e) -> e.name(); // unexpected error: The method name() is undefined for the type E
    }
}
Comment 5 Till Brychcy CLA 2016-12-16 14:15:01 EST
The "main" bug from comment #0 has been fixed by the fix for bug 501031, which is included in 4.7M3

The examples from comment #3 and comment #4 are different, but they also have been already been fixed by the fix for bug 501564, included in 4.7M4

*** This bug has been marked as a duplicate of bug 501031 ***