未验证 提交 55082ce4 编写于 作者: A Aaron Hill

Attach `TokenStream` to `ast::Path`

上级 3815e91c
......@@ -96,6 +96,7 @@ pub struct Path {
/// The segments in the path: the things separated by `::`.
/// Global paths begin with `kw::PathRoot`.
pub segments: Vec<PathSegment>,
pub tokens: Option<TokenStream>,
}
impl PartialEq<Symbol> for Path {
......@@ -117,7 +118,7 @@ impl Path {
// Convert a span and an identifier to the corresponding
// one-segment path.
pub fn from_ident(ident: Ident) -> Path {
Path { segments: vec![PathSegment::from_ident(ident)], span: ident.span }
Path { segments: vec![PathSegment::from_ident(ident)], span: ident.span, tokens: None }
}
pub fn is_global(&self) -> bool {
......@@ -1069,7 +1070,7 @@ pub struct Expr {
// `Expr` is used a lot. Make sure it doesn't unintentionally get bigger.
#[cfg(target_arch = "x86_64")]
rustc_data_structures::static_assert_size!(Expr, 104);
rustc_data_structures::static_assert_size!(Expr, 112);
impl Expr {
/// Returns `true` if this expression would be valid somewhere that expects a value;
......
......@@ -415,7 +415,7 @@ fn from_tokens<I>(tokens: &mut iter::Peekable<I>) -> Option<MetaItem>
}
}
let span = span.with_hi(segments.last().unwrap().ident.span.hi());
Path { span, segments }
Path { span, segments, tokens: None }
}
Some(TokenTree::Token(Token { kind: token::Interpolated(nt), .. })) => match *nt {
token::Nonterminal::NtMeta(ref item) => return item.meta(item.path.span),
......
......@@ -513,7 +513,7 @@ pub fn noop_visit_ident<T: MutVisitor>(Ident { name: _, span }: &mut Ident, vis:
vis.visit_span(span);
}
pub fn noop_visit_path<T: MutVisitor>(Path { segments, span }: &mut Path, vis: &mut T) {
pub fn noop_visit_path<T: MutVisitor>(Path { segments, span, tokens: _ }: &mut Path, vis: &mut T) {
vis.visit_span(span);
for PathSegment { ident, id, args } in segments {
vis.visit_ident(ident);
......
......@@ -251,7 +251,7 @@ fn lower_item_kind(
ItemKind::ExternCrate(orig_name) => hir::ItemKind::ExternCrate(orig_name),
ItemKind::Use(ref use_tree) => {
// Start with an empty prefix.
let prefix = Path { segments: vec![], span: use_tree.span };
let prefix = Path { segments: vec![], span: use_tree.span, tokens: None };
self.lower_use_tree(use_tree, &prefix, id, vis, ident, attrs)
}
......@@ -488,7 +488,7 @@ fn lower_use_tree(
*ident = tree.ident();
// First, apply the prefix to the path.
let mut path = Path { segments, span: path.span };
let mut path = Path { segments, span: path.span, tokens: None };
// Correctly resolve `self` imports.
if path.segments.len() > 1
......@@ -540,8 +540,11 @@ fn lower_use_tree(
hir::ItemKind::Use(path, hir::UseKind::Single)
}
UseTreeKind::Glob => {
let path =
self.lower_path(id, &Path { segments, span: path.span }, ParamMode::Explicit);
let path = self.lower_path(
id,
&Path { segments, span: path.span, tokens: None },
ParamMode::Explicit,
);
hir::ItemKind::Use(path, hir::UseKind::Glob)
}
UseTreeKind::Nested(ref trees) => {
......@@ -569,7 +572,7 @@ fn lower_use_tree(
// for that we return the `{}` import (called the
// `ListStem`).
let prefix = Path { segments, span: prefix.span.to(path.span) };
let prefix = Path { segments, span: prefix.span.to(path.span), tokens: None };
// Add all the nested `PathListItem`s to the HIR.
for &(ref use_tree, id) in trees {
......
......@@ -46,7 +46,7 @@ pub fn path_all(
id: ast::DUMMY_NODE_ID,
args,
});
ast::Path { span, segments }
ast::Path { span, segments, tokens: None }
}
pub fn ty_mt(&self, ty: P<ast::Ty>, mutbl: ast::Mutability) -> ast::MutTy {
......
......@@ -18,7 +18,7 @@ pub fn placeholder(
) -> AstFragment {
fn mac_placeholder() -> ast::MacCall {
ast::MacCall {
path: ast::Path { span: DUMMY_SP, segments: Vec::new() },
path: ast::Path { span: DUMMY_SP, segments: Vec::new(), tokens: None },
args: P(ast::MacArgs::Empty),
prior_type_ascription: None,
}
......
......@@ -278,6 +278,7 @@ pub fn nt_to_tokenstream(nt: &Nonterminal, sess: &ParseSess, span: Span) -> Toke
Some(tokenstream::TokenTree::token(token::Lifetime(ident.name), ident.span).into())
}
Nonterminal::NtMeta(ref attr) => attr.tokens.clone(),
Nonterminal::NtPath(ref path) => path.tokens.clone(),
Nonterminal::NtTT(ref tt) => Some(tt.clone().into()),
Nonterminal::NtExpr(ref expr) | Nonterminal::NtLiteral(ref expr) => {
if expr.tokens.is_none() {
......
......@@ -901,7 +901,7 @@ pub(super) fn maybe_recover_from_bad_qpath_stage_2<T: RecoverQPath>(
) -> PResult<'a, P<T>> {
self.expect(&token::ModSep)?;
let mut path = ast::Path { segments: Vec::new(), span: DUMMY_SP };
let mut path = ast::Path { segments: Vec::new(), span: DUMMY_SP, tokens: None };
self.parse_path_segments(&mut path.segments, T::PATH_STYLE)?;
path.span = ty_span.to(self.prev_token.span);
......
......@@ -787,7 +787,7 @@ fn parse_type_alias(&mut self, def: Defaultness) -> PResult<'a, ItemInfo> {
fn parse_use_tree(&mut self) -> PResult<'a, UseTree> {
let lo = self.token.span;
let mut prefix = ast::Path { segments: Vec::new(), span: lo.shrink_to_lo() };
let mut prefix = ast::Path { segments: Vec::new(), span: lo.shrink_to_lo(), tokens: None };
let kind = if self.check(&token::OpenDelim(token::Brace))
|| self.check(&token::BinOp(token::Star))
|| self.is_import_coupler()
......
......@@ -168,7 +168,15 @@ pub fn parse_nonterminal(&mut self, kind: NonterminalKind) -> PResult<'a, Nonter
return Err(self.struct_span_err(self.token.span, msg));
}
}
NonterminalKind::Path => token::NtPath(self.parse_path(PathStyle::Type)?),
NonterminalKind::Path => {
let (mut path, tokens) =
self.collect_tokens(|this| this.parse_path(PathStyle::Type))?;
// We have have eaten an NtPath, which could already have tokens
if path.tokens.is_none() {
path.tokens = Some(tokens);
}
token::NtPath(path)
}
NonterminalKind::Meta => {
let (mut attr, tokens) = self.collect_tokens(|this| this.parse_attr_item())?;
// We may have eaten a nonterminal, which could already have tokens
......
......@@ -64,7 +64,7 @@ pub(super) fn parse_qpath(&mut self, style: PathStyle) -> PResult<'a, (QSelf, Pa
path_span = path_lo.to(self.prev_token.span);
} else {
path_span = self.token.span.to(self.token.span);
path = ast::Path { segments: Vec::new(), span: path_span };
path = ast::Path { segments: Vec::new(), span: path_span, tokens: None };
}
// See doc comment for `unmatched_angle_bracket_count`.
......@@ -81,7 +81,10 @@ pub(super) fn parse_qpath(&mut self, style: PathStyle) -> PResult<'a, (QSelf, Pa
let qself = QSelf { ty, path_span, position: path.segments.len() };
self.parse_path_segments(&mut path.segments, style)?;
Ok((qself, Path { segments: path.segments, span: lo.to(self.prev_token.span) }))
Ok((
qself,
Path { segments: path.segments, span: lo.to(self.prev_token.span), tokens: None },
))
}
/// Recover from an invalid single colon, when the user likely meant a qualified path.
......@@ -144,7 +147,7 @@ pub(super) fn parse_path(&mut self, style: PathStyle) -> PResult<'a, Path> {
}
self.parse_path_segments(&mut segments, style)?;
Ok(Path { segments, span: lo.to(self.prev_token.span) })
Ok(Path { segments, span: lo.to(self.prev_token.span), tokens: None })
}
pub(super) fn parse_path_segments(
......
......@@ -794,7 +794,7 @@ fn lookup_import_candidates_from_module<FilterFn>(
}
segms.push(ast::PathSegment::from_ident(ident));
let path = Path { span: name_binding.span, segments: segms };
let path = Path { span: name_binding.span, segments: segms, tokens: None };
let did = match res {
Res::Def(DefKind::Ctor(..), did) => this.parent(did),
_ => res.opt_def_id(),
......
......@@ -1967,7 +1967,7 @@ fn resolve_qpath_anywhere(
if qself.is_none() {
let path_seg = |seg: &Segment| PathSegment::from_ident(seg.ident);
let path = Path { segments: path.iter().map(path_seg).collect(), span };
let path = Path { segments: path.iter().map(path_seg).collect(), span, tokens: None };
if let Ok((_, res)) =
self.r.resolve_macro_path(&path, None, &self.parent_scope, false, false)
{
......
......@@ -83,6 +83,7 @@ fn import_candidate_to_enum_paths(suggestion: &ImportSuggestion) -> (String, Str
let enum_path = ast::Path {
span: suggestion.path.span,
segments: suggestion.path.segments[0..path_len - 1].to_vec(),
tokens: None,
};
let enum_path_string = path_names_to_string(&enum_path);
......@@ -1065,7 +1066,8 @@ fn find_module(&mut self, def_id: DefId) -> Option<(Module<'a>, ImportSuggestion
path_segments.push(ast::PathSegment::from_ident(ident));
let module_def_id = module.def_id().unwrap();
if module_def_id == def_id {
let path = Path { span: name_binding.span, segments: path_segments };
let path =
Path { span: name_binding.span, segments: path_segments, tokens: None };
result = Some((
module,
ImportSuggestion {
......@@ -1095,7 +1097,7 @@ fn collect_enum_variants(&mut self, def_id: DefId) -> Option<Vec<Path>> {
if let Res::Def(DefKind::Variant, _) = name_binding.res() {
let mut segms = enum_import_suggestion.path.segments.clone();
segms.push(ast::PathSegment::from_ident(ident));
variants.push(Path { span: name_binding.span, segments: segms });
variants.push(Path { span: name_binding.span, segments: segms, tokens: None });
}
});
variants
......
......@@ -3189,6 +3189,7 @@ pub fn resolve_str_path_error(
.chain(path_str.split("::").skip(1).map(Ident::from_str))
.map(|i| self.new_ast_path_segment(i))
.collect(),
tokens: None,
}
} else {
ast::Path {
......@@ -3198,6 +3199,7 @@ pub fn resolve_str_path_error(
.map(Ident::from_str)
.map(|i| self.new_ast_path_segment(i))
.collect(),
tokens: None,
}
};
let module = self.get_module(module_id);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册