Skip to content

Commit

Permalink
Rollup merge of rust-lang#111923 - c410-f3r:impl, r=oli-obk
Browse files Browse the repository at this point in the history
[RFC-3086] Consider out-of-bound depths of `count`

Fix rust-lang#111905

In the matching of macro calls and their respective declarations (transcribe), a warning is issued if `count` has a depth greater than the number of nested `NamedMatch`s through be verification of `MatchedSeq` with empty elements.

Doesn't affect `( $( { $( [ $( ( $( $t:ident )* ) )* ] )* } )* ) => { ${count(t, 1)} }` called with `bar!( { [] [] } )` which will still continue to output `2`.
  • Loading branch information
TaKO8Ki committed Jun 27, 2023
2 parents a5f5328 + 5505f91 commit 1817bb2
Show file tree
Hide file tree
Showing 8 changed files with 55 additions and 10 deletions.
2 changes: 2 additions & 0 deletions compiler/rustc_expand/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ expand_meta_var_dif_seq_matchers = {$msg}
expand_meta_var_expr_unrecognized_var =
variable `{$key}` is not recognized in meta-variable expression
expand_missing_count_fragment =
related fragment that refers the `count` meta-variable expression was not found
expand_module_circular =
circular modules: {$modules}
Expand Down
7 changes: 7 additions & 0 deletions compiler/rustc_expand/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,3 +407,10 @@ pub struct DuplicateMatcherBinding {
#[label(expand_label2)]
pub prev: Span,
}

#[derive(Diagnostic)]
#[diag(expand_missing_count_fragment)]
pub(crate) struct MissingCountFragment {
#[primary_span]
pub span: Span,
}
2 changes: 1 addition & 1 deletion compiler/rustc_expand/src/mbe/metavar_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use rustc_span::Span;
#[derive(Debug, Clone, PartialEq, Encodable, Decodable)]
pub(crate) enum MetaVarExpr {
/// The number of repetitions of an identifier, optionally limited to a number
/// of outer-most repetition depths. If the depth limit is `None` then the depth is unlimited.
/// of outer-most repetition depths.
Count(Ident, Option<usize>),

/// Ignore a meta-variable for repetition without expansion.
Expand Down
9 changes: 6 additions & 3 deletions compiler/rustc_expand/src/mbe/transcribe.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::base::ExtCtxt;
use crate::errors::{
CountRepetitionMisplaced, MetaVarExprUnrecognizedVar, MetaVarsDifSeqMatchers, MustRepeatOnce,
NoSyntaxVarsExprRepeat, VarStillRepeating,
CountRepetitionMisplaced, MetaVarExprUnrecognizedVar, MetaVarsDifSeqMatchers,
MissingCountFragment, MustRepeatOnce, NoSyntaxVarsExprRepeat, VarStillRepeating,
};
use crate::mbe::macro_parser::{MatchedNonterminal, MatchedSeq, MatchedTokenTree, NamedMatch};
use crate::mbe::{self, MetaVarExpr};
Expand Down Expand Up @@ -448,6 +448,9 @@ fn count_repetitions<'a>(
}
}
MatchedSeq(named_matches) => {
if named_matches.is_empty() {
return Err(cx.create_err(MissingCountFragment { span: sp.entire() }));
}
let new_declared_lhs_depth = declared_lhs_depth + 1;
match depth_opt {
None => named_matches
Expand Down Expand Up @@ -506,7 +509,7 @@ fn out_of_bounds_err<'a>(
)
} else {
format!(
"depth parameter on meta-variable expression `{ty}` \
"depth parameter of meta-variable expression `{ty}` \
must be less than {max}"
)
};
Expand Down
19 changes: 19 additions & 0 deletions tests/ui/macros/rfc-3086-metavar-expr/issue-111905.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#![feature(macro_metavar_expr)]

macro_rules! foo {
($($t:ident)*) => { ${count(t, 4294967296)} };
//~^ ERROR related fragment that refers the `count` meta-variable expression was not found
}

macro_rules! bar {
( $( { $( [ $( ( $( $t:ident )* ) )* ] )* } )* ) => { ${count(t, 4294967296)} }
//~^ ERROR related fragment that refers the `count` meta-variable expression was not found
}

fn test() {
foo!();
bar!( { [] [] } );
}

fn main() {
}
14 changes: 14 additions & 0 deletions tests/ui/macros/rfc-3086-metavar-expr/issue-111905.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
error: related fragment that refers the `count` meta-variable expression was not found
--> $DIR/issue-111905.rs:4:26
|
LL | ($($t:ident)*) => { ${count(t, 4294967296)} };
| ^^^^^^^^^^^^^^^^^^^^^^

error: related fragment that refers the `count` meta-variable expression was not found
--> $DIR/issue-111905.rs:9:60
|
LL | ( $( { $( [ $( ( $( $t:ident )* ) )* ] )* } )* ) => { ${count(t, 4294967296)} }
| ^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 2 previous errors

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ macro_rules! a {
(
${count(foo, 0)},
${count(foo, 10)},
//~^ ERROR depth parameter on meta-variable expression `count` must be less than 4
//~^ ERROR depth parameter of meta-variable expression `count` must be less than 4
)
};
}
Expand All @@ -17,7 +17,7 @@ macro_rules! b {
${ignore(foo)}
${index(0)},
${index(10)},
//~^ ERROR depth parameter on meta-variable expression `index` must be less than 3
//~^ ERROR depth parameter of meta-variable expression `index` must be less than 3
)* )* )*
)
};
Expand All @@ -30,7 +30,7 @@ macro_rules! c {
${ignore(foo)}
${length(0)}
${length(10)}
//~^ ERROR depth parameter on meta-variable expression `length` must be less than 2
//~^ ERROR depth parameter of meta-variable expression `length` must be less than 2
)* )*
)
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
error: depth parameter on meta-variable expression `count` must be less than 4
error: depth parameter of meta-variable expression `count` must be less than 4
--> $DIR/out-of-bounds-arguments.rs:7:14
|
LL | ${count(foo, 10)},
| ^^^^^^^^^^^^^^^^

error: depth parameter on meta-variable expression `index` must be less than 3
error: depth parameter of meta-variable expression `index` must be less than 3
--> $DIR/out-of-bounds-arguments.rs:19:18
|
LL | ${index(10)},
| ^^^^^^^^^^^

error: depth parameter on meta-variable expression `length` must be less than 2
error: depth parameter of meta-variable expression `length` must be less than 2
--> $DIR/out-of-bounds-arguments.rs:32:18
|
LL | ${length(10)}
Expand Down

0 comments on commit 1817bb2

Please sign in to comment.