Skip to content

Commit

Permalink
Rollup merge of rust-lang#67268 - estebank:assoc-types, r=oli-obk
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Centril authored Dec 20, 2019
2 parents 0e61dcc + d72ceb4 commit 28a724e
Show file tree
Hide file tree
Showing 51 changed files with 1,229 additions and 522 deletions.
3 changes: 1 addition & 2 deletions src/librustc_error_codes/error_codes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ E0211: include_str!("./error_codes/E0211.md"),
E0214: include_str!("./error_codes/E0214.md"),
E0220: include_str!("./error_codes/E0220.md"),
E0221: include_str!("./error_codes/E0221.md"),
E0222: include_str!("./error_codes/E0222.md"),
E0223: include_str!("./error_codes/E0223.md"),
E0225: include_str!("./error_codes/E0225.md"),
E0229: include_str!("./error_codes/E0229.md"),
Expand Down Expand Up @@ -456,8 +457,6 @@ E0745: include_str!("./error_codes/E0745.md"),
// E0217, // ambiguous associated type, defined in multiple supertraits
// E0218, // no associated type defined
// E0219, // associated type defined in higher-ranked supertrait
// E0222, // Error code E0045 (variadic function must have C or cdecl calling
// convention) duplicate
E0224, // at least one non-builtin train is required for an object type
E0226, // only a single explicit lifetime bound is permitted
E0227, // ambiguous lifetime bound, explicit lifetime bound required
Expand Down
51 changes: 51 additions & 0 deletions src/librustc_error_codes/error_codes/E0222.md
Original file line number Diff line number Diff line change
@@ -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>
{}
```
10 changes: 7 additions & 3 deletions src/librustc_passes/ast_validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -736,9 +736,13 @@ impl<'a> Visitor<'a> for AstValidator<'a> {

for predicate in &generics.where_clause.predicates {
if let WherePredicate::EqPredicate(ref predicate) = *predicate {
self.err_handler()
.span_err(predicate.span, "equality constraints are not yet \
supported in where clauses (see #20041)");
self.err_handler().struct_span_err(
predicate.span,
"equality constraints are not yet supported in where clauses",
)
.span_label(predicate.span, "not supported")
.note("for more information, see #20041")
.emit();
}
}

Expand Down
Loading

0 comments on commit 28a724e

Please sign in to comment.