Skip to content

Commit

Permalink
Implement a least upper bound for marks.
Browse files Browse the repository at this point in the history
This is useful when trying to compute when something is lexically before
something else, but they aren't necessarily in the same SyntaxContext
  • Loading branch information
sapphire-arches committed Apr 24, 2018
1 parent 263b36b commit 498dbe4
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions src/libsyntax_pos/hygiene.rs
Expand Up @@ -21,6 +21,7 @@ use symbol::{Ident, Symbol};

use serialize::{Encodable, Decodable, Encoder, Decoder};
use std::collections::HashMap;
use rustc_data_structures::fx::FxHashSet;
use std::fmt;

/// A SyntaxContext represents a chain of macro expansions (represented by marks).
Expand Down Expand Up @@ -117,6 +118,32 @@ impl Mark {
true
})
}

/// Computes a mark such that both input marks are descendants of (or equal to) the returned
/// mark. That is, the following holds:
///
/// ```rust
/// let lub = lub(a, b);
/// assert!(a.is_descendant_of(lub))
/// assert!(b.is_descendant_of(lub))
/// ```
pub fn lub(mut a: Mark, mut b: Mark) -> Mark {
HygieneData::with(|data| {
// Compute the path from a to the root
let mut a_path = FxHashSet::<Mark>();
while a != Mark::root() {
a_path.insert(a);
a = data.marks[a.0 as usize].parent;
}

// While the path from b to the root hasn't intersected, move up the tree
while !a_path.contains(&b) {
b = data.marks[b.0 as usize].parent;
}

b
})
}
}

pub struct HygieneData {
Expand Down

0 comments on commit 498dbe4

Please sign in to comment.