Skip to content

Commit 051c52a

Browse files
authored
Rollup merge of rust-lang#105935 - Ezrashaw:add-test+docs-for-e0377, r=GuillaumeGomez
docs/test: add UI test and long-form error docs for `E0377`
2 parents 9888ef7 + e798fdf commit 051c52a

File tree

5 files changed

+55
-4
lines changed

5 files changed

+55
-4
lines changed

compiler/rustc_error_codes/src/error_codes.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ E0373: include_str!("./error_codes/E0373.md"),
184184
E0374: include_str!("./error_codes/E0374.md"),
185185
E0375: include_str!("./error_codes/E0375.md"),
186186
E0376: include_str!("./error_codes/E0376.md"),
187+
E0377: include_str!("./error_codes/E0377.md"),
187188
E0378: include_str!("./error_codes/E0378.md"),
188189
E0379: include_str!("./error_codes/E0379.md"),
189190
E0380: include_str!("./error_codes/E0380.md"),
@@ -579,8 +580,6 @@ E0791: include_str!("./error_codes/E0791.md"),
579580
// E0315, // cannot invoke closure outside of its lifetime
580581
// E0319, // trait impls for defaulted traits allowed just for structs/enums
581582
// E0372, // coherence not object safe
582-
E0377, // the trait `CoerceUnsized` may only be implemented for a coercion
583-
// between structures with the same definition
584583
// E0385, // {} in an aliasable location
585584
// E0402, // cannot use an outer type parameter in this context
586585
// E0406, // merged into 420
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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).

src/test/ui/error-codes/E0377.rs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#![feature(coerce_unsized)]
2+
use std::ops::CoerceUnsized;
3+
4+
pub struct Foo<T: ?Sized> {
5+
field_with_unsized_type: T,
6+
}
7+
8+
pub struct Bar<T: ?Sized> {
9+
field_with_unsized_type: T,
10+
}
11+
12+
impl<T, U> CoerceUnsized<Bar<U>> for Foo<T> where T: CoerceUnsized<U> {} //~ ERROR E0377
13+
14+
fn main() {}

src/test/ui/error-codes/E0377.stderr

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
error[E0377]: the trait `CoerceUnsized` may only be implemented for a coercion between structures with the same definition; expected `Foo`, found `Bar`
2+
--> $DIR/E0377.rs:12:1
3+
|
4+
LL | impl<T, U> CoerceUnsized<Bar<U>> for Foo<T> where T: CoerceUnsized<U> {}
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6+
7+
error: aborting due to previous error
8+
9+
For more information about this error, try `rustc --explain E0377`.

src/tools/tidy/src/error_codes_check.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ use regex::Regex;
1111

1212
// A few of those error codes can't be tested but all the others can and *should* be tested!
1313
const EXEMPTED_FROM_TEST: &[&str] = &[
14-
"E0313", "E0377", "E0461", "E0462", "E0465", "E0476", "E0490", "E0514", "E0519", "E0523",
15-
"E0554", "E0640", "E0717", "E0729", "E0789",
14+
"E0313", "E0461", "E0462", "E0465", "E0476", "E0490", "E0514", "E0519", "E0523", "E0554",
15+
"E0640", "E0717", "E0729", "E0789",
1616
];
1717

1818
// Some error codes don't have any tests apparently...

0 commit comments

Comments
 (0)