@@ -2649,24 +2649,31 @@ impl Const {
26492649 Type :: from_value_def ( db, self . id )
26502650 }
26512651
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 ( ) } )
26582656 }
2657+ }
26592658
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 ) ;
26702677 if let TyKind :: Scalar ( s) = data. ty . kind ( Interner ) {
26712678 if matches ! ( s, Scalar :: Int ( _) | Scalar :: Uint ( _) ) {
26722679 if let hir_ty:: ConstValue :: Concrete ( c) = & data. value {
@@ -2689,17 +2696,7 @@ impl Const {
26892696 }
26902697 }
26912698 }
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_ )
27032700 }
27042701}
27052702
@@ -2729,51 +2726,10 @@ impl Static {
27292726 Type :: from_value_def ( db, self . id )
27302727 }
27312728
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 ( ) } )
27772733 }
27782734}
27792735
0 commit comments