@@ -5,7 +5,7 @@ use crate::ty::{self, Ty, TyCtxt};
55use crate :: ty:: { ParamEnv , ParamEnvAnd } ;
66use rustc_errors:: ErrorReported ;
77use rustc_hir as hir;
8- use rustc_hir:: def_id:: LocalDefId ;
8+ use rustc_hir:: def_id:: { DefId , LocalDefId } ;
99use rustc_macros:: HashStable ;
1010
1111mod int;
@@ -44,11 +44,7 @@ impl<'tcx> Const<'tcx> {
4444 let hir_id = tcx. hir ( ) . local_def_id_to_hir_id ( def. did ) ;
4545
4646 let body_id = match tcx. hir ( ) . get ( hir_id) {
47- hir:: Node :: AnonConst ( ac)
48- | hir:: Node :: GenericParam ( hir:: GenericParam {
49- kind : hir:: GenericParamKind :: Const { ty : _, default : Some ( ac) } ,
50- ..
51- } ) => ac. body ,
47+ hir:: Node :: AnonConst ( ac) => ac. body ,
5248 _ => span_bug ! (
5349 tcx. def_span( def. did. to_def_id( ) ) ,
5450 "from_anon_const can only process anonymous constants"
@@ -206,3 +202,19 @@ impl<'tcx> Const<'tcx> {
206202 . unwrap_or_else ( || bug ! ( "expected usize, got {:#?}" , self ) )
207203 }
208204}
205+
206+ pub fn const_param_default < ' tcx > ( tcx : TyCtxt < ' tcx > , def_id : DefId ) -> & ' tcx Const < ' tcx > {
207+ let hir_id = tcx. hir ( ) . local_def_id_to_hir_id ( def_id. expect_local ( ) ) ;
208+ let default_def_id = match tcx. hir ( ) . get ( hir_id) {
209+ hir:: Node :: AnonConst ( ac)
210+ | hir:: Node :: GenericParam ( hir:: GenericParam {
211+ kind : hir:: GenericParamKind :: Const { ty : _, default : Some ( ac) } ,
212+ ..
213+ } ) => tcx. hir ( ) . local_def_id ( ac. hir_id ) ,
214+ _ => span_bug ! (
215+ tcx. def_span( def_id) ,
216+ "const_param_defaults expected a generic parameter with a constant"
217+ ) ,
218+ } ;
219+ Const :: from_anon_const ( tcx, default_def_id)
220+ }
0 commit comments