From bc744bca9063cd9145fceed4ba7ef14cab0ecdd6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Wed, 9 Oct 2019 15:54:23 -0700 Subject: [PATCH] Suggest associated bound restrictions in `impl`s --- src/librustc/hir/mod.rs | 6 +++ src/librustc/traits/error_reporting.rs | 18 ++++++++ src/test/ui/issues/issue-38821.stderr | 5 ++- .../missing-assoc-type-bound-restriction.rs | 25 +++++++++++ ...issing-assoc-type-bound-restriction.stderr | 43 +++++++++++++++++++ 5 files changed, 95 insertions(+), 2 deletions(-) create mode 100644 src/test/ui/suggestions/missing-assoc-type-bound-restriction.rs create mode 100644 src/test/ui/suggestions/missing-assoc-type-bound-restriction.stderr diff --git a/src/librustc/hir/mod.rs b/src/librustc/hir/mod.rs index 7350f89018be..fa0f620530de 100644 --- a/src/librustc/hir/mod.rs +++ b/src/librustc/hir/mod.rs @@ -669,6 +669,12 @@ impl WhereClause { Some(self.span) } } + + /// The `WhereClause` under normal circumstances points at either the predicates or the empty + /// space where the `where` clause should be. Only of use for diagnostic suggestions. + pub fn span_for_predicates_or_empty_place(&self) -> Span { + self.span + } } /// A single predicate in a where-clause. diff --git a/src/librustc/traits/error_reporting.rs b/src/librustc/traits/error_reporting.rs index 2ac691b47aca..91106d35a7ee 100644 --- a/src/librustc/traits/error_reporting.rs +++ b/src/librustc/traits/error_reporting.rs @@ -1044,6 +1044,24 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> { } return; } + hir::Node::Item(hir::Item { + kind: hir::ItemKind::Impl(_, _, _, generics, ..), .. + }) if projection.is_some() => { + err.span_suggestion( + generics.where_clause.span_for_predicates_or_empty_place().shrink_to_hi(), + "consider further restricting the associated type", + format!( + "{} {}", if generics.where_clause.predicates.is_empty() { + " where" + } else { + " ," + }, + trait_ref.to_predicate(), + ), + Applicability::MachineApplicable, + ); + return; + } hir::Node::Item(hir::Item { kind: hir::ItemKind::Struct(_, generics), span, .. }) | hir::Node::Item(hir::Item { kind: hir::ItemKind::Enum(_, generics), span, .. }) | hir::Node::Item(hir::Item { kind: hir::ItemKind::Union(_, generics), span, .. }) | diff --git a/src/test/ui/issues/issue-38821.stderr b/src/test/ui/issues/issue-38821.stderr index dbd204ec299d..3e2af7c44e4a 100644 --- a/src/test/ui/issues/issue-38821.stderr +++ b/src/test/ui/issues/issue-38821.stderr @@ -2,9 +2,10 @@ error[E0277]: the trait bound `::SqlType: NotNull` is not sat --> $DIR/issue-38821.rs:23:17 | LL | #[derive(Debug, Copy, Clone)] - | ^^^^ the trait `NotNull` is not implemented for `::SqlType` + | ^^^^- help: consider further restricting the associated type: `, ::SqlType: NotNull` + | | + | the trait `NotNull` is not implemented for `::SqlType` | - = help: consider adding a `where ::SqlType: NotNull` bound = note: required because of the requirements on the impl of `IntoNullable` for `::SqlType` error: aborting due to previous error diff --git a/src/test/ui/suggestions/missing-assoc-type-bound-restriction.rs b/src/test/ui/suggestions/missing-assoc-type-bound-restriction.rs new file mode 100644 index 000000000000..265ccb3125ca --- /dev/null +++ b/src/test/ui/suggestions/missing-assoc-type-bound-restriction.rs @@ -0,0 +1,25 @@ +// Running rustfix would cause the same suggestion to be applied multiple times, which results in +// invalid code. + +trait Parent { + type Ty; + type Assoc: Child; +} + +trait Child {} + +struct ChildWrapper(T); + +impl Child for ChildWrapper where T: Child {} + +struct ParentWrapper(T); + +impl> Parent for ParentWrapper { + //~^ ERROR the trait bound `::Assoc: Child` is not satisfied + //~| ERROR the trait bound `::Assoc: Child` is not satisfied + type Ty = A; + type Assoc = ChildWrapper; + //~^ ERROR the trait bound `::Assoc: Child` is not satisfied +} + +fn main() {} diff --git a/src/test/ui/suggestions/missing-assoc-type-bound-restriction.stderr b/src/test/ui/suggestions/missing-assoc-type-bound-restriction.stderr new file mode 100644 index 000000000000..bdea8ab97e5b --- /dev/null +++ b/src/test/ui/suggestions/missing-assoc-type-bound-restriction.stderr @@ -0,0 +1,43 @@ +error[E0277]: the trait bound `::Assoc: Child` is not satisfied + --> $DIR/missing-assoc-type-bound-restriction.rs:17:1 + | +LL | trait Parent { + | ------------ required by `Parent` +... +LL | impl> Parent for ParentWrapper { + | ^ - help: consider further restricting the associated type: `where ::Assoc: Child` + | _| + | | +LL | | +LL | | +LL | | type Ty = A; +LL | | type Assoc = ChildWrapper; +LL | | +LL | | } + | |_^ the trait `Child` is not implemented for `::Assoc` + +error[E0277]: the trait bound `::Assoc: Child` is not satisfied + --> $DIR/missing-assoc-type-bound-restriction.rs:17:28 + | +LL | impl> Parent for ParentWrapper { + | ^^^^^^ - help: consider further restricting the associated type: `where ::Assoc: Child` + | | + | the trait `Child` is not implemented for `::Assoc` + | + = note: required because of the requirements on the impl of `Child` for `ChildWrapper<::Assoc>` + +error[E0277]: the trait bound `::Assoc: Child` is not satisfied + --> $DIR/missing-assoc-type-bound-restriction.rs:21:5 + | +LL | trait Parent { + | ------------ required by `Parent` +... +LL | impl> Parent for ParentWrapper { + | - help: consider further restricting the associated type: `where ::Assoc: Child` +... +LL | type Assoc = ChildWrapper; + | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Child` is not implemented for `::Assoc` + +error: aborting due to 3 previous errors + +For more information about this error, try `rustc --explain E0277`.