Skip to content

Commit f6b8a9f

Browse files
authored
Rollup merge of #108883 - compiler-errors:post-norm-copy-err, r=BoxyUwU
Suppress copy impl error when post-normalized type references errors Suppress spurious errors from the `Copy` impl validity check when fields have bad types *post*-normalization, instead of just pre-normalization. ---- The const-generics test regressed recently due to #107965, cc `````@BoxyUwU.````` * I think it's because `[_; 0u32]: Copy` now fails to hold because a nested obligation `ConstArgHasType(0u32, usize)` fails. * It's interesting that `[const_error]` shows up in the type only after normalization, though, but I'm pretty sure that it's due to the evaluate call that happens when normalizing unevaluated consts.
2 parents 1a9376d + 8a99ffc commit f6b8a9f

File tree

5 files changed

+49
-1
lines changed

5 files changed

+49
-1
lines changed

compiler/rustc_trait_selection/src/traits/misc.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,12 @@ pub fn type_allowed_to_implement_copy<'tcx>(
8787
};
8888
let ty = ocx.normalize(&normalization_cause, param_env, unnormalized_ty);
8989
let normalization_errors = ocx.select_where_possible();
90-
if !normalization_errors.is_empty() {
90+
91+
// NOTE: The post-normalization type may also reference errors,
92+
// such as when we project to a missing type or we have a mismatch
93+
// between expected and found const-generic types. Don't report an
94+
// additional copy error here, since it's not typically useful.
95+
if !normalization_errors.is_empty() || ty.references_error() {
9196
tcx.sess.delay_span_bug(field_span, format!("couldn't normalize struct field `{unnormalized_ty}` when checking Copy implementation"));
9297
continue;
9398
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
trait AsPtr {
2+
type Ptr;
3+
}
4+
5+
impl AsPtr for () {
6+
type Ptr = *const void;
7+
//~^ ERROR cannot find type `void` in this scope
8+
}
9+
10+
#[derive(Copy, Clone)]
11+
struct Foo {
12+
p: <() as AsPtr>::Ptr,
13+
// Do not report a "`Copy` cannot be implemented" here.
14+
}
15+
16+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0412]: cannot find type `void` in this scope
2+
--> $DIR/illegal-copy-bad-projection.rs:6:23
3+
|
4+
LL | type Ptr = *const void;
5+
| ^^^^ not found in this scope
6+
7+
error: aborting due to previous error
8+
9+
For more information about this error, try `rustc --explain E0412`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#[derive(Copy, Clone)]
2+
pub struct Foo {
3+
x: [u8; SIZE],
4+
//~^ ERROR mismatched types
5+
}
6+
7+
const SIZE: u32 = 1;
8+
9+
fn main() {}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0308]: mismatched types
2+
--> $DIR/bad-generic-in-copy-impl.rs:3:13
3+
|
4+
LL | x: [u8; SIZE],
5+
| ^^^^ expected `usize`, found `u32`
6+
7+
error: aborting due to previous error
8+
9+
For more information about this error, try `rustc --explain E0308`.

0 commit comments

Comments
 (0)