From 27fc472e1ca1f1242a31cac93138dd318cdbbcec Mon Sep 17 00:00:00 2001 From: danielsun1106 Date: Sat, 20 Oct 2018 15:24:34 +0800 Subject: [PATCH] GROOVY-8851: Refine safe indexing for named properties --- src/antlr/GroovyParser.g4 | 2 +- .../parser/antlr4/GroovyParserTest.groovy | 1 + .../test/resources/core/SafeIndex_04x.groovy | 40 +++++++++++++++++++ 3 files changed, 42 insertions(+), 1 deletion(-) create mode 100644 subprojects/parser-antlr4/src/test/resources/core/SafeIndex_04x.groovy diff --git a/src/antlr/GroovyParser.g4 b/src/antlr/GroovyParser.g4 index 72d6fab6e5a..3f768a5fd42 100644 --- a/src/antlr/GroovyParser.g4 +++ b/src/antlr/GroovyParser.g4 @@ -1013,7 +1013,7 @@ indexPropertyArgs ; namedPropertyArgs - : LBRACK (mapEntryList | COLON) RBRACK + : QUESTION? LBRACK (mapEntryList | COLON) RBRACK ; primary diff --git a/subprojects/parser-antlr4/src/test/groovy/org/apache/groovy/parser/antlr4/GroovyParserTest.groovy b/subprojects/parser-antlr4/src/test/groovy/org/apache/groovy/parser/antlr4/GroovyParserTest.groovy index 5719656a09b..d6272293748 100644 --- a/subprojects/parser-antlr4/src/test/groovy/org/apache/groovy/parser/antlr4/GroovyParserTest.groovy +++ b/subprojects/parser-antlr4/src/test/groovy/org/apache/groovy/parser/antlr4/GroovyParserTest.groovy @@ -273,6 +273,7 @@ class GroovyParserTest extends GroovyTestCase { doRunAndTestAntlr4('core/SafeIndex_01x.groovy') doRunAndTestAntlr4('core/SafeIndex_02x.groovy') doRunAndTestAntlr4('core/SafeIndex_03x.groovy') + doRunAndTestAntlr4('core/SafeIndex_04x.groovy') } void "test groovy core - NegativeRelationalOperators"() { diff --git a/subprojects/parser-antlr4/src/test/resources/core/SafeIndex_04x.groovy b/subprojects/parser-antlr4/src/test/resources/core/SafeIndex_04x.groovy new file mode 100644 index 00000000000..909b09704e3 --- /dev/null +++ b/subprojects/parser-antlr4/src/test/resources/core/SafeIndex_04x.groovy @@ -0,0 +1,40 @@ +/* + * 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. + */ + +import groovy.transform.CompileStatic + +class SpecialSafeIndex { + String name + String location +} + +@CompileStatic +def csSpecialSafeIndex() { + SpecialSafeIndex ssi = SpecialSafeIndex?[name: 'Daniel.Sun', location: 'Shanghai'] + assert 'Daniel.Sun' == ssi.name + assert 'Shanghai' == ssi.location +} +csSpecialSafeIndex() + +def specialSafeIndex() { + SpecialSafeIndex ssi = SpecialSafeIndex?[name: 'Daniel.Sun', location: 'Shanghai'] + assert 'Daniel.Sun' == ssi.name + assert 'Shanghai' == ssi.location +} +specialSafeIndex()