JLS Technical Amguity

Alex Buckley alex.buckley at oracle.com
Mon Aug 17 23:08:29 UTC 2020


As you say, a type parameter in a class declaration is ultimately a 
TypeIdentifier, just like the name specified for the class itself. A 
TypeIdentifier cannot be `var` or `yield`. On JDK 14, javac correctly 
rejects `class A<var> {}` and `class B<yield> {}`:

|  Welcome to JShell -- Version 14
|  For an introduction type: /help intro

jshell> class A<var> {}
|  Error:
|  'var' not allowed here
|    as of release 10, 'var' is a restricted type name and cannot be 
used for type declarations
|  class A<var> {}
|          ^

jshell> class B<yield> {}
|  Error:
|  'yield' not allowed here
|    as of release 13, 'yield' is a restricted type name and cannot be 
used for type declarations
|  class B<yield> {}
|          ^

Alex

On 6/18/2020 5:38 PM, Sam Hooper wrote:
> Hi,
> There appears to be a technical ambiguity in the JLS 14. The restricted
> identifier yield is allowed as the name of a type variable when, according
> to the specification, it should not be. The following compiles fine:
> class Outer<yield>{
> 
> class Inner<yield>{
> 
> <yield> void method() {}
> 
> }
> 
> }
> 
> which it should not according to the following productions from JLS 14:
> 
> TypeParameters:
> < TypeParameterList
> <https://docs.oracle.com/javase/specs/jls/se14/html/jls-8.html#jls-TypeParameterList>
>   >
> TypeParameterList:
> TypeParameter
> <https://docs.oracle.com/javase/specs/jls/se14/html/jls-4.html#jls-TypeParameter>
>   {, TypeParameter
> <https://docs.oracle.com/javase/specs/jls/se14/html/jls-4.html#jls-TypeParameter>
> }
> TypeParameter:
> {TypeParameterModifier
> <https://docs.oracle.com/javase/specs/jls/se14/html/jls-4.html#jls-TypeParameterModifier>
> } TypeIdentifier
> <https://docs.oracle.com/javase/specs/jls/se14/html/jls-3.html#jls-TypeIdentifier>
>   [TypeBound
> <https://docs.oracle.com/javase/specs/jls/se14/html/jls-4.html#jls-TypeBound>
> ]
> TypeIdentifier:
> Identifier
> <https://docs.oracle.com/javase/specs/jls/se14/html/jls-3.html#jls-Identifier>
>   but not var or yield
> 
> Thanks for your time.
> 
> -Sam Hooper
> 


More information about the jls-jvms-spec-comments mailing list