Repository: groovy
Updated Branches:
  refs/heads/master e8c3ed4dd -> ed17fb24d


GROOVY-8851: Refine safe indexing for named properties(closes #813)


Project: http://git-wip-us.apache.org/repos/asf/groovy/repo
Commit: http://git-wip-us.apache.org/repos/asf/groovy/commit/ed17fb24
Tree: http://git-wip-us.apache.org/repos/asf/groovy/tree/ed17fb24
Diff: http://git-wip-us.apache.org/repos/asf/groovy/diff/ed17fb24

Branch: refs/heads/master
Commit: ed17fb24dc3e2045c1f83c63cfedfa9e870397f2
Parents: e8c3ed4
Author: Daniel Sun <sun...@apache.org>
Authored: Sat Oct 20 16:19:11 2018 +0800
Committer: Daniel Sun <sun...@apache.org>
Committed: Sat Oct 20 16:19:11 2018 +0800

----------------------------------------------------------------------
 src/antlr/GroovyParser.g4                       |  2 +-
 .../parser/antlr4/GroovyParserTest.groovy       |  1 +
 .../test/resources/core/SafeIndex_04x.groovy    | 40 ++++++++++++++++++++
 3 files changed, 42 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/groovy/blob/ed17fb24/src/antlr/GroovyParser.g4
----------------------------------------------------------------------
diff --git a/src/antlr/GroovyParser.g4 b/src/antlr/GroovyParser.g4
index 72d6fab..3f768a5 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

http://git-wip-us.apache.org/repos/asf/groovy/blob/ed17fb24/subprojects/parser-antlr4/src/test/groovy/org/apache/groovy/parser/antlr4/GroovyParserTest.groovy
----------------------------------------------------------------------
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 5719656..d627229 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"() {

http://git-wip-us.apache.org/repos/asf/groovy/blob/ed17fb24/subprojects/parser-antlr4/src/test/resources/core/SafeIndex_04x.groovy
----------------------------------------------------------------------
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 0000000..909b097
--- /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()

Reply via email to