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.
This commit is contained in:
Aaron Hill 2020-01-05 22:32:53 -05:00
parent e82febc78e
commit 5896998e76
No known key found for this signature in database
GPG Key ID: B4087E510E98B164
3 changed files with 31 additions and 0 deletions

View File

@ -74,6 +74,31 @@ impl<'tcx> MirPass<'tcx> for ConstProp {
return;
}
// Check if it's even possible to satisy the 'where' clauses
// for this item.
// This branch will never be taken for any normal function.
// However, it's possible to `#!feature(trivial_bounds)]` to write
// a function with impossible to satisfy clauses, e.g.:
// `fn foo() where String: Copy {}`
//
// We don't usually need to worry about this kind of case,
// since we would get a compilation error if the user tried
// to call it. However, since we can do const propagation
// even without any calls to the function, we need to make
// sure that it even makes sense to try to evaluate the body.
// If there are unsatisfiable where clauses, then all bets are
// off, and we just give up.
if !tcx.substitute_normalize_and_test_predicates((
source.def_id(),
InternalSubsts::identity_for_item(tcx, source.def_id()),
)) {
trace!(
"ConstProp skipped for item with unsatisfiable predicates: {:?}",
source.def_id()
);
return;
}
trace!("ConstProp starting for {:?}", source.def_id());
let dummy_body = &Body::new(

View File

@ -1,4 +1,8 @@
// run-pass
// Force mir to be emitted, to ensure that const
// propagation doesn't ICE on a function
// with an 'impossible' body. See issue #67696
// compile-flags: --emit=mir,link
// Inconsistent bounds with trait implementations
#![feature(trivial_bounds)]

View File

@ -0,0 +1,2 @@
warning: due to multiple output types requested, the explicitly specified output file name will be adapted for each output type