Another possible generics bug

Dan Smith daniel.smith at oracle.com
Tue Dec 6 18:57:50 PST 2011


On Dec 5, 2011, at 8:36 PM, Richard Warburton wrote:

> From http://java.sun.com/docs/books/jls/third_edition/html/names.html#6.3.1:
> 
> "A declaration d of a type named n shadows the declarations of any other types named n that are in scope at the point where d occurs throughout the scope of d."

There's also this:

"The scope of a declaration of a member m declared in or inherited by a class type C (§8.1.6) is the entire body of C, including any nested type declarations."

And this:

"The scope of a class's type parameter (§8.1.2) is the type parameter section of the class declaration, the type parameter section of any superclass or superinterface of the class declaration, and the class body."

To interpret the rules, we need to ask two questions:

1) Does the nested class T shadow the type parameter T?  Yes.  The declaration of the type parameter is clearly in scope at the point of the declaration of the class, and so the class shadows the type parameter.

2) Does the type parameter T shadow the nested class T?  No.  The declaration of the nested class is NOT in scope at the point of the type parameter declaration.  The nested class is in scope throughout "the entire body of C", but it's clear from the rule about that type parameter's scope that "the type parameter section of the class declaration" is separate from "the class body" -- otherwise, mentioning them both would be redundant.

It would be a Bad Thing if the answer to both questions were "yes," but fortunately, this is not the case.  The JLS could be clearer (specifically excluding the type parameters/extends clause from the scope of the nested class), but it seems like it says enough to convey the proper meaning.

So, yes, this is a compiler bug, and Eclipse seems to be behaving correctly.

—Dan


More information about the compiler-dev mailing list