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 https://github.com/rust-lang/rust/issues/61137.
This commit is contained in:
Guillaume Gomez 2023-01-19 11:19:35 +01:00 committed by GitHub
commit 246daa49ee
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
14 changed files with 96 additions and 46 deletions

View File

@ -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

View File

@ -1,4 +1,3 @@
use rustc_errors::struct_span_err;
use rustc_middle::ty::TyCtxt;
use rustc_span::symbol::sym;
@ -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();
}
}
}

View File

@ -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] =

View File

@ -0,0 +1,8 @@
#![feature(rustc_attrs)]
#[rustc_variance]
struct Foo<'a, T> { //~ ERROR [-, o]
t: &'a mut T,
}
fn main() {}

View File

@ -0,0 +1,8 @@
error: [-, o]
--> $DIR/E0208.rs:4:1
|
LL | struct Foo<'a, T> {
| ^^^^^^^^^^^^^^^^^
error: aborting due to previous error

View File

@ -1,4 +1,4 @@
error[E0208]: [o]
error: [o]
--> $DIR/variance-associated-consts.rs:13:1
|
LL | struct Foo<T: Trait> {
@ -6,4 +6,3 @@ LL | struct Foo<T: Trait> {
error: aborting due to previous error
For more information about this error, try `rustc --explain E0208`.

View File

@ -1,10 +1,10 @@
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>> {
@ -12,4 +12,3 @@ LL | struct Bar<'a, T : Trait<'a>> {
error: aborting due to 2 previous errors
For more information about this error, try `rustc --explain E0208`.

View File

@ -1,4 +1,4 @@
error[E0208]: [o]
error: [o]
--> $DIR/variance-object-types.rs:7:1
|
LL | struct Foo<'a> {
@ -6,4 +6,3 @@ LL | struct Foo<'a> {
error: aborting due to previous error
For more information about this error, try `rustc --explain E0208`.

View File

@ -1,40 +1,40 @@
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> {
@ -42,4 +42,3 @@ LL | enum Test8<'a, 'b, 'c:'b> {
error: aborting due to 7 previous errors
For more information about this error, try `rustc --explain E0208`.

View File

@ -1,28 +1,28 @@
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> {
@ -30,4 +30,3 @@ LL | struct Derived4<'a, 'b, 'c:'b> {
error: aborting due to 5 previous errors
For more information about this error, try `rustc --explain E0208`.

View File

@ -1,22 +1,22 @@
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>> {
@ -24,4 +24,3 @@ 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`.

View File

@ -1,4 +1,4 @@
error[E0208]: [-]
error: [-]
--> $DIR/variance-trait-object-bound.rs:14:1
|
LL | struct TOption<'a> {
@ -6,4 +6,3 @@ LL | struct TOption<'a> {
error: aborting due to previous error
For more information about this error, try `rustc --explain E0208`.

View File

@ -1,28 +1,28 @@
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> {
@ -30,4 +30,3 @@ LL | struct TestObject<A, R> {
error: aborting due to 5 previous errors
For more information about this error, try `rustc --explain E0208`.

View File

@ -1,34 +1,34 @@
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> {
@ -36,4 +36,3 @@ LL | enum Enum<A,B,C> {
error: aborting due to 6 previous errors
For more information about this error, try `rustc --explain E0208`.