-
Notifications
You must be signed in to change notification settings - Fork 12.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
use static as object-lifetime default for type XX in
Foo<Item=XX>
Currently the default is "inherited" from context, so e.g. `&impl Foo<Item = dyn Bar>` would default to `&'x impl Foo<Item = dyn Bar + 'x>`, but this triggers an ICE and is not very consistent. This patch doesn't implement what I expect would be the correct semantics, because those are likely too complex. Instead, it handles what I'd expect to be the common case -- where the trait has no lifetime parameters.
- Loading branch information
1 parent
282cc02
commit afa4a2a
Showing
9 changed files
with
229 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
src/test/ui/impl-trait/dyn-trait-elided-two-inputs-ref-assoc.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Test that we don't get an error with `dyn Bar` in an impl Trait | ||
// when there are multiple inputs. The `dyn Bar` should default to `+ | ||
// 'static`. This used to erroneously generate an error (cc #62517). | ||
// | ||
// check-pass | ||
|
||
trait Foo { | ||
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 { } | ||
|
||
fn bar(x: &str) -> &impl Foo<Item = dyn Bar> { &() } | ||
|
||
fn main() { | ||
let s = format!("foo"); | ||
let r = bar(&s); | ||
is_static(r.item()); | ||
} |
27 changes: 27 additions & 0 deletions
27
src/test/ui/object-lifetime/object-lifetime-default-dyn-binding-nonstatic1.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// Test that `dyn Bar<Item = XX>` uses `'static` as the default object | ||
// lifetime bound for the type `XX`. | ||
|
||
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 { } | ||
|
||
// Here, we should default to `dyn Bar + 'static`, but the current | ||
// code forces us into a conservative, hacky path. | ||
fn bar<'a>(x: &'a str) -> &'a dyn Foo<'a, Item = dyn Bar> { &() } | ||
//~^ ERROR please supply an explicit bound | ||
|
||
fn main() { | ||
let s = format!("foo"); | ||
let r = bar(&s); | ||
is_static(r.item()); | ||
} |
8 changes: 8 additions & 0 deletions
8
src/test/ui/object-lifetime/object-lifetime-default-dyn-binding-nonstatic1.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
error[E0228]: the lifetime bound for this object type cannot be deduced from context; please supply an explicit bound | ||
--> $DIR/object-lifetime-default-dyn-binding-nonstatic1.rs:20:50 | ||
| | ||
LL | fn bar<'a>(x: &'a str) -> &'a dyn Foo<'a, Item = dyn Bar> { &() } | ||
| ^^^^^^^ | ||
|
||
error: aborting due to previous error | ||
|
30 changes: 30 additions & 0 deletions
30
src/test/ui/object-lifetime/object-lifetime-default-dyn-binding-nonstatic2.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Test that `dyn Bar<Item = XX>` uses `'static` as the default object | ||
// lifetime bound for the type `XX`. | ||
|
||
trait Foo<'a> { | ||
type Item: 'a + ?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 { } | ||
|
||
// Here, we default to `dyn Bar + 'a`. Or, we *should*, but the | ||
// current code forces us into a conservative, hacky path. | ||
fn bar<'a>(x: &'a str) -> &'a dyn Foo<'a, Item = dyn Bar> { &() } | ||
//~^ ERROR please supply an explicit bound | ||
|
||
fn main() { | ||
let s = format!("foo"); | ||
let r = bar(&s); | ||
|
||
// If it weren't for the conservative path above, we'd expect an | ||
// error here. | ||
is_static(r.item()); | ||
} |
8 changes: 8 additions & 0 deletions
8
src/test/ui/object-lifetime/object-lifetime-default-dyn-binding-nonstatic2.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
error[E0228]: the lifetime bound for this object type cannot be deduced from context; please supply an explicit bound | ||
--> $DIR/object-lifetime-default-dyn-binding-nonstatic2.rs:20:50 | ||
| | ||
LL | fn bar<'a>(x: &'a str) -> &'a dyn Foo<'a, Item = dyn Bar> { &() } | ||
| ^^^^^^^ | ||
|
||
error: aborting due to previous error | ||
|
23 changes: 23 additions & 0 deletions
23
src/test/ui/object-lifetime/object-lifetime-default-dyn-binding-nonstatic3.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// Test that `dyn Bar<Item = XX>` uses `'static` as the default object | ||
// lifetime bound for the type `XX`. | ||
|
||
trait Foo<'a> { | ||
type Item: ?Sized; | ||
|
||
fn item(&self) -> Box<Self::Item> { panic!() } | ||
} | ||
|
||
trait Bar { } | ||
|
||
fn is_static<T>(_: T) where T: 'static { } | ||
|
||
// Here, we should default to `dyn Bar + 'static`, but the current | ||
// code forces us into a conservative, hacky path. | ||
fn bar(x: &str) -> &dyn Foo<Item = dyn Bar> { &() } | ||
//~^ ERROR please supply an explicit bound | ||
|
||
fn main() { | ||
let s = format!("foo"); | ||
let r = bar(&s); | ||
is_static(r.item()); | ||
} |
8 changes: 8 additions & 0 deletions
8
src/test/ui/object-lifetime/object-lifetime-default-dyn-binding-nonstatic3.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
error[E0228]: the lifetime bound for this object type cannot be deduced from context; please supply an explicit bound | ||
--> $DIR/object-lifetime-default-dyn-binding-nonstatic3.rs:16:36 | ||
| | ||
LL | fn bar(x: &str) -> &dyn Foo<Item = dyn Bar> { &() } | ||
| ^^^^^^^ | ||
|
||
error: aborting due to previous error | ||
|
28 changes: 28 additions & 0 deletions
28
src/test/ui/object-lifetime/object-lifetime-default-dyn-binding-static.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// Test that `dyn Bar<Item = XX>` uses `'static` as the default object | ||
// lifetime bound for the type `XX`. | ||
// | ||
// check-pass | ||
|
||
trait Foo { | ||
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 { } | ||
|
||
// Here, we default to `dyn Bar + 'static`, and not `&'x dyn Foo<Item | ||
// = dyn Bar + 'x>`. | ||
fn bar(x: &str) -> &dyn Foo<Item = dyn Bar> { &() } | ||
|
||
fn main() { | ||
let s = format!("foo"); | ||
let r = bar(&s); | ||
is_static(r.item()); | ||
} |