Skip to content

Commit 9213aec

Browse files
committed
Lower generic const items to HIR
1 parent afd009a commit 9213aec

File tree

16 files changed

+84
-41
lines changed

16 files changed

+84
-41
lines changed

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -231,9 +231,15 @@ impl<'hir> LoweringContext<'_, 'hir> {
231231
let (ty, body_id) = self.lower_const_item(t, span, e.as_deref());
232232
hir::ItemKind::Static(ty, *m, body_id)
233233
}
234-
ItemKind::Const(box ast::ConstItem { ty, expr, .. }) => {
235-
let (ty, body_id) = self.lower_const_item(ty, span, expr.as_deref());
236-
hir::ItemKind::Const(ty, body_id)
234+
ItemKind::Const(box ast::ConstItem { generics, ty, expr, .. }) => {
235+
let (generics, (ty, body_id)) = self.lower_generics(
236+
generics,
237+
Const::No,
238+
id,
239+
&ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
240+
|this| this.lower_const_item(ty, span, expr.as_deref()),
241+
);
242+
hir::ItemKind::Const(ty, generics, body_id)
237243
}
238244
ItemKind::Fn(box Fn {
239245
sig: FnSig { decl, header, span: fn_sig_span },
@@ -715,11 +721,23 @@ impl<'hir> LoweringContext<'_, 'hir> {
715721
let trait_item_def_id = hir_id.expect_owner();
716722

717723
let (generics, kind, has_default) = match &i.kind {
718-
AssocItemKind::Const(box ConstItem { ty, expr, .. }) => {
719-
let ty =
720-
self.lower_ty(ty, &ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy));
721-
let body = expr.as_ref().map(|x| self.lower_const_body(i.span, Some(x)));
722-
(hir::Generics::empty(), hir::TraitItemKind::Const(ty, body), body.is_some())
724+
AssocItemKind::Const(box ConstItem { generics, ty, expr, .. }) => {
725+
let (generics, kind) = self.lower_generics(
726+
&generics,
727+
Const::No,
728+
i.id,
729+
&ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
730+
|this| {
731+
let ty = this.lower_ty(
732+
ty,
733+
&ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy),
734+
);
735+
let body = expr.as_ref().map(|x| this.lower_const_body(i.span, Some(x)));
736+
737+
hir::TraitItemKind::Const(ty, body)
738+
},
739+
);
740+
(generics, kind, expr.is_some())
723741
}
724742
AssocItemKind::Fn(box Fn { sig, generics, body: None, .. }) => {
725743
let asyncness = sig.header.asyncness;
@@ -817,14 +835,19 @@ impl<'hir> LoweringContext<'_, 'hir> {
817835
self.lower_attrs(hir_id, &i.attrs);
818836

819837
let (generics, kind) = match &i.kind {
820-
AssocItemKind::Const(box ConstItem { ty, expr, .. }) => {
821-
let ty =
822-
self.lower_ty(ty, &ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy));
823-
(
824-
hir::Generics::empty(),
825-
hir::ImplItemKind::Const(ty, self.lower_const_body(i.span, expr.as_deref())),
826-
)
827-
}
838+
AssocItemKind::Const(box ConstItem { generics, ty, expr, .. }) => self.lower_generics(
839+
&generics,
840+
Const::No,
841+
i.id,
842+
&ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
843+
|this| {
844+
let ty = this
845+
.lower_ty(ty, &ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy));
846+
let body = this.lower_const_body(i.span, expr.as_deref());
847+
848+
hir::ImplItemKind::Const(ty, body)
849+
},
850+
),
828851
AssocItemKind::Fn(box Fn { sig, generics, body, .. }) => {
829852
self.current_item = Some(i.span);
830853
let asyncness = sig.header.asyncness;

compiler/rustc_hir/src/hir.rs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3130,9 +3130,9 @@ impl<'hir> Item<'hir> {
31303130
}
31313131
/// Expect an [`ItemKind::Const`] or panic.
31323132
#[track_caller]
3133-
pub fn expect_const(&self) -> (&'hir Ty<'hir>, BodyId) {
3134-
let ItemKind::Const(ty, body) = self.kind else { self.expect_failed("a constant") };
3135-
(ty, body)
3133+
pub fn expect_const(&self) -> (&'hir Ty<'hir>, &'hir Generics<'hir>, BodyId) {
3134+
let ItemKind::Const(ty, gen, body) = self.kind else { self.expect_failed("a constant") };
3135+
(ty, gen, body)
31363136
}
31373137
/// Expect an [`ItemKind::Fn`] or panic.
31383138
#[track_caller]
@@ -3319,7 +3319,7 @@ pub enum ItemKind<'hir> {
33193319
/// A `static` item.
33203320
Static(&'hir Ty<'hir>, Mutability, BodyId),
33213321
/// A `const` item.
3322-
Const(&'hir Ty<'hir>, BodyId),
3322+
Const(&'hir Ty<'hir>, &'hir Generics<'hir>, BodyId),
33233323
/// A function declaration.
33243324
Fn(FnSig<'hir>, &'hir Generics<'hir>, BodyId),
33253325
/// A MBE macro definition (`macro_rules!` or `macro`).
@@ -3372,6 +3372,7 @@ impl ItemKind<'_> {
33723372
Some(match *self {
33733373
ItemKind::Fn(_, ref generics, _)
33743374
| ItemKind::TyAlias(_, ref generics)
3375+
| ItemKind::Const(_, ref generics, _)
33753376
| ItemKind::OpaqueTy(OpaqueTy { ref generics, .. })
33763377
| ItemKind::Enum(_, ref generics)
33773378
| ItemKind::Struct(_, ref generics)
@@ -3567,7 +3568,9 @@ impl<'hir> OwnerNode<'hir> {
35673568
match self {
35683569
OwnerNode::Item(Item {
35693570
kind:
3570-
ItemKind::Static(_, _, body) | ItemKind::Const(_, body) | ItemKind::Fn(_, _, body),
3571+
ItemKind::Static(_, _, body)
3572+
| ItemKind::Const(_, _, body)
3573+
| ItemKind::Fn(_, _, body),
35713574
..
35723575
})
35733576
| OwnerNode::TraitItem(TraitItem {
@@ -3770,9 +3773,9 @@ impl<'hir> Node<'hir> {
37703773
pub fn ty(self) -> Option<&'hir Ty<'hir>> {
37713774
match self {
37723775
Node::Item(it) => match it.kind {
3773-
ItemKind::TyAlias(ty, _) | ItemKind::Static(ty, _, _) | ItemKind::Const(ty, _) => {
3774-
Some(ty)
3775-
}
3776+
ItemKind::TyAlias(ty, _)
3777+
| ItemKind::Static(ty, _, _)
3778+
| ItemKind::Const(ty, _, _) => Some(ty),
37763779
_ => None,
37773780
},
37783781
Node::TraitItem(it) => match it.kind {
@@ -3800,7 +3803,9 @@ impl<'hir> Node<'hir> {
38003803
match self {
38013804
Node::Item(Item {
38023805
kind:
3803-
ItemKind::Static(_, _, body) | ItemKind::Const(_, body) | ItemKind::Fn(_, _, body),
3806+
ItemKind::Static(_, _, body)
3807+
| ItemKind::Const(_, _, body)
3808+
| ItemKind::Fn(_, _, body),
38043809
..
38053810
})
38063811
| Node::TraitItem(TraitItem {

compiler/rustc_hir/src/intravisit.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -467,11 +467,17 @@ pub fn walk_item<'v, V: Visitor<'v>>(visitor: &mut V, item: &'v Item<'v>) {
467467
ItemKind::Use(ref path, _) => {
468468
visitor.visit_use(path, item.hir_id());
469469
}
470-
ItemKind::Static(ref typ, _, body) | ItemKind::Const(ref typ, body) => {
470+
ItemKind::Static(ref typ, _, body) => {
471471
visitor.visit_id(item.hir_id());
472472
visitor.visit_ty(typ);
473473
visitor.visit_nested_body(body);
474474
}
475+
ItemKind::Const(ref typ, ref generics, body) => {
476+
visitor.visit_id(item.hir_id());
477+
visitor.visit_ty(typ);
478+
visitor.visit_generics(generics);
479+
visitor.visit_nested_body(body);
480+
}
475481
ItemKind::Fn(ref sig, ref generics, body_id) => {
476482
visitor.visit_id(item.hir_id());
477483
visitor.visit_fn(

compiler/rustc_hir_analysis/src/collect/type_of.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::EarlyBinder<Ty
404404
icx.to_ty(ty)
405405
}
406406
}
407-
ItemKind::Const(ty, body_id) => {
407+
ItemKind::Const(ty, _, body_id) => {
408408
if is_suggestable_infer_ty(ty) {
409409
infer_placeholder_type(
410410
tcx, def_id, body_id, ty.span, item.ident, "constant",

compiler/rustc_hir_analysis/src/hir_wf_check.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ fn diagnostic_hir_wf_check<'tcx>(
130130
hir::Node::Item(item) => match item.kind {
131131
hir::ItemKind::TyAlias(ty, _)
132132
| hir::ItemKind::Static(ty, _, _)
133-
| hir::ItemKind::Const(ty, _) => vec![ty],
133+
| hir::ItemKind::Const(ty, _, _) => vec![ty],
134134
hir::ItemKind::Impl(impl_) => match &impl_.of_trait {
135135
Some(t) => t
136136
.path

compiler/rustc_hir_pretty/src/lib.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -420,19 +420,21 @@ impl<'a> State<'a> {
420420
fn print_associated_const(
421421
&mut self,
422422
ident: Ident,
423+
generics: &hir::Generics<'_>,
423424
ty: &hir::Ty<'_>,
424425
default: Option<hir::BodyId>,
425426
) {
426-
self.head("");
427427
self.word_space("const");
428428
self.print_ident(ident);
429+
self.print_generic_params(generics.params);
429430
self.word_space(":");
430431
self.print_type(ty);
431432
if let Some(expr) = default {
432433
self.space();
433434
self.word_space("=");
434435
self.ann.nested(self, Nested::Body(expr));
435436
}
437+
self.print_where_clause(generics);
436438
self.word(";")
437439
}
438440

@@ -532,16 +534,18 @@ impl<'a> State<'a> {
532534
self.word(";");
533535
self.end(); // end the outer cbox
534536
}
535-
hir::ItemKind::Const(ty, expr) => {
537+
hir::ItemKind::Const(ty, generics, expr) => {
536538
self.head("const");
537539
self.print_ident(item.ident);
540+
self.print_generic_params(generics.params);
538541
self.word_space(":");
539542
self.print_type(ty);
540543
self.space();
541544
self.end(); // end the head-ibox
542545

543546
self.word_space("=");
544547
self.ann.nested(self, Nested::Body(expr));
548+
self.print_where_clause(generics);
545549
self.word(";");
546550
self.end(); // end the outer cbox
547551
}
@@ -836,7 +840,7 @@ impl<'a> State<'a> {
836840
self.print_outer_attributes(self.attrs(ti.hir_id()));
837841
match ti.kind {
838842
hir::TraitItemKind::Const(ty, default) => {
839-
self.print_associated_const(ti.ident, ty, default);
843+
self.print_associated_const(ti.ident, ti.generics, ty, default);
840844
}
841845
hir::TraitItemKind::Fn(ref sig, hir::TraitFn::Required(arg_names)) => {
842846
self.print_method_sig(ti.ident, sig, ti.generics, arg_names, None);
@@ -865,7 +869,7 @@ impl<'a> State<'a> {
865869

866870
match ii.kind {
867871
hir::ImplItemKind::Const(ty, expr) => {
868-
self.print_associated_const(ii.ident, ty, Some(expr));
872+
self.print_associated_const(ii.ident, ii.generics, ty, Some(expr));
869873
}
870874
hir::ImplItemKind::Fn(ref sig, body) => {
871875
self.head("");

compiler/rustc_hir_typeck/src/expr.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1394,7 +1394,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
13941394
let Some((
13951395
_,
13961396
hir::Node::Local(hir::Local { ty: Some(ty), .. })
1397-
| hir::Node::Item(hir::Item { kind: hir::ItemKind::Const(ty, _), .. }),
1397+
| hir::Node::Item(hir::Item { kind: hir::ItemKind::Const(ty, _, _), .. }),
13981398
)) = parent_node
13991399
else {
14001400
return;

compiler/rustc_hir_typeck/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ fn primary_body_of(
101101
) -> Option<(hir::BodyId, Option<&hir::Ty<'_>>, Option<&hir::FnSig<'_>>)> {
102102
match node {
103103
Node::Item(item) => match item.kind {
104-
hir::ItemKind::Const(ty, body) | hir::ItemKind::Static(ty, _, body) => {
104+
hir::ItemKind::Const(ty, _, body) | hir::ItemKind::Static(ty, _, body) => {
105105
Some((body, Some(ty), None))
106106
}
107107
hir::ItemKind::Fn(ref sig, .., body) => Some((body, None, Some(sig))),

compiler/rustc_hir_typeck/src/pat.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -924,7 +924,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
924924
match opt_def_id {
925925
Some(def_id) => match self.tcx.hir().get_if_local(def_id) {
926926
Some(hir::Node::Item(hir::Item {
927-
kind: hir::ItemKind::Const(_, body_id), ..
927+
kind: hir::ItemKind::Const(_, _, body_id),
928+
..
928929
})) => match self.tcx.hir().get(body_id.hir_id) {
929930
hir::Node::Expr(expr) => {
930931
if hir::is_range_literal(expr) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2068,7 +2068,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
20682068
visitor.visit_body(body);
20692069
visitor.result.map(|r| &r.peel_refs().kind)
20702070
}
2071-
Some(hir::Node::Item(hir::Item { kind: hir::ItemKind::Const(ty, _), .. })) => {
2071+
Some(hir::Node::Item(hir::Item { kind: hir::ItemKind::Const(ty, _, _), .. })) => {
20722072
Some(&ty.peel_refs().kind)
20732073
}
20742074
_ => None,

compiler/rustc_lint/src/builtin.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1529,9 +1529,10 @@ declare_lint_pass!(
15291529
impl<'tcx> LateLintPass<'tcx> for UnusedBrokenConst {
15301530
fn check_item(&mut self, cx: &LateContext<'_>, it: &hir::Item<'_>) {
15311531
match it.kind {
1532-
hir::ItemKind::Const(_, body_id) => {
1532+
hir::ItemKind::Const(_, _, body_id) => {
15331533
let def_id = cx.tcx.hir().body_owner_def_id(body_id).to_def_id();
15341534
// trigger the query once for all constants since that will already report the errors
1535+
// FIXME(generic_const_items): Does this work properly with generic const items?
15351536
cx.tcx.ensure().const_eval_poly(def_id);
15361537
}
15371538
hir::ItemKind::Static(_, _, body_id) => {

compiler/rustc_middle/src/hir/map/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ pub fn associated_body(node: Node<'_>) -> Option<(LocalDefId, BodyId)> {
2424
match node {
2525
Node::Item(Item {
2626
owner_id,
27-
kind: ItemKind::Const(_, body) | ItemKind::Static(.., body) | ItemKind::Fn(.., body),
27+
kind: ItemKind::Const(_, _, body) | ItemKind::Static(.., body) | ItemKind::Fn(.., body),
2828
..
2929
})
3030
| Node::TraitItem(TraitItem {

compiler/rustc_mir_build/src/build/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,7 @@ fn construct_const<'a, 'tcx>(
570570
// Figure out what primary body this item has.
571571
let (span, const_ty_span) = match tcx.hir().get(hir_id) {
572572
Node::Item(hir::Item {
573-
kind: hir::ItemKind::Static(ty, _, _) | hir::ItemKind::Const(ty, _),
573+
kind: hir::ItemKind::Static(ty, _, _) | hir::ItemKind::Const(ty, _, _),
574574
span,
575575
..
576576
})

compiler/rustc_passes/src/reachable.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ impl<'tcx> ReachableContext<'tcx> {
236236
// Reachable constants will be inlined into other crates
237237
// unconditionally, so we need to make sure that their
238238
// contents are also reachable.
239-
hir::ItemKind::Const(_, init) | hir::ItemKind::Static(_, _, init) => {
239+
hir::ItemKind::Const(_, _, init) | hir::ItemKind::Static(_, _, init) => {
240240
self.visit_nested_body(init);
241241
}
242242

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
655655
| hir::ItemKind::Impl(hir::Impl { generics, .. })
656656
| hir::ItemKind::Fn(_, generics, _)
657657
| hir::ItemKind::TyAlias(_, generics)
658+
| hir::ItemKind::Const(_, generics, _)
658659
| hir::ItemKind::TraitAlias(generics, _)
659660
| hir::ItemKind::OpaqueTy(hir::OpaqueTy { generics, .. }),
660661
..
@@ -720,6 +721,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
720721
| hir::ItemKind::Impl(hir::Impl { generics, .. })
721722
| hir::ItemKind::Fn(_, generics, _)
722723
| hir::ItemKind::TyAlias(_, generics)
724+
| hir::ItemKind::Const(_, generics, _)
723725
| hir::ItemKind::TraitAlias(generics, _)
724726
| hir::ItemKind::OpaqueTy(hir::OpaqueTy { generics, .. }),
725727
..

src/librustdoc/clean/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2603,7 +2603,8 @@ fn clean_maybe_renamed_item<'tcx>(
26032603
ItemKind::Static(ty, mutability, body_id) => {
26042604
StaticItem(Static { type_: clean_ty(ty, cx), mutability, expr: Some(body_id) })
26052605
}
2606-
ItemKind::Const(ty, body_id) => ConstantItem(Constant {
2606+
// FIXME(fmease): rustdoc integration
2607+
ItemKind::Const(ty, _generics, body_id) => ConstantItem(Constant {
26072608
type_: clean_ty(ty, cx),
26082609
kind: ConstantKind::Local { body: body_id, def_id },
26092610
}),

0 commit comments

Comments
 (0)