Skip to content

Commit

Permalink
8211148: var in implicit lambdas shouldn't be accepted for source < 11
Browse files Browse the repository at this point in the history
Reviewed-by: mcimadamore
  • Loading branch information
Vicente Romero committed Oct 4, 2018
1 parent 73a6313 commit 2589e97
Show file tree
Hide file tree
Showing 8 changed files with 80 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ public enum Feature {
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),
SWITCH_MULTIPLE_CASE_LABELS(JDK12, Fragments.FeatureMultipleCaseLabels, DiagKind.PLURAL),
SWITCH_RULE(JDK12, Fragments.FeatureSwitchRules, DiagKind.PLURAL),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1783,7 +1783,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) {
Expand All @@ -1794,7 +1796,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;
}
Expand All @@ -1804,9 +1808,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;
Expand All @@ -1816,11 +1820,11 @@ enum LambdaParameterKind {
}
}

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 {
Expand Down Expand Up @@ -1849,7 +1853,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;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2832,6 +2832,9 @@ compiler.misc.feature.switch.expressions=\
compiler.misc.feature.raw.string.literals=\
raw string literals

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

Expand Down
32 changes: 32 additions & 0 deletions test/langtools/tools/javac/diags/examples/VarInImplicitLambda.java
Original file line number Diff line number Diff line change
@@ -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;
}
27 changes: 15 additions & 12 deletions test/langtools/tools/javac/lambda/LambdaParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -121,9 +121,9 @@ enum LambdaParameterKind implements ComboParameter {

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),
Expand Down Expand Up @@ -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;
}
}

Expand Down Expand Up @@ -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() +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 2589e97

Please sign in to comment.