]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_error_codes/src/error_codes/E0377.md
Rollup merge of #106946 - dtolnay:hashlinecolumn, r=m-ou-se
[rust.git] / compiler / rustc_error_codes / src / error_codes / E0377.md
1 The trait `CoerceUnsized` may only be implemented for a coercion between
2 structures with the same definition.
3
4 Example of erroneous code:
5
6 ```compile_fail,E0377
7 #![feature(coerce_unsized)]
8 use std::ops::CoerceUnsized;
9
10 pub struct Foo<T: ?Sized> {
11     field_with_unsized_type: T,
12 }
13
14 pub struct Bar<T: ?Sized> {
15     field_with_unsized_type: T,
16 }
17
18 // error: the trait `CoerceUnsized` may only be implemented for a coercion
19 //        between structures with the same definition
20 impl<T, U> CoerceUnsized<Bar<U>> for Foo<T> where T: CoerceUnsized<U> {}
21 ```
22
23 When attempting to implement `CoerceUnsized`, the `impl` signature must look
24 like: `impl CoerceUnsized<Type<U>> for Type<T> where T: CoerceUnsized<U>`;
25 the *implementer* and *`CoerceUnsized` type parameter* must be the same
26 type. In this example, `Bar` and `Foo` (even though structurally identical)
27 are *not* the same type and are rejected. Learn more about the `CoerceUnsized`
28 trait and DST coercion in
29 [the `CoerceUnsized` docs](../std/ops/trait.CoerceUnsized.html).