From 9d4620870c42224cc91d532b4e4cb20bbc2270b5 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Fri, 15 Nov 2019 13:04:52 +0100 Subject: [PATCH 01/12] Clean up E0023 --- src/librustc_error_codes/error_codes/E0023.md | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/librustc_error_codes/error_codes/E0023.md b/src/librustc_error_codes/error_codes/E0023.md index 92cae460c81db..23a9d22a60d82 100644 --- a/src/librustc_error_codes/error_codes/E0023.md +++ b/src/librustc_error_codes/error_codes/E0023.md @@ -1,6 +1,6 @@ -A pattern used to match against an enum variant must provide a sub-pattern for -each field of the enum variant. This error indicates that a pattern attempted to -extract an incorrect number of fields from a variant. +A pattern attempted to extract an incorrect number of fields from a variant. + +Erroneous code example: ``` enum Fruit { @@ -9,6 +9,9 @@ enum Fruit { } ``` +A pattern used to match against an enum variant must provide a sub-pattern for +each field of the enum variant. + Here the `Apple` variant has two fields, and should be matched against like so: ``` From 57564c80e3a269d102123804ff24046bc05071da Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Fri, 15 Nov 2019 13:07:37 +0100 Subject: [PATCH 02/12] Add link to error code explanation RFC --- src/librustc_error_codes/error_codes.rs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/librustc_error_codes/error_codes.rs b/src/librustc_error_codes/error_codes.rs index 5e6d5ce159d0f..ffefe51f854f5 100644 --- a/src/librustc_error_codes/error_codes.rs +++ b/src/librustc_error_codes/error_codes.rs @@ -1,6 +1,11 @@ // Error messages for EXXXX errors. Each message should start and end with a // new line, and be wrapped to 80 characters. In vim you can `:set tw=80` and // use `gq` to wrap paragraphs. Use `:set tw=0` to disable. +// +// /!\ IMPORTANT /!\ +// +// Error messages' format must follow the RFC 1567 available here: +// https://github.com/rust-lang/rfcs/pull/1567 crate::register_diagnostics! { From 88acc84073383387b80c2df67fc6e26cdaffedc8 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Fri, 15 Nov 2019 13:09:46 +0100 Subject: [PATCH 03/12] Clean up E0026 --- src/librustc_error_codes/error_codes/E0026.md | 37 +++++-------------- 1 file changed, 10 insertions(+), 27 deletions(-) diff --git a/src/librustc_error_codes/error_codes/E0026.md b/src/librustc_error_codes/error_codes/E0026.md index 9327b31ac4b9b..72c575aabb643 100644 --- a/src/librustc_error_codes/error_codes/E0026.md +++ b/src/librustc_error_codes/error_codes/E0026.md @@ -1,51 +1,34 @@ -This error indicates that a struct pattern attempted to extract a non-existent -field from a struct. Struct fields are identified by the name used before the -colon `:` so struct patterns should resemble the declaration of the struct type -being matched. +A struct pattern attempted to extract a non-existent field from a struct. -``` -// Correct matching. -struct Thing { - x: u32, - y: u32 -} - -let thing = Thing { x: 1, y: 2 }; - -match thing { - Thing { x: xfield, y: yfield } => {} -} -``` - -If you are using shorthand field patterns but want to refer to the struct field -by a different name, you should rename it explicitly. - -Change this: +Erroneous code example: ```compile_fail,E0026 struct Thing { x: u32, - y: u32 + y: u32, } let thing = Thing { x: 0, y: 0 }; match thing { - Thing { x, z } => {} + Thing { x, z } => {} // error: `Thing::z` field doesn't exist } ``` -To this: +If you are using shorthand field patterns but want to refer to the struct field +by a different name, you should rename it explicitly. Struct fields are +identified by the name used before the colon `:` so struct patterns should +resemble the declaration of the struct type being matched. ``` struct Thing { x: u32, - y: u32 + y: u32, } let thing = Thing { x: 0, y: 0 }; match thing { - Thing { x, y: z } => {} + Thing { x, y: z } => {} // we renamed `y` to `z` } ``` From 86506281b5520e38eef772c78d614ae4ba3c0f7e Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Fri, 15 Nov 2019 13:13:18 +0100 Subject: [PATCH 04/12] Clean up E0027 --- src/librustc_error_codes/error_codes/E0027.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/librustc_error_codes/error_codes/E0027.md b/src/librustc_error_codes/error_codes/E0027.md index ab2a20fe9e56d..a8b1340ca0c78 100644 --- a/src/librustc_error_codes/error_codes/E0027.md +++ b/src/librustc_error_codes/error_codes/E0027.md @@ -1,8 +1,7 @@ -This error indicates that a pattern for a struct fails to specify a sub-pattern -for every one of the struct's fields. Ensure that each field from the struct's -definition is mentioned in the pattern, or use `..` to ignore unwanted fields. +A pattern for a struct fails to specify a sub-pattern for every one of the +struct's fields. -For example: +Erroneous code example: ```compile_fail,E0027 struct Dog { @@ -18,7 +17,8 @@ match d { } ``` -This is correct (explicit): +To fix this error, ensure that each field from the struct's definition is +mentioned in the pattern, or use `..` to ignore unwanted fields. Example: ``` struct Dog { From f8d575a0073300cd9cc3f204bc3c21b86eb5ec18 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Fri, 15 Nov 2019 13:14:10 +0100 Subject: [PATCH 05/12] Clean up E0029 --- src/librustc_error_codes/error_codes/E0029.md | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/librustc_error_codes/error_codes/E0029.md b/src/librustc_error_codes/error_codes/E0029.md index fadfda0a80c18..d12d85b9b4c99 100644 --- a/src/librustc_error_codes/error_codes/E0029.md +++ b/src/librustc_error_codes/error_codes/E0029.md @@ -1,8 +1,6 @@ -In a match expression, only numbers and characters can be matched against a -range. This is because the compiler checks that the range is non-empty at -compile-time, and is unable to evaluate arbitrary comparison functions. If you -want to capture values of an orderable type between two end-points, you can use -a guard. +Something other than numbers and characters has been used for a range. + +Erroneous code example: ```compile_fail,E0029 let string = "salutations !"; @@ -20,3 +18,9 @@ match string { _ => {} } ``` + +In a match expression, only numbers and characters can be matched against a +range. This is because the compiler checks that the range is non-empty at +compile-time, and is unable to evaluate arbitrary comparison functions. If you +want to capture values of an orderable type between two end-points, you can use +a guard. From cab1955b216f2b84c6129a0b734c89419f565b4a Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Fri, 15 Nov 2019 13:15:05 +0100 Subject: [PATCH 06/12] Clean up E0033 --- src/librustc_error_codes/error_codes/E0033.md | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/librustc_error_codes/error_codes/E0033.md b/src/librustc_error_codes/error_codes/E0033.md index 0bb70e89f3dc8..c49cedf2d5869 100644 --- a/src/librustc_error_codes/error_codes/E0033.md +++ b/src/librustc_error_codes/error_codes/E0033.md @@ -1,8 +1,6 @@ -This error indicates that a pointer to a trait type cannot be implicitly -dereferenced by a pattern. Every trait defines a type, but because the -size of trait implementers isn't fixed, this type has no compile-time size. -Therefore, all accesses to trait types must be through pointers. If you -encounter this error you should try to avoid dereferencing the pointer. +A trait type has been dereferenced. + +Erroneous code example: ```compile_fail,E0033 # trait SomeTrait { fn method_one(&self){} fn method_two(&self){} } @@ -17,7 +15,13 @@ trait_obj.method_one(); trait_obj.method_two(); ``` +A pointer to a trait type cannot be implicitly dereferenced by a pattern. Every +trait defines a type, but because the size of trait implementers isn't fixed, +this type has no compile-time size. Therefore, all accesses to trait types must +be through pointers. If you encounter this error you should try to avoid +dereferencing the pointer. + You can read more about trait objects in the [Trait Objects] section of the Reference. -[Trait Objects]: https://doc.rust-lang.org/reference/types.html#trait-objects +[Trait Objects]: https://doc.rust-lang.org/reference/types.html#trait-objects \ No newline at end of file From a5fcc5137f2b07d176c8d915a2b609462ee3a32c Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Fri, 15 Nov 2019 13:16:47 +0100 Subject: [PATCH 07/12] Clean up E0040 --- src/librustc_error_codes/error_codes/E0040.md | 25 ++++++++++++++++--- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/src/librustc_error_codes/error_codes/E0040.md b/src/librustc_error_codes/error_codes/E0040.md index fb262018c35b1..1373f8340d8f6 100644 --- a/src/librustc_error_codes/error_codes/E0040.md +++ b/src/librustc_error_codes/error_codes/E0040.md @@ -1,8 +1,6 @@ -It is not allowed to manually call destructors in Rust. It is also not -necessary to do this since `drop` is called automatically whenever a value goes -out of scope. +It is not allowed to manually call destructors in Rust. -Here's an example of this error: +Erroneous code example: ```compile_fail,E0040 struct Foo { @@ -20,3 +18,22 @@ fn main() { x.drop(); // error: explicit use of destructor method } ``` + +It is unnecessary to do this since `drop` is called automatically whenever a +value goes out of scope. However, if you really need to drop a value by hand, +you can use the `std::mem::drop` function: + +``` +struct Foo { + x: i32, +} +impl Drop for Foo { + fn drop(&mut self) { + println!("kaboom"); + } +} +fn main() { + let mut x = Foo { x: -7 }; + drop(x); // ok! +} +``` From 11bb297d2c3f3a28666df15f6be78d35393daf6c Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Fri, 15 Nov 2019 13:18:05 +0100 Subject: [PATCH 08/12] Clean up E0045 --- src/librustc_error_codes/error_codes/E0045.md | 20 +++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/librustc_error_codes/error_codes/E0045.md b/src/librustc_error_codes/error_codes/E0045.md index 537490eb4cf5b..143c693bf7c07 100644 --- a/src/librustc_error_codes/error_codes/E0045.md +++ b/src/librustc_error_codes/error_codes/E0045.md @@ -1,18 +1,18 @@ -Rust only supports variadic parameters for interoperability with C code in its -FFI. As such, variadic parameters can only be used with functions which are -using the C ABI. Examples of erroneous code: - -```compile_fail -#![feature(unboxed_closures)] +Variadic parameters have been used on a non-C ABI function. -extern "rust-call" { fn foo(x: u8, ...); } +Erroneous code example: -// or +```compile_fail,E0045 +#![feature(unboxed_closures)] -fn foo(x: u8, ...) {} +extern "rust-call" { + fn foo(x: u8, ...); // error! +} ``` -To fix such code, put them in an extern "C" block: +Rust only supports variadic parameters for interoperability with C code in its +FFI. As such, variadic parameters can only be used with functions which are +using the C ABI. To fix such code, put them in an extern "C" block: ``` extern "C" { From 2312207d9187efa1f3923f1b454c0f9ba730f46d Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Fri, 15 Nov 2019 13:19:34 +0100 Subject: [PATCH 09/12] Clean up E0049 --- src/librustc_error_codes/error_codes/E0049.md | 25 ++++++++++++++++--- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/src/librustc_error_codes/error_codes/E0049.md b/src/librustc_error_codes/error_codes/E0049.md index 721a7fd57a51f..a2034a3428b2d 100644 --- a/src/librustc_error_codes/error_codes/E0049.md +++ b/src/librustc_error_codes/error_codes/E0049.md @@ -1,8 +1,7 @@ -This error indicates that an attempted implementation of a trait method -has the wrong number of type or const parameters. +An attempted implementation of a trait method has the wrong number of type or +const parameters. -For example, the trait below has a method `foo` with a type parameter `T`, -but the implementation of `foo` for the type `Bar` is missing this parameter: +Erroneous code example: ```compile_fail,E0049 trait Foo { @@ -17,3 +16,21 @@ impl Foo for Bar { fn foo(x: bool) -> Self { Bar } } ``` + +For example, the `Foo` trait has a method `foo` with a type parameter `T`, +but the implementation of `foo` for the type `Bar` is missing this parameter. +To fix this error, they must have the same type parameters: + +``` +trait Foo { + fn foo(x: T) -> Self; +} + +struct Bar; + +impl Foo for Bar { + fn foo(x: T) -> Self { // ok! + Bar + } +} +``` From 1a3c8c8964ec44c81fee69fbe25cbd8910909284 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Fri, 15 Nov 2019 13:20:17 +0100 Subject: [PATCH 10/12] Clean up E0050 --- src/librustc_error_codes/error_codes/E0050.md | 26 +++++++++++++++---- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/src/librustc_error_codes/error_codes/E0050.md b/src/librustc_error_codes/error_codes/E0050.md index 79d070802d304..7b84c48007399 100644 --- a/src/librustc_error_codes/error_codes/E0050.md +++ b/src/librustc_error_codes/error_codes/E0050.md @@ -1,9 +1,7 @@ -This error indicates that an attempted implementation of a trait method -has the wrong number of function parameters. +An attempted implementation of a trait method has the wrong number of function +parameters. -For example, the trait below has a method `foo` with two function parameters -(`&self` and `u8`), but the implementation of `foo` for the type `Bar` omits -the `u8` parameter: +Erroneous code example: ```compile_fail,E0050 trait Foo { @@ -18,3 +16,21 @@ impl Foo for Bar { fn foo(&self) -> bool { true } } ``` + +For example, the `Foo` trait has a method `foo` with two function parameters +(`&self` and `u8`), but the implementation of `foo` for the type `Bar` omits +the `u8` parameter. To fix this error, they must have the same parameters: + +``` +trait Foo { + fn foo(&self, x: u8) -> bool; +} + +struct Bar; + +impl Foo for Bar { + fn foo(&self, x: u8) -> bool { // ok! + true + } +} +``` From 041c9f6f48ee5f145d268b324d4be83013fb6754 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Fri, 15 Nov 2019 13:21:50 +0100 Subject: [PATCH 11/12] Clean up E0054 --- src/librustc_error_codes/error_codes/E0054.md | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/librustc_error_codes/error_codes/E0054.md b/src/librustc_error_codes/error_codes/E0054.md index af71cb44462a6..c3eb375fbcc9a 100644 --- a/src/librustc_error_codes/error_codes/E0054.md +++ b/src/librustc_error_codes/error_codes/E0054.md @@ -1,5 +1,6 @@ -It is not allowed to cast to a bool. If you are trying to cast a numeric type -to a bool, you can compare it with zero instead: +It is not allowed to cast to a bool. + +Erroneous code example: ```compile_fail,E0054 let x = 5; @@ -8,6 +9,9 @@ let x = 5; let x_is_nonzero = x as bool; ``` +If you are trying to cast a numeric type to a bool, you can compare it with +zero instead: + ``` let x = 5; From f9fdc380c785290c86568181f78a1584dfad4863 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Fri, 15 Nov 2019 13:05:21 +0100 Subject: [PATCH 12/12] Clean up E0025, E0034, E0044, E0046 and E0053 --- src/librustc_error_codes/error_codes/E0025.md | 5 +++-- src/librustc_error_codes/error_codes/E0034.md | 4 +++- src/librustc_error_codes/error_codes/E0044.md | 1 + src/librustc_error_codes/error_codes/E0046.md | 4 +++- src/librustc_error_codes/error_codes/E0053.md | 2 +- 5 files changed, 11 insertions(+), 5 deletions(-) diff --git a/src/librustc_error_codes/error_codes/E0025.md b/src/librustc_error_codes/error_codes/E0025.md index 51073babd01de..a85dc8c1978b2 100644 --- a/src/librustc_error_codes/error_codes/E0025.md +++ b/src/librustc_error_codes/error_codes/E0025.md @@ -1,5 +1,6 @@ -Each field of a struct can only be bound once in a pattern. Erroneous code -example: +Each field of a struct can only be bound once in a pattern. + +Erroneous code example: ```compile_fail,E0025 struct Foo { diff --git a/src/librustc_error_codes/error_codes/E0034.md b/src/librustc_error_codes/error_codes/E0034.md index bc804b7faefca..2a21f3441c660 100644 --- a/src/librustc_error_codes/error_codes/E0034.md +++ b/src/librustc_error_codes/error_codes/E0034.md @@ -1,5 +1,7 @@ The compiler doesn't know what method to call because more than one method -has the same prototype. Erroneous code example: +has the same prototype. + +Erroneous code example: ```compile_fail,E0034 struct Test; diff --git a/src/librustc_error_codes/error_codes/E0044.md b/src/librustc_error_codes/error_codes/E0044.md index 8e877e7185997..635ff95329013 100644 --- a/src/librustc_error_codes/error_codes/E0044.md +++ b/src/librustc_error_codes/error_codes/E0044.md @@ -1,4 +1,5 @@ You cannot use type or const parameters on foreign items. + Example of erroneous code: ```compile_fail,E0044 diff --git a/src/librustc_error_codes/error_codes/E0046.md b/src/librustc_error_codes/error_codes/E0046.md index 2d0bb0863e883..d8f95330c364b 100644 --- a/src/librustc_error_codes/error_codes/E0046.md +++ b/src/librustc_error_codes/error_codes/E0046.md @@ -1,4 +1,6 @@ -Items are missing in a trait implementation. Erroneous code example: +Items are missing in a trait implementation. + +Erroneous code example: ```compile_fail,E0046 trait Foo { diff --git a/src/librustc_error_codes/error_codes/E0053.md b/src/librustc_error_codes/error_codes/E0053.md index 822d1f0094d36..cb2a8638a29c5 100644 --- a/src/librustc_error_codes/error_codes/E0053.md +++ b/src/librustc_error_codes/error_codes/E0053.md @@ -1,7 +1,7 @@ The parameters of any trait method must match between a trait implementation and the trait definition. -Here are a couple examples of this error: +Erroneous code example: ```compile_fail,E0053 trait Foo {