Skip to content

Commit 5e799b2

Browse files
authored
Rollup merge of rust-lang#133589 - voidc:remove-array-len, r=boxyuwu
Remove `hir::ArrayLen` This refactoring removes `hir::ArrayLen`, replacing it with `hir::ConstArg`. To represent inferred array lengths (previously `hir::ArrayLen::Infer`), a new variant `ConstArgKind::Infer` is added. r? `@BoxyUwU`
2 parents 1e88cf4 + a6a6936 commit 5e799b2

File tree

6 files changed

+21
-41
lines changed

6 files changed

+21
-41
lines changed

clippy_lints/src/large_stack_arrays.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use clippy_utils::diagnostics::span_lint_and_then;
55
use clippy_utils::is_from_proc_macro;
66
use clippy_utils::macros::macro_backtrace;
77
use clippy_utils::source::snippet;
8-
use rustc_hir::{ArrayLen, Expr, ExprKind, Item, ItemKind, Node};
8+
use rustc_hir::{Expr, ExprKind, Item, ItemKind, Node};
99
use rustc_lint::{LateContext, LateLintPass};
1010
use rustc_middle::ty::layout::LayoutOf;
1111
use rustc_middle::ty::{self, ConstKind};
@@ -118,13 +118,13 @@ impl<'tcx> LateLintPass<'tcx> for LargeStackArrays {
118118

119119
/// Only giving help messages if the expr does not contains macro expanded codes.
120120
fn might_be_expanded<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'tcx>) -> bool {
121-
/// Check if the span of `ArrayLen` of a repeat expression is within the expr's span,
121+
/// Check if the span of `ConstArg` of a repeat expression is within the expr's span,
122122
/// if not, meaning this repeat expr is definitely from some proc-macro.
123123
///
124124
/// This is a fail-safe to a case where even the `is_from_proc_macro` is unable to determain the
125125
/// correct result.
126126
fn repeat_expr_might_be_expanded(expr: &Expr<'_>) -> bool {
127-
let ExprKind::Repeat(_, ArrayLen::Body(len_ct)) = expr.kind else {
127+
let ExprKind::Repeat(_, len_ct) = expr.kind else {
128128
return false;
129129
};
130130
!expr.span.contains(len_ct.span())

clippy_lints/src/utils/author.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use rustc_ast::LitIntType;
33
use rustc_ast::ast::{LitFloatType, LitKind};
44
use rustc_data_structures::fx::FxHashMap;
55
use rustc_hir::{
6-
self as hir, ArrayLen, BindingMode, CaptureBy, Closure, ClosureKind, ConstArg, ConstArgKind, CoroutineKind,
6+
self as hir, BindingMode, CaptureBy, Closure, ClosureKind, ConstArg, ConstArgKind, CoroutineKind,
77
ExprKind, FnRetTy, HirId, Lit, PatKind, QPath, StmtKind, TyKind,
88
};
99
use rustc_lint::{LateContext, LateLintPass, LintContext};
@@ -278,6 +278,7 @@ impl<'a, 'tcx> PrintVisitor<'a, 'tcx> {
278278
chain!(self, "let ConstArgKind::Anon({anon_const}) = {const_arg}.kind");
279279
self.body(field!(anon_const.body));
280280
},
281+
ConstArgKind::Infer(..) => chain!(self, "let ConstArgKind::Infer(..) = {const_arg}.kind"),
281282
}
282283
}
283284

@@ -611,14 +612,7 @@ impl<'a, 'tcx> PrintVisitor<'a, 'tcx> {
611612
bind!(self, value, length);
612613
kind!("Repeat({value}, {length})");
613614
self.expr(value);
614-
match length.value {
615-
ArrayLen::Infer(..) => chain!(self, "let ArrayLen::Infer(..) = length"),
616-
ArrayLen::Body(const_arg) => {
617-
bind!(self, const_arg);
618-
chain!(self, "let ArrayLen::Body({const_arg}) = {length}");
619-
self.const_arg(const_arg);
620-
},
621-
}
615+
self.const_arg(length);
622616
},
623617
ExprKind::Err(_) => kind!("Err(_)"),
624618
ExprKind::DropTemps(expr) => {

clippy_lints/src/zero_repeat_side_effects.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use clippy_utils::visitors::for_each_expr_without_closures;
55
use rustc_ast::LitKind;
66
use rustc_data_structures::packed::Pu128;
77
use rustc_errors::Applicability;
8-
use rustc_hir::{ArrayLen, ConstArgKind, ExprKind, Node};
8+
use rustc_hir::{ConstArgKind, ExprKind, Node};
99
use rustc_lint::{LateContext, LateLintPass};
1010
use rustc_middle::ty::Ty;
1111
use rustc_session::declare_lint_pass;
@@ -60,8 +60,7 @@ impl LateLintPass<'_> for ZeroRepeatSideEffects {
6060
// doesn't seem as confusing as `[f(); 0]`. It would also have false positives when eg.
6161
// the const item depends on `#[cfg]s` and has different values in different compilation
6262
// sessions).
63-
else if let ExprKind::Repeat(inner_expr, length) = expr.kind
64-
&& let ArrayLen::Body(const_arg) = length
63+
else if let ExprKind::Repeat(inner_expr, const_arg) = expr.kind
6564
&& let ConstArgKind::Anon(anon_const) = const_arg.kind
6665
&& let length_expr = hir_map.body(anon_const.body).value
6766
&& !length_expr.span.from_expansion()

clippy_utils/src/hir_utils.rs

Lines changed: 9 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc_data_structures::fx::FxHasher;
77
use rustc_hir::MatchSource::TryDesugar;
88
use rustc_hir::def::{DefKind, Res};
99
use rustc_hir::{
10-
ArrayLen, AssocItemConstraint, BinOpKind, BindingMode, Block, BodyId, Closure, ConstArg, ConstArgKind, Expr,
10+
AssocItemConstraint, BinOpKind, BindingMode, Block, BodyId, Closure, ConstArg, ConstArgKind, Expr,
1111
ExprField, ExprKind, FnRetTy, GenericArg, GenericArgs, HirId, HirIdMap, InlineAsmOperand, LetExpr, Lifetime,
1212
LifetimeName, Pat, PatField, PatKind, Path, PathSegment, PrimTy, QPath, Stmt, StmtKind, TraitBoundModifiers, Ty,
1313
TyKind,
@@ -266,14 +266,6 @@ impl HirEqInterExpr<'_, '_, '_> {
266266
})
267267
}
268268

269-
pub fn eq_array_length(&mut self, left: ArrayLen<'_>, right: ArrayLen<'_>) -> bool {
270-
match (left, right) {
271-
(ArrayLen::Infer(..), ArrayLen::Infer(..)) => true,
272-
(ArrayLen::Body(l_ct), ArrayLen::Body(r_ct)) => self.eq_const_arg(l_ct, r_ct),
273-
(_, _) => false,
274-
}
275-
}
276-
277269
pub fn eq_body(&mut self, left: BodyId, right: BodyId) -> bool {
278270
// swap out TypeckResults when hashing a body
279271
let old_maybe_typeck_results = self.inner.maybe_typeck_results.replace((
@@ -383,7 +375,7 @@ impl HirEqInterExpr<'_, '_, '_> {
383375
},
384376
(ExprKind::Path(l), ExprKind::Path(r)) => self.eq_qpath(l, r),
385377
(&ExprKind::Repeat(le, ll), &ExprKind::Repeat(re, rl)) => {
386-
self.eq_expr(le, re) && self.eq_array_length(ll, rl)
378+
self.eq_expr(le, re) && self.eq_const_arg(ll, rl)
387379
},
388380
(ExprKind::Ret(l), ExprKind::Ret(r)) => both(l.as_ref(), r.as_ref(), |l, r| self.eq_expr(l, r)),
389381
(&ExprKind::Struct(l_path, lf, ref lo), &ExprKind::Struct(r_path, rf, ref ro)) => {
@@ -469,8 +461,10 @@ impl HirEqInterExpr<'_, '_, '_> {
469461
match (&left.kind, &right.kind) {
470462
(ConstArgKind::Path(l_p), ConstArgKind::Path(r_p)) => self.eq_qpath(l_p, r_p),
471463
(ConstArgKind::Anon(l_an), ConstArgKind::Anon(r_an)) => self.eq_body(l_an.body, r_an.body),
464+
(ConstArgKind::Infer(..), ConstArgKind::Infer(..)) => true,
472465
// Use explicit match for now since ConstArg is undergoing flux.
473-
(ConstArgKind::Path(..), ConstArgKind::Anon(..)) | (ConstArgKind::Anon(..), ConstArgKind::Path(..)) => {
466+
(ConstArgKind::Path(..), ConstArgKind::Anon(..)) | (ConstArgKind::Anon(..), ConstArgKind::Path(..))
467+
| (ConstArgKind::Infer(..), _) | (_, ConstArgKind::Infer(..)) => {
474468
false
475469
},
476470
}
@@ -589,7 +583,7 @@ impl HirEqInterExpr<'_, '_, '_> {
589583
pub fn eq_ty(&mut self, left: &Ty<'_>, right: &Ty<'_>) -> bool {
590584
match (&left.kind, &right.kind) {
591585
(&TyKind::Slice(l_vec), &TyKind::Slice(r_vec)) => self.eq_ty(l_vec, r_vec),
592-
(&TyKind::Array(lt, ll), &TyKind::Array(rt, rl)) => self.eq_ty(lt, rt) && self.eq_array_length(ll, rl),
586+
(&TyKind::Array(lt, ll), &TyKind::Array(rt, rl)) => self.eq_ty(lt, rt) && self.eq_const_arg(ll, rl),
593587
(TyKind::Ptr(l_mut), TyKind::Ptr(r_mut)) => l_mut.mutbl == r_mut.mutbl && self.eq_ty(l_mut.ty, r_mut.ty),
594588
(TyKind::Ref(_, l_rmut), TyKind::Ref(_, r_rmut)) => {
595589
l_rmut.mutbl == r_rmut.mutbl && self.eq_ty(l_rmut.ty, r_rmut.ty)
@@ -1008,7 +1002,7 @@ impl<'a, 'tcx> SpanlessHash<'a, 'tcx> {
10081002
},
10091003
ExprKind::Repeat(e, len) => {
10101004
self.hash_expr(e);
1011-
self.hash_array_length(len);
1005+
self.hash_const_arg(len);
10121006
},
10131007
ExprKind::Ret(ref e) => {
10141008
if let Some(e) = *e {
@@ -1201,7 +1195,7 @@ impl<'a, 'tcx> SpanlessHash<'a, 'tcx> {
12011195
},
12021196
&TyKind::Array(ty, len) => {
12031197
self.hash_ty(ty);
1204-
self.hash_array_length(len);
1198+
self.hash_const_arg(len);
12051199
},
12061200
TyKind::Pat(ty, pat) => {
12071201
self.hash_ty(ty);
@@ -1252,13 +1246,6 @@ impl<'a, 'tcx> SpanlessHash<'a, 'tcx> {
12521246
}
12531247
}
12541248

1255-
pub fn hash_array_length(&mut self, length: ArrayLen<'_>) {
1256-
match length {
1257-
ArrayLen::Infer(..) => {},
1258-
ArrayLen::Body(ct) => self.hash_const_arg(ct),
1259-
}
1260-
}
1261-
12621249
pub fn hash_body(&mut self, body_id: BodyId) {
12631250
// swap out TypeckResults when hashing a body
12641251
let old_maybe_typeck_results = self.maybe_typeck_results.replace(self.cx.tcx.typeck_body(body_id));
@@ -1270,6 +1257,7 @@ impl<'a, 'tcx> SpanlessHash<'a, 'tcx> {
12701257
match &const_arg.kind {
12711258
ConstArgKind::Path(path) => self.hash_qpath(path),
12721259
ConstArgKind::Anon(anon) => self.hash_body(anon.body),
1260+
ConstArgKind::Infer(..) => {},
12731261
}
12741262
}
12751263

clippy_utils/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ use rustc_hir::definitions::{DefPath, DefPathData};
103103
use rustc_hir::hir_id::{HirIdMap, HirIdSet};
104104
use rustc_hir::intravisit::{FnKind, Visitor, walk_expr};
105105
use rustc_hir::{
106-
self as hir, Arm, ArrayLen, BindingMode, Block, BlockCheckMode, Body, ByRef, Closure, ConstArgKind, ConstContext,
106+
self as hir, Arm, BindingMode, Block, BlockCheckMode, Body, ByRef, Closure, ConstArgKind, ConstContext,
107107
Destination, Expr, ExprField, ExprKind, FnDecl, FnRetTy, GenericArgs, HirId, Impl, ImplItem, ImplItemKind,
108108
ImplItemRef, Item, ItemKind, LangItem, LetStmt, MatchSource, Mutability, Node, OwnerId, OwnerNode, Param, Pat,
109109
PatKind, Path, PathSegment, PrimTy, QPath, Stmt, StmtKind, TraitItem, TraitItemKind, TraitItemRef, TraitRef,
@@ -910,7 +910,7 @@ pub fn is_default_equivalent(cx: &LateContext<'_>, e: &Expr<'_>) -> bool {
910910
_ => false,
911911
},
912912
ExprKind::Tup(items) | ExprKind::Array(items) => items.iter().all(|x| is_default_equivalent(cx, x)),
913-
ExprKind::Repeat(x, ArrayLen::Body(len)) => {
913+
ExprKind::Repeat(x, len) => {
914914
if let ConstArgKind::Anon(anon_const) = len.kind
915915
&& let ExprKind::Lit(const_lit) = cx.tcx.hir().body(anon_const.body).value.kind
916916
&& let LitKind::Int(v, _) = const_lit.node
@@ -940,7 +940,7 @@ fn is_default_equivalent_from(cx: &LateContext<'_>, from_func: &Expr<'_>, arg: &
940940
..
941941
}) => return sym.is_empty() && is_path_lang_item(cx, ty, LangItem::String),
942942
ExprKind::Array([]) => return is_path_diagnostic_item(cx, ty, sym::Vec),
943-
ExprKind::Repeat(_, ArrayLen::Body(len)) => {
943+
ExprKind::Repeat(_, len) => {
944944
if let ConstArgKind::Anon(anon_const) = len.kind
945945
&& let ExprKind::Lit(const_lit) = cx.tcx.hir().body(anon_const.body).value.kind
946946
&& let LitKind::Int(v, _) = const_lit.node

tests/ui/author/repeat.stdout

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
if let ExprKind::Repeat(value, length) = expr.kind
22
&& let ExprKind::Lit(ref lit) = value.kind
33
&& let LitKind::Int(1, LitIntType::Unsigned(UintTy::U8)) = lit.node
4-
&& let ArrayLen::Body(const_arg) = length
5-
&& let ConstArgKind::Anon(anon_const) = const_arg.kind
4+
&& let ConstArgKind::Anon(anon_const) = length.kind
65
&& expr1 = &cx.tcx.hir().body(anon_const.body).value
76
&& let ExprKind::Lit(ref lit1) = expr1.kind
87
&& let LitKind::Int(5, LitIntType::Unsuffixed) = lit1.node

0 commit comments

Comments
 (0)