Skip to content

Commit 77345cc

Browse files
committed
Use ConstCx in more places
1 parent 85e2c32 commit 77345cc

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,
@@ -1997,14 +2000,17 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
19972000
let span = body.source_info(location).span;
19982001
let ty = operand.ty(*body, tcx);
19992002
if !self.infcx.type_is_copy_modulo_regions(self.param_env, ty, span) {
2003+
let ccx = ConstCx::new_with_param_env(
2004+
tcx,
2005+
self.mir_def_id,
2006+
body,
2007+
self.param_env,
2008+
);
20002009
// To determine if `const_in_array_repeat_expressions` feature gate should
20012010
// be mentioned, need to check if the rvalue is promotable.
20022011
let should_suggest =
20032012
should_suggest_const_in_array_repeat_expressions_attribute(
2004-
tcx,
2005-
self.mir_def_id,
2006-
body,
2007-
operand,
2013+
ccx, operand,
20082014
);
20092015
debug!("check_rvalue: should_suggest={:?}", should_suggest);
20102016

src/librustc_mir/transform/check_consts/mod.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,15 @@ impl ConstCx<'mir, 'tcx> {
3535
body: mir::ReadOnlyBodyAndCache<'mir, 'tcx>,
3636
) -> Self {
3737
let param_env = tcx.param_env(def_id);
38+
Self::new_with_param_env(tcx, def_id, body, param_env)
39+
}
40+
41+
pub fn new_with_param_env(
42+
tcx: TyCtxt<'tcx>,
43+
def_id: DefId,
44+
body: mir::ReadOnlyBodyAndCache<'mir, 'tcx>,
45+
param_env: ty::ParamEnv<'tcx>,
46+
) -> Self {
3847
let const_kind = ConstKind::for_item(tcx, def_id);
3948

4049
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
@@ -1153,22 +1153,19 @@ pub fn promote_candidates<'tcx>(
11531153
/// Feature attribute should be suggested if `operand` can be promoted and the feature is not
11541154
/// enabled.
11551155
crate fn should_suggest_const_in_array_repeat_expressions_attribute<'tcx>(
1156-
tcx: TyCtxt<'tcx>,
1157-
mir_def_id: DefId,
1158-
body: ReadOnlyBodyAndCache<'_, 'tcx>,
1156+
ccx: ConstCx<'_, 'tcx>,
11591157
operand: &Operand<'tcx>,
11601158
) -> bool {
1161-
let mut rpo = traversal::reverse_postorder(&body);
1162-
let (temps, _) = collect_temps_and_candidates(tcx, &body, &mut rpo);
1163-
let validator =
1164-
Validator { ccx: ConstCx::new(tcx, mir_def_id, body), temps: &temps, explicit: false };
1159+
let mut rpo = traversal::reverse_postorder(&ccx.body);
1160+
let (temps, _) = collect_temps_and_candidates(ccx.tcx, &ccx.body, &mut rpo);
1161+
let validator = Validator { ccx, temps: &temps, explicit: false };
11651162

11661163
let should_promote = validator.validate_operand(operand).is_ok();
1167-
let feature_flag = tcx.features().const_in_array_repeat_expressions;
1164+
let feature_flag = validator.ccx.tcx.features().const_in_array_repeat_expressions;
11681165
debug!(
1169-
"should_suggest_const_in_array_repeat_expressions_flag: mir_def_id={:?} \
1166+
"should_suggest_const_in_array_repeat_expressions_flag: def_id={:?} \
11701167
should_promote={:?} feature_flag={:?}",
1171-
mir_def_id, should_promote, feature_flag
1168+
validator.ccx.def_id, should_promote, feature_flag
11721169
);
11731170
should_promote && !feature_flag
11741171
}

0 commit comments

Comments
 (0)