@@ -13,10 +13,11 @@ use rustc::mir::interpret::{ConstEvalErr, ErrorHandled, ScalarMaybeUndef};
13
13
use rustc:: mir;
14
14
use rustc:: ty:: { self , TyCtxt , query:: TyCtxtAt } ;
15
15
use rustc:: ty:: layout:: { self , LayoutOf , VariantIdx } ;
16
- use rustc:: ty:: subst:: Subst ;
16
+ use rustc:: ty:: subst:: { Subst , SubstsRef } ;
17
17
use rustc:: traits:: Reveal ;
18
18
use rustc:: util:: common:: ErrorReported ;
19
19
use rustc_data_structures:: fx:: FxHashMap ;
20
+ use crate :: interpret:: alloc_type_name;
20
21
21
22
use syntax:: source_map:: { Span , DUMMY_SP } ;
22
23
@@ -588,11 +589,42 @@ pub fn const_eval_provider<'tcx>(
588
589
other => return other,
589
590
}
590
591
}
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
+
591
602
tcx. const_eval_raw ( key) . and_then ( |val| {
592
603
validate_and_turn_into_const ( tcx, val, key)
593
604
} )
594
605
}
595
606
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
+
596
628
pub fn const_eval_raw_provider < ' tcx > (
597
629
tcx : TyCtxt < ' tcx > ,
598
630
key : ty:: ParamEnvAnd < ' tcx , GlobalId < ' tcx > > ,
0 commit comments