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

Note that the caller chooses a type for type param #122195

Merged
merged 1 commit into from Mar 23, 2024

Conversation

jieyouxu
Copy link
Contributor

@jieyouxu jieyouxu commented Mar 8, 2024

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 #112088 and #104755.

@rustbot
Copy link
Collaborator

rustbot commented Mar 8, 2024

r? @fmease

rustbot has assigned @fmease.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@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 Mar 8, 2024
@rust-log-analyzer

This comment has been minimized.

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

err.note(format!(
"the caller chooses the type of {} which can be different from {}",
Copy link
Member

@fmease fmease Mar 8, 2024

Choose a reason for hiding this comment

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

Suggested change
"the caller chooses the type of {} which can be different from {}",
"the caller chooses a type for `{}` which can be different from `{}`",

Backticks & “type for” over “type of”. The former is more precise semantically: T stands for a type and can't be of a type, it's a type itself. Cf. the statement i32 is the type of 0i32.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Make sense, changed the wording to this.

@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 Mar 8, 2024
@fmease

This comment has been minimized.

@jieyouxu
Copy link
Contributor Author

jieyouxu commented Mar 8, 2024

You still need to bless the other tests/ui tests :)

(I think these are literally the only two tests that test this diagnostic)

Copy link
Member

@fmease fmease left a comment

Choose a reason for hiding this comment

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

Thanks! One last thing, could you please squash the commits into one? Then r=me

@fmease
Copy link
Member

fmease commented Mar 8, 2024

@bors rollup

@fmease
Copy link
Member

fmease commented Mar 8, 2024

@bors delegate+

@bors
Copy link
Contributor

bors commented Mar 8, 2024

✌️ @jieyouxu, you can now approve this pull request!

If @fmease told you to "r=me" after making some further change, please make that change, then do @bors r=@fmease

@@ -889,7 +889,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
self.dcx(),
errors::ExpectedReturnTypeLabel::Other { span: hir_ty.span, expected },
);
self.try_suggest_return_impl_trait(err, expected, ty, fn_id);
self.try_suggest_return_impl_trait(err, expected, found, fn_id);
Copy link
Member

@fmease fmease Mar 8, 2024

Choose a reason for hiding this comment

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

Actually I wonder if we should add this note in more cases, not just in cases where we suggest RPITs. For example here:

fn f<T>() -> (T,) {
    (0,)
}

Copy link
Contributor Author

@jieyouxu jieyouxu Mar 8, 2024

Choose a reason for hiding this comment

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

wait how do u undo a r= lol (if you want me to add it in more places in this PR)

Copy link
Member

Choose a reason for hiding this comment

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

Okay, let's do this!

@jieyouxu
Copy link
Contributor Author

jieyouxu commented Mar 8, 2024

@bors r=@fmease

@bors
Copy link
Contributor

bors commented Mar 8, 2024

📌 Commit a29ac16 has been approved by fmease

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Mar 8, 2024
@fmease
Copy link
Member

fmease commented Mar 8, 2024

Like this:
@bors r-
:)

@bors bors 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-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. labels Mar 8, 2024
@jieyouxu jieyouxu force-pushed the impl-return-note branch 2 times, most recently from 0f368e3 to eb7bbe8 Compare March 9, 2024 21:38
@jieyouxu
Copy link
Contributor Author

jieyouxu commented Mar 9, 2024

Changes since last review:

  • I forgor rustc_hir_typeck already uses translatable diagnostics, so made the note into a subdiagnostic instead.
  • Added the note for cases like fn f<T>() -> (T,) { (0,) } as well.

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Mar 9, 2024
@jieyouxu jieyouxu changed the title Note that type param is chosen by caller when suggesting return impl Trait Note that the caller chooses a type for type param Mar 9, 2024
Copy link
Member

Choose a reason for hiding this comment

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

I wonder if we should put this test into tests/ui/return/, too 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Moved the test into tests/ui/return/.

@@ -11,6 +11,7 @@ LL | let x: S = MaybeUninit::uninit();
|
= note: expected type parameter `S`
found union `MaybeUninit<_>`
= note: the caller chooses a type for `S` which can be different from `MaybeUninit<_>`
Copy link
Member

Choose a reason for hiding this comment

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

“caller” isn't accurate here, there's no function. While we could say something like “user of Bug” I'm not really sure we want to emit this note at all. See my other review comment.

Copy link
Contributor Author

@jieyouxu jieyouxu Mar 16, 2024

Choose a reason for hiding this comment

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

Moved the emission so it no longer notes for arbitrary expression coercion.

self.demand_coerce_diag(expr, ty, expected, expected_ty_expr, AllowTwoPhase::No);

if let Some(diag) = diag {
self.note_caller_chooses_ty_for_ty_param(diag, expected, ty);
Copy link
Member

@fmease fmease Mar 16, 2024

Choose a reason for hiding this comment

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

I don't think this is the right place since check_expr_coercible_to_type does not only get run on return expressions but almost anywhere where coercions can happen. See my comment over at file issue-67945-1.full.stderr.

I think you should move the err.subdiagnostic(NoteCallerChoosesTyForTyParam { … }) out of try_suggest_return_impl_trait and into the hir::FnRetTy::Return branch if that makes sense.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Moved the suggestion into the hir::FnRetTy::Return branch.

@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 Mar 16, 2024
@jieyouxu
Copy link
Contributor Author

Changes since last review:

  • Moved test into tests/ui/return/.
  • Moved emission of note to hir::FnRetTy::Return branch to avoid emitting note on arbitrary coercions.

@rust-log-analyzer

This comment has been minimized.

Copy link
Member

@fmease fmease left a comment

Choose a reason for hiding this comment

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

Thanks!

@jieyouxu
Copy link
Contributor Author

jieyouxu commented Mar 16, 2024

(One sec I forgot to bless some tests)
EDIT: Blessed them now.

@@ -14,6 +14,7 @@ LL | u
found type parameter `X`
= note: a type parameter was expected, but a different one was found; you might be missing a type parameter or trait bound
= note: for more information, visit https://doc.rust-lang.org/book/ch10-02-traits.html#traits-as-parameters
= note: the caller chooses a type for `Self` which can be different from `X`
Copy link
Member

Choose a reason for hiding this comment

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

Ah, this one is interesting. The note is technically speaking correct but I don't know if it helps the user. Remember that we add the note “the caller chooses …” because the user might not understand that fn f<T>() -> T doesn't mean “can return anything the callee / the author of the function wants to return”. What do you think? Do you think it's useful?

Copy link
Contributor Author

@jieyouxu jieyouxu Mar 16, 2024

Choose a reason for hiding this comment

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

This note doesn't seem very helpful in this instance, no. This particular case probably needs a separate note suggesting that X might not satisfy X: Sized but Self: Sized or something to that effect. Not entirely sure what help to give here though.

Copy link
Member

Choose a reason for hiding this comment

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

Let's not block this PR on this (given the long history of this diagnostic change with several failed PRs), we should probably not emit the note if the param is kw::SelfUpper but that can be done in a follow up.

Sorry for the delay, I was sitting at my desk wondering why we're not suggesting impl-Trait on fn f<T>() -> (T,) { (0,) } etc. (so, fn f() -> (impl Sized,) { (0,) } which works perfectly). I should probably open an issue for that. If we end up making try_suggest_return_impl_trait smarter at some point we can then probably also move the note back into impl-Trait since it does some other checks, too, which we don't do for note_caller_chooses_ty_for_ty_param.

suggesting that X might not satisfy X: Sized but Self: Sized or something to that effect

Nah, that's not relevant here. The Sized bounds only exist to allow the method to return self.

@rust-log-analyzer

This comment has been minimized.

@rustbot
Copy link
Collaborator

rustbot commented Mar 16, 2024

Some changes occurred in src/tools/clippy

cc @rust-lang/clippy

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

fmease commented Mar 22, 2024

Let's merge this. This should probably be further tweaked over time (like maybe making the heuristics stricter) but I don't want to block this.

@bors r+ rollup

@bors
Copy link
Contributor

bors commented Mar 22, 2024

📌 Commit cacdf92 has been approved by fmease

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Mar 22, 2024
bors added a commit to rust-lang-ci/rust that referenced this pull request Mar 22, 2024
…iaskrgr

Rollup of 8 pull requests

Successful merges:

 - rust-lang#114009 (compiler: allow transmute of ZST arrays with generics)
 - rust-lang#122195 (Note that the caller chooses a type for type param)
 - rust-lang#122651 (Suggest `_` for missing generic arguments in turbofish)
 - rust-lang#122784 (Add `tag_for_variant` query)
 - rust-lang#122839 (Split out `PredicatePolarity` from `ImplPolarity`)
 - rust-lang#122873 (Merge my contributor emails into one using mailmap)
 - rust-lang#122885 (Adjust better spastorino membership to triagebot's adhoc_groups)
 - rust-lang#122888 (add a couple more tests)

r? `@ghost`
`@rustbot` modify labels: rollup
bors added a commit to rust-lang-ci/rust that referenced this pull request Mar 22, 2024
…iaskrgr

Rollup of 8 pull requests

Successful merges:

 - rust-lang#114009 (compiler: allow transmute of ZST arrays with generics)
 - rust-lang#122195 (Note that the caller chooses a type for type param)
 - rust-lang#122651 (Suggest `_` for missing generic arguments in turbofish)
 - rust-lang#122784 (Add `tag_for_variant` query)
 - rust-lang#122839 (Split out `PredicatePolarity` from `ImplPolarity`)
 - rust-lang#122873 (Merge my contributor emails into one using mailmap)
 - rust-lang#122885 (Adjust better spastorino membership to triagebot's adhoc_groups)
 - rust-lang#122888 (add a couple more tests)

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit aa184c5 into rust-lang:master Mar 23, 2024
11 checks passed
@rustbot rustbot added this to the 1.79.0 milestone Mar 23, 2024
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.
@jieyouxu jieyouxu deleted the impl-return-note branch March 23, 2024 00:41
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.
flip1995 pushed a commit to flip1995/rust that referenced this pull request Apr 4, 2024
…iaskrgr

Rollup of 8 pull requests

Successful merges:

 - rust-lang#114009 (compiler: allow transmute of ZST arrays with generics)
 - rust-lang#122195 (Note that the caller chooses a type for type param)
 - rust-lang#122651 (Suggest `_` for missing generic arguments in turbofish)
 - rust-lang#122784 (Add `tag_for_variant` query)
 - rust-lang#122839 (Split out `PredicatePolarity` from `ImplPolarity`)
 - rust-lang#122873 (Merge my contributor emails into one using mailmap)
 - rust-lang#122885 (Adjust better spastorino membership to triagebot's adhoc_groups)
 - rust-lang#122888 (add a couple more tests)

r? `@ghost`
`@rustbot` modify labels: rollup
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. 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.

None yet

5 participants