Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,7 @@ public boolean isPreview(Feature feature) {
case SUPER_INIT -> true;
case PRIMITIVE_PATTERNS -> true;
case VALUE_CLASSES -> true;
case NULL_RESTRICTED_TYPES -> true;
//Note: this is a backdoor which allows to optionally treat all features as 'preview' (for testing).
//When real preview features will be added, this method can be implemented to return 'true'
//for those selected features, and 'false' for all the others.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,7 @@ public enum Feature {
PRIMITIVE_PATTERNS(JDK23, Fragments.FeaturePrimitivePatterns, DiagKind.PLURAL),
SUPER_INIT(JDK22, Fragments.FeatureSuperInit, DiagKind.NORMAL),
VALUE_CLASSES(JDK22, Fragments.FeatureValueClasses, DiagKind.PLURAL),
NULL_RESTRICTED_TYPES(JDK23, Fragments.FeatureNullRestrictedTypes, DiagKind.PLURAL),
;

enum DiagKind {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,8 @@ public class Types {

public final Warner noWarnings;

private boolean enableNullRestrictedTypes;
/* are nullable and null-restricted types allowed? */
private boolean allowNullRestrictedTypes;

// <editor-fold defaultstate="collapsed" desc="Instantiating">
public static Types instance(Context context) {
Expand All @@ -126,8 +127,9 @@ public String toString() {
return "NO_WARNINGS";
}
};
Options options = Options.instance(context);
enableNullRestrictedTypes = options.isSet("enableNullRestrictedTypes");
Preview preview = Preview.instance(context);
allowNullRestrictedTypes = (!preview.isPreview(Source.Feature.NULL_RESTRICTED_TYPES) || preview.isEnabled()) &&
Source.Feature.NULL_RESTRICTED_TYPES.allowedInSource(source);
}
// </editor-fold>

Expand Down Expand Up @@ -1086,7 +1088,7 @@ public final boolean isSubtypeNoCapture(Type t, Type s) {
}
public boolean isSubtype(Type t, Type s, boolean capture) {
if (t.equalsIgnoreMetadata(s)) {
if (enableNullRestrictedTypes) {
if (allowNullRestrictedTypes) {
new NullabilityComparator((t1, t2) -> hasNarrowerNullability(t1, t2)).visit(s, t);
}
return true;
Expand Down Expand Up @@ -1208,7 +1210,7 @@ public Boolean visitClassType(ClassType t, Type s) {
&& (!s.isParameterized() || containsTypeRecursive(s, sup))
&& isSubtypeNoCapture(sup.getEnclosingType(),
s.getEnclosingType());
if (result && enableNullRestrictedTypes) {
if (result && allowNullRestrictedTypes) {
new NullabilityComparator((t1, t2) -> hasNarrowerNullability(t1, t2)).visit(s, t);
}
return result;
Expand Down Expand Up @@ -1488,7 +1490,7 @@ public Boolean visitClassType(ClassType t, Type s) {
boolean equal = t.tsym == s.tsym
&& visit(t.getEnclosingType(), s.getEnclosingType())
&& containsTypeEquivalent(t.getTypeArguments(), s.getTypeArguments());
if (equal && enableNullRestrictedTypes) {
if (equal && allowNullRestrictedTypes) {
new NullabilityComparator((t1, t2) -> !hasSameNullability(t1, t2)).visit(s, t);
}
return equal;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,8 @@ protected Attr(Context context) {
unknownTypeInfo = new ResultInfo(KindSelector.TYP, Type.noType);
unknownTypeExprInfo = new ResultInfo(KindSelector.VAL_TYP, Type.noType);
recoveryInfo = new RecoveryInfo(deferredAttr.emptyDeferredAttrContext);
enableNullRestrictedTypes = options.isSet("enableNullRestrictedTypes");
allowNullRestrictedTypes = (!preview.isPreview(Source.Feature.NULL_RESTRICTED_TYPES) || preview.isEnabled()) &&
Source.Feature.NULL_RESTRICTED_TYPES.allowedInSource(source);
}

/** Switch: reifiable types in instanceof enabled?
Expand Down Expand Up @@ -215,7 +216,7 @@ protected Attr(Context context) {

/** Are null-restricted types allowed
*/
private final boolean enableNullRestrictedTypes;
private final boolean allowNullRestrictedTypes;

/** Check kind and type of given tree against protokind and prototype.
* If check succeeds, store type in tree and return it.
Expand Down Expand Up @@ -743,8 +744,7 @@ public Type attribType(JCTree tree, Env<AttrContext> env) {
*/
Type attribType(JCTree tree, Env<AttrContext> env, Type pt) {
Type result = attribTree(tree, env, new ResultInfo(KindSelector.TYP, pt));
if (enableNullRestrictedTypes &&
tree instanceof JCNullableTypeExpression nullableTypeExpression &&
if (allowNullRestrictedTypes && tree instanceof JCNullableTypeExpression nullableTypeExpression &&
nullableTypeExpression.getNullMarker() != NullMarker.UNSPECIFIED) {
result = tree.type = result.addMetadata(new TypeMetadata.NullMarker(nullableTypeExpression.getNullMarker()));
}
Expand Down Expand Up @@ -1183,7 +1183,7 @@ public void visitMethodDef(JCMethodDecl tree) {
for (List<JCExpression> l = tree.thrown; l.nonEmpty(); l = l.tail)
chk.checkType(l.head.pos(), l.head.type, syms.throwableType);

if (enableNullRestrictedTypes && tree.sym.isImplicitConstructor()) {
if (allowNullRestrictedTypes && tree.sym.isImplicitConstructor()) {
if (tree.body == null) {
tree.body = make.Block(0, List.nil());
} else {
Expand Down Expand Up @@ -1362,7 +1362,7 @@ public void visitVarDef(JCVariableDecl tree) {
log.error(tree, Errors.IllegalRecordComponentName(v));
}
}
if (enableNullRestrictedTypes) {
if (allowNullRestrictedTypes) {
Type elemOrType = result;
while (!elemOrType.hasTag(ERROR) && types.elemtype(elemOrType) != null) {
elemOrType = types.elemtype(elemOrType);
Expand Down Expand Up @@ -2960,7 +2960,7 @@ else if (!skipNonDiamondPath) {

if (tree.constructor != null && tree.constructor.kind == MTH) {
owntype = clazztype;
if (enableNullRestrictedTypes && owntype.getMetadata(TypeMetadata.NullMarker.class) == null) {
if (allowNullRestrictedTypes && owntype.getMetadata(TypeMetadata.NullMarker.class) == null) {
owntype = owntype.addMetadata(new TypeMetadata.NullMarker(NullMarker.NOT_NULL)); // constructor invocations are always null restricted
}
}
Expand Down Expand Up @@ -5433,8 +5433,7 @@ public void attribClass(DiagnosticPosition pos, ClassSymbol c) {
attribClass(c);
if (c.type.isValueClass()) {
final Env<AttrContext> env = typeEnvs.get(c);
if (enableNullRestrictedTypes &&
env != null && env.tree != null && env.tree.hasTag(CLASSDEF) && TreeInfo.getImplicitConstructor(((JCClassDecl)env.tree).defs) != null)
if (allowNullRestrictedTypes && env != null && env.tree != null && env.tree.hasTag(CLASSDEF) && TreeInfo.getImplicitConstructor(((JCClassDecl)env.tree).defs) != null)
chk.checkNonCyclicMembership((JCClassDecl)env.tree);
}
} catch (CompletionFailure ex) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,8 @@ protected Check(Context context) {
allowSealed = Feature.SEALED_CLASSES.allowedInSource(source);
allowValueClasses = (!preview.isPreview(Feature.VALUE_CLASSES) || preview.isEnabled()) &&
Feature.VALUE_CLASSES.allowedInSource(source);
enableNullRestrictedTypes = options.isSet("enableNullRestrictedTypes");
allowNullRestrictedTypes = (!preview.isPreview(Source.Feature.NULL_RESTRICTED_TYPES) || preview.isEnabled()) &&
Source.Feature.NULL_RESTRICTED_TYPES.allowedInSource(source);
}

/** Character for synthetic names
Expand Down Expand Up @@ -231,9 +232,10 @@ protected Check(Context context) {
*/
private final boolean allowValueClasses;

/** Are null-restricted types allowed
/** Are null restricted types allowed
*/
private final boolean enableNullRestrictedTypes;
private final boolean allowNullRestrictedTypes;

/* *************************************************************************
* Errors and Warnings
**************************************************************************/
Expand Down Expand Up @@ -313,7 +315,7 @@ public void warnUnchecked(DiagnosticPosition pos, Warning warnKey) {
* @param warnKey A warning key.
*/
public void warnNullableTypes(DiagnosticPosition pos, Warning warnKey) {
if (enableNullRestrictedTypes && lint.isEnabled(LintCategory.NULL)) {
if (allowNullRestrictedTypes && lint.isEnabled(LintCategory.NULL)) {
log.warning(LintCategory.NULL, pos, warnKey);
}
}
Expand Down Expand Up @@ -816,7 +818,7 @@ void checkConstraintsOfValueClass(JCClassDecl tree, ClassSymbol c) {
}

void checkConstraintsOfValueClassesWithImplicitConst(JCClassDecl classDecl, ClassSymbol c) {
if (enableNullRestrictedTypes) {
if (allowNullRestrictedTypes) {
JCMethodDecl implicitConstructor = TreeInfo.getImplicitConstructor(classDecl.defs);
if (implicitConstructor != null) {
Type encl = c.type.getEnclosingType();
Expand Down Expand Up @@ -2794,7 +2796,7 @@ void checkCompatibleSupertypes(DiagnosticPosition pos, Type c) {

boolean implementsLooselyConsistentValue = false;
try {
implementsLooselyConsistentValue = allowValueClasses && enableNullRestrictedTypes ? types.asSuper(c, syms.looselyConsistentValueType.tsym) != null : false;
implementsLooselyConsistentValue = allowValueClasses && allowNullRestrictedTypes ? types.asSuper(c, syms.looselyConsistentValueType.tsym) != null : false;
} catch (CompletionFailure cf) {
// ignore
}
Expand Down Expand Up @@ -4571,7 +4573,7 @@ public NullnessWarner(DiagnosticPosition pos) {

@Override
public void warn(LintCategory lint) {
if (enableNullRestrictedTypes) {
if (allowNullRestrictedTypes) {
boolean warned = this.warned;
super.warn(lint);
if (warned) return; // suppress redundant diagnostics
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ public static Lower instance(Context context) {
private final boolean useMatchException;
private final HashMap<TypePairs, String> typePairToName;
private final boolean allowValueClasses;
private final boolean enableNullRestrictedTypes;
private final boolean allowNullRestrictedTypes;

@SuppressWarnings("this-escape")
protected Lower(Context context) {
Expand Down Expand Up @@ -139,7 +139,8 @@ protected Lower(Context context) {
typePairToName = TypePairs.initialize(syms);
this.allowValueClasses = (!preview.isPreview(Feature.VALUE_CLASSES) || preview.isEnabled()) &&
Feature.VALUE_CLASSES.allowedInSource(source);
enableNullRestrictedTypes = options.isSet("enableNullRestrictedTypes");
this.allowNullRestrictedTypes = (!preview.isPreview(Source.Feature.NULL_RESTRICTED_TYPES) || preview.isEnabled()) &&
Source.Feature.NULL_RESTRICTED_TYPES.allowedInSource(source);
}

/** The currently enclosing class.
Expand Down Expand Up @@ -4401,7 +4402,7 @@ public void visitNewArray(JCNewArray tree) {
noOfDims++;
}
tree.elems = translate(tree.elems, types.elemtype(tree.type));
if (!enableNullRestrictedTypes || tree.elemtype == null || !originalElemType.type.isNonNullable()) {
if (!allowNullRestrictedTypes || tree.elemtype == null || !originalElemType.type.isNonNullable()) {
result = tree;
} else {
Symbol elemClass = syms.getClassField(tree.elemtype.type, types);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public class ClassWriter extends ClassFile {

/** Switch: are null-restricted types allowed
*/
private boolean enableNullRestrictedTypes;
private boolean allowNullRestrictedTypes;

/** Switch: generate CharacterRangeTable attribute.
*/
Expand Down Expand Up @@ -197,7 +197,8 @@ protected ClassWriter(Context context) {
dumpInnerClassModifiers = modifierFlags.indexOf('i') != -1;
dumpMethodModifiers = modifierFlags.indexOf('m') != -1;
}
enableNullRestrictedTypes = options.isSet("enableNullRestrictedTypes");
allowNullRestrictedTypes = (!preview.isPreview(Source.Feature.NULL_RESTRICTED_TYPES) || preview.isEnabled()) &&
Source.Feature.NULL_RESTRICTED_TYPES.allowedInSource(source);
}

public void addExtraAttributes(ToIntFunction<Symbol> addExtraAttributes) {
Expand Down Expand Up @@ -959,7 +960,7 @@ int writePermittedSubclassesIfNeeded(ClassSymbol csym) {
/** Write "ImplicitCreation" attribute.
*/
int writeImplicitCreationIfNeeded(ClassSymbol csym) {
if (enableNullRestrictedTypes && csym.isValueClass() && csym.hasImplicitConstructor()) {
if (allowNullRestrictedTypes && csym.isValueClass() && csym.hasImplicitConstructor()) {
int alenIdx = writeAttr(names.ImplicitCreation);
int flags = /*ACC_DEFAULT |*/ (csym.isSubClass(syms.looselyConsistentValueType.tsym, types) ? ACC_NON_ATOMIC : 0);
databuf.appendChar(flags);
Expand All @@ -972,9 +973,12 @@ int writeImplicitCreationIfNeeded(ClassSymbol csym) {
/** Write "NullRestricted" attribute.
*/
int writeNullRestrictedIfNeeded(Symbol sym) {
if (enableNullRestrictedTypes && sym.kind == VAR && sym.type.isNonNullable() && !sym.type.hasTag(ARRAY)) {
if (allowNullRestrictedTypes && sym.kind == VAR && sym.type.isNonNullable() && !sym.type.hasTag(ARRAY)) {
int alenIdx = writeAttr(names.NullRestricted);
endAttr(alenIdx);
if (preview.isPreview(Source.Feature.VALUE_CLASSES)) {
preview.markUsesPreview(null);
}
return 1;
}
return 0;
Expand Down
Loading