-
Notifications
You must be signed in to change notification settings - Fork 13.2k
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
Adapt pin!
to the new lifespan-extension rules of 2024 edition
#138622
Conversation
This comment has been minimized.
This comment has been minimized.
Code like |
6ca8cbb
to
20c99d9
Compare
Huh, preserving the exact semantics of 2021 edition has proven to be way more challenging than anticipated, albeit doable at the cost of more hacks (that is, the newer implementation in this PR fixes both the Maybe it will be better to "just" imbue the r? @lcnr |
This comment has been minimized.
This comment has been minimized.
20c99d9
to
52258fe
Compare
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
e097a03
to
3d9daf0
Compare
The job Click to see the possible cause of the failure (guessed by this bot)
|
Hmm, the unsized coërcion is giving me some trouble, it's non-trivial. @rustbot label -S-waiting-on-review +S-waiting-on-author |
/// The reason for that is that we'd end up with `{ foo(&mut temporary(0)) }` among the expansion | ||
/// of `pin!`, and since this happens in `match`-scrutinee position, temporaries get not to outlive | ||
/// their encompassing braced block(s), so `temporary(0)` dies before that `}`, and the resulting | ||
/// `Pin { … }` is born dangling. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It doesn't matter about this being in match
scrutinee position. E.g., this also fails in Rust 2024 and works in Rust 2021:
fn f<'a, T>(x: &'a T) -> &'a T { x }
fn g<T>(_: T) {}
struct W<T: ?Sized> {
_inner: T,
}
fn main() {
_ = g(W { _inner: &mut { f(&mut ()) } });
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, I'll edit if we keep up with this PR
Alas, I'll have to abandon this effort 😕. I thought I'd be able to use enough workarounds to produce the necessary
We'll need one of the "Alternatives to this PR" in order to showcase a solution |
It's good to have more motivation to add |
We have a quick fix: #138717 |
Fixes #138596
See the docs of the
lifetime_extension
module for info about the implementation and the, alas, hacks involved.The hackiest part is the need to add a
PhantomData
field toPin
, alongside dead code, to guide type inference so as to force a sorely neededDerefMut
coërcion to happen.Therefore, let's keep in mind possible:
Alternatives to this PR
In order, imho, of simplicity-and-shipping-velocity:
Language tweak: get explicit
Deref{,Mut}
, i.e.,&*
and&mut *
, to preserve lifespan-of-temps extension.Indeed, it is not currently the case, whereas implicit
Deref{,Mut}
(through deref-coërcions from type constraints) does preserve it!In other words, currently, we have the following silly disparity:
With this, the implementation of this PR gets significantly reduced, insofar anything
__phantom
related disappears, and we'd just have:Lib/compiler hack: expose a way for the
pin!
macro (or a helper macro thereof) to emit code using 2021 edition (for its braces) so that the emitted code benefits from the 2021 edition rules for lifespan-of-temps extension.What I dislike about this: it becomes an utter acknowledgment of edition 2024 having regressed in expressibility; we ought rather to try and find ways to be able to express 2021 edition semantics without actually needing to be using that edition, I'd say.
Language feature (and proper solution): have the language offer
super let
.This is the proper tool through which the
pin!
macro and the like ought to be implemented:The problem is that I don't see this happening quick enough w.r.t. fixing issues such as pin macro no longer lifetime extends argument #138596 (unless we temporarily reverted the edition of
core
back to 2021 in the meantime?).