Fix suggestions for &a: T parameters#97964
Conversation
Previously we were suggesting stuff like `fn f( &u32) {}`
This comment has been minimized.
This comment has been minimized.
compiler-errors
left a comment
There was a problem hiding this comment.
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<'_>) { |
There was a problem hiding this comment.
What is pat and inner in this case? Is pat the (direct?) parent of inner always?
There was a problem hiding this comment.
Yes. pat is Ref(inner), see
rust/compiler/rustc_typeck/src/check/pat.rs
Lines 201 to 202 in e9d49b2
rust/compiler/rustc_typeck/src/check/pat.rs
Line 1871 in e9d49b2
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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
This comment has been minimized.
This comment has been minimized.
f19b8e8 to
33ccd76
Compare
|
Thanks @WaffleLapkin, this is definitely an great improvement on the diagnostic. @bors r+ rollup |
|
📌 Commit 33ccd76 has been approved by |
…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`
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
I've accidentally discovered that we have broken suggestions for
&a: Tparameters:It's hard to see, but
fn f( &u32) {}(nomutand lost parameter name) and|&u32 ()(no closing|and lost parameter name)I've tried to fix this.
r? @compiler-errors