Skip to content

Commit 00776a7

Browse files
authored
Unrolled build for #144265
Rollup merge of #144265 - compiler-errors:copy-ice, r=oli-obk Dont ICE on copy error being suppressed due to overflow See comment in test file. Fixes #144165
2 parents c0b282f + b5d36e5 commit 00776a7

File tree

3 files changed

+36
-4
lines changed

3 files changed

+36
-4
lines changed

compiler/rustc_borrowck/src/diagnostics/move_errors.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -303,10 +303,11 @@ impl<'infcx, 'tcx> MirBorrowckCtxt<'_, 'infcx, 'tcx> {
303303
}
304304

305305
fn has_ambiguous_copy(&mut self, ty: Ty<'tcx>) -> bool {
306-
let Some(copy_trait_def) = self.infcx.tcx.lang_items().copy_trait() else { return false };
307-
// This is only going to be ambiguous if there are incoherent impls, because otherwise
308-
// ambiguity should never happen in MIR.
309-
self.infcx.type_implements_trait(copy_trait_def, [ty], self.infcx.param_env).may_apply()
306+
let Some(copy_def_id) = self.infcx.tcx.lang_items().copy_trait() else { return false };
307+
308+
// Avoid bogus move errors because of an incoherent `Copy` impl.
309+
self.infcx.type_implements_trait(copy_def_id, [ty], self.infcx.param_env).may_apply()
310+
&& self.infcx.tcx.coherent_trait(copy_def_id).is_err()
310311
}
311312

312313
fn report_cannot_move_from_static(&mut self, place: Place<'tcx>, span: Span) -> Diag<'infcx> {

tests/ui/borrowck/copy-overflow.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// Regression test for <https://github.com/rust-lang/rust/issues/144165>.
2+
3+
// We were previously suppressing the copy error in the `Clone` impl because we assumed
4+
// that the only way we get `Copy` ambiguity errors was due to incoherent impls. This is
5+
// not true, since ambiguities can be encountered due to overflows (among other ways).
6+
7+
struct S<T: 'static>(Option<&'static T>);
8+
9+
impl<T: 'static> Copy for S<T> where S<T>: Copy + Clone {}
10+
impl<T: 'static> Clone for S<T> {
11+
fn clone(&self) -> Self {
12+
*self
13+
//~^ ERROR cannot move out of `*self` which is behind a shared reference
14+
}
15+
}
16+
fn main() {}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
error[E0507]: cannot move out of `*self` which is behind a shared reference
2+
--> $DIR/copy-overflow.rs:12:9
3+
|
4+
LL | *self
5+
| ^^^^^ move occurs because `*self` has type `S<T>`, which does not implement the `Copy` trait
6+
|
7+
help: consider cloning the value if the performance cost is acceptable
8+
|
9+
LL - *self
10+
LL + self.clone()
11+
|
12+
13+
error: aborting due to 1 previous error
14+
15+
For more information about this error, try `rustc --explain E0507`.

0 commit comments

Comments
 (0)