@@ -10,11 +10,10 @@ use rustc::hir::{self, def_id::DefId};
1010use rustc:: hir:: def:: Def ;
1111use rustc:: mir:: interpret:: { ConstEvalErr , ErrorHandled } ;
1212use rustc:: mir;
13- use rustc:: ty:: { self , TyCtxt , Instance , query:: TyCtxtAt } ;
13+ use rustc:: ty:: { self , TyCtxt , query:: TyCtxtAt } ;
1414use rustc:: ty:: layout:: { self , LayoutOf , TyLayout , VariantIdx } ;
1515use rustc:: ty:: subst:: Subst ;
1616use rustc:: traits:: Reveal ;
17- use rustc_data_structures:: indexed_vec:: IndexVec ;
1817use rustc_data_structures:: fx:: FxHashMap ;
1918use rustc:: util:: common:: ErrorReported ;
2019
@@ -35,54 +34,6 @@ const STEPS_UNTIL_DETECTOR_ENABLED: isize = 1_000_000;
3534/// Should be a power of two for performance reasons.
3635const DETECTOR_SNAPSHOT_PERIOD : isize = 256 ;
3736
38- /// Warning: do not use this function if you expect to start interpreting the given `Mir`.
39- /// The `EvalContext` is only meant to be used to query values from constants and statics.
40- ///
41- /// This function is used during const propagation. We cannot use `mk_eval_cx`, because copy
42- /// propagation happens *during* the computation of the MIR of the current function. So if we
43- /// tried to call the `optimized_mir` query, we'd get a cycle error because we are (transitively)
44- /// inside the `optimized_mir` query of the `Instance` given.
45- ///
46- /// Since we are looking at the MIR of the function in an abstract manner, we don't have a
47- /// `ParamEnv` available to us. This function creates a `ParamEnv` for the given instance.
48- pub fn mk_borrowck_eval_cx < ' a , ' mir , ' tcx > (
49- tcx : TyCtxt < ' a , ' tcx , ' tcx > ,
50- instance : Instance < ' tcx > ,
51- mir : & ' mir mir:: Mir < ' tcx > ,
52- span : Span ,
53- ) -> EvalResult < ' tcx , CompileTimeEvalContext < ' a , ' mir , ' tcx > > {
54- debug ! ( "mk_borrowck_eval_cx: {:?}" , instance) ;
55- let param_env = tcx. param_env ( instance. def_id ( ) ) ;
56- mk_eval_cx_inner ( tcx, instance, mir, span, param_env)
57- }
58-
59- /// This is just a helper function to reduce code duplication between `mk_borrowck_eval_cx` and
60- /// `mk_eval_cx`. Do not call this function directly.
61- fn mk_eval_cx_inner < ' a , ' mir , ' tcx > (
62- tcx : TyCtxt < ' a , ' tcx , ' tcx > ,
63- instance : Instance < ' tcx > ,
64- mir : & ' mir mir:: Mir < ' tcx > ,
65- span : Span ,
66- param_env : ty:: ParamEnv < ' tcx > ,
67- ) -> EvalResult < ' tcx , CompileTimeEvalContext < ' a , ' mir , ' tcx > > {
68- let mut ecx = EvalContext :: new ( tcx. at ( span) , param_env, CompileTimeInterpreter :: new ( ) ) ;
69- // Insert a stack frame so any queries have the correct substs.
70- // We also avoid all the extra work performed by push_stack_frame,
71- // like initializing local variables
72- ecx. stack . push ( interpret:: Frame {
73- block : mir:: START_BLOCK ,
74- locals : IndexVec :: new ( ) ,
75- instance,
76- span,
77- mir,
78- return_place : None ,
79- return_to_block : StackPopCleanup :: Goto ( None ) , // never pop
80- stmt : 0 ,
81- extra : ( ) ,
82- } ) ;
83- Ok ( ecx)
84- }
85-
8637/// Warning: do not use this function if you expect to start interpreting the given `Mir`.
8738/// The `EvalContext` is only meant to be used to do field and index projections into constants for
8839/// `simd_shuffle` and const patterns in match arms.
@@ -91,15 +42,13 @@ fn mk_eval_cx_inner<'a, 'mir, 'tcx>(
9142/// that inform us about the generic bounds of the constant. E.g. using an associated constant
9243/// of a function's generic parameter will require knowledge about the bounds on the generic
9344/// parameter. These bounds are passed to `mk_eval_cx` via the `ParamEnv` argument.
94- fn mk_eval_cx < ' a , ' tcx > (
45+ pub ( crate ) fn mk_eval_cx < ' a , ' mir , ' tcx > (
9546 tcx : TyCtxt < ' a , ' tcx , ' tcx > ,
96- instance : Instance < ' tcx > ,
47+ span : Span ,
9748 param_env : ty:: ParamEnv < ' tcx > ,
98- ) -> EvalResult < ' tcx , CompileTimeEvalContext < ' a , ' tcx , ' tcx > > {
99- debug ! ( "mk_eval_cx: {:?}, {:?}" , instance, param_env) ;
100- let span = tcx. def_span ( instance. def_id ( ) ) ;
101- let mir = tcx. optimized_mir ( instance. def . def_id ( ) ) ;
102- mk_eval_cx_inner ( tcx, instance, mir, span, param_env)
49+ ) -> CompileTimeEvalContext < ' a , ' mir , ' tcx > {
50+ debug ! ( "mk_eval_cx: {:?}" , param_env) ;
51+ EvalContext :: new ( tcx. at ( span) , param_env, CompileTimeInterpreter :: new ( ) )
10352}
10453
10554pub ( crate ) fn eval_promoted < ' a , ' mir , ' tcx > (
@@ -108,7 +57,8 @@ pub(crate) fn eval_promoted<'a, 'mir, 'tcx>(
10857 mir : & ' mir mir:: Mir < ' tcx > ,
10958 param_env : ty:: ParamEnv < ' tcx > ,
11059) -> EvalResult < ' tcx , MPlaceTy < ' tcx > > {
111- let mut ecx = mk_borrowck_eval_cx ( tcx, cid. instance , mir, DUMMY_SP ) . unwrap ( ) ;
60+ let span = tcx. def_span ( cid. instance . def_id ( ) ) ;
61+ let mut ecx = mk_eval_cx ( tcx, span, param_env) ;
11262 eval_body_using_ecx ( & mut ecx, cid, Some ( mir) , param_env)
11363}
11464
@@ -529,13 +479,12 @@ impl<'a, 'mir, 'tcx> interpret::Machine<'a, 'mir, 'tcx>
529479pub fn const_field < ' a , ' tcx > (
530480 tcx : TyCtxt < ' a , ' tcx , ' tcx > ,
531481 param_env : ty:: ParamEnv < ' tcx > ,
532- instance : ty:: Instance < ' tcx > ,
533482 variant : Option < VariantIdx > ,
534483 field : mir:: Field ,
535484 value : ty:: Const < ' tcx > ,
536485) -> :: rustc:: mir:: interpret:: ConstEvalResult < ' tcx > {
537- trace ! ( "const_field: {:?}, {:?}, {:?}" , instance , field, value) ;
538- let ecx = mk_eval_cx ( tcx, instance , param_env) . unwrap ( ) ;
486+ trace ! ( "const_field: {:?}, {:?}" , field, value) ;
487+ let ecx = mk_eval_cx ( tcx, DUMMY_SP , param_env) ;
539488 let result = ( || {
540489 // get the operand again
541490 let op = lazy_const_to_op ( & ecx, ty:: LazyConst :: Evaluated ( value) , value. ty ) ?;
@@ -560,11 +509,10 @@ pub fn const_field<'a, 'tcx>(
560509pub fn const_variant_index < ' a , ' tcx > (
561510 tcx : TyCtxt < ' a , ' tcx , ' tcx > ,
562511 param_env : ty:: ParamEnv < ' tcx > ,
563- instance : ty:: Instance < ' tcx > ,
564512 val : ty:: Const < ' tcx > ,
565513) -> EvalResult < ' tcx , VariantIdx > {
566- trace ! ( "const_variant_index: {:?}, {:?}" , instance , val) ;
567- let ecx = mk_eval_cx ( tcx, instance , param_env) . unwrap ( ) ;
514+ trace ! ( "const_variant_index: {:?}" , val) ;
515+ let ecx = mk_eval_cx ( tcx, DUMMY_SP , param_env) ;
568516 let op = lazy_const_to_op ( & ecx, ty:: LazyConst :: Evaluated ( val) , val. ty ) ?;
569517 Ok ( ecx. read_discriminant ( op) ?. 1 )
570518}
@@ -584,7 +532,7 @@ fn validate_and_turn_into_const<'a, 'tcx>(
584532 key : ty:: ParamEnvAnd < ' tcx , GlobalId < ' tcx > > ,
585533) -> :: rustc:: mir:: interpret:: ConstEvalResult < ' tcx > {
586534 let cid = key. value ;
587- let ecx = mk_eval_cx ( tcx, cid . instance , key. param_env ) . unwrap ( ) ;
535+ let ecx = mk_eval_cx ( tcx, tcx . def_span ( key . value . instance . def_id ( ) ) , key. param_env ) ;
588536 let val = ( || {
589537 let op = ecx. raw_const_to_mplace ( constant) ?. into ( ) ;
590538 // FIXME: Once the visitor infrastructure landed, change validation to
0 commit comments