Skip to content

Commit

Permalink
fix(expand): prevent infinity loop in macro containing only "///"
Browse files Browse the repository at this point in the history
  • Loading branch information
bvanjoi committed Jun 6, 2023
1 parent fd9bf59 commit c927743
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 0 deletions.
1 change: 1 addition & 0 deletions compiler/rustc_expand/src/mbe/macro_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,7 @@ pub(super) fn compute_locs(matcher: &[TokenTree]) -> Vec<MatcherLoc> {
}

/// A single matcher position, representing the state of matching.
#[derive(Debug)]
struct MatcherPos {
/// The index into `TtParser::locs`, which represents the "dot".
idx: usize,
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_expand/src/mbe/macro_rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -647,6 +647,7 @@ fn check_lhs_no_empty_seq(sess: &ParseSess, tts: &[mbe::TokenTree]) -> bool {
if seq.separator.is_none()
&& seq.tts.iter().all(|seq_tt| match seq_tt {
TokenTree::MetaVarDecl(_, _, Some(NonterminalKind::Vis)) => true,
TokenTree::Token(t) => matches!(t, Token { kind: DocComment(..), .. }),
TokenTree::Sequence(_, sub_seq) => {
sub_seq.kleene.op == mbe::KleeneOp::ZeroOrMore
|| sub_seq.kleene.op == mbe::KleeneOp::ZeroOrOne
Expand Down
36 changes: 36 additions & 0 deletions tests/ui/macros/issue-112342-1.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// same as #95267, ignore doc comment although it's a bug.

macro_rules! m1 {
(
$(
///
)*
//~^^^ERROR repetition matches empty token tree
) => {};
}

m1! {}

macro_rules! m2 {
(
$(
///
)+
//~^^^ERROR repetition matches empty token tree
) => {};
}

m2! {}

macro_rules! m3 {
(
$(
///
)?
//~^^^ERROR repetition matches empty token tree
) => {};
}

m3! {}

fn main() {}
29 changes: 29 additions & 0 deletions tests/ui/macros/issue-112342-1.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
error: repetition matches empty token tree
--> $DIR/issue-112342-1.rs:5:10
|
LL | $(
| __________^
LL | | ///
LL | | )*
| |_________^

error: repetition matches empty token tree
--> $DIR/issue-112342-1.rs:16:10
|
LL | $(
| __________^
LL | | ///
LL | | )+
| |_________^

error: repetition matches empty token tree
--> $DIR/issue-112342-1.rs:27:10
|
LL | $(
| __________^
LL | | ///
LL | | )?
| |_________^

error: aborting due to 3 previous errors

28 changes: 28 additions & 0 deletions tests/ui/macros/issue-112342-2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// check-pass

// same as #95267, ignore doc comment although it's a bug.

macro_rules! m1 {
(
$(
///
$expr: expr,
)*
) => {};
}

m1! {}

macro_rules! m2 {
(
$(
///
$expr: expr,
///
)*
) => {};
}

m2! {}

fn main() {}

0 comments on commit c927743

Please sign in to comment.