Add has_default to GenericParamDefKind::Const
This currently creates a field which is always false on GenericParamDefKind for future use when consts are permitted to have defaults Update const_generics:default locations Previously just ignored them, now actually do something about them. Fix using type check instead of value Add parsing This adds all the necessary changes to lower const-generics defaults from parsing. Change P<Expr> to AnonConst This matches the arguments passed to instantiations of const generics, and makes it specific to just anonymous constants. Attempt to fix lowering bugs
This commit is contained in:
parent
79e5814f45
commit
e4e5db4e42
39 changed files with 158 additions and 77 deletions
|
@ -2221,7 +2221,7 @@ impl<'tcx> TyCtxt<'tcx> {
|
|||
let adt_def = self.adt_def(wrapper_def_id);
|
||||
let substs =
|
||||
InternalSubsts::for_item(self, wrapper_def_id, |param, substs| match param.kind {
|
||||
GenericParamDefKind::Lifetime | GenericParamDefKind::Const => bug!(),
|
||||
GenericParamDefKind::Lifetime | GenericParamDefKind::Const { .. } => bug!(),
|
||||
GenericParamDefKind::Type { has_default, .. } => {
|
||||
if param.index == 0 {
|
||||
ty_param.into()
|
||||
|
@ -2416,7 +2416,7 @@ impl<'tcx> TyCtxt<'tcx> {
|
|||
self.mk_region(ty::ReEarlyBound(param.to_early_bound_region_data())).into()
|
||||
}
|
||||
GenericParamDefKind::Type { .. } => self.mk_ty_param(param.index, param.name).into(),
|
||||
GenericParamDefKind::Const => {
|
||||
GenericParamDefKind::Const { .. } => {
|
||||
self.mk_const_param(param.index, param.name, self.type_of(param.def_id)).into()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -18,7 +18,9 @@ pub enum GenericParamDefKind {
|
|||
object_lifetime_default: ObjectLifetimeDefault,
|
||||
synthetic: Option<hir::SyntheticTyParamKind>,
|
||||
},
|
||||
Const,
|
||||
Const {
|
||||
has_default: bool,
|
||||
},
|
||||
}
|
||||
|
||||
impl GenericParamDefKind {
|
||||
|
@ -26,14 +28,14 @@ impl GenericParamDefKind {
|
|||
match self {
|
||||
GenericParamDefKind::Lifetime => "lifetime",
|
||||
GenericParamDefKind::Type { .. } => "type",
|
||||
GenericParamDefKind::Const => "constant",
|
||||
GenericParamDefKind::Const { .. } => "constant",
|
||||
}
|
||||
}
|
||||
pub fn to_ord(&self, tcx: TyCtxt<'_>) -> ast::ParamKindOrd {
|
||||
match self {
|
||||
GenericParamDefKind::Lifetime => ast::ParamKindOrd::Lifetime,
|
||||
GenericParamDefKind::Type { .. } => ast::ParamKindOrd::Type,
|
||||
GenericParamDefKind::Const => {
|
||||
GenericParamDefKind::Const { .. } => {
|
||||
ast::ParamKindOrd::Const { unordered: tcx.features().const_generics }
|
||||
}
|
||||
}
|
||||
|
@ -105,7 +107,7 @@ impl<'tcx> Generics {
|
|||
match param.kind {
|
||||
GenericParamDefKind::Lifetime => own_counts.lifetimes += 1,
|
||||
GenericParamDefKind::Type { .. } => own_counts.types += 1,
|
||||
GenericParamDefKind::Const => own_counts.consts += 1,
|
||||
GenericParamDefKind::Const { .. } => own_counts.consts += 1,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -118,12 +120,10 @@ impl<'tcx> Generics {
|
|||
for param in &self.params {
|
||||
match param.kind {
|
||||
GenericParamDefKind::Lifetime => (),
|
||||
GenericParamDefKind::Type { has_default, .. } => {
|
||||
GenericParamDefKind::Type { has_default, .. } |
|
||||
GenericParamDefKind::Const { has_default } => {
|
||||
own_defaults.types += has_default as usize;
|
||||
}
|
||||
GenericParamDefKind::Const => {
|
||||
// FIXME(const_generics:defaults)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -146,7 +146,7 @@ impl<'tcx> Generics {
|
|||
pub fn own_requires_monomorphization(&self) -> bool {
|
||||
for param in &self.params {
|
||||
match param.kind {
|
||||
GenericParamDefKind::Type { .. } | GenericParamDefKind::Const => return true,
|
||||
GenericParamDefKind::Type { .. } | GenericParamDefKind::Const { .. } => return true,
|
||||
GenericParamDefKind::Lifetime => {}
|
||||
}
|
||||
}
|
||||
|
@ -189,7 +189,7 @@ impl<'tcx> Generics {
|
|||
pub fn const_param(&'tcx self, param: &ParamConst, tcx: TyCtxt<'tcx>) -> &GenericParamDef {
|
||||
let param = self.param_at(param.index as usize, tcx);
|
||||
match param.kind {
|
||||
GenericParamDefKind::Const => param,
|
||||
GenericParamDefKind::Const { .. } => param,
|
||||
_ => bug!("expected const parameter, but found another generic parameter"),
|
||||
}
|
||||
}
|
||||
|
|
|
@ -593,7 +593,7 @@ fn polymorphize<'tcx>(
|
|||
},
|
||||
|
||||
// Simple case: If parameter is a const or type parameter..
|
||||
ty::GenericParamDefKind::Const | ty::GenericParamDefKind::Type { .. } if
|
||||
ty::GenericParamDefKind::Const { .. } | ty::GenericParamDefKind::Type { .. } if
|
||||
// ..and is within range and unused..
|
||||
unused.contains(param.index).unwrap_or(false) =>
|
||||
// ..then use the identity for this parameter.
|
||||
|
|
|
@ -193,17 +193,22 @@ pub trait Printer<'tcx>: Sized {
|
|||
.params
|
||||
.iter()
|
||||
.rev()
|
||||
.take_while(|param| {
|
||||
match param.kind {
|
||||
ty::GenericParamDefKind::Lifetime => false,
|
||||
ty::GenericParamDefKind::Type { has_default, .. } => {
|
||||
has_default
|
||||
&& substs[param.index as usize]
|
||||
== GenericArg::from(
|
||||
self.tcx().type_of(param.def_id).subst(self.tcx(), substs),
|
||||
)
|
||||
}
|
||||
ty::GenericParamDefKind::Const => false, // FIXME(const_generics_defaults)
|
||||
.take_while(|param| match param.kind {
|
||||
ty::GenericParamDefKind::Lifetime => false,
|
||||
ty::GenericParamDefKind::Type { has_default, .. } => {
|
||||
has_default
|
||||
&& substs[param.index as usize]
|
||||
== GenericArg::from(
|
||||
self.tcx().type_of(param.def_id).subst(self.tcx(), substs),
|
||||
)
|
||||
}
|
||||
ty::GenericParamDefKind::Const { has_default } => {
|
||||
has_default
|
||||
&& substs[param.index as usize]
|
||||
== GenericArg::from(crate::ty::Const::from_anon_const(
|
||||
self.tcx(),
|
||||
param.def_id.expect_local(),
|
||||
))
|
||||
}
|
||||
})
|
||||
.count();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue