Skip to content

Commit 55082ce

Browse files
committed
Attach TokenStream to ast::Path
1 parent 3815e91 commit 55082ce

File tree

15 files changed

+41
-21
lines changed

15 files changed

+41
-21
lines changed

compiler/rustc_ast/src/ast.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,7 @@ pub struct Path {
9696
/// The segments in the path: the things separated by `::`.
9797
/// Global paths begin with `kw::PathRoot`.
9898
pub segments: Vec<PathSegment>,
99+
pub tokens: Option<TokenStream>,
99100
}
100101

101102
impl PartialEq<Symbol> for Path {
@@ -117,7 +118,7 @@ impl Path {
117118
// Convert a span and an identifier to the corresponding
118119
// one-segment path.
119120
pub fn from_ident(ident: Ident) -> Path {
120-
Path { segments: vec![PathSegment::from_ident(ident)], span: ident.span }
121+
Path { segments: vec![PathSegment::from_ident(ident)], span: ident.span, tokens: None }
121122
}
122123

123124
pub fn is_global(&self) -> bool {
@@ -1069,7 +1070,7 @@ pub struct Expr {
10691070

10701071
// `Expr` is used a lot. Make sure it doesn't unintentionally get bigger.
10711072
#[cfg(target_arch = "x86_64")]
1072-
rustc_data_structures::static_assert_size!(Expr, 104);
1073+
rustc_data_structures::static_assert_size!(Expr, 112);
10731074

10741075
impl Expr {
10751076
/// Returns `true` if this expression would be valid somewhere that expects a value;

compiler/rustc_ast/src/attr/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ impl MetaItem {
415415
}
416416
}
417417
let span = span.with_hi(segments.last().unwrap().ident.span.hi());
418-
Path { span, segments }
418+
Path { span, segments, tokens: None }
419419
}
420420
Some(TokenTree::Token(Token { kind: token::Interpolated(nt), .. })) => match *nt {
421421
token::Nonterminal::NtMeta(ref item) => return item.meta(item.path.span),

compiler/rustc_ast/src/mut_visit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,7 @@ pub fn noop_visit_ident<T: MutVisitor>(Ident { name: _, span }: &mut Ident, vis:
513513
vis.visit_span(span);
514514
}
515515

516-
pub fn noop_visit_path<T: MutVisitor>(Path { segments, span }: &mut Path, vis: &mut T) {
516+
pub fn noop_visit_path<T: MutVisitor>(Path { segments, span, tokens: _ }: &mut Path, vis: &mut T) {
517517
vis.visit_span(span);
518518
for PathSegment { ident, id, args } in segments {
519519
vis.visit_ident(ident);

compiler/rustc_ast_lowering/src/item.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
251251
ItemKind::ExternCrate(orig_name) => hir::ItemKind::ExternCrate(orig_name),
252252
ItemKind::Use(ref use_tree) => {
253253
// Start with an empty prefix.
254-
let prefix = Path { segments: vec![], span: use_tree.span };
254+
let prefix = Path { segments: vec![], span: use_tree.span, tokens: None };
255255

256256
self.lower_use_tree(use_tree, &prefix, id, vis, ident, attrs)
257257
}
@@ -488,7 +488,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
488488
*ident = tree.ident();
489489

490490
// First, apply the prefix to the path.
491-
let mut path = Path { segments, span: path.span };
491+
let mut path = Path { segments, span: path.span, tokens: None };
492492

493493
// Correctly resolve `self` imports.
494494
if path.segments.len() > 1
@@ -540,8 +540,11 @@ impl<'hir> LoweringContext<'_, 'hir> {
540540
hir::ItemKind::Use(path, hir::UseKind::Single)
541541
}
542542
UseTreeKind::Glob => {
543-
let path =
544-
self.lower_path(id, &Path { segments, span: path.span }, ParamMode::Explicit);
543+
let path = self.lower_path(
544+
id,
545+
&Path { segments, span: path.span, tokens: None },
546+
ParamMode::Explicit,
547+
);
545548
hir::ItemKind::Use(path, hir::UseKind::Glob)
546549
}
547550
UseTreeKind::Nested(ref trees) => {
@@ -569,7 +572,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
569572
// for that we return the `{}` import (called the
570573
// `ListStem`).
571574

572-
let prefix = Path { segments, span: prefix.span.to(path.span) };
575+
let prefix = Path { segments, span: prefix.span.to(path.span), tokens: None };
573576

574577
// Add all the nested `PathListItem`s to the HIR.
575578
for &(ref use_tree, id) in trees {

compiler/rustc_expand/src/build.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ impl<'a> ExtCtxt<'a> {
4646
id: ast::DUMMY_NODE_ID,
4747
args,
4848
});
49-
ast::Path { span, segments }
49+
ast::Path { span, segments, tokens: None }
5050
}
5151

5252
pub fn ty_mt(&self, ty: P<ast::Ty>, mutbl: ast::Mutability) -> ast::MutTy {

compiler/rustc_expand/src/placeholders.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ pub fn placeholder(
1818
) -> AstFragment {
1919
fn mac_placeholder() -> ast::MacCall {
2020
ast::MacCall {
21-
path: ast::Path { span: DUMMY_SP, segments: Vec::new() },
21+
path: ast::Path { span: DUMMY_SP, segments: Vec::new(), tokens: None },
2222
args: P(ast::MacArgs::Empty),
2323
prior_type_ascription: None,
2424
}

compiler/rustc_parse/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,7 @@ pub fn nt_to_tokenstream(nt: &Nonterminal, sess: &ParseSess, span: Span) -> Toke
278278
Some(tokenstream::TokenTree::token(token::Lifetime(ident.name), ident.span).into())
279279
}
280280
Nonterminal::NtMeta(ref attr) => attr.tokens.clone(),
281+
Nonterminal::NtPath(ref path) => path.tokens.clone(),
281282
Nonterminal::NtTT(ref tt) => Some(tt.clone().into()),
282283
Nonterminal::NtExpr(ref expr) | Nonterminal::NtLiteral(ref expr) => {
283284
if expr.tokens.is_none() {

compiler/rustc_parse/src/parser/diagnostics.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -901,7 +901,7 @@ impl<'a> Parser<'a> {
901901
) -> PResult<'a, P<T>> {
902902
self.expect(&token::ModSep)?;
903903

904-
let mut path = ast::Path { segments: Vec::new(), span: DUMMY_SP };
904+
let mut path = ast::Path { segments: Vec::new(), span: DUMMY_SP, tokens: None };
905905
self.parse_path_segments(&mut path.segments, T::PATH_STYLE)?;
906906
path.span = ty_span.to(self.prev_token.span);
907907

compiler/rustc_parse/src/parser/item.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -787,7 +787,7 @@ impl<'a> Parser<'a> {
787787
fn parse_use_tree(&mut self) -> PResult<'a, UseTree> {
788788
let lo = self.token.span;
789789

790-
let mut prefix = ast::Path { segments: Vec::new(), span: lo.shrink_to_lo() };
790+
let mut prefix = ast::Path { segments: Vec::new(), span: lo.shrink_to_lo(), tokens: None };
791791
let kind = if self.check(&token::OpenDelim(token::Brace))
792792
|| self.check(&token::BinOp(token::Star))
793793
|| self.is_import_coupler()

compiler/rustc_parse/src/parser/nonterminal.rs

+9-1
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,15 @@ impl<'a> Parser<'a> {
168168
return Err(self.struct_span_err(self.token.span, msg));
169169
}
170170
}
171-
NonterminalKind::Path => token::NtPath(self.parse_path(PathStyle::Type)?),
171+
NonterminalKind::Path => {
172+
let (mut path, tokens) =
173+
self.collect_tokens(|this| this.parse_path(PathStyle::Type))?;
174+
// We have have eaten an NtPath, which could already have tokens
175+
if path.tokens.is_none() {
176+
path.tokens = Some(tokens);
177+
}
178+
token::NtPath(path)
179+
}
172180
NonterminalKind::Meta => {
173181
let (mut attr, tokens) = self.collect_tokens(|this| this.parse_attr_item())?;
174182
// We may have eaten a nonterminal, which could already have tokens

compiler/rustc_parse/src/parser/path.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ impl<'a> Parser<'a> {
6464
path_span = path_lo.to(self.prev_token.span);
6565
} else {
6666
path_span = self.token.span.to(self.token.span);
67-
path = ast::Path { segments: Vec::new(), span: path_span };
67+
path = ast::Path { segments: Vec::new(), span: path_span, tokens: None };
6868
}
6969

7070
// See doc comment for `unmatched_angle_bracket_count`.
@@ -81,7 +81,10 @@ impl<'a> Parser<'a> {
8181
let qself = QSelf { ty, path_span, position: path.segments.len() };
8282
self.parse_path_segments(&mut path.segments, style)?;
8383

84-
Ok((qself, Path { segments: path.segments, span: lo.to(self.prev_token.span) }))
84+
Ok((
85+
qself,
86+
Path { segments: path.segments, span: lo.to(self.prev_token.span), tokens: None },
87+
))
8588
}
8689

8790
/// Recover from an invalid single colon, when the user likely meant a qualified path.
@@ -144,7 +147,7 @@ impl<'a> Parser<'a> {
144147
}
145148
self.parse_path_segments(&mut segments, style)?;
146149

147-
Ok(Path { segments, span: lo.to(self.prev_token.span) })
150+
Ok(Path { segments, span: lo.to(self.prev_token.span), tokens: None })
148151
}
149152

150153
pub(super) fn parse_path_segments(

compiler/rustc_resolve/src/diagnostics.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -794,7 +794,7 @@ impl<'a> Resolver<'a> {
794794
}
795795

796796
segms.push(ast::PathSegment::from_ident(ident));
797-
let path = Path { span: name_binding.span, segments: segms };
797+
let path = Path { span: name_binding.span, segments: segms, tokens: None };
798798
let did = match res {
799799
Res::Def(DefKind::Ctor(..), did) => this.parent(did),
800800
_ => res.opt_def_id(),

compiler/rustc_resolve/src/late.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1967,7 +1967,7 @@ impl<'a, 'b, 'ast> LateResolutionVisitor<'a, 'b, 'ast> {
19671967

19681968
if qself.is_none() {
19691969
let path_seg = |seg: &Segment| PathSegment::from_ident(seg.ident);
1970-
let path = Path { segments: path.iter().map(path_seg).collect(), span };
1970+
let path = Path { segments: path.iter().map(path_seg).collect(), span, tokens: None };
19711971
if let Ok((_, res)) =
19721972
self.r.resolve_macro_path(&path, None, &self.parent_scope, false, false)
19731973
{

compiler/rustc_resolve/src/late/diagnostics.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ fn import_candidate_to_enum_paths(suggestion: &ImportSuggestion) -> (String, Str
8383
let enum_path = ast::Path {
8484
span: suggestion.path.span,
8585
segments: suggestion.path.segments[0..path_len - 1].to_vec(),
86+
tokens: None,
8687
};
8788
let enum_path_string = path_names_to_string(&enum_path);
8889

@@ -1065,7 +1066,8 @@ impl<'a> LateResolutionVisitor<'a, '_, '_> {
10651066
path_segments.push(ast::PathSegment::from_ident(ident));
10661067
let module_def_id = module.def_id().unwrap();
10671068
if module_def_id == def_id {
1068-
let path = Path { span: name_binding.span, segments: path_segments };
1069+
let path =
1070+
Path { span: name_binding.span, segments: path_segments, tokens: None };
10691071
result = Some((
10701072
module,
10711073
ImportSuggestion {
@@ -1095,7 +1097,7 @@ impl<'a> LateResolutionVisitor<'a, '_, '_> {
10951097
if let Res::Def(DefKind::Variant, _) = name_binding.res() {
10961098
let mut segms = enum_import_suggestion.path.segments.clone();
10971099
segms.push(ast::PathSegment::from_ident(ident));
1098-
variants.push(Path { span: name_binding.span, segments: segms });
1100+
variants.push(Path { span: name_binding.span, segments: segms, tokens: None });
10991101
}
11001102
});
11011103
variants

compiler/rustc_resolve/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -3189,6 +3189,7 @@ impl<'a> Resolver<'a> {
31893189
.chain(path_str.split("::").skip(1).map(Ident::from_str))
31903190
.map(|i| self.new_ast_path_segment(i))
31913191
.collect(),
3192+
tokens: None,
31923193
}
31933194
} else {
31943195
ast::Path {
@@ -3198,6 +3199,7 @@ impl<'a> Resolver<'a> {
31983199
.map(Ident::from_str)
31993200
.map(|i| self.new_ast_path_segment(i))
32003201
.collect(),
3202+
tokens: None,
32013203
}
32023204
};
32033205
let module = self.get_module(module_id);

0 commit comments

Comments
 (0)