diff --git a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/parser/Parser.java b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/parser/Parser.java index a412a0433cb..734a168938d 100644 --- a/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/parser/Parser.java +++ b/org.eclipse.jdt.core.compiler.batch/src/org/eclipse/jdt/internal/compiler/parser/Parser.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2000, 2024 IBM Corporation and others. + * Copyright (c) 2000, 2025 IBM Corporation and others. * * This program and the accompanying materials * are made available under the terms of the Eclipse Public License 2.0 @@ -3477,6 +3477,9 @@ private boolean isAFieldDeclarationInRecord() { continue; nestingTypeAndMethod = this.recordNestedMethodLevels.get(node); if (nestingTypeAndMethod != null) { // record declaration is done yet + if (nestingTypeAndMethod[0] != this.nestedType + || nestingTypeAndMethod[1] != this.nestedMethod[this.nestedType]) + return false; recordIndex = i; break; } 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 4b7e8210a31..0f24dae3901 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 @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2019, 2024 IBM Corporation and others. + * Copyright (c) 2019, 2025 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at @@ -28,7 +28,7 @@ public class RecordsRestrictedClassTest extends AbstractRegressionTest { static { // TESTS_NUMBERS = new int [] { 40 }; // TESTS_RANGE = new int[] { 1, -1 }; -// TESTS_NAMES = new String[] { "testIssue1641"}; +// TESTS_NAMES = new String[] { "testBug3504_1"}; } public static Class<?> testClass() { @@ -9591,4 +9591,29 @@ public static void main(String [] args) { }, "OK!"); } +public void testBug3504_1() { + runNegativeTest( + new String[] { + "X.java", + """ + class X { + public static void main(String[] args) { + record R(int x) { + static { + static int i = 0; + } + } + R r = new R(100); + System.out.println(r.x()); + } + } + """ + }, + "----------\n" + + "1. ERROR in X.java (at line 5)\n" + + " static int i = 0;\n" + + " ^\n" + + "Illegal modifier for the variable i; only final is permitted\n" + + "----------\n"); +} }