-
Notifications
You must be signed in to change notification settings - Fork 12.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use the macro structure spans instead of the invocation #43352
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -131,6 +131,15 @@ impl TokenTree { | |
} | ||
} | ||
|
||
/// Modify the `TokenTree`'s span inplace. | ||
pub fn set_span(&mut self, span: Span) { | ||
match *self { | ||
TokenTree::Token(ref mut sp, _) | TokenTree::Delimited(ref mut sp, _) => { | ||
*sp = span; | ||
} | ||
} | ||
} | ||
|
||
/// Indicates if the stream is a token that is equal to the provided token. | ||
pub fn eq_token(&self, t: Token) -> bool { | ||
match *self { | ||
|
@@ -190,6 +199,14 @@ impl PartialEq<TokenStream> for TokenStream { | |
} | ||
|
||
impl TokenStream { | ||
pub fn len(&self) -> usize { | ||
if let TokenStreamKind::Stream(ref slice) = self.kind { | ||
slice.len() | ||
} else { | ||
0 | ||
} | ||
} | ||
|
||
pub fn empty() -> TokenStream { | ||
TokenStream { kind: TokenStreamKind::Empty } | ||
} | ||
|
@@ -241,6 +258,21 @@ impl TokenStream { | |
} | ||
} | ||
|
||
pub fn map_pos<F: FnMut(usize, TokenTree) -> TokenTree>(self, mut f: F) -> TokenStream { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd rename this into There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
let mut trees = self.into_trees(); | ||
let mut result = Vec::new(); | ||
let mut i = 0; | ||
while let Some(stream) = trees.next_as_stream() { | ||
result.push(match stream.kind { | ||
TokenStreamKind::Tree(tree) => f(i, tree).into(), | ||
TokenStreamKind::JointTree(tree) => f(i, tree).joint(), | ||
_ => unreachable!() | ||
}); | ||
i += 1; | ||
} | ||
TokenStream::concat(result) | ||
} | ||
|
||
pub fn map<F: FnMut(TokenTree) -> TokenTree>(self, mut f: F) -> TokenStream { | ||
let mut trees = self.into_trees(); | ||
let mut result = Vec::new(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -100,6 +100,7 @@ impl Span { | |
if self.source_equal(&DUMMY_SP) { other } else { self } | ||
} | ||
|
||
/// Return true if `self` fully encloses `other`. | ||
pub fn contains(self, other: Span) -> bool { | ||
self.lo <= other.lo && other.hi <= self.hi | ||
} | ||
|
@@ -184,15 +185,32 @@ impl Span { | |
result | ||
} | ||
|
||
pub fn empty_ctxt(&self) -> bool { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This new function is unused. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removed. |
||
self.ctxt == SyntaxContext::empty() | ||
} | ||
|
||
/// Return a `Span` that would enclose both `self` and `end`. | ||
pub fn to(self, end: Span) -> Span { | ||
let lo = if self.lo < end.lo { | ||
self.lo | ||
} else { | ||
end.lo | ||
}; | ||
let hi = if self.hi > end.hi { | ||
self.hi | ||
} else { | ||
end.hi | ||
}; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Probably better to use standard There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done. |
||
// FIXME(jseyfried): self.ctxt should always equal end.ctxt here (c.f. issue #23480) | ||
if self.ctxt == SyntaxContext::empty() { | ||
Span { lo: self.lo, ..end } | ||
let ctxt = if self.ctxt == SyntaxContext::empty() { | ||
end.ctxt | ||
} else { | ||
Span { hi: end.hi, ..self } | ||
} | ||
self.ctxt | ||
}; | ||
Span {lo, hi, ctxt} | ||
} | ||
|
||
/// Return a `Span` between the end of `self` to the beginning of `end`. | ||
pub fn between(self, end: Span) -> Span { | ||
Span { | ||
lo: self.hi, | ||
|
@@ -205,6 +223,7 @@ impl Span { | |
} | ||
} | ||
|
||
/// Return a `Span` between the beginning of `self` to the beginning of `end`. | ||
pub fn until(self, end: Span) -> Span { | ||
Span { | ||
lo: self.lo, | ||
|
@@ -852,6 +871,7 @@ pub struct FileLines { | |
thread_local!(pub static SPAN_DEBUG: Cell<fn(Span, &mut fmt::Formatter) -> fmt::Result> = | ||
Cell::new(default_span_debug)); | ||
|
||
#[derive(Debug)] | ||
pub struct MacroBacktrace { | ||
/// span where macro was applied to generate this code | ||
pub call_site: Span, | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
macro_rules! m { | ||
($a:tt $b:tt) => { | ||
$b $a; | ||
} | ||
} | ||
|
||
fn main() { | ||
m!(S struct); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
warning: struct is never used: `S` | ||
--> $DIR/macro-span-replacement.rs:13:9 | ||
| | ||
13 | $b $a; | ||
| ^^^^^^ | ||
... | ||
18 | m!(S struct); | ||
| ------------- in this macro invocation | ||
| | ||
= note: #[warn(dead_code)] on by default | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just to be sure, everything is under
Rc
and cloning/mapping is cheap, right?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All I need is the spans after this, so I'm cloning the spans first into their own
Vec
.