Rollup merge of #128994 - nnethercote:fix-Parser-look_ahead-more, r=compiler-errors

Fix bug in `Parser::look_ahead`.

The special case was failing to handle invisible delimiters on one path.

Fixes (but doesn't close until beta backported) #128895.

r? `@davidtwco`
This commit is contained in:
Guillaume Gomez 2024-08-12 17:09:20 +02:00 committed by GitHub
commit 99a785d62d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 64 additions and 4 deletions

View File

@ -1167,10 +1167,12 @@ pub fn look_ahead<R>(&self, dist: usize, looker: impl FnOnce(&Token) -> R) -> R
match self.token_cursor.tree_cursor.look_ahead(0) { match self.token_cursor.tree_cursor.look_ahead(0) {
Some(tree) => { Some(tree) => {
// Indexing stayed within the current token tree. // Indexing stayed within the current token tree.
return match tree { match tree {
TokenTree::Token(token, _) => looker(token), TokenTree::Token(token, _) => return looker(token),
TokenTree::Delimited(dspan, _, delim, _) => { &TokenTree::Delimited(dspan, _, delim, _) => {
looker(&Token::new(token::OpenDelim(*delim), dspan.open)) if delim != Delimiter::Invisible {
return looker(&Token::new(token::OpenDelim(delim), dspan.open));
}
} }
}; };
} }

View File

@ -0,0 +1,44 @@
//@ force-host
//@ no-prefer-dynamic
#![crate_type = "proc-macro"]
extern crate proc_macro;
use proc_macro::*;
// This proc macro ignores its input and returns this token stream
//
// impl <«A1»: Comparable> Comparable for («A1»,) {}
//
// where `«`/`»` are invisible delimiters. This was being misparsed in bug
// #128895.
#[proc_macro]
pub fn main(_input: TokenStream) -> TokenStream {
let a1 = TokenTree::Group(
Group::new(
Delimiter::None,
std::iter::once(TokenTree::Ident(Ident::new("A1", Span::call_site()))).collect(),
)
);
vec![
TokenTree::Ident(Ident::new("impl", Span::call_site())),
TokenTree::Punct(Punct::new('<', Spacing::Alone)),
a1.clone(),
TokenTree::Punct(Punct::new(':', Spacing::Alone)),
TokenTree::Ident(Ident::new("Comparable", Span::call_site())),
TokenTree::Punct(Punct::new('>', Spacing::Alone)),
TokenTree::Ident(Ident::new("Comparable", Span::call_site())),
TokenTree::Ident(Ident::new("for", Span::call_site())),
TokenTree::Group(
Group::new(
Delimiter::Parenthesis,
vec![
a1.clone(),
TokenTree::Punct(Punct::new(',', Spacing::Alone)),
].into_iter().collect::<TokenStream>(),
)
),
TokenTree::Group(Group::new(Delimiter::Brace, TokenStream::new())),
].into_iter().collect::<TokenStream>()
}

View File

@ -0,0 +1,14 @@
//@ aux-build:parse-invis-delim-issue-128895.rs
//@ check-pass
#![no_std] // Don't load unnecessary hygiene information from std
extern crate std;
#[macro_use]
extern crate parse_invis_delim_issue_128895;
trait Comparable {}
parse_invis_delim_issue_128895::main!();
fn main() {}