@@ -292,6 +292,9 @@ pub fn eval_to_const_value_raw_provider<'tcx>(
292292 tcx : TyCtxt < ' tcx > ,
293293 key : ty:: PseudoCanonicalInput < ' tcx , GlobalId < ' tcx > > ,
294294) -> :: rustc_middle:: mir:: interpret:: EvalToConstValueResult < ' tcx > {
295+ if let Some ( ( value, _ty) ) = tcx. is_trivial_const ( key. value . instance . def_id ( ) ) {
296+ return Ok ( value) ;
297+ }
295298 tcx. eval_to_allocation_raw ( key) . map ( |val| turn_into_const_value ( tcx, val, key) )
296299}
297300
@@ -356,6 +359,7 @@ fn eval_in_interpreter<'tcx, R: InterpretationResult<'tcx>>(
356359 typing_env : ty:: TypingEnv < ' tcx > ,
357360) -> Result < R , ErrorHandled > {
358361 let def = cid. instance . def . def_id ( ) ;
362+
359363 let is_static = tcx. is_static ( def) ;
360364
361365 let mut ecx = InterpCx :: new (
@@ -368,6 +372,18 @@ fn eval_in_interpreter<'tcx, R: InterpretationResult<'tcx>>(
368372 // so we have to reject reading mutable global memory.
369373 CompileTimeMachine :: new ( CanAccessMutGlobal :: from ( is_static) , CheckAlignment :: Error ) ,
370374 ) ;
375+
376+ if let Some ( ( value, ty) ) = tcx. is_trivial_const ( def) {
377+ let layout = ecx. layout_of ( ty) . unwrap ( ) ;
378+ let opty = ecx. const_val_to_op ( value, ty, Some ( layout) ) . unwrap ( ) ;
379+ let res = ecx. allocate ( layout, MemoryKind :: Stack ) . unwrap ( ) ;
380+ ecx. copy_op ( & opty, & res) . unwrap ( ) ;
381+
382+ intern_const_alloc_recursive ( & mut ecx, InternKind :: Constant , & res) . unwrap ( ) ;
383+
384+ return Ok ( R :: make_result ( res, & mut ecx) ) ;
385+ }
386+
371387 let res = ecx. load_mir ( cid. instance . def , cid. promoted ) ;
372388 res. and_then ( |body| eval_body_using_ecx ( & mut ecx, cid, body) )
373389 . report_err ( )
0 commit comments