@@ -7,9 +7,9 @@ use base_db::Crate;
77use hir_def:: {
88 ConstId , EnumVariantId , GeneralConstId , HasModule , StaticId ,
99 attrs:: AttrFlags ,
10+ builtin_type:: { BuiltinInt , BuiltinType , BuiltinUint } ,
1011 expr_store:: Body ,
11- hir:: { Expr , ExprId } ,
12- type_ref:: LiteralConstRef ,
12+ hir:: { Expr , ExprId , Literal } ,
1313} ;
1414use hir_expand:: Lookup ;
1515use rustc_type_ir:: inherent:: IntoKind ;
@@ -23,7 +23,7 @@ use crate::{
2323 mir:: { MirEvalError , MirLowerError } ,
2424 next_solver:: {
2525 Const , ConstBytes , ConstKind , DbInterner , ErrorGuaranteed , GenericArg , GenericArgs ,
26- ParamEnv , StoredConst , StoredGenericArgs , Ty , ValueConst ,
26+ StoredConst , StoredGenericArgs , Ty , ValueConst ,
2727 } ,
2828 traits:: StoredParamEnvAndCrate ,
2929} ;
@@ -81,47 +81,122 @@ impl From<MirEvalError> for ConstEvalError {
8181/// Interns a constant scalar with the given type
8282pub fn intern_const_ref < ' a > (
8383 db : & ' a dyn HirDatabase ,
84- value : & LiteralConstRef ,
84+ value : & Literal ,
8585 ty : Ty < ' a > ,
86- krate : Crate ,
86+ _krate : Crate ,
8787) -> Const < ' a > {
8888 let interner = DbInterner :: new_no_crate ( db) ;
89- let layout = db
90- . layout_of_ty ( ty. store ( ) , ParamEnvAndCrate { param_env : ParamEnv :: empty ( ) , krate } . store ( ) ) ;
9189 let kind = match value {
92- LiteralConstRef :: Int ( i) => {
93- // FIXME: We should handle failure of layout better.
94- let size = layout. map ( |it| it. size . bytes_usize ( ) ) . unwrap_or ( 16 ) ;
90+ & Literal :: Uint ( i, builtin_ty)
91+ if builtin_ty. is_none ( ) || ty. as_builtin ( ) == builtin_ty. map ( BuiltinType :: Uint ) =>
92+ {
93+ let memory = match ty. as_builtin ( ) {
94+ Some ( BuiltinType :: Uint ( builtin_uint) ) => match builtin_uint {
95+ BuiltinUint :: U8 => Box :: new ( [ i as u8 ] ) as Box < [ u8 ] > ,
96+ BuiltinUint :: U16 => Box :: new ( ( i as u16 ) . to_le_bytes ( ) ) ,
97+ BuiltinUint :: U32 => Box :: new ( ( i as u32 ) . to_le_bytes ( ) ) ,
98+ BuiltinUint :: U64 => Box :: new ( ( i as u64 ) . to_le_bytes ( ) ) ,
99+ BuiltinUint :: U128 => Box :: new ( ( i) . to_le_bytes ( ) ) ,
100+ BuiltinUint :: Usize => Box :: new ( ( i as usize ) . to_le_bytes ( ) ) ,
101+ } ,
102+ _ => return Const :: new ( interner, rustc_type_ir:: ConstKind :: Error ( ErrorGuaranteed ) ) ,
103+ } ;
95104 rustc_type_ir:: ConstKind :: Value ( ValueConst :: new (
96105 ty,
97- ConstBytes {
98- memory : i. to_le_bytes ( ) [ 0 ..size] . into ( ) ,
99- memory_map : MemoryMap :: default ( ) ,
106+ ConstBytes { memory, memory_map : MemoryMap :: default ( ) } ,
107+ ) )
108+ }
109+ & Literal :: Int ( i, None )
110+ if ty
111+ . as_builtin ( )
112+ . is_some_and ( |builtin_ty| matches ! ( builtin_ty, BuiltinType :: Uint ( _) ) ) =>
113+ {
114+ let memory = match ty. as_builtin ( ) {
115+ Some ( BuiltinType :: Uint ( builtin_uint) ) => match builtin_uint {
116+ BuiltinUint :: U8 => Box :: new ( [ i as u8 ] ) as Box < [ u8 ] > ,
117+ BuiltinUint :: U16 => Box :: new ( ( i as u16 ) . to_le_bytes ( ) ) ,
118+ BuiltinUint :: U32 => Box :: new ( ( i as u32 ) . to_le_bytes ( ) ) ,
119+ BuiltinUint :: U64 => Box :: new ( ( i as u64 ) . to_le_bytes ( ) ) ,
120+ BuiltinUint :: U128 => Box :: new ( ( i as u128 ) . to_le_bytes ( ) ) ,
121+ BuiltinUint :: Usize => Box :: new ( ( i as usize ) . to_le_bytes ( ) ) ,
100122 } ,
123+ _ => return Const :: new ( interner, rustc_type_ir:: ConstKind :: Error ( ErrorGuaranteed ) ) ,
124+ } ;
125+ rustc_type_ir:: ConstKind :: Value ( ValueConst :: new (
126+ ty,
127+ ConstBytes { memory, memory_map : MemoryMap :: default ( ) } ,
101128 ) )
102129 }
103- LiteralConstRef :: UInt ( i) => {
104- let size = layout. map ( |it| it. size . bytes_usize ( ) ) . unwrap_or ( 16 ) ;
130+ & Literal :: Int ( i, builtin_ty)
131+ if builtin_ty. is_none ( ) || ty. as_builtin ( ) == builtin_ty. map ( BuiltinType :: Int ) =>
132+ {
133+ let memory = match ty. as_builtin ( ) {
134+ Some ( BuiltinType :: Int ( builtin_int) ) => match builtin_int {
135+ BuiltinInt :: I8 => Box :: new ( [ i as u8 ] ) as Box < [ u8 ] > ,
136+ BuiltinInt :: I16 => Box :: new ( ( i as i16 ) . to_le_bytes ( ) ) ,
137+ BuiltinInt :: I32 => Box :: new ( ( i as i32 ) . to_le_bytes ( ) ) ,
138+ BuiltinInt :: I64 => Box :: new ( ( i as i64 ) . to_le_bytes ( ) ) ,
139+ BuiltinInt :: I128 => Box :: new ( ( i) . to_le_bytes ( ) ) ,
140+ BuiltinInt :: Isize => Box :: new ( ( i as isize ) . to_le_bytes ( ) ) ,
141+ } ,
142+ _ => return Const :: new ( interner, rustc_type_ir:: ConstKind :: Error ( ErrorGuaranteed ) ) ,
143+ } ;
105144 rustc_type_ir:: ConstKind :: Value ( ValueConst :: new (
106145 ty,
107- ConstBytes {
108- memory : i. to_le_bytes ( ) [ 0 ..size] . into ( ) ,
109- memory_map : MemoryMap :: default ( ) ,
146+ ConstBytes { memory, memory_map : MemoryMap :: default ( ) } ,
147+ ) )
148+ }
149+ Literal :: Float ( float_type_wrapper, builtin_float)
150+ if builtin_float. is_none ( )
151+ || ty. as_builtin ( ) == builtin_float. map ( BuiltinType :: Float ) =>
152+ {
153+ let memory = match ty. as_builtin ( ) . unwrap ( ) {
154+ BuiltinType :: Float ( builtin_float) => match builtin_float {
155+ // FIXME:
156+ hir_def:: builtin_type:: BuiltinFloat :: F16 => Box :: new ( [ 0u8 ; 2 ] ) as Box < [ u8 ] > ,
157+ hir_def:: builtin_type:: BuiltinFloat :: F32 => {
158+ Box :: new ( float_type_wrapper. to_f32 ( ) . to_le_bytes ( ) )
159+ }
160+ hir_def:: builtin_type:: BuiltinFloat :: F64 => {
161+ Box :: new ( float_type_wrapper. to_f64 ( ) . to_le_bytes ( ) )
162+ }
163+ // FIXME:
164+ hir_def:: builtin_type:: BuiltinFloat :: F128 => Box :: new ( [ 0 ; 16 ] ) ,
110165 } ,
166+ _ => unreachable ! ( ) ,
167+ } ;
168+ rustc_type_ir:: ConstKind :: Value ( ValueConst :: new (
169+ ty,
170+ ConstBytes { memory, memory_map : MemoryMap :: default ( ) } ,
111171 ) )
112172 }
113- LiteralConstRef :: Bool ( b) => rustc_type_ir:: ConstKind :: Value ( ValueConst :: new (
173+ Literal :: Bool ( b) if ty . is_bool ( ) => rustc_type_ir:: ConstKind :: Value ( ValueConst :: new (
114174 ty,
115175 ConstBytes { memory : Box :: new ( [ * b as u8 ] ) , memory_map : MemoryMap :: default ( ) } ,
116176 ) ) ,
117- LiteralConstRef :: Char ( c) => rustc_type_ir:: ConstKind :: Value ( ValueConst :: new (
177+ Literal :: Char ( c) if ty . is_char ( ) => rustc_type_ir:: ConstKind :: Value ( ValueConst :: new (
118178 ty,
119179 ConstBytes {
120180 memory : ( * c as u32 ) . to_le_bytes ( ) . into ( ) ,
121181 memory_map : MemoryMap :: default ( ) ,
122182 } ,
123183 ) ) ,
124- LiteralConstRef :: Unknown => rustc_type_ir:: ConstKind :: Error ( ErrorGuaranteed ) ,
184+ Literal :: String ( symbol) if ty. is_str ( ) => rustc_type_ir:: ConstKind :: Value ( ValueConst :: new (
185+ ty,
186+ ConstBytes {
187+ memory : symbol. as_str ( ) . as_bytes ( ) . into ( ) ,
188+ memory_map : MemoryMap :: default ( ) ,
189+ } ,
190+ ) ) ,
191+ Literal :: ByteString ( items) if ty. as_slice ( ) . is_some_and ( |ty| ty. is_u8 ( ) ) => {
192+ rustc_type_ir:: ConstKind :: Value ( ValueConst :: new (
193+ ty,
194+ ConstBytes { memory : items. clone ( ) , memory_map : MemoryMap :: default ( ) } ,
195+ ) )
196+ }
197+ // FIXME
198+ Literal :: CString ( _items) => rustc_type_ir:: ConstKind :: Error ( ErrorGuaranteed ) ,
199+ _ => rustc_type_ir:: ConstKind :: Error ( ErrorGuaranteed ) ,
125200 } ;
126201 Const :: new ( interner, kind)
127202}
@@ -130,7 +205,15 @@ pub fn intern_const_ref<'a>(
130205pub fn usize_const < ' db > ( db : & ' db dyn HirDatabase , value : Option < u128 > , krate : Crate ) -> Const < ' db > {
131206 intern_const_ref (
132207 db,
133- & value. map_or ( LiteralConstRef :: Unknown , LiteralConstRef :: UInt ) ,
208+ & match value {
209+ Some ( value) => Literal :: Uint ( value, Some ( BuiltinUint :: Usize ) ) ,
210+ None => {
211+ return Const :: new (
212+ DbInterner :: new_no_crate ( db) ,
213+ rustc_type_ir:: ConstKind :: Error ( ErrorGuaranteed ) ,
214+ ) ;
215+ }
216+ } ,
134217 Ty :: new_uint ( DbInterner :: new_no_crate ( db) , rustc_type_ir:: UintTy :: Usize ) ,
135218 krate,
136219 )
0 commit comments