Skip to content

Commit 95ea7fd

Browse files
committed
Add HygieneData::{outer,expn_info,is_descendant_of} methods.
This commit factors out some repeated code.
1 parent 2232321 commit 95ea7fd

File tree

1 file changed

+24
-26
lines changed

1 file changed

+24
-26
lines changed

src/libsyntax_pos/hygiene.rs

+24-26
Original file line numberDiff line numberDiff line change
@@ -112,31 +112,14 @@ impl Mark {
112112
HygieneData::with(|data| data.marks[self.0 as usize].default_transparency = transparency)
113113
}
114114

115-
pub fn is_descendant_of(mut self, ancestor: Mark) -> bool {
116-
HygieneData::with(|data| {
117-
while self != ancestor {
118-
if self == Mark::root() {
119-
return false;
120-
}
121-
self = data.marks[self.0 as usize].parent;
122-
}
123-
true
124-
})
115+
pub fn is_descendant_of(self, ancestor: Mark) -> bool {
116+
HygieneData::with(|data| data.is_descendant_of(self, ancestor))
125117
}
126118

127119
/// `mark.outer_is_descendant_of(ctxt)` is equivalent to but faster than
128120
/// `mark.is_descendant_of(ctxt.outer())`.
129-
pub fn outer_is_descendant_of(mut self, ctxt: SyntaxContext) -> bool {
130-
HygieneData::with(|data| {
131-
let outer = data.syntax_contexts[ctxt.0 as usize].outer_mark;
132-
while self != outer {
133-
if self == Mark::root() {
134-
return false;
135-
}
136-
self = data.marks[self.0 as usize].parent;
137-
}
138-
true
139-
})
121+
pub fn outer_is_descendant_of(self, ctxt: SyntaxContext) -> bool {
122+
HygieneData::with(|data| data.is_descendant_of(self, data.outer(ctxt)))
140123
}
141124

142125
/// Computes a mark such that both input marks are descendants of (or equal to) the returned
@@ -216,6 +199,24 @@ impl HygieneData {
216199
fn with<T, F: FnOnce(&mut HygieneData) -> T>(f: F) -> T {
217200
GLOBALS.with(|globals| f(&mut *globals.hygiene_data.borrow_mut()))
218201
}
202+
203+
fn outer(&self, ctxt: SyntaxContext) -> Mark {
204+
self.syntax_contexts[ctxt.0 as usize].outer_mark
205+
}
206+
207+
fn expn_info(&self, mark: Mark) -> Option<ExpnInfo> {
208+
self.marks[mark.0 as usize].expn_info.clone()
209+
}
210+
211+
fn is_descendant_of(&self, mut mark: Mark, ancestor: Mark) -> bool {
212+
while mark != ancestor {
213+
if mark == Mark::root() {
214+
return false;
215+
}
216+
mark = self.marks[mark.0 as usize].parent;
217+
}
218+
true
219+
}
219220
}
220221

221222
pub fn clear_markings() {
@@ -514,17 +515,14 @@ impl SyntaxContext {
514515

515516
#[inline]
516517
pub fn outer(self) -> Mark {
517-
HygieneData::with(|data| data.syntax_contexts[self.0 as usize].outer_mark)
518+
HygieneData::with(|data| data.outer(self))
518519
}
519520

520521
/// `ctxt.outer_expn_info()` is equivalent to but faster than
521522
/// `ctxt.outer().expn_info()`.
522523
#[inline]
523524
pub fn outer_expn_info(self) -> Option<ExpnInfo> {
524-
HygieneData::with(|data| {
525-
let outer = data.syntax_contexts[self.0 as usize].outer_mark;
526-
data.marks[outer.0 as usize].expn_info.clone()
527-
})
525+
HygieneData::with(|data| data.expn_info(data.outer(self)))
528526
}
529527

530528
pub fn dollar_crate_name(self) -> Symbol {

0 commit comments

Comments
 (0)