Skip to content

Commit 5dfc5f2

Browse files
committed
Wf-check the output type of a function in MIR-typeck
1 parent ec19464 commit 5dfc5f2

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
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
);
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)