Skip to content

Suggest defining variable as mutable on &mut _ type mismatch in pats - #98431

Merged
bors merged 3 commits into
rust-lang:masterfrom
WaffleLapkin:mut_pat_suggestions
Jun 25, 2022
Merged

Suggest defining variable as mutable on &mut _ type mismatch in pats#98431
bors merged 3 commits into
rust-lang:masterfrom
WaffleLapkin:mut_pat_suggestions

Conversation

@WaffleLapkin

Copy link
Copy Markdown
Member

Suggest writing mut a where &mut a was written but a non-ref type provided.

Since we still don't have "apply either one of the suggestions but not both" kind of thing, the interaction with the suggestion of removing &[mut] or moving it to the type is weird, and idk how to make it better..

r? @compiler-errors

@rustbotrustbot added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Jun 23, 2022
@rust-highfiverust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jun 23, 2022
@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@compiler-errors

This comment was marked as outdated.


ident_kind.map(|thing| (
pat.span,
format!("to declare a mutable {thing} use `mut variable_name`"),

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I prefer to declare a mutable {thing} use: -- the mut variable_name part is redundant with the suggestion that follows direectly after

@compiler-errorscompiler-errors left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Looks fine after nits

Comment threadcompiler/rustc_typeck/src/check/pat.rs

@compiler-errorscompiler-errorsJun 24, 2022

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

sorry, my suggestion was not complete! you should probably remove this .map since it's no longer Option. I should have been more clear.

@rust-log-analyzer

This comment has been minimized.

WaffleLapkinand others added 2 commits June 24, 2022 21:14
Co-authored-by: Michael Goulet <michael@errs.io>
@compiler-errors

Copy link
Copy Markdown
Contributor

Cool, thanks.

@bors r+ rollup

@bors

bors commented Jun 24, 2022

Copy link
Copy Markdown
Collaborator

📌 Commit 1dfb53b has been approved by compiler-errors

@borsbors 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 24, 2022
@camelid

Copy link
Copy Markdown
Member

Since we still don't have "apply either one of the suggestions but not both" kind of thing, the interaction with the suggestion of removing &[mut] or moving it to the type is weird, and idk how to make it better..

I think that actually has been implemented: span_suggestions()

@compiler-errors

Copy link
Copy Markdown
Contributor

Does span_suggestions() work correctly with Applicability::MachineApplicable?

compiler-errors added a commit to compiler-errors/rust that referenced this pull request Jun 25, 2022
…=compiler-errors
Suggest defining variable as mutable on `&mut _` type mismatch in pats
Suggest writing `mut a` where `&mut a` was written but a non-ref type provided.
Since we still don't have "apply either one of the suggestions but not both" kind of thing, the interaction with the suggestion of removing `&[mut]` or moving it to the type is weird, and idk how to make it better..
r? `@compiler-errors`
@camelid

Copy link
Copy Markdown
Member

Does span_suggestions() work correctly with Applicability::MachineApplicable?

In what regard? I think rustfix won't autoapply the suggestions if you use span_suggestions (since it doesn't know which one you want).

JohnTitor added a commit to JohnTitor/rust that referenced this pull request Jun 25, 2022
…=compiler-errors
Suggest defining variable as mutable on `&mut _` type mismatch in pats
Suggest writing `mut a` where `&mut a` was written but a non-ref type provided.
Since we still don't have "apply either one of the suggestions but not both" kind of thing, the interaction with the suggestion of removing `&[mut]` or moving it to the type is weird, and idk how to make it better..
r? `@compiler-errors`
@JohnTitorJohnTitor mentioned this pull request Jun 25, 2022
bors added a commit to rust-lang-ci/rust that referenced this pull request Jun 25, 2022
…askrgr
Rollup of 9 pull requests
Successful merges:
- rust-lang#96412 (Windows: Iterative `remove_dir_all`)
- rust-lang#98126 (Mitigate MMIO stale data vulnerability)
- rust-lang#98149 (Set relocation_model to Pic on emscripten target)
- rust-lang#98194 (Leak pthread_{mutex,rwlock}_t if it's dropped while locked.)
- rust-lang#98298 (Point to type parameter definition when not finding variant, method and associated item)
- rust-lang#98311 (Reverse folder hierarchy)
- rust-lang#98401 (Add tracking issues to `--extern` option docs.)
- rust-lang#98429 (Use correct substs in enum discriminant cast)
- rust-lang#98431 (Suggest defining variable as mutable on `&mut _` type mismatch in pats)
Failed merges:
r? `@ghost`
`@rustbot` modify labels: rollup
@bors
bors merged commit 1f923c2 into rust-lang:masterJun 25, 2022
@rustbotrustbot added this to the 1.64.0 milestone Jun 25, 2022
@WaffleLapkin
WaffleLapkin deleted the mut_pat_suggestions branch June 26, 2022 10:56
@WaffleLapkin

Copy link
Copy Markdown
MemberAuthor

@camelid rustfix tries to apply all suggestions:

; cargo +stage1 fix --broken-code
Checking t v0.1.0 (/home/waffle/projects/repos/t)
warning: error applying suggestions to `src/main.rs`
The full error message was:
> Cannot replace slice of data that was already replaced
This likely indicates a bug in either rustc or cargo itself,
and we would appreciate a bug report! You're likely to see
a number of compiler warnings after this message which cargo
attempted to fix but failed. If you could open an issue at
https://github.com/rust-lang/rust/issues
quoting the full output of this command we'd be very appreciative!
Note that you may be able to make some more progress in the near-term
fixing code with the `--broken-code` flag
Fixed src/main.rs (0 fixes)
Finished dev [unoptimized + debuginfo] target(s) in 0.15s

Also the output of the diagnostic is not perfect either, since suggestions don't have labels and it's hard to distinguish them:

error[E0308]: mismatched types
--> src/main.rs:1:7
|
1 | fn _f(&mut _a: i32) {}
| ^^^^^^^ --- expected due to this
| |
| expected `i32`, found `&mut _`
|
= note: expected type `i32`
found mutable reference `&mut _`
help: consider removing `&mut` from the pattern or using `mut` to declare variable as mutable
|
1 - fn _f(&mut _a: i32) {}
1 + fn _f(_a: i32) {}
|
1 | fn _f(mut _a: i32) {}
| ~~~

I think to fix this problem we need a different format for suggestions, that would allow for more flexibility. For example marking placeholders as such (so the tooling could allow users to fill them) or having "either one of these suggestions, but not all" kind of thing. I was meaning to try implementing something like this for a while, but haven't yet.

@camelid

Copy link
Copy Markdown
Member

or having "either one of these suggestions, but not all" kind of thing

I believe that's what span_suggestions is supposed to be, but the implementation is non-optimal. In this case, I think you should be able to fix the crash by making the suggestion non-MachineApplicable (see rust-lang/cargo#13030). When @compiler-errors asked in #98431 (comment) about MachineApplicable, I'd forgotten about this issue with rustfix 😅

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-borsStatus: Waiting on bors to run and complete tests. Bors will change the label on completion.T-compilerRelevant 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

@WaffleLapkin@rust-log-analyzer@compiler-errors@bors@camelid@rust-highfive@rustbot