about summary refs log tree commit diff
path: root/tests/ui
diff options
context:
space:
mode:
authorEsteban Küber <esteban@kuber.com.ar>2023-09-29 03:16:11 +0000
committerEsteban Küber <esteban@kuber.com.ar>2023-10-13 19:13:56 +0000
commit781e86477cf71c7850d4d1f169c58d7e398654f7 (patch)
tree0a825a39f8c331e5dd1f611867d3efe79d8958f7 /tests/ui
parentdf4379b4eb5357263f0cf75475953f9b5c48c31f (diff)
downloadrust-781e86477cf71c7850d4d1f169c58d7e398654f7.tar.gz
rust-781e86477cf71c7850d4d1f169c58d7e398654f7.zip
Suggest trait bounds for used associated type on type param
Fix #101351.

When an associated type on a type parameter is used, and the type
parameter isn't constrained by the correct trait, suggest the
appropriate trait bound:

```
error[E0220]: associated type `Associated` not found for `T`
 --> file.rs:6:15
  |
6 |     field: T::Associated,
  |               ^^^^^^^^^^ there is a similarly named associated type `Associated` in the trait `Foo`
  |
help: consider restricting type parameter `T`
  |
5 | struct Generic<T: Foo> {
  |                 +++++
  ```

When an associated type on a type parameter has a typo, suggest fixing
it:

```
error[E0220]: associated type `Baa` not found for `T`
  --> $DIR/issue-55673.rs:9:8
   |
LL |     T::Baa: std::fmt::Debug,
   |        ^^^ there is a similarly named associated type `Bar` in the trait `Foo`
   |
help: change the associated type name to use `Bar` from `Foo`
   |
LL |     T::Bar: std::fmt::Debug,
   |        ~~~
```
Diffstat (limited to 'tests/ui')
-rw-r--r--tests/ui/resolve/issue-55673.fixed21
-rw-r--r--tests/ui/resolve/issue-55673.rs9
-rw-r--r--tests/ui/resolve/issue-55673.stderr24
-rw-r--r--tests/ui/type-alias-impl-trait/not_well_formed.fixed19
-rw-r--r--tests/ui/type-alias-impl-trait/not_well_formed.rs2
-rw-r--r--tests/ui/type-alias-impl-trait/not_well_formed.stderr7
6 files changed, 79 insertions, 3 deletions
diff --git a/tests/ui/resolve/issue-55673.fixed b/tests/ui/resolve/issue-55673.fixed
new file mode 100644
index 00000000000..261742a26cb
--- /dev/null
+++ b/tests/ui/resolve/issue-55673.fixed
@@ -0,0 +1,21 @@
+// run-rustfix
+#![allow(dead_code)]
+trait Foo {
+    type Bar;
+}
+
+fn foo<T: Foo>()
+where
+    T::Bar: std::fmt::Debug,
+    //~^ ERROR associated type `Baa` not found for `T`
+{
+}
+
+fn bar<T>()
+where
+    T::Bar: std::fmt::Debug, T: Foo
+    //~^ ERROR associated type `Baa` not found for `T`
+{
+}
+
+fn main() {}
diff --git a/tests/ui/resolve/issue-55673.rs b/tests/ui/resolve/issue-55673.rs
index 0436bd39742..6ac49be141c 100644
--- a/tests/ui/resolve/issue-55673.rs
+++ b/tests/ui/resolve/issue-55673.rs
@@ -1,3 +1,5 @@
+// run-rustfix
+#![allow(dead_code)]
 trait Foo {
     type Bar;
 }
@@ -9,4 +11,11 @@ where
 {
 }
 
+fn bar<T>()
+where
+    T::Baa: std::fmt::Debug,
+    //~^ ERROR associated type `Baa` not found for `T`
+{
+}
+
 fn main() {}
diff --git a/tests/ui/resolve/issue-55673.stderr b/tests/ui/resolve/issue-55673.stderr
index 39318f95905..ffc3252230a 100644
--- a/tests/ui/resolve/issue-55673.stderr
+++ b/tests/ui/resolve/issue-55673.stderr
@@ -1,9 +1,29 @@
 error[E0220]: associated type `Baa` not found for `T`
-  --> $DIR/issue-55673.rs:7:8
+  --> $DIR/issue-55673.rs:9:8
    |
 LL |     T::Baa: std::fmt::Debug,
    |        ^^^ there is a similarly named associated type `Bar` in the trait `Foo`
+   |
+help: change the associated type name to use `Bar` from `Foo`
+   |
+LL |     T::Bar: std::fmt::Debug,
+   |        ~~~
+
+error[E0220]: associated type `Baa` not found for `T`
+  --> $DIR/issue-55673.rs:16:8
+   |
+LL |     T::Baa: std::fmt::Debug,
+   |        ^^^ there is a similarly named associated type `Bar` in the trait `Foo`
+   |
+help: consider further restricting type parameter `T`
+   |
+LL |     T::Baa: std::fmt::Debug, T: Foo
+   |                            ~~~~~~~~
+help: and also change the associated type name
+   |
+LL |     T::Bar: std::fmt::Debug,
+   |        ~~~
 
-error: aborting due to previous error
+error: aborting due to 2 previous errors
 
 For more information about this error, try `rustc --explain E0220`.
diff --git a/tests/ui/type-alias-impl-trait/not_well_formed.fixed b/tests/ui/type-alias-impl-trait/not_well_formed.fixed
new file mode 100644
index 00000000000..d98e83ff6dd
--- /dev/null
+++ b/tests/ui/type-alias-impl-trait/not_well_formed.fixed
@@ -0,0 +1,19 @@
+// run-rustfix
+#![feature(type_alias_impl_trait)]
+#![allow(dead_code)]
+
+fn main() {}
+
+trait TraitWithAssoc {
+    type Assoc;
+}
+
+type Foo<V: TraitWithAssoc> = impl Trait<V::Assoc>; //~ associated type `Assoc` not found for `V`
+
+trait Trait<U> {}
+
+impl<W> Trait<W> for () {}
+
+fn foo_desugared<T: TraitWithAssoc>(_: T) -> Foo<T> {
+    ()
+}
diff --git a/tests/ui/type-alias-impl-trait/not_well_formed.rs b/tests/ui/type-alias-impl-trait/not_well_formed.rs
index fbb7a4d58e4..18f173a693d 100644
--- a/tests/ui/type-alias-impl-trait/not_well_formed.rs
+++ b/tests/ui/type-alias-impl-trait/not_well_formed.rs
@@ -1,4 +1,6 @@
+// run-rustfix
 #![feature(type_alias_impl_trait)]
+#![allow(dead_code)]
 
 fn main() {}
 
diff --git a/tests/ui/type-alias-impl-trait/not_well_formed.stderr b/tests/ui/type-alias-impl-trait/not_well_formed.stderr
index c36b95f47e8..e89cae2d104 100644
--- a/tests/ui/type-alias-impl-trait/not_well_formed.stderr
+++ b/tests/ui/type-alias-impl-trait/not_well_formed.stderr
@@ -1,8 +1,13 @@
 error[E0220]: associated type `Assoc` not found for `V`
-  --> $DIR/not_well_formed.rs:9:29
+  --> $DIR/not_well_formed.rs:11:29
    |
 LL | type Foo<V> = impl Trait<V::Assoc>;
    |                             ^^^^^ there is a similarly named associated type `Assoc` in the trait `TraitWithAssoc`
+   |
+help: consider restricting type parameter `V`
+   |
+LL | type Foo<V: TraitWithAssoc> = impl Trait<V::Assoc>;
+   |           ++++++++++++++++
 
 error: aborting due to previous error