Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

syntax: Unify macro and attribute arguments in AST #66935

Merged
merged 6 commits into from
Dec 3, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Address review comments
petrochenkov committed Dec 2, 2019

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
commit 498737c8e9cf52be1bde3bef7ffa24a3d0540257
2 changes: 2 additions & 0 deletions src/librustc_parse/lib.rs
Original file line number Diff line number Diff line change
@@ -277,6 +277,8 @@ pub fn parse_in_attr<'a, T>(
) -> PResult<'a, T> {
let mut parser = Parser::new(
sess,
// FIXME(#66940, Centril | petrochenkov): refactor this function so it doesn't
// require reconstructing and immediately re-parsing delimiters.
attr.get_normal_item().args.outer_tokens(),
None,
false,
3 changes: 2 additions & 1 deletion src/librustc_parse/parser/mod.rs
Original file line number Diff line number Diff line change
@@ -1024,7 +1024,8 @@ impl<'a> Parser<'a> {
self.check(&token::OpenDelim(DelimToken::Brace)) {
match self.parse_token_tree() {
TokenTree::Delimited(dspan, delim, tokens) =>
MacArgs::Delimited(dspan, MacDelimiter::from_token(delim), tokens),
// We've confirmed above that there is a delimiter so unwrapping is OK.
MacArgs::Delimited(dspan, MacDelimiter::from_token(delim).unwrap(), tokens),
_ => unreachable!(),
}
} else if !delimited_only {
22 changes: 13 additions & 9 deletions src/libsyntax/ast.rs
Original file line number Diff line number Diff line change
@@ -1396,8 +1396,12 @@ pub enum MacArgs {
/// Delimited arguments - `#[attr()/[]/{}]` or `mac!()/[]/{}`.
Delimited(DelimSpan, MacDelimiter, TokenStream),
/// Arguments of a key-value attribute - `#[attr = "value"]`.
/// Span belongs to the `=` token, token stream is the "value".
Eq(Span, TokenStream),
Eq(
/// Span of the `=` token.
Span,
/// Token stream of the "value".
TokenStream,
),
}

impl MacArgs {
@@ -1421,13 +1425,13 @@ impl MacArgs {
pub fn inner_tokens(&self) -> TokenStream {
match self {
MacArgs::Empty => TokenStream::default(),
MacArgs::Delimited(.., tokens) => tokens.clone(),
MacArgs::Delimited(.., tokens) |
MacArgs::Eq(.., tokens) => tokens.clone(),
}
}

/// Tokens together with the delimiters or `=`.
/// Use of this functions generally means that something suboptimal or hacky is happening.
/// Use of this method generally means that something suboptimal or hacky is happening.
pub fn outer_tokens(&self) -> TokenStream {
match *self {
MacArgs::Empty => TokenStream::default(),
@@ -1461,12 +1465,12 @@ impl MacDelimiter {
}
}

pub fn from_token(delim: DelimToken) -> MacDelimiter {
pub fn from_token(delim: DelimToken) -> Option<MacDelimiter> {
match delim {
token::Paren => MacDelimiter::Parenthesis,
token::Bracket => MacDelimiter::Bracket,
token::Brace => MacDelimiter::Brace,
token::NoDelim => panic!("expected a delimiter"),
token::Paren => Some(MacDelimiter::Parenthesis),
token::Bracket => Some(MacDelimiter::Bracket),
token::Brace => Some(MacDelimiter::Brace),
token::NoDelim => None,
}
}
}