Auto merge of #124987 - workingjubilee:macro-metavar-expr-with-a-shorter-len, r=c410-f3r,joshtriplett,joshtriplett

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

Implements the rename suggested in https://github.com/rust-lang/rust/pull/122808#issuecomment-2047722187
> 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`
This commit is contained in:
bors 2024-05-16 00:26:20 +00:00
commit b71e8cbaf2
14 changed files with 163 additions and 148 deletions

View file

@ -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 {
@ -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,
);
@ -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,
}
}
}
@ -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,

View file

@ -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)));

View file

@ -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(())