Skip to content

Commit a9d967e

Browse files
committed
rust: macros: versions: Make <foo> parsing stricter
This was previously breaking the init!() macros which use <- as part of their syntax (and would've also broken operators like <). Restrict the angle bracket nesting parsing to only start after ::, unless we're in a struct definition. Signed-off-by: Asahi Lina <[email protected]>
1 parent f75716c commit a9d967e

1 file changed

Lines changed: 29 additions & 8 deletions

File tree

rust/macros/versions.rs

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,18 +18,29 @@ fn expect_punct(it: &mut impl Iterator<Item = TokenTree>) -> String {
1818
}
1919
}
2020

21-
fn drop_until_punct(it: &mut impl Iterator<Item = TokenTree>, delimiter: &str) {
21+
fn drop_until_punct(it: &mut impl Iterator<Item = TokenTree>, delimiter: &str, is_struct: bool) {
2222
let mut depth: isize = 0;
23+
let mut colons: isize = 0;
2324
for token in it.by_ref() {
2425
if let TokenTree::Punct(punct) = token {
2526
match punct.as_char() {
27+
':' => {
28+
colons += 1;
29+
}
2630
'<' => {
27-
depth += 1;
31+
if depth > 0 || colons == 2 || is_struct {
32+
depth += 1;
33+
}
34+
colons = 0;
2835
}
2936
'>' => {
30-
depth -= 1;
37+
if depth > 0 {
38+
depth -= 1;
39+
}
40+
colons = 0;
3141
}
3242
_ => {
43+
colons = 0;
3344
if depth == 0 && delimiter.contains(&punct.to_string()) {
3445
break;
3546
}
@@ -41,16 +52,26 @@ fn drop_until_punct(it: &mut impl Iterator<Item = TokenTree>, delimiter: &str) {
4152

4253
fn drop_until_braces(it: &mut impl Iterator<Item = TokenTree>) {
4354
let mut depth: isize = 0;
55+
let mut colons: isize = 0;
4456
for token in it.by_ref() {
4557
match token {
4658
TokenTree::Punct(punct) => match punct.as_char() {
59+
':' => {
60+
colons += 1;
61+
}
4762
'<' => {
48-
depth += 1;
63+
if depth > 0 || colons == 2 {
64+
depth += 1;
65+
}
66+
colons = 0;
4967
}
5068
'>' => {
51-
depth -= 1;
69+
if depth > 0 {
70+
depth -= 1;
71+
}
72+
colons = 0;
5273
}
53-
_ => (),
74+
_ => colons = 0,
5475
},
5576
TokenTree::Group(group) if group.delimiter() == Delimiter::Brace => {
5677
if depth == 0 {
@@ -169,7 +190,7 @@ fn filter_versions(
169190
TokenTree::Ident(ident) if ident.to_string() == "ver" => {
170191
if check_version(config, ver, &mut grp_it) {
171192
} else if is_struct {
172-
drop_until_punct(&mut it, ",");
193+
drop_until_punct(&mut it, ",", true);
173194
} else {
174195
let first = it.next().unwrap();
175196
match &first {
@@ -181,7 +202,7 @@ fn filter_versions(
181202
}
182203
TokenTree::Group(_) => (),
183204
_ => {
184-
drop_until_punct(&mut it, ",;");
205+
drop_until_punct(&mut it, ",;", false);
185206
}
186207
}
187208
}

0 commit comments

Comments
 (0)