Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
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
1,038 changes: 904 additions & 134 deletions compiler/rustc_ast/src/tokenstream.rs

Large diffs are not rendered by default.

21 changes: 18 additions & 3 deletions compiler/rustc_expand/src/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::rc::Rc;
use std::sync::Arc;

use rustc_ast::attr::MarkedAttrs;
use rustc_ast::tokenstream::TokenStream;
use rustc_ast::tokenstream::{FlatTokenCursor, TokenStream};
use rustc_ast::visit::{AssocCtxt, Visitor};
use rustc_ast::{self as ast, AttrVec, Attribute, HasAttrs, Item, NodeId, PatKind, Safety};
use rustc_data_structures::fx::{FxHashMap, FxIndexMap};
Expand Down Expand Up @@ -278,8 +278,23 @@ impl<'cx> MacroExpanderResult<'cx> {
) -> Self {
// Emit SEMICOLON_IN_EXPRESSIONS_FROM_MACROS here, rather than the NON_LOCAL version.
let is_local = true;
let parser =
ParserAnyMacro::from_tts(cx, tts, site_span, arm_span, is_local, macro_ident, &[], &[]);

// Parse an existing view in place; only tree-backed streams need the
// flattening pass.
let cursor = match tts.flat_view() {
Some(view) => FlatTokenCursor::from_view(view),
None => FlatTokenCursor::new(tts),
};
let parser = ParserAnyMacro::from_flat(
cx,
cursor,
site_span,
arm_span,
is_local,
macro_ident,
&[],
&[],
);
ExpandResult::Ready(Box::new(parser))
}
}
Expand Down
36 changes: 25 additions & 11 deletions compiler/rustc_expand/src/mbe/macro_rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use ast::token::IdentIsRaw;
use rustc_ast::token::NtPatKind::*;
use rustc_ast::token::TokenKind::*;
use rustc_ast::token::{self, Delimiter, NonterminalKind, Token, TokenKind};
use rustc_ast::tokenstream::{self, DelimSpan, TokenStream};
use rustc_ast::tokenstream::{self, DelimSpan, FlatTokenCursor, TokenStream};
use rustc_ast::{self as ast, DUMMY_NODE_ID, NodeId, Safety};
use rustc_ast_pretty::pprust;
use rustc_data_structures::fx::{FxHashMap, FxIndexMap};
Expand Down Expand Up @@ -120,10 +120,10 @@ impl<'a, 'b> ParserAnyMacro<'a, 'b> {
fragment
}

#[instrument(skip(cx, tts, bindings, matched_rule_bindings))]
pub(crate) fn from_tts<'cx>(
#[instrument(skip(cx, flat, bindings, matched_rule_bindings))]
pub(crate) fn from_flat<'cx>(
cx: &'cx mut ExtCtxt<'a>,
tts: TokenStream,
flat: FlatTokenCursor,
site_span: Span,
arm_span: Span,
is_local: bool,
Expand All @@ -133,7 +133,7 @@ impl<'a, 'b> ParserAnyMacro<'a, 'b> {
matched_rule_bindings: &'b [MatcherLoc],
) -> Self {
Self {
parser: Parser::new(&cx.sess.psess, tts, None),
parser: Parser::new_from_flat(&cx.sess.psess, flat, None),

// Pass along the original expansion site and the name of the macro
// so we can print a useful error message if the parse of the expanded
Expand Down Expand Up @@ -256,7 +256,8 @@ impl MacroRulesMacroExpander {

let id = cx.current_expansion.id;
let tts = transcribe(psess, &named_matches, rhs, *rhs_span, self.transparency, id)
.map_err(|e| e.emit())?;
.map_err(|e| e.emit())?
.to_token_stream();

if cx.trace_macros() {
let msg = format!("to `{}`", pprust::tts_to_string(&tts));
Expand Down Expand Up @@ -462,16 +463,16 @@ fn expand_macro<'cx, 'a: 'cx>(

// rhs has holes ( `$id` and `$(...)` that need filled)
let id = cx.current_expansion.id;
let tts = match transcribe(psess, &named_matches, rhs, *rhs_span, transparency, id) {
Ok(tts) => tts,
let flat = match transcribe(psess, &named_matches, rhs, *rhs_span, transparency, id) {
Ok(flat) => flat,
Err(err) => {
let guar = err.emit();
return DummyResult::any(arm_span, guar);
}
};

if cx.trace_macros() {
let msg = format!("to `{}`", pprust::tts_to_string(&tts));
let msg = format!("to `{}`", pprust::tts_to_string(&flat.to_token_stream()));
trace_macros_note(&mut cx.expansions, sp, msg);
}

Expand All @@ -481,7 +482,7 @@ fn expand_macro<'cx, 'a: 'cx>(
}

// Let the context choose how to interpret the result. Weird, but useful for X-macros.
Box::new(ParserAnyMacro::from_tts(cx, tts, sp, arm_span, is_local, name, rules, lhs))
Box::new(ParserAnyMacro::from_flat(cx, flat, sp, arm_span, is_local, name, rules, lhs))
}
Err(CanRetry::No(guar)) => {
debug!("Will not retry matching as an error was emitted already");
Expand Down Expand Up @@ -559,7 +560,8 @@ fn expand_macro_attr(

let id = cx.current_expansion.id;
let tts = transcribe(psess, &named_matches, rhs, *rhs_span, transparency, id)
.map_err(|e| e.emit())?;
.map_err(|e| e.emit())?
.to_token_stream();

if cx.trace_macros() {
let msg = format!("to `{}`", pprust::tts_to_string(&tts));
Expand Down Expand Up @@ -1865,6 +1867,18 @@ pub(super) fn parser_from_cx(
mut tts: TokenStream,
recovery: Recovery,
) -> Parser<'_> {
// Macro-invocation arguments usually arrive as a lazy view of the flat
// token buffer; parse straight from it, unless doc comments require the
// desugaring pre-pass (rare). The scan is O(arguments) per invocation,
// but so was the tree walk `desugar_doc_comments` did here before; the
// flat scan replaces it, not adds to it.
if let Some(view) = tts.flat_view()
&& !view.entries().iter().any(|e| matches!(e.token().kind, token::DocComment(..)))
{
let cursor = FlatTokenCursor::from_view(view);
return Parser::new_from_flat(psess, cursor, rustc_parse::MACRO_ARGUMENTS)
.recovery(recovery);
}
tts.desugar_doc_comments();
Parser::new(psess, tts, rustc_parse::MACRO_ARGUMENTS).recovery(recovery)
}
Loading
Loading