Skip to content

Commit d670919

Browse files
committed
auto merge of #18105 : nikomatsakis/rust/issue-18055, r=pcwalton
Check object lifetime bounds in coercions, not just trait bounds. Fixes #18055. r? @pcwalton This is a [breaking change]. Change code like this: fn foo(v: &[u8]) -> Box<Clone+'static> { ... } to make the lifetimes agree: // either... fn foo(v: &'static[u8]) -> Box<Clone+'static> { box v } // or ... fn foo<'a>(v: &'a [u8]) -> Box<Clone+'a> { box v }
2 parents 41a7910 + 7876cf9 commit d670919

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

src/librustc/middle/typeck/check/mod.rs

+7
Original file line numberDiff line numberDiff line change
@@ -1681,10 +1681,17 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
16811681
self.register_unsize_obligations(span, &**u)
16821682
}
16831683
ty::UnsizeVtable(ref ty_trait, self_ty) => {
1684+
// If the type is `Foo+'a`, ensures that the type
1685+
// being cast to `Foo+'a` implements `Foo`:
16841686
vtable2::register_object_cast_obligations(self,
16851687
span,
16861688
ty_trait,
16871689
self_ty);
1690+
1691+
// If the type is `Foo+'a`, ensures that the type
1692+
// being cast to `Foo+'a` outlives `'a`:
1693+
let origin = infer::RelateObjectBound(span);
1694+
self.register_region_obligation(origin, self_ty, ty_trait.bounds.region_bound);
16881695
}
16891696
}
16901697
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// Test that attempts to implicitly coerce a value into an
12+
// object respect the lifetime bound on the object type.
13+
14+
fn a(v: &[u8]) -> Box<Clone + 'static> {
15+
let x: Box<Clone + 'static> = box v; //~ ERROR does not outlive
16+
x
17+
}
18+
19+
fn b(v: &[u8]) -> Box<Clone + 'static> {
20+
box v //~ ERROR does not outlive
21+
}
22+
23+
fn c(v: &[u8]) -> Box<Clone> {
24+
box v // OK thanks to lifetime elision
25+
}
26+
27+
fn d<'a,'b>(v: &'a [u8]) -> Box<Clone+'b> {
28+
box v //~ ERROR does not outlive
29+
}
30+
31+
fn e<'a:'b,'b>(v: &'a [u8]) -> Box<Clone+'b> {
32+
box v // OK, thanks to 'a:'b
33+
}
34+
35+
fn main() { }

0 commit comments

Comments
 (0)