@@ -23,8 +23,9 @@ use crate::{
2323 mir:: { MirEvalError , MirLowerError } ,
2424 next_solver:: {
2525 Const , ConstBytes , ConstKind , DbInterner , ErrorGuaranteed , GenericArg , GenericArgs ,
26- ParamEnv , Ty , ValueConst ,
26+ ParamEnv , StoredConst , StoredGenericArgs , Ty , ValueConst ,
2727 } ,
28+ traits:: StoredParamEnvAndCrate ,
2829} ;
2930
3031use super :: mir:: { interpret_mir, lower_to_mir, pad16} ;
@@ -38,12 +39,12 @@ pub fn unknown_const_as_generic<'db>(ty: Ty<'db>) -> GenericArg<'db> {
3839}
3940
4041#[ derive( Debug , Clone , PartialEq , Eq ) ]
41- pub enum ConstEvalError < ' db > {
42- MirLowerError ( MirLowerError < ' db > ) ,
43- MirEvalError ( MirEvalError < ' db > ) ,
42+ pub enum ConstEvalError {
43+ MirLowerError ( MirLowerError ) ,
44+ MirEvalError ( MirEvalError ) ,
4445}
4546
46- impl ConstEvalError < ' _ > {
47+ impl ConstEvalError {
4748 pub fn pretty_print (
4849 & self ,
4950 f : & mut String ,
@@ -62,17 +63,17 @@ impl ConstEvalError<'_> {
6263 }
6364}
6465
65- impl < ' db > From < MirLowerError < ' db > > for ConstEvalError < ' db > {
66- fn from ( value : MirLowerError < ' db > ) -> Self {
66+ impl From < MirLowerError > for ConstEvalError {
67+ fn from ( value : MirLowerError ) -> Self {
6768 match value {
6869 MirLowerError :: ConstEvalError ( _, e) => * e,
6970 _ => ConstEvalError :: MirLowerError ( value) ,
7071 }
7172 }
7273}
7374
74- impl < ' db > From < MirEvalError < ' db > > for ConstEvalError < ' db > {
75- fn from ( value : MirEvalError < ' db > ) -> Self {
75+ impl From < MirEvalError > for ConstEvalError {
76+ fn from ( value : MirEvalError ) -> Self {
7677 ConstEvalError :: MirEvalError ( value)
7778 }
7879}
@@ -85,7 +86,8 @@ pub fn intern_const_ref<'a>(
8586 krate : Crate ,
8687) -> Const < ' a > {
8788 let interner = DbInterner :: new_no_crate ( db) ;
88- let layout = db. layout_of_ty ( ty, ParamEnvAndCrate { param_env : ParamEnv :: empty ( ) , krate } ) ;
89+ let layout = db
90+ . layout_of_ty ( ty. store ( ) , ParamEnvAndCrate { param_env : ParamEnv :: empty ( ) , krate } . store ( ) ) ;
8991 let kind = match value {
9092 LiteralConstRef :: Int ( i) => {
9193 // FIXME: We should handle failure of layout better.
@@ -180,10 +182,10 @@ pub fn try_const_isize<'db>(db: &'db dyn HirDatabase, c: &Const<'db>) -> Option<
180182 }
181183}
182184
183- pub ( crate ) fn const_eval_discriminant_variant < ' db > (
184- db : & ' db dyn HirDatabase ,
185+ pub ( crate ) fn const_eval_discriminant_variant (
186+ db : & dyn HirDatabase ,
185187 variant_id : EnumVariantId ,
186- ) -> Result < i128 , ConstEvalError < ' db > > {
188+ ) -> Result < i128 , ConstEvalError > {
187189 let interner = DbInterner :: new_no_crate ( db) ;
188190 let def = variant_id. into ( ) ;
189191 let body = db. body ( def) ;
@@ -206,8 +208,9 @@ pub(crate) fn const_eval_discriminant_variant<'db>(
206208
207209 let mir_body = db. monomorphized_mir_body (
208210 def,
209- GenericArgs :: new_from_iter ( interner, [ ] ) ,
210- ParamEnvAndCrate { param_env : db. trait_environment_for_body ( def) , krate : def. krate ( db) } ,
211+ GenericArgs :: empty ( interner) . store ( ) ,
212+ ParamEnvAndCrate { param_env : db. trait_environment_for_body ( def) , krate : def. krate ( db) }
213+ . store ( ) ,
211214 ) ?;
212215 let c = interpret_mir ( db, mir_body, false , None ) ?. 0 ?;
213216 let c = if is_signed {
@@ -233,7 +236,7 @@ pub(crate) fn eval_to_const<'db>(expr: ExprId, ctx: &mut InferenceContext<'_, 'd
233236 }
234237 if has_closure ( ctx. body , expr) {
235238 // Type checking clousres need an isolated body (See the above FIXME). Bail out early to prevent panic.
236- return unknown_const ( infer [ expr ] ) ;
239+ return Const :: error ( ctx . interner ( ) ) ;
237240 }
238241 if let Expr :: Path ( p) = & ctx. body [ expr] {
239242 let mut ctx = TyLoweringContext :: new (
@@ -252,63 +255,89 @@ pub(crate) fn eval_to_const<'db>(expr: ExprId, ctx: &mut InferenceContext<'_, 'd
252255 {
253256 return result;
254257 }
255- unknown_const ( infer [ expr ] )
258+ Const :: error ( ctx . interner ( ) )
256259}
257260
258- pub ( crate ) fn const_eval_cycle_result < ' db > (
259- _: & ' db dyn HirDatabase ,
260- _: salsa:: Id ,
261- _: ConstId ,
262- _: GenericArgs < ' db > ,
263- _: Option < ParamEnvAndCrate < ' db > > ,
264- ) -> Result < Const < ' db > , ConstEvalError < ' db > > {
265- Err ( ConstEvalError :: MirLowerError ( MirLowerError :: Loop ) )
266- }
267-
268- pub ( crate ) fn const_eval_static_cycle_result < ' db > (
269- _: & ' db dyn HirDatabase ,
270- _: salsa:: Id ,
271- _: StaticId ,
272- ) -> Result < Const < ' db > , ConstEvalError < ' db > > {
273- Err ( ConstEvalError :: MirLowerError ( MirLowerError :: Loop ) )
274- }
275-
276- pub ( crate ) fn const_eval_discriminant_cycle_result < ' db > (
277- _: & ' db dyn HirDatabase ,
261+ pub ( crate ) fn const_eval_discriminant_cycle_result (
262+ _: & dyn HirDatabase ,
278263 _: salsa:: Id ,
279264 _: EnumVariantId ,
280- ) -> Result < i128 , ConstEvalError < ' db > > {
265+ ) -> Result < i128 , ConstEvalError > {
281266 Err ( ConstEvalError :: MirLowerError ( MirLowerError :: Loop ) )
282267}
283268
284- pub ( crate ) fn const_eval_query < ' db > (
269+ pub ( crate ) fn const_eval < ' db > (
285270 db : & ' db dyn HirDatabase ,
286271 def : ConstId ,
287272 subst : GenericArgs < ' db > ,
288273 trait_env : Option < ParamEnvAndCrate < ' db > > ,
289- ) -> Result < Const < ' db > , ConstEvalError < ' db > > {
290- let body = db. monomorphized_mir_body (
291- def. into ( ) ,
292- subst,
293- ParamEnvAndCrate { param_env : db. trait_environment ( def. into ( ) ) , krate : def. krate ( db) } ,
294- ) ?;
295- let c = interpret_mir ( db, body, false , trait_env) ?. 0 ?;
296- Ok ( c)
274+ ) -> Result < Const < ' db > , ConstEvalError > {
275+ return match const_eval_query ( db, def, subst. store ( ) , trait_env. map ( |env| env. store ( ) ) ) {
276+ Ok ( konst) => Ok ( konst. as_ref ( ) ) ,
277+ Err ( err) => Err ( err. clone ( ) ) ,
278+ } ;
279+
280+ #[ salsa:: tracked( returns( ref) , cycle_result = const_eval_cycle_result) ]
281+ pub ( crate ) fn const_eval_query < ' db > (
282+ db : & ' db dyn HirDatabase ,
283+ def : ConstId ,
284+ subst : StoredGenericArgs ,
285+ trait_env : Option < StoredParamEnvAndCrate > ,
286+ ) -> Result < StoredConst , ConstEvalError > {
287+ let body = db. monomorphized_mir_body (
288+ def. into ( ) ,
289+ subst,
290+ ParamEnvAndCrate { param_env : db. trait_environment ( def. into ( ) ) , krate : def. krate ( db) }
291+ . store ( ) ,
292+ ) ?;
293+ let c = interpret_mir ( db, body, false , trait_env. as_ref ( ) . map ( |env| env. as_ref ( ) ) ) ?. 0 ?;
294+ Ok ( c. store ( ) )
295+ }
296+
297+ pub ( crate ) fn const_eval_cycle_result (
298+ _: & dyn HirDatabase ,
299+ _: salsa:: Id ,
300+ _: ConstId ,
301+ _: StoredGenericArgs ,
302+ _: Option < StoredParamEnvAndCrate > ,
303+ ) -> Result < StoredConst , ConstEvalError > {
304+ Err ( ConstEvalError :: MirLowerError ( MirLowerError :: Loop ) )
305+ }
297306}
298307
299- pub ( crate ) fn const_eval_static_query < ' db > (
308+ pub ( crate ) fn const_eval_static < ' db > (
300309 db : & ' db dyn HirDatabase ,
301310 def : StaticId ,
302- ) -> Result < Const < ' db > , ConstEvalError < ' db > > {
303- let interner = DbInterner :: new_no_crate ( db) ;
304- let body = db. monomorphized_mir_body (
305- def. into ( ) ,
306- GenericArgs :: new_from_iter ( interner, [ ] ) ,
307- ParamEnvAndCrate {
308- param_env : db. trait_environment_for_body ( def. into ( ) ) ,
309- krate : def. krate ( db) ,
310- } ,
311- ) ?;
312- let c = interpret_mir ( db, body, false , None ) ?. 0 ?;
313- Ok ( c)
311+ ) -> Result < Const < ' db > , ConstEvalError > {
312+ return match const_eval_static_query ( db, def) {
313+ Ok ( konst) => Ok ( konst. as_ref ( ) ) ,
314+ Err ( err) => Err ( err. clone ( ) ) ,
315+ } ;
316+
317+ #[ salsa:: tracked( returns( ref) , cycle_result = const_eval_static_cycle_result) ]
318+ pub ( crate ) fn const_eval_static_query < ' db > (
319+ db : & ' db dyn HirDatabase ,
320+ def : StaticId ,
321+ ) -> Result < StoredConst , ConstEvalError > {
322+ let interner = DbInterner :: new_no_crate ( db) ;
323+ let body = db. monomorphized_mir_body (
324+ def. into ( ) ,
325+ GenericArgs :: empty ( interner) . store ( ) ,
326+ ParamEnvAndCrate {
327+ param_env : db. trait_environment_for_body ( def. into ( ) ) ,
328+ krate : def. krate ( db) ,
329+ }
330+ . store ( ) ,
331+ ) ?;
332+ let c = interpret_mir ( db, body, false , None ) ?. 0 ?;
333+ Ok ( c. store ( ) )
334+ }
335+
336+ pub ( crate ) fn const_eval_static_cycle_result (
337+ _: & dyn HirDatabase ,
338+ _: salsa:: Id ,
339+ _: StaticId ,
340+ ) -> Result < StoredConst , ConstEvalError > {
341+ Err ( ConstEvalError :: MirLowerError ( MirLowerError :: Loop ) )
342+ }
314343}
0 commit comments