Skip to content
Permalink
Browse files
8211148: var in implicit lambdas shouldn't be accepted for source < 11
Backport-of: 2589e97
  • Loading branch information
GoeLin committed Oct 11, 2021
1 parent 65cab1e commit 84fc3b73f9aa8e8ba916d7057b6120929d6a1e2e
@@ -182,6 +182,7 @@ public Target requiredTarget() {
UNDERSCORE_IDENTIFIER(MIN, JDK8),
PRIVATE_INTERFACE_METHODS(JDK9, Fragments.FeaturePrivateIntfMethods, DiagKind.PLURAL),
LOCAL_VARIABLE_TYPE_INFERENCE(JDK10),
VAR_SYNTAX_IMPLICIT_LAMBDAS(JDK11, Fragments.FeatureVarSyntaxInImplicitLambda, DiagKind.PLURAL),
IMPORT_ON_DEMAND_OBSERVABLE_PACKAGES(JDK1_2, JDK8);

enum DiagKind {
@@ -1690,7 +1690,9 @@ JCExpression lambdaExpressionOrStatement(boolean hasParens, boolean explicitPara
if (param.vartype != null &&
isRestrictedLocalVarTypeName(param.vartype, false) &&
param.vartype.hasTag(TYPEARRAY)) {
log.error(DiagnosticFlag.SYNTAX, param.pos, Errors.VarNotAllowedArray);
log.error(DiagnosticFlag.SYNTAX, param.pos,
Feature.VAR_SYNTAX_IMPLICIT_LAMBDAS.allowedInSource(source)
? Errors.VarNotAllowedArray : Errors.VarNotAllowedHere);
}
lambdaClassifier.addParameter(param);
if (lambdaClassifier.result() == LambdaParameterKind.ERROR) {
@@ -1701,7 +1703,9 @@ JCExpression lambdaExpressionOrStatement(boolean hasParens, boolean explicitPara
log.error(DiagnosticFlag.SYNTAX, pos, Errors.InvalidLambdaParameterDeclaration(lambdaClassifier.diagFragment));
}
for (JCVariableDecl param: params) {
if (param.vartype != null && isRestrictedLocalVarTypeName(param.vartype, true)) {
if (param.vartype != null
&& isRestrictedLocalVarTypeName(param.vartype, true)) {
checkSourceLevel(param.pos, Feature.VAR_SYNTAX_IMPLICIT_LAMBDAS);
param.startPos = TreeInfo.getStartPos(param.vartype);
param.vartype = null;
}
@@ -1711,9 +1715,9 @@ JCExpression lambdaExpressionOrStatement(boolean hasParens, boolean explicitPara
}

enum LambdaParameterKind {
EXPLICIT(0),
IMPLICIT(1),
VAR(2),
VAR(0),
EXPLICIT(1),
IMPLICIT(2),
ERROR(-1);

private final int index;
@@ -1723,11 +1727,11 @@ JCExpression lambdaExpressionOrStatement(boolean hasParens, boolean explicitPara
}
}

private final static Fragment[][] decisionTable = new Fragment[][]{
/* EXPLICIT IMPLICIT VAR */
/* EXPLICIT */ {null, ImplicitAndExplicitNotAllowed, VarAndExplicitNotAllowed},
/* IMPLICIT */ {ImplicitAndExplicitNotAllowed, null, VarAndImplicitNotAllowed},
/* VAR */ {VarAndExplicitNotAllowed, VarAndImplicitNotAllowed, null}
private final static Fragment[][] decisionTable = new Fragment[][] {
/* VAR EXPLICIT IMPLICIT */
/* VAR */ {null, VarAndExplicitNotAllowed, VarAndImplicitNotAllowed},
/* EXPLICIT */ {VarAndExplicitNotAllowed, null, ImplicitAndExplicitNotAllowed},
/* IMPLICIT */ {VarAndImplicitNotAllowed, ImplicitAndExplicitNotAllowed, null},
};

class LambdaClassifier {
@@ -1756,7 +1760,10 @@ private void reduce(LambdaParameterKind newKind) {
} else if (kind != newKind && kind != LambdaParameterKind.ERROR) {
LambdaParameterKind currentKind = kind;
kind = LambdaParameterKind.ERROR;
diagFragment = decisionTable[currentKind.index][newKind.index];
boolean varIndex = currentKind.index == LambdaParameterKind.VAR.index ||
newKind.index == LambdaParameterKind.VAR.index;
diagFragment = Feature.VAR_SYNTAX_IMPLICIT_LAMBDAS.allowedInSource(source) || !varIndex ?
decisionTable[currentKind.index][newKind.index] : null;
}
}

@@ -2795,6 +2795,9 @@ compiler.misc.feature.static.intf.method.invoke=\
compiler.misc.feature.private.intf.methods=\
private interface methods

compiler.misc.feature.var.syntax.in.implicit.lambda=\
var syntax in implicit lambdas

compiler.warn.underscore.as.identifier=\
as of release 9, ''_'' is a keyword, and may not be used as an identifier

@@ -0,0 +1,32 @@
/*
* Copyright (c) 2018, 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
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

// key: compiler.misc.feature.var.syntax.in.implicit.lambda
// key: compiler.err.feature.not.supported.in.source.plural
// options: -source 10 -Xlint:-options

import java.util.function.*;

class VarInImplicitLambda {
IntBinaryOperator f2 = (var x, y) -> x + y;
}
@@ -107,8 +107,8 @@ public String expand(String optParameter) {
}

enum SourceKind {
SOURCE_9("9"),
SOURCE_10("10");
SOURCE_10("10"),
SOURCE_11("11");

String sourceNumber;

@@ -121,9 +121,9 @@ public String expand(String optParameter) {

IMPLICIT_1("", ExplicitKind.IMPLICIT),
IMPLICIT_2("var", ExplicitKind.IMPLICIT_VAR),
EXPLIICT_SIMPLE("A", ExplicitKind.EXPLICIT),
EXPLIICT_SIMPLE_ARR1("A[]", ExplicitKind.EXPLICIT),
EXPLIICT_SIMPLE_ARR2("A[][]", ExplicitKind.EXPLICIT),
EXPLICIT_SIMPLE("A", ExplicitKind.EXPLICIT),
EXPLICIT_SIMPLE_ARR1("A[]", ExplicitKind.EXPLICIT),
EXPLICIT_SIMPLE_ARR2("A[][]", ExplicitKind.EXPLICIT),
EXPLICIT_VARARGS("A...", ExplicitKind.EXPLICIT),
EXPLICIT_GENERIC1("A<X>", ExplicitKind.EXPLICIT),
EXPLICIT_GENERIC2("A<? extends X, ? super Y>", ExplicitKind.EXPLICIT),
@@ -157,13 +157,7 @@ public String expand(String optParameter) {
}

ExplicitKind explicitKind(SourceKind sk) {
switch (explicitKind) {
case IMPLICIT_VAR:
return (sk == SourceKind.SOURCE_9) ?
ExplicitKind.EXPLICIT : ExplicitKind.IMPLICIT_VAR;
default:
return explicitKind;
}
return explicitKind;
}
}

@@ -299,6 +293,15 @@ void check(Result<?> res) {
errorExpected |= pn == LambdaParameterName.UNDERSCORE &&
lk.arity() > 0;

for (int i = 0; i < lk.arity(); i++) {
if (!lk.isShort() &&
pks[i].explicitKind(sk) == LambdaParameterKind.ExplicitKind.IMPLICIT_VAR &&
sk == SourceKind.SOURCE_10) {
errorExpected = true;
break;
}
}

if (errorExpected != res.hasErrors()) {
fail("invalid diagnostics for source:\n" +
res.compilationInfo() +
@@ -3,6 +3,7 @@
* @bug 8198512 8199327
* @summary compiler support for local-variable syntax for lambda parameters
* @compile/fail/ref=VarInImplicitLambdaNegTest01.out -XDrawDiagnostics VarInImplicitLambdaNegTest01.java
* @compile/fail/ref=VarInImplicitLambdaNegTest01_source10.out -source 10 -XDrawDiagnostics VarInImplicitLambdaNegTest01.java
*/

import java.util.function.*;
@@ -1,6 +1,6 @@
VarInImplicitLambdaNegTest01.java:11:28: compiler.err.invalid.lambda.parameter.declaration: (compiler.misc.var.and.implicit.not.allowed)
VarInImplicitLambdaNegTest01.java:12:28: compiler.err.invalid.lambda.parameter.declaration: (compiler.misc.var.and.implicit.not.allowed)
VarInImplicitLambdaNegTest01.java:13:28: compiler.err.invalid.lambda.parameter.declaration: (compiler.misc.var.and.explicit.not.allowed)
VarInImplicitLambdaNegTest01.java:14:28: compiler.err.invalid.lambda.parameter.declaration: (compiler.misc.implicit.and.explicit.not.allowed)
VarInImplicitLambdaNegTest01.java:16:52: compiler.err.var.not.allowed.array
VarInImplicitLambdaNegTest01.java:13:28: compiler.err.invalid.lambda.parameter.declaration: (compiler.misc.var.and.implicit.not.allowed)
VarInImplicitLambdaNegTest01.java:14:28: compiler.err.invalid.lambda.parameter.declaration: (compiler.misc.var.and.explicit.not.allowed)
VarInImplicitLambdaNegTest01.java:15:28: compiler.err.invalid.lambda.parameter.declaration: (compiler.misc.implicit.and.explicit.not.allowed)
VarInImplicitLambdaNegTest01.java:17:52: compiler.err.var.not.allowed.array
5 errors
@@ -0,0 +1,6 @@
- compiler.warn.source.no.bootclasspath: 10
VarInImplicitLambdaNegTest01.java:12:36: compiler.err.feature.not.supported.in.source.plural: (compiler.misc.feature.var.syntax.in.implicit.lambda), 10, 11
VarInImplicitLambdaNegTest01.java:15:28: compiler.err.invalid.lambda.parameter.declaration: (compiler.misc.implicit.and.explicit.not.allowed)
VarInImplicitLambdaNegTest01.java:17:52: compiler.err.var.not.allowed.here
3 errors
1 warning

1 comment on commit 84fc3b7

@openjdk-notifier

This comment has been minimized.

Copy link

@openjdk-notifier openjdk-notifier bot commented on 84fc3b7 Oct 11, 2021

Please sign in to comment.