提交 62f23c2b 编写于 作者: V varkor

Add Const generic param to ty

Co-Authored-By: NGabriel Smith <yodaldevoid@users.noreply.github.com>
上级 f22dca0a
...@@ -837,7 +837,8 @@ pub enum GenericParamDefKind { ...@@ -837,7 +837,8 @@ pub enum GenericParamDefKind {
has_default: bool, has_default: bool,
object_lifetime_default: ObjectLifetimeDefault, object_lifetime_default: ObjectLifetimeDefault,
synthetic: Option<hir::SyntheticTyParamKind>, synthetic: Option<hir::SyntheticTyParamKind>,
} },
Const,
} }
#[derive(Clone, RustcEncodable, RustcDecodable)] #[derive(Clone, RustcEncodable, RustcDecodable)]
...@@ -880,6 +881,7 @@ pub fn to_bound_region(&self) -> ty::BoundRegion { ...@@ -880,6 +881,7 @@ pub fn to_bound_region(&self) -> ty::BoundRegion {
pub struct GenericParamCount { pub struct GenericParamCount {
pub lifetimes: usize, pub lifetimes: usize,
pub types: usize, pub types: usize,
pub consts: usize,
} }
/// Information about the formal type/lifetime parameters associated /// Information about the formal type/lifetime parameters associated
...@@ -915,6 +917,7 @@ pub fn own_counts(&self) -> GenericParamCount { ...@@ -915,6 +917,7 @@ pub fn own_counts(&self) -> GenericParamCount {
match param.kind { match param.kind {
GenericParamDefKind::Lifetime => own_counts.lifetimes += 1, GenericParamDefKind::Lifetime => own_counts.lifetimes += 1,
GenericParamDefKind::Type { .. } => own_counts.types += 1, GenericParamDefKind::Type { .. } => own_counts.types += 1,
GenericParamDefKind::Const => own_counts.consts += 1,
}; };
} }
...@@ -924,7 +927,7 @@ pub fn own_counts(&self) -> GenericParamCount { ...@@ -924,7 +927,7 @@ pub fn own_counts(&self) -> GenericParamCount {
pub fn requires_monomorphization(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> bool { pub fn requires_monomorphization(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> bool {
for param in &self.params { for param in &self.params {
match param.kind { match param.kind {
GenericParamDefKind::Type { .. } => return true, GenericParamDefKind::Type { .. } | GenericParamDefKind::Const => return true,
GenericParamDefKind::Lifetime => {} GenericParamDefKind::Lifetime => {}
} }
} }
...@@ -944,7 +947,7 @@ pub fn region_param(&'tcx self, ...@@ -944,7 +947,7 @@ pub fn region_param(&'tcx self,
if let Some(index) = param.index.checked_sub(self.parent_count as u32) { if let Some(index) = param.index.checked_sub(self.parent_count as u32) {
let param = &self.params[index as usize]; let param = &self.params[index as usize];
match param.kind { match param.kind {
ty::GenericParamDefKind::Lifetime => param, GenericParamDefKind::Lifetime => param,
_ => bug!("expected lifetime parameter, but found another generic parameter") _ => bug!("expected lifetime parameter, but found another generic parameter")
} }
} else { } else {
...@@ -961,7 +964,7 @@ pub fn type_param(&'tcx self, ...@@ -961,7 +964,7 @@ pub fn type_param(&'tcx self,
if let Some(index) = param.idx.checked_sub(self.parent_count as u32) { if let Some(index) = param.idx.checked_sub(self.parent_count as u32) {
let param = &self.params[index as usize]; let param = &self.params[index as usize];
match param.kind { match param.kind {
ty::GenericParamDefKind::Type {..} => param, GenericParamDefKind::Type { .. } => param,
_ => bug!("expected type parameter, but found another generic parameter") _ => bug!("expected type parameter, but found another generic parameter")
} }
} else { } else {
...@@ -969,6 +972,23 @@ pub fn type_param(&'tcx self, ...@@ -969,6 +972,23 @@ pub fn type_param(&'tcx self,
.type_param(param, tcx) .type_param(param, tcx)
} }
} }
/// Returns the `ConstParameterDef` associated with this `ParamConst`.
pub fn const_param(&'tcx self,
param: &ParamConst,
tcx: TyCtxt<'a, 'gcx, 'tcx>)
-> &GenericParamDef {
if let Some(index) = param.index.checked_sub(self.parent_count as u32) {
let param = &self.params[index as usize];
match param.kind {
GenericParamDefKind::Const => param,
_ => bug!("expected const parameter, but found another generic parameter")
}
} else {
tcx.generics_of(self.parent.expect("parent_count>0 but no parent?"))
.const_param(param, tcx)
}
}
} }
/// Bounds on generics. /// Bounds on generics.
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册