Skip to content
Merged
Show file tree
Hide file tree
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
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