Skip to content

Commit e4ad188

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 c405e43 commit e4ad188

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
@@ -1551,15 +1551,15 @@ impl<'hir> LoweringContext<'_, 'hir> {
15511551

15521552
if let Some((span, hir_id, def_id)) = host_param_parts {
15531553
let const_node_id = self.next_node_id();
1554-
let anon_const =
1554+
let anon_const_did =
15551555
self.create_def(def_id, const_node_id, kw::Empty, DefKind::AnonConst, span);
15561556

15571557
let const_id = self.next_id();
15581558
let const_expr_id = self.next_id();
15591559
let bool_id = self.next_id();
15601560

15611561
self.children.push((def_id, hir::MaybeOwner::NonOwner(hir_id)));
1562-
self.children.push((anon_const, hir::MaybeOwner::NonOwner(const_id)));
1562+
self.children.push((anon_const_did, hir::MaybeOwner::NonOwner(const_id)));
15631563

15641564
let const_body = self.lower_body(|this| {
15651565
(
@@ -1574,6 +1574,17 @@ impl<'hir> LoweringContext<'_, 'hir> {
15741574
)
15751575
});
15761576

1577+
let default_ac = self.arena.alloc(hir::AnonConst {
1578+
def_id: anon_const_did,
1579+
hir_id: const_id,
1580+
body: const_body,
1581+
span,
1582+
});
1583+
let default_ct = self.arena.alloc(hir::ConstArg {
1584+
hir_id: self.next_id(),
1585+
kind: hir::ConstArgKind::Anon(default_ac),
1586+
is_desugared_from_effects: true,
1587+
});
15771588
let param = hir::GenericParam {
15781589
def_id,
15791590
hir_id,
@@ -1597,12 +1608,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
15971608
}),
15981609
)),
15991610
)),
1600-
default: Some(self.arena.alloc(hir::AnonConst {
1601-
def_id: anon_const,
1602-
hir_id: const_id,
1603-
body: const_body,
1604-
span,
1605-
})),
1611+
default: Some(default_ct),
16061612
is_host_effect: true,
16071613
},
16081614
colon_span: None,

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2207,7 +2207,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
22072207
false
22082208
}
22092209
})
2210-
.map(|def| self.lower_anon_const(def));
2210+
.map(|def| self.lower_anon_const_as_const_arg(def));
22112211

22122212
(
22132213
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
@@ -364,7 +364,7 @@ pub trait Visitor<'v>: Sized {
364364
fn visit_generic_param(&mut self, p: &'v GenericParam<'v>) -> Self::Result {
365365
walk_generic_param(self, p)
366366
}
367-
fn visit_const_param_default(&mut self, _param: HirId, ct: &'v AnonConst) -> Self::Result {
367+
fn visit_const_param_default(&mut self, _param: HirId, ct: &'v ConstArg<'v>) -> Self::Result {
368368
walk_const_param_default(self, ct)
369369
}
370370
fn visit_generics(&mut self, g: &'v Generics<'v>) -> Self::Result {
@@ -933,9 +933,9 @@ pub fn walk_generic_param<'v, V: Visitor<'v>>(
933933

934934
pub fn walk_const_param_default<'v, V: Visitor<'v>>(
935935
visitor: &mut V,
936-
ct: &'v AnonConst,
936+
ct: &'v ConstArg<'v>,
937937
) -> V::Result {
938-
visitor.visit_anon_const(ct)
938+
visitor.visit_const_arg(ct)
939939
}
940940

941941
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
@@ -358,7 +358,7 @@ fn const_evaluatable_predicates_of(
358358
}
359359
}
360360

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

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
@@ -2140,7 +2140,7 @@ impl<'a> State<'a> {
21402140
if let Some(default) = default {
21412141
self.space();
21422142
self.word_space("=");
2143-
self.print_anon_const(default);
2143+
self.print_const_arg(default);
21442144
}
21452145
}
21462146
}

compiler/rustc_middle/src/ty/consts.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -541,15 +541,15 @@ pub fn const_param_default<'tcx>(
541541
tcx: TyCtxt<'tcx>,
542542
def_id: LocalDefId,
543543
) -> ty::EarlyBinder<'tcx, Const<'tcx>> {
544-
let default_def_id = match tcx.hir_node_by_def_id(def_id) {
544+
let default_ct = match tcx.hir_node_by_def_id(def_id) {
545545
hir::Node::GenericParam(hir::GenericParam {
546-
kind: hir::GenericParamKind::Const { default: Some(ac), .. },
546+
kind: hir::GenericParamKind::Const { default: Some(ct), .. },
547547
..
548-
}) => ac.def_id,
548+
}) => ct,
549549
_ => span_bug!(
550550
tcx.def_span(def_id),
551551
"`const_param_default` expected a generic parameter with a constant"
552552
),
553553
};
554-
ty::EarlyBinder::bind(Const::from_anon_const(tcx, default_def_id))
554+
ty::EarlyBinder::bind(Const::from_const_arg_without_feeding(tcx, default_ct))
555555
}

src/librustdoc/clean/mod.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -669,8 +669,9 @@ fn clean_generic_param<'tcx>(
669669
param.name.ident().name,
670670
GenericParamDefKind::Const {
671671
ty: Box::new(clean_ty(ty, cx)),
672-
default: default
673-
.map(|ct| Box::new(ty::Const::from_anon_const(cx.tcx, ct.def_id).to_string())),
672+
default: default.map(|ct| {
673+
Box::new(ty::Const::from_const_arg_without_feeding(cx.tcx, ct).to_string())
674+
}),
674675
is_host_effect,
675676
},
676677
),

0 commit comments

Comments
 (0)