@@ -13,8 +13,10 @@ use rustc::mir::interpret::{ConstEvalErr, ErrorHandled, ScalarMaybeUndef};
1313use rustc:: mir;
1414use rustc:: ty:: { self , Ty , TyCtxt , subst:: Subst } ;
1515use rustc:: ty:: layout:: { self , LayoutOf , VariantIdx } ;
16+ use rustc:: ty:: subst:: { Subst , SubstsRef } ;
1617use rustc:: traits:: Reveal ;
1718use rustc_data_structures:: fx:: FxHashMap ;
19+ use crate :: interpret:: alloc_type_name;
1820
1921use syntax:: source_map:: { Span , DUMMY_SP } ;
2022
@@ -604,11 +606,42 @@ pub fn const_eval_provider<'tcx>(
604606 other => return other,
605607 }
606608 }
609+
610+ if let ty:: InstanceDef :: Intrinsic ( def_id) = key. value . instance . def {
611+ let ty = key. value . instance . ty ( tcx) ;
612+ let substs = match ty. sty {
613+ ty:: FnDef ( _, substs) => substs,
614+ _ => bug ! ( "intrinsic with type {:?}" , ty) ,
615+ } ;
616+ return Ok ( eval_intrinsic ( tcx, def_id, substs) ) ;
617+ }
618+
607619 tcx. const_eval_raw ( key) . and_then ( |val| {
608620 validate_and_turn_into_const ( tcx, val, key)
609621 } )
610622}
611623
624+ fn eval_intrinsic < ' tcx > (
625+ tcx : TyCtxt < ' tcx > ,
626+ def_id : DefId ,
627+ substs : SubstsRef < ' tcx > ,
628+ ) -> & ' tcx ty:: Const < ' tcx > {
629+ match & * tcx. item_name ( def_id) . as_str ( ) {
630+ "type_name" => {
631+ let alloc = alloc_type_name ( tcx, substs. type_at ( 0 ) ) ;
632+ tcx. mk_const ( ty:: Const {
633+ val : ConstValue :: Slice {
634+ data : alloc,
635+ start : 0 ,
636+ end : alloc. bytes . len ( ) ,
637+ } ,
638+ ty : tcx. mk_static_str ( ) ,
639+ } )
640+ } ,
641+ other => bug ! ( "`{}` is not a zero arg intrinsic" , other) ,
642+ }
643+ }
644+
612645pub fn const_eval_raw_provider < ' tcx > (
613646 tcx : TyCtxt < ' tcx > ,
614647 key : ty:: ParamEnvAnd < ' tcx , GlobalId < ' tcx > > ,
0 commit comments