8250625: Compiler implementation of Pattern Matching for instanceof (Final)
Reviewed-by: vromero
This commit is contained in:
parent
60e4aca846
commit
18bc95ba51
@ -54,7 +54,6 @@ public @interface PreviewFeature {
|
||||
public boolean essentialAPI() default false;
|
||||
|
||||
public enum Feature {
|
||||
PATTERN_MATCHING_IN_INSTANCEOF,
|
||||
// 8242284:
|
||||
// The TEXT_BLOCKS enum constant is not used in the JDK 15 codebase, but
|
||||
// exists to support the bootcycle build of JDK 15. The bootcycle build
|
||||
|
@ -121,20 +121,9 @@ public enum ElementKind {
|
||||
RECORD_COMPONENT,
|
||||
|
||||
/**
|
||||
* {@preview Associated with pattern matching for {@code
|
||||
* instanceof}, a preview feature of the Java language.
|
||||
*
|
||||
* This enum constant is associated with <i>pattern
|
||||
* matching for {@code instanceof}</i>, a preview
|
||||
* feature of the Java language. Preview features
|
||||
* may be removed in a future release, or upgraded to permanent
|
||||
* features of the Java language.}
|
||||
*
|
||||
* A binding variable in a pattern .
|
||||
* @since 14
|
||||
* A binding variable in a pattern.
|
||||
* @since 16
|
||||
*/
|
||||
@jdk.internal.PreviewFeature(feature=jdk.internal.PreviewFeature.Feature.PATTERN_MATCHING_IN_INSTANCEOF,
|
||||
essentialAPI=false)
|
||||
BINDING_VARIABLE;
|
||||
|
||||
/**
|
||||
|
@ -25,34 +25,17 @@
|
||||
|
||||
package com.sun.source.tree;
|
||||
|
||||
import javax.lang.model.element.Name;
|
||||
|
||||
/**
|
||||
* {@preview Associated with pattern matching for instanceof, a preview feature of
|
||||
* the Java language.
|
||||
*
|
||||
* This interface is associated with <i>pattern matching for instanceof</i>, a preview
|
||||
* feature of the Java language. Preview features
|
||||
* may be removed in a future release, or upgraded to permanent
|
||||
* features of the Java language.}
|
||||
*
|
||||
* A binding pattern tree
|
||||
*
|
||||
* @since 14
|
||||
* @since 16
|
||||
*/
|
||||
public interface BindingPatternTree extends PatternTree {
|
||||
|
||||
/**
|
||||
* Returns the type of the bind variable.
|
||||
* @return the type
|
||||
* Returns the binding variable.
|
||||
* @return the binding variable
|
||||
*/
|
||||
Tree getType();
|
||||
|
||||
/**
|
||||
* A binding variable name.
|
||||
* @return the name of the binding variable
|
||||
*/
|
||||
Name getBinding();
|
||||
VariableTree getVariable();
|
||||
|
||||
}
|
||||
|
||||
|
@ -49,18 +49,11 @@ public interface InstanceOfTree extends ExpressionTree {
|
||||
/**
|
||||
* Returns the type for which to check.
|
||||
* @return the type
|
||||
* @see #getPattern()
|
||||
*/
|
||||
Tree getType();
|
||||
|
||||
/**
|
||||
* {@preview Associated with pattern matching for instanceof, a preview feature of
|
||||
* the Java language.
|
||||
*
|
||||
* This method is associated with <i>pattern matching for instanceof</i>, a preview
|
||||
* feature of the Java language. Preview features
|
||||
* may be removed in a future release, or upgraded to permanent
|
||||
* features of the Java language.}
|
||||
*
|
||||
* Returns the tested pattern, or null if this instanceof does not use
|
||||
* a pattern.
|
||||
*
|
||||
@ -77,7 +70,7 @@ public interface InstanceOfTree extends ExpressionTree {
|
||||
* returns null.
|
||||
*
|
||||
* @return the tested pattern, or null if this instanceof does not use a pattern
|
||||
* @since 14
|
||||
* @since 16
|
||||
*/
|
||||
PatternTree getPattern();
|
||||
}
|
||||
|
@ -26,17 +26,9 @@
|
||||
package com.sun.source.tree;
|
||||
|
||||
/**
|
||||
* {@preview Associated with pattern matching for instanceof, a preview feature of
|
||||
* the Java language.
|
||||
*
|
||||
* This interface is associated with <i>pattern matching for instanceof</i>, a preview
|
||||
* feature of the Java language. Preview features
|
||||
* may be removed in a future release, or upgraded to permanent
|
||||
* features of the Java language.}
|
||||
*
|
||||
* A tree node used as the base class for the different kinds of
|
||||
* statements.
|
||||
* patterns.
|
||||
*
|
||||
* @since 14
|
||||
* @since 16
|
||||
*/
|
||||
public interface PatternTree extends Tree {}
|
||||
|
@ -220,17 +220,9 @@ public interface Tree {
|
||||
PARENTHESIZED(ParenthesizedTree.class),
|
||||
|
||||
/**
|
||||
* {@preview Associated with pattern matching for instanceof, a preview feature of
|
||||
* the Java language.
|
||||
*
|
||||
* This enum constant is associated with <i>pattern matching for instanceof</i>, a preview
|
||||
* feature of the Java language. Preview features
|
||||
* may be removed in a future release, or upgraded to permanent
|
||||
* features of the Java language.}
|
||||
*
|
||||
* Used for instances of {@link BindingPatternTree}.
|
||||
*
|
||||
* @since 14
|
||||
* @since 16
|
||||
*/
|
||||
BINDING_PATTERN(BindingPatternTree.class),
|
||||
|
||||
|
@ -258,19 +258,11 @@ public interface TreeVisitor<R,P> {
|
||||
R visitLiteral(LiteralTree node, P p);
|
||||
|
||||
/**
|
||||
* {@preview Associated with pattern matching for instanceof, a preview feature of
|
||||
* the Java language.
|
||||
*
|
||||
* This method is associated with <i>pattern matching for instanceof</i>, a preview
|
||||
* feature of the Java language. Preview features
|
||||
* may be removed in a future release, or upgraded to permanent
|
||||
* features of the Java language.}
|
||||
*
|
||||
* Visits an BindingPattern node.
|
||||
* @param node the node being visited
|
||||
* @param p a parameter value
|
||||
* @return a result value
|
||||
* @since 14
|
||||
* @since 16
|
||||
*/
|
||||
R visitBindingPattern(BindingPatternTree node, P p);
|
||||
|
||||
|
@ -691,7 +691,7 @@ public class TreeScanner<R,P> implements TreeVisitor<R,P> {
|
||||
*/
|
||||
@Override
|
||||
public R visitBindingPattern(BindingPatternTree node, P p) {
|
||||
return scan(node.getType(), p);
|
||||
return scan(node.getVariable(), p);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -165,9 +165,7 @@ public class Preview {
|
||||
* @return true, if given feature is a preview feature.
|
||||
*/
|
||||
public boolean isPreview(Feature feature) {
|
||||
if (feature == Feature.PATTERN_MATCHING_IN_INSTANCEOF ||
|
||||
feature == Feature.REIFIABLE_TYPES_INSTANCEOF ||
|
||||
feature == Feature.SEALED_CLASSES)
|
||||
if (feature == Feature.SEALED_CLASSES)
|
||||
return 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'
|
||||
|
@ -1855,7 +1855,7 @@ public abstract class Symbol extends AnnoConstruct implements PoolConstant, Elem
|
||||
public static class BindingSymbol extends VarSymbol {
|
||||
|
||||
public BindingSymbol(Name name, Type type, Symbol owner) {
|
||||
super(Flags.FINAL | Flags.HASINIT | Flags.MATCH_BINDING, name, type, owner);
|
||||
super(Flags.HASINIT | Flags.MATCH_BINDING, name, type, owner);
|
||||
}
|
||||
|
||||
public boolean isAliasFor(BindingSymbol b) {
|
||||
|
@ -951,9 +951,8 @@ public class TypeAnnotations {
|
||||
" within frame " + frame);
|
||||
}
|
||||
|
||||
case BINDING_PATTERN:
|
||||
case VARIABLE:
|
||||
VarSymbol v = frame.hasTag(Tag.BINDINGPATTERN) ? ((JCBindingPattern) frame).symbol : ((JCVariableDecl) frame).sym;
|
||||
VarSymbol v = ((JCVariableDecl) frame).sym;
|
||||
if (v.getKind() != ElementKind.FIELD) {
|
||||
appendTypeAnnotationsToOwner(v, v.getRawTypeAttributes());
|
||||
}
|
||||
|
@ -308,8 +308,6 @@ public class Attr extends JCTree.Visitor {
|
||||
isAssignableAsBlankFinal(v, env)))) {
|
||||
if (v.isResourceVariable()) { //TWR resource
|
||||
log.error(pos, Errors.TryResourceMayNotBeAssigned(v));
|
||||
} else if ((v.flags() & MATCH_BINDING) != 0) {
|
||||
log.error(pos, Errors.PatternBindingMayNotBeAssigned(v));
|
||||
} else {
|
||||
log.error(pos, Errors.CantAssignValToFinalVar(v));
|
||||
}
|
||||
@ -3933,10 +3931,14 @@ public class Attr extends JCTree.Visitor {
|
||||
if (tree.pattern.getTag() == BINDINGPATTERN) {
|
||||
attribTree(tree.pattern, env, unknownExprInfo);
|
||||
clazztype = tree.pattern.type;
|
||||
if (types.isSubtype(exprtype, clazztype) &&
|
||||
!exprtype.isErroneous() && !clazztype.isErroneous()) {
|
||||
log.error(tree.pos(), Errors.InstanceofPatternNoSubtype(clazztype, exprtype));
|
||||
}
|
||||
JCBindingPattern pattern = (JCBindingPattern) tree.pattern;
|
||||
typeTree = pattern.vartype;
|
||||
typeTree = pattern.var.vartype;
|
||||
if (!clazztype.hasTag(TYPEVAR)) {
|
||||
clazztype = chk.checkClassOrArrayType(pattern.vartype.pos(), clazztype);
|
||||
clazztype = chk.checkClassOrArrayType(pattern.var.vartype.pos(), clazztype);
|
||||
}
|
||||
} else {
|
||||
clazztype = attribType(tree.pattern, env);
|
||||
@ -3962,7 +3964,9 @@ public class Attr extends JCTree.Visitor {
|
||||
valid = true;
|
||||
}
|
||||
} else {
|
||||
log.error(typeTree.pos(), Errors.IllegalGenericTypeForInstof);
|
||||
log.error(DiagnosticFlag.SOURCE_LEVEL, tree.pos(),
|
||||
Feature.REIFIABLE_TYPES_INSTANCEOF.error(this.sourceName));
|
||||
allowReifiableTypesInInstanceof = true;
|
||||
}
|
||||
if (!valid) {
|
||||
clazztype = types.createErrorType(clazztype);
|
||||
@ -3975,15 +3979,17 @@ public class Attr extends JCTree.Visitor {
|
||||
|
||||
public void visitBindingPattern(JCBindingPattern tree) {
|
||||
ResultInfo varInfo = new ResultInfo(KindSelector.TYP, resultInfo.pt, resultInfo.checkContext);
|
||||
tree.type = attribTree(tree.vartype, env, varInfo);
|
||||
VarSymbol v = tree.symbol = new BindingSymbol(tree.name, tree.vartype.type, env.info.scope.owner);
|
||||
if (chk.checkUnique(tree.pos(), v, env.info.scope)) {
|
||||
chk.checkTransparentVar(tree.pos(), v, env.info.scope);
|
||||
tree.type = tree.var.type = attribTree(tree.var.vartype, env, varInfo);
|
||||
BindingSymbol v = new BindingSymbol(tree.var.name, tree.var.vartype.type, env.info.scope.owner);
|
||||
v.pos = tree.pos;
|
||||
tree.var.sym = v;
|
||||
if (chk.checkUnique(tree.var.pos(), v, env.info.scope)) {
|
||||
chk.checkTransparentVar(tree.var.pos(), v, env.info.scope);
|
||||
}
|
||||
annotate.queueScanTreeAndTypeAnnotate(tree.vartype, env, v, tree.pos());
|
||||
annotate.queueScanTreeAndTypeAnnotate(tree.var.vartype, env, v, tree.var.pos());
|
||||
annotate.flush();
|
||||
result = tree.type;
|
||||
matchBindings = new MatchBindings(List.of(tree.symbol), List.nil());
|
||||
matchBindings = new MatchBindings(List.of(v), List.nil());
|
||||
}
|
||||
|
||||
public void visitIndexed(JCArrayAccess tree) {
|
||||
@ -5733,9 +5739,9 @@ public class Attr extends JCTree.Visitor {
|
||||
|
||||
@Override
|
||||
public void visitBindingPattern(JCBindingPattern that) {
|
||||
if (that.symbol == null) {
|
||||
that.symbol = new BindingSymbol(that.name, that.type, syms.noSymbol);
|
||||
that.symbol.adr = 0;
|
||||
if (that.var.sym == null) {
|
||||
that.var.sym = new BindingSymbol(that.var.name, that.var.type, syms.noSymbol);
|
||||
that.var.sym.adr = 0;
|
||||
}
|
||||
super.visitBindingPattern(that);
|
||||
}
|
||||
|
@ -1875,7 +1875,8 @@ public class Flow {
|
||||
void checkInit(DiagnosticPosition pos, VarSymbol sym, Error errkey) {
|
||||
if ((sym.adr >= firstadr || sym.owner.kind != TYP) &&
|
||||
trackable(sym) &&
|
||||
!inits.isMember(sym.adr)) {
|
||||
!inits.isMember(sym.adr) &&
|
||||
(sym.flags_field & CLASH) == 0) {
|
||||
log.error(pos, errkey);
|
||||
inits.incl(sym.adr);
|
||||
}
|
||||
@ -2758,6 +2759,12 @@ public class Flow {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitBindingPattern(JCBindingPattern tree) {
|
||||
super.visitBindingPattern(tree);
|
||||
initParam(tree.var);
|
||||
}
|
||||
|
||||
void referenced(Symbol sym) {
|
||||
unrefdResources.remove(sym);
|
||||
}
|
||||
|
@ -148,6 +148,7 @@ public class MatchBindingsComputer extends TreeScanner {
|
||||
(v1.flags() & CLASH) == 0 &&
|
||||
(v2.flags() & CLASH) == 0) {
|
||||
log.error(pos, Errors.MatchBindingExists);
|
||||
v2.flags_field |= CLASH;
|
||||
list = list.append(v2);
|
||||
}
|
||||
}
|
||||
@ -166,6 +167,7 @@ public class MatchBindingsComputer extends TreeScanner {
|
||||
(ov.flags() & CLASH) == 0 &&
|
||||
(v.flags() & CLASH) == 0) {
|
||||
log.error(pos, Errors.MatchBindingExists);
|
||||
v.flags_field |= CLASH;
|
||||
}
|
||||
}
|
||||
list = list.append(v);
|
||||
|
@ -153,20 +153,20 @@ public class TransPatterns extends TreeTranslator {
|
||||
//=>
|
||||
//(let T' N$temp = E; N$temp instanceof T && (N = (T) N$temp == (T) N$temp))
|
||||
JCBindingPattern patt = (JCBindingPattern)tree.pattern;
|
||||
VarSymbol pattSym = patt.symbol;
|
||||
VarSymbol pattSym = patt.var.sym;
|
||||
Type tempType = tree.expr.type.hasTag(BOT) ?
|
||||
syms.objectType
|
||||
: tree.expr.type;
|
||||
VarSymbol temp = new VarSymbol(pattSym.flags() | Flags.SYNTHETIC,
|
||||
names.fromString(pattSym.name.toString() + target.syntheticNameChar() + "temp"),
|
||||
tempType,
|
||||
patt.symbol.owner);
|
||||
patt.var.sym.owner);
|
||||
JCExpression translatedExpr = translate(tree.expr);
|
||||
Type castTargetType = types.boxedTypeOrType(pattSym.erasure(types));
|
||||
|
||||
result = makeTypeTest(make.Ident(temp), make.Type(castTargetType));
|
||||
|
||||
VarSymbol bindingVar = bindingContext.bindingDeclared(patt.symbol);
|
||||
VarSymbol bindingVar = bindingContext.bindingDeclared((BindingSymbol) patt.var.sym);
|
||||
if (bindingVar != null) { //TODO: cannot be null here?
|
||||
JCAssign fakeInit = (JCAssign)make.at(tree.pos).Assign(
|
||||
make.Ident(bindingVar), convert(make.Ident(temp), castTargetType)).setType(bindingVar.erasure(types));
|
||||
|
@ -568,9 +568,7 @@ public class TransTypes extends TreeTranslator {
|
||||
}
|
||||
|
||||
public void visitBindingPattern(JCBindingPattern tree) {
|
||||
if (tree.vartype != null) {
|
||||
tree.vartype = translate(tree.vartype, null);
|
||||
}
|
||||
tree.var = translate(tree.var, null);
|
||||
result = tree;
|
||||
}
|
||||
|
||||
|
@ -257,13 +257,10 @@ public class TreeDiffer extends TreeScanner {
|
||||
@Override
|
||||
public void visitBindingPattern(JCBindingPattern tree) {
|
||||
JCBindingPattern that = (JCBindingPattern) parameter;
|
||||
result =
|
||||
scan(tree.vartype, that.vartype)
|
||||
&& tree.name == that.name;
|
||||
result = scan(tree.var, that.var);
|
||||
if (!result) {
|
||||
return;
|
||||
}
|
||||
equiv.put(tree.symbol, that.symbol);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -106,12 +106,6 @@ public class TreeHasher extends TreeScanner {
|
||||
super.visitSelect(tree);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitBindingPattern(JCTree.JCBindingPattern tree) {
|
||||
symbolHashes.computeIfAbsent(tree.symbol, k -> symbolHashes.size());
|
||||
super.visitBindingPattern(tree);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void visitVarDef(JCVariableDecl tree) {
|
||||
symbolHashes.computeIfAbsent(tree.sym, k -> symbolHashes.size());
|
||||
|
@ -932,10 +932,18 @@ public class JavacParser implements Parser {
|
||||
if (token.kind == INSTANCEOF) {
|
||||
int pos = token.pos;
|
||||
nextToken();
|
||||
JCTree pattern = parseType();
|
||||
int typePos = token.pos;
|
||||
JCExpression type = parseType();
|
||||
JCTree pattern;
|
||||
if (token.kind == IDENTIFIER) {
|
||||
checkSourceLevel(token.pos, Feature.PATTERN_MATCHING_IN_INSTANCEOF);
|
||||
pattern = toP(F.at(token.pos).BindingPattern(ident(), pattern));
|
||||
JCModifiers mods = F.at(Position.NOPOS).Modifiers(0);
|
||||
JCVariableDecl var = toP(F.at(token.pos).VarDef(mods, ident(), type, null));
|
||||
TreeInfo.getStartPos(var);
|
||||
pattern = toP(F.at(typePos).BindingPattern(var));
|
||||
TreeInfo.getStartPos(pattern);
|
||||
} else {
|
||||
pattern = type;
|
||||
}
|
||||
odStack[top] = F.at(pos).TypeTest(odStack[top], pattern);
|
||||
} else {
|
||||
|
@ -545,10 +545,6 @@ compiler.err.final.parameter.may.not.be.assigned=\
|
||||
compiler.err.try.resource.may.not.be.assigned=\
|
||||
auto-closeable resource {0} may not be assigned
|
||||
|
||||
# 0: symbol
|
||||
compiler.err.pattern.binding.may.not.be.assigned=\
|
||||
pattern binding {0} may not be assigned
|
||||
|
||||
# 0: symbol
|
||||
compiler.err.multicatch.parameter.may.not.be.assigned=\
|
||||
multi-catch parameter {0} may not be assigned
|
||||
@ -620,9 +616,6 @@ compiler.err.illegal.self.ref=\
|
||||
compiler.warn.self.ref=\
|
||||
self-reference in initializer of variable ''{0}''
|
||||
|
||||
compiler.err.illegal.generic.type.for.instof=\
|
||||
illegal generic type for instanceof
|
||||
|
||||
# 0: type
|
||||
compiler.err.illegal.initializer.for.type=\
|
||||
illegal initializer for {0}
|
||||
@ -1418,6 +1411,10 @@ compiler.misc.varargs.trustme.on.reifiable.varargs=\
|
||||
compiler.err.instanceof.reifiable.not.safe=\
|
||||
{0} cannot be safely cast to {1}
|
||||
|
||||
# 0: type, 1: type
|
||||
compiler.err.instanceof.pattern.no.subtype=\
|
||||
pattern type {0} is a subtype of expression type {1}
|
||||
|
||||
# 0: symbol
|
||||
compiler.misc.varargs.trustme.on.non.varargs.meth=\
|
||||
Method {0} is not a varargs method.
|
||||
|
@ -2164,7 +2164,7 @@ public abstract class JCTree implements Tree, Cloneable, DiagnosticPosition {
|
||||
@DefinedBy(Api.COMPILER_TREE)
|
||||
public Kind getKind() { return Kind.INSTANCE_OF; }
|
||||
@DefinedBy(Api.COMPILER_TREE)
|
||||
public JCTree getType() { return pattern instanceof JCPattern ? pattern.hasTag(BINDINGPATTERN) ? ((JCBindingPattern) pattern).vartype : null : pattern; }
|
||||
public JCTree getType() { return pattern instanceof JCPattern ? pattern.hasTag(BINDINGPATTERN) ? ((JCBindingPattern) pattern).var.vartype : null : pattern; }
|
||||
|
||||
@Override @DefinedBy(Api.COMPILER_TREE)
|
||||
public JCPattern getPattern() {
|
||||
@ -2188,31 +2188,19 @@ public abstract class JCTree implements Tree, Cloneable, DiagnosticPosition {
|
||||
*/
|
||||
public static abstract class JCPattern extends JCTree
|
||||
implements PatternTree {
|
||||
public JCExpression constExpression() {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static class JCBindingPattern extends JCPattern
|
||||
implements BindingPatternTree {
|
||||
public Name name;
|
||||
public BindingSymbol symbol;
|
||||
public JCTree vartype;
|
||||
public JCVariableDecl var;
|
||||
|
||||
protected JCBindingPattern(Name name, BindingSymbol symbol, JCTree vartype) {
|
||||
this.name = name;
|
||||
this.symbol = symbol;
|
||||
this.vartype = vartype;
|
||||
}
|
||||
|
||||
@DefinedBy(Api.COMPILER_TREE)
|
||||
public Name getBinding() {
|
||||
return name;
|
||||
protected JCBindingPattern(JCVariableDecl var) {
|
||||
this.var = var;
|
||||
}
|
||||
|
||||
@Override @DefinedBy(Api.COMPILER_TREE)
|
||||
public Tree getType() {
|
||||
return vartype;
|
||||
public VariableTree getVariable() {
|
||||
return var;
|
||||
}
|
||||
|
||||
@Override
|
||||
@ -3211,7 +3199,7 @@ public abstract class JCTree implements Tree, Cloneable, DiagnosticPosition {
|
||||
JCBinary Binary(Tag opcode, JCExpression lhs, JCExpression rhs);
|
||||
JCTypeCast TypeCast(JCTree expr, JCExpression type);
|
||||
JCInstanceOf TypeTest(JCExpression expr, JCTree clazz);
|
||||
JCBindingPattern BindingPattern(Name name, JCTree vartype);
|
||||
JCBindingPattern BindingPattern(JCVariableDecl var);
|
||||
JCArrayAccess Indexed(JCExpression indexed, JCExpression index);
|
||||
JCFieldAccess Select(JCExpression selected, Name selector);
|
||||
JCIdent Ident(Name idname);
|
||||
|
@ -895,9 +895,7 @@ public class Pretty extends JCTree.Visitor {
|
||||
|
||||
public void visitBindingPattern(JCBindingPattern patt) {
|
||||
try {
|
||||
printExpr(patt.vartype);
|
||||
print(" ");
|
||||
print(patt.name);
|
||||
printExpr(patt.var);
|
||||
} catch (IOException e) {
|
||||
throw new UncheckedIOException(e);
|
||||
}
|
||||
|
@ -493,8 +493,8 @@ public class TreeCopier<P> implements TreeVisitor<JCTree,P> {
|
||||
@DefinedBy(Api.COMPILER_TREE)
|
||||
public JCTree visitBindingPattern(BindingPatternTree node, P p) {
|
||||
JCBindingPattern t = (JCBindingPattern) node;
|
||||
JCTree vartype = copy(t.vartype, p);
|
||||
return M.at(t.pos).BindingPattern(t.name, vartype);
|
||||
JCVariableDecl var = copy(t.var, p);
|
||||
return M.at(t.pos).BindingPattern(var);
|
||||
}
|
||||
|
||||
@DefinedBy(Api.COMPILER_TREE)
|
||||
|
@ -39,6 +39,7 @@ import com.sun.tools.javac.util.JCDiagnostic.DiagnosticPosition;
|
||||
|
||||
import static com.sun.tools.javac.code.Flags.*;
|
||||
import static com.sun.tools.javac.code.Kinds.Kind.*;
|
||||
import com.sun.tools.javac.code.Symbol.VarSymbol;
|
||||
import static com.sun.tools.javac.code.TypeTag.BOT;
|
||||
import static com.sun.tools.javac.tree.JCTree.Tag.*;
|
||||
import static com.sun.tools.javac.tree.JCTree.Tag.BLOCK;
|
||||
@ -535,7 +536,7 @@ public class TreeInfo {
|
||||
}
|
||||
case BINDINGPATTERN: {
|
||||
JCBindingPattern node = (JCBindingPattern)tree;
|
||||
return getStartPos(node.vartype);
|
||||
return getStartPos(node.var);
|
||||
}
|
||||
case ERRONEOUS: {
|
||||
JCErroneous node = (JCErroneous)tree;
|
||||
@ -926,8 +927,6 @@ public class TreeInfo {
|
||||
if (node.type != null)
|
||||
return node.type.tsym;
|
||||
return null;
|
||||
case BINDINGPATTERN:
|
||||
return ((JCBindingPattern) node).symbol;
|
||||
default:
|
||||
return null;
|
||||
}
|
||||
|
@ -476,8 +476,8 @@ public class TreeMaker implements JCTree.Factory {
|
||||
return tree;
|
||||
}
|
||||
|
||||
public JCBindingPattern BindingPattern(Name name, JCTree vartype) {
|
||||
JCBindingPattern tree = new JCBindingPattern(name, null, vartype);
|
||||
public JCBindingPattern BindingPattern(JCVariableDecl var) {
|
||||
JCBindingPattern tree = new JCBindingPattern(var);
|
||||
tree.pos = pos;
|
||||
return tree;
|
||||
}
|
||||
|
@ -304,8 +304,7 @@ public class TreeScanner extends Visitor {
|
||||
}
|
||||
|
||||
public void visitBindingPattern(JCBindingPattern tree) {
|
||||
if (tree.vartype != null)
|
||||
scan(tree.vartype);
|
||||
scan(tree.var);
|
||||
}
|
||||
|
||||
public void visitIndexed(JCArrayAccess tree) {
|
||||
|
@ -359,7 +359,7 @@ public class TreeTranslator extends JCTree.Visitor {
|
||||
}
|
||||
|
||||
public void visitBindingPattern(JCBindingPattern tree) {
|
||||
tree.vartype = translate(tree.vartype);
|
||||
tree.var = translate(tree.var);
|
||||
result = tree;
|
||||
}
|
||||
|
||||
|
0
test/langtools/tools/javac/T4881267-old.out
Normal file
0
test/langtools/tools/javac/T4881267-old.out
Normal file
@ -1,2 +1,2 @@
|
||||
T4881267.java:10:34: compiler.err.illegal.generic.type.for.instof
|
||||
T4881267.java:10:21: compiler.err.instanceof.reifiable.not.safe: java.lang.Object, T
|
||||
1 error
|
||||
|
@ -399,14 +399,14 @@ public class CombinationsTargetTest2 extends ClassfileTestHelper {
|
||||
case src7p: // (repeating) type annotations in use of instanceof with type test pattern
|
||||
/*
|
||||
* class Test10{
|
||||
* String data = "test";
|
||||
* Object data = "test";
|
||||
* boolean dataIsString = ( data instanceof @A @B @A @B String str);
|
||||
* }
|
||||
*/
|
||||
source = new String( source +
|
||||
"// " + src.description + "\n" +
|
||||
"class "+ testname + "{\n" +
|
||||
" String data = \"test\";\n" +
|
||||
" Object data = \"test\";\n" +
|
||||
" boolean dataIsString = ( data instanceof _As_ _Bs_ String str && str.isEmpty());\n" +
|
||||
"}\n").concat(sourceBase).replace("_OTHER_", annot2).replace("_As_",As).replace("_Bs_",Bs) +
|
||||
"\n\n";
|
||||
@ -455,7 +455,7 @@ public class CombinationsTargetTest2 extends ClassfileTestHelper {
|
||||
source = new String( source +
|
||||
"// " + src.description + "\n" +
|
||||
"class "+ testname + "{\n" +
|
||||
" String data = \"test\";\n" +
|
||||
" Object data = \"test\";\n" +
|
||||
" Boolean isString() { \n" +
|
||||
" if( data instanceof _As_ _Bs_ String str)\n" +
|
||||
" return true;\n" +
|
||||
|
@ -161,7 +161,6 @@ compiler.misc.locn.module_source_path # fragment uninter
|
||||
compiler.misc.locn.system_modules # fragment uninteresting in and of itself
|
||||
compiler.misc.locn.upgrade_module_path # fragment uninteresting in and of itself
|
||||
compiler.misc.inferred.do.not.conform.to.eq.bounds # hard to generate, could probably be removed
|
||||
compiler.err.feature.not.supported.in.source # Generated for using diamond before source 7
|
||||
|
||||
# The following are new module-related messages, that need new examples to be created
|
||||
compiler.err.duplicate.module.on.path
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2010, 2020, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
@ -21,14 +21,13 @@
|
||||
* questions.
|
||||
*/
|
||||
|
||||
// key: compiler.err.feature.not.supported.in.source.plural
|
||||
// key: compiler.misc.feature.reifiable.types.instanceof
|
||||
// key: compiler.warn.preview.feature.use.plural
|
||||
// options: --enable-preview -source ${jdk.version} -Xlint:preview
|
||||
// options: -source 15 -Xlint:-options
|
||||
|
||||
class PatternMatchingInstanceof {
|
||||
boolean m(I<String> i) {
|
||||
return i instanceof C<String>;
|
||||
}
|
||||
interface I<T> {}
|
||||
class C<T> implements I<T> {}
|
||||
import java.util.*;
|
||||
|
||||
class FeatureReifiableTypesInstanceof {
|
||||
List o;
|
||||
boolean b = (o instanceof List<String>);
|
||||
}
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
@ -21,11 +21,10 @@
|
||||
* questions.
|
||||
*/
|
||||
|
||||
// key: compiler.err.illegal.generic.type.for.instof
|
||||
// key: compiler.err.instanceof.pattern.no.subtype
|
||||
|
||||
import java.util.*;
|
||||
|
||||
class IllegalInstanceof {
|
||||
List o;
|
||||
boolean b = (o instanceof List<String>);
|
||||
class InstanceofPatternNoSubtype {
|
||||
boolean test(Object o) {
|
||||
return o instanceof Object obj;
|
||||
}
|
||||
}
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
@ -22,9 +22,6 @@
|
||||
*/
|
||||
|
||||
// key: compiler.err.instanceof.reifiable.not.safe
|
||||
// key: compiler.note.preview.filename
|
||||
// key: compiler.note.preview.recompile
|
||||
// options: --enable-preview -source ${jdk.version}
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2017, 2020, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
@ -22,9 +22,6 @@
|
||||
*/
|
||||
|
||||
// key: compiler.err.match.binding.exists
|
||||
// key: compiler.note.preview.filename
|
||||
// key: compiler.note.preview.recompile
|
||||
// options: --enable-preview -source ${jdk.version}
|
||||
|
||||
class MatchBindingExists {
|
||||
public void test(Object o1, Object o2) {
|
||||
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
@ -22,8 +22,8 @@
|
||||
*/
|
||||
|
||||
// key: compiler.misc.feature.pattern.matching.instanceof
|
||||
// key: compiler.warn.preview.feature.use
|
||||
// options: --enable-preview -source ${jdk.version} -Xlint:preview
|
||||
// key: compiler.err.feature.not.supported.in.source
|
||||
// options: -source 15 -Xlint:-options
|
||||
|
||||
class PatternMatchingInstanceof {
|
||||
boolean m(Object o) {
|
||||
|
@ -1,2 +1,2 @@
|
||||
InstanceOf2.java:12:48: compiler.err.illegal.generic.type.for.instof
|
||||
InstanceOf2.java:12:29: compiler.err.instanceof.reifiable.not.safe: java.lang.Class<compiler.misc.type.captureof: 1, ? extends InstanceOf2>, java.lang.Class<InstanceOf2>
|
||||
1 error
|
||||
|
@ -4,7 +4,7 @@
|
||||
* @summary the type in an instanceof expression must be reifiable
|
||||
* @author seligman
|
||||
*
|
||||
* @compile/fail/ref=InstanceOf3.out -XDrawDiagnostics InstanceOf3.java
|
||||
* @compile/fail/ref=InstanceOf3.out -XDrawDiagnostics -source 15 -Xlint:-options InstanceOf3.java
|
||||
*/
|
||||
|
||||
public class InstanceOf3 {
|
||||
|
@ -1,2 +1,2 @@
|
||||
InstanceOf3.java:12:48: compiler.err.illegal.generic.type.for.instof
|
||||
InstanceOf3.java:12:32: compiler.err.feature.not.supported.in.source.plural: (compiler.misc.feature.reifiable.types.instanceof), 15, 16
|
||||
1 error
|
||||
|
@ -0,0 +1,5 @@
|
||||
BadTest.java:19:50: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: List<java.lang.Object>, List<Cell<java.lang.String>>)
|
||||
BadTest.java:23:48: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: List<java.lang.Object>, List<java.lang.String>)
|
||||
BadTest.java:24:54: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: List<java.lang.Object>, List<Cell<java.lang.String>>)
|
||||
BadTest.java:26:38: compiler.err.feature.not.supported.in.source.plural: (compiler.misc.feature.reifiable.types.instanceof), 15, 16
|
||||
4 errors
|
@ -3,6 +3,7 @@
|
||||
* @summary Negative regression test from odersky
|
||||
* @author odersky
|
||||
*
|
||||
* @compile/fail/ref=BadTest-old.out -XDrawDiagnostics -source 15 -Xlint:-options BadTest.java
|
||||
* @compile/fail/ref=BadTest.out -XDrawDiagnostics BadTest.java
|
||||
*/
|
||||
|
||||
|
@ -1,5 +1,5 @@
|
||||
BadTest.java:18:50: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: List<java.lang.Object>, List<Cell<java.lang.String>>)
|
||||
BadTest.java:22:48: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: List<java.lang.Object>, List<java.lang.String>)
|
||||
BadTest.java:23:54: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: List<java.lang.Object>, List<Cell<java.lang.String>>)
|
||||
BadTest.java:25:53: compiler.err.illegal.generic.type.for.instof
|
||||
BadTest.java:19:50: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: List<java.lang.Object>, List<Cell<java.lang.String>>)
|
||||
BadTest.java:23:48: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: List<java.lang.Object>, List<java.lang.String>)
|
||||
BadTest.java:24:54: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: List<java.lang.Object>, List<Cell<java.lang.String>>)
|
||||
BadTest.java:26:35: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: List<java.lang.Object>, List<java.lang.String>)
|
||||
4 errors
|
||||
|
@ -2,7 +2,7 @@
|
||||
* @test /nodynamiccopyright/
|
||||
* @bug 8231827
|
||||
* @summary Clashing bindings are reported correctly
|
||||
* @compile/fail/ref=BindingsExistTest.out -XDrawDiagnostics --enable-preview -source ${jdk.version} BindingsExistTest.java
|
||||
* @compile/fail/ref=BindingsExistTest.out -XDrawDiagnostics BindingsExistTest.java
|
||||
*/
|
||||
public class BindingsExistTest {
|
||||
public void t(Object o1, Object o2) {
|
||||
|
@ -4,6 +4,4 @@ BindingsExistTest.java:16:35: compiler.err.already.defined: kindname.variable, k
|
||||
BindingsExistTest.java:19:34: compiler.err.already.defined: kindname.variable, s2, kindname.method, t(java.lang.Object,java.lang.Object)
|
||||
BindingsExistTest.java:22:20: compiler.err.already.defined: kindname.variable, s3, kindname.method, t(java.lang.Object,java.lang.Object)
|
||||
BindingsExistTest.java:28:16: compiler.err.already.defined: kindname.variable, s4, kindname.method, t(java.lang.Object,java.lang.Object)
|
||||
- compiler.note.preview.filename: BindingsExistTest.java
|
||||
- compiler.note.preview.recompile
|
||||
6 errors
|
||||
|
@ -25,8 +25,8 @@
|
||||
* @test
|
||||
* @bug 8231827
|
||||
* @summary Basic tests for bindings from instanceof
|
||||
* @compile --enable-preview -source ${jdk.version} BindingsTest1.java
|
||||
* @run main/othervm --enable-preview BindingsTest1
|
||||
* @compile BindingsTest1.java
|
||||
* @run main BindingsTest1
|
||||
*/
|
||||
|
||||
public class BindingsTest1 {
|
||||
|
@ -2,7 +2,7 @@
|
||||
* @test /nodynamiccopyright/
|
||||
* @bug 8231827
|
||||
* @summary Basic tests for bindings from instanceof - tests for merging pattern variables
|
||||
* @compile/fail/ref=BindingsTest1Merging.out -XDrawDiagnostics --enable-preview -source ${jdk.version} BindingsTest1Merging.java
|
||||
* @compile/fail/ref=BindingsTest1Merging.out -XDrawDiagnostics BindingsTest1Merging.java
|
||||
*/
|
||||
|
||||
public class BindingsTest1Merging {
|
||||
|
@ -7,6 +7,4 @@ BindingsTest1Merging.java:44:21: compiler.err.match.binding.exists
|
||||
BindingsTest1Merging.java:50:36: compiler.err.match.binding.exists
|
||||
BindingsTest1Merging.java:56:39: compiler.err.match.binding.exists
|
||||
BindingsTest1Merging.java:62:42: compiler.err.match.binding.exists
|
||||
- compiler.note.preview.filename: BindingsTest1Merging.java
|
||||
- compiler.note.preview.recompile
|
||||
9 errors
|
||||
|
@ -2,7 +2,7 @@
|
||||
* @test /nodynamiccopyright/
|
||||
* @bug 8231827
|
||||
* @summary Ensure that scopes arising from conditionalExpressions are handled corrected.
|
||||
* @compile/fail/ref=BindingsTest2.out -XDrawDiagnostics -XDshould-stop.at=FLOW --enable-preview -source ${jdk.version} BindingsTest2.java
|
||||
* @compile/fail/ref=BindingsTest2.out -XDrawDiagnostics -XDshould-stop.at=FLOW BindingsTest2.java
|
||||
*/
|
||||
public class BindingsTest2 {
|
||||
public static boolean Ktrue() { return true; }
|
||||
|
@ -49,6 +49,4 @@ BindingsTest2.java:241:17: compiler.err.cant.resolve.location: kindname.variable
|
||||
BindingsTest2.java:135:17: compiler.err.unreachable.stmt
|
||||
BindingsTest2.java:160:17: compiler.err.unreachable.stmt
|
||||
BindingsTest2.java:185:17: compiler.err.unreachable.stmt
|
||||
- compiler.note.preview.filename: BindingsTest2.java
|
||||
- compiler.note.preview.recompile
|
||||
51 errors
|
@ -95,10 +95,7 @@ public class BreakAndLoops extends ComboInstance<BreakAndLoops> {
|
||||
case "NESTED" -> brk;
|
||||
case "BODY" -> innerLabel;
|
||||
default -> throw new UnsupportedOperationException(pname);
|
||||
})
|
||||
.withOption("--enable-preview")
|
||||
.withOption("-source")
|
||||
.withOption(String.valueOf(Runtime.version().feature()));
|
||||
});
|
||||
|
||||
task.generate(result -> {
|
||||
boolean shouldPass;
|
||||
|
@ -2,7 +2,7 @@
|
||||
* @test /nodynamicopyright/
|
||||
* @bug 8231827
|
||||
* @summary Match which involves a cast conversion
|
||||
* @compile/fail/ref=CastConversionMatch.out -XDrawDiagnostics --enable-preview -source ${jdk.version} CastConversionMatch.java
|
||||
* @compile/fail/ref=CastConversionMatch.out -XDrawDiagnostics CastConversionMatch.java
|
||||
*/
|
||||
|
||||
public class CastConversionMatch {
|
||||
|
@ -1,4 +1,2 @@
|
||||
CastConversionMatch.java:11:26: compiler.err.type.found.req: int, (compiler.misc.type.req.class.array)
|
||||
- compiler.note.preview.filename: CastConversionMatch.java
|
||||
- compiler.note.preview.recompile
|
||||
1 error
|
||||
|
@ -85,10 +85,7 @@ public class ConditionalTest extends ComboInstance<ConditionalTest> {
|
||||
case "TRUE" -> trueSec;
|
||||
case "FALSE" -> falseSec;
|
||||
default -> throw new UnsupportedOperationException(pname);
|
||||
})
|
||||
.withOption("--enable-preview")
|
||||
.withOption("-source")
|
||||
.withOption(String.valueOf(Runtime.version().feature()));
|
||||
});
|
||||
|
||||
task.analyze(result -> {
|
||||
boolean shouldPass;
|
||||
|
@ -2,7 +2,7 @@
|
||||
* @test /nodynamiccopyright/
|
||||
* @bug 8231827
|
||||
* @summary Basic pattern bindings scope test
|
||||
* @compile/fail/ref=DuplicateBindingTest.out -XDrawDiagnostics --enable-preview -source ${jdk.version} DuplicateBindingTest.java
|
||||
* @compile/fail/ref=DuplicateBindingTest.out -XDrawDiagnostics DuplicateBindingTest.java
|
||||
*/
|
||||
|
||||
public class DuplicateBindingTest {
|
||||
@ -10,17 +10,17 @@ public class DuplicateBindingTest {
|
||||
int f;
|
||||
|
||||
public static boolean main(String[] args) {
|
||||
Object o1 = "";
|
||||
Object o2 = "";
|
||||
|
||||
if (args != null) {
|
||||
int s;
|
||||
if (args[0] instanceof String s) { // NOT OK. Redef same scope.
|
||||
if (o1 instanceof String s) { // NOT OK. Redef same scope.
|
||||
}
|
||||
if (args[0] instanceof String f) { // OK to redef field.
|
||||
if (o1 instanceof String f) { // OK to redef field.
|
||||
}
|
||||
}
|
||||
|
||||
Object o1 = "";
|
||||
Object o2 = "";
|
||||
|
||||
if (o1 instanceof String s && o2 instanceof String s) {} //error - already in scope on RHS (in scope due to LHS when true)
|
||||
if (o1 instanceof String s && !(o2 instanceof String s)) {} //error - already in scope on RHS (in scope due to LHS when true)
|
||||
|
@ -1,4 +1,4 @@
|
||||
DuplicateBindingTest.java:16:43: compiler.err.already.defined: kindname.variable, s, kindname.method, main(java.lang.String[])
|
||||
DuplicateBindingTest.java:18:38: compiler.err.already.defined: kindname.variable, s, kindname.method, main(java.lang.String[])
|
||||
DuplicateBindingTest.java:25:60: compiler.err.match.binding.exists
|
||||
DuplicateBindingTest.java:26:62: compiler.err.match.binding.exists
|
||||
DuplicateBindingTest.java:27:39: compiler.err.match.binding.exists
|
||||
@ -22,6 +22,4 @@ DuplicateBindingTest.java:50:102: compiler.err.match.binding.exists
|
||||
DuplicateBindingTest.java:51:73: compiler.err.match.binding.exists
|
||||
DuplicateBindingTest.java:54:69: compiler.err.cant.resolve.location: kindname.variable, s, , , (compiler.misc.location: kindname.class, DuplicateBindingTest, null)
|
||||
DuplicateBindingTest.java:55:44: compiler.err.cant.resolve.location: kindname.variable, s, , , (compiler.misc.location: kindname.class, DuplicateBindingTest, null)
|
||||
- compiler.note.preview.filename: DuplicateBindingTest.java
|
||||
- compiler.note.preview.recompile
|
||||
24 errors
|
||||
|
@ -2,7 +2,7 @@
|
||||
* @test /nodynamiccopyright/
|
||||
* @bug 8187420 8231827
|
||||
* @summary Error message mentions relevant types transposed
|
||||
* @compile/fail/ref=EnsureTypesOrderTest.out -XDrawDiagnostics --enable-preview -source ${jdk.version} EnsureTypesOrderTest.java
|
||||
* @compile/fail/ref=EnsureTypesOrderTest.out -XDrawDiagnostics EnsureTypesOrderTest.java
|
||||
*/
|
||||
public class EnsureTypesOrderTest {
|
||||
public static void main(String [] args) {
|
||||
|
@ -1,4 +1,2 @@
|
||||
EnsureTypesOrderTest.java:9:13: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: java.lang.String[], java.lang.String)
|
||||
- compiler.note.preview.filename: EnsureTypesOrderTest.java
|
||||
- compiler.note.preview.recompile
|
||||
1 error
|
@ -25,8 +25,8 @@
|
||||
* @test
|
||||
* @bug 8231827
|
||||
* @summary All example code from "Pattern Matching for Java" document, released April 2017, adjusted to current state (no switches, etc)
|
||||
* @compile --enable-preview -source ${jdk.version} ExamplesFromProposal.java
|
||||
* @run main/othervm --enable-preview ExamplesFromProposal
|
||||
* @compile ExamplesFromProposal.java
|
||||
* @run main ExamplesFromProposal
|
||||
*/
|
||||
|
||||
interface Node {
|
||||
|
@ -2,7 +2,7 @@
|
||||
* @test /nodynamiccopyright/
|
||||
* @bug 8231827
|
||||
* @summary Ensure that in type test patterns, the predicate is not trivially provable false.
|
||||
* @compile/fail/ref=ImpossibleTypeTest.out -XDrawDiagnostics --enable-preview -source ${jdk.version} ImpossibleTypeTest.java
|
||||
* @compile/fail/ref=ImpossibleTypeTest.out -XDrawDiagnostics ImpossibleTypeTest.java
|
||||
*/
|
||||
public class ImpossibleTypeTest {
|
||||
|
||||
|
@ -1,5 +1,3 @@
|
||||
ImpossibleTypeTest.java:14:13: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: java.lang.Integer, java.lang.String)
|
||||
ImpossibleTypeTest.java:17:26: compiler.err.cant.resolve.location: kindname.class, Undefined, , , (compiler.misc.location: kindname.class, ImpossibleTypeTest, null)
|
||||
- compiler.note.preview.filename: ImpossibleTypeTest.java
|
||||
- compiler.note.preview.recompile
|
||||
2 errors
|
||||
|
@ -26,8 +26,8 @@
|
||||
* @bug 8231827
|
||||
* @summary Ensure the LV table entries are generated for bindings
|
||||
* @modules jdk.jdeps/com.sun.tools.classfile
|
||||
* @compile -g --enable-preview -source ${jdk.version} LocalVariableTable.java
|
||||
* @run main/othervm --enable-preview LocalVariableTable
|
||||
* @compile -g LocalVariableTable.java
|
||||
* @run main LocalVariableTable
|
||||
*/
|
||||
|
||||
import java.io.*;
|
||||
|
@ -2,7 +2,7 @@
|
||||
* @test /nodynamiccopyright/
|
||||
* @bug 8231827
|
||||
* @summary Basic pattern bindings scope test
|
||||
* @compile/fail/ref=MatchBindingScopeTest.out -XDrawDiagnostics --enable-preview -source ${jdk.version} MatchBindingScopeTest.java
|
||||
* @compile/fail/ref=MatchBindingScopeTest.out -XDrawDiagnostics MatchBindingScopeTest.java
|
||||
*/
|
||||
public class MatchBindingScopeTest {
|
||||
|
||||
|
@ -11,6 +11,4 @@ MatchBindingScopeTest.java:56:48: compiler.err.cant.resolve.location: kindname.v
|
||||
MatchBindingScopeTest.java:57:32: compiler.err.cant.resolve.location: kindname.variable, j, , , (compiler.misc.location: kindname.class, MatchBindingScopeTest, null)
|
||||
MatchBindingScopeTest.java:62:23: compiler.err.cant.resolve.location: kindname.variable, j, , , (compiler.misc.location: kindname.class, MatchBindingScopeTest, null)
|
||||
MatchBindingScopeTest.java:65:23: compiler.err.cant.resolve.location: kindname.variable, j, , , (compiler.misc.location: kindname.class, MatchBindingScopeTest, null)
|
||||
- compiler.note.preview.filename: MatchBindingScopeTest.java
|
||||
- compiler.note.preview.recompile
|
||||
13 errors
|
22
test/langtools/tools/javac/patterns/NoSubtypeCheck.java
Normal file
22
test/langtools/tools/javac/patterns/NoSubtypeCheck.java
Normal file
@ -0,0 +1,22 @@
|
||||
/*
|
||||
* @test /nodynamiccopyright/
|
||||
* @bug 8250625
|
||||
* @summary Verify pattern matching test which is always true produces an error
|
||||
* @compile/fail/ref=NoSubtypeCheck.out -XDrawDiagnostics NoSubtypeCheck.java
|
||||
*/
|
||||
public class NoSubtypeCheck {
|
||||
|
||||
public static void main(Object o, String s, List<String> l) {
|
||||
boolean b1 = o instanceof Object v1;
|
||||
boolean b2 = o instanceof String v2;
|
||||
boolean b3 = s instanceof Object v3;
|
||||
boolean b4 = s instanceof String v4;
|
||||
boolean b5 = l instanceof List<String> v5;
|
||||
boolean b6 = l instanceof List2<String> v6;
|
||||
boolean b7 = undef instanceof String v7;
|
||||
boolean b8 = o instanceof Undef v7;
|
||||
}
|
||||
|
||||
public interface List<T> {}
|
||||
public interface List2<T> extends List<T> {}
|
||||
}
|
7
test/langtools/tools/javac/patterns/NoSubtypeCheck.out
Normal file
7
test/langtools/tools/javac/patterns/NoSubtypeCheck.out
Normal file
@ -0,0 +1,7 @@
|
||||
NoSubtypeCheck.java:10:24: compiler.err.instanceof.pattern.no.subtype: java.lang.Object, java.lang.Object
|
||||
NoSubtypeCheck.java:12:24: compiler.err.instanceof.pattern.no.subtype: java.lang.Object, java.lang.String
|
||||
NoSubtypeCheck.java:13:24: compiler.err.instanceof.pattern.no.subtype: java.lang.String, java.lang.String
|
||||
NoSubtypeCheck.java:14:24: compiler.err.instanceof.pattern.no.subtype: NoSubtypeCheck.List<java.lang.String>, NoSubtypeCheck.List<java.lang.String>
|
||||
NoSubtypeCheck.java:16:22: compiler.err.cant.resolve.location: kindname.variable, undef, , , (compiler.misc.location: kindname.class, NoSubtypeCheck, null)
|
||||
NoSubtypeCheck.java:17:35: compiler.err.cant.resolve.location: kindname.class, Undef, , , (compiler.misc.location: kindname.class, NoSubtypeCheck, null)
|
||||
6 errors
|
@ -27,8 +27,8 @@
|
||||
* @summary Verify there are no unnecessary checkcasts and conditions generated
|
||||
* for the pattern matching in instanceof.
|
||||
* @modules jdk.jdeps/com.sun.tools.classfile
|
||||
* @compile --enable-preview -source ${jdk.version} NoUnnecessaryCast.java
|
||||
* @run main/othervm --enable-preview NoUnnecessaryCast
|
||||
* @compile NoUnnecessaryCast.java
|
||||
* @run main NoUnnecessaryCast
|
||||
*/
|
||||
|
||||
import java.io.File;
|
||||
|
@ -25,8 +25,7 @@
|
||||
* @test
|
||||
* @bug 8231827
|
||||
* @summary Testing pattern matching against the null constant
|
||||
* @compile --enable-preview -source ${jdk.version} NullsInPatterns.java
|
||||
* @run main/othervm --enable-preview NullsInPatterns
|
||||
* @compile/fail/ref=NullsInPatterns.out -XDrawDiagnostics NullsInPatterns.java
|
||||
*/
|
||||
import java.util.List;
|
||||
|
||||
|
3
test/langtools/tools/javac/patterns/NullsInPatterns.out
Normal file
3
test/langtools/tools/javac/patterns/NullsInPatterns.out
Normal file
@ -0,0 +1,3 @@
|
||||
NullsInPatterns.java:35:18: compiler.err.instanceof.pattern.no.subtype: java.util.List, compiler.misc.type.null
|
||||
NullsInPatterns.java:46:18: compiler.err.instanceof.pattern.no.subtype: java.util.List<?>, compiler.misc.type.null
|
||||
2 errors
|
@ -26,7 +26,7 @@
|
||||
* @bug 8231827
|
||||
* @summary Check proper positions.
|
||||
* @build PatternMatchPosTest
|
||||
* @compile/ref=PatternMatchPosTest.out -processor PatternMatchPosTest -Xlint:unchecked -XDrawDiagnostics --enable-preview -source ${jdk.version} PatternMatchPosTestData.java
|
||||
* @compile/ref=PatternMatchPosTest.out -processor PatternMatchPosTest -Xlint:unchecked -XDrawDiagnostics PatternMatchPosTestData.java
|
||||
*/
|
||||
|
||||
import java.io.IOException;
|
||||
@ -85,8 +85,10 @@ public class PatternMatchPosTest extends AbstractProcessor {
|
||||
if (print) {
|
||||
int start = (int) sp.getStartPosition(dataPath.getCompilationUnit(), tree);
|
||||
int end = (int) sp.getEndPosition(dataPath.getCompilationUnit(), tree);
|
||||
processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE,
|
||||
text.substring(start, end));
|
||||
if (start != (-1) || end != (-1)) {
|
||||
processingEnv.getMessager().printMessage(Diagnostic.Kind.NOTE,
|
||||
text.substring(start, end));
|
||||
}
|
||||
}
|
||||
return super.scan(tree, p);
|
||||
}
|
||||
|
@ -2,13 +2,13 @@
|
||||
- compiler.note.proc.messager: o instanceof String s
|
||||
- compiler.note.proc.messager: o
|
||||
- compiler.note.proc.messager: String s
|
||||
- compiler.note.proc.messager: String s
|
||||
- compiler.note.proc.messager: String
|
||||
- compiler.note.proc.messager: (o instanceof java.lang.String s)
|
||||
- compiler.note.proc.messager: o instanceof java.lang.String s
|
||||
- compiler.note.proc.messager: o
|
||||
- compiler.note.proc.messager: java.lang.String s
|
||||
- compiler.note.proc.messager: java.lang.String s
|
||||
- compiler.note.proc.messager: java.lang.String
|
||||
- compiler.note.proc.messager: java.lang
|
||||
- compiler.note.proc.messager: java
|
||||
- compiler.note.preview.filename: PatternMatchPosTestData.java
|
||||
- compiler.note.preview.recompile
|
||||
- compiler.note.proc.messager: java
|
@ -25,8 +25,8 @@
|
||||
* @test
|
||||
* @bug 8231827
|
||||
* @summary Basic pattern test
|
||||
* @compile --enable-preview -source ${jdk.version} PatternTypeTest2.java
|
||||
* @run main/othervm --enable-preview PatternTypeTest2
|
||||
* @compile PatternTypeTest2.java
|
||||
* @run main PatternTypeTest2
|
||||
*/
|
||||
public class PatternTypeTest2 {
|
||||
|
||||
|
@ -1,16 +0,0 @@
|
||||
/*
|
||||
* @test /nodynamiccopyright/
|
||||
* @bug 8231827
|
||||
* @summary Ensure that in type test patterns, the predicate is not trivially provable false.
|
||||
* @compile/fail/ref=PatternVariablesAreFinal.out -XDrawDiagnostics --enable-preview -source ${jdk.version} PatternVariablesAreFinal.java
|
||||
*/
|
||||
public class PatternVariablesAreFinal {
|
||||
public static void main(String[] args) {
|
||||
Object o = 32;
|
||||
if (o instanceof String s) {
|
||||
s = "hello again";
|
||||
System.out.println(s);
|
||||
}
|
||||
System.out.println("test complete");
|
||||
}
|
||||
}
|
@ -1,4 +0,0 @@
|
||||
PatternVariablesAreFinal.java:11:13: compiler.err.pattern.binding.may.not.be.assigned: s
|
||||
- compiler.note.preview.filename: PatternVariablesAreFinal.java
|
||||
- compiler.note.preview.recompile
|
||||
1 error
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2017, 2020, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
@ -21,15 +21,24 @@
|
||||
* questions.
|
||||
*/
|
||||
|
||||
// key: compiler.err.pattern.binding.may.not.be.assigned
|
||||
// key: compiler.note.preview.filename
|
||||
// key: compiler.note.preview.recompile
|
||||
// options: --enable-preview -source ${jdk.version}
|
||||
|
||||
class ResourceMayNotBeAssigned {
|
||||
void m(Object o) {
|
||||
/*
|
||||
* @test
|
||||
* @bug 8231827
|
||||
* @summary Pattern variables are non-final.
|
||||
* @compile/fail/ref=PatternVariablesAreNonFinal.out -XDrawDiagnostics PatternVariablesAreNonFinal.java
|
||||
*/
|
||||
public class PatternVariablesAreNonFinal {
|
||||
public static void main(String[] args) {
|
||||
Object o = 32;
|
||||
if (o instanceof String s) {
|
||||
s = "";
|
||||
s = "hello again";
|
||||
new Runnable() {
|
||||
@Override
|
||||
public void run() {
|
||||
System.err.println(s);
|
||||
}
|
||||
};
|
||||
}
|
||||
System.out.println("test complete");
|
||||
}
|
||||
}
|
@ -0,0 +1,2 @@
|
||||
PatternVariablesAreNonFinal.java:38:40: compiler.err.cant.ref.non.effectively.final.var: s, (compiler.misc.inner.cls)
|
||||
1 error
|
@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright (c) 2017, 2019, Oracle and/or its affiliates. All rights reserved.
|
||||
* Copyright (c) 2017, 2020, Oracle and/or its affiliates. All rights reserved.
|
||||
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
||||
*
|
||||
* This code is free software; you can redistribute it and/or modify it
|
||||
@ -24,11 +24,11 @@
|
||||
/*
|
||||
* @test
|
||||
* @bug 8231827
|
||||
* @summary Pattern variables are final so should be allowed to be referenced in an inner class
|
||||
* @compile --enable-preview -source ${jdk.version} PatternVariablesAreFinal2.java
|
||||
* @run main/othervm --enable-preview PatternVariablesAreFinal2
|
||||
* @summary Pattern variables can be effectivelly final so should be allowed to be referenced in an inner class
|
||||
* @compile PatternVariablesAreNonFinal2.java
|
||||
* @run main PatternVariablesAreNonFinal2
|
||||
*/
|
||||
public class PatternVariablesAreFinal2 {
|
||||
public class PatternVariablesAreNonFinal2 {
|
||||
public static void main(String[] args) {
|
||||
Object o = "42";
|
||||
if (o instanceof String s) {
|
||||
@ -36,5 +36,9 @@ public class PatternVariablesAreFinal2 {
|
||||
void run() { System.err.println(s); }
|
||||
}.run();
|
||||
}
|
||||
if (o instanceof String s) {
|
||||
s = "hello again";
|
||||
System.out.println(s);
|
||||
}
|
||||
}
|
||||
}
|
@ -105,7 +105,7 @@ public class PatternsSimpleVisitorTest {
|
||||
|
||||
StringWriter out = new StringWriter();
|
||||
JavacTask ct = (JavacTask) tool.getTask(out, null, noErrors,
|
||||
List.of("--enable-preview", "-source", Integer.toString(Runtime.version().feature())), null,
|
||||
null, null,
|
||||
Arrays.asList(new MyFileObject(code)));
|
||||
return ct.parse().iterator().next();
|
||||
}
|
||||
|
@ -2,7 +2,7 @@
|
||||
* @test /nodynamiccopyright/
|
||||
* @bug 8231827
|
||||
* @summary Verify behavior w.r.t. non-reifiable types and type test patterns in instanceof
|
||||
* @compile/fail/ref=Reifiable.out --enable-preview -source ${jdk.version} -XDrawDiagnostics Reifiable.java
|
||||
* @compile/fail/ref=Reifiable.out -XDrawDiagnostics Reifiable.java
|
||||
*/
|
||||
|
||||
public class Reifiable implements ReifiableI {
|
||||
|
@ -2,6 +2,4 @@ Reifiable.java:10:16: compiler.err.instanceof.reifiable.not.safe: java.lang.Obje
|
||||
Reifiable.java:12:16: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: Reifiable.List<java.lang.String>, Reifiable.ListImpl<Reifiable>)
|
||||
Reifiable.java:13:39: compiler.err.not.within.bounds: java.lang.String, T
|
||||
Reifiable.java:14:16: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: Reifiable.List<Reifiable>, Reifiable.Unrelated<Reifiable>)
|
||||
- compiler.note.preview.filename: Reifiable.java
|
||||
- compiler.note.preview.recompile
|
||||
4 errors
|
||||
|
@ -1,7 +1,5 @@
|
||||
ReifiableOld.java:12:37: compiler.err.illegal.generic.type.for.instof
|
||||
ReifiableOld.java:13:38: compiler.err.illegal.generic.type.for.instof
|
||||
ReifiableOld.java:14:38: compiler.err.illegal.generic.type.for.instof
|
||||
ReifiableOld.java:15:38: compiler.err.illegal.generic.type.for.instof
|
||||
ReifiableOld.java:15:39: compiler.err.not.within.bounds: java.lang.String, T
|
||||
ReifiableOld.java:16:39: compiler.err.illegal.generic.type.for.instof
|
||||
6 errors
|
||||
ReifiableOld.java:11:18: compiler.err.feature.not.supported.in.source.plural: (compiler.misc.feature.reifiable.types.instanceof), 15, 16
|
||||
ReifiableOld.java:13:16: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: ReifiableOld.List<java.lang.String>, ReifiableOld.ListImpl<ReifiableOld>)
|
||||
ReifiableOld.java:14:39: compiler.err.not.within.bounds: java.lang.String, T
|
||||
ReifiableOld.java:15:16: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: ReifiableOld.List<ReifiableOld>, ReifiableOld.Unrelated<ReifiableOld>)
|
||||
4 errors
|
||||
|
@ -2,9 +2,8 @@
|
||||
* @test /nodynamiccopyright/
|
||||
* @bug 8231827
|
||||
* @summary Verify behavior w.r.t. non-reifiable types in instanceof
|
||||
* @compile/fail/ref=ReifiableOld-old.out -source 13 -Xlint:-options -XDrawDiagnostics ReifiableOld.java
|
||||
* @compile/fail/ref=ReifiableOld-old.out -source ${jdk.version} -XDrawDiagnostics ReifiableOld.java
|
||||
* @compile/fail/ref=ReifiableOld.out --enable-preview -source ${jdk.version} -XDrawDiagnostics ReifiableOld.java
|
||||
* @compile/fail/ref=ReifiableOld-old.out -source 15 -Xlint:-options -XDrawDiagnostics ReifiableOld.java
|
||||
* @compile/fail/ref=ReifiableOld.out -XDrawDiagnostics ReifiableOld.java
|
||||
*/
|
||||
|
||||
public class ReifiableOld implements ReifiableOldI {
|
||||
|
@ -1,7 +1,5 @@
|
||||
ReifiableOld.java:12:16: compiler.err.instanceof.reifiable.not.safe: java.lang.Object, ReifiableOld.ListImpl<ReifiableOld>
|
||||
ReifiableOld.java:14:16: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: ReifiableOld.List<java.lang.String>, ReifiableOld.ListImpl<ReifiableOld>)
|
||||
ReifiableOld.java:15:39: compiler.err.not.within.bounds: java.lang.String, T
|
||||
ReifiableOld.java:16:16: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: ReifiableOld.List<ReifiableOld>, ReifiableOld.Unrelated<ReifiableOld>)
|
||||
- compiler.note.preview.filename: ReifiableOld.java
|
||||
- compiler.note.preview.recompile
|
||||
ReifiableOld.java:11:16: compiler.err.instanceof.reifiable.not.safe: java.lang.Object, ReifiableOld.ListImpl<ReifiableOld>
|
||||
ReifiableOld.java:13:16: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: ReifiableOld.List<java.lang.String>, ReifiableOld.ListImpl<ReifiableOld>)
|
||||
ReifiableOld.java:14:39: compiler.err.not.within.bounds: java.lang.String, T
|
||||
ReifiableOld.java:15:16: compiler.err.prob.found.req: (compiler.misc.inconvertible.types: ReifiableOld.List<ReifiableOld>, ReifiableOld.Unrelated<ReifiableOld>)
|
||||
4 errors
|
||||
|
@ -2,7 +2,7 @@
|
||||
* @test /nodynamiccopyright/
|
||||
* @bug 8187429 8231827
|
||||
* @summary Missing unchecked conversion warning
|
||||
* @compile/fail/ref=UncheckedWarningOnMatchesTest.out -Xlint:unchecked -Werror -XDrawDiagnostics --enable-preview -source ${jdk.version} UncheckedWarningOnMatchesTest.java
|
||||
* @compile/fail/ref=UncheckedWarningOnMatchesTest.out -Xlint:unchecked -Werror -XDrawDiagnostics UncheckedWarningOnMatchesTest.java
|
||||
*/
|
||||
import java.util.ArrayList;
|
||||
|
||||
|
@ -1,4 +1,2 @@
|
||||
UncheckedWarningOnMatchesTest.java:14:13: compiler.err.instanceof.reifiable.not.safe: java.lang.Object, java.util.ArrayList<java.lang.Integer>
|
||||
- compiler.note.preview.filename: UncheckedWarningOnMatchesTest.java
|
||||
- compiler.note.preview.recompile
|
||||
1 error
|
||||
|
@ -98,10 +98,7 @@ public class ScopeTest extends JavacTemplateTestBase {
|
||||
}
|
||||
|
||||
private void assertOK(String block) {
|
||||
String sourceVersion = Integer.toString(Runtime.version().feature());
|
||||
|
||||
reset();
|
||||
addCompileOptions("--enable-preview", "-source", sourceVersion);
|
||||
program(block);
|
||||
try {
|
||||
compile();
|
||||
@ -113,10 +110,7 @@ public class ScopeTest extends JavacTemplateTestBase {
|
||||
}
|
||||
|
||||
private void assertFail(String expectedDiag, String block) {
|
||||
String sourceVersion = Integer.toString(Runtime.version().feature());
|
||||
|
||||
reset();
|
||||
addCompileOptions("--enable-preview", "-source", sourceVersion);
|
||||
program(block);
|
||||
try {
|
||||
compile();
|
||||
|
@ -105,20 +105,20 @@ public class TestBindingVariable extends JavacTestingAbstractProcessor implement
|
||||
}
|
||||
@Override
|
||||
public Void visitBindingPattern(BindingPatternTree node, Void p) {
|
||||
handleCurrentTreeAsBindingVar();
|
||||
handleTreeAsBindingVar(new TreePath(getCurrentPath(), node.getVariable()));
|
||||
return super.visitBindingPattern(node, p);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Void visitIdentifier(IdentifierTree node, Void p) {
|
||||
if (node.getName().contentEquals("bindingVar")) {
|
||||
handleCurrentTreeAsBindingVar();
|
||||
handleTreeAsBindingVar(getCurrentPath());
|
||||
}
|
||||
return super.visitIdentifier(node, p);
|
||||
}
|
||||
|
||||
private void handleCurrentTreeAsBindingVar() {
|
||||
Element element = trees.getElement(getCurrentPath());
|
||||
private void handleTreeAsBindingVar(TreePath tp) {
|
||||
Element element = trees.getElement(tp);
|
||||
|
||||
System.out.println("Name: " + element.getSimpleName() +
|
||||
"\tKind: " + element.getKind());
|
||||
|
Loading…
Reference in New Issue
Block a user