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

Fix suggestions for &a: T parameters #97964

Merged
merged 5 commits into from
Jun 16, 2022

Conversation

WaffleLapkin
Copy link
Member

I've accidentally discovered that we have broken suggestions for &a: T parameters:

fn f(&mut bar: u32) {}

fn main() {
    let _ = |&mut a| ();
}
error[E0308]: mismatched types
 --> ./t.rs:1:6
  |
1 | fn f(&mut bar: u32) {}
  |      ^^^^^^^^-----
  |      |         |
  |      |         expected due to this
  |      expected `u32`, found `&mut _`
  |      help: did you mean `bar`: `&u32`
  |
  = note:           expected type `u32`
          found mutable reference `&mut _`

error[E0308]: mismatched types
 --> ./t.rs:4:23
  |
4 |     let _: fn(u32) = |&mut a| ();
  |                       ^^^^^--
  |                       |    |
  |                       |    expected due to this
  |                       expected `u32`, found `&mut _`
  |                       help: did you mean `a`: `&u32`
  |
  = note:           expected type `u32`
          found mutable reference `&mut _`

It's hard to see, but

  1. The help span is overlapping with "expected" spans
  2. It suggests fn f( &u32) {} (no mut and lost parameter name) and |&u32 () (no closing | and lost parameter name)

I've tried to fix this.

r? @compiler-errors

Previously we were suggesting stuff like `fn f( &u32) {}`
@rustbot rustbot added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Jun 10, 2022
@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jun 10, 2022
@rust-log-analyzer

This comment has been minimized.

Copy link
Member

@compiler-errors compiler-errors left a comment

Choose a reason for hiding this comment

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

I left some general comments, but thanks @WaffleLapkin for such thorough UI tests.

inner: &Pat<'_>,
expected: Ty<'tcx>,
) {
fn borrow_pat_suggestion(&self, err: &mut Diagnostic, pat: &Pat<'_>, inner: &Pat<'_>) {
Copy link
Member

Choose a reason for hiding this comment

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

What is pat and inner in this case? Is pat the (direct?) parent of inner always?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes. pat is Ref(inner), see

PatKind::Ref(inner, mutbl) => {
self.check_pat_ref(pat, inner, mutbl, expected, def_bm, ti)

self.borrow_pat_suggestion(&mut err, pat, inner);

Copy link
Member

Choose a reason for hiding this comment

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

Huh, could we simplify the redundant parameters then, if you think that would be simpler? Or if not, could you leave a comment that explains this relationship.

Copy link
Member Author

Choose a reason for hiding this comment

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

What we really want is to have pat: Pat<PatKind::Ref> or something, but we don't have variant types so everything is messy :(

In the end I've removed inner (we need to match anyway, so that should be fine) and added comments about preconditions

src/test/ui/mismatched_types/issue-38371.stderr Outdated Show resolved Hide resolved
compiler/rustc_typeck/src/check/pat.rs Outdated Show resolved Hide resolved
compiler/rustc_typeck/src/check/pat.rs Show resolved Hide resolved
compiler/rustc_typeck/src/check/pat.rs Show resolved Hide resolved
@rust-log-analyzer

This comment has been minimized.

@compiler-errors
Copy link
Member

Thanks @WaffleLapkin, this is definitely an great improvement on the diagnostic.

@bors r+ rollup

@bors
Copy link
Contributor

bors commented Jun 15, 2022

📌 Commit 33ccd76 has been approved by compiler-errors

@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 Jun 15, 2022
GuillaumeGomez added a commit to GuillaumeGomez/rust that referenced this pull request Jun 15, 2022
…ions, r=compiler-errors

Fix suggestions for `&a: T` parameters

I've accidentally discovered that we have broken suggestions for `&a: T` parameters:
```rust
fn f(&mut bar: u32) {}

fn main() {
    let _ = |&mut a| ();
}
```
```text
error[E0308]: mismatched types
 --> ./t.rs:1:6
  |
1 | fn f(&mut bar: u32) {}
  |      ^^^^^^^^-----
  |      |         |
  |      |         expected due to this
  |      expected `u32`, found `&mut _`
  |      help: did you mean `bar`: `&u32`
  |
  = note:           expected type `u32`
          found mutable reference `&mut _`

error[E0308]: mismatched types
 --> ./t.rs:4:23
  |
4 |     let _: fn(u32) = |&mut a| ();
  |                       ^^^^^--
  |                       |    |
  |                       |    expected due to this
  |                       expected `u32`, found `&mut _`
  |                       help: did you mean `a`: `&u32`
  |
  = note:           expected type `u32`
          found mutable reference `&mut _`
```

It's hard to see, but
1. The help span is overlapping with "expected" spans
2. It suggests `fn f( &u32) {}` (no `mut` and lost parameter name) and `|&u32 ()` (no closing `|` and lost parameter name)

I've tried to fix this.

r? `@compiler-errors`
bors added a commit to rust-lang-ci/rust that referenced this pull request Jun 15, 2022
Rollup of 7 pull requests

Successful merges:

 - rust-lang#97202 (os str capacity documentation)
 - rust-lang#97964 (Fix suggestions for `&a: T` parameters)
 - rust-lang#98053 (Fix generic impl rustdoc json output)
 - rust-lang#98059 (Inline `const_eval_select`)
 - rust-lang#98092 (Fix sidebar items expand collapse)
 - rust-lang#98119 (Refactor path segment parameter error)
 - rust-lang#98135 (Add regression test for rust-lang#93775)

Failed merges:

r? `@ghost`
`@rustbot` modify labels: rollup
@bors bors merged commit 52afa3a into rust-lang:master Jun 16, 2022
@rustbot rustbot added this to the 1.63.0 milestone Jun 16, 2022
@WaffleLapkin WaffleLapkin deleted the fix_borrow_par_suggestions branch June 19, 2022 09:11
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.

7 participants