Skip to content

Conversation

kvverti
Copy link

@kvverti kvverti commented Nov 23, 2022

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

r? rust-lang/diagnostics

@rustbot
Copy link
Collaborator

rustbot commented Nov 23, 2022

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @davidtwco (or someone else) soon.

Please see the contribution instructions for more information.

@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 Nov 23, 2022
@rust-log-analyzer
Copy link
Collaborator

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

Click to see the possible cause of the failure (guessed by this bot)
........................................................................................ 10472/13887
........................................................................................ 10560/13887
........................................................................................ 10648/13887
........................................................................................ 10736/13887
.................................................F...F.................................. 10824/13887
........................................................iiiii...i....i.i................ 11000/13887
........................................................................................ 11088/13887
.......................................................i................................ 11176/13887
.................................................................iiiiii.i..ii..iiiiiiiii 11264/13887
---
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/src/test/ui/return/return-impl-trait-bad.rs" "-Zthreads=1" "--target=x86_64-unknown-linux-gnu" "--error-format" "json" "--json" "future-incompat" "-Ccodegen-units=1" "-Zui-testing" "-Zdeduplicate-diagnostics=no" "-Cstrip=debuginfo" "--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
--- stderr -------------------------------
  --> /checkout/src/test/ui/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
  --> /checkout/src/test/ui/return/return-impl-trait-bad.rs:9:5
Some tests failed in compiletest suite=ui mode=ui host=x86_64-unknown-linux-gnu target=x86_64-unknown-linux-gnu
   |
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
  --> /checkout/src/test/ui/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
  --> /checkout/src/test/ui/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`
   = note: the caller chooses the value of a type parameter

---
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/src/test/ui/return/return-impl-trait.rs" "-Zthreads=1" "--target=x86_64-unknown-linux-gnu" "--error-format" "json" "--json" "future-incompat" "-Ccodegen-units=1" "-Zui-testing" "-Zdeduplicate-diagnostics=no" "-Cstrip=debuginfo" "--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
--- stderr -------------------------------
  --> /checkout/src/test/ui/return/return-impl-trait.rs:15:5
   |
   |
LL | fn bar<T: Trait + std::marker::Sync>() -> T
   |        |                                  |
   |        |                                  expected `T` because of return type
   |        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 `()`
   = note: the caller chooses the value of a type parameter


error[E0308]: mismatched types
  --> /checkout/src/test/ui/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 `()`
   = note: the caller chooses the value of a type parameter

@fmease
Copy link
Member

fmease commented Nov 23, 2022

You need to bless the tests locally (./x.py test <FAILING_TESTS...> --bless) and commit the resulting changes.

@@ -878,6 +878,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
format!("impl {}", all_bounds_str),
Applicability::MaybeIncorrect,
);
err.note("the caller chooses the value of a type parameter");
Copy link
Member

Choose a reason for hiding this comment

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

Maybe something like..

the type of T is chosen by the caller

..would be clearer?

I'm not keen on describing type parameters as having a "value", I think that could lead to confusion.

@apiraino
Copy link
Contributor

apiraino commented Jan 5, 2023

Switching to waiting on author to carry out the changes mentioned in the latest comments. @kvverti Feel free to request a review with @rustbot ready, thanks!

@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 Jan 5, 2023
@JohnCSimon
Copy link
Member

@kvverti

Ping from triage: I'm closing this due to inactivity, Please reopen when you are ready to continue with this.
Note: if you are going to continue please open the PR BEFORE you push to it, else you won't be able to reopen - this is a quirk of github.
Thanks for your contribution.

@rustbot label: +S-inactive

@JohnCSimon JohnCSimon closed this Mar 17, 2023
@rustbot rustbot added the S-inactive Status: Inactive and waiting on the author. This is often applied to closed PRs. label Mar 17, 2023
jieyouxu added a commit to jieyouxu/rust that referenced this pull request Mar 8, 2024
jieyouxu added a commit to jieyouxu/rust that referenced this pull request Mar 8, 2024
jieyouxu added a commit to jieyouxu/rust that referenced this pull request Mar 8, 2024
jieyouxu added a commit to jieyouxu/rust that referenced this pull request Mar 8, 2024
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. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. 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.

7 participants