Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a note when suggesting to use Impl Trait #112088

Closed
wants to merge 3 commits into from

Conversation

sladyn98
Copy link
Contributor

Add a note to E308 explaining that generic type variables are chosen by the caller rather than the implementer, when suggesting to use impl Trait instead of a generic type variable.

error[E0308]: mismatched types
 --> src/lib.rs:3:5
  |
2 | fn foo<T: Clone>() -> T {
  |        -              -
  |        |              |
  |        |              expected `T` because of return type
  |        |              help: consider using an impl return type: `impl Clone`
  |        this type parameter
3 |     "hello"
  |     ^^^^^^^ expected type parameter `T`, found `&str`
  |
  = note: expected type parameter `T`
                  found reference `&'static str`
  = note: the caller chooses the value of a type parameter

This PR completes the work left here #104755

r? rust-lang/diagnostics

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels May 29, 2023
@sladyn98
Copy link
Contributor Author

@davidtwco Requesting your review as you reviewed the previous PR here #104755

Thanks in advance

@compiler-errors
Copy link
Member

Did you bless tests or are there no UI tests affected by this change?

@rust-log-analyzer

This comment has been minimized.

@sladyn98
Copy link
Contributor Author

@compiler-errors Oh yeah I did a x.py test --bless and committed the result

@sladyn98 sladyn98 requested a review from lukas-code May 29, 2023 22:07
@rust-log-analyzer

This comment has been minimized.

Add tests

Update compiler/rustc_hir_typeck/src/fn_ctxt/suggestions.rs

Co-authored-by: Lukas <lukas-code@outlook.com>

Fixed tests
@@ -856,6 +856,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
format!("impl {}", all_bounds_str),
Applicability::MaybeIncorrect,
);
err.note(format!("the type of `{}` is chosen by the caller", expected_ty_as_param.name));
Copy link
Member

@compiler-errors compiler-errors May 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if this note adds much value in this context. Not exactly sure why, but something about the ordering or placement of this note feels to me like it's missing context.

In the tests/ui/return/return-impl-trait-bad.stderr stderr below:

LL | fn used_in_trait<T>() -> T
   |                  -       -
   |                  |       |
   |                  |       expected `T` because of return type
   |                  |       help: consider using an impl return type: `impl Send`
   |                  this type parameter

My sense would be that "this type parameter" label could be replaced with the text above... Not sure how hard that is though.

Alternatively, "the type of {} is chosen by the caller" should probably be extended to explain its significance. Maybe something along the lines of "and may be a type that is different than {(the other type mentioned)}"...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I updated the message to explain its significance. However finding exactly where "this type parameter" is derived from and put into the message here is a bit hard. Do let me know if this is sufficient, or if not some more context on how I can find the construction of the above "this type parameter"

@@ -53,6 +53,7 @@ LL | "don't suggest this, because the generic param is used in the bound."
|
= note: expected type parameter `T`
found reference `&'static str`
= note: the type of `T` is chosen by the caller and may be a type that is different than `T`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

may be a type that is different than T

Hmm... typo?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed it

Copy link
Member

@compiler-errors compiler-errors May 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I was not clear with what I meant here.

the type of T is chosen by the caller and may be a type that is different than T

This sentence mentions T twice, which does not make sense. The other type in this error is &'static str, which should be mentioned.

@rust-log-analyzer
Copy link
Collaborator

The job x86_64-gnu-llvm-14 failed! Check out the build log: (web) (plain)

Click to see the possible cause of the failure (guessed by this bot)

53    |
54    = note: expected type parameter `T`
55                    found reference `&'static str`
-    = note: the type of `T` is chosen by the caller and may be a type that is different than `T`
+    = note: the type of `T` is chosen by the caller and may be a type that is different from `T`
58 error: aborting due to 4 previous errors
59 



The actual stderr differed from the expected stderr.
Actual stderr saved to /checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/return/return-impl-trait-bad/return-impl-trait-bad.stderr
To update references, rerun the tests and pass the `--bless` flag
To only update this specific test, also pass `--test-args return/return-impl-trait-bad.rs`

error: 1 errors occurred comparing output.
status: exit status: 1
command: "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2/bin/rustc" "/checkout/tests/ui/return/return-impl-trait-bad.rs" "-Zthreads=1" "--sysroot" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2" "--target=x86_64-unknown-linux-gnu" "--error-format" "json" "--json" "future-incompat" "-Ccodegen-units=1" "-Zui-testing" "-Zsimulate-remapped-rust-src-base=/rustc/FAKE_PREFIX" "-Ztranslate-remapped-path-to-local-path=no" "-Zdeduplicate-diagnostics=no" "-Cstrip=debuginfo" "--remap-path-prefix=/checkout/tests/ui=fake-test-src-base" "--emit" "metadata" "-C" "prefer-dynamic" "--out-dir" "/checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/return/return-impl-trait-bad" "-A" "unused" "-Crpath" "-Cdebuginfo=0" "-Lnative=/checkout/obj/build/x86_64-unknown-linux-gnu/native/rust-test-helpers" "-L" "/checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/return/return-impl-trait-bad/auxiliary"
stdout: none
error[E0308]: mismatched types
  --> fake-test-src-base/return/return-impl-trait-bad.rs:5:5
   |
   |
LL | fn bad_echo<T>(_t: T) -> T {
   |             -            - expected `T` because of return type
   |             this type parameter
   |             this type parameter
LL |     "this should not suggest impl Trait" //~ ERROR mismatched types
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected type parameter `T`, found `&str`
   = note: expected type parameter `T`
                   found reference `&'static str`

error[E0308]: mismatched types
error[E0308]: mismatched types
  --> fake-test-src-base/return/return-impl-trait-bad.rs:9:5
   |
LL | fn bad_echo_2<T: Trait>(_t: T) -> T {
   |               -                   - expected `T` because of return type
   |               this type parameter
   |               this type parameter
LL |     "this will not suggest it, because that would probably be wrong" //~ ERROR mismatched types
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected type parameter `T`, found `&str`
   = note: expected type parameter `T`
                   found reference `&'static str`

error[E0308]: mismatched types
error[E0308]: mismatched types
  --> fake-test-src-base/return/return-impl-trait-bad.rs:17:5
   |
LL | fn other_bounds_bad<T>() -> T
   |                     -       - expected `T` because of return type
   |                     this type parameter
...
...
LL |     "don't suggest this, because Option<T> places additional constraints" //~ ERROR mismatched types
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected type parameter `T`, found `&str`
   = note: expected type parameter `T`
                   found reference `&'static str`

error[E0308]: mismatched types
error[E0308]: mismatched types
  --> fake-test-src-base/return/return-impl-trait-bad.rs:28:5
   |
LL | fn used_in_trait<T>() -> T
   |                  |       |
   |                  |       expected `T` because of return type
   |                  |       help: consider using an impl return type: `impl Send`
   |                  this type parameter
   |                  this type parameter
...
LL |     "don't suggest this, because the generic param is used in the bound." //~ ERROR mismatched types
   |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected type parameter `T`, found `&str`
   = note: expected type parameter `T`
                   found reference `&'static str`
                   found reference `&'static str`
   = note: the type of `T` is chosen by the caller and may be a type that is different from `T`
error: aborting due to 4 previous errors

For more information about this error, try `rustc --explain E0308`.
------------------------------------------
---

12    |
13    = note: expected type parameter `T`
14                    found unit type `()`
-    = note: the type of `T` is chosen by the caller and may be a type that is different than `T`
+    = note: the type of `T` is chosen by the caller and may be a type that is different from `T`
17 error[E0308]: mismatched types
18   --> $DIR/return-impl-trait.rs:23:5

29    |
29    |
30    = note: expected type parameter `T`
31                    found unit type `()`
-    = note: the type of `T` is chosen by the caller and may be a type that is different than `T`
+    = note: the type of `T` is chosen by the caller and may be a type that is different from `T`
34 error: aborting due to 2 previous errors
35 



The actual stderr differed from the expected stderr.
Actual stderr saved to /checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/return/return-impl-trait/return-impl-trait.stderr
To update references, rerun the tests and pass the `--bless` flag
To only update this specific test, also pass `--test-args return/return-impl-trait.rs`

error: 1 errors occurred comparing output.
status: exit status: 1
command: "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2/bin/rustc" "/checkout/tests/ui/return/return-impl-trait.rs" "-Zthreads=1" "--sysroot" "/checkout/obj/build/x86_64-unknown-linux-gnu/stage2" "--target=x86_64-unknown-linux-gnu" "--error-format" "json" "--json" "future-incompat" "-Ccodegen-units=1" "-Zui-testing" "-Zsimulate-remapped-rust-src-base=/rustc/FAKE_PREFIX" "-Ztranslate-remapped-path-to-local-path=no" "-Zdeduplicate-diagnostics=no" "-Cstrip=debuginfo" "--remap-path-prefix=/checkout/tests/ui=fake-test-src-base" "--emit" "metadata" "-C" "prefer-dynamic" "--out-dir" "/checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/return/return-impl-trait" "-A" "unused" "-Crpath" "-Cdebuginfo=0" "-Lnative=/checkout/obj/build/x86_64-unknown-linux-gnu/native/rust-test-helpers" "-L" "/checkout/obj/build/x86_64-unknown-linux-gnu/test/ui/return/return-impl-trait/auxiliary"
stdout: none
error[E0308]: mismatched types
  --> fake-test-src-base/return/return-impl-trait.rs:15:5
   |
   |
LL | fn bar<T: Trait + std::marker::Sync>() -> T
   |        |                                  |
   |        |                                  expected `T` because of return type
Build completed unsuccessfully in 0:12:55
   |        this type parameter                help: consider using an impl return type: `impl Trait + std::marker::Sync + Send`
   |        this type parameter                help: consider using an impl return type: `impl Trait + std::marker::Sync + Send`
...
LL |     () //~ ERROR mismatched types
   |     ^^ expected type parameter `T`, found `()`
   = note: expected type parameter `T`
                   found unit type `()`
                   found unit type `()`
   = note: the type of `T` is chosen by the caller and may be a type that is different from `T`
error[E0308]: mismatched types
  --> fake-test-src-base/return/return-impl-trait.rs:23:5
   |
   |
LL | fn other_bounds<T>() -> T
   |                 |       |
   |                 |       expected `T` because of return type
   |                 |       help: consider using an impl return type: `impl Trait`
   |                 this type parameter
   |                 this type parameter
...
LL |     () //~ ERROR mismatched types
   |     ^^ expected type parameter `T`, found `()`
   = note: expected type parameter `T`
                   found unit type `()`
                   found unit type `()`
   = note: the type of `T` is chosen by the caller and may be a type that is different from `T`
error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0308`.
------------------------------------------

@@ -856,6 +856,9 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
format!("impl {}", all_bounds_str),
Applicability::MaybeIncorrect,
);

let ty::Param(found_ty_as_param) = found.kind() else { return };
err.note(format!("the type of `{}` is chosen by the caller and may be a type that is different from `{}`", expected_ty_as_param.name, found_ty_as_param.name));
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@compiler-errors Here found_ty_as_param should return the parameter found right, or am I understanding something wrong

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You don't need found_ty_as_param -- the found ty might be any type, and you can just render that to string.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

    err.note(format!("the type of `{}` is chosen by the caller and may be a type that is different from `{}` ", expected_ty_as_param.name, found.to_string()));

Converting found to string still does not return the type expected.

@compiler-errors
Copy link
Member

@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 5, 2023
@Dylan-DPC
Copy link
Member

@sladyn98 any updates on this?

@Dylan-DPC
Copy link
Member

Closing this as inactive. Feel free to reöpen this pr or create a new pr if you get the time to work on this. Thanks

@Dylan-DPC Dylan-DPC closed this Oct 31, 2023
@Dylan-DPC Dylan-DPC added S-inactive Status: Inactive and waiting on the author. This is often applied to closed PRs. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Oct 31, 2023
jieyouxu added a commit to jieyouxu/rust that referenced this pull request Mar 8, 2024
…Trait

Revives rust-lang#112088 and rust-lang#104755.

Co-authored-by: sladynnunes <snunes@usc.edu>
Co-authored-by: Thalia Nero <chrisvn00@gmail.com>
jieyouxu added a commit to jieyouxu/rust that referenced this pull request Mar 8, 2024
…Trait

Revives rust-lang#112088 and rust-lang#104755.

Co-authored-by: sladynnunes <snunes@usc.edu>
Co-authored-by: Thalia Nero <chrisvn00@gmail.com>
jieyouxu added a commit to jieyouxu/rust that referenced this pull request Mar 8, 2024
…Trait

Revives rust-lang#112088 and rust-lang#104755.

Co-authored-by: sladynnunes <snunes@usc.edu>
Co-authored-by: Thalia Nero <chrisvn00@gmail.com>
jieyouxu added a commit to jieyouxu/rust that referenced this pull request Mar 8, 2024
…Trait

Revives rust-lang#112088 and rust-lang#104755.

Co-authored-by: sladynnunes <snunes@usc.edu>
Co-authored-by: Thalia Nero <chrisvn00@gmail.com>
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this pull request Mar 22, 2024
Note that the caller chooses a type for type param

```
error[E0308]: mismatched types
  --> $DIR/return-impl-trait.rs:23:5
   |
LL | fn other_bounds<T>() -> T
   |                 -       -
   |                 |       |
   |                 |       expected `T` because of return type
   |                 |       help: consider using an impl return type: `impl Trait`
   |                 expected this type parameter
...
LL |     ()
   |     ^^ expected type parameter `T`, found `()`
   |
   = note: expected type parameter `T`
                   found unit type `()`
   = note: the caller chooses the type of T which can be different from ()
```

Tried to see if "expected this type parameter" can be replaced, but that goes all the way to `rustc_infer` so seems not worth the effort and can affect other diagnostics.

Revives rust-lang#112088 and rust-lang#104755.
rust-timer added a commit to rust-lang-ci/rust that referenced this pull request Mar 23, 2024
Rollup merge of rust-lang#122195 - jieyouxu:impl-return-note, r=fmease

Note that the caller chooses a type for type param

```
error[E0308]: mismatched types
  --> $DIR/return-impl-trait.rs:23:5
   |
LL | fn other_bounds<T>() -> T
   |                 -       -
   |                 |       |
   |                 |       expected `T` because of return type
   |                 |       help: consider using an impl return type: `impl Trait`
   |                 expected this type parameter
...
LL |     ()
   |     ^^ expected type parameter `T`, found `()`
   |
   = note: expected type parameter `T`
                   found unit type `()`
   = note: the caller chooses the type of T which can be different from ()
```

Tried to see if "expected this type parameter" can be replaced, but that goes all the way to `rustc_infer` so seems not worth the effort and can affect other diagnostics.

Revives rust-lang#112088 and rust-lang#104755.
flip1995 pushed a commit to flip1995/rust that referenced this pull request Apr 4, 2024
Note that the caller chooses a type for type param

```
error[E0308]: mismatched types
  --> $DIR/return-impl-trait.rs:23:5
   |
LL | fn other_bounds<T>() -> T
   |                 -       -
   |                 |       |
   |                 |       expected `T` because of return type
   |                 |       help: consider using an impl return type: `impl Trait`
   |                 expected this type parameter
...
LL |     ()
   |     ^^ expected type parameter `T`, found `()`
   |
   = note: expected type parameter `T`
                   found unit type `()`
   = note: the caller chooses the type of T which can be different from ()
```

Tried to see if "expected this type parameter" can be replaced, but that goes all the way to `rustc_infer` so seems not worth the effort and can affect other diagnostics.

Revives rust-lang#112088 and rust-lang#104755.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-inactive Status: Inactive and waiting on the author. This is often applied to closed PRs. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants