Skip to content

Commit

Permalink
Auto merge of #124987 - workingjubilee:macro-metavar-expr-with-a-shor…
Browse files Browse the repository at this point in the history
…ter-len, r=c410-f3r,joshtriplett,joshtriplett

Rename `${length()}` to `${len()}`

Implements the rename suggested in #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`
  • Loading branch information
bors committed May 16, 2024
2 parents 1871252 + 6e74155 commit b71e8cb
Show file tree
Hide file tree
Showing 14 changed files with 163 additions and 148 deletions.
10 changes: 5 additions & 5 deletions compiler/rustc_expand/src/mbe/metavar_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub(crate) enum MetaVarExpr {

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

impl MetaVarExpr {
Expand All @@ -48,13 +48,13 @@ impl MetaVarExpr {
MetaVarExpr::Ignore(parse_ident(&mut iter, psess, ident.span)?)
}
"index" => MetaVarExpr::Index(parse_depth(&mut iter, psess, ident.span)?),
"length" => MetaVarExpr::Length(parse_depth(&mut iter, psess, ident.span)?),
"len" => MetaVarExpr::Len(parse_depth(&mut iter, psess, ident.span)?),
_ => {
let err_msg = "unrecognized meta-variable expression";
let mut err = psess.dcx.struct_span_err(ident.span, err_msg);
err.span_suggestion(
ident.span,
"supported expressions are count, ignore, index and length",
"supported expressions are count, ignore, index and len",
"",
Applicability::MachineApplicable,
);
Expand All @@ -68,7 +68,7 @@ impl MetaVarExpr {
pub(crate) fn ident(&self) -> Option<Ident> {
match *self {
MetaVarExpr::Count(ident, _) | MetaVarExpr::Ignore(ident) => Some(ident),
MetaVarExpr::Index(..) | MetaVarExpr::Length(..) => None,
MetaVarExpr::Index(..) | MetaVarExpr::Len(..) => None,
}
}
}
Expand Down Expand Up @@ -111,7 +111,7 @@ fn parse_count<'psess>(
Ok(MetaVarExpr::Count(ident, depth))
}

/// Parses the depth used by index(depth) and length(depth).
/// Parses the depth used by index(depth) and len(depth).
fn parse_depth<'psess>(
iter: &mut RefTokenTreeCursor<'_>,
psess: &'psess ParseSess,
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_expand/src/mbe/quoted.rs
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ fn parse_sep_and_kleene_op<'a>(

// `$$` or a meta-variable is the lhs of a macro but shouldn't.
//
// For example, `macro_rules! foo { ( ${length()} ) => {} }`
// For example, `macro_rules! foo { ( ${len()} ) => {} }`
fn span_dollar_dollar_or_metavar_in_the_lhs_err(sess: &Session, token: &Token) {
sess.dcx()
.span_err(token.span, format!("unexpected token: {}", pprust::token_to_string(token)));
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_expand/src/mbe/transcribe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -685,14 +685,14 @@ fn transcribe_metavar_expr<'a>(
}
None => return Err(out_of_bounds_err(cx, repeats.len(), sp.entire(), "index")),
},
MetaVarExpr::Length(depth) => match repeats.iter().nth_back(depth) {
MetaVarExpr::Len(depth) => match repeats.iter().nth_back(depth) {
Some((_, length)) => {
result.push(TokenTree::token_alone(
TokenKind::lit(token::Integer, sym::integer(*length), None),
visited_span(),
));
}
None => return Err(out_of_bounds_err(cx, repeats.len(), sp.entire(), "length")),
None => return Err(out_of_bounds_err(cx, repeats.len(), sp.entire(), "len")),
},
}
Ok(())
Expand Down
4 changes: 2 additions & 2 deletions tests/ui/macros/meta-variable-depth-outside-repeat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

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

Expand Down
6 changes: 3 additions & 3 deletions tests/ui/macros/meta-variable-depth-outside-repeat.stderr
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
error: meta-variable expression `length` with depth parameter must be called inside of a macro repetition
error: meta-variable expression `len` with depth parameter must be called inside of a macro repetition
--> $DIR/meta-variable-depth-outside-repeat.rs:5:10
|
LL | ${length(0)}
| ^^^^^^^^^^^
LL | ${len(0)}
| ^^^^^^^^

error: aborting due to 1 previous error

108 changes: 60 additions & 48 deletions tests/ui/macros/rfc-3086-metavar-expr/count-and-length-are-distinct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,60 +3,55 @@
#![feature(macro_metavar_expr)]

fn main() {
macro_rules! one_nested_count_and_length {
macro_rules! one_nested_count_and_len {
( $( [ $( $l:literal ),* ] ),* ) => {
[
// outer-most repetition
$(
// inner-most repetition
$(
${ignore($l)} ${index()}, ${length()},
${ignore($l)} ${index()}, ${len()},
)*
${count($l)}, ${index()}, ${length()},
${count($l)}, ${index()}, ${len()},
)*
${count($l)},
]
};
}
assert_eq!(
one_nested_count_and_length!(["foo"], ["bar", "baz"]),
one_nested_count_and_len!(["foo"], ["bar", "baz"]),
[
// # ["foo"]

// ## inner-most repetition (first iteration)
//
// `index` is 0 because this is the first inner-most iteration.
// `length` is 1 because there is only one inner-most repetition, "foo".
// `len` is 1 because there is only one inner-most repetition, "foo".
0, 1,

// ## outer-most repetition (first iteration)
//
// `count` is 1 because of "foo", i,e, `$l` has only one repetition,
// `index` is 0 because this is the first outer-most iteration.
// `length` is 2 because there are 2 outer-most repetitions, ["foo"] and ["bar", "baz"]
// `len` is 2 because there are 2 outer-most repetitions, ["foo"] and ["bar", "baz"]
1, 0, 2,

// # ["bar", "baz"]

// ## inner-most repetition (first iteration)
//
// `index` is 0 because this is the first inner-most iteration
// `length` is 2 because there are repetitions, "bar" and "baz"
// `len` is 2 because there are repetitions, "bar" and "baz"
0, 2,

// ## inner-most repetition (second iteration)
//
// `index` is 1 because this is the second inner-most iteration
// `length` is 2 because there are repetitions, "bar" and "baz"
// `len` is 2 because there are repetitions, "bar" and "baz"
1, 2,

// ## outer-most repetition (second iteration)
//
// `count` is 2 because of "bar" and "baz", i,e, `$l` has two repetitions,
// `index` is 1 because this is the second outer-most iteration
// `length` is 2 because there are 2 outer-most repetitions, ["foo"] and ["bar", "baz"]
// `len` is 2 because there are 2 outer-most repetitions, ["foo"] and ["bar", "baz"]
2, 1, 2,

// # last count

// Because there are a total of 3 repetitions of `$l`, "foo", "bar" and "baz"
Expand Down Expand Up @@ -131,7 +126,6 @@ fn main() {
&[2][..],
// t u v w x y z
&[7][..],

// (a b c) (d e f)
&[6, 2][..],
// (g h) (i j k l m)
Expand All @@ -142,15 +136,13 @@ fn main() {
&[5, 3][..],
// (t u v w x y z)
&[7, 1][..],

// [ (a b c) (d e f) ]
// [ (g h) (i j k l m) ]
// [ (n) ]
&[14, 5, 3][..],
// [ (o) (p q) (r s) ]
// [ (t u v w x y z) ]
&[12, 4, 2][..],

// {
// [ (a b c) (d e f) ]
// [ (g h) (i j k l m) ]
Expand All @@ -165,43 +157,43 @@ fn main() {
);

// Grouped from the outer-most to the inner-most
macro_rules! three_nested_length {
macro_rules! three_nested_len {
( $( { $( [ $( ( $( $i:ident )* ) )* ] )* } )* ) => {
&[
$( $( $( $(
&[
${ignore($i)} ${length(3)},
${ignore($i)} ${length(2)},
${ignore($i)} ${length(1)},
${ignore($i)} ${length(0)},
${ignore($i)} ${len(3)},
${ignore($i)} ${len(2)},
${ignore($i)} ${len(1)},
${ignore($i)} ${len(0)},
][..],
)* )* )* )*

$( $( $(
&[
${ignore($i)} ${length(2)},
${ignore($i)} ${length(1)},
${ignore($i)} ${length(0)},
${ignore($i)} ${len(2)},
${ignore($i)} ${len(1)},
${ignore($i)} ${len(0)},
][..],
)* )* )*

$( $(
&[
${ignore($i)} ${length(1)},
${ignore($i)} ${length(0)},
${ignore($i)} ${len(1)},
${ignore($i)} ${len(0)},
][..],
)* )*

$(
&[
${ignore($i)} ${length(0)},
${ignore($i)} ${len(0)},
][..],
)*
][..]
}
}
assert_eq!(
three_nested_length!(
three_nested_len!(
{
[ (a b c) (d e f) ]
[ (g h) (i j k l m) ]
Expand All @@ -214,45 +206,64 @@ fn main() {
),
&[
// a b c
&[2, 3, 2, 3][..], &[2, 3, 2, 3][..], &[2, 3, 2, 3][..],
&[2, 3, 2, 3][..],
&[2, 3, 2, 3][..],
&[2, 3, 2, 3][..],
// d e f
&[2, 3, 2, 3][..], &[2, 3, 2, 3][..], &[2, 3, 2, 3][..],
&[2, 3, 2, 3][..],
&[2, 3, 2, 3][..],
&[2, 3, 2, 3][..],
// g h
&[2, 3, 2, 2][..], &[2, 3, 2, 2][..],
&[2, 3, 2, 2][..],
&[2, 3, 2, 2][..],
// i j k l m
&[2, 3, 2, 5][..], &[2, 3, 2, 5][..], &[2, 3, 2, 5][..], &[2, 3, 2, 5][..],
&[2, 3, 2, 5][..],
&[2, 3, 2, 5][..],
&[2, 3, 2, 5][..],
&[2, 3, 2, 5][..],
&[2, 3, 2, 5][..],
// n
&[2, 3, 1, 1][..],
// o
&[2, 2, 3, 1][..],
// p q
&[2, 2, 3, 2][..], &[2, 2, 3, 2][..],
&[2, 2, 3, 2][..],
&[2, 2, 3, 2][..],
// r s
&[2, 2, 3, 2][..], &[2, 2, 3, 2][..],
&[2, 2, 3, 2][..],
&[2, 2, 3, 2][..],
// t u v w x y z
&[2, 2, 1, 7][..], &[2, 2, 1, 7][..], &[2, 2, 1, 7][..], &[2, 2, 1, 7][..],
&[2, 2, 1, 7][..], &[2, 2, 1, 7][..], &[2, 2, 1, 7][..],

&[2, 2, 1, 7][..],
&[2, 2, 1, 7][..],
&[2, 2, 1, 7][..],
&[2, 2, 1, 7][..],
&[2, 2, 1, 7][..],
&[2, 2, 1, 7][..],
&[2, 2, 1, 7][..],
// (a b c) (d e f)
&[2, 3, 2][..], &[2, 3, 2][..],
&[2, 3, 2][..],
&[2, 3, 2][..],
// (g h) (i j k l m)
&[2, 3, 2][..], &[2, 3, 2][..],
&[2, 3, 2][..],
&[2, 3, 2][..],
// (n)
&[2, 3, 1][..],
// (o) (p q) (r s)
&[2, 2, 3][..], &[2, 2, 3][..], &[2, 2, 3][..],
&[2, 2, 3][..],
&[2, 2, 3][..],
&[2, 2, 3][..],
// (t u v w x y z)
&[2, 2, 1][..],

// [ (a b c) (d e f) ]
// [ (g h) (i j k l m) ]
// [ (n) ]
&[2, 3][..], &[2, 3][..], &[2, 3,][..],
&[2, 3][..],
&[2, 3][..],
&[2, 3,][..],
// [ (o) (p q) (r s) ]
// [ (t u v w x y z) ]
&[2, 2][..], &[2, 2][..],

&[2, 2][..],
&[2, 2][..],
// {
// [ (a b c) (d e f) ]
// [ (g h) (i j k l m) ]
Expand All @@ -262,10 +273,11 @@ fn main() {
// [ (o) (p q) (r s) ]
// [ (t u v w x y z) ]
// }
&[2][..], &[2][..]
&[2][..],
&[2][..]
][..]
);

// It is possible to say, to some degree, that count is an "amalgamation" of length (see
// each length line result and compare them with the count results)
// It is possible to say, to some degree, that count is an "amalgamation" of len (see
// each len line result and compare them with the count results)
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,17 @@ macro_rules! count_depth_limits {
};
}

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

/// Produce index and length tuples for literals in a 2-dimensional
/// Produce index and len tuples for literals in a 2-dimensional
/// macro repetition.
macro_rules! enumerate_literals_2 {
( $( [ $( ($l:literal) ),* ] ),* ) => {
Expand All @@ -56,9 +56,9 @@ macro_rules! enumerate_literals_2 {
$(
(
${index(1)},
${length(1)},
${len(1)},
${index(0)},
${length(0)},
${len(0)},
$l
),
)*
Expand Down Expand Up @@ -134,7 +134,6 @@ fn main() {
(0, 2, 0, 3, "foo"),
(0, 2, 1, 3, "bar"),
(0, 2, 2, 3, "baz"),

(1, 2, 0, 4, "qux"),
(1, 2, 1, 4, "quux"),
(1, 2, 2, 4, "quuz"),
Expand Down
Loading

0 comments on commit b71e8cb

Please sign in to comment.