Skip to content

Commit 5896998

Browse files
committed
Don't run const propagation on items with inconsistent bounds
Using `#![feature(trivial_bounds)]`, it's possible to write functions with unsatisfiable 'where' clauses, making them uncallable. However, the user can act as if these 'where' clauses are true inside the body of the function, leading to code that would normally be impossible to write. Since const propgation can run even without any user-written calls to a function, we need to explcitly check for these uncallable functions.
1 parent e82febc commit 5896998

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

src/librustc_mir/transform/const_prop.rs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,31 @@ impl<'tcx> MirPass<'tcx> for ConstProp {
7474
return;
7575
}
7676

77+
// Check if it's even possible to satisy the 'where' clauses
78+
// for this item.
79+
// This branch will never be taken for any normal function.
80+
// However, it's possible to `#!feature(trivial_bounds)]` to write
81+
// a function with impossible to satisfy clauses, e.g.:
82+
// `fn foo() where String: Copy {}`
83+
//
84+
// We don't usually need to worry about this kind of case,
85+
// since we would get a compilation error if the user tried
86+
// to call it. However, since we can do const propagation
87+
// even without any calls to the function, we need to make
88+
// sure that it even makes sense to try to evaluate the body.
89+
// If there are unsatisfiable where clauses, then all bets are
90+
// off, and we just give up.
91+
if !tcx.substitute_normalize_and_test_predicates((
92+
source.def_id(),
93+
InternalSubsts::identity_for_item(tcx, source.def_id()),
94+
)) {
95+
trace!(
96+
"ConstProp skipped for item with unsatisfiable predicates: {:?}",
97+
source.def_id()
98+
);
99+
return;
100+
}
101+
77102
trace!("ConstProp starting for {:?}", source.def_id());
78103

79104
let dummy_body = &Body::new(

src/test/ui/trivial-bounds/trivial-bounds-inconsistent-associated-functions.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
// run-pass
2+
// Force mir to be emitted, to ensure that const
3+
// propagation doesn't ICE on a function
4+
// with an 'impossible' body. See issue #67696
5+
// compile-flags: --emit=mir,link
26
// Inconsistent bounds with trait implementations
37

48
#![feature(trivial_bounds)]
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
warning: due to multiple output types requested, the explicitly specified output file name will be adapted for each output type
2+

0 commit comments

Comments
 (0)