Experiment: Reborrow traits#151753
Conversation
This comment has been minimized.
This comment has been minimized.
e4c08f7 to
3b1957a
Compare
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.
|
Some changes occurred in cc @BoxyUwU Some changes occurred in match checking cc @Nadrieril This PR changes MIR cc @oli-obk, @RalfJung, @JakobDegen, @vakaras This PR changes rustc_public cc @oli-obk, @celinval, @ouz-a, @makai410 Some changes occurred to MIR optimizations cc @rust-lang/wg-mir-opt Some changes occurred to constck cc @fee1-dead Some changes occurred to the CTFE / Miri interpreter cc @rust-lang/miri Some changes occurred in compiler/rustc_codegen_cranelift cc @bjorn3 Some changes occurred to the CTFE machinery Some changes occurred in src/tools/clippy cc @rust-lang/clippy |
|
r? compiler |
|
Where can I read context on what CoerceShared is/why it exists, and what the design for reborrow traits is |
I added a link to the experiment tracking issue but put it very shortly: |
|
I think I'll just |
Thank you <3 my top secret plan (designed together with tmandry) was to get a compiler review and then reroll into types to that view as well so that works well :) |
Appreciated. |
|
Hi @oli-obk, it looks like this is ready for another round of review when you get a chance. Thanks! 🙂 |
7cdb66f to
339fc61
Compare
This comment has been minimized.
This comment has been minimized.
| let right_ty = rvalue.ty(&self.body.local_decls, self.tcx); | ||
|
|
||
| if matches!(rvalue, Rvalue::Reborrow(..)) { | ||
| // Trust me bro: Reborrow/CoerceShared is only inserted if types match. |
There was a problem hiding this comment.
wait, but this is validation :D
double-checking seems good, especially to catch optimizations from messing up
There was a problem hiding this comment.
Yeah; the thinking here is that because we only produce an Rvalue::Reborrow from an adjustment, and we only produce such an adjustment if we're looking at an assignment where we're going either from T -> T where T: Reborrow, or we're going from T -> U where T: CoerceShared<U>, then at this point we know we're necessarily doing a T -> T or T -> U and types match.
But double-checking is fine by me, and it does also offer up a future improvement of the implementation where we drop the adjustment and instead perform Reborrow somewhere at a deeper level on every use/access of a Reborrow or CoerceShared type.
There was a problem hiding this comment.
Okay; I added checking that the Rvalue::Reborrow(target, ..) type matches left_ty; an early return is still needed to skip calling super_assignment as something there (which I don't quite remember right now) doesn't work with Rvalue::Reborrow.
| if !errors.is_empty() { Err(errors) } else { Ok(()) } | ||
| } | ||
|
|
||
| pub(crate) fn coerce_shared_info<'tcx>( |
There was a problem hiding this comment.
how much is the logic of this copied from similar logic on the other coercion traits? Can we share some of the algorithm or sth else here?
There was a problem hiding this comment.
The basic structure and error handling is copied from CoerceUnsized. I'll take a look at possible deduplications.
There was a problem hiding this comment.
I deduplicated some code between CoerceShared and Reborrow handling, but didn't find anything significant to dedupe between other coercion traits. Most of the work is in the nitty-gritty field type checking, which is generally pretty unique for each trait.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
|
@rustbot ready |
This comment has been minimized.
This comment has been minimized.
aa831d0 to
b26bf4c
Compare
|
This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
This comment has been minimized.
This comment has been minimized.
|
☔ The latest upstream changes (presumably #154341) made this pull request unmergeable. Please resolve the merge conflicts. |
| // CoerceShared changes the type from right_ty to target_ty. | ||
| *target_ty |
There was a problem hiding this comment.
that's what rvalue.ty would return, so you can just use the normal code path
| } | ||
|
|
||
| if matches!(rvalue, Rvalue::Reborrow(..)) { | ||
| // Reborrow/CoerceShared must not call into super_statement. |
There was a problem hiding this comment.
why? it looks like super_statement just calls super_assign, which calls visit_rvalue, which you already defined as a noop for Rvalue::Reborrow. so the only thing I can think of is the dest being problematic somehow via visit_place?
View all comments
With this PR we now have basic functional Reborrow and CoerceShared traits. The current limitations are:
&mutwrappers is working (though I've not tested generic wrappers likeOption<&mut T>yet), but CoerceShared of&mutwrappers currently causes an ICE.The remaining tasks to complete before I'd consider this PR mergeable are:
Reborrow traits experiment: #145612
Co-authored by @dingxiangfei2009