Skip to content

Commit 246daa4

Browse files
Rollup merge of #106931 - Ezrashaw:docs-e0208, r=compiler-errors
document + UI test `E0208` and make its output more user-friendly Cleans up `E0208`'s output a lot. It could actually be useful for someone learning about variance now. I also added a UI test for it in `tests/ui/error-codes/` and wrote some docs for it. r? `@GuillaumeGomez` another error code, can't be bothered to find the issue :P. Obviously there's some compiler stuff, so you'll have to hand it off. Part of #61137.
2 parents 1a878df + 708861e commit 246daa4

14 files changed

+96
-46
lines changed
Original file line numberDiff line numberDiff line change
@@ -1 +1,46 @@
11
#### This error code is internal to the compiler and will not be emitted with normal Rust code.
2+
#### Note: this error code is no longer emitted by the compiler.
3+
4+
This error code shows the variance of a type's generic parameters.
5+
6+
Erroneous code example:
7+
8+
```compile_fail
9+
// NOTE: this feature is perma-unstable and should *only* be used for
10+
// testing purposes.
11+
#![feature(rustc_attrs)]
12+
13+
#[rustc_variance]
14+
struct Foo<'a, T> { // error: deliberate error to display type's variance
15+
t: &'a mut T,
16+
}
17+
```
18+
19+
which produces the following error:
20+
21+
```text
22+
error: [-, o]
23+
--> <anon>:4:1
24+
|
25+
4 | struct Foo<'a, T> {
26+
| ^^^^^^^^^^^^^^^^^
27+
```
28+
29+
*Note that while `#[rustc_variance]` still exists and is used within the*
30+
*compiler, it no longer is marked as `E0208` and instead has no error code.*
31+
32+
This error is deliberately triggered with the `#[rustc_variance]` attribute
33+
(`#![feature(rustc_attrs)]` must be enabled) and helps to show you the variance
34+
of the type's generic parameters. You can read more about variance and
35+
subtyping in [this section of the Rustnomicon]. For a more in depth look at
36+
variance (including a more complete list of common variances) see
37+
[this section of the Reference]. For information on how variance is implemented
38+
in the compiler, see [this section of `rustc-dev-guide`].
39+
40+
This error can be easily fixed by removing the `#[rustc_variance]` attribute,
41+
the compiler's suggestion to comment it out can be applied automatically with
42+
`rustfix`.
43+
44+
[this section of the Rustnomicon]: https://doc.rust-lang.org/nomicon/subtyping.html
45+
[this section of the Reference]: https://doc.rust-lang.org/reference/subtyping.html#variance
46+
[this section of `rustc-dev-guide`]: https://rustc-dev-guide.rust-lang.org/variance.html
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
use rustc_errors::struct_span_err;
21
use rustc_middle::ty::TyCtxt;
32
use rustc_span::symbol::sym;
43

@@ -8,8 +7,8 @@ pub fn test_variance(tcx: TyCtxt<'_>) {
87
for id in tcx.hir().items() {
98
if tcx.has_attr(id.owner_id.to_def_id(), sym::rustc_variance) {
109
let variances_of = tcx.variances_of(id.owner_id);
11-
struct_span_err!(tcx.sess, tcx.def_span(id.owner_id), E0208, "{:?}", variances_of)
12-
.emit();
10+
11+
tcx.sess.struct_span_err(tcx.def_span(id.owner_id), format!("{variances_of:?}")).emit();
1312
}
1413
}
1514
}

src/tools/tidy/src/error_codes.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ const ERROR_DOCS_PATH: &str = "compiler/rustc_error_codes/src/error_codes/";
2727
const ERROR_TESTS_PATH: &str = "tests/ui/error-codes/";
2828

2929
// Error codes that (for some reason) can't have a doctest in their explanation. Error codes are still expected to provide a code example, even if untested.
30-
const IGNORE_DOCTEST_CHECK: &[&str] =
31-
&["E0208", "E0464", "E0570", "E0601", "E0602", "E0640", "E0717"];
30+
const IGNORE_DOCTEST_CHECK: &[&str] = &["E0464", "E0570", "E0601", "E0602", "E0640", "E0717"];
3231

3332
// Error codes that don't yet have a UI test. This list will eventually be removed.
3433
const IGNORE_UI_TEST_CHECK: &[&str] =

tests/ui/error-codes/E0208.rs

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
#![feature(rustc_attrs)]
2+
3+
#[rustc_variance]
4+
struct Foo<'a, T> { //~ ERROR [-, o]
5+
t: &'a mut T,
6+
}
7+
8+
fn main() {}

tests/ui/error-codes/E0208.stderr

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: [-, o]
2+
--> $DIR/E0208.rs:4:1
3+
|
4+
LL | struct Foo<'a, T> {
5+
| ^^^^^^^^^^^^^^^^^
6+
7+
error: aborting due to previous error
8+
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
error[E0208]: [o]
1+
error: [o]
22
--> $DIR/variance-associated-consts.rs:13:1
33
|
44
LL | struct Foo<T: Trait> {
55
| ^^^^^^^^^^^^^^^^^^^^
66

77
error: aborting due to previous error
88

9-
For more information about this error, try `rustc --explain E0208`.
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
1-
error[E0208]: [-, +]
1+
error: [-, +]
22
--> $DIR/variance-associated-types.rs:13:1
33
|
44
LL | struct Foo<'a, T : Trait<'a>> {
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
66

7-
error[E0208]: [o, o]
7+
error: [o, o]
88
--> $DIR/variance-associated-types.rs:18:1
99
|
1010
LL | struct Bar<'a, T : Trait<'a>> {
1111
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1212

1313
error: aborting due to 2 previous errors
1414

15-
For more information about this error, try `rustc --explain E0208`.
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
error[E0208]: [o]
1+
error: [o]
22
--> $DIR/variance-object-types.rs:7:1
33
|
44
LL | struct Foo<'a> {
55
| ^^^^^^^^^^^^^^
66

77
error: aborting due to previous error
88

9-
For more information about this error, try `rustc --explain E0208`.
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,44 @@
1-
error[E0208]: [-, -, -]
1+
error: [-, -, -]
22
--> $DIR/variance-regions-direct.rs:9:1
33
|
44
LL | struct Test2<'a, 'b, 'c> {
55
| ^^^^^^^^^^^^^^^^^^^^^^^^
66

7-
error[E0208]: [+, +, +]
7+
error: [+, +, +]
88
--> $DIR/variance-regions-direct.rs:18:1
99
|
1010
LL | struct Test3<'a, 'b, 'c> {
1111
| ^^^^^^^^^^^^^^^^^^^^^^^^
1212

13-
error[E0208]: [-, o]
13+
error: [-, o]
1414
--> $DIR/variance-regions-direct.rs:27:1
1515
|
1616
LL | struct Test4<'a, 'b:'a> {
1717
| ^^^^^^^^^^^^^^^^^^^^^^^
1818

19-
error[E0208]: [+, o]
19+
error: [+, o]
2020
--> $DIR/variance-regions-direct.rs:35:1
2121
|
2222
LL | struct Test5<'a, 'b:'a> {
2323
| ^^^^^^^^^^^^^^^^^^^^^^^
2424

25-
error[E0208]: [-, o]
25+
error: [-, o]
2626
--> $DIR/variance-regions-direct.rs:45:1
2727
|
2828
LL | struct Test6<'a, 'b:'a> {
2929
| ^^^^^^^^^^^^^^^^^^^^^^^
3030

31-
error[E0208]: [*]
31+
error: [*]
3232
--> $DIR/variance-regions-direct.rs:52:1
3333
|
3434
LL | struct Test7<'a> {
3535
| ^^^^^^^^^^^^^^^^
3636

37-
error[E0208]: [+, -, o]
37+
error: [+, -, o]
3838
--> $DIR/variance-regions-direct.rs:59:1
3939
|
4040
LL | enum Test8<'a, 'b, 'c:'b> {
4141
| ^^^^^^^^^^^^^^^^^^^^^^^^^
4242

4343
error: aborting due to 7 previous errors
4444

45-
For more information about this error, try `rustc --explain E0208`.
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,32 @@
1-
error[E0208]: [+, -, o, *]
1+
error: [+, -, o, *]
22
--> $DIR/variance-regions-indirect.rs:8:1
33
|
44
LL | enum Base<'a, 'b, 'c:'b, 'd> {
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
66

7-
error[E0208]: [*, o, -, +]
7+
error: [*, o, -, +]
88
--> $DIR/variance-regions-indirect.rs:15:1
99
|
1010
LL | struct Derived1<'w, 'x:'y, 'y, 'z> {
1111
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1212

13-
error[E0208]: [o, o, *]
13+
error: [o, o, *]
1414
--> $DIR/variance-regions-indirect.rs:20:1
1515
|
1616
LL | struct Derived2<'a, 'b:'a, 'c> {
1717
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1818

19-
error[E0208]: [o, -, *]
19+
error: [o, -, *]
2020
--> $DIR/variance-regions-indirect.rs:25:1
2121
|
2222
LL | struct Derived3<'a:'b, 'b, 'c> {
2323
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2424

25-
error[E0208]: [+, -, o]
25+
error: [+, -, o]
2626
--> $DIR/variance-regions-indirect.rs:30:1
2727
|
2828
LL | struct Derived4<'a, 'b, 'c:'b> {
2929
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3030

3131
error: aborting due to 5 previous errors
3232

33-
For more information about this error, try `rustc --explain E0208`.
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,26 @@
1-
error[E0208]: [+, +]
1+
error: [+, +]
22
--> $DIR/variance-trait-bounds.rs:16:1
33
|
44
LL | struct TestStruct<U,T:Setter<U>> {
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
66

7-
error[E0208]: [*, +]
7+
error: [*, +]
88
--> $DIR/variance-trait-bounds.rs:21:1
99
|
1010
LL | enum TestEnum<U,T:Setter<U>> {
1111
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1212

13-
error[E0208]: [*, +]
13+
error: [*, +]
1414
--> $DIR/variance-trait-bounds.rs:26:1
1515
|
1616
LL | struct TestContraStruct<U,T:Setter<U>> {
1717
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1818

19-
error[E0208]: [*, +]
19+
error: [*, +]
2020
--> $DIR/variance-trait-bounds.rs:31:1
2121
|
2222
LL | struct TestBox<U,T:Getter<U>+Setter<U>> {
2323
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2424

2525
error: aborting due to 4 previous errors
2626

27-
For more information about this error, try `rustc --explain E0208`.
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1-
error[E0208]: [-]
1+
error: [-]
22
--> $DIR/variance-trait-object-bound.rs:14:1
33
|
44
LL | struct TOption<'a> {
55
| ^^^^^^^^^^^^^^^^^^
66

77
error: aborting due to previous error
88

9-
For more information about this error, try `rustc --explain E0208`.
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,32 @@
1-
error[E0208]: [+, +]
1+
error: [+, +]
22
--> $DIR/variance-types-bounds.rs:7:1
33
|
44
LL | struct TestImm<A, B> {
55
| ^^^^^^^^^^^^^^^^^^^^
66

7-
error[E0208]: [+, o]
7+
error: [+, o]
88
--> $DIR/variance-types-bounds.rs:13:1
99
|
1010
LL | struct TestMut<A, B:'static> {
1111
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1212

13-
error[E0208]: [+, o]
13+
error: [+, o]
1414
--> $DIR/variance-types-bounds.rs:19:1
1515
|
1616
LL | struct TestIndirect<A:'static, B:'static> {
1717
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1818

19-
error[E0208]: [o, o]
19+
error: [o, o]
2020
--> $DIR/variance-types-bounds.rs:24:1
2121
|
2222
LL | struct TestIndirect2<A:'static, B:'static> {
2323
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2424

25-
error[E0208]: [o, o]
25+
error: [o, o]
2626
--> $DIR/variance-types-bounds.rs:38:1
2727
|
2828
LL | struct TestObject<A, R> {
2929
| ^^^^^^^^^^^^^^^^^^^^^^^
3030

3131
error: aborting due to 5 previous errors
3232

33-
For more information about this error, try `rustc --explain E0208`.
+6-7
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,38 @@
1-
error[E0208]: [-, o, o]
1+
error: [-, o, o]
22
--> $DIR/variance-types.rs:10:1
33
|
44
LL | struct InvariantMut<'a,A:'a,B:'a> {
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
66

7-
error[E0208]: [o]
7+
error: [o]
88
--> $DIR/variance-types.rs:15:1
99
|
1010
LL | struct InvariantCell<A> {
1111
| ^^^^^^^^^^^^^^^^^^^^^^^
1212

13-
error[E0208]: [o]
13+
error: [o]
1414
--> $DIR/variance-types.rs:20:1
1515
|
1616
LL | struct InvariantIndirect<A> {
1717
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
1818

19-
error[E0208]: [+]
19+
error: [+]
2020
--> $DIR/variance-types.rs:25:1
2121
|
2222
LL | struct Covariant<A> {
2323
| ^^^^^^^^^^^^^^^^^^^
2424

25-
error[E0208]: [-]
25+
error: [-]
2626
--> $DIR/variance-types.rs:30:1
2727
|
2828
LL | struct Contravariant<A> {
2929
| ^^^^^^^^^^^^^^^^^^^^^^^
3030

31-
error[E0208]: [+, -, o]
31+
error: [+, -, o]
3232
--> $DIR/variance-types.rs:35:1
3333
|
3434
LL | enum Enum<A,B,C> {
3535
| ^^^^^^^^^^^^^^^^
3636

3737
error: aborting due to 6 previous errors
3838

39-
For more information about this error, try `rustc --explain E0208`.

0 commit comments

Comments
 (0)