revset: clean up 'revset lifetimes on dyn Revset

If I understand correctly, the 'revset lifetimes on `Box<dyn
Revset<'index> + 'revset>` are not constrained by the lifetime of a
revset; we don't have any revsets that borrow data from other
revsets. Instead, they're all about constraining a boxed revset to the
index's lifetime. Without the lifetime annotation, it would default to
'static, and the borrow-checker doesn't like `dyn Revset<'index> +
'static`, since the revset could then live longer than the index it
borrows.
This commit is contained in:
Martin von Zweigbergk 2023-02-16 22:36:51 -08:00 committed by Martin von Zweigbergk
parent 0159092c31
commit 8966580ba4

View file

@ -1686,14 +1686,14 @@ where
} }
} }
struct ChildrenRevset<'revset, 'index: 'revset> { struct ChildrenRevset<'index> {
// The revisions we want to find children for // The revisions we want to find children for
root_set: Box<dyn Revset<'index> + 'revset>, root_set: Box<dyn Revset<'index> + 'index>,
// Consider only candidates from this set // Consider only candidates from this set
candidate_set: Box<dyn Revset<'index> + 'revset>, candidate_set: Box<dyn Revset<'index> + 'index>,
} }
impl<'index> Revset<'index> for ChildrenRevset<'_, 'index> { impl<'index> Revset<'index> for ChildrenRevset<'index> {
fn iter(&self) -> RevsetIterator<'_, 'index> { fn iter(&self) -> RevsetIterator<'_, 'index> {
let roots: HashSet<_> = self let roots: HashSet<_> = self
.root_set .root_set
@ -1712,19 +1712,19 @@ impl<'index> Revset<'index> for ChildrenRevset<'_, 'index> {
} }
} }
impl<'index> ToPredicateFn<'index> for ChildrenRevset<'_, 'index> { impl<'index> ToPredicateFn<'index> for ChildrenRevset<'index> {
fn to_predicate_fn(&self) -> Box<dyn FnMut(&IndexEntry<'index>) -> bool + '_> { fn to_predicate_fn(&self) -> Box<dyn FnMut(&IndexEntry<'index>) -> bool + '_> {
// TODO: can be optimized if candidate_set contains all heads // TODO: can be optimized if candidate_set contains all heads
self.iter().into_predicate_fn() self.iter().into_predicate_fn()
} }
} }
struct FilterRevset<'revset, 'index: 'revset, P> { struct FilterRevset<'index, P> {
candidates: Box<dyn Revset<'index> + 'revset>, candidates: Box<dyn Revset<'index> + 'index>,
predicate: P, predicate: P,
} }
impl<'index, P> Revset<'index> for FilterRevset<'_, 'index, P> impl<'index, P> Revset<'index> for FilterRevset<'index, P>
where where
P: ToPredicateFn<'index>, P: ToPredicateFn<'index>,
{ {
@ -1734,7 +1734,7 @@ where
} }
} }
impl<'index, P> ToPredicateFn<'index> for FilterRevset<'_, 'index, P> impl<'index, P> ToPredicateFn<'index> for FilterRevset<'index, P>
where where
P: ToPredicateFn<'index>, P: ToPredicateFn<'index>,
{ {
@ -1746,12 +1746,12 @@ where
} }
} }
struct UnionRevset<'revset, 'index: 'revset> { struct UnionRevset<'index> {
set1: Box<dyn Revset<'index> + 'revset>, set1: Box<dyn Revset<'index> + 'index>,
set2: Box<dyn Revset<'index> + 'revset>, set2: Box<dyn Revset<'index> + 'index>,
} }
impl<'index> Revset<'index> for UnionRevset<'_, 'index> { impl<'index> Revset<'index> for UnionRevset<'index> {
fn iter(&self) -> RevsetIterator<'_, 'index> { fn iter(&self) -> RevsetIterator<'_, 'index> {
RevsetIterator::new(Box::new(UnionRevsetIterator { RevsetIterator::new(Box::new(UnionRevsetIterator {
iter1: self.set1.iter().peekable(), iter1: self.set1.iter().peekable(),
@ -1760,7 +1760,7 @@ impl<'index> Revset<'index> for UnionRevset<'_, 'index> {
} }
} }
impl<'index> ToPredicateFn<'index> for UnionRevset<'_, 'index> { impl<'index> ToPredicateFn<'index> for UnionRevset<'index> {
fn to_predicate_fn(&self) -> Box<dyn FnMut(&IndexEntry<'index>) -> bool + '_> { fn to_predicate_fn(&self) -> Box<dyn FnMut(&IndexEntry<'index>) -> bool + '_> {
let mut p1 = self.set1.to_predicate_fn(); let mut p1 = self.set1.to_predicate_fn();
let mut p2 = self.set2.to_predicate_fn(); let mut p2 = self.set2.to_predicate_fn();
@ -1792,12 +1792,12 @@ impl<'revset, 'index> Iterator for UnionRevsetIterator<'revset, 'index> {
} }
} }
struct IntersectionRevset<'revset, 'index: 'revset> { struct IntersectionRevset<'index> {
set1: Box<dyn Revset<'index> + 'revset>, set1: Box<dyn Revset<'index> + 'index>,
set2: Box<dyn Revset<'index> + 'revset>, set2: Box<dyn Revset<'index> + 'index>,
} }
impl<'index> Revset<'index> for IntersectionRevset<'_, 'index> { impl<'index> Revset<'index> for IntersectionRevset<'index> {
fn iter(&self) -> RevsetIterator<'_, 'index> { fn iter(&self) -> RevsetIterator<'_, 'index> {
RevsetIterator::new(Box::new(IntersectionRevsetIterator { RevsetIterator::new(Box::new(IntersectionRevsetIterator {
iter1: self.set1.iter().peekable(), iter1: self.set1.iter().peekable(),
@ -1806,7 +1806,7 @@ impl<'index> Revset<'index> for IntersectionRevset<'_, 'index> {
} }
} }
impl<'index> ToPredicateFn<'index> for IntersectionRevset<'_, 'index> { impl<'index> ToPredicateFn<'index> for IntersectionRevset<'index> {
fn to_predicate_fn(&self) -> Box<dyn FnMut(&IndexEntry<'index>) -> bool + '_> { fn to_predicate_fn(&self) -> Box<dyn FnMut(&IndexEntry<'index>) -> bool + '_> {
let mut p1 = self.set1.to_predicate_fn(); let mut p1 = self.set1.to_predicate_fn();
let mut p2 = self.set2.to_predicate_fn(); let mut p2 = self.set2.to_predicate_fn();
@ -1848,14 +1848,14 @@ impl<'revset, 'index> Iterator for IntersectionRevsetIterator<'revset, 'index> {
} }
} }
struct DifferenceRevset<'revset, 'index: 'revset> { struct DifferenceRevset<'index> {
// The minuend (what to subtract from) // The minuend (what to subtract from)
set1: Box<dyn Revset<'index> + 'revset>, set1: Box<dyn Revset<'index> + 'index>,
// The subtrahend (what to subtract) // The subtrahend (what to subtract)
set2: Box<dyn Revset<'index> + 'revset>, set2: Box<dyn Revset<'index> + 'index>,
} }
impl<'index> Revset<'index> for DifferenceRevset<'_, 'index> { impl<'index> Revset<'index> for DifferenceRevset<'index> {
fn iter(&self) -> RevsetIterator<'_, 'index> { fn iter(&self) -> RevsetIterator<'_, 'index> {
RevsetIterator::new(Box::new(DifferenceRevsetIterator { RevsetIterator::new(Box::new(DifferenceRevsetIterator {
iter1: self.set1.iter().peekable(), iter1: self.set1.iter().peekable(),
@ -1864,7 +1864,7 @@ impl<'index> Revset<'index> for DifferenceRevset<'_, 'index> {
} }
} }
impl<'index> ToPredicateFn<'index> for DifferenceRevset<'_, 'index> { impl<'index> ToPredicateFn<'index> for DifferenceRevset<'index> {
fn to_predicate_fn(&self) -> Box<dyn FnMut(&IndexEntry<'index>) -> bool + '_> { fn to_predicate_fn(&self) -> Box<dyn FnMut(&IndexEntry<'index>) -> bool + '_> {
// TODO: optimize 'p1' out for unary negate? // TODO: optimize 'p1' out for unary negate?
let mut p1 = self.set1.to_predicate_fn(); let mut p1 = self.set1.to_predicate_fn();
@ -2126,10 +2126,10 @@ pub fn evaluate_expression<'index>(
} }
} }
fn revset_for_commit_ids<'revset, 'index: 'revset>( fn revset_for_commit_ids<'index>(
repo: &'index dyn Repo, repo: &'index dyn Repo,
commit_ids: &[CommitId], commit_ids: &[CommitId],
) -> Box<dyn Revset<'index> + 'revset> { ) -> Box<dyn Revset<'index> + 'index> {
let index = repo.index(); let index = repo.index();
let mut index_entries = vec![]; let mut index_entries = vec![];
for id in commit_ids { for id in commit_ids {
@ -2140,10 +2140,10 @@ fn revset_for_commit_ids<'revset, 'index: 'revset>(
Box::new(EagerRevset { index_entries }) Box::new(EagerRevset { index_entries })
} }
pub fn revset_for_commits<'revset, 'index: 'revset>( pub fn revset_for_commits<'index>(
repo: &'index dyn Repo, repo: &'index dyn Repo,
commits: &[&Commit], commits: &[&Commit],
) -> Box<dyn Revset<'index> + 'revset> { ) -> Box<dyn Revset<'index> + 'index> {
let index = repo.index(); let index = repo.index();
let mut index_entries = commits let mut index_entries = commits
.iter() .iter()
@ -2211,11 +2211,11 @@ fn build_predicate_fn<'index>(
} }
} }
pub fn filter_by_diff<'revset, 'index: 'revset>( pub fn filter_by_diff<'index>(
repo: &'index dyn Repo, repo: &'index dyn Repo,
matcher: impl Borrow<dyn Matcher + 'index> + 'index, matcher: impl Borrow<dyn Matcher + 'index> + 'index,
candidates: Box<dyn Revset<'index> + 'revset>, candidates: Box<dyn Revset<'index> + 'index>,
) -> Box<dyn Revset<'index> + 'revset> { ) -> Box<dyn Revset<'index> + 'index> {
Box::new(FilterRevset::<PurePredicateFn> { Box::new(FilterRevset::<PurePredicateFn> {
candidates, candidates,
predicate: Box::new(move |entry| has_diff_from_parent(repo, entry, matcher.borrow())), predicate: Box::new(move |entry| has_diff_from_parent(repo, entry, matcher.borrow())),