Skip to content

Helpful suggestions for incorrect address-of mutability - #160897

Draft
TirushOne wants to merge 5 commits into
rust-lang:mainfrom
TirushOne:mut-raw-ptr-suggestions
Draft

Helpful suggestions for incorrect address-of mutability#160897
TirushOne wants to merge 5 commits 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 issue #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);
  |                +++

…he user provided `&raw const T`. Bundled this new logic along with similar logic for `&T` and `core::ptr::null()`, into a single function `suggest_mut_addr`.
@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.

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

Labels

S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. 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.

4 participants