Misguiding help on error "mut
must be attached to each individual binding"
#80186
Labels
A-diagnostics
Area: Messages for errors, warnings, and lints
A-patterns
Relating to patterns and pattern matching
A-suggestion-diagnostics
Area: Suggestions generated by the compiler applied by `cargo fix`
C-bug
Category: This is a bug.
D-invalid-suggestion
Diagnostics: A structured suggestion resulting in incorrect code.
T-compiler
Relevant to the compiler team, which will review and decide on the PR/issue.
I tried this code to create a mutable variable binding to
tuple.0
while also dereferencing it in the same pattern. Of course this code is not correct. I think the intention can be guessed because of the order in which&
andmut
appear, especially in the parenthesized variant.This is the correct code, where the tokens
&
andmut
are separated by parentheses to avoid parsing as&mut
. This is also perfectly logical since patterns are read from the outside inwards destructuring the given value. My fault was to interpret&
andmut
like operators which got me stuck at the code I've written above.The issue is that the help output by the compiler isn't actually helping much because it changes semantics of the code. The compiler suggests to use
&mut
which would dereference a mutable reference when used in a pattern. That is very different from dereferencing a shared reference and mutably binding a variable.Since
&mut
consists of two tokens&
andmut
the compiler tries to show you that the two token are in the wrong order I guess. In my opinion this is less probable than trying to express the pattern&(mut variable)
wrongly asmut &variable
. For the variantmut (&variable)
the help is not correct anyway since the compiler ignores the parentheses.I propose to change the help text to suggest
&(mut last)
instead. I'm not sure whether there are cases where this help would be wrong also.Current error and help:
Meta
All the compiler channels produce the same output.
rustc --version --verbose
:Stable:
Beta:
Nightly:
The text was updated successfully, but these errors were encountered: