Skip to content

Commit

Permalink
Attach TokenStream to ast::Ty
Browse files Browse the repository at this point in the history
A `Ty` does not have outer attributes, so we only capture tokens
when parsing a `macro_rules!` matcher
  • Loading branch information
Aaron1011 committed Sep 10, 2020
1 parent de4bd9f commit 1823dea
Show file tree
Hide file tree
Showing 12 changed files with 42 additions and 15 deletions.
8 changes: 5 additions & 3 deletions compiler/rustc_ast/src/ast.rs
Expand Up @@ -587,7 +587,7 @@ impl Pat {
_ => return None,
};

Some(P(Ty { kind, id: self.id, span: self.span }))
Some(P(Ty { kind, id: self.id, span: self.span, tokens: None }))
}

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

Some(P(Ty { kind, id: self.id, span: self.span }))
Some(P(Ty { kind, id: self.id, span: self.span, tokens: None }))
}

pub fn precedence(&self) -> ExprPrecedence {
Expand Down Expand Up @@ -1867,6 +1867,7 @@ pub struct Ty {
pub id: NodeId,
pub kind: TyKind,
pub span: Span,
pub tokens: Option<TokenStream>,
}

#[derive(Clone, Encodable, Decodable, Debug)]
Expand Down Expand Up @@ -2145,7 +2146,7 @@ impl Param {
/// Builds a `Param` object from `ExplicitSelf`.
pub fn from_self(attrs: AttrVec, eself: ExplicitSelf, eself_ident: Ident) -> Param {
let span = eself.span.to(eself_ident.span);
let infer_ty = P(Ty { id: DUMMY_NODE_ID, kind: TyKind::ImplicitSelf, span });
let infer_ty = P(Ty { id: DUMMY_NODE_ID, kind: TyKind::ImplicitSelf, span, tokens: None });
let param = |mutbl, ty| Param {
attrs,
pat: P(Pat {
Expand All @@ -2168,6 +2169,7 @@ impl Param {
id: DUMMY_NODE_ID,
kind: TyKind::Rptr(lt, MutTy { ty: infer_ty, mutbl }),
span,
tokens: None,
}),
),
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_ast/src/mut_visit.rs
Expand Up @@ -451,7 +451,7 @@ pub fn noop_visit_ty_constraint<T: MutVisitor>(
}

pub fn noop_visit_ty<T: MutVisitor>(ty: &mut P<Ty>, vis: &mut T) {
let Ty { id, kind, span } = ty.deref_mut();
let Ty { id, kind, span, tokens: _ } = ty.deref_mut();
vis.visit_id(id);
match kind {
TyKind::Infer | TyKind::ImplicitSelf | TyKind::Err | TyKind::Never | TyKind::CVarArgs => {}
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_ast_lowering/src/lib.rs
Expand Up @@ -1106,6 +1106,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
id: node_id,
kind: TyKind::ImplTrait(impl_trait_node_id, bounds.clone()),
span: constraint.span,
tokens: None,
},
itctx,
);
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_builtin_macros/src/concat_idents.rs
Expand Up @@ -61,6 +61,7 @@ pub fn expand_concat_idents<'cx>(
id: ast::DUMMY_NODE_ID,
kind: ast::TyKind::Path(None, ast::Path::from_ident(self.ident)),
span: self.ident.span,
tokens: None,
}))
}
}
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_expand/src/base.rs
Expand Up @@ -607,6 +607,7 @@ impl DummyResult {
id: ast::DUMMY_NODE_ID,
kind: if is_error { ast::TyKind::Err } else { ast::TyKind::Tup(Vec::new()) },
span: sp,
tokens: None,
})
}
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_expand/src/build.rs
Expand Up @@ -54,7 +54,7 @@ impl<'a> ExtCtxt<'a> {
}

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

pub fn ty_path(&self, path: ast::Path) -> P<ast::Ty> {
Expand Down
12 changes: 8 additions & 4 deletions compiler/rustc_expand/src/placeholders.rs
Expand Up @@ -37,7 +37,8 @@ pub fn placeholder(
tokens: None,
})
};
let ty = || P(ast::Ty { id, kind: ast::TyKind::MacCall(mac_placeholder()), span });
let ty =
|| P(ast::Ty { id, kind: ast::TyKind::MacCall(mac_placeholder()), span, tokens: None });
let pat =
|| P(ast::Pat { id, kind: ast::PatKind::MacCall(mac_placeholder()), span, tokens: None });

Expand Down Expand Up @@ -88,9 +89,12 @@ pub fn placeholder(
kind: ast::PatKind::MacCall(mac_placeholder()),
tokens: None,
})),
AstFragmentKind::Ty => {
AstFragment::Ty(P(ast::Ty { id, span, kind: ast::TyKind::MacCall(mac_placeholder()) }))
}
AstFragmentKind::Ty => AstFragment::Ty(P(ast::Ty {
id,
span,
kind: ast::TyKind::MacCall(mac_placeholder()),
tokens: None,
})),
AstFragmentKind::Stmts => AstFragment::Stmts(smallvec![{
let mac = P(ast::MacCallStmt {
mac: mac_placeholder(),
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_parse/src/lib.rs
Expand Up @@ -270,6 +270,7 @@ pub fn nt_to_tokenstream(nt: &Nonterminal, sess: &ParseSess, span: Span) -> Toke
}
Nonterminal::NtBlock(ref block) => block.tokens.clone(),
Nonterminal::NtPat(ref pat) => pat.tokens.clone(),
Nonterminal::NtTy(ref ty) => ty.tokens.clone(),
Nonterminal::NtIdent(ident, is_raw) => {
Some(tokenstream::TokenTree::token(token::Ident(ident.name, is_raw), ident.span).into())
}
Expand Down
9 changes: 7 additions & 2 deletions compiler/rustc_parse/src/parser/diagnostics.rs
Expand Up @@ -28,7 +28,7 @@ pub(super) fn dummy_arg(ident: Ident) -> Param {
span: ident.span,
tokens: None,
});
let ty = Ty { kind: TyKind::Err, span: ident.span, id: ast::DUMMY_NODE_ID };
let ty = Ty { kind: TyKind::Err, span: ident.span, id: ast::DUMMY_NODE_ID, tokens: None };
Param {
attrs: AttrVec::default(),
id: ast::DUMMY_NODE_ID,
Expand Down Expand Up @@ -75,7 +75,12 @@ impl RecoverQPath for Ty {
Some(P(self.clone()))
}
fn recovered(qself: Option<QSelf>, path: ast::Path) -> Self {
Self { span: path.span, kind: TyKind::Path(qself, path), id: ast::DUMMY_NODE_ID }
Self {
span: path.span,
kind: TyKind::Path(qself, path),
id: ast::DUMMY_NODE_ID,
tokens: None,
}
}
}

Expand Down
9 changes: 7 additions & 2 deletions compiler/rustc_parse/src/parser/item.rs
Expand Up @@ -510,7 +510,12 @@ impl<'a> Parser<'a> {
{
let span = self.prev_token.span.between(self.token.span);
self.struct_span_err(span, "missing trait in a trait impl").emit();
P(Ty { kind: TyKind::Path(None, err_path(span)), span, id: DUMMY_NODE_ID })
P(Ty {
kind: TyKind::Path(None, err_path(span)),
span,
id: DUMMY_NODE_ID,
tokens: None,
})
} else {
self.parse_ty()?
};
Expand Down Expand Up @@ -1046,7 +1051,7 @@ impl<'a> Parser<'a> {

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

/// Parses an enum declaration.
Expand Down
9 changes: 8 additions & 1 deletion compiler/rustc_parse/src/parser/nonterminal.rs
Expand Up @@ -141,7 +141,14 @@ impl<'a> Parser<'a> {
token::NtExpr(expr)
}
NonterminalKind::Literal => token::NtLiteral(self.parse_literal_maybe_minus()?),
NonterminalKind::Ty => token::NtTy(self.parse_ty()?),
NonterminalKind::Ty => {
let (mut ty, tokens) = self.collect_tokens(|this| this.parse_ty())?;
// We have an eaten an NtTy, which could already have tokens
if ty.tokens.is_none() {
ty.tokens = Some(tokens);
}
token::NtTy(ty)
}
// this could be handled like a token, since it is one
NonterminalKind::Ident => {
if let Some((ident, is_raw)) = get_macro_ident(&self.token) {
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_parse/src/parser/ty.rs
Expand Up @@ -626,6 +626,6 @@ impl<'a> Parser<'a> {
}

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

0 comments on commit 1823dea

Please sign in to comment.