Add optional ConstArg field to ItemKind::Const

Currently always None, but will be Some if the const item value is a
const path.
This commit is contained in:
Noah Lev 2025-04-07 17:26:21 -04:00
parent e0883a2a6c
commit acd3ad9e26
21 changed files with 30 additions and 28 deletions

View File

@ -212,7 +212,8 @@ impl<'hir> LoweringContext<'_, 'hir> {
},
);
self.lower_define_opaque(hir_id, &define_opaque);
hir::ItemKind::Const(ident, ty, generics, body_id)
// TODO: make const arg instead of always using None
hir::ItemKind::Const(ident, ty, generics, body_id, None)
}
ItemKind::Fn(box Fn {
sig: FnSig { decl, header, span: fn_sig_span },

View File

@ -3986,8 +3986,8 @@ impl<'hir> Item<'hir> {
expect_static, (Ident, &'hir Ty<'hir>, Mutability, BodyId),
ItemKind::Static(ident, ty, mutbl, body), (*ident, ty, *mutbl, *body);
expect_const, (Ident, &'hir Ty<'hir>, &'hir Generics<'hir>, BodyId),
ItemKind::Const(ident, ty, generics, body), (*ident, ty, generics, *body);
expect_const, (Ident, &'hir Ty<'hir>, &'hir Generics<'hir>, BodyId, Option<&'hir ConstArg<'hir>>),
ItemKind::Const(ident, ty, generics, body, ct), (*ident, ty, generics, *body, *ct);
expect_fn, (Ident, &FnSig<'hir>, &'hir Generics<'hir>, BodyId),
ItemKind::Fn { ident, sig, generics, body, .. }, (*ident, sig, generics, *body);
@ -4157,7 +4157,7 @@ pub enum ItemKind<'hir> {
/// A `static` item.
Static(Ident, &'hir Ty<'hir>, Mutability, BodyId),
/// A `const` item.
Const(Ident, &'hir Ty<'hir>, &'hir Generics<'hir>, BodyId),
Const(Ident, &'hir Ty<'hir>, &'hir Generics<'hir>, BodyId, Option<&'hir ConstArg<'hir>>),
/// A function declaration.
Fn {
ident: Ident,
@ -4252,7 +4252,7 @@ impl ItemKind<'_> {
Some(match self {
ItemKind::Fn { generics, .. }
| ItemKind::TyAlias(_, _, generics)
| ItemKind::Const(_, _, generics, _)
| ItemKind::Const(_, _, generics, _, _)
| ItemKind::Enum(_, _, generics)
| ItemKind::Struct(_, _, generics)
| ItemKind::Union(_, _, generics)
@ -4455,7 +4455,7 @@ impl<'hir> OwnerNode<'hir> {
OwnerNode::Item(Item {
kind:
ItemKind::Static(_, _, _, body)
| ItemKind::Const(_, _, _, body)
| ItemKind::Const(_, _, _, body, _)
| ItemKind::Fn { body, .. },
..
})
@ -4681,7 +4681,7 @@ impl<'hir> Node<'hir> {
Node::Item(it) => match it.kind {
ItemKind::TyAlias(_, ty, _)
| ItemKind::Static(_, ty, _, _)
| ItemKind::Const(_, ty, _, _) => Some(ty),
| ItemKind::Const(_, ty, _, _, _) => Some(ty),
ItemKind::Impl(impl_item) => Some(&impl_item.self_ty),
_ => None,
},
@ -4712,7 +4712,7 @@ impl<'hir> Node<'hir> {
Node::Item(Item {
owner_id,
kind:
ItemKind::Const(_, _, _, body)
ItemKind::Const(_, _, _, body, _)
| ItemKind::Static(.., body)
| ItemKind::Fn { body, .. },
..

View File

@ -550,11 +550,12 @@ pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item<'v>) -> V::
try_visit!(visitor.visit_ty_unambig(typ));
try_visit!(visitor.visit_nested_body(body));
}
ItemKind::Const(ident, ref typ, ref generics, body) => {
ItemKind::Const(ident, ref typ, ref generics, body, ct) => {
try_visit!(visitor.visit_ident(ident));
try_visit!(visitor.visit_ty_unambig(typ));
try_visit!(visitor.visit_generics(generics));
try_visit!(visitor.visit_nested_body(body));
visit_opt!(visitor, visit_const_arg_unambig, ct);
}
ItemKind::Fn { ident, sig, generics, body: body_id, .. } => {
try_visit!(visitor.visit_ident(ident));

View File

@ -628,7 +628,7 @@ impl<'a, 'tcx> Visitor<'tcx> for BoundVarContext<'a, 'tcx> {
intravisit::walk_item(self, item);
}
hir::ItemKind::TyAlias(_, _, generics)
| hir::ItemKind::Const(_, _, generics, _)
| hir::ItemKind::Const(_, _, generics,_, _)
| hir::ItemKind::Enum(_, _, generics)
| hir::ItemKind::Struct(_, _, generics)
| hir::ItemKind::Union(_, _, generics)

View File

@ -216,7 +216,7 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::EarlyBinder<'_
icx.lower_ty(ty)
}
}
ItemKind::Const(ident, ty, _, body_id) => {
ItemKind::Const(ident, ty, _, body_id,_) => {
if ty.is_suggestable_infer_ty() {
infer_placeholder_type(
icx.lowerer(),

View File

@ -138,7 +138,7 @@ fn diagnostic_hir_wf_check<'tcx>(
hir::Node::Item(item) => match item.kind {
hir::ItemKind::TyAlias(_, ty, _)
| hir::ItemKind::Static(_, ty, _, _)
| hir::ItemKind::Const(_, ty, _, _) => vec![ty],
| hir::ItemKind::Const(_, ty, _, _,_) => vec![ty],
hir::ItemKind::Impl(impl_) => match &impl_.of_trait {
Some(t) => t
.path

View File

@ -608,7 +608,7 @@ impl<'a> State<'a> {
self.word(";");
self.end(); // end the outer cbox
}
hir::ItemKind::Const(ident, ty, generics, expr) => {
hir::ItemKind::Const(ident, ty, generics, expr, _ct) => {
self.head("const");
self.print_ident(ident);
self.print_generic_params(generics.params);

View File

@ -1245,7 +1245,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
match opt_def_id {
Some(def_id) => match self.tcx.hir_get_if_local(def_id) {
Some(hir::Node::Item(hir::Item {
kind: hir::ItemKind::Const(_, _, _, body_id),
kind: hir::ItemKind::Const(_, _, _, body_id, _),
..
})) => match self.tcx.hir_node(body_id.hir_id) {
hir::Node::Expr(expr) => {

View File

@ -949,7 +949,7 @@ impl<'tcx> LateContext<'tcx> {
..
}) => *init,
hir::Node::Item(item) => match item.kind {
hir::ItemKind::Const(.., body_id) | hir::ItemKind::Static(.., body_id) => {
hir::ItemKind::Const(.., body_id,_) | hir::ItemKind::Static(.., body_id) => {
Some(self.tcx.hir_body(body_id).value)
}
_ => None,

View File

@ -183,7 +183,7 @@ impl<'tcx> LateLintPass<'tcx> for NonLocalDefinitions {
&& parent_opt_item_name != Some(kw::Underscore)
&& let Some(parent) = parent.as_local()
&& let Node::Item(item) = cx.tcx.hir_node_by_def_id(parent)
&& let ItemKind::Const(ident, ty, _, _) = item.kind
&& let ItemKind::Const(ident, ty, _, _, _) = item.kind
&& let TyKind::Tup(&[]) = ty.kind
{
Some(ident.span)

View File

@ -563,7 +563,7 @@ fn construct_const<'a, 'tcx>(
// Figure out what primary body this item has.
let (span, const_ty_span) = match tcx.hir_node(hir_id) {
Node::Item(hir::Item {
kind: hir::ItemKind::Static(_, ty, _, _) | hir::ItemKind::Const(_, ty, _, _),
kind: hir::ItemKind::Static(_, ty, _, _) | hir::ItemKind::Const(_, ty, _, _, _),
span,
..
})

View File

@ -204,7 +204,7 @@ impl<'tcx> ReachableContext<'tcx> {
}
}
hir::ItemKind::Const(_, _, _, init) => {
hir::ItemKind::Const(_, _, _, init, _) => {
// Only things actually ending up in the final constant value are reachable
// for codegen. Everything else is only needed during const-eval, so even if
// const-eval happens in a downstream crate, all they need is

View File

@ -2026,7 +2026,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
}
LetVisitor { span }.visit_body(body).break_value()
}
hir::Node::Item(hir::Item { kind: hir::ItemKind::Const(_, ty, _, _), .. }) => {
hir::Node::Item(hir::Item { kind: hir::ItemKind::Const(_, ty, _, _, _), .. }) => {
Some(&ty.peel_refs().kind)
}
_ => None,

View File

@ -358,7 +358,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
| hir::ItemKind::Impl(hir::Impl { generics, .. })
| hir::ItemKind::Fn { generics, .. }
| hir::ItemKind::TyAlias(_, _, generics)
| hir::ItemKind::Const(_, _, generics, _)
| hir::ItemKind::Const(_, _, generics, _, _)
| hir::ItemKind::TraitAlias(_, generics, _),
..
})
@ -418,7 +418,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
| hir::ItemKind::Impl(hir::Impl { generics, .. })
| hir::ItemKind::Fn { generics, .. }
| hir::ItemKind::TyAlias(_, _, generics)
| hir::ItemKind::Const(_, _, generics, _)
| hir::ItemKind::Const(_, _, generics, _, _)
| hir::ItemKind::TraitAlias(_, generics, _),
..
}) if !param_ty => {

View File

@ -2804,7 +2804,7 @@ fn clean_maybe_renamed_item<'tcx>(
mutability,
expr: Some(body_id),
}),
ItemKind::Const(_, ty, generics, body_id) => ConstantItem(Box::new(Constant {
ItemKind::Const(_, ty, generics, body_id, _) => ConstantItem(Box::new(Constant {
generics: clean_generics(generics, cx),
type_: clean_ty(ty, cx),
kind: ConstantKind::Local { body: body_id, def_id },

View File

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

View File

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

View File

@ -447,7 +447,7 @@ impl<'tcx> LateLintPass<'tcx> for Types {
let is_exported = cx.effective_visibilities.is_exported(item.owner_id.def_id);
match item.kind {
ItemKind::Static(_, ty, _, _) | ItemKind::Const(_, ty, _, _) => self.check_ty(
ItemKind::Static(_, ty, _, _) | ItemKind::Const(_, ty, _, _, _) => self.check_ty(
cx,
ty,
CheckTyContext {

View File

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

View File

@ -644,7 +644,7 @@ impl<'tcx> ConstEvalCtxt<'tcx> {
// which is NOT constant for our purposes.
if let Some(node) = self.tcx.hir_get_if_local(def_id)
&& let Node::Item(Item {
kind: ItemKind::Const(.., body_id),
kind: ItemKind::Const(.., body_id, _),
..
}) = node
&& let Node::Expr(Expr {

View File

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