Bug 565830 - record - nested record contains reference to the enclosing
instance

Change-Id: I44d8f11f1ef5e55eaaf8c2675a530ab2fd709fba
Signed-off-by: Manoj Palat <manpalat@in.ibm.com>
diff --git a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/RecordsRestrictedClassTest.java b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/RecordsRestrictedClassTest.java
index 314a553..6de0a9b 100644
--- a/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/RecordsRestrictedClassTest.java
+++ b/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/RecordsRestrictedClassTest.java
@@ -3898,4 +3898,28 @@
 			},
 		"10");
 }
+public void testBug565830_01() {
+	runConformTest(
+		new String[] {
+			"X.java",
+			"class X {\n"+
+			"    void bar() throws Exception {\n"+
+			"        record Bar(int x) implements java.io.Serializable {\n"+
+			"            void printMyFields() {\n"+
+			"                for (var field : this.getClass().getDeclaredFields()) {\n"+
+			"                    System.out.println(field);\n"+
+			"                }\n"+
+			"            }\n"+
+			"        }\n"+
+			"        var bar = new Bar(1);\n"+
+			"        bar.printMyFields();\n"+
+			"        new java.io.ObjectOutputStream(java.io.OutputStream.nullOutputStream()).writeObject(bar);\n"+
+			"    }\n"+
+			"    public static void main(String[] args) throws Exception {\n"+
+			"        new X().bar();\n"+
+			"    }\n"+
+			"}",
+		},
+		"private final int X$1Bar.x");
+}
 }
\ No newline at end of file
diff --git a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/NestedTypeBinding.java b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/NestedTypeBinding.java
index 529574f..90f7a9b 100644
--- a/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/NestedTypeBinding.java
+++ b/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/lookup/NestedTypeBinding.java
@@ -77,9 +77,14 @@
 
 /* Add a new synthetic argument for <enclosingType>.
 * Answer the new argument or the existing argument if one already existed.
+* Do not add if this is static (eg. nested records)
 */
 public SyntheticArgumentBinding addSyntheticArgument(ReferenceBinding targetEnclosingType) {
 	if (!isPrototype()) throw new IllegalStateException();
+	if (isStatic()) {
+		assert this.isRecord();// a local record is implicitly static; no other local type can be static
+		return null;
+	}
 	SyntheticArgumentBinding synthLocal = null;
 	if (this.enclosingInstances == null) {
 		synthLocal = new SyntheticArgumentBinding(targetEnclosingType);