[type-annos-observers] Should JSR308 forbid type annotations on uses of a type parameter ?

Werner Dietl wdietl at gmail.com
Thu Sep 12 02:27:16 PDT 2013


Hi Srikanth,

For the use in type systems, having the distinction between type parameter
declarations and type variable uses is necessary.

The Checker Framework manual dedicates a section discussing this
interaction:
http://types.cs.washington.edu/checker-framework/current/checkers-manual.html#generics

In brief,  one can imagine:

class C<@TA T extends @TB Upper> {
    T field1;
    @TC T field2;
}

Note that @TA can have @Target meta-annotation TYPE_PARAMETER - which is
subsumed in the more general TYPE_USE.

In the Checker Framework,  we interpret @TA as an annotation on the
implicit lower bound of type variable T.
@TB is an annotation on the upper bound of T.

Fields field1 and field2 have different types.
The type of field1 is the normal use of the type variable and behaves like
a type variable in Java.
The type of field2 gives a more specific type annotation,  @TC, that should
override any annotations from T.

As a concrete example,  let's consider a Nullness type system with
annotations @Nullable and @NonNull, where a @NonNull reference is a subtype
of a @Nullable reference.

We can imagine a class:

class Box<@NonNull T extends @Nullable Object> {
  T f1;
  @Nullable T f2;

  void foo() {
    f1 = null; // error
    f2 = null; // valid
  }
}

The lower bound of T is @NonNull and the upper bound is @Nullable - any
instantiation is allowed.

The first assignment must be forbidden: the concrete instantiation of Box
might be "Box<@NonNull String>" ; allowing the assignment of the null value
into field f1 would be unsound.
However,  the type of field f2 declares that null is always a valid value
and therefore the assignment to f2 is valid.

This is the interpretation that we are using in the Checker Framework.
However,  making the distinction between type parameter declarations and
type variable uses is a general one and allowing different type annotations
on them seems generally useful.

Does this explanation help?
cu, WMD.
On Sep 11, 2013 10:39 PM, "Srikanth S Adayapalam" <
srikanth_sankaran at in.ibm.com> wrote:

> Hello !
>
> I am trying to understand if there are valid scenarios where type
> annotating the use of a type
> variable actually makes sense. The following program compiles fine with
> both eclipse and
> javac 8b100:
>
> import java.lang.annotation.ElementType;
> import java.lang.annotation.Target;
>
> @Target(ElementType.TYPE_USE)
> @interface TA {
> }
> @Target(ElementType.TYPE_USE)
> @interface TB {
> }
>
> public abstract class X<@TA T> {
>
>                  @TB T t;
> }
>
> Studying this test case, since both occurrence of T would denote the
> same type instantiation, does it make sense to think of "only @TA
> annotated T" and "only @TB annotated T" ?
>
> Shouldn't the language insist that they be consolidated and presented all
> at the declaration site ?
>
> Have I overlooked any valid scenarios where this would actually make sense
> ?
>
> Thanks!
> Srikanth
>


More information about the type-annotations-spec-observers mailing list