Skip to content

Commit c0b16b4

Browse files
committed
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.
2 parents b13d65a + 621d7e9 commit c0b16b4

File tree

51 files changed

+1160
-459
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

51 files changed

+1160
-459
lines changed

Diff for: src/librustc_error_codes/error_codes.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ E0211: include_str!("./error_codes/E0211.md"),
116116
E0214: include_str!("./error_codes/E0214.md"),
117117
E0220: include_str!("./error_codes/E0220.md"),
118118
E0221: include_str!("./error_codes/E0221.md"),
119+
E0222: include_str!("./error_codes/E0222.md"),
119120
E0223: include_str!("./error_codes/E0223.md"),
120121
E0225: include_str!("./error_codes/E0225.md"),
121122
E0229: include_str!("./error_codes/E0229.md"),
@@ -457,8 +458,6 @@ E0745: include_str!("./error_codes/E0745.md"),
457458
// E0217, // ambiguous associated type, defined in multiple supertraits
458459
// E0218, // no associated type defined
459460
// E0219, // associated type defined in higher-ranked supertrait
460-
// E0222, // Error code E0045 (variadic function must have C or cdecl calling
461-
// convention) duplicate
462461
E0224, // at least one non-builtin train is required for an object type
463462
E0226, // only a single explicit lifetime bound is permitted
464463
E0227, // ambiguous lifetime bound, explicit lifetime bound required

Diff for: src/librustc_error_codes/error_codes/E0222.md

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
An attempt was made to constrain an associated type.
2+
For example:
3+
4+
```compile_fail,E0222
5+
pub trait Vehicle {
6+
type Color;
7+
}
8+
9+
pub trait Box {
10+
type Color;
11+
}
12+
13+
pub trait BoxCar : Box + Vehicle {}
14+
15+
fn dent_object<COLOR>(c: dyn BoxCar<Color=COLOR>) {} // Invalid constraint
16+
```
17+
18+
In this example, `BoxCar` has two super-traits: `Vehicle` and `Box`. Both of
19+
these traits define an associated type `Color`. `BoxCar` inherits two types
20+
with that name from both super-traits. Because of this, we need to use the
21+
fully qualified path syntax to refer to the appropriate `Color` associated
22+
type, either `<BoxCar as Vehicle>::Color` or `<BoxCar as Box>::Color`, but this
23+
syntax is not allowed to be used in a function signature.
24+
25+
In order to encode this kind of constraint, a `where` clause and a new type
26+
parameter are needed:
27+
28+
```
29+
pub trait Vehicle {
30+
type Color;
31+
}
32+
33+
pub trait Box {
34+
type Color;
35+
}
36+
37+
pub trait BoxCar : Box + Vehicle {}
38+
39+
// Introduce a new `CAR` type parameter
40+
fn foo<CAR, COLOR>(
41+
c: CAR,
42+
) where
43+
// Bind the type parameter `CAR` to the trait `BoxCar`
44+
CAR: BoxCar,
45+
// Further restrict `<BoxCar as Vehicle>::Color` to be the same as the
46+
// type parameter `COLOR`
47+
CAR: Vehicle<Color = COLOR>,
48+
// We can also simultaneously restrict the other trait's associated type
49+
CAR: Box<Color = COLOR>
50+
{}
51+
```

Diff for: src/librustc_passes/ast_validation.rs

+1
Original file line numberDiff line numberDiff line change
@@ -804,6 +804,7 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
804804
predicate.span,
805805
"equality constraints are not yet supported in `where` clauses",
806806
)
807+
.span_label(predicate.span, "not supported")
807808
.note(
808809
"for more information, see https://github.com/rust-lang/rust/issues/20041",
809810
)

0 commit comments

Comments
 (0)