Skip to content

Commit f0f7a59

Browse files
committed
Use ConstCx in more places
1 parent 6a3fb26 commit f0f7a59

File tree

3 files changed

+27
-15
lines changed

3 files changed

+27
-15
lines changed

src/librustc_mir/borrow_check/type_check/mod.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@ use rustc_trait_selection::traits::{self, ObligationCause, PredicateObligations}
4242
use crate::dataflow::move_paths::MoveData;
4343
use crate::dataflow::MaybeInitializedPlaces;
4444
use crate::dataflow::ResultsCursor;
45-
use crate::transform::promote_consts::should_suggest_const_in_array_repeat_expressions_attribute;
45+
use crate::transform::{
46+
check_consts::ConstCx,
47+
promote_consts::should_suggest_const_in_array_repeat_expressions_attribute,
48+
};
4649

4750
use crate::borrow_check::{
4851
borrow_set::BorrowSet,
@@ -1984,14 +1987,17 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
19841987
let span = body.source_info(location).span;
19851988
let ty = operand.ty(body, tcx);
19861989
if !self.infcx.type_is_copy_modulo_regions(self.param_env, ty, span) {
1990+
let ccx = ConstCx::new_with_param_env(
1991+
tcx,
1992+
self.mir_def_id,
1993+
body,
1994+
self.param_env,
1995+
);
19871996
// To determine if `const_in_array_repeat_expressions` feature gate should
19881997
// be mentioned, need to check if the rvalue is promotable.
19891998
let should_suggest =
19901999
should_suggest_const_in_array_repeat_expressions_attribute(
1991-
tcx,
1992-
self.mir_def_id,
1993-
body,
1994-
operand,
2000+
ccx, operand,
19952001
);
19962002
debug!("check_rvalue: should_suggest={:?}", should_suggest);
19972003

src/librustc_mir/transform/check_consts/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,15 @@ pub struct ConstCx<'mir, 'tcx> {
3131
impl ConstCx<'mir, 'tcx> {
3232
pub fn new(tcx: TyCtxt<'tcx>, def_id: DefId, body: &'mir mir::Body<'tcx>) -> Self {
3333
let param_env = tcx.param_env(def_id);
34+
Self::new_with_param_env(tcx, def_id, body, param_env)
35+
}
36+
37+
pub fn new_with_param_env(
38+
tcx: TyCtxt<'tcx>,
39+
def_id: DefId,
40+
body: &'mir mir::Body<'tcx>,
41+
param_env: ty::ParamEnv<'tcx>,
42+
) -> Self {
3443
let const_kind = ConstKind::for_item(tcx, def_id);
3544

3645
ConstCx { body, tcx, def_id, param_env, const_kind }

src/librustc_mir/transform/promote_consts.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1147,22 +1147,19 @@ pub fn promote_candidates<'tcx>(
11471147
/// Feature attribute should be suggested if `operand` can be promoted and the feature is not
11481148
/// enabled.
11491149
crate fn should_suggest_const_in_array_repeat_expressions_attribute<'tcx>(
1150-
tcx: TyCtxt<'tcx>,
1151-
mir_def_id: DefId,
1152-
body: &Body<'tcx>,
1150+
ccx: ConstCx<'_, 'tcx>,
11531151
operand: &Operand<'tcx>,
11541152
) -> bool {
1155-
let mut rpo = traversal::reverse_postorder(&body);
1156-
let (temps, _) = collect_temps_and_candidates(tcx, &body, &mut rpo);
1157-
let validator =
1158-
Validator { ccx: ConstCx::new(tcx, mir_def_id, body), temps: &temps, explicit: false };
1153+
let mut rpo = traversal::reverse_postorder(&ccx.body);
1154+
let (temps, _) = collect_temps_and_candidates(ccx.tcx, &ccx.body, &mut rpo);
1155+
let validator = Validator { ccx, temps: &temps, explicit: false };
11591156

11601157
let should_promote = validator.validate_operand(operand).is_ok();
1161-
let feature_flag = tcx.features().const_in_array_repeat_expressions;
1158+
let feature_flag = validator.ccx.tcx.features().const_in_array_repeat_expressions;
11621159
debug!(
1163-
"should_suggest_const_in_array_repeat_expressions_flag: mir_def_id={:?} \
1160+
"should_suggest_const_in_array_repeat_expressions_flag: def_id={:?} \
11641161
should_promote={:?} feature_flag={:?}",
1165-
mir_def_id, should_promote, feature_flag
1162+
validator.ccx.def_id, should_promote, feature_flag
11661163
);
11671164
should_promote && !feature_flag
11681165
}

0 commit comments

Comments
 (0)