From 66ea471f9d4a14e5af72e86ee7b12ed9d9fa3ca5 Mon Sep 17 00:00:00 2001 From: Esteban Küber Date: Thu, 12 Dec 2019 21:15:19 -0800 Subject: Handle more specific case E0222 --- src/librustc_error_codes/error_codes/E0222.md | 51 +++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 src/librustc_error_codes/error_codes/E0222.md (limited to 'src/librustc_error_codes/error_codes') diff --git a/src/librustc_error_codes/error_codes/E0222.md b/src/librustc_error_codes/error_codes/E0222.md new file mode 100644 index 00000000000..66b6c4d712b --- /dev/null +++ b/src/librustc_error_codes/error_codes/E0222.md @@ -0,0 +1,51 @@ +An attempt was made to constrain an associated type. +For example: + +```compile_fail,E0222 +pub trait Vehicle { + type Color; +} + +pub trait Box { + type Color; +} + +pub trait BoxCar : Box + Vehicle {} + +fn dent_object(c: dyn BoxCar) {} // Invalid constraint +``` + +In this example, `BoxCar` has two super-traits: `Vehicle` and `Box`. Both of +these traits define an associated type `Color`. `BoxCar` inherits two types +with that name from both super-traits. Because of this, we need to use the +fully qualified path syntax to refer to the appropriate `Color` associated +type, either `::Color` or `::Color`, but this +syntax is not allowed to be used in a function signature. + +In order to encode this kind of constraint, a `where` clause and a new type +parameter are needed: + +``` +pub trait Vehicle { + type Color; +} + +pub trait Box { + type Color; +} + +pub trait BoxCar : Box + Vehicle {} + +// Introduce a new `CAR` type parameter +fn foo( + c: CAR, +) where + // Bind the type parameter `CAR` to the trait `BoxCar` + CAR: BoxCar, + // Further restrict `::Color` to be the same as the + // type parameter `COLOR` + CAR: Vehicle, + // We can also simultaneously restrict the other trait's associated type + CAR: Box +{} +``` -- cgit 1.4.1-3-g733a5