Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 1823dea

Browse files
committed
Attach TokenStream to ast::Ty
A `Ty` does not have outer attributes, so we only capture tokens when parsing a `macro_rules!` matcher
1 parent de4bd9f commit 1823dea

File tree

12 files changed

+42
-15
lines changed

12 files changed

+42
-15
lines changed

compiler/rustc_ast/src/ast.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -587,7 +587,7 @@ impl Pat {
587587
_ => return None,
588588
};
589589

590-
Some(P(Ty { kind, id: self.id, span: self.span }))
590+
Some(P(Ty { kind, id: self.id, span: self.span, tokens: None }))
591591
}
592592

593593
/// Walk top-down and call `it` in each place where a pattern occurs
@@ -1169,7 +1169,7 @@ impl Expr {
11691169
_ => return None,
11701170
};
11711171

1172-
Some(P(Ty { kind, id: self.id, span: self.span }))
1172+
Some(P(Ty { kind, id: self.id, span: self.span, tokens: None }))
11731173
}
11741174

11751175
pub fn precedence(&self) -> ExprPrecedence {
@@ -1867,6 +1867,7 @@ pub struct Ty {
18671867
pub id: NodeId,
18681868
pub kind: TyKind,
18691869
pub span: Span,
1870+
pub tokens: Option<TokenStream>,
18701871
}
18711872

18721873
#[derive(Clone, Encodable, Decodable, Debug)]
@@ -2145,7 +2146,7 @@ impl Param {
21452146
/// Builds a `Param` object from `ExplicitSelf`.
21462147
pub fn from_self(attrs: AttrVec, eself: ExplicitSelf, eself_ident: Ident) -> Param {
21472148
let span = eself.span.to(eself_ident.span);
2148-
let infer_ty = P(Ty { id: DUMMY_NODE_ID, kind: TyKind::ImplicitSelf, span });
2149+
let infer_ty = P(Ty { id: DUMMY_NODE_ID, kind: TyKind::ImplicitSelf, span, tokens: None });
21492150
let param = |mutbl, ty| Param {
21502151
attrs,
21512152
pat: P(Pat {
@@ -2168,6 +2169,7 @@ impl Param {
21682169
id: DUMMY_NODE_ID,
21692170
kind: TyKind::Rptr(lt, MutTy { ty: infer_ty, mutbl }),
21702171
span,
2172+
tokens: None,
21712173
}),
21722174
),
21732175
}

compiler/rustc_ast/src/mut_visit.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ pub fn noop_visit_ty_constraint<T: MutVisitor>(
451451
}
452452

453453
pub fn noop_visit_ty<T: MutVisitor>(ty: &mut P<Ty>, vis: &mut T) {
454-
let Ty { id, kind, span } = ty.deref_mut();
454+
let Ty { id, kind, span, tokens: _ } = ty.deref_mut();
455455
vis.visit_id(id);
456456
match kind {
457457
TyKind::Infer | TyKind::ImplicitSelf | TyKind::Err | TyKind::Never | TyKind::CVarArgs => {}

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1106,6 +1106,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
11061106
id: node_id,
11071107
kind: TyKind::ImplTrait(impl_trait_node_id, bounds.clone()),
11081108
span: constraint.span,
1109+
tokens: None,
11091110
},
11101111
itctx,
11111112
);

compiler/rustc_builtin_macros/src/concat_idents.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ pub fn expand_concat_idents<'cx>(
6161
id: ast::DUMMY_NODE_ID,
6262
kind: ast::TyKind::Path(None, ast::Path::from_ident(self.ident)),
6363
span: self.ident.span,
64+
tokens: None,
6465
}))
6566
}
6667
}

compiler/rustc_expand/src/base.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -607,6 +607,7 @@ impl DummyResult {
607607
id: ast::DUMMY_NODE_ID,
608608
kind: if is_error { ast::TyKind::Err } else { ast::TyKind::Tup(Vec::new()) },
609609
span: sp,
610+
tokens: None,
610611
})
611612
}
612613
}

compiler/rustc_expand/src/build.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ impl<'a> ExtCtxt<'a> {
5454
}
5555

5656
pub fn ty(&self, span: Span, kind: ast::TyKind) -> P<ast::Ty> {
57-
P(ast::Ty { id: ast::DUMMY_NODE_ID, span, kind })
57+
P(ast::Ty { id: ast::DUMMY_NODE_ID, span, kind, tokens: None })
5858
}
5959

6060
pub fn ty_path(&self, path: ast::Path) -> P<ast::Ty> {

compiler/rustc_expand/src/placeholders.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ pub fn placeholder(
3737
tokens: None,
3838
})
3939
};
40-
let ty = || P(ast::Ty { id, kind: ast::TyKind::MacCall(mac_placeholder()), span });
40+
let ty =
41+
|| P(ast::Ty { id, kind: ast::TyKind::MacCall(mac_placeholder()), span, tokens: None });
4142
let pat =
4243
|| P(ast::Pat { id, kind: ast::PatKind::MacCall(mac_placeholder()), span, tokens: None });
4344

@@ -88,9 +89,12 @@ pub fn placeholder(
8889
kind: ast::PatKind::MacCall(mac_placeholder()),
8990
tokens: None,
9091
})),
91-
AstFragmentKind::Ty => {
92-
AstFragment::Ty(P(ast::Ty { id, span, kind: ast::TyKind::MacCall(mac_placeholder()) }))
93-
}
92+
AstFragmentKind::Ty => AstFragment::Ty(P(ast::Ty {
93+
id,
94+
span,
95+
kind: ast::TyKind::MacCall(mac_placeholder()),
96+
tokens: None,
97+
})),
9498
AstFragmentKind::Stmts => AstFragment::Stmts(smallvec![{
9599
let mac = P(ast::MacCallStmt {
96100
mac: mac_placeholder(),

compiler/rustc_parse/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ pub fn nt_to_tokenstream(nt: &Nonterminal, sess: &ParseSess, span: Span) -> Toke
270270
}
271271
Nonterminal::NtBlock(ref block) => block.tokens.clone(),
272272
Nonterminal::NtPat(ref pat) => pat.tokens.clone(),
273+
Nonterminal::NtTy(ref ty) => ty.tokens.clone(),
273274
Nonterminal::NtIdent(ident, is_raw) => {
274275
Some(tokenstream::TokenTree::token(token::Ident(ident.name, is_raw), ident.span).into())
275276
}

compiler/rustc_parse/src/parser/diagnostics.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ pub(super) fn dummy_arg(ident: Ident) -> Param {
2828
span: ident.span,
2929
tokens: None,
3030
});
31-
let ty = Ty { kind: TyKind::Err, span: ident.span, id: ast::DUMMY_NODE_ID };
31+
let ty = Ty { kind: TyKind::Err, span: ident.span, id: ast::DUMMY_NODE_ID, tokens: None };
3232
Param {
3333
attrs: AttrVec::default(),
3434
id: ast::DUMMY_NODE_ID,
@@ -75,7 +75,12 @@ impl RecoverQPath for Ty {
7575
Some(P(self.clone()))
7676
}
7777
fn recovered(qself: Option<QSelf>, path: ast::Path) -> Self {
78-
Self { span: path.span, kind: TyKind::Path(qself, path), id: ast::DUMMY_NODE_ID }
78+
Self {
79+
span: path.span,
80+
kind: TyKind::Path(qself, path),
81+
id: ast::DUMMY_NODE_ID,
82+
tokens: None,
83+
}
7984
}
8085
}
8186

compiler/rustc_parse/src/parser/item.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,12 @@ impl<'a> Parser<'a> {
510510
{
511511
let span = self.prev_token.span.between(self.token.span);
512512
self.struct_span_err(span, "missing trait in a trait impl").emit();
513-
P(Ty { kind: TyKind::Path(None, err_path(span)), span, id: DUMMY_NODE_ID })
513+
P(Ty {
514+
kind: TyKind::Path(None, err_path(span)),
515+
span,
516+
id: DUMMY_NODE_ID,
517+
tokens: None,
518+
})
514519
} else {
515520
self.parse_ty()?
516521
};
@@ -1046,7 +1051,7 @@ impl<'a> Parser<'a> {
10461051

10471052
// The user intended that the type be inferred,
10481053
// so treat this as if the user wrote e.g. `const A: _ = expr;`.
1049-
P(Ty { kind: TyKind::Infer, span: id.span, id: ast::DUMMY_NODE_ID })
1054+
P(Ty { kind: TyKind::Infer, span: id.span, id: ast::DUMMY_NODE_ID, tokens: None })
10501055
}
10511056

10521057
/// Parses an enum declaration.

compiler/rustc_parse/src/parser/nonterminal.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,14 @@ impl<'a> Parser<'a> {
141141
token::NtExpr(expr)
142142
}
143143
NonterminalKind::Literal => token::NtLiteral(self.parse_literal_maybe_minus()?),
144-
NonterminalKind::Ty => token::NtTy(self.parse_ty()?),
144+
NonterminalKind::Ty => {
145+
let (mut ty, tokens) = self.collect_tokens(|this| this.parse_ty())?;
146+
// We have an eaten an NtTy, which could already have tokens
147+
if ty.tokens.is_none() {
148+
ty.tokens = Some(tokens);
149+
}
150+
token::NtTy(ty)
151+
}
145152
// this could be handled like a token, since it is one
146153
NonterminalKind::Ident => {
147154
if let Some((ident, is_raw)) = get_macro_ident(&self.token) {

compiler/rustc_parse/src/parser/ty.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,6 @@ impl<'a> Parser<'a> {
626626
}
627627

628628
pub(super) fn mk_ty(&self, span: Span, kind: TyKind) -> P<Ty> {
629-
P(Ty { kind, span, id: ast::DUMMY_NODE_ID })
629+
P(Ty { kind, span, id: ast::DUMMY_NODE_ID, tokens: None })
630630
}
631631
}

0 commit comments

Comments
 (0)