@@ -5,7 +5,7 @@ use crate::ty::{self, Ty, TyCtxt};
5
5
use crate :: ty:: { ParamEnv , ParamEnvAnd } ;
6
6
use rustc_errors:: ErrorReported ;
7
7
use rustc_hir as hir;
8
- use rustc_hir:: def_id:: LocalDefId ;
8
+ use rustc_hir:: def_id:: { DefId , LocalDefId } ;
9
9
use rustc_macros:: HashStable ;
10
10
11
11
mod int;
@@ -44,11 +44,7 @@ impl<'tcx> Const<'tcx> {
44
44
let hir_id = tcx. hir ( ) . local_def_id_to_hir_id ( def. did ) ;
45
45
46
46
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 ,
52
48
_ => span_bug ! (
53
49
tcx. def_span( def. did. to_def_id( ) ) ,
54
50
"from_anon_const can only process anonymous constants"
@@ -206,3 +202,19 @@ impl<'tcx> Const<'tcx> {
206
202
. unwrap_or_else ( || bug ! ( "expected usize, got {:#?}" , self ) )
207
203
}
208
204
}
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