Skip to content

Commit 33e81da

Browse files
committed
Auto merge of rust-lang#111923 - c410-f3r:impl, r=oli-obk
[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`.
2 parents e6e931d + 82ba5fb commit 33e81da

File tree

8 files changed

+56
-10
lines changed

8 files changed

+56
-10
lines changed

compiler/rustc_expand/messages.ftl

+3
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ expand_meta_var_dif_seq_matchers = {$msg}
8080
expand_meta_var_expr_unrecognized_var =
8181
variable `{$key}` is not recognized in meta-variable expression
8282
83+
expand_missing_count_fragment =
84+
related fragment that refers the `count` meta-variable expression was not found
85+
8386
expand_module_circular =
8487
circular modules: {$modules}
8588

compiler/rustc_expand/src/errors.rs

+7
Original file line numberDiff line numberDiff line change
@@ -408,3 +408,10 @@ pub struct DuplicateMatcherBinding {
408408
#[label(expand_label2)]
409409
pub prev: Span,
410410
}
411+
412+
#[derive(Diagnostic)]
413+
#[diag(expand_missing_count_fragment)]
414+
pub(crate) struct MissingCountFragment {
415+
#[primary_span]
416+
pub span: Span,
417+
}

compiler/rustc_expand/src/mbe/metavar_expr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use rustc_span::Span;
1111
#[derive(Debug, Clone, PartialEq, Encodable, Decodable)]
1212
pub(crate) enum MetaVarExpr {
1313
/// The number of repetitions of an identifier, optionally limited to a number
14-
/// of outer-most repetition depths. If the depth limit is `None` then the depth is unlimited.
14+
/// of outer-most repetition depths.
1515
Count(Ident, Option<usize>),
1616

1717
/// Ignore a meta-variable for repetition without expansion.

compiler/rustc_expand/src/mbe/transcribe.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::base::ExtCtxt;
22
use crate::errors::{
3-
CountRepetitionMisplaced, MetaVarExprUnrecognizedVar, MetaVarsDifSeqMatchers, MustRepeatOnce,
4-
NoSyntaxVarsExprRepeat, VarStillRepeating,
3+
CountRepetitionMisplaced, MetaVarExprUnrecognizedVar, MetaVarsDifSeqMatchers,
4+
MissingCountFragment, MustRepeatOnce, NoSyntaxVarsExprRepeat, VarStillRepeating,
55
};
66
use crate::mbe::macro_parser::{MatchedNonterminal, MatchedSeq, MatchedTokenTree, NamedMatch};
77
use crate::mbe::{self, MetaVarExpr};
@@ -447,6 +447,9 @@ fn count_repetitions<'a>(
447447
}
448448
}
449449
MatchedSeq(named_matches) => {
450+
if named_matches.is_empty() {
451+
return Err(cx.create_err(MissingCountFragment { span: sp.entire() }));
452+
}
450453
let new_declared_lhs_depth = declared_lhs_depth + 1;
451454
match depth_opt {
452455
None => named_matches
@@ -505,7 +508,7 @@ fn out_of_bounds_err<'a>(
505508
)
506509
} else {
507510
format!(
508-
"depth parameter on meta-variable expression `{ty}` \
511+
"depth parameter of meta-variable expression `{ty}` \
509512
must be less than {max}"
510513
)
511514
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#![feature(macro_metavar_expr)]
2+
3+
macro_rules! foo {
4+
($($t:ident)*) => { ${count(t, 4294967296)} };
5+
//~^ ERROR related fragment that refers the `count` meta-variable expression was not found
6+
}
7+
8+
macro_rules! bar {
9+
( $( { $( [ $( ( $( $t:ident )* ) )* ] )* } )* ) => { ${count(t, 4294967296)} }
10+
//~^ ERROR related fragment that refers the `count` meta-variable expression was not found
11+
}
12+
13+
fn test() {
14+
foo!();
15+
bar!( { [] [] } );
16+
}
17+
18+
fn main() {
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
error: related fragment that refers the `count` meta-variable expression was not found
2+
--> $DIR/issue-111905.rs:4:26
3+
|
4+
LL | ($($t:ident)*) => { ${count(t, 4294967296)} };
5+
| ^^^^^^^^^^^^^^^^^^^^^^
6+
7+
error: related fragment that refers the `count` meta-variable expression was not found
8+
--> $DIR/issue-111905.rs:9:60
9+
|
10+
LL | ( $( { $( [ $( ( $( $t:ident )* ) )* ] )* } )* ) => { ${count(t, 4294967296)} }
11+
| ^^^^^^^^^^^^^^^^^^^^^^
12+
13+
error: aborting due to 2 previous errors
14+

tests/ui/macros/rfc-3086-metavar-expr/out-of-bounds-arguments.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ macro_rules! a {
55
(
66
${count(foo, 0)},
77
${count(foo, 10)},
8-
//~^ ERROR depth parameter on meta-variable expression `count` must be less than 4
8+
//~^ ERROR depth parameter of meta-variable expression `count` must be less than 4
99
)
1010
};
1111
}
@@ -17,7 +17,7 @@ macro_rules! b {
1717
${ignore(foo)}
1818
${index(0)},
1919
${index(10)},
20-
//~^ ERROR depth parameter on meta-variable expression `index` must be less than 3
20+
//~^ ERROR depth parameter of meta-variable expression `index` must be less than 3
2121
)* )* )*
2222
)
2323
};
@@ -30,7 +30,7 @@ macro_rules! c {
3030
${ignore(foo)}
3131
${length(0)}
3232
${length(10)}
33-
//~^ ERROR depth parameter on meta-variable expression `length` must be less than 2
33+
//~^ ERROR depth parameter of meta-variable expression `length` must be less than 2
3434
)* )*
3535
)
3636
};

tests/ui/macros/rfc-3086-metavar-expr/out-of-bounds-arguments.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
error: depth parameter on meta-variable expression `count` must be less than 4
1+
error: depth parameter of meta-variable expression `count` must be less than 4
22
--> $DIR/out-of-bounds-arguments.rs:7:14
33
|
44
LL | ${count(foo, 10)},
55
| ^^^^^^^^^^^^^^^^
66

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

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

0 commit comments

Comments
 (0)