8259025: Record compact constructor using Objects.requireNonNull

Reviewed-by: attila
This commit is contained in:
Guoxiong Li 2021-01-11 13:50:53 +00:00 committed by Attila Szegedi
parent 23801da94b
commit 33fbc10cb8
2 changed files with 13 additions and 3 deletions

View File

@ -2962,7 +2962,6 @@ public class Attr extends JCTree.Visitor {
localEnv.info.isSerializable = true;
localEnv.info.isSerializableLambda = true;
}
localEnv.info.isLambda = true;
List<Type> explicitParamTypes = null;
if (that.paramKind == JCLambda.ParameterKind.EXPLICIT) {
//attribute lambda parameters
@ -3404,6 +3403,7 @@ public class Attr extends JCTree.Visitor {
lambdaEnv = env.dup(that, env.info.dup(env.info.scope.dup()));
}
lambdaEnv.info.yieldResult = null;
lambdaEnv.info.isLambda = true;
return lambdaEnv;
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2021, 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
@ -25,7 +25,7 @@
* RecordCompilationTests
*
* @test
* @bug 8250629 8252307 8247352 8241151 8246774
* @bug 8250629 8252307 8247352 8241151 8246774 8259025
* @summary Negative compilation tests, and positive compilation (smoke) tests for records
* @library /lib/combo /tools/lib /tools/javac/lib
* @modules
@ -343,6 +343,16 @@ public class RecordCompilationTests extends CompilationTestCase {
assertOK("import java.util.*; record R(String x, String y) { public R { Objects.requireNonNull(x); Objects.requireNonNull(y); } }");
// The lambda expressions in the constructor should be compiled successfully.
assertOK("""
import static java.util.Objects.*;
record R(String v) {
R {
requireNonNull(v, () -> "v must be provided");
requireNonNullElseGet(v, () -> "w");
}
}""");
// Not OK to redeclare canonical without DA
assertFail("compiler.err.var.might.not.have.been.initialized", "record R(int x, int y) { # }",
"public R(int x, int y) { this.x = x; }");