-
-
Notifications
You must be signed in to change notification settings - Fork 14.9k
Expand file tree
/
Copy pathobject-lifetime-default-assoc-ty-binding-item-bounds.rs
More file actions
37 lines (29 loc) · 1.34 KB
/
object-lifetime-default-assoc-ty-binding-item-bounds.rs
File metadata and controls
37 lines (29 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
// Ideally, given an assoc type binding `dyn Trait<AssocTy = Ty>`, we'd factor in the item bounds of
// assoc type `AssocTy` when computing the trait object lifetime default for type `Ty`.
//
// However, since the current implementation can't handle this we instead conservatively and hackily
// treat the trait object lifetime default of the RHS as indeterminate if any lifetime arguments are
// passed to the trait ref (or the GAT) thus rejecting any implicit trait object lifetime bounds.
// This way, we can still implement the desired behavior in the future.
trait Foo<'a> {
type Item: ?Sized;
fn item(&self) -> Box<Self::Item> { panic!() }
}
trait Bar {}
impl<T> Foo<'_> for T {
type Item = dyn Bar;
}
fn is_static<T>(_: T) where T: 'static {}
// FIXME: Ideally, we'd elaborate `dyn Bar` to `dyn Bar + 'static` instead of rejecting it.
fn bar<'a>(x: &'a str) -> &'a dyn Foo<'a, Item = dyn Bar> { &() }
//~^ ERROR cannot deduce the lifetime bound for this trait object type from context
// FIXME: Ideally, we'd elaborate `dyn Bar` to `dyn Bar + 'static` instead of rejecting it.
fn baz(x: &str) -> &dyn Foo<Item = dyn Bar> { &() }
//~^ ERROR cannot deduce the lifetime bound for this trait object type from context
fn main() {
let s = format!("foo");
let r = bar(&s);
is_static(r.item());
let r = baz(&s);
is_static(r.item());
}