Skip to content

Commit f38e177

Browse files
Rollup merge of #156784 - ikow:fix/reborrow-early-return, r=dingxiangfei2009
Fix reborrow_info early return skipping field validation `reborrow_info` validates that all data fields of a struct implementing `Reborrow` are either `Copy` or themselves `Reborrow`. When iterating the fields, finding a `Reborrow` field returned `Ok(())` immediately, so any remaining fields were never validated. This let a struct with a non-`Copy`, non-`Reborrow` second field implement `Reborrow`. Changing the early `return Ok(())` to `continue` makes the loop validate every field. Tracking issue: #145612
2 parents 4474668 + 65f4151 commit f38e177

3 files changed

Lines changed: 26 additions & 2 deletions

File tree

compiler/rustc_hir_analysis/src/coherence/builtin.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -563,8 +563,8 @@ pub(crate) fn reborrow_info<'tcx>(
563563
)
564564
.is_ok()
565565
{
566-
// Field implements Reborrow.
567-
return Ok(());
566+
// Field implements Reborrow, check remaining fields.
567+
continue;
568568
}
569569

570570
// Field does not implement Reborrow: it must be Copy.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#![feature(reborrow)]
2+
3+
use std::marker::Reborrow;
4+
5+
// Regression test: `reborrow_info` must validate ALL data fields,
6+
// not just stop at the first Reborrow field.
7+
8+
struct Bad<'a> {
9+
first: &'a mut i32,
10+
second: String, //~ ERROR the trait bound `String: Copy` is not satisfied
11+
}
12+
13+
impl<'a> Reborrow for Bad<'a> {}
14+
15+
fn main() {}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0277]: the trait bound `String: Copy` is not satisfied
2+
--> $DIR/reborrow_multi_field_validation.rs:10:5
3+
|
4+
LL | second: String,
5+
| ^^^^^^^^^^^^^^ the trait `Copy` is not implemented for `String`
6+
7+
error: aborting due to 1 previous error
8+
9+
For more information about this error, try `rustc --explain E0277`.

0 commit comments

Comments
 (0)