Skip to content

Commit f2c1265

Browse files
committed
Add ConstArgKind::Path and make ConstArg its own HIR node
This is a very large commit since a lot needs to be changed in order to make the tests pass. The salient changes are: - `ConstArgKind` gets a new `Path` variant, and all const params are now represented using it. Non-param paths still use `ConstArgKind::Anon` to prevent this change from getting too large, but they will soon use the `Path` variant too. - `ConstArg` gets a distinct `hir_id` field and its own variant in `hir::Node`. This affected many parts of the compiler that expected the parent of an `AnonConst` to be the containing context (e.g., an array repeat expression). They have been changed to check the "grandparent" where necessary. - Some `ast::AnonConst`s now have their `DefId`s created in rustc_ast_lowering rather than `DefCollector`. This is because in some cases they will end up becoming a `ConstArgKind::Path` instead, which has no `DefId`. We have to solve this in a hacky way where we guess whether the `AnonConst` could end up as a path const since we can't know for sure until after name resolution (`N` could refer to a free const or a nullary struct). If it has no chance as being a const param, then we create a `DefId` in `DefCollector` -- otherwise we decide during ast_lowering. This will have to be updated once all path consts use `ConstArgKind::Path`. - We explicitly use `ConstArgHasType` for array lengths, rather than implicitly relying on anon const type feeding -- this is due to the addition of `ConstArgKind::Path`. - Some tests have their outputs changed, but the changes are for the most part minor (including removing duplicate or almost-duplicate errors). One test now ICEs, but it is for an incomplete, unstable feature and is now tracked at rust-lang#127009.
1 parent 6e99e2d commit f2c1265

File tree

5 files changed

+21
-11
lines changed

5 files changed

+21
-11
lines changed

clippy_lints/src/utils/author.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -271,15 +271,14 @@ impl<'a, 'tcx> PrintVisitor<'a, 'tcx> {
271271

272272
fn const_arg(&self, const_arg: &Binding<&ConstArg<'_>>) {
273273
match const_arg.value.kind {
274-
// FIXME: uncomment for ConstArgKind::Path
275-
// ConstArgKind::Path(ref qpath) => {
276-
// bind!(self, qpath);
277-
// chain!(self, "let ConstArgKind::Path(ref {qpath}) = {const_arg}.kind");
278-
// self.qpath(qpath);
279-
// },
274+
ConstArgKind::Path(ref qpath) => {
275+
bind!(self, qpath);
276+
chain!(self, "let ConstArgKind::Path(ref {qpath}) = {const_arg}.kind");
277+
self.qpath(qpath);
278+
},
280279
ConstArgKind::Anon(anon_const) => {
281280
bind!(self, anon_const);
282-
chain!(self, "let ConstArgKind::({anon_const}) = {const_arg}.kind");
281+
chain!(self, "let ConstArgKind::Anon({anon_const}) = {const_arg}.kind");
283282
self.body(field!(anon_const.body));
284283
},
285284
}

clippy_utils/src/hir_utils.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,7 +421,12 @@ impl HirEqInterExpr<'_, '_, '_> {
421421

422422
fn eq_const_arg(&mut self, left: &ConstArg<'_>, right: &ConstArg<'_>) -> bool {
423423
match (&left.kind, &right.kind) {
424+
(ConstArgKind::Path(l_p), ConstArgKind::Path(r_p)) => self.eq_qpath(l_p, r_p),
424425
(ConstArgKind::Anon(l_an), ConstArgKind::Anon(r_an)) => self.eq_body(l_an.body, r_an.body),
426+
// Use explicit match for now since ConstArg is undergoing flux.
427+
(ConstArgKind::Path(..), ConstArgKind::Anon(..)) | (ConstArgKind::Anon(..), ConstArgKind::Path(..)) => {
428+
false
429+
},
425430
}
426431
}
427432

@@ -1142,6 +1147,7 @@ impl<'a, 'tcx> SpanlessHash<'a, 'tcx> {
11421147

11431148
fn hash_const_arg(&mut self, const_arg: &ConstArg<'_>) {
11441149
match &const_arg.kind {
1150+
ConstArgKind::Path(path) => self.hash_qpath(path),
11451151
ConstArgKind::Anon(anon) => self.hash_body(anon.body),
11461152
}
11471153
}

clippy_utils/src/lib.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -904,7 +904,6 @@ pub fn is_default_equivalent(cx: &LateContext<'_>, e: &Expr<'_>) -> bool {
904904
},
905905
ExprKind::Tup(items) | ExprKind::Array(items) => items.iter().all(|x| is_default_equivalent(cx, x)),
906906
ExprKind::Repeat(x, ArrayLen::Body(len)) => {
907-
#[allow(irrefutable_let_patterns)] // FIXME
908907
if let ConstArgKind::Anon(anon_const) = len.kind
909908
&& let ExprKind::Lit(const_lit) = cx.tcx.hir().body(anon_const.body).value.kind
910909
&& let LitKind::Int(v, _) = const_lit.node
@@ -935,7 +934,6 @@ fn is_default_equivalent_from(cx: &LateContext<'_>, from_func: &Expr<'_>, arg: &
935934
}) => return sym.is_empty() && is_path_lang_item(cx, ty, LangItem::String),
936935
ExprKind::Array([]) => return is_path_diagnostic_item(cx, ty, sym::Vec),
937936
ExprKind::Repeat(_, ArrayLen::Body(len)) => {
938-
#[allow(irrefutable_let_patterns)] // FIXME
939937
if let ConstArgKind::Anon(anon_const) = len.kind
940938
&& let ExprKind::Lit(const_lit) = cx.tcx.hir().body(anon_const.body).value.kind
941939
&& let LitKind::Int(v, _) = const_lit.node

tests/ui/author/repeat.stdout

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
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(anon_const) = length
4+
&& let ArrayLen::Body(const_arg) = length
5+
&& let ConstArgKind::Anon(anon_const) = const_arg.kind
56
&& expr1 = &cx.tcx.hir().body(anon_const.body).value
67
&& let ExprKind::Lit(ref lit1) = expr1.kind
78
&& let LitKind::Int(5, LitIntType::Unsuffixed) = lit1.node

tests/ui/min_ident_chars.stderr

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,5 +193,11 @@ error: this ident consists of a single char
193193
LL | fn wrong_pythagoras(a: f32, b: f32) -> f32 {
194194
| ^
195195

196-
error: aborting due to 32 previous errors
196+
error: this ident consists of a single char
197+
--> tests/ui/min_ident_chars.rs:93:41
198+
|
199+
LL | struct Array<T, const N: usize>([T; N]);
200+
| ^
201+
202+
error: aborting due to 33 previous errors
197203

0 commit comments

Comments
 (0)