changeset 29441:9e8d258708bb

revset: check invalid function syntax "func-name"() explicitly Before the error was caught at func() as an unknown identifier, and the optimizer failed to detect the syntax error. This patch introduces getsymbol() helper to ensure that a string is not allowed as a function name.
author Yuya Nishihara <yuya@tcha.org>
date Mon, 27 Jun 2016 20:44:14 +0900
parents 009cc6c89d0f
children 456609cbd840
files mercurial/revset.py tests/test-revset.t
diffstat 2 files changed, 28 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/mercurial/revset.py	Tue Jun 28 22:39:06 2016 +0900
+++ b/mercurial/revset.py	Mon Jun 27 20:44:14 2016 +0900
@@ -302,6 +302,11 @@
 
 # helpers
 
+def getsymbol(x):
+    if x and x[0] == 'symbol':
+        return x[1]
+    raise error.ParseError(_('not a symbol'))
+
 def getstring(x, err):
     if x and (x[0] == 'string' or x[0] == 'symbol'):
         return x[1]
@@ -414,13 +419,14 @@
     raise error.ParseError(_("can't use a key-value pair in this context"))
 
 def func(repo, subset, a, b):
-    if a[0] == 'symbol' and a[1] in symbols:
-        return symbols[a[1]](repo, subset, b)
+    f = getsymbol(a)
+    if f in symbols:
+        return symbols[f](repo, subset, b)
 
     keep = lambda fn: getattr(fn, '__doc__', None) is not None
 
     syms = [s for (s, fn) in symbols.items() if keep(fn)]
-    raise error.UnknownIdentifier(a[1], syms)
+    raise error.UnknownIdentifier(f, syms)
 
 # functions
 
@@ -2304,11 +2310,11 @@
     """
     if (revs is not None
         and revs[0] == 'func'
-        and getstring(revs[1], _('not a symbol')) == 'ancestors'
+        and getsymbol(revs[1]) == 'ancestors'
         and bases is not None
         and bases[0] == 'not'
         and bases[1][0] == 'func'
-        and getstring(bases[1][1], _('not a symbol')) == 'ancestors'):
+        and getsymbol(bases[1][1]) == 'ancestors'):
         return ('list', revs[2], bases[1][2])
 
 def _optimize(x, small):
@@ -2419,7 +2425,7 @@
         ws, ts = zip(*(_optimize(y, small) for y in x[1:]))
         return sum(ws), (op,) + ts
     elif op == 'func':
-        f = getstring(x[1], _("not a symbol"))
+        f = getsymbol(x[1])
         wa, ta = _optimize(x[2], small)
         if f in ("author branch closed date desc file grep keyword "
                  "outgoing user"):
--- a/tests/test-revset.t	Tue Jun 28 22:39:06 2016 +0900
+++ b/tests/test-revset.t	Mon Jun 27 20:44:14 2016 +0900
@@ -434,6 +434,12 @@
   4
   $ hg book -d date
 
+function name should be a symbol
+
+  $ log '"date"(2005)'
+  hg: parse error: not a symbol
+  [255]
+
 keyword arguments
 
   $ log 'extra(branch, value=a)'
@@ -2033,6 +2039,16 @@
   hg: parse error: missing argument
   [255]
 
+invalid function call should not be optimized to only()
+
+  $ log '"ancestors"(6) and not ancestors(4)'
+  hg: parse error: not a symbol
+  [255]
+
+  $ log 'ancestors(6) and not "ancestors"(4)'
+  hg: parse error: not a symbol
+  [255]
+
 we can use patterns when searching for tags
 
   $ log 'tag("1..*")'