Skip to content
Merged
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
12 changes: 10 additions & 2 deletions compiler/rustc_ast/src/tokenstream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,11 @@ impl TokenStream {

/// Push `tt` onto the end of the stream, possibly gluing it to the last
/// token. Uses `make_mut` to maximize efficiency.
pub fn push_tree(&mut self, tt: TokenTree) {
///
/// This is intended for specific proc macro use. For general `TokenStream`
/// construction within the compiler just build a `Vec<TokenTree>` with
/// normal `Vec` operations and then do `TokenStream::new`.
pub fn push_tree_with_gluing(&mut self, tt: TokenTree) {
let vec_mut = Arc::make_mut(&mut self.0);

if Self::try_glue_to_last(vec_mut, &tt) {
Expand All @@ -691,7 +695,11 @@ impl TokenStream {
/// Push `stream` onto the end of the stream, possibly gluing the first
/// token tree to the last token. (No other token trees will be glued.)
/// Uses `make_mut` to maximize efficiency.
pub fn push_stream(&mut self, stream: TokenStream) {
///
/// This is intended for specific proc macro use. For general `TokenStream`
/// construction within the compiler just build a `Vec<TokenTree>` with
/// normal `Vec` operations and then do `TokenStream::new`.
pub fn push_stream_with_gluing(&mut self, stream: TokenStream) {
let vec_mut = Arc::make_mut(&mut self.0);

let stream_iter = stream.0.iter().cloned();
Expand Down
18 changes: 9 additions & 9 deletions compiler/rustc_builtin_macros/src/contracts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ fn expand_contract_clause(
ecx: &mut ExtCtxt<'_>,
attr_span: Span,
annotated: TokenStream,
inject: impl FnOnce(&mut TokenStream) -> Result<(), ErrorGuaranteed>,
inject: impl FnOnce(&mut Vec<TokenTree>) -> Result<(), ErrorGuaranteed>,
) -> Result<TokenStream, ErrorGuaranteed> {
let mut new_tts = TokenStream::default();
let mut new_tts = vec![];
let mut cursor = annotated.iter();

let is_kw = |tt: &TokenTree, sym: Symbol| {
Expand All @@ -60,7 +60,7 @@ fn expand_contract_clause(
// Find the `fn` keyword to check if this is a function.
if cursor
.find(|tt| {
new_tts.push_tree((*tt).clone());
new_tts.push((*tt).clone());
is_kw(tt, kw::Fn)
})
.is_none()
Expand Down Expand Up @@ -102,7 +102,7 @@ fn expand_contract_clause(
if is_kw(tt, kw::Where) {
break tt;
}
new_tts.push_tree(tt.clone());
new_tts.push(tt.clone());
};

// At this point, we've transcribed everything from the `fn` through the formal parameter list
Expand All @@ -114,9 +114,9 @@ fn expand_contract_clause(

// Above we injected the internal AST requires/ensures construct. Now copy over all the other
// token trees.
new_tts.push_tree(next_tt.clone());
new_tts.push(next_tt.clone());
while let Some(tt) = cursor.next() {
new_tts.push_tree(tt.clone());
new_tts.push(tt.clone());
if cursor.peek().is_none()
&& !matches!(tt, TokenTree::Delimited(_, _, token::Delimiter::Brace, _))
{
Expand All @@ -127,7 +127,7 @@ fn expand_contract_clause(
}
}

Ok(new_tts)
Ok(TokenStream::new(new_tts))
}

fn expand_contract_clause_tts(
Expand All @@ -139,11 +139,11 @@ fn expand_contract_clause_tts(
) -> Result<TokenStream, ErrorGuaranteed> {
let feature_span = ecx.with_def_site_ctxt(attr_span);
expand_contract_clause(ecx, attr_span, annotated, |new_tts| {
new_tts.push_tree(TokenTree::Token(
new_tts.push(TokenTree::Token(
token::Token::from_ast_ident(Ident::new(clause_keyword, feature_span)),
Spacing::Joint,
));
new_tts.push_tree(TokenTree::Delimited(
new_tts.push(TokenTree::Delimited(
DelimSpan::from_single(attr_span),
DelimSpacing::new(Spacing::JointHidden, Spacing::JointHidden),
token::Delimiter::Brace,
Expand Down
9 changes: 6 additions & 3 deletions compiler/rustc_expand/src/mbe/transcribe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -565,9 +565,12 @@ fn transcribe_pnr<'tx>(

let leading_if_span =
guard.span_with_leading_if.with_hi(guard.span_with_leading_if.lo() + BytePos(2));
let mut ts =
TokenStream::token_alone(token::Ident(kw::If, IdentIsRaw::No), leading_if_span);
ts.push_stream(TokenStream::from_ast(&guard.cond));
let ts = std::iter::once(TokenTree::token_alone(
token::Ident(kw::If, IdentIsRaw::No),
leading_if_span,
))
.chain(TokenStream::from_ast(&guard.cond).iter().cloned())
Comment thread
Kobzol marked this conversation as resolved.
.collect();

mk_delimited(guard.span_with_leading_if, MetaVarKind::Guard, ts)
}
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_expand/src/proc_macro_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -663,7 +663,7 @@ impl server::Server for Rustc<'_, '_> {
let mut stream = base.unwrap_or_default();
for tree in trees {
for tt in (tree, &mut *self).to_internal() {
stream.push_tree(tt);
stream.push_tree_with_gluing(tt);
}
}
stream
Expand All @@ -676,7 +676,7 @@ impl server::Server for Rustc<'_, '_> {
) -> Self::TokenStream {
let mut stream = base.unwrap_or_default();
for s in streams {
stream.push_stream(s);
stream.push_stream_with_gluing(s);
}
stream
}
Expand Down
10 changes: 5 additions & 5 deletions compiler/rustc_parse/src/parser/tokenstream/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ fn test_concat() {
let test_fst = string_to_ts("foo::bar");
let test_snd = string_to_ts("::baz");
let mut eq_res = TokenStream::default();
eq_res.push_stream(test_fst);
eq_res.push_stream(test_snd);
eq_res.push_stream_with_gluing(test_fst);
eq_res.push_stream_with_gluing(test_snd);
assert_eq!(test_res.iter().count(), 5);
assert_eq!(eq_res.iter().count(), 5);
assert_eq!(cmp_token_stream(&test_res, &eq_res), true);
Expand Down Expand Up @@ -105,9 +105,9 @@ fn test_is_empty() {
fn test_dotdotdot() {
create_default_session_globals_then(|| {
let mut stream = TokenStream::default();
stream.push_tree(TokenTree::token_joint(token::Dot, sp(0, 1)));
stream.push_tree(TokenTree::token_joint(token::Dot, sp(1, 2)));
stream.push_tree(TokenTree::token_alone(token::Dot, sp(2, 3)));
stream.push_tree_with_gluing(TokenTree::token_joint(token::Dot, sp(0, 1)));
stream.push_tree_with_gluing(TokenTree::token_joint(token::Dot, sp(1, 2)));
stream.push_tree_with_gluing(TokenTree::token_alone(token::Dot, sp(2, 3)));
assert!(cmp_token_stream(&stream, &string_to_ts("...")));
assert_eq!(stream.iter().count(), 1);
})
Expand Down
Loading