From 2255948c6cbe0793ac80c923860c82fa8f7650c5 Mon Sep 17 00:00:00 2001 From: Nikhil Shagrithaya Date: Sun, 12 Jun 2016 03:09:38 +0530 Subject: [PATCH 1/4] Add error description for E0406 --- src/librustc_resolve/diagnostics.rs | 38 ++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/src/librustc_resolve/diagnostics.rs b/src/librustc_resolve/diagnostics.rs index 476b9a5447b07..5c7d597d1bb78 100644 --- a/src/librustc_resolve/diagnostics.rs +++ b/src/librustc_resolve/diagnostics.rs @@ -444,6 +444,43 @@ impl SomeTrait for Foo { // ok! ``` "##, +E0406: r##" +The function is referring to an associated type which hasn't been declared in +the trait. + +```compile_fail +trait Foo { + type Bar; + + fn return_bool(&self, &Self::Bar, &Self::Baz) -> bool; + // etc +} +``` + +One solution might be to declare the associated type in the trait: + +``` +trait Foo { + type Bar; + type Baz; //declare required associated type + + fn return_bool(&self, &Self::Bar, &Self::Baz) -> bool; + // etc +} +``` +Alternatively, you could remove the input variable +corresponding to the associated type from the function signature: + +``` +trait Foo { + type Bar; + + fn return_bool(&self, &Self::Bar) -> bool; //&Self::Baz has been removed + // etc +} +``` +"##, + E0407: r##" A definition of a method not in the implemented trait was given in a trait implementation. Example of erroneous code: @@ -1105,7 +1142,6 @@ register_diagnostics! { // E0257, // E0258, E0402, // cannot use an outer type parameter in this context - E0406, // undeclared associated type // E0410, merged into 408 // E0413, merged into 530 // E0414, merged into 530 From 27325c956572839a86ea51a07a0083a18f8b204c Mon Sep 17 00:00:00 2001 From: Nikhil Shagrithaya Date: Sun, 12 Jun 2016 15:55:37 +0530 Subject: [PATCH 2/4] Error desc: Minor fixes --- src/librustc_resolve/diagnostics.rs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/librustc_resolve/diagnostics.rs b/src/librustc_resolve/diagnostics.rs index 5c7d597d1bb78..5cb4e4db6efbd 100644 --- a/src/librustc_resolve/diagnostics.rs +++ b/src/librustc_resolve/diagnostics.rs @@ -445,15 +445,16 @@ impl SomeTrait for Foo { // ok! "##, E0406: r##" -The function is referring to an associated type which hasn't been declared in -the trait. +A function is referring to an associated type which hasn't been declared in +the trait. Example of erroneous code: ```compile_fail trait Foo { type Bar; + // error: function signature contains a reference to Self::Baz, + // but 'Baz' hasn't been declared as an associated type of the trait fn return_bool(&self, &Self::Bar, &Self::Baz) -> bool; - // etc } ``` @@ -462,12 +463,12 @@ One solution might be to declare the associated type in the trait: ``` trait Foo { type Bar; - type Baz; //declare required associated type + type Baz; // declare 'Baz' fn return_bool(&self, &Self::Bar, &Self::Baz) -> bool; - // etc } ``` + Alternatively, you could remove the input variable corresponding to the associated type from the function signature: @@ -475,8 +476,7 @@ corresponding to the associated type from the function signature: trait Foo { type Bar; - fn return_bool(&self, &Self::Bar) -> bool; //&Self::Baz has been removed - // etc + fn return_bool(&self, &Self::Bar) -> bool; // &Self::Baz has been removed } ``` "##, From 19892f4372212e3d02e6e645468a520eca7eeaf7 Mon Sep 17 00:00:00 2001 From: Nikhil Shagrithaya Date: Sun, 12 Jun 2016 21:05:40 +0530 Subject: [PATCH 3/4] Error desc: minor fixes II --- src/librustc_resolve/diagnostics.rs | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/librustc_resolve/diagnostics.rs b/src/librustc_resolve/diagnostics.rs index 5cb4e4db6efbd..93c7f0290ce01 100644 --- a/src/librustc_resolve/diagnostics.rs +++ b/src/librustc_resolve/diagnostics.rs @@ -452,18 +452,26 @@ the trait. Example of erroneous code: trait Foo { type Bar; - // error: function signature contains a reference to Self::Baz, - // but 'Baz' hasn't been declared as an associated type of the trait + // error: function signature contains a reference to `Self::Baz`, but + // `Baz` hasn't been declared as an associated type of the trait fn return_bool(&self, &Self::Bar, &Self::Baz) -> bool; } ``` +The use of 'associated types' allows us declare variables which might be used +in any of the function signatures in a particular trait. This helps in +eliminating redundancies and improves readability of code. It works by using +the `type` keyword to declare a variable (Eg: `type A;`), and then using it +in the function signature as `&Self::A`. `Self` refers to the object which +has invoked the method, and this object can be of any type for which trait +`Foo` has been implemented. + One solution might be to declare the associated type in the trait: ``` trait Foo { type Bar; - type Baz; // declare 'Baz' + type Baz; // declare `Baz` fn return_bool(&self, &Self::Bar, &Self::Baz) -> bool; } @@ -476,7 +484,8 @@ corresponding to the associated type from the function signature: trait Foo { type Bar; - fn return_bool(&self, &Self::Bar) -> bool; // &Self::Baz has been removed + // `&Self::Baz` has been removed + fn return_bool(&self, &Self::Bar) -> bool; } ``` "##, From 45eec30537db5ea7ab0bc9d4903a60d29723b55b Mon Sep 17 00:00:00 2001 From: Nikhil Shagrithaya Date: Mon, 13 Jun 2016 17:28:50 +0530 Subject: [PATCH 4/4] Error desc: minor fixes III --- src/librustc_resolve/diagnostics.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/librustc_resolve/diagnostics.rs b/src/librustc_resolve/diagnostics.rs index 93c7f0290ce01..5db90e15dcd99 100644 --- a/src/librustc_resolve/diagnostics.rs +++ b/src/librustc_resolve/diagnostics.rs @@ -457,14 +457,9 @@ trait Foo { fn return_bool(&self, &Self::Bar, &Self::Baz) -> bool; } ``` - -The use of 'associated types' allows us declare variables which might be used -in any of the function signatures in a particular trait. This helps in -eliminating redundancies and improves readability of code. It works by using -the `type` keyword to declare a variable (Eg: `type A;`), and then using it -in the function signature as `&Self::A`. `Self` refers to the object which -has invoked the method, and this object can be of any type for which trait -`Foo` has been implemented. +You tried to use an associated type which hasn't been declared in the +trait body. All associated types must be declared with the +type keyword, e.g. `type a;` before being used. One solution might be to declare the associated type in the trait: @@ -488,6 +483,11 @@ trait Foo { fn return_bool(&self, &Self::Bar) -> bool; } ``` + +For more on associated types, refer to the associated types section +of the Rust book: + +https://doc.rust-lang.org/book/associated-types.html "##, E0407: r##"