diff --git a/hgext/rebase.py b/hgext/rebase.py
index d6e7ac6519736518195286e859237792e503fff8_aGdleHQvcmViYXNlLnB5..bb2f543b48b5290c634cc26c7e61d7c3e9dd8f6e_aGdleHQvcmViYXNlLnB5 100644
--- a/hgext/rebase.py
+++ b/hgext/rebase.py
@@ -890,7 +890,7 @@
             # no backup of rebased cset versions needed
             repair.strip(repo.ui, repo, strippoints)
 
-    if activebookmark:
+    if activebookmark and activebookmark in repo._bookmarks:
         bookmarks.activate(repo, activebookmark)
 
     clearstatus(repo)
diff --git a/mercurial/revset.py b/mercurial/revset.py
index d6e7ac6519736518195286e859237792e503fff8_bWVyY3VyaWFsL3JldnNldC5weQ==..bb2f543b48b5290c634cc26c7e61d7c3e9dd8f6e_bWVyY3VyaWFsL3JldnNldC5weQ== 100644
--- a/mercurial/revset.py
+++ b/mercurial/revset.py
@@ -2095,7 +2095,6 @@
     "parent": parentspec,
     "parentpost": p1,
     "only": only,
-    "onlypost": only,
 }
 
 def optimize(x, small):
@@ -2112,6 +2111,8 @@
     elif op == 'only':
         return optimize(('func', ('symbol', 'only'),
                          ('list', x[1], x[2])), small)
+    elif op == 'onlypost':
+        return optimize(('func', ('symbol', 'only'), x[1]), small)
     elif op == 'dagrangepre':
         return optimize(('func', ('symbol', 'ancestors'), x[1]), small)
     elif op == 'dagrangepost':
diff --git a/tests/test-bookmarks-rebase.t b/tests/test-bookmarks-rebase.t
index d6e7ac6519736518195286e859237792e503fff8_dGVzdHMvdGVzdC1ib29rbWFya3MtcmViYXNlLnQ=..bb2f543b48b5290c634cc26c7e61d7c3e9dd8f6e_dGVzdHMvdGVzdC1ib29rbWFya3MtcmViYXNlLnQ= 100644
--- a/tests/test-bookmarks-rebase.t
+++ b/tests/test-bookmarks-rebase.t
@@ -90,3 +90,18 @@
    * three                     4:dd7c838e8362
      two                       3:42e5ed2cdcf4
 
+after aborted rebase, restoring a bookmark that has been removed should not fail
+
+  $ hg rebase -s three -d two
+  rebasing 4:dd7c838e8362 "4" (tip three)
+  merging d
+  warning: conflicts during merge.
+  merging d incomplete! (edit conflicts, then use 'hg resolve --mark')
+  unresolved conflicts (see hg resolve, then hg rebase --continue)
+  [1]
+  $ hg bookmark -d three
+  $ hg rebase --abort
+  rebase aborted
+  $ hg bookmark
+     one                       1:925d80f479bb
+     two                       3:42e5ed2cdcf4
diff --git a/tests/test-revset.t b/tests/test-revset.t
index d6e7ac6519736518195286e859237792e503fff8_dGVzdHMvdGVzdC1yZXZzZXQudA==..bb2f543b48b5290c634cc26c7e61d7c3e9dd8f6e_dGVzdHMvdGVzdC1yZXZzZXQudA== 100644
--- a/tests/test-revset.t
+++ b/tests/test-revset.t
@@ -593,6 +593,29 @@
   8
   9
 
+Test opreand of '%' is optimized recursively (issue4670)
+
+  $ try --optimize '8:9-8%'
+  (onlypost
+    (minus
+      (range
+        ('symbol', '8')
+        ('symbol', '9'))
+      ('symbol', '8')))
+  * optimized:
+  (func
+    ('symbol', 'only')
+    (and
+      (range
+        ('symbol', '8')
+        ('symbol', '9'))
+      (not
+        ('symbol', '8'))))
+  * set:
+  <baseset+ [8, 9]>
+  8
+  9
+
 Test the order of operations
 
   $ log '7 + 9%5 + 2'