-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Incoherent (?) Lifetime HRTB on associated type results in unsoundness in stable, safe code #141713
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Fun ✨ trait WriteWhereClause<T>: FnOnce() -> T {}
impl<F: FnOnce() -> T, T> WriteWhereClause<T> for F {}
fn takes_any_requires_static<F>(_: F, any_lt: F::Output) -> &'static str
where
// Where-clause rejected by HIR lowering. This must not be soundness-critical,
// as elaboration can give us the exact same where-clause regardless. It should
// simply not be possible to prove it.
// F: for<'a> FnOnce() -> &'a String,
// Elaborating the below where-clause does work. This is #136547.
F: for<'a> WriteWhereClause<&'a str>,
{
any_lt
}
fn dangle() -> &'static str {
let temp = String::from("temporary");
takes_any_requires_static(|| "whatever", temp.as_str())
}
fn main() {
println!("{:?}", dangle());
} The type of impl<'a, T> FnOnce<()> for Closure<for<'b> fn() -> &'b str> {
type Output = &'a T;
}
|
Wow, that is very insightful. I was assuming that pub trait Producer<T>: FnOnce() -> T {}
impl<T, F: FnOnce() -> T> Producer<T> for F {}
fn make_static<T, P: for<'a> Producer<&'a T>>(_: P, any_borrow: P::Output) -> &'static T {
any_borrow
}
pub fn main() {
let victim = vec![vec![1]];
let long_lived = make_static(|| unreachable!(), &victim);
drop(victim);
println!("{:?}", long_lived);
} Very cool, this can be used to write the most readable UB I've ever seen! @lcnr I do wonder though, why do you have these in a personal repo instead of reporting them here? I guess that gives me an extra place to check when reporting soundness bugs in the future 🙃 Oh, and I also wonder what makes the |
the issues in that repo are not bugs or unsoundnesses, they are interesting fun facts about Rust 😁 as in, lcnr/random-rust-snippets#12 is something that limits future designs, it isn't unsound by itself as user-written impls can't have unconstrained associated types
The |
Oddly enough, I'm having trouble getting the UB to happen by using functions or async blocks (instead of closures). It seems to be just specifically closures that cause this issue. |
This is the case as for functions, we also check whether the return type would have unconstrained lifetimes, and if so, the lifetime is forced to be early-bound, as in, it's added as a generic parameter of the function type ( fn foo<'a>(x: &'a ()) -> &'a str { x } // late-bound lt
fn bar<'a>() -> &'a str { "hi there" } // early-bound lt
// this gets desugared to pretty much
struct foo;
impl<'a> FnOnce<(&'a (),)> for foo {
type Output = &'a ();
// ...
}
struct bar<'a>;
impl<'a> FnOnce<()> for bar<'a> {
type Output = &'a ();
// ...
} |
Oh, I misread the issue. I just saw the functions and thought "yea that looks unsound", but didn't see that it was to demonstrate a compile error. I thought my issue was a duplicate 😄 Still a very cool repo! I have some of my own snippets/toolbox I go back to when I encounter things that I think might cause soundness bugs |
This issue looks kind of similar to #130347 |
While testing the simplified version, I realized that I got the earliest affected version wrong. Here is an exhaustive list of results for all versions from 1.60-1.72.1:
I'm not sure how I missed this, because in my command history I found that 1.69 was the first old version I tried. I must have done something very wrong, because unlike what I initially described, no version except those described here ICE. |
Regression is in |
Don't feel like building the compiler right now, but 6718ea1 looks interesting |
yeah, it should be #101834 👍 |
Uh oh!
There was an error while loading. Please reload this page.
This code results in undefined behavior in safe code.
The earliest affected version is
1.721.67.0.ManyAll earlier versions rejectconstruct_implementor
,but most just1.70.0-1.72.0 ICE. Current nightly is also affected.I am not entirely sure what is going on in this code; I was trying to create a function type that returns a reference of any requested lifetime (don't ask why), i.e.
for<'a> Fn() -> &'a T
. For some reason it works when hiding behind a blanket implementation, though it is very fragile. TakingFn::Output
of such a function type yields a type that seems to kind of resemble "for<'a> &'a T
" and allows assignment to&'static dyn Any
, even thoughT
is not'static
.The text was updated successfully, but these errors were encountered: