Skip to content

Commit c927743

Browse files
committed
fix(expand): prevent infinity loop in macro containing only "///"
1 parent fd9bf59 commit c927743

File tree

5 files changed

+95
-0
lines changed

5 files changed

+95
-0
lines changed

compiler/rustc_expand/src/mbe/macro_parser.rs

+1
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ pub(super) fn compute_locs(matcher: &[TokenTree]) -> Vec<MatcherLoc> {
249249
}
250250

251251
/// A single matcher position, representing the state of matching.
252+
#[derive(Debug)]
252253
struct MatcherPos {
253254
/// The index into `TtParser::locs`, which represents the "dot".
254255
idx: usize,

compiler/rustc_expand/src/mbe/macro_rules.rs

+1
Original file line numberDiff line numberDiff line change
@@ -647,6 +647,7 @@ fn check_lhs_no_empty_seq(sess: &ParseSess, tts: &[mbe::TokenTree]) -> bool {
647647
if seq.separator.is_none()
648648
&& seq.tts.iter().all(|seq_tt| match seq_tt {
649649
TokenTree::MetaVarDecl(_, _, Some(NonterminalKind::Vis)) => true,
650+
TokenTree::Token(t) => matches!(t, Token { kind: DocComment(..), .. }),
650651
TokenTree::Sequence(_, sub_seq) => {
651652
sub_seq.kleene.op == mbe::KleeneOp::ZeroOrMore
652653
|| sub_seq.kleene.op == mbe::KleeneOp::ZeroOrOne

tests/ui/macros/issue-112342-1.rs

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
// same as #95267, ignore doc comment although it's a bug.
2+
3+
macro_rules! m1 {
4+
(
5+
$(
6+
///
7+
)*
8+
//~^^^ERROR repetition matches empty token tree
9+
) => {};
10+
}
11+
12+
m1! {}
13+
14+
macro_rules! m2 {
15+
(
16+
$(
17+
///
18+
)+
19+
//~^^^ERROR repetition matches empty token tree
20+
) => {};
21+
}
22+
23+
m2! {}
24+
25+
macro_rules! m3 {
26+
(
27+
$(
28+
///
29+
)?
30+
//~^^^ERROR repetition matches empty token tree
31+
) => {};
32+
}
33+
34+
m3! {}
35+
36+
fn main() {}

tests/ui/macros/issue-112342-1.stderr

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
error: repetition matches empty token tree
2+
--> $DIR/issue-112342-1.rs:5:10
3+
|
4+
LL | $(
5+
| __________^
6+
LL | | ///
7+
LL | | )*
8+
| |_________^
9+
10+
error: repetition matches empty token tree
11+
--> $DIR/issue-112342-1.rs:16:10
12+
|
13+
LL | $(
14+
| __________^
15+
LL | | ///
16+
LL | | )+
17+
| |_________^
18+
19+
error: repetition matches empty token tree
20+
--> $DIR/issue-112342-1.rs:27:10
21+
|
22+
LL | $(
23+
| __________^
24+
LL | | ///
25+
LL | | )?
26+
| |_________^
27+
28+
error: aborting due to 3 previous errors
29+

tests/ui/macros/issue-112342-2.rs

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// check-pass
2+
3+
// same as #95267, ignore doc comment although it's a bug.
4+
5+
macro_rules! m1 {
6+
(
7+
$(
8+
///
9+
$expr: expr,
10+
)*
11+
) => {};
12+
}
13+
14+
m1! {}
15+
16+
macro_rules! m2 {
17+
(
18+
$(
19+
///
20+
$expr: expr,
21+
///
22+
)*
23+
) => {};
24+
}
25+
26+
m2! {}
27+
28+
fn main() {}

0 commit comments

Comments
 (0)