Skip to content

Commit 82d9ae9

Browse files
authored
Rollup merge of #99355 - compiler-errors:macro-metavar-less-than-zero, r=petrochenkov
better error for bad depth parameter on macro metavar expr Fixes #99060
2 parents 43f6366 + dd109c8 commit 82d9ae9

File tree

5 files changed

+39
-9
lines changed

5 files changed

+39
-9
lines changed

compiler/rustc_expand/src/mbe/transcribe.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,18 @@ fn out_of_bounds_err<'a>(
512512
span: Span,
513513
ty: &str,
514514
) -> DiagnosticBuilder<'a, ErrorGuaranteed> {
515-
cx.struct_span_err(span, &format!("{ty} depth must be less than {max}"))
515+
let msg = if max == 0 {
516+
format!(
517+
"meta-variable expression `{ty}` with depth parameter \
518+
must be called inside of a macro repetition"
519+
)
520+
} else {
521+
format!(
522+
"depth parameter on meta-variable expression `{ty}` \
523+
must be less than {max}"
524+
)
525+
};
526+
cx.struct_span_err(span, &msg)
516527
}
517528

518529
fn transcribe_metavar_expr<'a>(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#![feature(macro_metavar_expr)]
2+
3+
macro_rules! metavar {
4+
( $i:expr ) => {
5+
${length(0)}
6+
//~^ ERROR meta-variable expression `length` with depth parameter must be called inside of a macro repetition
7+
};
8+
}
9+
10+
const _: i32 = metavar!(0);
11+
12+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: meta-variable expression `length` with depth parameter must be called inside of a macro repetition
2+
--> $DIR/meta-variable-depth-outside-repeat.rs:5:10
3+
|
4+
LL | ${length(0)}
5+
| ^^^^^^^^^^^
6+
7+
error: aborting due to previous error
8+

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

+4-5
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 count depth must be less than 4
8+
//~^ ERROR depth parameter on 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 index depth must be less than 3
20+
//~^ ERROR depth parameter on meta-variable expression `index` must be less than 3
2121
)* )* )*
2222
)
2323
};
@@ -30,15 +30,14 @@ macro_rules! c {
3030
${ignore(foo)}
3131
${length(0)}
3232
${length(10)}
33-
//~^ ERROR length depth must be less than 2
33+
//~^ ERROR depth parameter on meta-variable expression `length` must be less than 2
3434
)* )*
3535
)
3636
};
3737
}
3838

39-
4039
fn main() {
4140
a!( { [ (a) ] [ (b c) ] } );
4241
b!( { [ a b ] } );
43-
c!( { a } );
42+
c!({ a });
4443
}

src/test/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: count depth must be less than 4
1+
error: depth parameter on 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: index depth must be less than 3
7+
error: depth parameter on 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: length depth must be less than 2
13+
error: depth parameter on 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)