Skip to content

Commit a1dd39e

Browse files
committed
Track distinct spans for open and close delimiter
1 parent c5a561c commit a1dd39e

File tree

17 files changed

+163
-126
lines changed

17 files changed

+163
-126
lines changed

src/libproc_macro/lib.rs

+10-24
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ use std::str::FromStr;
6363
use syntax::errors::DiagnosticBuilder;
6464
use syntax::parse::{self, token};
6565
use syntax::symbol::Symbol;
66-
use syntax::tokenstream;
67-
use syntax_pos::{BytePos, Pos, FileName};
66+
use syntax::tokenstream::{self, DelimSpan};
67+
use syntax_pos::{Pos, FileName};
6868

6969
/// The main type provided by this crate, representing an abstract stream of
7070
/// tokens, or, more specifically, a sequence of token trees.
@@ -609,7 +609,7 @@ impl fmt::Display for TokenTree {
609609
pub struct Group {
610610
delimiter: Delimiter,
611611
stream: TokenStream,
612-
span: Span,
612+
span: DelimSpan,
613613
}
614614

615615
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
@@ -650,7 +650,7 @@ impl Group {
650650
Group {
651651
delimiter: delimiter,
652652
stream: stream,
653-
span: Span::call_site(),
653+
span: DelimSpan::from_single(Span::call_site().0),
654654
}
655655
}
656656

@@ -678,43 +678,29 @@ impl Group {
678678
/// ```
679679
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
680680
pub fn span(&self) -> Span {
681-
self.span
681+
Span(self.span.entire())
682682
}
683683

684-
/// Returns the span pointing to the opening delimiter of this group, or the
685-
/// span of the entire group if this is a None-delimited group.
684+
/// Returns the span pointing to the opening delimiter of this group.
686685
///
687686
/// ```text
688687
/// pub fn span_open(&self) -> Span {
689688
/// ^
690689
/// ```
691690
#[unstable(feature = "proc_macro_span", issue = "38356")]
692691
pub fn span_open(&self) -> Span {
693-
if self.delimiter == Delimiter::None {
694-
self.span
695-
} else {
696-
let lo = self.span.0.lo();
697-
let new_hi = BytePos::from_usize(lo.to_usize() + 1);
698-
Span(self.span.0.with_hi(new_hi))
699-
}
692+
Span(self.span.open)
700693
}
701694

702-
/// Returns the span pointing to the closing delimiter of this group, or the
703-
/// span of the entire group if this is a None-delimited group.
695+
/// Returns the span pointing to the closing delimiter of this group.
704696
///
705697
/// ```text
706698
/// pub fn span_close(&self) -> Span {
707699
/// ^
708700
/// ```
709701
#[unstable(feature = "proc_macro_span", issue = "38356")]
710702
pub fn span_close(&self) -> Span {
711-
let hi = self.span.0.hi();
712-
if self.delimiter == Delimiter::None || hi.to_usize() == 0 {
713-
self.span
714-
} else {
715-
let new_lo = BytePos::from_usize(hi.to_usize() - 1);
716-
Span(self.span.0.with_lo(new_lo))
717-
}
703+
Span(self.span.close)
718704
}
719705

720706
/// Configures the span for this `Group`'s delimiters, but not its internal
@@ -725,7 +711,7 @@ impl Group {
725711
/// tokens at the level of the `Group`.
726712
#[stable(feature = "proc_macro_lib2", since = "1.29.0")]
727713
pub fn set_span(&mut self, span: Span) {
728-
self.span = span;
714+
self.span = DelimSpan::from_single(span.0);
729715
}
730716
}
731717

src/libproc_macro/rustc.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ impl TokenTree {
6464
tokenstream::TokenTree::Delimited(span, delimed) => {
6565
let delimiter = Delimiter::from_internal(delimed.delim);
6666
let mut g = Group::new(delimiter, ::TokenStream(delimed.tts.into()));
67-
g.set_span(Span(span));
67+
g.span = span;
6868
return g.into();
6969
}
7070
};
@@ -192,7 +192,7 @@ impl TokenTree {
192192
self::TokenTree::Punct(tt) => (tt.as_char(), tt.spacing(), tt.span()),
193193
self::TokenTree::Group(tt) => {
194194
return TokenTree::Delimited(
195-
tt.span.0,
195+
tt.span,
196196
Delimited {
197197
delim: tt.delimiter.to_internal(),
198198
tts: tt.stream.0.into(),

src/librustc/ich/hcx.rs

+12
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ use syntax::ast;
2828
use syntax::source_map::SourceMap;
2929
use syntax::ext::hygiene::SyntaxContext;
3030
use syntax::symbol::Symbol;
31+
use syntax::tokenstream::DelimSpan;
3132
use syntax_pos::{Span, DUMMY_SP};
3233
use syntax_pos::hygiene;
3334

@@ -396,6 +397,17 @@ impl<'a> HashStable<StableHashingContext<'a>> for Span {
396397
}
397398
}
398399

400+
impl<'a> HashStable<StableHashingContext<'a>> for DelimSpan {
401+
fn hash_stable<W: StableHasherResult>(
402+
&self,
403+
hcx: &mut StableHashingContext<'a>,
404+
hasher: &mut StableHasher<W>,
405+
) {
406+
self.open.hash_stable(hcx, hasher);
407+
self.close.hash_stable(hcx, hasher);
408+
}
409+
}
410+
399411
pub fn hash_stable_trait_impls<'a, 'gcx, W, R>(
400412
hcx: &mut StableHashingContext<'a>,
401413
hasher: &mut StableHasher<W>,

src/librustc_resolve/macros.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ use syntax::parse::parser::PathStyle;
3535
use syntax::parse::token::{self, Token};
3636
use syntax::ptr::P;
3737
use syntax::symbol::{Symbol, keywords};
38-
use syntax::tokenstream::{TokenStream, TokenTree, Delimited};
38+
use syntax::tokenstream::{TokenStream, TokenTree, Delimited, DelimSpan};
3939
use syntax::util::lev_distance::find_best_match_for_name;
4040
use syntax_pos::{Span, DUMMY_SP};
4141
use errors::Applicability;
@@ -279,7 +279,8 @@ impl<'a, 'crateloader: 'a> base::Resolver for Resolver<'a, 'crateloader> {
279279
tokens.push(TokenTree::Token(path.span, tok).into());
280280
}
281281
}
282-
attrs[i].tokens = TokenTree::Delimited(attrs[i].span, Delimited {
282+
let delim_span = DelimSpan::from_single(attrs[i].span);
283+
attrs[i].tokens = TokenTree::Delimited(delim_span, Delimited {
283284
delim: token::Paren,
284285
tts: TokenStream::concat(tokens).into(),
285286
}).into();

src/libsyntax/attr/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ use parse::token::{self, Token};
3434
use ptr::P;
3535
use symbol::Symbol;
3636
use ThinVec;
37-
use tokenstream::{TokenStream, TokenTree, Delimited};
37+
use tokenstream::{TokenStream, TokenTree, Delimited, DelimSpan};
3838
use GLOBALS;
3939

4040
use std::iter;
@@ -535,7 +535,7 @@ impl MetaItemKind {
535535
}
536536
tokens.push(item.node.tokens());
537537
}
538-
TokenTree::Delimited(span, Delimited {
538+
TokenTree::Delimited(DelimSpan::from_single(span), Delimited {
539539
delim: token::Paren,
540540
tts: TokenStream::concat(tokens).into(),
541541
}).into()

src/libsyntax/ext/quote.rs

+13-9
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,14 @@
1010

1111
use ast::{self, Arg, Arm, Block, Expr, Item, Pat, Stmt, Ty};
1212
use source_map::respan;
13-
use syntax_pos::Span;
13+
use syntax_pos::{Span, DUMMY_SP};
1414
use ext::base::ExtCtxt;
1515
use ext::base;
1616
use ext::build::AstBuilder;
1717
use parse::parser::{Parser, PathStyle};
1818
use parse::token;
1919
use ptr::P;
20-
use tokenstream::{TokenStream, TokenTree};
20+
use tokenstream::{DelimSpan, TokenStream, TokenTree};
2121

2222
/// Quasiquoting works via token trees.
2323
///
@@ -36,7 +36,7 @@ pub mod rt {
3636
use symbol::Symbol;
3737
use ThinVec;
3838

39-
use tokenstream::{self, TokenTree, TokenStream};
39+
use tokenstream::{self, DelimSpan, TokenTree, TokenStream};
4040

4141
pub use parse::new_parser_from_tts;
4242
pub use syntax_pos::{BytePos, Span, DUMMY_SP, FileName};
@@ -245,7 +245,8 @@ pub mod rt {
245245
}
246246
inner.push(self.tokens.clone());
247247

248-
r.push(TokenTree::Delimited(self.span, tokenstream::Delimited {
248+
let delim_span = DelimSpan::from_single(self.span);
249+
r.push(TokenTree::Delimited(delim_span, tokenstream::Delimited {
249250
delim: token::Bracket, tts: TokenStream::concat(inner).into()
250251
}));
251252
r
@@ -261,7 +262,7 @@ pub mod rt {
261262

262263
impl ToTokens for () {
263264
fn to_tokens(&self, _cx: &ExtCtxt) -> Vec<TokenTree> {
264-
vec![TokenTree::Delimited(DUMMY_SP, tokenstream::Delimited {
265+
vec![TokenTree::Delimited(DelimSpan::dummy(), tokenstream::Delimited {
265266
delim: token::Paren,
266267
tts: TokenStream::empty().into(),
267268
})]
@@ -385,13 +386,16 @@ pub fn unflatten(tts: Vec<TokenTree>) -> Vec<TokenTree> {
385386

386387
let mut results = Vec::new();
387388
let mut result = Vec::new();
389+
let mut open_span = DUMMY_SP;
388390
for tree in tts {
389391
match tree {
390-
TokenTree::Token(_, token::OpenDelim(..)) => {
392+
TokenTree::Token(span, token::OpenDelim(..)) => {
393+
open_span = span;
391394
results.push(::std::mem::replace(&mut result, Vec::new()));
392395
}
393396
TokenTree::Token(span, token::CloseDelim(delim)) => {
394-
let tree = TokenTree::Delimited(span, Delimited {
397+
let delim_span = DelimSpan::from_pair(open_span, span);
398+
let tree = TokenTree::Delimited(delim_span, Delimited {
395399
delim,
396400
tts: result.into_iter().map(TokenStream::from).collect::<TokenStream>().into(),
397401
});
@@ -756,9 +760,9 @@ fn statements_mk_tt(cx: &ExtCtxt, tt: &TokenTree, quoted: bool) -> Vec<ast::Stmt
756760
vec![cx.stmt_expr(e_push)]
757761
},
758762
TokenTree::Delimited(span, ref delimed) => {
759-
let mut stmts = statements_mk_tt(cx, &delimed.open_tt(span), false);
763+
let mut stmts = statements_mk_tt(cx, &delimed.open_tt(span.open), false);
760764
stmts.extend(statements_mk_tts(cx, delimed.stream()));
761-
stmts.extend(statements_mk_tt(cx, &delimed.close_tt(span), false));
765+
stmts.extend(statements_mk_tt(cx, &delimed.close_tt(span.close), false));
762766
stmts
763767
}
764768
}

src/libsyntax/ext/tt/macro_parser.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ pub use self::ParseResult::*;
8585
use self::TokenTreeOrTokenTreeSlice::*;
8686

8787
use ast::Ident;
88-
use syntax_pos::{self, BytePos, Span};
88+
use syntax_pos::{self, Span};
8989
use errors::FatalError;
9090
use ext::tt::quoted::{self, TokenTree};
9191
use parse::{Directory, ParseSess};
@@ -94,7 +94,7 @@ use parse::token::{self, DocComment, Nonterminal, Token};
9494
use print::pprust;
9595
use OneVector;
9696
use symbol::keywords;
97-
use tokenstream::TokenStream;
97+
use tokenstream::{DelimSpan, TokenStream};
9898

9999
use rustc_data_structures::fx::FxHashMap;
100100
use std::collections::hash_map::Entry::{Occupied, Vacant};
@@ -154,7 +154,7 @@ struct MatcherPos<'a> {
154154
/// The beginning position in the source that the beginning of this matcher corresponds to. In
155155
/// other words, the token in the source at `sp_lo` is matched against the first token of the
156156
/// matcher.
157-
sp_lo: BytePos,
157+
sp_lo: Span,
158158

159159
/// For each named metavar in the matcher, we keep track of token trees matched against the
160160
/// metavar by the black box parser. In particular, there may be more than one match per
@@ -285,7 +285,7 @@ fn create_matches(len: usize) -> Vec<Rc<Vec<NamedMatch>>> {
285285

286286
/// Generate the top-level matcher position in which the "dot" is before the first token of the
287287
/// matcher `ms` and we are going to start matching at position `lo` in the source.
288-
fn initial_matcher_pos(ms: &[TokenTree], lo: BytePos) -> MatcherPos {
288+
fn initial_matcher_pos(ms: &[TokenTree], lo: Span) -> MatcherPos {
289289
let match_idx_hi = count_names(ms);
290290
let matches = create_matches(match_idx_hi);
291291
MatcherPos {
@@ -332,7 +332,7 @@ fn initial_matcher_pos(ms: &[TokenTree], lo: BytePos) -> MatcherPos {
332332
/// token tree it was derived from.
333333
#[derive(Debug, Clone)]
334334
pub enum NamedMatch {
335-
MatchedSeq(Rc<Vec<NamedMatch>>, syntax_pos::Span),
335+
MatchedSeq(Rc<Vec<NamedMatch>>, DelimSpan),
336336
MatchedNonterminal(Rc<Nonterminal>),
337337
}
338338

@@ -488,7 +488,7 @@ fn inner_parse_loop<'a>(
488488
// Add matches from this repetition to the `matches` of `up`
489489
for idx in item.match_lo..item.match_hi {
490490
let sub = item.matches[idx].clone();
491-
let span = span.with_lo(item.sp_lo);
491+
let span = DelimSpan::from_pair(item.sp_lo, span);
492492
new_pos.push_match(idx, MatchedSeq(sub, span));
493493
}
494494

@@ -556,7 +556,7 @@ fn inner_parse_loop<'a>(
556556
match_cur: item.match_cur,
557557
match_hi: item.match_cur + seq.num_captures,
558558
up: Some(item),
559-
sp_lo: sp.lo(),
559+
sp_lo: sp.open,
560560
top_elts: Tt(TokenTree::Sequence(sp, seq)),
561561
})));
562562
}
@@ -643,7 +643,7 @@ pub fn parse(
643643
//
644644
// This MatcherPos instance is allocated on the stack. All others -- and
645645
// there are frequently *no* others! -- are allocated on the heap.
646-
let mut initial = initial_matcher_pos(ms, parser.span.lo());
646+
let mut initial = initial_matcher_pos(ms, parser.span);
647647
let mut cur_items = smallvec![MatcherPosHandle::Ref(&mut initial)];
648648
let mut next_items = Vec::new();
649649

0 commit comments

Comments
 (0)