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`.
This commit is contained in:
Noah Lev 2024-06-07 20:26:50 -07:00
parent 67fccb7045
commit 1c49d406b6
12 changed files with 34 additions and 26 deletions

View File

@ -181,7 +181,7 @@ impl<'a, 'hir> Visitor<'hir> for NodeCollector<'a, 'hir> {
intravisit::walk_generic_param(self, param);
}
fn visit_const_param_default(&mut self, param: HirId, ct: &'hir AnonConst) {
fn visit_const_param_default(&mut self, param: HirId, ct: &'hir ConstArg<'hir>) {
self.with_parent(param, |this| {
intravisit::walk_const_param_default(this, ct);
})

View File

@ -1601,7 +1601,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
if let Some((span, hir_id, def_id)) = host_param_parts {
let const_node_id = self.next_node_id();
let anon_const =
let anon_const_did =
self.create_def(def_id, const_node_id, kw::Empty, DefKind::AnonConst, span);
let const_id = self.next_id();
@ -1609,7 +1609,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
let bool_id = self.next_id();
self.children.push((def_id, hir::MaybeOwner::NonOwner(hir_id)));
self.children.push((anon_const, hir::MaybeOwner::NonOwner(const_id)));
self.children.push((anon_const_did, hir::MaybeOwner::NonOwner(const_id)));
let const_body = self.lower_body(|this| {
(
@ -1624,6 +1624,16 @@ impl<'hir> LoweringContext<'_, 'hir> {
)
});
let default_ac = self.arena.alloc(hir::AnonConst {
def_id: anon_const_did,
hir_id: const_id,
body: const_body,
span,
});
let default_ct = self.arena.alloc(hir::ConstArg {
kind: hir::ConstArgKind::Anon(default_ac),
is_desugared_from_effects: true,
});
let param = hir::GenericParam {
def_id,
hir_id,
@ -1647,13 +1657,7 @@ impl<'hir> LoweringContext<'_, 'hir> {
}),
)),
)),
// FIXME(effects) we might not need a default.
default: Some(self.arena.alloc(hir::AnonConst {
def_id: anon_const,
hir_id: const_id,
body: const_body,
span,
})),
default: Some(default_ct),
is_host_effect: true,
synthetic: true,
},

View File

@ -2204,7 +2204,7 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
false
}
})
.map(|def| self.lower_anon_const_to_anon_const(def));
.map(|def| self.lower_anon_const_to_const_arg(def));
(
hir::ParamName::Plain(self.lower_ident(param.ident)),

View File

@ -548,7 +548,7 @@ pub enum GenericParamKind<'hir> {
Const {
ty: &'hir Ty<'hir>,
/// Optional default value for the const generic param
default: Option<&'hir AnonConst>,
default: Option<&'hir ConstArg<'hir>>,
is_host_effect: bool,
synthetic: bool,
},

View File

@ -367,7 +367,7 @@ pub trait Visitor<'v>: Sized {
fn visit_generic_param(&mut self, p: &'v GenericParam<'v>) -> Self::Result {
walk_generic_param(self, p)
}
fn visit_const_param_default(&mut self, _param: HirId, ct: &'v AnonConst) -> Self::Result {
fn visit_const_param_default(&mut self, _param: HirId, ct: &'v ConstArg<'v>) -> Self::Result {
walk_const_param_default(self, ct)
}
fn visit_generics(&mut self, g: &'v Generics<'v>) -> Self::Result {
@ -940,9 +940,9 @@ pub fn walk_generic_param<'v, V: Visitor<'v>>(
pub fn walk_const_param_default<'v, V: Visitor<'v>>(
visitor: &mut V,
ct: &'v AnonConst,
ct: &'v ConstArg<'v>,
) -> V::Result {
visitor.visit_anon_const(ct)
visitor.visit_const_arg(ct)
}
pub fn walk_generics<'v, V: Visitor<'v>>(visitor: &mut V, generics: &'v Generics<'v>) -> V::Result {

View File

@ -304,7 +304,10 @@ impl<'tcx> Visitor<'tcx> for CollectItemTypesVisitor<'tcx> {
self.tcx.ensure().type_of(param.def_id);
if let Some(default) = default {
// need to store default and type of default
self.tcx.ensure().type_of(default.def_id);
#[allow(irrefutable_let_patterns)] // FIXME
if let hir::ConstArgKind::Anon(ac) = default.kind {
self.tcx.ensure().type_of(ac.def_id);
}
self.tcx.ensure().const_param_default(param.def_id);
}
}

View File

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

View File

@ -954,7 +954,7 @@ impl<'a, 'tcx> Visitor<'tcx> for BoundVarContext<'a, 'tcx> {
GenericParamKind::Const { ty, default, .. } => {
self.visit_ty(ty);
if let Some(default) = default {
self.visit_body(self.tcx.hir().body(default.body));
self.visit_const_arg(default);
}
}
}

View File

@ -73,7 +73,7 @@ fn anon_const_type_of<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> Ty<'tcx> {
def_id: param_def_id,
kind: GenericParamKind::Const { default: Some(ct), .. },
..
}) if ct.hir_id == hir_id => {
}) if ct.anon_const_hir_id() == hir_id => {
return tcx
.type_of(param_def_id)
.no_bound_vars()

View File

@ -2161,7 +2161,7 @@ impl<'a> State<'a> {
if let Some(default) = default {
self.space();
self.word_space("=");
self.print_anon_const(default);
self.print_const_arg(default);
}
}
}

View File

@ -505,15 +505,15 @@ pub fn const_param_default<'tcx>(
tcx: TyCtxt<'tcx>,
def_id: LocalDefId,
) -> ty::EarlyBinder<'tcx, Const<'tcx>> {
let default_def_id = match tcx.hir_node_by_def_id(def_id) {
let default_ct = match tcx.hir_node_by_def_id(def_id) {
hir::Node::GenericParam(hir::GenericParam {
kind: hir::GenericParamKind::Const { default: Some(ac), .. },
kind: hir::GenericParamKind::Const { default: Some(ct), .. },
..
}) => ac.def_id,
}) => ct,
_ => span_bug!(
tcx.def_span(def_id),
"`const_param_default` expected a generic parameter with a constant"
),
};
ty::EarlyBinder::bind(Const::from_anon_const(tcx, default_def_id))
ty::EarlyBinder::bind(Const::from_const_arg(tcx, default_ct, FeedConstTy::No))
}

View File

@ -635,8 +635,9 @@ fn clean_generic_param<'tcx>(
param.name.ident().name,
GenericParamDefKind::Const {
ty: Box::new(clean_ty(ty, cx)),
default: default
.map(|ct| Box::new(ty::Const::from_anon_const(cx.tcx, ct.def_id).to_string())),
default: default.map(|ct| {
Box::new(ty::Const::from_const_arg(cx.tcx, ct, ty::FeedConstTy::No).to_string())
}),
synthetic,
},
),