RFR: universal type variables: initial prototype

Vicente Romero vromero at openjdk.java.net
Wed Aug 4 15:49:46 UTC 2021

On Wed, 4 Aug 2021 10:07:42 GMT, Maurizio Cimadamore <mcimadamore at openjdk.org> wrote:

>> First iteration of the protype for universal type variables
> src/java.base/share/classes/jdk/internal/javac/PreviewFeature.java line 64:
>> 62:     public enum Feature {
>> 64:         UNIVERSAL_TVARS,
> As Dan noted yesterday, for other Valhalla features we do not generally use preview mechanisms (this code needs to be pushed in the valhalla repo). We can postpone the preview dance for later. In fact, to ease experiments in JDK code it would be better to just make universal type variable syntax available by default.
> There are some other places where this UNIVERSAL_TVARS constant is used which should be cleaned up for now (e.g. Preview.java).

then I guess we will need to update the text in the JEP as the it is explicitly saying that this one is a preview feature

> src/jdk.compiler/share/classes/com/sun/tools/javac/code/Type.java line 1946:
>> 1944:         public Type lower;
>> 1945: 
>> 1946:         public boolean universal = false;
> Can we use a flag on the tsym for this? After all, being "universal" is a declaration (symbol) not a use (type) property?

well yes and no, if we have:

class C<__universal T> {
    T.ref t;

T and T.ref share the same symbol but they have different types that's why the universal field has been set to the type

> src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java line 825:
>> 823:     }
>> 824: 
>> 825:     List<Type> attribTypes(List<JCExpression> trees, Env<AttrContext> env, Predicate<JCExpression> valueOK) {
> Do you really need a predicate here? Wouldn't a boolean be ok? Or perhaps a flag in env, or some other means?

the thing is that one value only doesn't apply to all cases, what if some type variables are universal but others aren't?

> src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Attr.java line 5160:
>> 5158: 
>> 5159:         // Attribute type parameters
>> 5160:         List<Type> actuals = !allowUniversalTVars ?
> If you are doing this, you might be better off just attributing types one at a time inside the loop above, and avoid using a set to collect the ones which need special treatment?

I will try that out

> src/jdk.compiler/share/classes/com/sun/tools/javac/comp/Check.java line 707:
>> 705:          } else if (!a.hasTag(WILDCARD)) {
>> 706:              a = types.cvarUpperBound(a);
>> 707:              return types.isBoundedBy(a, bound, (t, s, w) -> types.isSubtype(t, s));
> This is a good change - I'm a bit scared about the other calls in this routine, to `isCastable` and `notSoftSubtype`. These type relations are mostly doing things outside the spec, and we probably need to sprinkle `isBounded` in there as well.



PR: https://git.openjdk.java.net/valhalla/pull/521

More information about the valhalla-dev mailing list