Skip to content

Commit 9b7e0af

Browse files
authored
Rollup merge of rust-lang#116315 - cjgillot:cpl-clean, r=petrochenkov
Do not check for impossible predicates in const-prop lint. The enclosing query already checks for them, and replaces the body with a single `unreachable` if they are indeed impossible.
2 parents 8ddc0df + 465a82f commit 9b7e0af

File tree

2 files changed

+11
-50
lines changed

2 files changed

+11
-50
lines changed

compiler/rustc_mir_transform/src/const_prop_lint.rs

+10-47
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ use rustc_middle::ty::{
2222
};
2323
use rustc_span::Span;
2424
use rustc_target::abi::{HasDataLayout, Size, TargetDataLayout};
25-
use rustc_trait_selection::traits;
2625

2726
use crate::const_prop::CanConstProp;
2827
use crate::const_prop::ConstPropMachine;
@@ -35,9 +34,9 @@ use crate::MirLint;
3534
/// Severely regress performance.
3635
const MAX_ALLOC_LIMIT: u64 = 1024;
3736

38-
pub struct ConstProp;
37+
pub struct ConstPropLint;
3938

40-
impl<'tcx> MirLint<'tcx> for ConstProp {
39+
impl<'tcx> MirLint<'tcx> for ConstPropLint {
4140
fn run_lint(&self, tcx: TyCtxt<'tcx>, body: &Body<'tcx>) {
4241
if body.tainted_by_errors.is_some() {
4342
return;
@@ -49,61 +48,25 @@ impl<'tcx> MirLint<'tcx> for ConstProp {
4948
}
5049

5150
let def_id = body.source.def_id().expect_local();
52-
let is_fn_like = tcx.def_kind(def_id).is_fn_like();
53-
let is_assoc_const = tcx.def_kind(def_id) == DefKind::AssocConst;
51+
let def_kind = tcx.def_kind(def_id);
52+
let is_fn_like = def_kind.is_fn_like();
53+
let is_assoc_const = def_kind == DefKind::AssocConst;
5454

5555
// Only run const prop on functions, methods, closures and associated constants
5656
if !is_fn_like && !is_assoc_const {
5757
// skip anon_const/statics/consts because they'll be evaluated by miri anyway
58-
trace!("ConstProp skipped for {:?}", def_id);
58+
trace!("ConstPropLint skipped for {:?}", def_id);
5959
return;
6060
}
6161

62-
let is_generator = tcx.type_of(def_id.to_def_id()).instantiate_identity().is_generator();
6362
// FIXME(welseywiser) const prop doesn't work on generators because of query cycles
6463
// computing their layout.
65-
if is_generator {
66-
trace!("ConstProp skipped for generator {:?}", def_id);
64+
if let DefKind::Generator = def_kind {
65+
trace!("ConstPropLint skipped for generator {:?}", def_id);
6766
return;
6867
}
6968

70-
// Check if it's even possible to satisfy the 'where' clauses
71-
// for this item.
72-
// This branch will never be taken for any normal function.
73-
// However, it's possible to `#!feature(trivial_bounds)]` to write
74-
// a function with impossible to satisfy clauses, e.g.:
75-
// `fn foo() where String: Copy {}`
76-
//
77-
// We don't usually need to worry about this kind of case,
78-
// since we would get a compilation error if the user tried
79-
// to call it. However, since we can do const propagation
80-
// even without any calls to the function, we need to make
81-
// sure that it even makes sense to try to evaluate the body.
82-
// If there are unsatisfiable where clauses, then all bets are
83-
// off, and we just give up.
84-
//
85-
// We manually filter the predicates, skipping anything that's not
86-
// "global". We are in a potentially generic context
87-
// (e.g. we are evaluating a function without substituting generic
88-
// parameters, so this filtering serves two purposes:
89-
//
90-
// 1. We skip evaluating any predicates that we would
91-
// never be able prove are unsatisfiable (e.g. `<T as Foo>`
92-
// 2. We avoid trying to normalize predicates involving generic
93-
// parameters (e.g. `<T as Foo>::MyItem`). This can confuse
94-
// the normalization code (leading to cycle errors), since
95-
// it's usually never invoked in this way.
96-
let predicates = tcx
97-
.predicates_of(def_id.to_def_id())
98-
.predicates
99-
.iter()
100-
.filter_map(|(p, _)| if p.is_global() { Some(*p) } else { None });
101-
if traits::impossible_predicates(tcx, traits::elaborate(tcx, predicates).collect()) {
102-
trace!("ConstProp skipped for {:?}: found unsatisfiable predicates", def_id);
103-
return;
104-
}
105-
106-
trace!("ConstProp starting for {:?}", def_id);
69+
trace!("ConstPropLint starting for {:?}", def_id);
10770

10871
// FIXME(oli-obk, eddyb) Optimize locals (or even local paths) to hold
10972
// constants, instead of just checking for const-folding succeeding.
@@ -112,7 +75,7 @@ impl<'tcx> MirLint<'tcx> for ConstProp {
11275
let mut linter = ConstPropagator::new(body, tcx);
11376
linter.visit_body(body);
11477

115-
trace!("ConstProp done for {:?}", def_id);
78+
trace!("ConstPropLint done for {:?}", def_id);
11679
}
11780
}
11881

compiler/rustc_mir_transform/src/lib.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,7 @@ fn run_runtime_lowering_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
496496
&elaborate_box_derefs::ElaborateBoxDerefs,
497497
&generator::StateTransform,
498498
&add_retag::AddRetag,
499-
&Lint(const_prop_lint::ConstProp),
499+
&Lint(const_prop_lint::ConstPropLint),
500500
];
501501
pm::run_passes_no_validate(tcx, body, passes, Some(MirPhase::Runtime(RuntimePhase::Initial)));
502502
}
@@ -554,8 +554,6 @@ fn run_optimization_passes<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
554554
&const_prop::ConstProp,
555555
&gvn::GVN,
556556
&dataflow_const_prop::DataflowConstProp,
557-
//
558-
// Const-prop runs unconditionally, but doesn't mutate the MIR at mir-opt-level=0.
559557
&const_debuginfo::ConstDebugInfo,
560558
&o1(simplify_branches::SimplifyConstCondition::AfterConstProp),
561559
&early_otherwise_branch::EarlyOtherwiseBranch,

0 commit comments

Comments
 (0)