Overhaul MacArgs::Eq
.
The value in `MacArgs::Eq` is currently represented as a `Token`. Because of `TokenKind::Interpolated`, `Token` can be either a token or an arbitrary AST fragment. In practice, a `MacArgs::Eq` starts out as a literal or macro call AST fragment, and then is later lowered to a literal token. But this is very non-obvious. `Token` is a much more general type than what is needed. This commit restricts things, by introducing a new type `MacArgsEqKind` that is either an AST expression (pre-lowering) or an AST literal (post-lowering). The downside is that the code is a bit more verbose in a few places. The benefit is that makes it much clearer what the possibilities are (though also shorter in some other places). Also, it removes one use of `TokenKind::Interpolated`, taking us a step closer to removing that variant, which will let us make `Token` impl `Copy` and remove many "handle Interpolated" code paths in the parser. Things to note: - Error messages have improved. Messages like this: ``` unexpected token: `"bug" + "found"` ``` now say "unexpected expression", which makes more sense. Although arbitrary expressions can exist within tokens thanks to `TokenKind::Interpolated`, that's not obvious to anyone who doesn't know compiler internals. - In `parse_mac_args_common`, we no longer need to collect tokens for the value expression.
This commit is contained in:
parent
ae5f67f9e8
commit
99f5945f85
21 changed files with 174 additions and 88 deletions
|
@ -13,7 +13,7 @@ use rustc_ast::util::comments::{gather_comments, Comment, CommentStyle};
|
|||
use rustc_ast::util::parser;
|
||||
use rustc_ast::{self as ast, BlockCheckMode, PatKind, RangeEnd, RangeSyntax};
|
||||
use rustc_ast::{attr, Term};
|
||||
use rustc_ast::{GenericArg, MacArgs};
|
||||
use rustc_ast::{GenericArg, MacArgs, MacArgsEq};
|
||||
use rustc_ast::{GenericBound, SelfKind, TraitBoundModifier};
|
||||
use rustc_ast::{InlineAsmOperand, InlineAsmRegOrRegClass};
|
||||
use rustc_ast::{InlineAsmOptions, InlineAsmTemplatePiece};
|
||||
|
@ -472,11 +472,18 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
|
|||
MacArgs::Empty => {
|
||||
self.print_path(&item.path, false, 0);
|
||||
}
|
||||
MacArgs::Eq(_, token) => {
|
||||
MacArgs::Eq(_, MacArgsEq::Ast(expr)) => {
|
||||
self.print_path(&item.path, false, 0);
|
||||
self.space();
|
||||
self.word_space("=");
|
||||
let token_str = self.token_to_string_ext(token, true);
|
||||
let token_str = self.expr_to_string(expr);
|
||||
self.word(token_str);
|
||||
}
|
||||
MacArgs::Eq(_, MacArgsEq::Hir(lit)) => {
|
||||
self.print_path(&item.path, false, 0);
|
||||
self.space();
|
||||
self.word_space("=");
|
||||
let token_str = self.literal_to_string(lit);
|
||||
self.word(token_str);
|
||||
}
|
||||
}
|
||||
|
@ -818,6 +825,10 @@ pub trait PrintState<'a>: std::ops::Deref<Target = pp::Printer> + std::ops::Dere
|
|||
Self::to_string(|s| s.print_expr(e))
|
||||
}
|
||||
|
||||
fn literal_to_string(&self, lit: &ast::Lit) -> String {
|
||||
Self::to_string(|s| s.print_literal(lit))
|
||||
}
|
||||
|
||||
fn tt_to_string(&self, tt: &TokenTree) -> String {
|
||||
Self::to_string(|s| s.print_tt(tt, false))
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue