This is an automated email from the ASF dual-hosted git repository.
sunlan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git
The following commit(s) were added to refs/heads/master by this push:
new 312f6624f1 GROOVY-10240: Only record can have compact constructor
312f6624f1 is described below
commit 312f6624f169fff22993ef2cbdbf896384c54e37
Author: Daniel Sun <[email protected]>
AuthorDate: Sat Mar 29 16:09:06 2025 +0900
GROOVY-10240: Only record can have compact constructor
---
.../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 01d848479e..e6f6a56d5b 100644
--- a/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java
+++ b/src/main/java/org/apache/groovy/parser/antlr4/AstBuilder.java
@@ -1660,7 +1660,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 0f0c7e7565..b701f682ff 100644
--- a/src/test/org/apache/groovy/parser/antlr4/SyntaxErrorTest.groovy
+++ b/src/test/org/apache/groovy/parser/antlr4/SyntaxErrorTest.groovy
@@ -234,6 +234,11 @@ final class SyntaxErrorTest {
|'''.stripMargin()
}
+ @Test
+ void 'groovy core - ClassDeclaration 4'() {
+ TestUtils.doRunAndShouldFail('fail/ClassDeclaration_04x.groovy')
+ }
+
@Test
void 'groovy core - EnumDeclaration 1'() {
expectParseError '''\