@@ -18,6 +18,7 @@ use rustc::traits;
1818use rustc:: hir:: def:: Def ;
1919use rustc:: hir:: def_id:: DefId ;
2020use rustc:: ty:: { self , Ty , TyCtxt } ;
21+ use rustc:: ty:: maps:: Providers ;
2122use rustc:: ty:: util:: IntTypeExt ;
2223use rustc:: ty:: subst:: { Substs , Subst } ;
2324use rustc:: traits:: Reveal ;
@@ -163,12 +164,6 @@ pub struct ConstContext<'a, 'tcx: 'a> {
163164}
164165
165166impl < ' a , ' tcx > ConstContext < ' a , ' tcx > {
166- pub fn new ( tcx : TyCtxt < ' a , ' tcx , ' tcx > , body : hir:: BodyId ) -> Self {
167- let def_id = tcx. hir . body_owner_def_id ( body) ;
168- ty:: queries:: mir_const_qualif:: get ( tcx, DUMMY_SP , def_id) ;
169- ConstContext :: with_tables ( tcx, tcx. item_tables ( def_id) )
170- }
171-
172167 pub fn with_tables ( tcx : TyCtxt < ' a , ' tcx , ' tcx > , tables : & ' a ty:: TypeckTables < ' tcx > ) -> Self {
173168 ConstContext {
174169 tcx : tcx,
@@ -799,34 +794,20 @@ impl<'a, 'tcx> ConstContext<'a, 'tcx> {
799794 }
800795}
801796
797+ pub fn provide ( providers : & mut Providers ) {
798+ * providers = Providers {
799+ monomorphic_const_eval,
800+ ..* providers
801+ } ;
802+ }
802803
803- /// Returns the value of the length-valued expression
804- pub fn eval_length < ' a , ' tcx > ( tcx : TyCtxt < ' a , ' tcx , ' tcx > ,
805- count : hir:: BodyId ,
806- reason : & str )
807- -> Result < usize , ErrorReported >
808- {
809- let count_expr = & tcx. hir . body ( count) . value ;
810- match ConstContext :: new ( tcx, count) . eval ( count_expr) {
811- Ok ( Integral ( Usize ( count) ) ) => {
812- let val = count. as_u64 ( tcx. sess . target . uint_type ) ;
813- assert_eq ! ( val as usize as u64 , val) ;
814- Ok ( val as usize )
815- } ,
816- Ok ( _) |
817- Err ( ConstEvalErr { kind : TypeckError , .. } ) => Err ( ErrorReported ) ,
818- Err ( err) => {
819- let mut diag = err. struct_error ( tcx, count_expr. span , reason) ;
820-
821- if let hir:: ExprPath ( hir:: QPath :: Resolved ( None , ref path) ) = count_expr. node {
822- if let Def :: Local ( ..) = path. def {
823- diag. note ( & format ! ( "`{}` is a variable" ,
824- tcx. hir. node_to_pretty_string( count_expr. id) ) ) ;
825- }
826- }
804+ fn monomorphic_const_eval < ' a , ' tcx > ( tcx : TyCtxt < ' a , ' tcx , ' tcx > ,
805+ def_id : DefId )
806+ -> EvalResult < ' tcx > {
807+ ty:: queries:: mir_const_qualif:: get ( tcx, DUMMY_SP , def_id) ;
808+ let cx = ConstContext :: with_tables ( tcx, tcx. item_tables ( def_id) ) ;
827809
828- diag. emit ( ) ;
829- Err ( ErrorReported )
830- }
831- }
810+ let id = tcx. hir . as_local_node_id ( def_id) . unwrap ( ) ;
811+ let body = tcx. hir . body_owned_by ( id) ;
812+ cx. eval ( & tcx. hir . body ( body) . value )
832813}
0 commit comments