forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of rust-lang#105546 - JohnTitor:issue-44454, r=compiler-…
…errors Add some regression tests for rust-lang#44454 Closes rust-lang#44454 r? `@compiler-errors` Signed-off-by: Yuki Okushi <jtitor@2k36.org>
- Loading branch information
Showing
6 changed files
with
115 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Taken from https://github.com/rust-lang/rust/issues/44454#issue-256435333 | ||
|
||
trait Animal<X>: 'static {} | ||
|
||
fn foo<Y, X>() | ||
where | ||
Y: Animal<X> + ?Sized, | ||
{ | ||
// `Y` implements `Animal<X>` so `Y` is 'static. | ||
baz::<Y>() | ||
} | ||
|
||
fn bar<'a>(_arg: &'a i32) { | ||
foo::<dyn Animal<&'a i32>, &'a i32>() //~ ERROR: lifetime may not live long enough | ||
} | ||
|
||
fn baz<T: 'static + ?Sized>() {} | ||
|
||
fn main() { | ||
let a = 5; | ||
bar(&a); | ||
} |
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,10 @@ | ||
error: lifetime may not live long enough | ||
--> $DIR/issue-44454-1.rs:14:5 | ||
| | ||
LL | fn bar<'a>(_arg: &'a i32) { | ||
| -- lifetime `'a` defined here | ||
LL | foo::<dyn Animal<&'a i32>, &'a i32>() | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ requires that `'a` must outlive `'static` | ||
|
||
error: aborting due to previous error | ||
|
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,22 @@ | ||
// Taken from https://github.com/rust-lang/rust/issues/44454#issuecomment-1175925928 | ||
|
||
trait Trait<ARG: 'static>: 'static { | ||
type Assoc: AsRef<str>; | ||
} | ||
|
||
fn hr<T: ?Sized, ARG>(x: T::Assoc) -> Box<dyn AsRef<str> + 'static> | ||
where | ||
T: Trait<ARG> | ||
{ | ||
Box::new(x) | ||
} | ||
|
||
fn extend_lt<'a>(x: &'a str) -> Box<dyn AsRef<str> + 'static> { | ||
type DynTrait = dyn for<'a> Trait<&'a str, Assoc = &'a str>; | ||
hr::<DynTrait, _>(x) //~ ERROR: borrowed data escapes outside of function | ||
} | ||
|
||
fn main() { | ||
let extended = extend_lt(&String::from("hello")); | ||
println!("{}", extended.as_ref().as_ref()); | ||
} |
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,17 @@ | ||
error[E0521]: borrowed data escapes outside of function | ||
--> $DIR/issue-44454-2.rs:16:5 | ||
| | ||
LL | fn extend_lt<'a>(x: &'a str) -> Box<dyn AsRef<str> + 'static> { | ||
| -- - `x` is a reference that is only valid in the function body | ||
| | | ||
| lifetime `'a` defined here | ||
LL | type DynTrait = dyn for<'a> Trait<&'a str, Assoc = &'a str>; | ||
LL | hr::<DynTrait, _>(x) | ||
| ^^^^^^^^^^^^^^^^^^^^ | ||
| | | ||
| `x` escapes the function body here | ||
| argument requires that `'a` must outlive `'static` | ||
|
||
error: aborting due to previous error | ||
|
||
For more information about this error, try `rustc --explain E0521`. |
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,33 @@ | ||
// Taken from https://github.com/rust-lang/rust/issues/44454#issuecomment-1332781290 | ||
|
||
use std::any::Any; | ||
|
||
trait Animal<X>: 'static {} | ||
|
||
trait Projector { | ||
type Foo; | ||
} | ||
|
||
impl<X> Projector for dyn Animal<X> { | ||
type Foo = X; | ||
} | ||
|
||
fn make_static<'a, T>(t: &'a T) -> &'static T { | ||
let x: <dyn Animal<&'a T> as Projector>::Foo = t; | ||
let any = generic::<dyn Animal<&'a T>, &'a T>(x); | ||
//~^ ERROR: lifetime may not live long enough | ||
any.downcast_ref::<&'static T>().unwrap() | ||
} | ||
|
||
fn generic<T: Projector + Animal<U> + ?Sized, U>(x: <T as Projector>::Foo) -> Box<dyn Any> { | ||
make_static_any(x) | ||
} | ||
|
||
fn make_static_any<U: 'static>(u: U) -> Box<dyn Any> { | ||
Box::new(u) | ||
} | ||
|
||
fn main() { | ||
let a = make_static(&"salut".to_string()); | ||
println!("{}", *a); | ||
} |
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,11 @@ | ||
error: lifetime may not live long enough | ||
--> $DIR/issue-44454-3.rs:17:15 | ||
| | ||
LL | fn make_static<'a, T>(t: &'a T) -> &'static T { | ||
| -- lifetime `'a` defined here | ||
LL | let x: <dyn Animal<&'a T> as Projector>::Foo = t; | ||
LL | let any = generic::<dyn Animal<&'a T>, &'a T>(x); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ requires that `'a` must outlive `'static` | ||
|
||
error: aborting due to previous error | ||
|