@@ -2721,8 +2721,8 @@ impl<'tcx> ConstantKind<'tcx> {
2721
2721
}
2722
2722
}
2723
2723
2724
- #[ inline]
2725
2724
/// Panics if the value cannot be evaluated or doesn't contain a valid integer of the given type.
2725
+ #[ inline]
2726
2726
pub fn eval_bits ( self , tcx : TyCtxt < ' tcx > , param_env : ty:: ParamEnv < ' tcx > , ty : Ty < ' tcx > ) -> u128 {
2727
2727
self . try_eval_bits ( tcx, param_env, ty)
2728
2728
. unwrap_or_else ( || bug ! ( "expected bits of {:#?}, got {:#?}" , ty, self ) )
@@ -2793,112 +2793,6 @@ impl<'tcx> ConstantKind<'tcx> {
2793
2793
Self :: from_bits ( tcx, n as u128 , ty:: ParamEnv :: empty ( ) . and ( ty) )
2794
2794
}
2795
2795
2796
- #[ instrument( skip( tcx) , level = "debug" ) ]
2797
- pub fn try_eval_lit_or_param (
2798
- tcx : TyCtxt < ' tcx > ,
2799
- ty : Ty < ' tcx > ,
2800
- expr : & ' tcx hir:: Expr < ' tcx > ,
2801
- ) -> Option < Self > {
2802
- // Unwrap a block, so that e.g. `{ P }` is recognised as a parameter. Const arguments
2803
- // currently have to be wrapped in curly brackets, so it's necessary to special-case.
2804
- let expr = match & expr. kind {
2805
- hir:: ExprKind :: Block ( block, _) if block. stmts . is_empty ( ) && block. expr . is_some ( ) => {
2806
- block. expr . as_ref ( ) . unwrap ( )
2807
- }
2808
- _ => expr,
2809
- } ;
2810
-
2811
- let lit_input = match expr. kind {
2812
- hir:: ExprKind :: Lit ( ref lit) => {
2813
- Some ( interpret:: LitToConstInput { lit : & lit. node , ty, neg : false } )
2814
- }
2815
- hir:: ExprKind :: Unary ( hir:: UnOp :: Neg , ref expr) => match expr. kind {
2816
- hir:: ExprKind :: Lit ( ref lit) => {
2817
- Some ( interpret:: LitToConstInput { lit : & lit. node , ty, neg : true } )
2818
- }
2819
- _ => None ,
2820
- } ,
2821
- _ => None ,
2822
- } ;
2823
-
2824
- if let Some ( lit_input) = lit_input {
2825
- // If an error occurred, ignore that it's a literal and leave reporting the error up to
2826
- // mir.
2827
- match tcx. at ( expr. span ) . lit_to_mir_constant ( lit_input) {
2828
- Ok ( c) => return Some ( c) ,
2829
- Err ( e) => {
2830
- tcx. sess . delay_span_bug (
2831
- expr. span ,
2832
- & format ! ( "Const::from_anon_const: couldn't lit_to_const {:?}" , e) ,
2833
- ) ;
2834
- }
2835
- }
2836
- }
2837
- use hir:: { def:: DefKind :: ConstParam , def:: Res , ExprKind , Path , QPath } ;
2838
- match expr. kind {
2839
- ExprKind :: Path ( QPath :: Resolved ( _, & Path { res : Res :: Def ( ConstParam , def_id) , .. } ) ) => {
2840
- // Find the name and index of the const parameter by indexing the generics of
2841
- // the parent item and construct a `ParamConst`.
2842
- let hir_id = tcx. hir ( ) . local_def_id_to_hir_id ( def_id. expect_local ( ) ) ;
2843
- let item_id = tcx. hir ( ) . get_parent_node ( hir_id) ;
2844
- let item_def_id = tcx. hir ( ) . local_def_id ( item_id) ;
2845
- let generics = tcx. generics_of ( item_def_id. to_def_id ( ) ) ;
2846
- let index = generics. param_def_id_to_index [ & def_id] ;
2847
- let name = tcx. hir ( ) . name ( hir_id) ;
2848
- let ty_const = tcx. mk_const ( ty:: ConstS {
2849
- val : ty:: ConstKind :: Param ( ty:: ParamConst :: new ( index, name) ) ,
2850
- ty,
2851
- } ) ;
2852
-
2853
- Some ( Self :: Ty ( ty_const) )
2854
- }
2855
- _ => None ,
2856
- }
2857
- }
2858
-
2859
- #[ instrument( skip( tcx) , level = "debug" ) ]
2860
- pub fn from_inline_const ( tcx : TyCtxt < ' tcx > , def_id : LocalDefId ) -> Self {
2861
- let hir_id = tcx. hir ( ) . local_def_id_to_hir_id ( def_id) ;
2862
-
2863
- let body_id = match tcx. hir ( ) . get ( hir_id) {
2864
- hir:: Node :: AnonConst ( ac) => ac. body ,
2865
- _ => span_bug ! (
2866
- tcx. def_span( def_id. to_def_id( ) ) ,
2867
- "from_inline_const can only process anonymous constants"
2868
- ) ,
2869
- } ;
2870
-
2871
- let expr = & tcx. hir ( ) . body ( body_id) . value ;
2872
-
2873
- let ty = tcx. typeck ( def_id) . node_type ( hir_id) ;
2874
-
2875
- let ret = match Self :: try_eval_lit_or_param ( tcx, ty, expr) {
2876
- Some ( v) => v,
2877
- None => {
2878
- let typeck_root_def_id = tcx. typeck_root_def_id ( def_id. to_def_id ( ) ) ;
2879
- let parent_substs =
2880
- tcx. erase_regions ( InternalSubsts :: identity_for_item ( tcx, typeck_root_def_id) ) ;
2881
- let substs = ty:: InlineConstSubsts :: new (
2882
- tcx,
2883
- ty:: InlineConstSubstsParts { parent_substs, ty } ,
2884
- )
2885
- . substs ;
2886
- let ty_const = tcx. mk_const ( ty:: ConstS {
2887
- val : ty:: ConstKind :: Unevaluated ( ty:: Unevaluated {
2888
- def : ty:: WithOptConstParam :: unknown ( def_id) . to_global ( ) ,
2889
- substs,
2890
- promoted : None ,
2891
- } ) ,
2892
- ty,
2893
- } ) ;
2894
-
2895
- Self :: Ty ( ty_const)
2896
- }
2897
- } ;
2898
- debug_assert ! ( !ret. has_free_regions( ) ) ;
2899
- ret
2900
- }
2901
-
2902
2796
/// Literals are converted to `ConstantKindVal`, const generic parameters are eagerly
2903
2797
/// converted to a constant, everything else becomes `Unevaluated`.
2904
2798
pub fn from_anon_const (
0 commit comments