diff options
| author | bors <bors@rust-lang.org> | 2019-12-26 03:42:59 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2019-12-26 03:42:59 +0000 |
| commit | c0b16b4e6aa94cd83fd2c029356ba537dc4502c6 (patch) | |
| tree | a96f943e2f0d94458b4e3d1e2db6d6ae48c86e10 /src/librustc_error_codes/error_codes | |
| parent | b13d65ab9274323de72539556f2c5f7eef29f4a1 (diff) | |
| parent | 621d7e959b24ea8640584f0ced13ec016be35d16 (diff) | |
| download | rust-c0b16b4e6aa94cd83fd2c029356ba537dc4502c6.tar.gz rust-c0b16b4e6aa94cd83fd2c029356ba537dc4502c6.zip | |
Auto merge of #67268 - estebank:assoc-types, r=oli-obk
Tweak errors for missing associated types and type parameters * On `dyn Trait` missing associated types, provide a structured suggestion for them * On missing type parameters, provide structured suggestion for them * Point at trait definition when missing required type parameter * Tweak output of E0658 * Tweak wording of E0719 * Account for `Trait1 + Trait2` case Fix #66380, fix #60595. CC #63711.
Diffstat (limited to 'src/librustc_error_codes/error_codes')
| -rw-r--r-- | src/librustc_error_codes/error_codes/E0222.md | 51 |
1 files changed, 51 insertions, 0 deletions
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<COLOR>(c: dyn BoxCar<Color=COLOR>) {} // 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 `<BoxCar as Vehicle>::Color` or `<BoxCar as Box>::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<CAR, COLOR>( + c: CAR, +) where + // Bind the type parameter `CAR` to the trait `BoxCar` + CAR: BoxCar, + // Further restrict `<BoxCar as Vehicle>::Color` to be the same as the + // type parameter `COLOR` + CAR: Vehicle<Color = COLOR>, + // We can also simultaneously restrict the other trait's associated type + CAR: Box<Color = COLOR> +{} +``` |
