Skip to content

Commit 28a724e

Browse files
authored
Rollup merge of rust-lang#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 rust-lang#66380, fix rust-lang#60595. CC rust-lang#63711.
2 parents 0e61dcc + d72ceb4 commit 28a724e

File tree

51 files changed

+1229
-522
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

+1229
-522
lines changed

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"),
@@ -456,8 +457,6 @@ E0745: include_str!("./error_codes/E0745.md"),
456457
// E0217, // ambiguous associated type, defined in multiple supertraits
457458
// E0218, // no associated type defined
458459
// E0219, // associated type defined in higher-ranked supertrait
459-
// E0222, // Error code E0045 (variadic function must have C or cdecl calling
460-
// convention) duplicate
461460
E0224, // at least one non-builtin train is required for an object type
462461
E0226, // only a single explicit lifetime bound is permitted
463462
E0227, // ambiguous lifetime bound, explicit lifetime bound required
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+
```

src/librustc_passes/ast_validation.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -736,9 +736,13 @@ impl<'a> Visitor<'a> for AstValidator<'a> {
736736

737737
for predicate in &generics.where_clause.predicates {
738738
if let WherePredicate::EqPredicate(ref predicate) = *predicate {
739-
self.err_handler()
740-
.span_err(predicate.span, "equality constraints are not yet \
741-
supported in where clauses (see #20041)");
739+
self.err_handler().struct_span_err(
740+
predicate.span,
741+
"equality constraints are not yet supported in where clauses",
742+
)
743+
.span_label(predicate.span, "not supported")
744+
.note("for more information, see #20041")
745+
.emit();
742746
}
743747
}
744748

0 commit comments

Comments
 (0)