Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

universal type variables: initial prototype #521

Closed
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -412,6 +412,11 @@ public static EnumSet<Flag> asFlagSet(long flags) {
*/
public static final long NON_SEALED = 1L<<63; // ClassSymbols

/**
* Flag to indicate that the type variables is universal
*/
public static final long UNIVERSAL = 1L<<61; // TypeVariableSymbols

// Encodings for extended flags stored using attributes
/**
* Flag to indicate that the primitive class is reference default.
@@ -324,7 +324,12 @@ public enum LintCategory {
/**
* Warn about use of preview features.
*/
PREVIEW("preview");
PREVIEW("preview"),

/**
* Warn about use of universal type variables.
*/
UNIVERSAL("universal");

LintCategory(String option) {
this(option, false);
@@ -234,6 +234,7 @@ public enum Feature {
CASE_NULL(JDK17, Fragments.FeatureCaseNull, DiagKind.NORMAL),
PATTERN_SWITCH(JDK17, Fragments.FeaturePatternSwitch, DiagKind.PLURAL),
REDUNDANT_STRICTFP(JDK17),
UNIVERSAL_TVARS(JDK18, Fragments.FeatureUniversalTvars, DiagKind.PLURAL),
;

enum DiagKind {
@@ -1943,12 +1943,32 @@ public static class TypeVar extends Type implements TypeVariable {
*/
public Type lower;

public TVFlavor flavor;

/** if this type variable is universal then it will also have a link to a pure reference
* type variable, it is important to know that a universal type variable and its
* corresponding referenceTypeVar share the same tsym. So if it is needed to double check if
* a type variable is universal or not, we need to check its type not the type of its tsym
*/
public TypeVar projection = null;

public enum TVFlavor {
REFERENCE,
UNIVERSAL,
REFERENCE_FROM_UNIVERSAL
}

public TypeVar(Name name, Symbol owner, Type lower) {
this(name, owner, lower, TVFlavor.REFERENCE);
}

public TypeVar(Name name, Symbol owner, Type lower, TVFlavor flavor) {
super(null, TypeMetadata.EMPTY);
Assert.checkNonNull(lower);
tsym = new TypeVariableSymbol(0, name, this, owner);
tsym = new TypeVariableSymbol(flavor == TVFlavor.UNIVERSAL ? UNIVERSAL : 0, name, this, owner);
this.setUpperBound(null);
this.lower = lower;
this.flavor = flavor;
}

public TypeVar(TypeSymbol tsym, Type bound, Type lower) {
@@ -1957,10 +1977,16 @@ public TypeVar(TypeSymbol tsym, Type bound, Type lower) {

public TypeVar(TypeSymbol tsym, Type bound, Type lower,
TypeMetadata metadata) {
this(tsym, bound, lower, metadata, TVFlavor.REFERENCE);
}

public TypeVar(TypeSymbol tsym, Type bound, Type lower,
TypeMetadata metadata, TVFlavor flavor) {
super(tsym, metadata);
Assert.checkNonNull(lower);
this.setUpperBound(bound);
this.lower = lower;
this.flavor = flavor;
}

@Override
@@ -2021,6 +2047,32 @@ public boolean isNullOrReference() {
public <R, P> R accept(TypeVisitor<R, P> v, P p) {
return v.visitTypeVariable(this, p);
}

public TVFlavor getTVFlavor() {
return flavor;
}

public boolean hasUniversalFlavor() {
return flavor == TVFlavor.UNIVERSAL;
}

@Override
public Type withTypeVar(Type t) {
Copy link
Collaborator

@mcimadamore mcimadamore Aug 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Uhm - isn't this method used on wildcard types, and it all other types is supposed to just return this ?

if (t.hasTag(TYPEVAR) &&
((TypeVar)t).flavor == TVFlavor.REFERENCE_FROM_UNIVERSAL &&
projection != null) {
return projection;
}
return this;
}

@Override
public TypeVar referenceProjection() {
if (projection == null) {
projection = new TypeVar(tsym, _bound, lower, metadata, TVFlavor.REFERENCE_FROM_UNIVERSAL);
}
return projection;
}
}

/** A captured type variable comes from wildcards which can have
@@ -2040,6 +2092,7 @@ public CapturedType(Name name,
this.lower = Assert.checkNonNull(lower);
this.setUpperBound(upper);
this.wildcard = wildcard;
this.flavor = wildcard.bound != null ? wildcard.bound.flavor : TVFlavor.REFERENCE;
}

public CapturedType(TypeSymbol tsym,
@@ -2050,6 +2103,7 @@ public CapturedType(TypeSymbol tsym,
TypeMetadata metadata) {
super(tsym, bound, lower, metadata);
this.wildcard = wildcard;
this.flavor = wildcard.bound != null ? wildcard.bound.flavor : TVFlavor.REFERENCE;
}

@Override
@@ -95,6 +95,7 @@ public class Types {
final boolean allowDefaultMethods;
final boolean mapCapturesToBounds;
final boolean allowValueBasedClasses;
final boolean allowUniversalTVars;
final Check chk;
final Enter enter;
JCDiagnostic.Factory diags;
@@ -125,7 +126,9 @@ protected Types(Context context) {
diags = JCDiagnostic.Factory.instance(context);
noWarnings = new Warner(null);
Options options = Options.instance(context);
Preview preview = Preview.instance(context);
allowValueBasedClasses = options.isSet("allowValueBasedClasses");
allowUniversalTVars = Feature.UNIVERSAL_TVARS.allowedInSource(source);
}
// </editor-fold>

@@ -608,6 +611,11 @@ public boolean isConvertible(Type t, Type s, Warner warn) {

boolean tValue = t.isPrimitiveClass();
boolean sValue = s.isPrimitiveClass();
if (allowUniversalTVars && (s.hasTag(TYPEVAR)) && ((TypeVar)s).hasUniversalFlavor() &&
(t.hasTag(BOT) || t.hasTag(TYPEVAR) && !((TypeVar)t).hasUniversalFlavor())) {
warn.warn(LintCategory.UNIVERSAL);
return true;
}
if (tValue != sValue) {
return tValue ?
isSubtype(t.referenceProjection(), s) :
@@ -1014,6 +1022,32 @@ public boolean isPrimitiveClass(Type t) {
return t != null && t.isPrimitiveClass();
}

@FunctionalInterface
public interface SubtypeTestFlavor {
boolean subtypeTest(Type t, Type s, Warner warn);
}

public boolean isBoundedBy(Type t, Type s, SubtypeTestFlavor subtypeTestFlavor) {
return isBoundedBy(t, s, noWarnings, subtypeTestFlavor);
}

/**
* Is type t bounded by s?
*/
public boolean isBoundedBy(Type t, Type s, Warner warn, SubtypeTestFlavor subtypeTestFlavor) {
boolean result = subtypeTestFlavor.subtypeTest(t, s, warn);
if (allowUniversalTVars && !result) {
if (isPrimitiveClass(t)) {
return isBoundedBy(t.referenceProjection(), s, warn, subtypeTestFlavor);
} else if (t.hasTag(TYPEVAR) && ((TypeVar)t).hasUniversalFlavor()) {
return isBoundedBy(t.getUpperBound(), s, warn, subtypeTestFlavor);
} else if (s.hasTag(TYPEVAR) && ((TypeVar)s).hasUniversalFlavor()) {
return isBoundedBy(t, s.getLowerBound(), warn, subtypeTestFlavor);
}
}
return result;
}

// <editor-fold defaultstate="collapsed" desc="isSubtype">
/**
* Is t an unchecked subtype of s?
@@ -1051,6 +1085,9 @@ private boolean isSubtypeUncheckedInternal(Type t, Type s, boolean capture, Warn
}
} else if (isSubtype(t, s, capture)) {
return true;
} else if (allowUniversalTVars && t.hasTag(TYPEVAR) && s.hasTag(TYPEVAR) && t.tsym == s.tsym) {
Copy link
Collaborator

@mcimadamore mcimadamore Aug 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure the check does what the comment says?

Copy link
Contributor Author

@vicente-romero-oracle vicente-romero-oracle Aug 5, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will remove the comment, probably a self comment I guess

warn.warn(LintCategory.UNIVERSAL);
return true;
} else if (t.hasTag(TYPEVAR)) {
return isSubtypeUncheckedInternal(t.getUpperBound(), s, false, warn);
} else if (!s.isRaw()) {
@@ -1102,6 +1139,9 @@ public final boolean isSubtypeNoCapture(Type t, Type s) {
public boolean isSubtype(Type t, Type s, boolean capture) {
if (t.equalsIgnoreMetadata(s))
return true;
if (t.hasTag(TYPEVAR) && s.hasTag(TYPEVAR) && t.tsym == s.tsym) {
return true;
}
if (s.isPartial())
return isSuperType(s, t);

@@ -1143,6 +1183,9 @@ public Boolean visitType(Type t, Type s) {
case TYPEVAR:
return isSubtypeNoCapture(t.getUpperBound(), s);
case BOT:
if (allowUniversalTVars && s.hasTag(TYPEVAR) && ((TypeVar)s).hasUniversalFlavor()) {
warnStack.head.warn(LintCategory.UNIVERSAL);
}
return
s.hasTag(BOT) || (s.hasTag(CLASS) && !isPrimitiveClass(s)) ||
s.hasTag(ARRAY) || s.hasTag(TYPEVAR);
@@ -1641,8 +1684,10 @@ public Boolean visitWildcardType(WildcardType t, Type s) {
// ---------------------------------------------------------------------------
return isSameWildcard(t, s)
|| isCaptureOf(s, t)
|| ((t.isExtendsBound() || isSubtypeNoCapture(wildLowerBound(t), wildLowerBound(s))) &&
(t.isSuperBound() || isSubtypeNoCapture(wildUpperBound(s), wildUpperBound(t))));
|| ((t.isExtendsBound() || isBoundedBy(wildLowerBound(t), wildLowerBound(s),
(t1, s1, w) -> isSubtypeNoCapture(t1, s1))) &&
(t.isSuperBound() || isBoundedBy(wildUpperBound(s), wildUpperBound(t),
(t1, s1, w) -> isSubtypeNoCapture(t1, s1))));
}
}

@@ -1655,6 +1700,16 @@ public Boolean visitUndetVar(UndetVar t, Type s) {
}
}

@Override
public Boolean visitTypeVar(TypeVar t, Type s) {
if (s.hasTag(TYPEVAR)) {
TypeVar other = (TypeVar)s;
if (allowUniversalTVars && t.hasUniversalFlavor() != other.hasUniversalFlavor() && t.tsym == other.tsym)
return true;
}
return isSameType(t, s);
}

@Override
public Boolean visitErrorType(ErrorType t, Type s) {
return true;
@@ -2075,7 +2130,7 @@ public boolean notSoftSubtype(Type t, Type s) {
if (!s.hasTag(WILDCARD))
s = cvarUpperBound(s);

return !isSubtype(t, relaxBound(s));
return !isBoundedBy(t, relaxBound(s), (t1, s1, w) -> isSubtype(t1, s1));
}

private Type relaxBound(Type t) {
@@ -3555,6 +3610,13 @@ public Type visitTypeVar(TypeVar t, Void ignored) {
if (t.equalsIgnoreMetadata(from.head)) {
return to.head.withTypeVar(t);
}
if (allowUniversalTVars &&
Copy link
Collaborator

@mcimadamore mcimadamore Aug 4, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure about this - the main issue here seems that t.equalsIgnoreMetadata is too weak and would return false for ref vs.val mismatches. But again I'm uncertain about the need of overriding withTypeVar (see comment above).

Copy link
Contributor Author

@vicente-romero-oracle vicente-romero-oracle Aug 5, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure I follow you here, t.equalsIgnoreMetadata is already being used in the method, not sure I see in what case it can give an incorrect answer

Copy link
Collaborator

@mcimadamore mcimadamore Aug 5, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I mean is that the newly added code seems to workaround the fact that in some cases we do not call withTypeVar. I'm saying that because you added another test which, if satisfied, ends up calling to.head.withTypeVar(t); again, which seems to suggest that you found cases where this call was needed, but was not triggered - probably because of a mismatch between T and T.ref?

t.flavor == TypeVar.TVFlavor.REFERENCE_FROM_UNIVERSAL &&
from.head.hasTag(TYPEVAR) &&
((TypeVar)from.head).projection != null &&
Copy link
Collaborator

@mcimadamore mcimadamore Aug 10, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't this test be isUniversal() ?

Copy link
Contributor Author

@vicente-romero-oracle vicente-romero-oracle Aug 11, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

doing this change right now breaks the build, I think it is because we generate reference projections for universal type vars in a lazy way, so some universal type variables could have its projection field set to null

Copy link
Collaborator

@mcimadamore mcimadamore Aug 11, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ugh - I understand - moving forward it'd be nice not to depend on tricky init semantics.

Copy link
Contributor Author

@vicente-romero-oracle vicente-romero-oracle Aug 11, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep, will fix this

t.equalsIgnoreMetadata(((TypeVar)from.head).projection)) {
Copy link
Collaborator

@mcimadamore mcimadamore Aug 10, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and here, we should call referencceProjection() ?

Copy link
Contributor Author

@vicente-romero-oracle vicente-romero-oracle Aug 11, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep I agree

return to.head.withTypeVar(t);
}
}
return t;
}
@@ -3704,7 +3766,11 @@ public List<Type> newInstances(List<Type> tvars) {
private static final TypeMapping<Void> newInstanceFun = new TypeMapping<Void>() {
@Override
public TypeVar visitTypeVar(TypeVar t, Void _unused) {
return new TypeVar(t.tsym, t.getUpperBound(), t.getLowerBound(), t.getMetadata());
TypeVar newTV = new TypeVar(t.tsym, t.getUpperBound(), t.getLowerBound(), t.getMetadata(), t.flavor);
if (t.projection != null) {
newTV.referenceProjection();
}
return newTV;
}
};
// </editor-fold>