From 4805e1291af490b6c76f49ee241ac9f0bc0ceba4 Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Mon, 11 Apr 2016 15:23:35 +0530 Subject: [PATCH 1/2] Review fixes for #32878 (which was accidentally merged) --- src/librustc_typeck/diagnostics.rs | 28 ++++++++++++++++++---------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/src/librustc_typeck/diagnostics.rs b/src/librustc_typeck/diagnostics.rs index 36a3082f830..cad1accfcb7 100644 --- a/src/librustc_typeck/diagnostics.rs +++ b/src/librustc_typeck/diagnostics.rs @@ -3645,9 +3645,8 @@ fn main() { "##, E0520: r##" -A non-default implementation was already made on this type -implementation so it cannot be specialized afterward. Erroneous -code example: +A non-default implementation was already made on this type so it cannot be +specialized further. Erroneous code example: ```compile_fail #![feature(specialization)] @@ -3656,14 +3655,18 @@ trait SpaceLama { fn fly(&self); } +// applies to all T impl SpaceLama for T { default fn fly(&self) {} } +// non-default impl +// applies to all `Clone` T and overrides the previous impl impl SpaceLama for T { fn fly(&self) {} } +// since `i32` is clone, this conflicts with the previous implementation impl SpaceLama for i32 { default fn fly(&self) {} // error: item `fly` is provided by an `impl` that specializes @@ -3672,28 +3675,33 @@ impl SpaceLama for i32 { } ``` -To fix this error, you need to specialize the implementation on the -parent(s) implementation first. Example: +Specialization only allows you to override `default` functions in +implementations. -```compile_fail +To fix this error, you need to mark all the parent implementations as default. +Example: + +``` #![feature(specialization)] trait SpaceLama { fn fly(&self); } +// applies to all T impl SpaceLama for T { default fn fly(&self) {} // This is a parent implementation. } +// applies to all `Clone` T; overrides the previous impl impl SpaceLama for T { - default fn fly(&self) {} // This is a parent implementation but not - // a default one so you need to add default - // keyword. + default fn fly(&self) {} // This is a parent implementation but was + // previously not a default one, causing the error } +// applies to i32, overrides the previous two impls impl SpaceLama for i32 { - default fn fly(&self) {} // And now that's ok! + fn fly(&self) {} // And now that's ok! } ``` "##, From 69095bb02393b861c062fa80cde717e9eb7b2d29 Mon Sep 17 00:00:00 2001 From: Manish Goregaokar Date: Mon, 11 Apr 2016 15:30:22 +0530 Subject: [PATCH 2/2] Tibet does not have a space program. Peru does. --- src/librustc_typeck/diagnostics.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/librustc_typeck/diagnostics.rs b/src/librustc_typeck/diagnostics.rs index cad1accfcb7..8f76bf92ef4 100644 --- a/src/librustc_typeck/diagnostics.rs +++ b/src/librustc_typeck/diagnostics.rs @@ -3651,23 +3651,23 @@ specialized further. Erroneous code example: ```compile_fail #![feature(specialization)] -trait SpaceLama { +trait SpaceLlama { fn fly(&self); } // applies to all T -impl SpaceLama for T { +impl SpaceLlama for T { default fn fly(&self) {} } // non-default impl // applies to all `Clone` T and overrides the previous impl -impl SpaceLama for T { +impl SpaceLlama for T { fn fly(&self) {} } // since `i32` is clone, this conflicts with the previous implementation -impl SpaceLama for i32 { +impl SpaceLlama for i32 { default fn fly(&self) {} // error: item `fly` is provided by an `impl` that specializes // another, but the item in the parent `impl` is not marked @@ -3684,23 +3684,23 @@ Example: ``` #![feature(specialization)] -trait SpaceLama { +trait SpaceLlama { fn fly(&self); } // applies to all T -impl SpaceLama for T { +impl SpaceLlama for T { default fn fly(&self) {} // This is a parent implementation. } // applies to all `Clone` T; overrides the previous impl -impl SpaceLama for T { +impl SpaceLlama for T { default fn fly(&self) {} // This is a parent implementation but was // previously not a default one, causing the error } // applies to i32, overrides the previous two impls -impl SpaceLama for i32 { +impl SpaceLlama for i32 { fn fly(&self) {} // And now that's ok! } ```