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

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

Merged
merged 3 commits into from
Jun 25, 2022

Conversation

WaffleLapkin
Copy link
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

@rustbot rustbot added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Jun 23, 2022
@rust-highfive rust-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
Member

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

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.

Looks fine after nits

compiler/rustc_typeck/src/check/pat.rs Outdated Show resolved Hide resolved
},

// Don't provide suggestions in other cases
_ => None,
_ => break 'block None,
};

ident_kind.map(|thing| (
Copy link
Member

@compiler-errors compiler-errors Jun 24, 2022

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.

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

Cool, thanks.

@bors r+ rollup

@bors
Copy link
Contributor

bors commented Jun 24, 2022

📌 Commit 1dfb53b 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 24, 2022
@camelid
Copy link
Member

camelid commented Jun 24, 2022

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
Member

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
Member

camelid commented Jun 25, 2022

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`
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:master Jun 25, 2022
@rustbot rustbot 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
Member Author

@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
Member

camelid commented Jun 29, 2022

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 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

7 participants