Skip to content

Commit 7f4b594

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 0ee891d commit 7f4b594

File tree

11 files changed

+41
-24
lines changed

11 files changed

+41
-24
lines changed

compiler/rustc_ast_lowering/src/index.rs

Lines changed: 1 addition & 1 deletion
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

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1560,15 +1560,15 @@ impl<'hir> LoweringContext<'_, 'hir> {
15601560

15611561
if let Some((span, hir_id, def_id)) = host_param_parts {
15621562
let const_node_id = self.next_node_id();
1563-
let anon_const =
1563+
let anon_const_did =
15641564
self.create_def(def_id, const_node_id, kw::Empty, DefKind::AnonConst, span);
15651565

15661566
let const_id = self.next_id();
15671567
let const_expr_id = self.next_id();
15681568
let bool_id = self.next_id();
15691569

15701570
self.children.push((def_id, hir::MaybeOwner::NonOwner(hir_id)));
1571-
self.children.push((anon_const, hir::MaybeOwner::NonOwner(const_id)));
1571+
self.children.push((anon_const_did, hir::MaybeOwner::NonOwner(const_id)));
15721572

15731573
let const_body = self.lower_body(|this| {
15741574
(
@@ -1583,6 +1583,17 @@ impl<'hir> LoweringContext<'_, 'hir> {
15831583
)
15841584
});
15851585

1586+
let default_ac = self.arena.alloc(hir::AnonConst {
1587+
def_id: anon_const_did,
1588+
hir_id: const_id,
1589+
body: const_body,
1590+
span,
1591+
});
1592+
let default_ct = self.arena.alloc(hir::ConstArg {
1593+
hir_id: self.next_id(),
1594+
kind: hir::ConstArgKind::Anon(default_ac),
1595+
is_desugared_from_effects: true,
1596+
});
15861597
let param = hir::GenericParam {
15871598
def_id,
15881599
hir_id,
@@ -1606,12 +1617,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
16061617
}),
16071618
)),
16081619
)),
1609-
default: Some(self.arena.alloc(hir::AnonConst {
1610-
def_id: anon_const,
1611-
hir_id: const_id,
1612-
body: const_body,
1613-
span,
1614-
})),
1620+
default: Some(default_ct),
16151621
is_host_effect: true,
16161622
},
16171623
colon_span: None,

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2202,7 +2202,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
22022202
false
22032203
}
22042204
})
2205-
.map(|def| self.lower_anon_const(def));
2205+
.map(|def| self.lower_anon_const_as_const_arg(def));
22062206

22072207
(
22082208
hir::ParamName::Plain(self.lower_ident(param.ident)),

compiler/rustc_hir/src/hir.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ pub enum GenericParamKind<'hir> {
539539
Const {
540540
ty: &'hir Ty<'hir>,
541541
/// Optional default value for the const generic param
542-
default: Option<&'hir AnonConst>,
542+
default: Option<&'hir ConstArg<'hir>>,
543543
is_host_effect: bool,
544544
},
545545
}

compiler/rustc_hir/src/intravisit.rs

Lines changed: 3 additions & 3 deletions
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 {
@@ -946,9 +946,9 @@ pub fn walk_generic_param<'v, V: Visitor<'v>>(
946946

947947
pub fn walk_const_param_default<'v, V: Visitor<'v>>(
948948
visitor: &mut V,
949-
ct: &'v AnonConst,
949+
ct: &'v ConstArg<'v>,
950950
) -> V::Result {
951-
visitor.visit_anon_const(ct)
951+
visitor.visit_const_arg(ct)
952952
}
953953

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

compiler/rustc_hir_analysis/src/collect.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,17 @@ impl<'tcx> Visitor<'tcx> for CollectItemTypesVisitor<'tcx> {
295295
self.tcx.ensure().type_of(param.def_id);
296296
if let Some(default) = default {
297297
// need to store default and type of default
298-
self.tcx.ensure().type_of(default.def_id);
298+
let default_did = match &default.kind {
299+
hir::ConstArgKind::Path(hir::QPath::Resolved(
300+
None,
301+
hir::Path { res, .. },
302+
)) => res.def_id(),
303+
hir::ConstArgKind::Path(qpath) => {
304+
bug!("invalid path for const arg: {qpath:?}")
305+
}
306+
hir::ConstArgKind::Anon(ac) => ac.def_id.into(),
307+
};
308+
self.tcx.ensure().type_of(default_did);
299309
self.tcx.ensure().const_param_default(param.def_id);
300310
}
301311
}

compiler/rustc_hir_analysis/src/collect/predicates_of.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ fn const_evaluatable_predicates_of(
360360
}
361361
}
362362

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

compiler/rustc_hir_analysis/src/collect/resolve_bound_vars.rs

Lines changed: 1 addition & 1 deletion
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, is_host_effect: _ } => {
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_pretty/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2142,7 +2142,7 @@ impl<'a> State<'a> {
21422142
if let Some(default) = default {
21432143
self.space();
21442144
self.word_space("=");
2145-
self.print_anon_const(default);
2145+
self.print_const_arg(default);
21462146
}
21472147
}
21482148
}

compiler/rustc_middle/src/ty/consts.rs

Lines changed: 4 additions & 4 deletions
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_without_feeding(tcx, default_ct))
519519
}

src/librustdoc/clean/mod.rs

Lines changed: 3 additions & 2 deletions
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_without_feeding(cx.tcx, ct).to_string())
640+
}),
640641
is_host_effect,
641642
},
642643
),

0 commit comments

Comments
 (0)