Skip to content

Commit

Permalink
Auto merge of #57282 - matthewjasper:wellformed-return-ty, r=nikomats…
Browse files Browse the repository at this point in the history
…akis

Wf-check the output type of a function in MIR-typeck

Closes #57265

cc @scalexm
  • Loading branch information
bors committed Jan 3, 2019
2 parents ec19464 + 8ca83e9 commit 1409363
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/librustc_mir/borrow_check/nll/type_check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1452,7 +1452,7 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
self.check_call_dest(mir, term, &sig, destination, term_location);

self.prove_predicates(
sig.inputs().iter().map(|ty| ty::Predicate::WellFormed(ty)),
sig.inputs_and_output.iter().map(|ty| ty::Predicate::WellFormed(ty)),
term_location.to_locations(),
ConstraintCategory::Boring,
);
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_mir/interpret/machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ pub trait Machine<'a, 'mir, 'tcx>: Sized {
type MemoryExtra: Default;

/// Extra data stored in every allocation.
type AllocExtra: AllocationExtra<Self::PointerTag, Self::MemoryExtra>;
type AllocExtra: AllocationExtra<Self::PointerTag, Self::MemoryExtra> + 'static;

/// Memory's allocation map
type MemoryMap:
Expand Down
26 changes: 26 additions & 0 deletions src/test/ui/nll/issue-57265-return-type-wf-check.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#![feature(nll)]

use std::any::Any;

#[derive(Debug, Clone)]
struct S<T: 'static>(T);

// S<&'a T> is in the return type, so we get an implied bound
// &'a T: 'static
fn foo<'a, T>(x: &'a T) -> (S<&'a T>, Box<dyn Any + 'static>) {
let y = S(x);

let z = Box::new(y.clone()) as Box<dyn Any + 'static>;
(y, z)
}

fn main() {
let x = 5;

// Check that we require that the argument is of type `&'static String`,
// so that the return type is well-formed.
let (_, z) = foo(&"hello".to_string());
//~^ ERROR temporary value dropped while borrowed

println!("{:?}", z.downcast_ref::<S<&'static String>>());
}
12 changes: 12 additions & 0 deletions src/test/ui/nll/issue-57265-return-type-wf-check.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
error[E0716]: temporary value dropped while borrowed
--> $DIR/issue-57265-return-type-wf-check.rs:22:23
|
LL | let (_, z) = foo(&"hello".to_string());
| -----^^^^^^^^^^^^^^^^^^^-- temporary value is freed at the end of this statement
| | |
| | creates a temporary which is freed while still in use
| argument requires that borrow lasts for `'static`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0716`.

0 comments on commit 1409363

Please sign in to comment.