Skip to content

Commit b71e8cb

Browse files
committed
Auto merge of rust-lang#124987 - workingjubilee:macro-metavar-expr-with-a-shorter-len, r=c410-f3r,joshtriplett,joshtriplett
Rename `${length()}` to `${len()}` Implements the rename suggested in rust-lang#122808 (comment) > I brought this up in the doc PR but it belongs here – `length` should probably be renamed `len` before stabilization. The latter is de facto standard in the standard library, whereas the former is only used in a single unstable API. These metafunctions aren’t library items of course, but should presumably still be consistent with established names. r? `@c410-f3r`
2 parents 1871252 + 6e74155 commit b71e8cb

14 files changed

+163
-148
lines changed

compiler/rustc_expand/src/mbe/metavar_expr.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ pub(crate) enum MetaVarExpr {
2323

2424
/// The length of the repetition at a particular depth, where 0 is the inner-most
2525
/// repetition. The `usize` is the depth.
26-
Length(usize),
26+
Len(usize),
2727
}
2828

2929
impl MetaVarExpr {
@@ -48,13 +48,13 @@ impl MetaVarExpr {
4848
MetaVarExpr::Ignore(parse_ident(&mut iter, psess, ident.span)?)
4949
}
5050
"index" => MetaVarExpr::Index(parse_depth(&mut iter, psess, ident.span)?),
51-
"length" => MetaVarExpr::Length(parse_depth(&mut iter, psess, ident.span)?),
51+
"len" => MetaVarExpr::Len(parse_depth(&mut iter, psess, ident.span)?),
5252
_ => {
5353
let err_msg = "unrecognized meta-variable expression";
5454
let mut err = psess.dcx.struct_span_err(ident.span, err_msg);
5555
err.span_suggestion(
5656
ident.span,
57-
"supported expressions are count, ignore, index and length",
57+
"supported expressions are count, ignore, index and len",
5858
"",
5959
Applicability::MachineApplicable,
6060
);
@@ -68,7 +68,7 @@ impl MetaVarExpr {
6868
pub(crate) fn ident(&self) -> Option<Ident> {
6969
match *self {
7070
MetaVarExpr::Count(ident, _) | MetaVarExpr::Ignore(ident) => Some(ident),
71-
MetaVarExpr::Index(..) | MetaVarExpr::Length(..) => None,
71+
MetaVarExpr::Index(..) | MetaVarExpr::Len(..) => None,
7272
}
7373
}
7474
}
@@ -111,7 +111,7 @@ fn parse_count<'psess>(
111111
Ok(MetaVarExpr::Count(ident, depth))
112112
}
113113

114-
/// Parses the depth used by index(depth) and length(depth).
114+
/// Parses the depth used by index(depth) and len(depth).
115115
fn parse_depth<'psess>(
116116
iter: &mut RefTokenTreeCursor<'_>,
117117
psess: &'psess ParseSess,

compiler/rustc_expand/src/mbe/quoted.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ fn parse_sep_and_kleene_op<'a>(
357357

358358
// `$$` or a meta-variable is the lhs of a macro but shouldn't.
359359
//
360-
// For example, `macro_rules! foo { ( ${length()} ) => {} }`
360+
// For example, `macro_rules! foo { ( ${len()} ) => {} }`
361361
fn span_dollar_dollar_or_metavar_in_the_lhs_err(sess: &Session, token: &Token) {
362362
sess.dcx()
363363
.span_err(token.span, format!("unexpected token: {}", pprust::token_to_string(token)));

compiler/rustc_expand/src/mbe/transcribe.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -685,14 +685,14 @@ fn transcribe_metavar_expr<'a>(
685685
}
686686
None => return Err(out_of_bounds_err(cx, repeats.len(), sp.entire(), "index")),
687687
},
688-
MetaVarExpr::Length(depth) => match repeats.iter().nth_back(depth) {
688+
MetaVarExpr::Len(depth) => match repeats.iter().nth_back(depth) {
689689
Some((_, length)) => {
690690
result.push(TokenTree::token_alone(
691691
TokenKind::lit(token::Integer, sym::integer(*length), None),
692692
visited_span(),
693693
));
694694
}
695-
None => return Err(out_of_bounds_err(cx, repeats.len(), sp.entire(), "length")),
695+
None => return Err(out_of_bounds_err(cx, repeats.len(), sp.entire(), "len")),
696696
},
697697
}
698698
Ok(())

tests/ui/macros/meta-variable-depth-outside-repeat.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
macro_rules! metavar {
44
( $i:expr ) => {
5-
${length(0)}
6-
//~^ ERROR meta-variable expression `length` with depth parameter must be called inside of a macro repetition
5+
${len(0)}
6+
//~^ ERROR meta-variable expression `len` with depth parameter must be called inside of a macro repetition
77
};
88
}
99

Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error: meta-variable expression `length` with depth parameter must be called inside of a macro repetition
1+
error: meta-variable expression `len` with depth parameter must be called inside of a macro repetition
22
--> $DIR/meta-variable-depth-outside-repeat.rs:5:10
33
|
4-
LL | ${length(0)}
5-
| ^^^^^^^^^^^
4+
LL | ${len(0)}
5+
| ^^^^^^^^
66

77
error: aborting due to 1 previous error
88

tests/ui/macros/rfc-3086-metavar-expr/count-and-length-are-distinct.rs

+60-48
Original file line numberDiff line numberDiff line change
@@ -3,60 +3,55 @@
33
#![feature(macro_metavar_expr)]
44

55
fn main() {
6-
macro_rules! one_nested_count_and_length {
6+
macro_rules! one_nested_count_and_len {
77
( $( [ $( $l:literal ),* ] ),* ) => {
88
[
99
// outer-most repetition
1010
$(
1111
// inner-most repetition
1212
$(
13-
${ignore($l)} ${index()}, ${length()},
13+
${ignore($l)} ${index()}, ${len()},
1414
)*
15-
${count($l)}, ${index()}, ${length()},
15+
${count($l)}, ${index()}, ${len()},
1616
)*
1717
${count($l)},
1818
]
1919
};
2020
}
2121
assert_eq!(
22-
one_nested_count_and_length!(["foo"], ["bar", "baz"]),
22+
one_nested_count_and_len!(["foo"], ["bar", "baz"]),
2323
[
2424
// # ["foo"]
2525

2626
// ## inner-most repetition (first iteration)
2727
//
2828
// `index` is 0 because this is the first inner-most iteration.
29-
// `length` is 1 because there is only one inner-most repetition, "foo".
29+
// `len` is 1 because there is only one inner-most repetition, "foo".
3030
0, 1,
31-
3231
// ## outer-most repetition (first iteration)
3332
//
3433
// `count` is 1 because of "foo", i,e, `$l` has only one repetition,
3534
// `index` is 0 because this is the first outer-most iteration.
36-
// `length` is 2 because there are 2 outer-most repetitions, ["foo"] and ["bar", "baz"]
35+
// `len` is 2 because there are 2 outer-most repetitions, ["foo"] and ["bar", "baz"]
3736
1, 0, 2,
38-
3937
// # ["bar", "baz"]
4038

4139
// ## inner-most repetition (first iteration)
4240
//
4341
// `index` is 0 because this is the first inner-most iteration
44-
// `length` is 2 because there are repetitions, "bar" and "baz"
42+
// `len` is 2 because there are repetitions, "bar" and "baz"
4543
0, 2,
46-
4744
// ## inner-most repetition (second iteration)
4845
//
4946
// `index` is 1 because this is the second inner-most iteration
50-
// `length` is 2 because there are repetitions, "bar" and "baz"
47+
// `len` is 2 because there are repetitions, "bar" and "baz"
5148
1, 2,
52-
5349
// ## outer-most repetition (second iteration)
5450
//
5551
// `count` is 2 because of "bar" and "baz", i,e, `$l` has two repetitions,
5652
// `index` is 1 because this is the second outer-most iteration
57-
// `length` is 2 because there are 2 outer-most repetitions, ["foo"] and ["bar", "baz"]
53+
// `len` is 2 because there are 2 outer-most repetitions, ["foo"] and ["bar", "baz"]
5854
2, 1, 2,
59-
6055
// # last count
6156

6257
// Because there are a total of 3 repetitions of `$l`, "foo", "bar" and "baz"
@@ -131,7 +126,6 @@ fn main() {
131126
&[2][..],
132127
// t u v w x y z
133128
&[7][..],
134-
135129
// (a b c) (d e f)
136130
&[6, 2][..],
137131
// (g h) (i j k l m)
@@ -142,15 +136,13 @@ fn main() {
142136
&[5, 3][..],
143137
// (t u v w x y z)
144138
&[7, 1][..],
145-
146139
// [ (a b c) (d e f) ]
147140
// [ (g h) (i j k l m) ]
148141
// [ (n) ]
149142
&[14, 5, 3][..],
150143
// [ (o) (p q) (r s) ]
151144
// [ (t u v w x y z) ]
152145
&[12, 4, 2][..],
153-
154146
// {
155147
// [ (a b c) (d e f) ]
156148
// [ (g h) (i j k l m) ]
@@ -165,43 +157,43 @@ fn main() {
165157
);
166158

167159
// Grouped from the outer-most to the inner-most
168-
macro_rules! three_nested_length {
160+
macro_rules! three_nested_len {
169161
( $( { $( [ $( ( $( $i:ident )* ) )* ] )* } )* ) => {
170162
&[
171163
$( $( $( $(
172164
&[
173-
${ignore($i)} ${length(3)},
174-
${ignore($i)} ${length(2)},
175-
${ignore($i)} ${length(1)},
176-
${ignore($i)} ${length(0)},
165+
${ignore($i)} ${len(3)},
166+
${ignore($i)} ${len(2)},
167+
${ignore($i)} ${len(1)},
168+
${ignore($i)} ${len(0)},
177169
][..],
178170
)* )* )* )*
179171

180172
$( $( $(
181173
&[
182-
${ignore($i)} ${length(2)},
183-
${ignore($i)} ${length(1)},
184-
${ignore($i)} ${length(0)},
174+
${ignore($i)} ${len(2)},
175+
${ignore($i)} ${len(1)},
176+
${ignore($i)} ${len(0)},
185177
][..],
186178
)* )* )*
187179

188180
$( $(
189181
&[
190-
${ignore($i)} ${length(1)},
191-
${ignore($i)} ${length(0)},
182+
${ignore($i)} ${len(1)},
183+
${ignore($i)} ${len(0)},
192184
][..],
193185
)* )*
194186

195187
$(
196188
&[
197-
${ignore($i)} ${length(0)},
189+
${ignore($i)} ${len(0)},
198190
][..],
199191
)*
200192
][..]
201193
}
202194
}
203195
assert_eq!(
204-
three_nested_length!(
196+
three_nested_len!(
205197
{
206198
[ (a b c) (d e f) ]
207199
[ (g h) (i j k l m) ]
@@ -214,45 +206,64 @@ fn main() {
214206
),
215207
&[
216208
// a b c
217-
&[2, 3, 2, 3][..], &[2, 3, 2, 3][..], &[2, 3, 2, 3][..],
209+
&[2, 3, 2, 3][..],
210+
&[2, 3, 2, 3][..],
211+
&[2, 3, 2, 3][..],
218212
// d e f
219-
&[2, 3, 2, 3][..], &[2, 3, 2, 3][..], &[2, 3, 2, 3][..],
213+
&[2, 3, 2, 3][..],
214+
&[2, 3, 2, 3][..],
215+
&[2, 3, 2, 3][..],
220216
// g h
221-
&[2, 3, 2, 2][..], &[2, 3, 2, 2][..],
217+
&[2, 3, 2, 2][..],
218+
&[2, 3, 2, 2][..],
222219
// i j k l m
223-
&[2, 3, 2, 5][..], &[2, 3, 2, 5][..], &[2, 3, 2, 5][..], &[2, 3, 2, 5][..],
220+
&[2, 3, 2, 5][..],
221+
&[2, 3, 2, 5][..],
222+
&[2, 3, 2, 5][..],
223+
&[2, 3, 2, 5][..],
224224
&[2, 3, 2, 5][..],
225225
// n
226226
&[2, 3, 1, 1][..],
227227
// o
228228
&[2, 2, 3, 1][..],
229229
// p q
230-
&[2, 2, 3, 2][..], &[2, 2, 3, 2][..],
230+
&[2, 2, 3, 2][..],
231+
&[2, 2, 3, 2][..],
231232
// r s
232-
&[2, 2, 3, 2][..], &[2, 2, 3, 2][..],
233+
&[2, 2, 3, 2][..],
234+
&[2, 2, 3, 2][..],
233235
// t u v w x y z
234-
&[2, 2, 1, 7][..], &[2, 2, 1, 7][..], &[2, 2, 1, 7][..], &[2, 2, 1, 7][..],
235-
&[2, 2, 1, 7][..], &[2, 2, 1, 7][..], &[2, 2, 1, 7][..],
236-
236+
&[2, 2, 1, 7][..],
237+
&[2, 2, 1, 7][..],
238+
&[2, 2, 1, 7][..],
239+
&[2, 2, 1, 7][..],
240+
&[2, 2, 1, 7][..],
241+
&[2, 2, 1, 7][..],
242+
&[2, 2, 1, 7][..],
237243
// (a b c) (d e f)
238-
&[2, 3, 2][..], &[2, 3, 2][..],
244+
&[2, 3, 2][..],
245+
&[2, 3, 2][..],
239246
// (g h) (i j k l m)
240-
&[2, 3, 2][..], &[2, 3, 2][..],
247+
&[2, 3, 2][..],
248+
&[2, 3, 2][..],
241249
// (n)
242250
&[2, 3, 1][..],
243251
// (o) (p q) (r s)
244-
&[2, 2, 3][..], &[2, 2, 3][..], &[2, 2, 3][..],
252+
&[2, 2, 3][..],
253+
&[2, 2, 3][..],
254+
&[2, 2, 3][..],
245255
// (t u v w x y z)
246256
&[2, 2, 1][..],
247-
248257
// [ (a b c) (d e f) ]
249258
// [ (g h) (i j k l m) ]
250259
// [ (n) ]
251-
&[2, 3][..], &[2, 3][..], &[2, 3,][..],
260+
&[2, 3][..],
261+
&[2, 3][..],
262+
&[2, 3,][..],
252263
// [ (o) (p q) (r s) ]
253264
// [ (t u v w x y z) ]
254-
&[2, 2][..], &[2, 2][..],
255-
265+
&[2, 2][..],
266+
&[2, 2][..],
256267
// {
257268
// [ (a b c) (d e f) ]
258269
// [ (g h) (i j k l m) ]
@@ -262,10 +273,11 @@ fn main() {
262273
// [ (o) (p q) (r s) ]
263274
// [ (t u v w x y z) ]
264275
// }
265-
&[2][..], &[2][..]
276+
&[2][..],
277+
&[2][..]
266278
][..]
267279
);
268280

269-
// It is possible to say, to some degree, that count is an "amalgamation" of length (see
270-
// each length line result and compare them with the count results)
281+
// It is possible to say, to some degree, that count is an "amalgamation" of len (see
282+
// each len line result and compare them with the count results)
271283
}

tests/ui/macros/rfc-3086-metavar-expr/feature-gate-macro_metavar_expr.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,17 @@ macro_rules! count_depth_limits {
3737
};
3838
}
3939

40-
/// Produce (index, length) pairs for literals in a macro repetition.
40+
/// Produce (index, len) pairs for literals in a macro repetition.
4141
/// The literal is not included in the output, so this macro uses the
4242
/// `ignore` meta-variable expression to create a non-expanding
4343
/// repetition binding.
4444
macro_rules! enumerate_literals {
4545
( $( ($l:stmt) ),* ) => {
46-
[$( ${ignore($l)} (${index()}, ${length()}) ),*]
46+
[$( ${ignore($l)} (${index()}, ${len()}) ),*]
4747
};
4848
}
4949

50-
/// Produce index and length tuples for literals in a 2-dimensional
50+
/// Produce index and len tuples for literals in a 2-dimensional
5151
/// macro repetition.
5252
macro_rules! enumerate_literals_2 {
5353
( $( [ $( ($l:literal) ),* ] ),* ) => {
@@ -56,9 +56,9 @@ macro_rules! enumerate_literals_2 {
5656
$(
5757
(
5858
${index(1)},
59-
${length(1)},
59+
${len(1)},
6060
${index(0)},
61-
${length(0)},
61+
${len(0)},
6262
$l
6363
),
6464
)*
@@ -134,7 +134,6 @@ fn main() {
134134
(0, 2, 0, 3, "foo"),
135135
(0, 2, 1, 3, "bar"),
136136
(0, 2, 2, 3, "baz"),
137-
138137
(1, 2, 0, 4, "qux"),
139138
(1, 2, 1, 4, "quux"),
140139
(1, 2, 2, 4, "quuz"),

0 commit comments

Comments
 (0)