This is an automated email from the ASF dual-hosted git repository. sunlan pushed a commit to branch GROOVY_4_0_X in repository https://gitbox.apache.org/repos/asf/groovy.git
commit 8b50a71b97105e8e82f31e7e4da0c33a2150be77 Author: Daniel Sun <[email protected]> AuthorDate: Sat Mar 29 16:09:06 2025 +0900 GROOVY-10240: Only record can have compact constructor (cherry picked from commit 312f6624f169fff22993ef2cbdbf896384c54e37) --- .../apache/groovy/parser/antlr4/AstBuilder.java | 2 +- .../fail/ClassDeclaration_04x.groovy | 25 ++++++++++++++++++++++ .../groovy/parser/antlr4/SyntaxErrorTest.groovy | 5 +++++ 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java b/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java index 1d29bf20ff..38f8780ece 100644 --- a/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java +++ b/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java @@ -1678,7 +1678,7 @@ public class AstBuilder extends GroovyParserBaseVisitor<Object> { ClassNode classNode = ctx.getNodeMetaData(CLASS_DECLARATION_CLASS_NODE); if (classNode.getAnnotations().stream().noneMatch(a -> a.getClassNode().getName().equals(RECORD_TYPE_NAME))) { - createParsingFailedException("Only record can have compact constructor", ctx); + throw createParsingFailedException("Only record can have compact constructor", ctx); } if (new ModifierManager(this, ctx.getNodeMetaData(COMPACT_CONSTRUCTOR_DECLARATION_MODIFIERS)).containsAny(VAR)) { diff --git a/src/test-resources/fail/ClassDeclaration_04x.groovy b/src/test-resources/fail/ClassDeclaration_04x.groovy new file mode 100644 index 0000000000..1b6ec69782 --- /dev/null +++ b/src/test-resources/fail/ClassDeclaration_04x.groovy @@ -0,0 +1,25 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package fail + +class Person { + Person { + // Only record can have compact constructor + } +} diff --git a/src/test/org/apache/groovy/parser/antlr4/SyntaxErrorTest.groovy b/src/test/org/apache/groovy/parser/antlr4/SyntaxErrorTest.groovy index d9341fa9a0..56d93fcb94 100644 --- a/src/test/org/apache/groovy/parser/antlr4/SyntaxErrorTest.groovy +++ b/src/test/org/apache/groovy/parser/antlr4/SyntaxErrorTest.groovy @@ -233,6 +233,11 @@ final class SyntaxErrorTest { |'''.stripMargin() } + @Test + void 'groovy core - ClassDeclaration 4'() { + TestUtils.doRunAndShouldFail('fail/ClassDeclaration_04x.groovy') + } + @Test void 'test groovy core - EnumDeclaration 1'() { expectParseError '''\
