@@ -13,10 +13,11 @@ use rustc::mir::interpret::{ConstEvalErr, ErrorHandled, ScalarMaybeUndef};
1313use rustc:: mir;
1414use rustc:: ty:: { self , TyCtxt , query:: TyCtxtAt } ;
1515use rustc:: ty:: layout:: { self , LayoutOf , VariantIdx } ;
16- use rustc:: ty:: subst:: Subst ;
16+ use rustc:: ty:: subst:: { Subst , SubstsRef } ;
1717use rustc:: traits:: Reveal ;
1818use rustc:: util:: common:: ErrorReported ;
1919use rustc_data_structures:: fx:: FxHashMap ;
20+ use crate :: interpret:: alloc_type_name;
2021
2122use syntax:: source_map:: { Span , DUMMY_SP } ;
2223
@@ -588,11 +589,42 @@ pub fn const_eval_provider<'tcx>(
588589 other => return other,
589590 }
590591 }
592+
593+ if let ty:: InstanceDef :: Intrinsic ( def_id) = key. value . instance . def {
594+ let ty = key. value . instance . ty ( tcx) ;
595+ let substs = match ty. sty {
596+ ty:: FnDef ( _, substs) => substs,
597+ _ => bug ! ( "intrinsic with type {:?}" , ty) ,
598+ } ;
599+ return Ok ( eval_intrinsic ( tcx, def_id, substs) ) ;
600+ }
601+
591602 tcx. const_eval_raw ( key) . and_then ( |val| {
592603 validate_and_turn_into_const ( tcx, val, key)
593604 } )
594605}
595606
607+ fn eval_intrinsic < ' tcx > (
608+ tcx : TyCtxt < ' tcx > ,
609+ def_id : DefId ,
610+ substs : SubstsRef < ' tcx > ,
611+ ) -> & ' tcx ty:: Const < ' tcx > {
612+ match & * tcx. item_name ( def_id) . as_str ( ) {
613+ "type_name" => {
614+ let alloc = alloc_type_name ( tcx, substs. type_at ( 0 ) ) ;
615+ tcx. mk_const ( ty:: Const {
616+ val : ConstValue :: Slice {
617+ data : alloc,
618+ start : 0 ,
619+ end : alloc. bytes . len ( ) ,
620+ } ,
621+ ty : tcx. mk_static_str ( ) ,
622+ } )
623+ } ,
624+ other => bug ! ( "`{}` is not a zero arg intrinsic" , other) ,
625+ }
626+ }
627+
596628pub fn const_eval_raw_provider < ' tcx > (
597629 tcx : TyCtxt < ' tcx > ,
598630 key : ty:: ParamEnvAnd < ' tcx , GlobalId < ' tcx > > ,
0 commit comments