Skip to content

Commit

Permalink
Refactor how spans are combined in the parser.
Browse files Browse the repository at this point in the history
  • Loading branch information
jseyfried committed Mar 29, 2017
1 parent ec7c0ae commit f08d5ad
Show file tree
Hide file tree
Showing 22 changed files with 363 additions and 438 deletions.
5 changes: 2 additions & 3 deletions src/librustc/middle/resolve_lifetime.rs
Expand Up @@ -29,7 +29,7 @@ use syntax::ast;
use syntax::attr;
use syntax::ptr::P;
use syntax::symbol::keywords;
use syntax_pos::{mk_sp, Span};
use syntax_pos::Span;
use errors::DiagnosticBuilder;
use util::nodemap::{NodeMap, NodeSet, FxHashSet, FxHashMap, DefIdMap};
use rustc_back::slice;
Expand Down Expand Up @@ -1468,8 +1468,7 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
self.resolve_lifetime_ref(bound);
} else {
self.insert_lifetime(bound, Region::Static);
let full_span = mk_sp(lifetime_i.lifetime.span.lo, bound.span.hi);
self.sess.struct_span_warn(full_span,
self.sess.struct_span_warn(lifetime_i.lifetime.span.to(bound.span),
&format!("unnecessary lifetime parameter `{}`", lifetime_i.lifetime.name))
.help(&format!("you can use the `'static` lifetime directly, in place \
of `{}`", lifetime_i.lifetime.name))
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_metadata/cstore_impl.rs
Expand Up @@ -36,7 +36,7 @@ use syntax::ast;
use syntax::attr;
use syntax::parse::filemap_to_stream;
use syntax::symbol::Symbol;
use syntax_pos::{mk_sp, Span};
use syntax_pos::{Span, NO_EXPANSION};
use rustc::hir::svh::Svh;
use rustc_back::target::Target;
use rustc::hir;
Expand Down Expand Up @@ -395,7 +395,7 @@ impl CrateStore for cstore::CStore {
let source_name = format!("<{} macros>", name);

let filemap = sess.parse_sess.codemap().new_filemap(source_name, None, def.body);
let local_span = mk_sp(filemap.start_pos, filemap.end_pos);
let local_span = Span { lo: filemap.start_pos, hi: filemap.end_pos, ctxt: NO_EXPANSION };
let body = filemap_to_stream(&sess.parse_sess, filemap);

// Mark the attrs as used
Expand Down
6 changes: 3 additions & 3 deletions src/librustc_metadata/decoder.rs
Expand Up @@ -39,7 +39,7 @@ use syntax::attr;
use syntax::ast;
use syntax::codemap;
use syntax::ext::base::MacroKind;
use syntax_pos::{self, Span, BytePos, Pos, DUMMY_SP};
use syntax_pos::{self, Span, BytePos, Pos, DUMMY_SP, NO_EXPANSION};

pub struct DecodeContext<'a, 'tcx: 'a> {
opaque: opaque::Decoder<'a>,
Expand Down Expand Up @@ -243,7 +243,7 @@ impl<'a, 'tcx> SpecializedDecoder<Span> for DecodeContext<'a, 'tcx> {
let sess = if let Some(sess) = self.sess {
sess
} else {
return Ok(syntax_pos::mk_sp(lo, hi));
return Ok(Span { lo: lo, hi: hi, ctxt: NO_EXPANSION });
};

let (lo, hi) = if lo > hi {
Expand Down Expand Up @@ -290,7 +290,7 @@ impl<'a, 'tcx> SpecializedDecoder<Span> for DecodeContext<'a, 'tcx> {
let lo = (lo - filemap.original_start_pos) + filemap.translated_filemap.start_pos;
let hi = (hi - filemap.original_start_pos) + filemap.translated_filemap.start_pos;

Ok(syntax_pos::mk_sp(lo, hi))
Ok(Span { lo: lo, hi: hi, ctxt: NO_EXPANSION })
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/librustc_save_analysis/lib.rs
Expand Up @@ -742,7 +742,7 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
let ident_start = text.find(&name).expect("Name not in signature?");
let ident_end = ident_start + name.len();
Signature {
span: mk_sp(item.span.lo, item.span.lo + BytePos(text.len() as u32)),
span: Span { hi: item.span.lo + BytePos(text.len() as u32), ..item.span },
text: text,
ident_start: ident_start,
ident_end: ident_end,
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_save_analysis/span_utils.rs
Expand Up @@ -305,10 +305,10 @@ impl<'a> SpanUtils<'a> {
continue;
}
if let TokenTree::Token(_, token::Semi) = tok {
return self.snippet(mk_sp(first_span.lo, prev.span().hi));
return self.snippet(first_span.to(prev.span()));
} else if let TokenTree::Delimited(_, ref d) = tok {
if d.delim == token::Brace {
return self.snippet(mk_sp(first_span.lo, prev.span().hi));
return self.snippet(first_span.to(prev.span()));
}
}
prev = tok;
Expand Down
10 changes: 5 additions & 5 deletions src/libsyntax/ast.rs
Expand Up @@ -17,7 +17,7 @@ pub use self::PathParameters::*;
pub use symbol::{Ident, Symbol as Name};
pub use util::ThinVec;

use syntax_pos::{mk_sp, BytePos, Span, DUMMY_SP};
use syntax_pos::{Span, DUMMY_SP};
use codemap::{respan, Spanned};
use abi::Abi;
use ext::hygiene::{Mark, SyntaxContext};
Expand Down Expand Up @@ -1433,7 +1433,7 @@ impl Arg {
TyKind::Rptr(lt, MutTy{ref ty, mutbl}) if ty.node == TyKind::ImplicitSelf => {
Some(respan(self.pat.span, SelfKind::Region(lt, mutbl)))
}
_ => Some(respan(mk_sp(self.pat.span.lo, self.ty.span.hi),
_ => Some(respan(self.pat.span.to(self.ty.span),
SelfKind::Explicit(self.ty.clone(), mutbl))),
}
}
Expand All @@ -1450,7 +1450,7 @@ impl Arg {
}

pub fn from_self(eself: ExplicitSelf, eself_ident: SpannedIdent) -> Arg {
let span = mk_sp(eself.span.lo, eself_ident.span.hi);
let span = eself.span.to(eself_ident.span);
let infer_ty = P(Ty {
id: DUMMY_NODE_ID,
node: TyKind::ImplicitSelf,
Expand Down Expand Up @@ -1687,11 +1687,11 @@ pub struct PolyTraitRef {
}

impl PolyTraitRef {
pub fn new(lifetimes: Vec<LifetimeDef>, path: Path, lo: BytePos, hi: BytePos) -> Self {
pub fn new(lifetimes: Vec<LifetimeDef>, path: Path, span: Span) -> Self {
PolyTraitRef {
bound_lifetimes: lifetimes,
trait_ref: TraitRef { path: path, ref_id: DUMMY_NODE_ID },
span: mk_sp(lo, hi),
span: span,
}
}
}
Expand Down
15 changes: 7 additions & 8 deletions src/libsyntax/attr.rs
Expand Up @@ -18,8 +18,8 @@ use ast;
use ast::{AttrId, Attribute, Name, Ident};
use ast::{MetaItem, MetaItemKind, NestedMetaItem, NestedMetaItemKind};
use ast::{Lit, LitKind, Expr, ExprKind, Item, Local, Stmt, StmtKind};
use codemap::{Spanned, spanned, dummy_spanned, mk_sp};
use syntax_pos::{Span, BytePos, DUMMY_SP};
use codemap::{Spanned, respan, dummy_spanned};
use syntax_pos::{Span, DUMMY_SP};
use errors::Handler;
use feature_gate::{Features, GatedCfg};
use parse::lexer::comments::{doc_comment_style, strip_doc_comment_decoration};
Expand Down Expand Up @@ -447,17 +447,16 @@ pub fn mk_spanned_attr_outer(sp: Span, id: AttrId, item: MetaItem) -> Attribute
}
}

pub fn mk_sugared_doc_attr(id: AttrId, text: Symbol, lo: BytePos, hi: BytePos)
-> Attribute {
pub fn mk_sugared_doc_attr(id: AttrId, text: Symbol, span: Span) -> Attribute {
let style = doc_comment_style(&text.as_str());
let lit = spanned(lo, hi, LitKind::Str(text, ast::StrStyle::Cooked));
let lit = respan(span, LitKind::Str(text, ast::StrStyle::Cooked));
Attribute {
id: id,
style: style,
path: ast::Path::from_ident(mk_sp(lo, hi), ast::Ident::from_str("doc")),
tokens: MetaItemKind::NameValue(lit).tokens(mk_sp(lo, hi)),
path: ast::Path::from_ident(span, ast::Ident::from_str("doc")),
tokens: MetaItemKind::NameValue(lit).tokens(span),
is_sugared_doc: true,
span: mk_sp(lo, hi),
span: span,
}
}

Expand Down
4 changes: 0 additions & 4 deletions src/libsyntax/codemap.rs
Expand Up @@ -49,10 +49,6 @@ pub struct Spanned<T> {
pub span: Span,
}

pub fn spanned<T>(lo: BytePos, hi: BytePos, t: T) -> Spanned<T> {
respan(mk_sp(lo, hi), t)
}

pub fn respan<T>(sp: Span, t: T) -> Spanned<T> {
Spanned {node: t, span: sp}
}
Expand Down
9 changes: 4 additions & 5 deletions src/libsyntax/ext/tt/macro_parser.rs
Expand Up @@ -79,7 +79,7 @@ pub use self::ParseResult::*;
use self::TokenTreeOrTokenTreeVec::*;

use ast::Ident;
use syntax_pos::{self, BytePos, mk_sp, Span};
use syntax_pos::{self, BytePos, Span};
use codemap::Spanned;
use errors::FatalError;
use ext::tt::quoted::{self, TokenTree};
Expand Down Expand Up @@ -285,7 +285,7 @@ fn inner_parse_loop(sess: &ParseSess,
eof_eis: &mut SmallVector<Box<MatcherPos>>,
bb_eis: &mut SmallVector<Box<MatcherPos>>,
token: &Token,
span: &syntax_pos::Span)
span: syntax_pos::Span)
-> ParseResult<()> {
while let Some(mut ei) = cur_eis.pop() {
// When unzipped trees end, remove them
Expand Down Expand Up @@ -323,8 +323,7 @@ fn inner_parse_loop(sess: &ParseSess,
for idx in ei.match_lo..ei.match_hi {
let sub = ei.matches[idx].clone();
new_pos.matches[idx]
.push(Rc::new(MatchedSeq(sub, mk_sp(ei.sp_lo,
span.hi))));
.push(Rc::new(MatchedSeq(sub, Span { lo: ei.sp_lo, ..span })));
}

new_pos.match_cur = ei.match_hi;
Expand Down Expand Up @@ -426,7 +425,7 @@ pub fn parse(sess: &ParseSess, tts: TokenStream, ms: &[TokenTree], directory: Op
assert!(next_eis.is_empty());

match inner_parse_loop(sess, &mut cur_eis, &mut next_eis, &mut eof_eis, &mut bb_eis,
&parser.token, &parser.span) {
&parser.token, parser.span) {
Success(_) => {},
Failure(sp, tok) => return Failure(sp, tok),
Error(sp, msg) => return Error(sp, msg),
Expand Down
29 changes: 12 additions & 17 deletions src/libsyntax/parse/attr.rs
Expand Up @@ -10,8 +10,7 @@

use attr;
use ast;
use syntax_pos::{mk_sp, Span};
use codemap::spanned;
use codemap::respan;
use parse::common::SeqSep;
use parse::PResult;
use parse::token::{self, Nonterminal};
Expand Down Expand Up @@ -49,8 +48,7 @@ impl<'a> Parser<'a> {
just_parsed_doc_comment = false;
}
token::DocComment(s) => {
let Span { lo, hi, .. } = self.span;
let attr = attr::mk_sugared_doc_attr(attr::mk_attr_id(), s, lo, hi);
let attr = attr::mk_sugared_doc_attr(attr::mk_attr_id(), s, self.span);
if attr.style != ast::AttrStyle::Outer {
let mut err = self.fatal("expected outer doc comment");
err.note("inner doc comments like this (starting with \
Expand Down Expand Up @@ -94,7 +92,7 @@ impl<'a> Parser<'a> {
self.token);
let (span, path, tokens, mut style) = match self.token {
token::Pound => {
let lo = self.span.lo;
let lo = self.span;
self.bump();

if inner_parse_policy == InnerAttributeParsePolicy::Permitted {
Expand Down Expand Up @@ -122,9 +120,9 @@ impl<'a> Parser<'a> {
self.expect(&token::OpenDelim(token::Bracket))?;
let (path, tokens) = self.parse_path_and_tokens()?;
self.expect(&token::CloseDelim(token::Bracket))?;
let hi = self.prev_span.hi;
let hi = self.prev_span;

(mk_sp(lo, hi), path, tokens, style)
(lo.to(hi), path, tokens, style)
}
_ => {
let token_str = self.this_token_to_string();
Expand Down Expand Up @@ -189,8 +187,7 @@ impl<'a> Parser<'a> {
}
token::DocComment(s) => {
// we need to get the position of this token before we bump.
let Span { lo, hi, .. } = self.span;
let attr = attr::mk_sugared_doc_attr(attr::mk_attr_id(), s, lo, hi);
let attr = attr::mk_sugared_doc_attr(attr::mk_attr_id(), s, self.span);
if attr.style == ast::AttrStyle::Inner {
attrs.push(attr);
self.bump();
Expand Down Expand Up @@ -238,11 +235,10 @@ impl<'a> Parser<'a> {
return Ok(meta);
}

let lo = self.span.lo;
let lo = self.span;
let ident = self.parse_ident()?;
let node = self.parse_meta_item_kind()?;
let hi = self.prev_span.hi;
Ok(ast::MetaItem { name: ident.name, node: node, span: mk_sp(lo, hi) })
Ok(ast::MetaItem { name: ident.name, node: node, span: lo.to(self.prev_span) })
}

pub fn parse_meta_item_kind(&mut self) -> PResult<'a, ast::MetaItemKind> {
Expand All @@ -258,26 +254,25 @@ impl<'a> Parser<'a> {

/// matches meta_item_inner : (meta_item | UNSUFFIXED_LIT) ;
fn parse_meta_item_inner(&mut self) -> PResult<'a, ast::NestedMetaItem> {
let sp = self.span;
let lo = self.span.lo;
let lo = self.span;

match self.parse_unsuffixed_lit() {
Ok(lit) => {
return Ok(spanned(lo, self.prev_span.hi, ast::NestedMetaItemKind::Literal(lit)))
return Ok(respan(lo.to(self.prev_span), ast::NestedMetaItemKind::Literal(lit)))
}
Err(ref mut err) => self.diagnostic().cancel(err)
}

match self.parse_meta_item() {
Ok(mi) => {
return Ok(spanned(lo, self.prev_span.hi, ast::NestedMetaItemKind::MetaItem(mi)))
return Ok(respan(lo.to(self.prev_span), ast::NestedMetaItemKind::MetaItem(mi)))
}
Err(ref mut err) => self.diagnostic().cancel(err)
}

let found = self.this_token_to_string();
let msg = format!("expected unsuffixed literal or identifier, found {}", found);
Err(self.diagnostic().struct_span_err(sp, &msg))
Err(self.diagnostic().struct_span_err(lo, &msg))
}

/// matches meta_seq = ( COMMASEP(meta_item_inner) )
Expand Down

0 comments on commit f08d5ad

Please sign in to comment.