Skip to content

Commit

Permalink
Rollup merge of rust-lang#106931 - Ezrashaw:docs-e0208, r=compiler-er…
Browse files Browse the repository at this point in the history
…rors

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 rust-lang#61137.
  • Loading branch information
GuillaumeGomez authored Jan 19, 2023
2 parents 1a878df + 708861e commit 246daa4
Show file tree
Hide file tree
Showing 14 changed files with 96 additions and 46 deletions.
45 changes: 45 additions & 0 deletions compiler/rustc_error_codes/src/error_codes/E0208.md
Original file line number Diff line number Diff line change
@@ -1 +1,46 @@
#### This error code is internal to the compiler and will not be emitted with normal Rust code.
#### Note: this error code is no longer emitted by the compiler.

This error code shows the variance of a type's generic parameters.

Erroneous code example:

```compile_fail
// NOTE: this feature is perma-unstable and should *only* be used for
// testing purposes.
#![feature(rustc_attrs)]
#[rustc_variance]
struct Foo<'a, T> { // error: deliberate error to display type's variance
t: &'a mut T,
}
```

which produces the following error:

```text
error: [-, o]
--> <anon>:4:1
|
4 | struct Foo<'a, T> {
| ^^^^^^^^^^^^^^^^^
```

*Note that while `#[rustc_variance]` still exists and is used within the*
*compiler, it no longer is marked as `E0208` and instead has no error code.*

This error is deliberately triggered with the `#[rustc_variance]` attribute
(`#![feature(rustc_attrs)]` must be enabled) and helps to show you the variance
of the type's generic parameters. You can read more about variance and
subtyping in [this section of the Rustnomicon]. For a more in depth look at
variance (including a more complete list of common variances) see
[this section of the Reference]. For information on how variance is implemented
in the compiler, see [this section of `rustc-dev-guide`].

This error can be easily fixed by removing the `#[rustc_variance]` attribute,
the compiler's suggestion to comment it out can be applied automatically with
`rustfix`.

[this section of the Rustnomicon]: https://doc.rust-lang.org/nomicon/subtyping.html
[this section of the Reference]: https://doc.rust-lang.org/reference/subtyping.html#variance
[this section of `rustc-dev-guide`]: https://rustc-dev-guide.rust-lang.org/variance.html
5 changes: 2 additions & 3 deletions compiler/rustc_hir_analysis/src/variance/test.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
use rustc_errors::struct_span_err;
use rustc_middle::ty::TyCtxt;
use rustc_span::symbol::sym;

Expand All @@ -8,8 +7,8 @@ pub fn test_variance(tcx: TyCtxt<'_>) {
for id in tcx.hir().items() {
if tcx.has_attr(id.owner_id.to_def_id(), sym::rustc_variance) {
let variances_of = tcx.variances_of(id.owner_id);
struct_span_err!(tcx.sess, tcx.def_span(id.owner_id), E0208, "{:?}", variances_of)
.emit();

tcx.sess.struct_span_err(tcx.def_span(id.owner_id), format!("{variances_of:?}")).emit();
}
}
}
3 changes: 1 addition & 2 deletions src/tools/tidy/src/error_codes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,7 @@ const ERROR_DOCS_PATH: &str = "compiler/rustc_error_codes/src/error_codes/";
const ERROR_TESTS_PATH: &str = "tests/ui/error-codes/";

// 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.
const IGNORE_DOCTEST_CHECK: &[&str] =
&["E0208", "E0464", "E0570", "E0601", "E0602", "E0640", "E0717"];
const IGNORE_DOCTEST_CHECK: &[&str] = &["E0464", "E0570", "E0601", "E0602", "E0640", "E0717"];

// Error codes that don't yet have a UI test. This list will eventually be removed.
const IGNORE_UI_TEST_CHECK: &[&str] =
Expand Down
8 changes: 8 additions & 0 deletions tests/ui/error-codes/E0208.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#![feature(rustc_attrs)]

#[rustc_variance]
struct Foo<'a, T> { //~ ERROR [-, o]
t: &'a mut T,
}

fn main() {}
8 changes: 8 additions & 0 deletions tests/ui/error-codes/E0208.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
error: [-, o]
--> $DIR/E0208.rs:4:1
|
LL | struct Foo<'a, T> {
| ^^^^^^^^^^^^^^^^^

error: aborting due to previous error

3 changes: 1 addition & 2 deletions tests/ui/variance/variance-associated-consts.stderr
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
error[E0208]: [o]
error: [o]
--> $DIR/variance-associated-consts.rs:13:1
|
LL | struct Foo<T: Trait> {
| ^^^^^^^^^^^^^^^^^^^^

error: aborting due to previous error

For more information about this error, try `rustc --explain E0208`.
5 changes: 2 additions & 3 deletions tests/ui/variance/variance-associated-types.stderr
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
error[E0208]: [-, +]
error: [-, +]
--> $DIR/variance-associated-types.rs:13:1
|
LL | struct Foo<'a, T : Trait<'a>> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

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

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0208`.
3 changes: 1 addition & 2 deletions tests/ui/variance/variance-object-types.stderr
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
error[E0208]: [o]
error: [o]
--> $DIR/variance-object-types.rs:7:1
|
LL | struct Foo<'a> {
| ^^^^^^^^^^^^^^

error: aborting due to previous error

For more information about this error, try `rustc --explain E0208`.
15 changes: 7 additions & 8 deletions tests/ui/variance/variance-regions-direct.stderr
Original file line number Diff line number Diff line change
@@ -1,45 +1,44 @@
error[E0208]: [-, -, -]
error: [-, -, -]
--> $DIR/variance-regions-direct.rs:9:1
|
LL | struct Test2<'a, 'b, 'c> {
| ^^^^^^^^^^^^^^^^^^^^^^^^

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

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

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

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

error[E0208]: [*]
error: [*]
--> $DIR/variance-regions-direct.rs:52:1
|
LL | struct Test7<'a> {
| ^^^^^^^^^^^^^^^^

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

error: aborting due to 7 previous errors

For more information about this error, try `rustc --explain E0208`.
11 changes: 5 additions & 6 deletions tests/ui/variance/variance-regions-indirect.stderr
Original file line number Diff line number Diff line change
@@ -1,33 +1,32 @@
error[E0208]: [+, -, o, *]
error: [+, -, o, *]
--> $DIR/variance-regions-indirect.rs:8:1
|
LL | enum Base<'a, 'b, 'c:'b, 'd> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^

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

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

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

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

error: aborting due to 5 previous errors

For more information about this error, try `rustc --explain E0208`.
9 changes: 4 additions & 5 deletions tests/ui/variance/variance-trait-bounds.stderr
Original file line number Diff line number Diff line change
@@ -1,27 +1,26 @@
error[E0208]: [+, +]
error: [+, +]
--> $DIR/variance-trait-bounds.rs:16:1
|
LL | struct TestStruct<U,T:Setter<U>> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

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

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

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

error: aborting due to 4 previous errors

For more information about this error, try `rustc --explain E0208`.
3 changes: 1 addition & 2 deletions tests/ui/variance/variance-trait-object-bound.stderr
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
error[E0208]: [-]
error: [-]
--> $DIR/variance-trait-object-bound.rs:14:1
|
LL | struct TOption<'a> {
| ^^^^^^^^^^^^^^^^^^

error: aborting due to previous error

For more information about this error, try `rustc --explain E0208`.
11 changes: 5 additions & 6 deletions tests/ui/variance/variance-types-bounds.stderr
Original file line number Diff line number Diff line change
@@ -1,33 +1,32 @@
error[E0208]: [+, +]
error: [+, +]
--> $DIR/variance-types-bounds.rs:7:1
|
LL | struct TestImm<A, B> {
| ^^^^^^^^^^^^^^^^^^^^

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

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

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

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

error: aborting due to 5 previous errors

For more information about this error, try `rustc --explain E0208`.
13 changes: 6 additions & 7 deletions tests/ui/variance/variance-types.stderr
Original file line number Diff line number Diff line change
@@ -1,39 +1,38 @@
error[E0208]: [-, o, o]
error: [-, o, o]
--> $DIR/variance-types.rs:10:1
|
LL | struct InvariantMut<'a,A:'a,B:'a> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

error[E0208]: [o]
error: [o]
--> $DIR/variance-types.rs:15:1
|
LL | struct InvariantCell<A> {
| ^^^^^^^^^^^^^^^^^^^^^^^

error[E0208]: [o]
error: [o]
--> $DIR/variance-types.rs:20:1
|
LL | struct InvariantIndirect<A> {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^

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

error[E0208]: [-]
error: [-]
--> $DIR/variance-types.rs:30:1
|
LL | struct Contravariant<A> {
| ^^^^^^^^^^^^^^^^^^^^^^^

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

error: aborting due to 6 previous errors

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

0 comments on commit 246daa4

Please sign in to comment.