Skip to content

Commit 1409363

Browse files
committed
Auto merge of #57282 - matthewjasper:wellformed-return-ty, r=nikomatsakis
Wf-check the output type of a function in MIR-typeck Closes #57265 cc @scalexm
2 parents ec19464 + 8ca83e9 commit 1409363

File tree

4 files changed

+40
-2
lines changed

4 files changed

+40
-2
lines changed

src/librustc_mir/borrow_check/nll/type_check/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1452,7 +1452,7 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
14521452
self.check_call_dest(mir, term, &sig, destination, term_location);
14531453

14541454
self.prove_predicates(
1455-
sig.inputs().iter().map(|ty| ty::Predicate::WellFormed(ty)),
1455+
sig.inputs_and_output.iter().map(|ty| ty::Predicate::WellFormed(ty)),
14561456
term_location.to_locations(),
14571457
ConstraintCategory::Boring,
14581458
);

src/librustc_mir/interpret/machine.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ pub trait Machine<'a, 'mir, 'tcx>: Sized {
7676
type MemoryExtra: Default;
7777

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

8181
/// Memory's allocation map
8282
type MemoryMap:
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#![feature(nll)]
2+
3+
use std::any::Any;
4+
5+
#[derive(Debug, Clone)]
6+
struct S<T: 'static>(T);
7+
8+
// S<&'a T> is in the return type, so we get an implied bound
9+
// &'a T: 'static
10+
fn foo<'a, T>(x: &'a T) -> (S<&'a T>, Box<dyn Any + 'static>) {
11+
let y = S(x);
12+
13+
let z = Box::new(y.clone()) as Box<dyn Any + 'static>;
14+
(y, z)
15+
}
16+
17+
fn main() {
18+
let x = 5;
19+
20+
// Check that we require that the argument is of type `&'static String`,
21+
// so that the return type is well-formed.
22+
let (_, z) = foo(&"hello".to_string());
23+
//~^ ERROR temporary value dropped while borrowed
24+
25+
println!("{:?}", z.downcast_ref::<S<&'static String>>());
26+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error[E0716]: temporary value dropped while borrowed
2+
--> $DIR/issue-57265-return-type-wf-check.rs:22:23
3+
|
4+
LL | let (_, z) = foo(&"hello".to_string());
5+
| -----^^^^^^^^^^^^^^^^^^^-- temporary value is freed at the end of this statement
6+
| | |
7+
| | creates a temporary which is freed while still in use
8+
| argument requires that borrow lasts for `'static`
9+
10+
error: aborting due to previous error
11+
12+
For more information about this error, try `rustc --explain E0716`.

0 commit comments

Comments
 (0)