@@ -2649,24 +2649,31 @@ impl Const {
2649
2649
Type :: from_value_def ( db, self . id )
2650
2650
}
2651
2651
2652
- /// Evaluate the constant and return the result as a string.
2653
- ///
2654
- /// This function is intended for IDE assistance, different from [`Const::render_eval`].
2655
- pub fn eval ( self , db : & dyn HirDatabase ) -> Result < String , ConstEvalError > {
2656
- let c = db. const_eval ( self . id . into ( ) , Substitution :: empty ( Interner ) , None ) ?;
2657
- Ok ( format ! ( "{}" , c. display( db, self . krate( db) . edition( db) ) ) )
2652
+ /// Evaluate the constant.
2653
+ pub fn eval ( self , db : & dyn HirDatabase ) -> Result < EvaluatedConst , ConstEvalError > {
2654
+ db. const_eval ( self . id . into ( ) , Substitution :: empty ( Interner ) , None )
2655
+ . map ( |it| EvaluatedConst { const_ : it, def : self . id . into ( ) } )
2658
2656
}
2657
+ }
2659
2658
2660
- /// Evaluate the constant and return the result as a string, with more detailed information.
2661
- ///
2662
- /// This function is intended for user-facing display.
2663
- pub fn render_eval (
2664
- self ,
2665
- db : & dyn HirDatabase ,
2666
- edition : Edition ,
2667
- ) -> Result < String , ConstEvalError > {
2668
- let c = db. const_eval ( self . id . into ( ) , Substitution :: empty ( Interner ) , None ) ?;
2669
- let data = & c. data ( Interner ) ;
2659
+ impl HasVisibility for Const {
2660
+ fn visibility ( & self , db : & dyn HirDatabase ) -> Visibility {
2661
+ db. const_visibility ( self . id )
2662
+ }
2663
+ }
2664
+
2665
+ pub struct EvaluatedConst {
2666
+ def : DefWithBodyId ,
2667
+ const_ : hir_ty:: Const ,
2668
+ }
2669
+
2670
+ impl EvaluatedConst {
2671
+ pub fn render ( & self , db : & dyn HirDatabase , edition : Edition ) -> String {
2672
+ format ! ( "{}" , self . const_. display( db, edition) )
2673
+ }
2674
+
2675
+ pub fn render_debug ( & self , db : & dyn HirDatabase ) -> Result < String , MirEvalError > {
2676
+ let data = self . const_ . data ( Interner ) ;
2670
2677
if let TyKind :: Scalar ( s) = data. ty . kind ( Interner ) {
2671
2678
if matches ! ( s, Scalar :: Int ( _) | Scalar :: Uint ( _) ) {
2672
2679
if let hir_ty:: ConstValue :: Concrete ( c) = & data. value {
@@ -2689,17 +2696,7 @@ impl Const {
2689
2696
}
2690
2697
}
2691
2698
}
2692
- if let Ok ( s) = mir:: render_const_using_debug_impl ( db, self . id . into ( ) , & c) {
2693
- Ok ( s)
2694
- } else {
2695
- Ok ( format ! ( "{}" , c. display( db, edition) ) )
2696
- }
2697
- }
2698
- }
2699
-
2700
- impl HasVisibility for Const {
2701
- fn visibility ( & self , db : & dyn HirDatabase ) -> Visibility {
2702
- db. const_visibility ( self . id )
2699
+ mir:: render_const_using_debug_impl ( db, self . def , & self . const_ )
2703
2700
}
2704
2701
}
2705
2702
@@ -2729,51 +2726,10 @@ impl Static {
2729
2726
Type :: from_value_def ( db, self . id )
2730
2727
}
2731
2728
2732
- /// Evaluate the static and return the result as a string.
2733
- ///
2734
- /// This function is intended for IDE assistance, different from [`Static::render_eval`].
2735
- pub fn eval ( self , db : & dyn HirDatabase ) -> Result < String , ConstEvalError > {
2736
- let c = db. const_eval ( self . id . into ( ) , Substitution :: empty ( Interner ) , None ) ?;
2737
- Ok ( format ! ( "{}" , c. display( db, self . krate( db) . edition( db) ) ) )
2738
- }
2739
-
2740
- /// Evaluate the static and return the result as a string, with more detailed information.
2741
- ///
2742
- /// This function is intended for user-facing display.
2743
- pub fn render_eval (
2744
- self ,
2745
- db : & dyn HirDatabase ,
2746
- edition : Edition ,
2747
- ) -> Result < String , ConstEvalError > {
2748
- let c = db. const_eval ( self . id . into ( ) , Substitution :: empty ( Interner ) , None ) ?;
2749
- let data = & c. data ( Interner ) ;
2750
- if let TyKind :: Scalar ( s) = data. ty . kind ( Interner ) {
2751
- if matches ! ( s, Scalar :: Int ( _) | Scalar :: Uint ( _) ) {
2752
- if let hir_ty:: ConstValue :: Concrete ( c) = & data. value {
2753
- if let hir_ty:: ConstScalar :: Bytes ( b, _) = & c. interned {
2754
- let value = u128:: from_le_bytes ( mir:: pad16 ( b, false ) ) ;
2755
- let value_signed =
2756
- i128:: from_le_bytes ( mir:: pad16 ( b, matches ! ( s, Scalar :: Int ( _) ) ) ) ;
2757
- let mut result = if let Scalar :: Int ( _) = s {
2758
- value_signed. to_string ( )
2759
- } else {
2760
- value. to_string ( )
2761
- } ;
2762
- if value >= 10 {
2763
- format_to ! ( result, " ({value:#X})" ) ;
2764
- return Ok ( result) ;
2765
- } else {
2766
- return Ok ( result) ;
2767
- }
2768
- }
2769
- }
2770
- }
2771
- }
2772
- if let Ok ( s) = mir:: render_const_using_debug_impl ( db, self . id . into ( ) , & c) {
2773
- Ok ( s)
2774
- } else {
2775
- Ok ( format ! ( "{}" , c. display( db, edition) ) )
2776
- }
2729
+ /// Evaluate the static initializer.
2730
+ pub fn eval ( self , db : & dyn HirDatabase ) -> Result < EvaluatedConst , ConstEvalError > {
2731
+ db. const_eval ( self . id . into ( ) , Substitution :: empty ( Interner ) , None )
2732
+ . map ( |it| EvaluatedConst { const_ : it, def : self . id . into ( ) } )
2777
2733
}
2778
2734
}
2779
2735
0 commit comments