Skip to content

Commit 7422b86

Browse files
committed
Lower generic consts to HIR
1 parent bcdda5e commit 7422b86

File tree

16 files changed

+85
-41
lines changed

16 files changed

+85
-41
lines changed

compiler/rustc_ast_lowering/src/item.rs

Lines changed: 40 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 },
@@ -723,11 +729,24 @@ impl<'hir> LoweringContext<'_, 'hir> {
723729
let trait_item_def_id = hir_id.expect_owner();
724730

725731
let (generics, kind, has_default) = match &i.kind {
726-
AssocItemKind::Const(box ConstItem { ty, expr, .. }) => {
727-
let ty =
728-
self.lower_ty(ty, &ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy));
729-
let body = expr.as_ref().map(|x| self.lower_const_body(i.span, Some(x)));
730-
(hir::Generics::empty(), hir::TraitItemKind::Const(ty, body), body.is_some())
732+
AssocItemKind::Const(box ConstItem { generics, ty, expr, .. }) => {
733+
let (generics, kind) = self.lower_generics(
734+
&generics,
735+
Const::No,
736+
i.id,
737+
&ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
738+
|this| {
739+
let ty = this.lower_ty(
740+
ty,
741+
// FIXME(generic_consts): Should we create `AssocConstTy` and use it here?
742+
&ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy),
743+
);
744+
let body = expr.as_ref().map(|x| this.lower_const_body(i.span, Some(x)));
745+
746+
hir::TraitItemKind::Const(ty, body)
747+
},
748+
);
749+
(generics, kind, expr.is_some())
731750
}
732751
AssocItemKind::Fn(box Fn { sig, generics, body: None, .. }) => {
733752
let asyncness = sig.header.asyncness;
@@ -825,14 +844,19 @@ impl<'hir> LoweringContext<'_, 'hir> {
825844
self.lower_attrs(hir_id, &i.attrs);
826845

827846
let (generics, kind) = match &i.kind {
828-
AssocItemKind::Const(box ConstItem { ty, expr, .. }) => {
829-
let ty =
830-
self.lower_ty(ty, &ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy));
831-
(
832-
hir::Generics::empty(),
833-
hir::ImplItemKind::Const(ty, self.lower_const_body(i.span, expr.as_deref())),
834-
)
835-
}
847+
AssocItemKind::Const(box ConstItem { generics, ty, expr, .. }) => self.lower_generics(
848+
&generics,
849+
Const::No,
850+
i.id,
851+
&ImplTraitContext::Disallowed(ImplTraitPosition::Generic),
852+
|this| {
853+
let ty = this
854+
.lower_ty(ty, &ImplTraitContext::Disallowed(ImplTraitPosition::ConstTy));
855+
let body = this.lower_const_body(i.span, expr.as_deref());
856+
857+
hir::ImplItemKind::Const(ty, body)
858+
},
859+
),
836860
AssocItemKind::Fn(box Fn { sig, generics, body, .. }) => {
837861
self.current_item = Some(i.span);
838862
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
@@ -3122,9 +3122,9 @@ impl<'hir> Item<'hir> {
31223122
}
31233123
/// Expect an [`ItemKind::Const`] or panic.
31243124
#[track_caller]
3125-
pub fn expect_const(&self) -> (&'hir Ty<'hir>, BodyId) {
3126-
let ItemKind::Const(ty, body) = self.kind else { self.expect_failed("a constant") };
3127-
(ty, body)
3125+
pub fn expect_const(&self) -> (&'hir Ty<'hir>, &'hir Generics<'hir>, BodyId) {
3126+
let ItemKind::Const(ty, gen, body) = self.kind else { self.expect_failed("a constant") };
3127+
(ty, gen, body)
31283128
}
31293129
/// Expect an [`ItemKind::Fn`] or panic.
31303130
#[track_caller]
@@ -3305,7 +3305,7 @@ pub enum ItemKind<'hir> {
33053305
/// A `static` item.
33063306
Static(&'hir Ty<'hir>, Mutability, BodyId),
33073307
/// A `const` item.
3308-
Const(&'hir Ty<'hir>, BodyId),
3308+
Const(&'hir Ty<'hir>, &'hir Generics<'hir>, BodyId),
33093309
/// A function declaration.
33103310
Fn(FnSig<'hir>, &'hir Generics<'hir>, BodyId),
33113311
/// A MBE macro definition (`macro_rules!` or `macro`).
@@ -3358,6 +3358,7 @@ impl ItemKind<'_> {
33583358
Some(match *self {
33593359
ItemKind::Fn(_, ref generics, _)
33603360
| ItemKind::TyAlias(_, ref generics)
3361+
| ItemKind::Const(_, ref generics, _)
33613362
| ItemKind::OpaqueTy(OpaqueTy { ref generics, .. })
33623363
| ItemKind::Enum(_, ref generics)
33633364
| ItemKind::Struct(_, ref generics)
@@ -3553,7 +3554,9 @@ impl<'hir> OwnerNode<'hir> {
35533554
match self {
35543555
OwnerNode::Item(Item {
35553556
kind:
3556-
ItemKind::Static(_, _, body) | ItemKind::Const(_, body) | ItemKind::Fn(_, _, body),
3557+
ItemKind::Static(_, _, body)
3558+
| ItemKind::Const(_, _, body)
3559+
| ItemKind::Fn(_, _, body),
35573560
..
35583561
})
35593562
| OwnerNode::TraitItem(TraitItem {
@@ -3756,9 +3759,9 @@ impl<'hir> Node<'hir> {
37563759
pub fn ty(self) -> Option<&'hir Ty<'hir>> {
37573760
match self {
37583761
Node::Item(it) => match it.kind {
3759-
ItemKind::TyAlias(ty, _) | ItemKind::Static(ty, _, _) | ItemKind::Const(ty, _) => {
3760-
Some(ty)
3761-
}
3762+
ItemKind::TyAlias(ty, _)
3763+
| ItemKind::Static(ty, _, _)
3764+
| ItemKind::Const(ty, _, _) => Some(ty),
37623765
_ => None,
37633766
},
37643767
Node::TraitItem(it) => match it.kind {
@@ -3786,7 +3789,9 @@ impl<'hir> Node<'hir> {
37863789
match self {
37873790
Node::Item(Item {
37883791
kind:
3789-
ItemKind::Static(_, _, body) | ItemKind::Const(_, body) | ItemKind::Fn(_, _, body),
3792+
ItemKind::Static(_, _, body)
3793+
| ItemKind::Const(_, _, body)
3794+
| ItemKind::Fn(_, _, body),
37903795
..
37913796
})
37923797
| 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
@@ -405,7 +405,7 @@ pub(super) fn type_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::EarlyBinder<Ty
405405
icx.to_ty(ty)
406406
}
407407
}
408-
ItemKind::Const(ty, body_id) => {
408+
ItemKind::Const(ty, _, body_id) => {
409409
if is_suggestable_infer_ty(ty) {
410410
infer_placeholder_type(
411411
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
@@ -1392,7 +1392,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
13921392
});
13931393
let Some((_,
13941394
hir::Node::Local(hir::Local { ty: Some(ty), .. })
1395-
| hir::Node::Item(hir::Item { kind: hir::ItemKind::Const(ty, _), .. }))
1395+
| hir::Node::Item(hir::Item { kind: hir::ItemKind::Const(ty, _, _), .. }))
13961396
) = parent_node else {
13971397
return
13981398
};

compiler/rustc_hir_typeck/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ fn primary_body_of(
102102
) -> Option<(hir::BodyId, Option<&hir::Ty<'_>>, Option<&hir::FnSig<'_>>)> {
103103
match node {
104104
Node::Item(item) => match item.kind {
105-
hir::ItemKind::Const(ty, body) | hir::ItemKind::Static(ty, _, body) => {
105+
hir::ItemKind::Const(ty, _, body) | hir::ItemKind::Static(ty, _, body) => {
106106
Some((body, Some(ty), None))
107107
}
108108
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
@@ -922,7 +922,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
922922
match opt_def_id {
923923
Some(def_id) => match self.tcx.hir().get_if_local(def_id) {
924924
Some(hir::Node::Item(hir::Item {
925-
kind: hir::ItemKind::Const(_, body_id), ..
925+
kind: hir::ItemKind::Const(_, _, body_id),
926+
..
926927
})) => match self.tcx.hir().get(body_id.hir_id) {
927928
hir::Node::Expr(expr) => {
928929
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
@@ -2069,7 +2069,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
20692069
visitor.visit_body(body);
20702070
visitor.result.map(|r| &r.peel_refs().kind)
20712071
}
2072-
Some(hir::Node::Item(hir::Item { kind: hir::ItemKind::Const(ty, _), .. })) => {
2072+
Some(hir::Node::Item(hir::Item { kind: hir::ItemKind::Const(ty, _, _), .. })) => {
20732073
Some(&ty.peel_refs().kind)
20742074
}
20752075
_ => None,

compiler/rustc_lint/src/builtin.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1531,9 +1531,10 @@ declare_lint_pass!(
15311531
impl<'tcx> LateLintPass<'tcx> for UnusedBrokenConst {
15321532
fn check_item(&mut self, cx: &LateContext<'_>, it: &hir::Item<'_>) {
15331533
match it.kind {
1534-
hir::ItemKind::Const(_, body_id) => {
1534+
hir::ItemKind::Const(_, _, body_id) => {
15351535
let def_id = cx.tcx.hir().body_owner_def_id(body_id).to_def_id();
15361536
// trigger the query once for all constants since that will already report the errors
1537+
// FIXME(generic_consts): Does this work properly with generic consts?
15371538
cx.tcx.ensure().const_eval_poly(def_id);
15381539
}
15391540
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
@@ -562,7 +562,7 @@ fn construct_const<'a, 'tcx>(
562562
// Figure out what primary body this item has.
563563
let (span, const_ty_span) = match tcx.hir().get(hir_id) {
564564
Node::Item(hir::Item {
565-
kind: hir::ItemKind::Static(ty, _, _) | hir::ItemKind::Const(ty, _),
565+
kind: hir::ItemKind::Static(ty, _, _) | hir::ItemKind::Const(ty, _, _),
566566
span,
567567
..
568568
})

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
@@ -654,6 +654,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
654654
| hir::ItemKind::Impl(hir::Impl { generics, .. })
655655
| hir::ItemKind::Fn(_, generics, _)
656656
| hir::ItemKind::TyAlias(_, generics)
657+
| hir::ItemKind::Const(_, generics, _)
657658
| hir::ItemKind::TraitAlias(generics, _)
658659
| hir::ItemKind::OpaqueTy(hir::OpaqueTy { generics, .. }),
659660
..
@@ -719,6 +720,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
719720
| hir::ItemKind::Impl(hir::Impl { generics, .. })
720721
| hir::ItemKind::Fn(_, generics, _)
721722
| hir::ItemKind::TyAlias(_, generics)
723+
| hir::ItemKind::Const(_, generics, _)
722724
| hir::ItemKind::TraitAlias(generics, _)
723725
| hir::ItemKind::OpaqueTy(hir::OpaqueTy { generics, .. }),
724726
..

src/librustdoc/clean/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2446,7 +2446,8 @@ fn clean_maybe_renamed_item<'tcx>(
24462446
ItemKind::Static(ty, mutability, body_id) => {
24472447
StaticItem(Static { type_: clean_ty(ty, cx), mutability, expr: Some(body_id) })
24482448
}
2449-
ItemKind::Const(ty, body_id) => ConstantItem(Constant {
2449+
// FIXME(fmease): rustdoc integration
2450+
ItemKind::Const(ty, _generics, body_id) => ConstantItem(Constant {
24502451
type_: clean_ty(ty, cx),
24512452
kind: ConstantKind::Local { body: body_id, def_id },
24522453
}),

0 commit comments

Comments
 (0)