Skip to content

Commit acd3ad9

Browse files
committed
Add optional ConstArg field to ItemKind::Const
Currently always None, but will be Some if the const item value is a const path.
1 parent e0883a2 commit acd3ad9

File tree

21 files changed

+30
-28
lines changed

21 files changed

+30
-28
lines changed

compiler/rustc_ast_lowering/src/item.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
212212
},
213213
);
214214
self.lower_define_opaque(hir_id, &define_opaque);
215-
hir::ItemKind::Const(ident, ty, generics, body_id)
215+
// TODO: make const arg instead of always using None
216+
hir::ItemKind::Const(ident, ty, generics, body_id, None)
216217
}
217218
ItemKind::Fn(box Fn {
218219
sig: FnSig { decl, header, span: fn_sig_span },

compiler/rustc_hir/src/hir.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -3986,8 +3986,8 @@ impl<'hir> Item<'hir> {
39863986
expect_static, (Ident, &'hir Ty<'hir>, Mutability, BodyId),
39873987
ItemKind::Static(ident, ty, mutbl, body), (*ident, ty, *mutbl, *body);
39883988

3989-
expect_const, (Ident, &'hir Ty<'hir>, &'hir Generics<'hir>, BodyId),
3990-
ItemKind::Const(ident, ty, generics, body), (*ident, ty, generics, *body);
3989+
expect_const, (Ident, &'hir Ty<'hir>, &'hir Generics<'hir>, BodyId, Option<&'hir ConstArg<'hir>>),
3990+
ItemKind::Const(ident, ty, generics, body, ct), (*ident, ty, generics, *body, *ct);
39913991

39923992
expect_fn, (Ident, &FnSig<'hir>, &'hir Generics<'hir>, BodyId),
39933993
ItemKind::Fn { ident, sig, generics, body, .. }, (*ident, sig, generics, *body);
@@ -4157,7 +4157,7 @@ pub enum ItemKind<'hir> {
41574157
/// A `static` item.
41584158
Static(Ident, &'hir Ty<'hir>, Mutability, BodyId),
41594159
/// A `const` item.
4160-
Const(Ident, &'hir Ty<'hir>, &'hir Generics<'hir>, BodyId),
4160+
Const(Ident, &'hir Ty<'hir>, &'hir Generics<'hir>, BodyId, Option<&'hir ConstArg<'hir>>),
41614161
/// A function declaration.
41624162
Fn {
41634163
ident: Ident,
@@ -4252,7 +4252,7 @@ impl ItemKind<'_> {
42524252
Some(match self {
42534253
ItemKind::Fn { generics, .. }
42544254
| ItemKind::TyAlias(_, _, generics)
4255-
| ItemKind::Const(_, _, generics, _)
4255+
| ItemKind::Const(_, _, generics, _, _)
42564256
| ItemKind::Enum(_, _, generics)
42574257
| ItemKind::Struct(_, _, generics)
42584258
| ItemKind::Union(_, _, generics)
@@ -4455,7 +4455,7 @@ impl<'hir> OwnerNode<'hir> {
44554455
OwnerNode::Item(Item {
44564456
kind:
44574457
ItemKind::Static(_, _, _, body)
4458-
| ItemKind::Const(_, _, _, body)
4458+
| ItemKind::Const(_, _, _, body, _)
44594459
| ItemKind::Fn { body, .. },
44604460
..
44614461
})
@@ -4681,7 +4681,7 @@ impl<'hir> Node<'hir> {
46814681
Node::Item(it) => match it.kind {
46824682
ItemKind::TyAlias(_, ty, _)
46834683
| ItemKind::Static(_, ty, _, _)
4684-
| ItemKind::Const(_, ty, _, _) => Some(ty),
4684+
| ItemKind::Const(_, ty, _, _, _) => Some(ty),
46854685
ItemKind::Impl(impl_item) => Some(&impl_item.self_ty),
46864686
_ => None,
46874687
},
@@ -4712,7 +4712,7 @@ impl<'hir> Node<'hir> {
47124712
Node::Item(Item {
47134713
owner_id,
47144714
kind:
4715-
ItemKind::Const(_, _, _, body)
4715+
ItemKind::Const(_, _, _, body, _)
47164716
| ItemKind::Static(.., body)
47174717
| ItemKind::Fn { body, .. },
47184718
..

compiler/rustc_hir/src/intravisit.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -550,11 +550,12 @@ pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item<'v>) -> V::
550550
try_visit!(visitor.visit_ty_unambig(typ));
551551
try_visit!(visitor.visit_nested_body(body));
552552
}
553-
ItemKind::Const(ident, ref typ, ref generics, body) => {
553+
ItemKind::Const(ident, ref typ, ref generics, body, ct) => {
554554
try_visit!(visitor.visit_ident(ident));
555555
try_visit!(visitor.visit_ty_unambig(typ));
556556
try_visit!(visitor.visit_generics(generics));
557557
try_visit!(visitor.visit_nested_body(body));
558+
visit_opt!(visitor, visit_const_arg_unambig, ct);
558559
}
559560
ItemKind::Fn { ident, sig, generics, body: body_id, .. } => {
560561
try_visit!(visitor.visit_ident(ident));

compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,7 @@ impl<'a, 'tcx> Visitor<'tcx> for BoundVarContext<'a, 'tcx> {
628628
intravisit::walk_item(self, item);
629629
}
630630
hir::ItemKind::TyAlias(_, _, generics)
631-
| hir::ItemKind::Const(_, _, generics, _)
631+
| hir::ItemKind::Const(_, _, generics,_, _)
632632
| hir::ItemKind::Enum(_, _, generics)
633633
| hir::ItemKind::Struct(_, _, generics)
634634
| hir::ItemKind::Union(_, _, generics)

compiler/rustc_hir_analysis/src/collect/type_of.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::EarlyBinder<'_
216216
icx.lower_ty(ty)
217217
}
218218
}
219-
ItemKind::Const(ident, ty, _, body_id) => {
219+
ItemKind::Const(ident, ty, _, body_id,_) => {
220220
if ty.is_suggestable_infer_ty() {
221221
infer_placeholder_type(
222222
icx.lowerer(),

compiler/rustc_hir_analysis/src/hir_wf_check.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ fn diagnostic_hir_wf_check<'tcx>(
138138
hir::Node::Item(item) => match item.kind {
139139
hir::ItemKind::TyAlias(_, ty, _)
140140
| hir::ItemKind::Static(_, ty, _, _)
141-
| hir::ItemKind::Const(_, ty, _, _) => vec![ty],
141+
| hir::ItemKind::Const(_, ty, _, _,_) => vec![ty],
142142
hir::ItemKind::Impl(impl_) => match &impl_.of_trait {
143143
Some(t) => t
144144
.path

compiler/rustc_hir_pretty/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -608,7 +608,7 @@ impl<'a> State<'a> {
608608
self.word(";");
609609
self.end(); // end the outer cbox
610610
}
611-
hir::ItemKind::Const(ident, ty, generics, expr) => {
611+
hir::ItemKind::Const(ident, ty, generics, expr, _ct) => {
612612
self.head("const");
613613
self.print_ident(ident);
614614
self.print_generic_params(generics.params);

compiler/rustc_hir_typeck/src/pat.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1245,7 +1245,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
12451245
match opt_def_id {
12461246
Some(def_id) => match self.tcx.hir_get_if_local(def_id) {
12471247
Some(hir::Node::Item(hir::Item {
1248-
kind: hir::ItemKind::Const(_, _, _, body_id),
1248+
kind: hir::ItemKind::Const(_, _, _, body_id, _),
12491249
..
12501250
})) => match self.tcx.hir_node(body_id.hir_id) {
12511251
hir::Node::Expr(expr) => {

compiler/rustc_lint/src/context.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -949,7 +949,7 @@ impl<'tcx> LateContext<'tcx> {
949949
..
950950
}) => *init,
951951
hir::Node::Item(item) => match item.kind {
952-
hir::ItemKind::Const(.., body_id) | hir::ItemKind::Static(.., body_id) => {
952+
hir::ItemKind::Const(.., body_id,_) | hir::ItemKind::Static(.., body_id) => {
953953
Some(self.tcx.hir_body(body_id).value)
954954
}
955955
_ => None,

compiler/rustc_lint/src/non_local_def.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ impl<'tcx> LateLintPass<'tcx> for NonLocalDefinitions {
183183
&& parent_opt_item_name != Some(kw::Underscore)
184184
&& let Some(parent) = parent.as_local()
185185
&& let Node::Item(item) = cx.tcx.hir_node_by_def_id(parent)
186-
&& let ItemKind::Const(ident, ty, _, _) = item.kind
186+
&& let ItemKind::Const(ident, ty, _, _, _) = item.kind
187187
&& let TyKind::Tup(&[]) = ty.kind
188188
{
189189
Some(ident.span)

compiler/rustc_mir_build/src/builder/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,7 @@ fn construct_const<'a, 'tcx>(
563563
// Figure out what primary body this item has.
564564
let (span, const_ty_span) = match tcx.hir_node(hir_id) {
565565
Node::Item(hir::Item {
566-
kind: hir::ItemKind::Static(_, ty, _, _) | hir::ItemKind::Const(_, ty, _, _),
566+
kind: hir::ItemKind::Static(_, ty, _, _) | hir::ItemKind::Const(_, ty, _, _, _),
567567
span,
568568
..
569569
})

compiler/rustc_passes/src/reachable.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ impl<'tcx> ReachableContext<'tcx> {
204204
}
205205
}
206206

207-
hir::ItemKind::Const(_, _, _, init) => {
207+
hir::ItemKind::Const(_, _, _, init, _) => {
208208
// Only things actually ending up in the final constant value are reachable
209209
// for codegen. Everything else is only needed during const-eval, so even if
210210
// const-eval happens in a downstream crate, all they need is

compiler/rustc_trait_selection/src/error_reporting/infer/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2026,7 +2026,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
20262026
}
20272027
LetVisitor { span }.visit_body(body).break_value()
20282028
}
2029-
hir::Node::Item(hir::Item { kind: hir::ItemKind::Const(_, ty, _, _), .. }) => {
2029+
hir::Node::Item(hir::Item { kind: hir::ItemKind::Const(_, ty, _, _, _), .. }) => {
20302030
Some(&ty.peel_refs().kind)
20312031
}
20322032
_ => None,

compiler/rustc_trait_selection/src/error_reporting/traits/suggestions.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
358358
| hir::ItemKind::Impl(hir::Impl { generics, .. })
359359
| hir::ItemKind::Fn { generics, .. }
360360
| hir::ItemKind::TyAlias(_, _, generics)
361-
| hir::ItemKind::Const(_, _, generics, _)
361+
| hir::ItemKind::Const(_, _, generics, _, _)
362362
| hir::ItemKind::TraitAlias(_, generics, _),
363363
..
364364
})
@@ -418,7 +418,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
418418
| hir::ItemKind::Impl(hir::Impl { generics, .. })
419419
| hir::ItemKind::Fn { generics, .. }
420420
| hir::ItemKind::TyAlias(_, _, generics)
421-
| hir::ItemKind::Const(_, _, generics, _)
421+
| hir::ItemKind::Const(_, _, generics, _, _)
422422
| hir::ItemKind::TraitAlias(_, generics, _),
423423
..
424424
}) if !param_ty => {

src/librustdoc/clean/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2804,7 +2804,7 @@ fn clean_maybe_renamed_item<'tcx>(
28042804
mutability,
28052805
expr: Some(body_id),
28062806
}),
2807-
ItemKind::Const(_, ty, generics, body_id) => ConstantItem(Box::new(Constant {
2807+
ItemKind::Const(_, ty, generics, body_id, _) => ConstantItem(Box::new(Constant {
28082808
generics: clean_generics(generics, cx),
28092809
type_: clean_ty(ty, cx),
28102810
kind: ConstantKind::Local { body: body_id, def_id },

src/tools/clippy/clippy_lints/src/large_const_arrays.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ impl_lint_pass!(LargeConstArrays => [LARGE_CONST_ARRAYS]);
4848

4949
impl<'tcx> LateLintPass<'tcx> for LargeConstArrays {
5050
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'_>) {
51-
if let ItemKind::Const(ident, _, generics, _) = &item.kind
51+
if let ItemKind::Const(ident, _, generics, _,_) = &item.kind
5252
// Since static items may not have generics, skip generic const items.
5353
// FIXME(generic_const_items): I don't think checking `generics.hwcp` suffices as it
5454
// doesn't account for empty where-clauses that only consist of keyword `where` IINM.

src/tools/clippy/clippy_lints/src/non_copy_const.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,7 @@ impl<'tcx> NonCopyConst<'tcx> {
310310

311311
impl<'tcx> LateLintPass<'tcx> for NonCopyConst<'tcx> {
312312
fn check_item(&mut self, cx: &LateContext<'tcx>, it: &'tcx Item<'_>) {
313-
if let ItemKind::Const(.., body_id) = it.kind {
313+
if let ItemKind::Const(.., body_id,_) = it.kind {
314314
let ty = cx.tcx.type_of(it.owner_id).instantiate_identity();
315315
if !ignored_macro(cx, it)
316316
&& self.interior_mut.is_interior_mut_ty(cx, ty)

src/tools/clippy/clippy_lints/src/types/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ impl<'tcx> LateLintPass<'tcx> for Types {
447447
let is_exported = cx.effective_visibilities.is_exported(item.owner_id.def_id);
448448

449449
match item.kind {
450-
ItemKind::Static(_, ty, _, _) | ItemKind::Const(_, ty, _, _) => self.check_ty(
450+
ItemKind::Static(_, ty, _, _) | ItemKind::Const(_, ty, _, _, _) => self.check_ty(
451451
cx,
452452
ty,
453453
CheckTyContext {

src/tools/clippy/clippy_lints/src/undocumented_unsafe_blocks.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ impl<'tcx> LateLintPass<'tcx> for UndocumentedUnsafeBlocks {
243243
},
244244
(ItemKind::Impl(_), _) => {},
245245
// const and static items only need a safety comment if their body is an unsafe block, lint otherwise
246-
(&ItemKind::Const(.., body) | &ItemKind::Static(.., body), HasSafetyComment::Yes(pos)) => {
246+
(&ItemKind::Const(.., body, _) | &ItemKind::Static(.., body), HasSafetyComment::Yes(pos)) => {
247247
if !is_lint_allowed(cx, UNNECESSARY_SAFETY_COMMENT, body.hir_id) {
248248
let body = cx.tcx.hir_body(body);
249249
if !matches!(

src/tools/clippy/clippy_utils/src/consts.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -644,7 +644,7 @@ impl<'tcx> ConstEvalCtxt<'tcx> {
644644
// which is NOT constant for our purposes.
645645
if let Some(node) = self.tcx.hir_get_if_local(def_id)
646646
&& let Node::Item(Item {
647-
kind: ItemKind::Const(.., body_id),
647+
kind: ItemKind::Const(.., body_id, _),
648648
..
649649
}) = node
650650
&& let Node::Expr(Expr {

src/tools/clippy/clippy_utils/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2648,7 +2648,7 @@ fn with_test_item_names(tcx: TyCtxt<'_>, module: LocalModDefId, f: impl Fn(&[Sym
26482648
for id in tcx.hir_module_free_items(module) {
26492649
if matches!(tcx.def_kind(id.owner_id), DefKind::Const)
26502650
&& let item = tcx.hir_item(id)
2651-
&& let ItemKind::Const(ident, ty, _generics, _body) = item.kind
2651+
&& let ItemKind::Const(ident, ty, _generics, _body, _ct_arg) = item.kind
26522652
{
26532653
if let TyKind::Path(QPath::Resolved(_, path)) = ty.kind {
26542654
// We could also check for the type name `test::TestDescAndFn`

0 commit comments

Comments
 (0)