8269738: AssertionError when combining pattern matching and function closure

Reviewed-by: jlahoda, vromero
This commit is contained in:
Guoxiong Li 2021-07-08 14:15:07 +00:00
parent 168af2e6b2
commit 9e75f922b1
2 changed files with 20 additions and 2 deletions

View File

@ -768,7 +768,7 @@ public class TransPatterns extends TreeTranslator {
VarSymbol bindingDeclared(BindingSymbol varSymbol) {
VarSymbol res = parent.bindingDeclared(varSymbol);
if (res == null) {
res = new VarSymbol(varSymbol.flags(), varSymbol.name, varSymbol.type, varSymbol.owner);
res = new VarSymbol(varSymbol.flags(), varSymbol.name, varSymbol.type, currentMethodSym);
res.setTypeAttributes(varSymbol.getRawTypeAttributes());
hoistedVarMap.put(varSymbol, res);
}

View File

@ -23,7 +23,7 @@
/*
* @test
* @bug 8267610
* @bug 8267610 8269738
* @summary LambdaToMethod cannot capture pattern variables. So the TransPatterns should
* transform the pattern variables and symbols to normal variables and symbols.
* @compile --enable-preview -source ${jdk.version} LambdaCannotCapturePatternVariables.java
@ -34,11 +34,24 @@ import java.util.function.Supplier;
public class LambdaCannotCapturePatternVariables {
static Number num1 = 1;
static Number num2 = null;
static Number staticNum1 = (num1 instanceof Integer i) ? ((Supplier<Integer>) () -> i).get() : null;
static Number staticNum2 = (num2 instanceof Integer i) ? ((Supplier<Integer>) () -> i).get() : null;
Number instanceNum1 = (num1 instanceof Integer i) ? ((Supplier<Integer>) () -> i).get() : null;
Number instanceNum2 = (num2 instanceof Integer i) ? ((Supplier<Integer>) () -> i).get() : null;
public static void main(String[] args) {
var testVar = new LambdaCannotCapturePatternVariables();
testVar.testInstanceOfPatternVariable(Integer.valueOf(1));
testVar.testSwitchPatternVariable(Integer.valueOf(1));
testVar.test(Integer.valueOf(1));
assertTrue(staticNum1 != null, "staticNum1 is null unexpectedly");
assertTrue(staticNum2 == null, "staticNum1 is not null unexpectedly");
assertTrue(testVar.instanceNum1 != null, "instanceNum1 is null unexpectedly");
assertTrue(testVar.instanceNum2 == null, "instanceNum2 is not null unexpectedly");
assertTrue(staticNum1.intValue() == 1, "staticNum1.intValue() is not equal to 1");
assertTrue(testVar.instanceNum1.intValue() == 1, "instanceNum1.intValue() is not equal to 1");
}
public Integer testInstanceOfPatternVariable(Object x) {
@ -69,4 +82,9 @@ public class LambdaCannotCapturePatternVariables {
return ((y instanceof Integer z) ? z : bar);
})).get() : bar);
}
static void assertTrue(boolean cond, String info) {
if (!cond)
throw new AssertionError(info);
}
}