Fix reborrow_info early return skipping field validation#156784
Conversation
|
rustbot has assigned @dingxiangfei2009. Use Why was this reviewer chosen?The reviewer was selected based on:
|
This comment has been minimized.
This comment has been minimized.
|
cc @aapoalas Ah right, this is indeed a typo. |
|
|
||
| struct Bad<'a> { | ||
| first: &'a mut i32, | ||
| second: String, //~ ERROR the trait bound `String: Copy` is not satisfied |
There was a problem hiding this comment.
Let's ./x t tests/ui/reborrow/reborrow_multi_field_validation.rs --bless once.
There was a problem hiding this comment.
Please strip the commit "co authored by LLM" thing. Either it is your work, or it is not. If it weren't, we could just use the tools ourselves instead of filtering our reviews through you to your LLM.
Similarly do not include "test" sections in your PR description. We do see test changes in the diff.
|
Reminder, once the PR becomes ready for a review, use |
|
How much was Claude involved into replacing |
|
Genuine question: Why does Reborrow not have a Copy supertrait bound, rather than enforcing all fields are Copy? What happens if the Reborrow type also impls Drop? A type that is both implicitly bitwise copyable but also impls Drop seems undesirable as copies can be elided and are supposed to be generally cheap (ignoring large Copy types). I bring this up because my gut feeling is that while this PR fixes the desired behavior, it seems like Reborrow ultimately duplicates Copy as it just seems to be (at least to me) a Copy impl that also changes a lifetime. So perhaps the correct fix is to require types that impl Reborrow to also impl Copy, then allow the existing codepath for verifying Copy impls to handle this. |
Fairly simple, actually: because fn foo<T: Copy + Debug>(t: T) {
let u = t;
let v = t;
println!("{t} {u} {v}");
}This should cause a borrow checker error if called with a type that implements An argument has actually been made that the semi-reverse should be true: that perhaps there should be a blanket implementation of
This (and |
`reborrow_info` validates that all data fields of a struct implementing `Reborrow` are either `Copy` or themselves `Reborrow`. When a field implementing `Reborrow` was found, the loop returned `Ok(())` immediately, so any remaining fields were never validated. This let a struct whose first data field is `Reborrow` and whose second field is neither `Copy` nor `Reborrow` incorrectly implement `Reborrow`. Change the early `return Ok(())` to `continue` so every field is checked.
fabfa33 to
65f4151
Compare
reborrow_infovalidates that all data fields of a struct implementingReborroware eitherCopyor themselvesReborrow. When iterating the fields, finding aReborrowfield returnedOk(())immediately, so any remaining fields were never validated. This let a struct with a non-Copy, non-Reborrowsecond field implementReborrow.Changing the early
return Ok(())tocontinuemakes the loop validate every field.Tracking issue: #145612