diff options
| author | Matthias Krüger <476013+matthiaskrgr@users.noreply.github.com> | 2025-07-11 07:35:18 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-07-11 07:35:18 +0200 |
| commit | 3d6eb225e26448b09fdec1ba7222f3e7174c6ac2 (patch) | |
| tree | 17d044aac71ec4a410ebbb33c8f8349d46d395e1 /tests/ui/lifetimes | |
| parent | 2730bebbf88ae9473b339bb7f66eb77fb2c9fa6f (diff) | |
| parent | 3ad95cccf9b3af7e527869a1eb130217971b7a57 (diff) | |
| download | rust-3d6eb225e26448b09fdec1ba7222f3e7174c6ac2.tar.gz rust-3d6eb225e26448b09fdec1ba7222f3e7174c6ac2.zip | |
Rollup merge of #143302 - Kivooeo:tf27, r=tgross35
`tests/ui`: A New Order [27/N] > [!NOTE] > > Intermediate commits are intended to help review, but will be squashed prior to merge. Some `tests/ui/` housekeeping, to trim down number of tests directly under `tests/ui/`. Part of rust-lang/rust#133895. r? ``@tgross35``
Diffstat (limited to 'tests/ui/lifetimes')
| -rw-r--r-- | tests/ui/lifetimes/any-lifetime-escape-higher-rank.rs | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/tests/ui/lifetimes/any-lifetime-escape-higher-rank.rs b/tests/ui/lifetimes/any-lifetime-escape-higher-rank.rs new file mode 100644 index 00000000000..f9f38ee532d --- /dev/null +++ b/tests/ui/lifetimes/any-lifetime-escape-higher-rank.rs @@ -0,0 +1,37 @@ +//! Checks that `std::any::Any` cannot be used to circumvent lifetime rules +//! with higher-rank types. + +//@ run-pass + +use std::any::Any; + +struct Foo<'a>(&'a str); + +fn good(s: &String) -> Foo<'_> { + Foo(s) +} + +fn bad1(s: String) -> Option<&'static str> { + let a: Box<dyn Any> = Box::new(good as fn(&String) -> Foo); + a.downcast_ref::<fn(&String) -> Foo<'static>>().map(|f| f(&s).0) +} + +trait AsStr<'a, 'b> { + fn get(&'a self) -> &'b str; +} + +impl<'a> AsStr<'a, 'a> for String { + fn get(&'a self) -> &'a str { + self + } +} + +fn bad2(s: String) -> Option<&'static str> { + let a: Box<dyn Any> = Box::new(Box::new(s) as Box<dyn for<'a> AsStr<'a, 'a>>); + a.downcast_ref::<Box<dyn for<'a> AsStr<'a, 'static>>>().map(|x| x.get()) +} + +fn main() { + assert_eq!(bad1(String::from("foo")), None); + assert_eq!(bad2(String::from("bar")), None); +} |
