Skip to content

Commit fad07e8

Browse files
committed
Change Token::interpolated_to_tokenstream().
It is currently a method of `Token`, but it only is valid to call if `self` is a `Token::Interpolated`. This commit eliminates the possibility of misuse by changing it to an associated function that takes a `Nonterminal`, which also simplifies the call sites. This requires splitting out a new function, `nonterminal_to_string`.
1 parent f47ec2a commit fad07e8

File tree

4 files changed

+37
-39
lines changed

4 files changed

+37
-39
lines changed

src/librustc/hir/lowering.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1131,12 +1131,12 @@ impl<'a> LoweringContext<'a> {
11311131

11321132
fn lower_token(&mut self, token: Token, span: Span) -> TokenStream {
11331133
match token {
1134-
Token::Interpolated(_) => {}
1134+
Token::Interpolated(nt) => {
1135+
let tts = Token::interpolated_to_tokenstream(&self.sess.parse_sess, nt, span);
1136+
self.lower_token_stream(tts)
1137+
}
11351138
other => return TokenTree::Token(span, other).into(),
11361139
}
1137-
1138-
let tts = token.interpolated_to_tokenstream(&self.sess.parse_sess, span);
1139-
self.lower_token_stream(tts)
11401140
}
11411141

11421142
fn lower_arm(&mut self, arm: &Arm) -> hir::Arm {

src/libsyntax/parse/token.rs

+3-9
Original file line numberDiff line numberDiff line change
@@ -508,14 +508,8 @@ impl Token {
508508
}
509509
}
510510

511-
pub fn interpolated_to_tokenstream(&self, sess: &ParseSess, span: Span)
512-
-> TokenStream
513-
{
514-
let nt = match *self {
515-
Token::Interpolated(ref nt) => nt,
516-
_ => panic!("only works on interpolated tokens"),
517-
};
518-
511+
pub fn interpolated_to_tokenstream(sess: &ParseSess, nt: Lrc<(Nonterminal, LazyTokenStream)>,
512+
span: Span) -> TokenStream {
519513
// An `Interpolated` token means that we have a `Nonterminal`
520514
// which is often a parsed AST item. At this point we now need
521515
// to convert the parsed AST to an actual token stream, e.g.
@@ -558,7 +552,7 @@ impl Token {
558552

559553
let tokens_for_real = nt.1.force(|| {
560554
// FIXME(#43081): Avoid this pretty-print + reparse hack
561-
let source = pprust::token_to_string(self);
555+
let source = pprust::nonterminal_to_string(&nt.0);
562556
let filename = FileName::macro_expansion_source_code(&source);
563557
let (tokens, errors) = parse_stream_from_source_str(
564558
filename, source, sess, Some(span));

src/libsyntax/print/pprust.rs

+28-24
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::ast::{Attribute, MacDelimiter, GenericArg};
44
use crate::util::parser::{self, AssocOp, Fixity};
55
use crate::attr;
66
use crate::source_map::{self, SourceMap, Spanned};
7-
use crate::parse::token::{self, BinOpToken, Token};
7+
use crate::parse::token::{self, BinOpToken, Nonterminal, Token};
88
use crate::parse::lexer::comments;
99
use crate::parse::{self, ParseSess};
1010
use crate::print::pp::{self, Breaks};
@@ -257,29 +257,33 @@ pub fn token_to_string(tok: &Token) -> String {
257257
token::Comment => "/* */".to_string(),
258258
token::Shebang(s) => format!("/* shebang: {}*/", s),
259259

260-
token::Interpolated(ref nt) => match nt.0 {
261-
token::NtExpr(ref e) => expr_to_string(e),
262-
token::NtMeta(ref e) => meta_item_to_string(e),
263-
token::NtTy(ref e) => ty_to_string(e),
264-
token::NtPath(ref e) => path_to_string(e),
265-
token::NtItem(ref e) => item_to_string(e),
266-
token::NtBlock(ref e) => block_to_string(e),
267-
token::NtStmt(ref e) => stmt_to_string(e),
268-
token::NtPat(ref e) => pat_to_string(e),
269-
token::NtIdent(e, false) => ident_to_string(e),
270-
token::NtIdent(e, true) => format!("r#{}", ident_to_string(e)),
271-
token::NtLifetime(e) => ident_to_string(e),
272-
token::NtLiteral(ref e) => expr_to_string(e),
273-
token::NtTT(ref tree) => tt_to_string(tree.clone()),
274-
token::NtArm(ref e) => arm_to_string(e),
275-
token::NtImplItem(ref e) => impl_item_to_string(e),
276-
token::NtTraitItem(ref e) => trait_item_to_string(e),
277-
token::NtGenerics(ref e) => generic_params_to_string(&e.params),
278-
token::NtWhereClause(ref e) => where_clause_to_string(e),
279-
token::NtArg(ref e) => arg_to_string(e),
280-
token::NtVis(ref e) => vis_to_string(e),
281-
token::NtForeignItem(ref e) => foreign_item_to_string(e),
282-
}
260+
token::Interpolated(ref nt) => nonterminal_to_string(&nt.0),
261+
}
262+
}
263+
264+
pub fn nonterminal_to_string(nt: &Nonterminal) -> String {
265+
match *nt {
266+
token::NtExpr(ref e) => expr_to_string(e),
267+
token::NtMeta(ref e) => meta_item_to_string(e),
268+
token::NtTy(ref e) => ty_to_string(e),
269+
token::NtPath(ref e) => path_to_string(e),
270+
token::NtItem(ref e) => item_to_string(e),
271+
token::NtBlock(ref e) => block_to_string(e),
272+
token::NtStmt(ref e) => stmt_to_string(e),
273+
token::NtPat(ref e) => pat_to_string(e),
274+
token::NtIdent(e, false) => ident_to_string(e),
275+
token::NtIdent(e, true) => format!("r#{}", ident_to_string(e)),
276+
token::NtLifetime(e) => ident_to_string(e),
277+
token::NtLiteral(ref e) => expr_to_string(e),
278+
token::NtTT(ref tree) => tt_to_string(tree.clone()),
279+
token::NtArm(ref e) => arm_to_string(e),
280+
token::NtImplItem(ref e) => impl_item_to_string(e),
281+
token::NtTraitItem(ref e) => trait_item_to_string(e),
282+
token::NtGenerics(ref e) => generic_params_to_string(&e.params),
283+
token::NtWhereClause(ref e) => where_clause_to_string(e),
284+
token::NtArg(ref e) => arg_to_string(e),
285+
token::NtVis(ref e) => vis_to_string(e),
286+
token::NtForeignItem(ref e) => foreign_item_to_string(e),
283287
}
284288
}
285289

src/libsyntax_ext/proc_macro_server.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -178,8 +178,8 @@ impl FromInternal<(TreeAndJoint, &'_ ParseSess, &'_ mut Vec<Self>)>
178178
tt!(Punct::new('#', false))
179179
}
180180

181-
Interpolated(_) => {
182-
let stream = token.interpolated_to_tokenstream(sess, span);
181+
Interpolated(nt) => {
182+
let stream = Token::interpolated_to_tokenstream(sess, nt, span);
183183
TokenTree::Group(Group {
184184
delimiter: Delimiter::None,
185185
stream,

0 commit comments

Comments
 (0)