Question on remapping type annotations when casts are discarded by the compiler

Alex Buckley alex.buckley at oracle.com
Wed Jul 10 16:45:20 PDT 2013


Hi Andy,

Sun/Oracle tries very hard to avoid rigid requirements about what a Java 
compiler must emit. Attributes in the JVM Specification are defined in 
terms of what they represent, not the conditions under which they 
represent it. There are some exceptions involving declarations (e.g. the 
Signature attribute has a non-trivial "must occur when..." clause) but 
annotations on an expression do not rise to the level of being mandated. 
There is no useful conformance test that JCK-VM engineers can write, and 
as you point out there are difficult corner cases. The text about what a 
compiler is required to do will not appear in JLS8 or JVMS8.

Alex

On 7/10/2013 2:04 PM, Andy Clement wrote:
> I'm in one of the corners of the JSR308 spec, Section 3.3.10, which reads:
>
> ===
>
> If the compiler eliminates an annotated cast, it is required to retain the
> annotations on the cast in the class file (if the annotation type has at
> least RetentionPolicy.CLASS retention). When a cast is removed, the
> compiler may need to adjust (the locations of) the annotations, to account
> for the relationship between the expression’s type and the casted-to type.
> Consider:
>
>    class C<S, T> { ... }
>    class D<A, B> extends C<B, A> { ... }
>    ...
>    ... (C<@A1 X, @A2 Y>) myD ...
>
> The compiler may leave out the upcast, but in that case it must record that
> @A1 is attached to the second type argument of D, even though it was
> originally attached to the first type argument of C.
>
> ===
> I have this (hopefully sane?) set of declarations:
>
> class C<X,Y,Z> {}
>
> class D<A,B> extends C<B,String,A> {}
>
> D<Integer,Float> d = new D<Integer,Float>();
>
> C<Float,String,Integer> o2 = (C<@Anno1 Float, at Anno2 String, @Anno3
> Integer>) d;
>
> If the cast were not discarded, the type anno attributes would look like a
> bit like:
>
> @Anno1 TYPE_ARGUMENT(0)
> @Anno2 TYPE_ARGUMENT(1)
> @Anno3 TYPE_ARGUMENT(2)
>
> But the cast is discarded so I need to remap the annotation positions based
> on the spec section above, mapping them to where they exist on D.
>
> @Anno1 becomes TYPE_ARGUMENT(1)
> @Anno3 becomes TYPE_ARGUMENT(0)
>
> What happens to @Anno2?
>
> thanks
> Andy Clement
>


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