Skip to content

Commit 1c49d40

Browse files
committed
Use ConstArg for const param defaults
Now everything that actually affects the type system (i.e., excluding const blocks, enum variant discriminants, etc.) *should* be using `ConstArg`.
1 parent 67fccb7 commit 1c49d40

File tree

12 files changed

+34
-26
lines changed

12 files changed

+34
-26
lines changed

compiler/rustc_ast_lowering/src/index.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -181,7 +181,7 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
181181
intravisit::walk_generic_param(self, param);
182182
}
183183

184-
fn visit_const_param_default(&mut self, param: HirId, ct: &'hir AnonConst) {
184+
fn visit_const_param_default(&mut self, param: HirId, ct: &'hir ConstArg<'hir>) {
185185
self.with_parent(param, |this| {
186186
intravisit::walk_const_param_default(this, ct);
187187
})

compiler/rustc_ast_lowering/src/item.rs

+13-9
Original file line numberDiff line numberDiff line change
@@ -1601,15 +1601,15 @@ impl<'hir> LoweringContext<'_, 'hir> {
16011601

16021602
if let Some((span, hir_id, def_id)) = host_param_parts {
16031603
let const_node_id = self.next_node_id();
1604-
let anon_const =
1604+
let anon_const_did =
16051605
self.create_def(def_id, const_node_id, kw::Empty, DefKind::AnonConst, span);
16061606

16071607
let const_id = self.next_id();
16081608
let const_expr_id = self.next_id();
16091609
let bool_id = self.next_id();
16101610

16111611
self.children.push((def_id, hir::MaybeOwner::NonOwner(hir_id)));
1612-
self.children.push((anon_const, hir::MaybeOwner::NonOwner(const_id)));
1612+
self.children.push((anon_const_did, hir::MaybeOwner::NonOwner(const_id)));
16131613

16141614
let const_body = self.lower_body(|this| {
16151615
(
@@ -1624,6 +1624,16 @@ impl<'hir> LoweringContext<'_, 'hir> {
16241624
)
16251625
});
16261626

1627+
let default_ac = self.arena.alloc(hir::AnonConst {
1628+
def_id: anon_const_did,
1629+
hir_id: const_id,
1630+
body: const_body,
1631+
span,
1632+
});
1633+
let default_ct = self.arena.alloc(hir::ConstArg {
1634+
kind: hir::ConstArgKind::Anon(default_ac),
1635+
is_desugared_from_effects: true,
1636+
});
16271637
let param = hir::GenericParam {
16281638
def_id,
16291639
hir_id,
@@ -1647,13 +1657,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
16471657
}),
16481658
)),
16491659
)),
1650-
// FIXME(effects) we might not need a default.
1651-
default: Some(self.arena.alloc(hir::AnonConst {
1652-
def_id: anon_const,
1653-
hir_id: const_id,
1654-
body: const_body,
1655-
span,
1656-
})),
1660+
default: Some(default_ct),
16571661
is_host_effect: true,
16581662
synthetic: true,
16591663
},

compiler/rustc_ast_lowering/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2204,7 +2204,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
22042204
false
22052205
}
22062206
})
2207-
.map(|def| self.lower_anon_const_to_anon_const(def));
2207+
.map(|def| self.lower_anon_const_to_const_arg(def));
22082208

22092209
(
22102210
hir::ParamName::Plain(self.lower_ident(param.ident)),

compiler/rustc_hir/src/hir.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,7 @@ pub enum GenericParamKind<'hir> {
548548
Const {
549549
ty: &'hir Ty<'hir>,
550550
/// Optional default value for the const generic param
551-
default: Option<&'hir AnonConst>,
551+
default: Option<&'hir ConstArg<'hir>>,
552552
is_host_effect: bool,
553553
synthetic: bool,
554554
},

compiler/rustc_hir/src/intravisit.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ pub trait Visitor<'v>: Sized {
367367
fn visit_generic_param(&mut self, p: &'v GenericParam<'v>) -> Self::Result {
368368
walk_generic_param(self, p)
369369
}
370-
fn visit_const_param_default(&mut self, _param: HirId, ct: &'v AnonConst) -> Self::Result {
370+
fn visit_const_param_default(&mut self, _param: HirId, ct: &'v ConstArg<'v>) -> Self::Result {
371371
walk_const_param_default(self, ct)
372372
}
373373
fn visit_generics(&mut self, g: &'v Generics<'v>) -> Self::Result {
@@ -940,9 +940,9 @@ pub fn walk_generic_param<'v, V: Visitor<'v>>(
940940

941941
pub fn walk_const_param_default<'v, V: Visitor<'v>>(
942942
visitor: &mut V,
943-
ct: &'v AnonConst,
943+
ct: &'v ConstArg<'v>,
944944
) -> V::Result {
945-
visitor.visit_anon_const(ct)
945+
visitor.visit_const_arg(ct)
946946
}
947947

948948
pub fn walk_generics<'v, V: Visitor<'v>>(visitor: &mut V, generics: &'v Generics<'v>) -> V::Result {

compiler/rustc_hir_analysis/src/collect.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,10 @@ impl<'tcx> Visitor<'tcx> for CollectItemTypesVisitor<'tcx> {
304304
self.tcx.ensure().type_of(param.def_id);
305305
if let Some(default) = default {
306306
// need to store default and type of default
307-
self.tcx.ensure().type_of(default.def_id);
307+
#[allow(irrefutable_let_patterns)] // FIXME
308+
if let hir::ConstArgKind::Anon(ac) = default.kind {
309+
self.tcx.ensure().type_of(ac.def_id);
310+
}
308311
self.tcx.ensure().const_param_default(param.def_id);
309312
}
310313
}

compiler/rustc_hir_analysis/src/collect/predicates_of.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -388,7 +388,7 @@ fn const_evaluatable_predicates_of(
388388
}
389389
}
390390

391-
fn visit_const_param_default(&mut self, _param: HirId, _ct: &'tcx hir::AnonConst) {
391+
fn visit_const_param_default(&mut self, _param: HirId, _ct: &'tcx hir::ConstArg<'tcx>) {
392392
// Do not look into const param defaults,
393393
// these get checked when they are actually instantiated.
394394
//

compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -954,7 +954,7 @@ impl<'a, 'tcx> Visitor<'tcx> for BoundVarContext<'a, 'tcx> {
954954
GenericParamKind::Const { ty, default, .. } => {
955955
self.visit_ty(ty);
956956
if let Some(default) = default {
957-
self.visit_body(self.tcx.hir().body(default.body));
957+
self.visit_const_arg(default);
958958
}
959959
}
960960
}

compiler/rustc_hir_analysis/src/collect/type_of.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ fn anon_const_type_of<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> Ty<'tcx> {
7373
def_id: param_def_id,
7474
kind: GenericParamKind::Const { default: Some(ct), .. },
7575
..
76-
}) if ct.hir_id == hir_id => {
76+
}) if ct.anon_const_hir_id() == hir_id => {
7777
return tcx
7878
.type_of(param_def_id)
7979
.no_bound_vars()

compiler/rustc_hir_pretty/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2161,7 +2161,7 @@ impl<'a> State<'a> {
21612161
if let Some(default) = default {
21622162
self.space();
21632163
self.word_space("=");
2164-
self.print_anon_const(default);
2164+
self.print_const_arg(default);
21652165
}
21662166
}
21672167
}

compiler/rustc_middle/src/ty/consts.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -505,15 +505,15 @@ pub fn const_param_default<'tcx>(
505505
tcx: TyCtxt<'tcx>,
506506
def_id: LocalDefId,
507507
) -> ty::EarlyBinder<'tcx, Const<'tcx>> {
508-
let default_def_id = match tcx.hir_node_by_def_id(def_id) {
508+
let default_ct = match tcx.hir_node_by_def_id(def_id) {
509509
hir::Node::GenericParam(hir::GenericParam {
510-
kind: hir::GenericParamKind::Const { default: Some(ac), .. },
510+
kind: hir::GenericParamKind::Const { default: Some(ct), .. },
511511
..
512-
}) => ac.def_id,
512+
}) => ct,
513513
_ => span_bug!(
514514
tcx.def_span(def_id),
515515
"`const_param_default` expected a generic parameter with a constant"
516516
),
517517
};
518-
ty::EarlyBinder::bind(Const::from_anon_const(tcx, default_def_id))
518+
ty::EarlyBinder::bind(Const::from_const_arg(tcx, default_ct, FeedConstTy::No))
519519
}

src/librustdoc/clean/mod.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -635,8 +635,9 @@ fn clean_generic_param<'tcx>(
635635
param.name.ident().name,
636636
GenericParamDefKind::Const {
637637
ty: Box::new(clean_ty(ty, cx)),
638-
default: default
639-
.map(|ct| Box::new(ty::Const::from_anon_const(cx.tcx, ct.def_id).to_string())),
638+
default: default.map(|ct| {
639+
Box::new(ty::Const::from_const_arg(cx.tcx, ct, ty::FeedConstTy::No).to_string())
640+
}),
640641
synthetic,
641642
},
642643
),

0 commit comments

Comments
 (0)