1
Fork 0

Give spans to individual path segments in AST

This commit is contained in:
Vadim Petrochenkov 2017-03-08 20:30:06 +03:00 committed by Alex Crichton
parent f573db4f80
commit 32575a0487
10 changed files with 60 additions and 41 deletions

View file

@ -3124,11 +3124,10 @@ impl<'a> Resolver<'a> {
if ident.name == lookup_name && ns == namespace { if ident.name == lookup_name && ns == namespace {
if filter_fn(name_binding.def()) { if filter_fn(name_binding.def()) {
// create the path // create the path
let span = name_binding.span;
let mut segms = path_segments.clone(); let mut segms = path_segments.clone();
segms.push(ident.into()); segms.push(ast::PathSegment::from_ident(ident, name_binding.span));
let path = Path { let path = Path {
span: span, span: name_binding.span,
segments: segms, segments: segms,
}; };
// the entity is accessible in the following cases: // the entity is accessible in the following cases:
@ -3148,7 +3147,7 @@ impl<'a> Resolver<'a> {
if let Some(module) = name_binding.module() { if let Some(module) = name_binding.module() {
// form the path // form the path
let mut path_segments = path_segments.clone(); let mut path_segments = path_segments.clone();
path_segments.push(ident.into()); path_segments.push(ast::PathSegment::from_ident(ident, name_binding.span));
if !in_module_is_extern || name_binding.vis == ty::Visibility::Public { if !in_module_is_extern || name_binding.vis == ty::Visibility::Public {
// add the module to the lookup // add the module to the lookup

View file

@ -110,8 +110,11 @@ impl<'a> base::Resolver for Resolver<'a> {
path.segments[0].identifier.name = keywords::CrateRoot.name(); path.segments[0].identifier.name = keywords::CrateRoot.name();
let module = self.0.resolve_crate_var(ident.ctxt); let module = self.0.resolve_crate_var(ident.ctxt);
if !module.is_local() { if !module.is_local() {
let span = path.segments[0].span;
path.segments.insert(1, match module.kind { path.segments.insert(1, match module.kind {
ModuleKind::Def(_, name) => ast::Ident::with_empty_ctxt(name).into(), ModuleKind::Def(_, name) => ast::PathSegment::from_ident(
ast::Ident::with_empty_ctxt(name), span
),
_ => unreachable!(), _ => unreachable!(),
}) })
} }

View file

@ -134,7 +134,7 @@ impl Path {
pub fn from_ident(s: Span, identifier: Ident) -> Path { pub fn from_ident(s: Span, identifier: Ident) -> Path {
Path { Path {
span: s, span: s,
segments: vec![identifier.into()], segments: vec![PathSegment::from_ident(identifier, s)],
} }
} }
@ -159,6 +159,8 @@ impl Path {
pub struct PathSegment { pub struct PathSegment {
/// The identifier portion of this path segment. /// The identifier portion of this path segment.
pub identifier: Ident, pub identifier: Ident,
/// Span of the segment identifier.
pub span: Span,
/// Type/lifetime parameters attached to this path. They come in /// Type/lifetime parameters attached to this path. They come in
/// two flavors: `Path<A,B,C>` and `Path(A,B) -> C`. Note that /// two flavors: `Path<A,B,C>` and `Path(A,B) -> C`. Note that
@ -170,16 +172,14 @@ pub struct PathSegment {
pub parameters: Option<P<PathParameters>>, pub parameters: Option<P<PathParameters>>,
} }
impl From<Ident> for PathSegment {
fn from(id: Ident) -> Self {
PathSegment { identifier: id, parameters: None }
}
}
impl PathSegment { impl PathSegment {
pub fn from_ident(ident: Ident, span: Span) -> Self {
PathSegment { identifier: ident, span: span, parameters: None }
}
pub fn crate_root() -> Self { pub fn crate_root() -> Self {
PathSegment { PathSegment {
identifier: keywords::CrateRoot.ident(), identifier: keywords::CrateRoot.ident(),
span: DUMMY_SP,
parameters: None, parameters: None,
} }
} }

View file

@ -38,11 +38,11 @@ pub trait AstBuilder {
fn qpath(&self, self_type: P<ast::Ty>, fn qpath(&self, self_type: P<ast::Ty>,
trait_path: ast::Path, trait_path: ast::Path,
ident: ast::Ident) ident: ast::SpannedIdent)
-> (ast::QSelf, ast::Path); -> (ast::QSelf, ast::Path);
fn qpath_all(&self, self_type: P<ast::Ty>, fn qpath_all(&self, self_type: P<ast::Ty>,
trait_path: ast::Path, trait_path: ast::Path,
ident: ast::Ident, ident: ast::SpannedIdent,
lifetimes: Vec<ast::Lifetime>, lifetimes: Vec<ast::Lifetime>,
types: Vec<P<ast::Ty>>, types: Vec<P<ast::Ty>>,
bindings: Vec<ast::TypeBinding>) bindings: Vec<ast::TypeBinding>)
@ -323,7 +323,7 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
segments.push(ast::PathSegment::crate_root()); segments.push(ast::PathSegment::crate_root());
} }
segments.extend(idents.into_iter().map(Into::into)); segments.extend(idents.into_iter().map(|i| ast::PathSegment::from_ident(i, sp)));
let parameters = if lifetimes.is_empty() && types.is_empty() && bindings.is_empty() { let parameters = if lifetimes.is_empty() && types.is_empty() && bindings.is_empty() {
None None
} else { } else {
@ -333,7 +333,11 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
bindings: bindings, bindings: bindings,
}))) })))
}; };
segments.push(ast::PathSegment { identifier: last_identifier, parameters: parameters }); segments.push(ast::PathSegment {
identifier: last_identifier,
span: sp,
parameters: parameters
});
ast::Path { ast::Path {
span: sp, span: sp,
segments: segments, segments: segments,
@ -346,7 +350,7 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
fn qpath(&self, fn qpath(&self,
self_type: P<ast::Ty>, self_type: P<ast::Ty>,
trait_path: ast::Path, trait_path: ast::Path,
ident: ast::Ident) ident: ast::SpannedIdent)
-> (ast::QSelf, ast::Path) { -> (ast::QSelf, ast::Path) {
self.qpath_all(self_type, trait_path, ident, vec![], vec![], vec![]) self.qpath_all(self_type, trait_path, ident, vec![], vec![], vec![])
} }
@ -357,7 +361,7 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
fn qpath_all(&self, fn qpath_all(&self,
self_type: P<ast::Ty>, self_type: P<ast::Ty>,
trait_path: ast::Path, trait_path: ast::Path,
ident: ast::Ident, ident: ast::SpannedIdent,
lifetimes: Vec<ast::Lifetime>, lifetimes: Vec<ast::Lifetime>,
types: Vec<P<ast::Ty>>, types: Vec<P<ast::Ty>>,
bindings: Vec<ast::TypeBinding>) bindings: Vec<ast::TypeBinding>)
@ -369,7 +373,8 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
bindings: bindings, bindings: bindings,
}; };
path.segments.push(ast::PathSegment { path.segments.push(ast::PathSegment {
identifier: ident, identifier: ident.node,
span: ident.span,
parameters: Some(P(ast::PathParameters::AngleBracketed(parameters))), parameters: Some(P(ast::PathParameters::AngleBracketed(parameters))),
}); });

View file

@ -434,8 +434,9 @@ pub fn noop_fold_usize<T: Folder>(i: usize, _: &mut T) -> usize {
pub fn noop_fold_path<T: Folder>(Path { segments, span }: Path, fld: &mut T) -> Path { pub fn noop_fold_path<T: Folder>(Path { segments, span }: Path, fld: &mut T) -> Path {
Path { Path {
segments: segments.move_map(|PathSegment {identifier, parameters}| PathSegment { segments: segments.move_map(|PathSegment {identifier, span, parameters}| PathSegment {
identifier: fld.fold_ident(identifier), identifier: fld.fold_ident(identifier),
span: fld.new_span(span),
parameters: parameters.map(|ps| ps.map(|ps| fld.fold_path_parameters(ps))), parameters: parameters.map(|ps| ps.map(|ps| fld.fold_path_parameters(ps))),
}), }),
span: fld.new_span(span) span: fld.new_span(span)

View file

@ -617,13 +617,17 @@ mod tests {
Span {lo: BytePos(a), hi: BytePos(b), expn_id: NO_EXPANSION} Span {lo: BytePos(a), hi: BytePos(b), expn_id: NO_EXPANSION}
} }
fn str2seg(s: &str, lo: u32, hi: u32) -> ast::PathSegment {
ast::PathSegment::from_ident(Ident::from_str(s), sp(lo, hi))
}
#[test] fn path_exprs_1() { #[test] fn path_exprs_1() {
assert!(string_to_expr("a".to_string()) == assert!(string_to_expr("a".to_string()) ==
P(ast::Expr{ P(ast::Expr{
id: ast::DUMMY_NODE_ID, id: ast::DUMMY_NODE_ID,
node: ast::ExprKind::Path(None, ast::Path { node: ast::ExprKind::Path(None, ast::Path {
span: sp(0, 1), span: sp(0, 1),
segments: vec![Ident::from_str("a").into()], segments: vec![str2seg("a", 0, 1)],
}), }),
span: sp(0, 1), span: sp(0, 1),
attrs: ThinVec::new(), attrs: ThinVec::new(),
@ -637,8 +641,8 @@ mod tests {
node: ast::ExprKind::Path(None, ast::Path { node: ast::ExprKind::Path(None, ast::Path {
span: sp(0, 6), span: sp(0, 6),
segments: vec![ast::PathSegment::crate_root(), segments: vec![ast::PathSegment::crate_root(),
Ident::from_str("a").into(), str2seg("a", 2, 3),
Ident::from_str("b").into()] str2seg("b", 5, 6)]
}), }),
span: sp(0, 6), span: sp(0, 6),
attrs: ThinVec::new(), attrs: ThinVec::new(),
@ -744,7 +748,7 @@ mod tests {
id: ast::DUMMY_NODE_ID, id: ast::DUMMY_NODE_ID,
node:ast::ExprKind::Path(None, ast::Path{ node:ast::ExprKind::Path(None, ast::Path{
span: sp(7, 8), span: sp(7, 8),
segments: vec![Ident::from_str("d").into()], segments: vec![str2seg("d", 7, 8)],
}), }),
span:sp(7,8), span:sp(7,8),
attrs: ThinVec::new(), attrs: ThinVec::new(),
@ -761,7 +765,7 @@ mod tests {
id: ast::DUMMY_NODE_ID, id: ast::DUMMY_NODE_ID,
node: ast::ExprKind::Path(None, ast::Path { node: ast::ExprKind::Path(None, ast::Path {
span:sp(0,1), span:sp(0,1),
segments: vec![Ident::from_str("b").into()], segments: vec![str2seg("b", 0, 1)],
}), }),
span: sp(0,1), span: sp(0,1),
attrs: ThinVec::new()})), attrs: ThinVec::new()})),
@ -802,7 +806,7 @@ mod tests {
ty: P(ast::Ty{id: ast::DUMMY_NODE_ID, ty: P(ast::Ty{id: ast::DUMMY_NODE_ID,
node: ast::TyKind::Path(None, ast::Path{ node: ast::TyKind::Path(None, ast::Path{
span:sp(10,13), span:sp(10,13),
segments: vec![Ident::from_str("i32").into()], segments: vec![str2seg("i32", 10, 13)],
}), }),
span:sp(10,13) span:sp(10,13)
}), }),
@ -844,7 +848,7 @@ mod tests {
node: ast::ExprKind::Path(None, node: ast::ExprKind::Path(None,
ast::Path{ ast::Path{
span:sp(17,18), span:sp(17,18),
segments: vec![Ident::from_str("b").into()], segments: vec![str2seg("b", 17, 18)],
}), }),
span: sp(17,18), span: sp(17,18),
attrs: ThinVec::new()})), attrs: ThinVec::new()})),

View file

@ -27,7 +27,7 @@ use ast::Local;
use ast::MacStmtStyle; use ast::MacStmtStyle;
use ast::Mac_; use ast::Mac_;
use ast::{MutTy, Mutability}; use ast::{MutTy, Mutability};
use ast::{Pat, PatKind}; use ast::{Pat, PatKind, PathSegment};
use ast::{PolyTraitRef, QSelf}; use ast::{PolyTraitRef, QSelf};
use ast::{Stmt, StmtKind}; use ast::{Stmt, StmtKind};
use ast::{VariantData, StructField}; use ast::{VariantData, StructField};
@ -1811,7 +1811,7 @@ impl<'a> Parser<'a> {
}; };
if is_global { if is_global {
segments.insert(0, ast::PathSegment::crate_root()); segments.insert(0, PathSegment::crate_root());
} }
// Assemble the span. // Assemble the span.
@ -1829,11 +1829,12 @@ impl<'a> Parser<'a> {
/// - `a::b<T,U>::c<V,W>` /// - `a::b<T,U>::c<V,W>`
/// - `a::b<T,U>::c(V) -> W` /// - `a::b<T,U>::c(V) -> W`
/// - `a::b<T,U>::c(V)` /// - `a::b<T,U>::c(V)`
pub fn parse_path_segments_without_colons(&mut self) -> PResult<'a, Vec<ast::PathSegment>> { pub fn parse_path_segments_without_colons(&mut self) -> PResult<'a, Vec<PathSegment>> {
let mut segments = Vec::new(); let mut segments = Vec::new();
loop { loop {
// First, parse an identifier. // First, parse an identifier.
let identifier = self.parse_path_segment_ident()?; let identifier = self.parse_path_segment_ident()?;
let ident_span = self.prev_span;
if self.check(&token::ModSep) && self.look_ahead(1, |t| *t == token::Lt) { if self.check(&token::ModSep) && self.look_ahead(1, |t| *t == token::Lt) {
self.bump(); self.bump();
@ -1881,7 +1882,11 @@ impl<'a> Parser<'a> {
}; };
// Assemble and push the result. // Assemble and push the result.
segments.push(ast::PathSegment { identifier: identifier, parameters: parameters }); segments.push(PathSegment {
identifier: identifier,
span: ident_span,
parameters: parameters
});
// Continue only if we see a `::` // Continue only if we see a `::`
if !self.eat(&token::ModSep) { if !self.eat(&token::ModSep) {
@ -1892,15 +1897,16 @@ impl<'a> Parser<'a> {
/// Examples: /// Examples:
/// - `a::b::<T,U>::c` /// - `a::b::<T,U>::c`
pub fn parse_path_segments_with_colons(&mut self) -> PResult<'a, Vec<ast::PathSegment>> { pub fn parse_path_segments_with_colons(&mut self) -> PResult<'a, Vec<PathSegment>> {
let mut segments = Vec::new(); let mut segments = Vec::new();
loop { loop {
// First, parse an identifier. // First, parse an identifier.
let identifier = self.parse_path_segment_ident()?; let identifier = self.parse_path_segment_ident()?;
let ident_span = self.prev_span;
// If we do not see a `::`, stop. // If we do not see a `::`, stop.
if !self.eat(&token::ModSep) { if !self.eat(&token::ModSep) {
segments.push(identifier.into()); segments.push(PathSegment::from_ident(identifier, ident_span));
return Ok(segments); return Ok(segments);
} }
@ -1909,8 +1915,9 @@ impl<'a> Parser<'a> {
// Consumed `a::b::<`, go look for types // Consumed `a::b::<`, go look for types
let (lifetimes, types, bindings) = self.parse_generic_args()?; let (lifetimes, types, bindings) = self.parse_generic_args()?;
self.expect_gt()?; self.expect_gt()?;
segments.push(ast::PathSegment { segments.push(PathSegment {
identifier: identifier, identifier: identifier,
span: ident_span,
parameters: ast::AngleBracketedParameterData { parameters: ast::AngleBracketedParameterData {
lifetimes: lifetimes, lifetimes: lifetimes,
types: types, types: types,
@ -1924,7 +1931,7 @@ impl<'a> Parser<'a> {
} }
} else { } else {
// Consumed `a::`, go look for `b` // Consumed `a::`, go look for `b`
segments.push(identifier.into()); segments.push(PathSegment::from_ident(identifier, ident_span));
} }
} }
} }
@ -1932,14 +1939,14 @@ impl<'a> Parser<'a> {
/// Examples: /// Examples:
/// - `a::b::c` /// - `a::b::c`
pub fn parse_path_segments_without_types(&mut self) pub fn parse_path_segments_without_types(&mut self)
-> PResult<'a, Vec<ast::PathSegment>> { -> PResult<'a, Vec<PathSegment>> {
let mut segments = Vec::new(); let mut segments = Vec::new();
loop { loop {
// First, parse an identifier. // First, parse an identifier.
let identifier = self.parse_path_segment_ident()?; let identifier = self.parse_path_segment_ident()?;
// Assemble and push the result. // Assemble and push the result.
segments.push(identifier.into()); segments.push(PathSegment::from_ident(identifier, self.prev_span));
// If we do not see a `::` or see `::{`/`::*`, stop. // If we do not see a `::` or see `::{`/`::*`, stop.
if !self.check(&token::ModSep) || self.is_import_coupler() { if !self.check(&token::ModSep) || self.is_import_coupler() {
@ -5902,7 +5909,7 @@ impl<'a> Parser<'a> {
// `{foo, bar}`, `::{foo, bar}`, `*`, or `::*`. // `{foo, bar}`, `::{foo, bar}`, `*`, or `::*`.
self.eat(&token::ModSep); self.eat(&token::ModSep);
let prefix = ast::Path { let prefix = ast::Path {
segments: vec![ast::PathSegment::crate_root()], segments: vec![PathSegment::crate_root()],
span: mk_sp(lo, self.span.hi), span: mk_sp(lo, self.span.hi),
}; };
let view_path_kind = if self.eat(&token::BinOp(token::Star)) { let view_path_kind = if self.eat(&token::BinOp(token::Star)) {

View file

@ -82,7 +82,7 @@ pub fn maybe_inject_crates_ref(sess: &ParseSess,
vis: ast::Visibility::Inherited, vis: ast::Visibility::Inherited,
node: ast::ItemKind::Use(P(codemap::dummy_spanned(ast::ViewPathGlob(ast::Path { node: ast::ItemKind::Use(P(codemap::dummy_spanned(ast::ViewPathGlob(ast::Path {
segments: ["{{root}}", name, "prelude", "v1"].into_iter().map(|name| { segments: ["{{root}}", name, "prelude", "v1"].into_iter().map(|name| {
ast::Ident::from_str(name).into() ast::PathSegment::from_ident(ast::Ident::from_str(name), DUMMY_SP)
}).collect(), }).collect(),
span: span, span: span,
})))), })))),

View file

@ -580,7 +580,7 @@ fn nospan<T>(t: T) -> codemap::Spanned<T> {
fn path_node(ids: Vec<Ident>) -> ast::Path { fn path_node(ids: Vec<Ident>) -> ast::Path {
ast::Path { ast::Path {
span: DUMMY_SP, span: DUMMY_SP,
segments: ids.into_iter().map(Into::into).collect(), segments: ids.into_iter().map(|id| ast::PathSegment::from_ident(id, DUMMY_SP)).collect(),
} }
} }

View file

@ -61,7 +61,7 @@ pub fn expand_syntax_ext<'cx>(cx: &'cx mut ExtCtxt,
fn path(&self) -> ast::Path { fn path(&self) -> ast::Path {
ast::Path { ast::Path {
span: self.span, span: self.span,
segments: vec![self.ident.into()], segments: vec![ast::PathSegment::from_ident(self.ident, self.span)],
} }
} }
} }