Skip to content

Commit 8b8110e

Browse files
committed
Auto merge of #110728 - cjgillot:no-false-optes, r=oli-obk
Do not bother optimizing impossible functions. This is currently checked by `ConstProp`, but I see no reason to restrict it to ConstProp only.
2 parents e3ccd4b + 0ee32fb commit 8b8110e

File tree

2 files changed

+46
-38
lines changed

2 files changed

+46
-38
lines changed

compiler/rustc_mir_transform/src/const_prop.rs

-37
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ use rustc_middle::ty::{self, ConstKind, Instance, ParamEnv, Ty, TyCtxt, TypeVisi
1818
use rustc_span::{def_id::DefId, Span, DUMMY_SP};
1919
use rustc_target::abi::{self, Align, HasDataLayout, Size, TargetDataLayout};
2020
use rustc_target::spec::abi::Abi as CallAbi;
21-
use rustc_trait_selection::traits;
2221

2322
use crate::MirPass;
2423
use rustc_const_eval::interpret::{
@@ -84,42 +83,6 @@ impl<'tcx> MirPass<'tcx> for ConstProp {
8483
return;
8584
}
8685

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

12588
let dummy_body = &Body::new(

compiler/rustc_mir_transform/src/lib.rs

+46-1
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,12 @@ use rustc_middle::mir::visit::Visitor as _;
3030
use rustc_middle::mir::{
3131
traversal, AnalysisPhase, Body, ClearCrossCrate, ConstQualifs, Constant, LocalDecl, MirPass,
3232
MirPhase, Operand, Place, ProjectionElem, Promoted, RuntimePhase, Rvalue, SourceInfo,
33-
Statement, StatementKind, TerminatorKind,
33+
Statement, StatementKind, TerminatorKind, START_BLOCK,
3434
};
3535
use rustc_middle::ty::query::Providers;
3636
use rustc_middle::ty::{self, TyCtxt, TypeVisitableExt};
3737
use rustc_span::sym;
38+
use rustc_trait_selection::traits;
3839

3940
#[macro_use]
4041
mod pass_manager;
@@ -389,6 +390,50 @@ fn mir_drops_elaborated_and_const_checked(tcx: TyCtxt<'_>, def: LocalDefId) -> &
389390
body.tainted_by_errors = Some(error_reported);
390391
}
391392

393+
// Check if it's even possible to satisfy the 'where' clauses
394+
// for this item.
395+
//
396+
// This branch will never be taken for any normal function.
397+
// However, it's possible to `#!feature(trivial_bounds)]` to write
398+
// a function with impossible to satisfy clauses, e.g.:
399+
// `fn foo() where String: Copy {}`
400+
//
401+
// We don't usually need to worry about this kind of case,
402+
// since we would get a compilation error if the user tried
403+
// to call it. However, since we optimize even without any
404+
// calls to the function, we need to make sure that it even
405+
// makes sense to try to evaluate the body.
406+
//
407+
// If there are unsatisfiable where clauses, then all bets are
408+
// off, and we just give up.
409+
//
410+
// We manually filter the predicates, skipping anything that's not
411+
// "global". We are in a potentially generic context
412+
// (e.g. we are evaluating a function without substituting generic
413+
// parameters, so this filtering serves two purposes:
414+
//
415+
// 1. We skip evaluating any predicates that we would
416+
// never be able prove are unsatisfiable (e.g. `<T as Foo>`
417+
// 2. We avoid trying to normalize predicates involving generic
418+
// parameters (e.g. `<T as Foo>::MyItem`). This can confuse
419+
// the normalization code (leading to cycle errors), since
420+
// it's usually never invoked in this way.
421+
let predicates = tcx
422+
.predicates_of(body.source.def_id())
423+
.predicates
424+
.iter()
425+
.filter_map(|(p, _)| if p.is_global() { Some(*p) } else { None });
426+
if traits::impossible_predicates(tcx, traits::elaborate(tcx, predicates).collect()) {
427+
trace!("found unsatisfiable predicates for {:?}", body.source);
428+
// Clear the body to only contain a single `unreachable` statement.
429+
let bbs = body.basic_blocks.as_mut();
430+
bbs.raw.truncate(1);
431+
bbs[START_BLOCK].statements.clear();
432+
bbs[START_BLOCK].terminator_mut().kind = TerminatorKind::Unreachable;
433+
body.var_debug_info.clear();
434+
body.local_decls.raw.truncate(body.arg_count + 1);
435+
}
436+
392437
run_analysis_to_runtime_passes(tcx, &mut body);
393438

394439
tcx.alloc_steal_mir(body)

0 commit comments

Comments
 (0)