Skip to content

Commit 62f23c2

Browse files
varkoryodaldevoid
andcommitted
Add Const generic param to ty
Co-Authored-By: Gabriel Smith <[email protected]>
1 parent f22dca0 commit 62f23c2

File tree

1 file changed

+24
-4
lines changed

1 file changed

+24
-4
lines changed

src/librustc/ty/mod.rs

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -837,7 +837,8 @@ pub enum GenericParamDefKind {
837837
has_default: bool,
838838
object_lifetime_default: ObjectLifetimeDefault,
839839
synthetic: Option<hir::SyntheticTyParamKind>,
840-
}
840+
},
841+
Const,
841842
}
842843

843844
#[derive(Clone, RustcEncodable, RustcDecodable)]
@@ -880,6 +881,7 @@ impl GenericParamDef {
880881
pub struct GenericParamCount {
881882
pub lifetimes: usize,
882883
pub types: usize,
884+
pub consts: usize,
883885
}
884886

885887
/// Information about the formal type/lifetime parameters associated
@@ -915,6 +917,7 @@ impl<'a, 'gcx, 'tcx> Generics {
915917
match param.kind {
916918
GenericParamDefKind::Lifetime => own_counts.lifetimes += 1,
917919
GenericParamDefKind::Type { .. } => own_counts.types += 1,
920+
GenericParamDefKind::Const => own_counts.consts += 1,
918921
};
919922
}
920923

@@ -924,7 +927,7 @@ impl<'a, 'gcx, 'tcx> Generics {
924927
pub fn requires_monomorphization(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> bool {
925928
for param in &self.params {
926929
match param.kind {
927-
GenericParamDefKind::Type { .. } => return true,
930+
GenericParamDefKind::Type { .. } | GenericParamDefKind::Const => return true,
928931
GenericParamDefKind::Lifetime => {}
929932
}
930933
}
@@ -944,7 +947,7 @@ impl<'a, 'gcx, 'tcx> Generics {
944947
if let Some(index) = param.index.checked_sub(self.parent_count as u32) {
945948
let param = &self.params[index as usize];
946949
match param.kind {
947-
ty::GenericParamDefKind::Lifetime => param,
950+
GenericParamDefKind::Lifetime => param,
948951
_ => bug!("expected lifetime parameter, but found another generic parameter")
949952
}
950953
} else {
@@ -961,14 +964,31 @@ impl<'a, 'gcx, 'tcx> Generics {
961964
if let Some(index) = param.idx.checked_sub(self.parent_count as u32) {
962965
let param = &self.params[index as usize];
963966
match param.kind {
964-
ty::GenericParamDefKind::Type {..} => param,
967+
GenericParamDefKind::Type { .. } => param,
965968
_ => bug!("expected type parameter, but found another generic parameter")
966969
}
967970
} else {
968971
tcx.generics_of(self.parent.expect("parent_count > 0 but no parent?"))
969972
.type_param(param, tcx)
970973
}
971974
}
975+
976+
/// Returns the `ConstParameterDef` associated with this `ParamConst`.
977+
pub fn const_param(&'tcx self,
978+
param: &ParamConst,
979+
tcx: TyCtxt<'a, 'gcx, 'tcx>)
980+
-> &GenericParamDef {
981+
if let Some(index) = param.index.checked_sub(self.parent_count as u32) {
982+
let param = &self.params[index as usize];
983+
match param.kind {
984+
GenericParamDefKind::Const => param,
985+
_ => bug!("expected const parameter, but found another generic parameter")
986+
}
987+
} else {
988+
tcx.generics_of(self.parent.expect("parent_count>0 but no parent?"))
989+
.const_param(param, tcx)
990+
}
991+
}
972992
}
973993

974994
/// Bounds on generics.

0 commit comments

Comments
 (0)