Fix reborrow_info early return skipping field validation#156784
Conversation
The `reborrow_info` function validates that all data fields of a struct implementing `Reborrow` are either `Copy` or themselves `Reborrow`. However, when iterating over data fields, finding a `Reborrow` field caused an immediate `return Ok(())`, skipping validation of all remaining fields. This could allow a struct with a non-Copy, non-Reborrow second field to incorrectly implement `Reborrow`. Fix: change `return Ok(())` to `continue` so all fields are checked. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
|
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.
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
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 |
Summary
reborrow_info()incompiler/rustc_hir_analysis/src/coherence/builtin.rsvalidates that all data fields of a struct implementingReborroware eitherCopyorReborrow. However, when iterating over data fields, finding aReborrowfield caused an immediatereturn Ok(())(line 567), skipping validation of all remaining fields.This could allow a struct with a non-Copy, non-Reborrow second data field to incorrectly implement
Reborrow, potentially leading to unsound bitwise copies of types that should not be copied.Fix
Change
return Ok(())tocontinueso the loop validates all fields, not just the first one.Test plan
tests/ui/reborrow/reborrow_multi_field_validation.rs— a compile-fail test with a struct that has aReborrowfirst field and aString(non-Copy, non-Reborrow) second fieldTracking issue: #145612 (reborrow feature)