Skip to content

Helpful suggestions for incorrect address-of mutability - #160897

Closed
TirushOne wants to merge 1 commit into
rust-lang:mainfrom
TirushOne:mut-raw-ptr-suggestions
Closed

TirushOne wants to merge 1 commit into
rust-lang:mainfrom
TirushOne:mut-raw-ptr-suggestions

Conversation

@TirushOne

@TirushOne TirushOne commented Aug 11, 2026 •

Copy link
Copy Markdown
Contributor

Added suggestions for & and &raw expressions used as function arguments when the function expects a mutable address.

I also did a small refactor, grouping all 'suggest mut' code into a single function called suggest_addr_mut, and removed suggest_ptr_null_mut. Its logic was moved into the new suggest_addr_mut.

Fixes #159490.

Example:

fn main() {
    let s = String::new();
    takes_raw(&raw const s);
    takes_ref(&s);
}

fn takes_raw(_: *mut String) {}
fn takes_ref(_: &mut String) {}

Diagnostics before:

error[E0308]: mismatched types
  |
3 |     takes_raw(&raw const s);
  |     --------- ^^^^^^^^^^^^ types differ in mutability
  |     |
  |     arguments to this function are incorrect
  |
  = note: expected raw pointer `*mut String`
             found raw pointer `*const String`
note: function defined here
 -->..\src\main.rs:7:4
  |
7 | fn takes_raw(_: *mut String) {}
  |    ^^^^^^^^^ --------------

error[E0308]: mismatched types
 --> ..\src\main.rs:4:15
  |
4 |     takes_ref(&s);
  |     --------- ^^ types differ in mutability
  |     |
  |     arguments to this function are incorrect
  |
  = note: expected mutable reference `&mut String`
                     found reference `&String`
note: function defined here
 --> ..\src\main.rs:8:4
  |
8 | fn takes_ref(_: &mut String) {}
  |    ^^^^^^^^^ --------------

Diagnostics after:

error[E0308]: mismatched types                                                                                                                        
 --> ..\src\main.rs:3:15
  |
3 |     takes_raw(&raw const s);
  |     --------- ^^^^^^^^^^^^ types differ in mutability
  |     |
  |     arguments to this function are incorrect
  |
  = note: expected raw pointer `*mut String`
             found raw pointer `*const String`
note: function defined here
 --> ..\src\main.rs:7:4
  |
7 | fn takes_raw(_: *mut String) {}
  |    ^^^^^^^^^ --------------
help: consider using `&raw mut` instead
  |
3 -     takes_raw(&raw const s);
3 +     takes_raw(&raw mut s);
  |

error[E0308]: mismatched types                                                                                                                        
 --> ..\src\main.rs:4:15
  |
4 |     takes_ref(&s);
  |     --------- ^^ types differ in mutability
  |     |
  |     arguments to this function are incorrect
  |
  = note: expected mutable reference `&mut String`
                     found reference `&String`
note: function defined here
 --> ..\src\main.rs:8:4
  |
8 | fn takes_ref(_: &mut String) {}
  |    ^^^^^^^^^ --------------
help: consider using `&mut` instead
  |
4 |     takes_ref(&mut s);
  |                +++

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Aug 11, 2026
@rustbot

rustbot commented Aug 11, 2026

Copy link
Copy Markdown
Collaborator

r? @petrochenkov

rustbot has assigned @petrochenkov.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

Why was this reviewer chosen?

The reviewer was selected based on:

  • Owners of files modified in this PR: compiler
  • compiler expanded to 75 candidates
  • Random selection from 18 candidates

@rust-log-analyzer

This comment has been minimized.

@TirushOne
TirushOne marked this pull request as draft August 12, 2026 02:29
@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Aug 12, 2026
@rust-log-analyzer

This comment has been minimized.

@petrochenkov
petrochenkov marked this pull request as ready for review August 16, 2026 14:24
@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Aug 16, 2026
@petrochenkov

Copy link
Copy Markdown
Contributor

Could you add a test case for &raw const -> &raw mut as well?
@rustbot author

@rustbot rustbot added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Aug 18, 2026
@rustbot

rustbot commented Aug 18, 2026

Copy link
Copy Markdown
Collaborator

Reminder, once the PR becomes ready for a review, use @rustbot ready.

@chenyukang

Copy link
Copy Markdown
Member

This code:

fn takes_raw(_: *mut String) {}

fn main() {
    let mut value = String::new();
    takes_raw(&value);
}

we can also suggest &mut here, but seems this PR missed it.

also please git squash the commits.

@rustbot

rustbot commented Sep 1, 2026

Copy link
Copy Markdown
Collaborator

This PR was rebased onto a different main commit. Here's a range-diff highlighting what actually changed.

Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers.

@rustbot

This comment has been minimized.

@rustbot rustbot added the has-merge-commits PR has merge commits, merge with caution. label Sep 1, 2026
@TirushOne
TirushOne force-pushed the mut-raw-ptr-suggestions branch from 44edf43 to a5bbf02 Compare September 1, 2026 05:11
@rustbot rustbot removed has-merge-commits PR has merge commits, merge with caution. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Sep 1, 2026
@TirushOne

Copy link
Copy Markdown
Contributor Author

I made a dogs breakfast out of the git history for this PR, instead of untangling it I created a new PR with the suggested changes and assigned you to it @petrochenkov. You can find it here as #162106.

@TirushOne TirushOne closed this Sep 1, 2026
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Oct 9, 2026
…ons, r=jackh726

Helpful suggestions for incorrect address-of mutability (2)

Added suggestions for & and &raw expressions used as function arguments when the function expects a mutable address. There is now also a suggestion for providing a &raw const/mut when a reference was expected, as well suggestions when & was used but the function expected *mut.

I also did a small refactor, grouping all 'suggest mut' code into a single function called suggest_addr_mut, and removed suggest_ptr_null_mut. Its logic was moved into the new suggest_addr_mut.

The test at tests\ui\span\coerce-suggestions.rs was updated to trigger all new diagnostics and blessed accordingly.

Fixes: rust-lang#159490.

This PR is mostly a clean-up of rust-lang#160897, with an extra diagnostics thrown in at the suggestion of chenyukang.

r? petrochenkov
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Oct 9, 2026
…ons, r=jackh726

Helpful suggestions for incorrect address-of mutability (2)

Added suggestions for & and &raw expressions used as function arguments when the function expects a mutable address. There is now also a suggestion for providing a &raw const/mut when a reference was expected, as well suggestions when & was used but the function expected *mut.

I also did a small refactor, grouping all 'suggest mut' code into a single function called suggest_addr_mut, and removed suggest_ptr_null_mut. Its logic was moved into the new suggest_addr_mut.

The test at tests\ui\span\coerce-suggestions.rs was updated to trigger all new diagnostics and blessed accordingly.

Fixes: rust-lang#159490.

This PR is mostly a clean-up of rust-lang#160897, with an extra diagnostics thrown in at the suggestion of chenyukang.

r? petrochenkov
JonathanBrouwer added a commit to JonathanBrouwer/rust that referenced this pull request Oct 9, 2026
…ons, r=jackh726

Helpful suggestions for incorrect address-of mutability (2)

Added suggestions for & and &raw expressions used as function arguments when the function expects a mutable address. There is now also a suggestion for providing a &raw const/mut when a reference was expected, as well suggestions when & was used but the function expected *mut.

I also did a small refactor, grouping all 'suggest mut' code into a single function called suggest_addr_mut, and removed suggest_ptr_null_mut. Its logic was moved into the new suggest_addr_mut.

The test at tests\ui\span\coerce-suggestions.rs was updated to trigger all new diagnostics and blessed accordingly.

Fixes: rust-lang#159490.

This PR is mostly a clean-up of rust-lang#160897, with an extra diagnostics thrown in at the suggestion of chenyukang.

r? petrochenkov
rust-bors Bot pushed a commit that referenced this pull request Oct 9, 2026
Rollup merge of #162106 - TirushOne:addr-mutability-suggestions, r=jackh726

Helpful suggestions for incorrect address-of mutability (2)

Added suggestions for & and &raw expressions used as function arguments when the function expects a mutable address. There is now also a suggestion for providing a &raw const/mut when a reference was expected, as well suggestions when & was used but the function expected *mut.

I also did a small refactor, grouping all 'suggest mut' code into a single function called suggest_addr_mut, and removed suggest_ptr_null_mut. Its logic was moved into the new suggest_addr_mut.

The test at tests\ui\span\coerce-suggestions.rs was updated to trigger all new diagnostics and blessed accordingly.

Fixes: #159490.

This PR is mostly a clean-up of #160897, with an extra diagnostics thrown in at the suggestion of chenyukang.

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

Labels

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.

Suggest changing argument mutability

5 participants