Skip to content

Commit 99a785d

Browse files
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`
2 parents 7c6dca9 + 46b4c5a commit 99a785d

File tree

3 files changed

+64
-4
lines changed

3 files changed

+64
-4
lines changed

compiler/rustc_parse/src/parser/mod.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -1167,10 +1167,12 @@ impl<'a> Parser<'a> {
11671167
match self.token_cursor.tree_cursor.look_ahead(0) {
11681168
Some(tree) => {
11691169
// Indexing stayed within the current token tree.
1170-
return match tree {
1171-
TokenTree::Token(token, _) => looker(token),
1172-
TokenTree::Delimited(dspan, _, delim, _) => {
1173-
looker(&Token::new(token::OpenDelim(*delim), dspan.open))
1170+
match tree {
1171+
TokenTree::Token(token, _) => return looker(token),
1172+
&TokenTree::Delimited(dspan, _, delim, _) => {
1173+
if delim != Delimiter::Invisible {
1174+
return looker(&Token::new(token::OpenDelim(delim), dspan.open));
1175+
}
11741176
}
11751177
};
11761178
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
//@ force-host
2+
//@ no-prefer-dynamic
3+
4+
#![crate_type = "proc-macro"]
5+
6+
extern crate proc_macro;
7+
8+
use proc_macro::*;
9+
10+
// This proc macro ignores its input and returns this token stream
11+
//
12+
// impl <«A1»: Comparable> Comparable for («A1»,) {}
13+
//
14+
// where `«`/`»` are invisible delimiters. This was being misparsed in bug
15+
// #128895.
16+
#[proc_macro]
17+
pub fn main(_input: TokenStream) -> TokenStream {
18+
let a1 = TokenTree::Group(
19+
Group::new(
20+
Delimiter::None,
21+
std::iter::once(TokenTree::Ident(Ident::new("A1", Span::call_site()))).collect(),
22+
)
23+
);
24+
vec![
25+
TokenTree::Ident(Ident::new("impl", Span::call_site())),
26+
TokenTree::Punct(Punct::new('<', Spacing::Alone)),
27+
a1.clone(),
28+
TokenTree::Punct(Punct::new(':', Spacing::Alone)),
29+
TokenTree::Ident(Ident::new("Comparable", Span::call_site())),
30+
TokenTree::Punct(Punct::new('>', Spacing::Alone)),
31+
TokenTree::Ident(Ident::new("Comparable", Span::call_site())),
32+
TokenTree::Ident(Ident::new("for", Span::call_site())),
33+
TokenTree::Group(
34+
Group::new(
35+
Delimiter::Parenthesis,
36+
vec![
37+
a1.clone(),
38+
TokenTree::Punct(Punct::new(',', Spacing::Alone)),
39+
].into_iter().collect::<TokenStream>(),
40+
)
41+
),
42+
TokenTree::Group(Group::new(Delimiter::Brace, TokenStream::new())),
43+
].into_iter().collect::<TokenStream>()
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//@ aux-build:parse-invis-delim-issue-128895.rs
2+
//@ check-pass
3+
4+
#![no_std] // Don't load unnecessary hygiene information from std
5+
extern crate std;
6+
7+
#[macro_use]
8+
extern crate parse_invis_delim_issue_128895;
9+
10+
trait Comparable {}
11+
12+
parse_invis_delim_issue_128895::main!();
13+
14+
fn main() {}

0 commit comments

Comments
 (0)