rust-lang / rust-lang/rust

`raw-borrows-via-references` lint causes syntax error

Open
#161,693 1 comment 0 reactions 1 assignee View on GitHub

@chenyukang is already working on this.

Since Aug 28, 2026.

A-diagnostics A-suggestion-diagnostics C-bug D-invalid-suggestion D-papercut needs-triage T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Using the following flags

--force-warn raw-borrows-via-references

this code:

//@ check-pass
#![feature(const_volatile)]

const _READ: () = unsafe {
    let x = 42i32;
    let y = (&x as *const i32).read_volatile();
    assert!(x == y);
};

const _WRITE: () = unsafe {
    let mut x = 42i32;
    (&mut x as *mut i32).write_volatile(13);
    assert!(x == 13);
};

fn main() {}

caused the following diagnostics:

    Checking _volatile v0.1.0 (/tmp/icemaker_global_tempdir.e4pJywawOZz0/icemaker_clippyfix_tempdir.8L3d64fmzMwO/_volatile)
warning: creating an intermediate reference implies aliasing requirements even when immediately cast to a raw pointers
 --> src/main.rs:6:13
  |
6 |     let y = (&x as *const i32).read_volatile();
  |             ^^^^^^^^^^^^^^^^^^
  |
  = note: requested on the command line with `--force-warn raw-borrows-via-references`
help: consider using `&raw const` for a safer and more explicit raw pointer
  |
6 -     let y = (&x as *const i32).read_volatile();
6 +     let y = &raw const x).read_volatile();
  |

warning: creating an intermediate reference implies aliasing requirements even when immediately cast to a raw pointers
  --> src/main.rs:12:5
   |
12 |     (&mut x as *mut i32).write_volatile(13);
   |     ^^^^^^^^^^^^^^^^^^^^
   |
help: consider using `&raw mut` for a safer and more explicit raw pointer
   |
12 -     (&mut x as *mut i32).write_volatile(13);
12 +     &raw mut x).write_volatile(13);
   |

warning: `_volatile` (bin "_volatile") generated 2 warnings (run `cargo clippy --fix --bin "_volatile" -p _volatile -- -Aclippy::all -Awarnings --force-warn raw-borrows-via-references --cap-lints warn` to apply 2 suggestions)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.21s

However after applying these diagnostics, the resulting code:

//@ check-pass
#![feature(const_volatile)]

const _READ: () = unsafe {
    let x = 42i32;
    let y = &raw const x).read_volatile();
    assert!(x == y);
};

const _WRITE: () = unsafe {
    let mut x = 42i32;
    &raw mut x).write_volatile(13);
    assert!(x == 13);
};

fn main() {}

no longer compiled:

    Checking _volatile v0.1.0 (/tmp/icemaker_global_tempdir.e4pJywawOZz0/icemaker_clippyfix_tempdir.8L3d64fmzMwO/_volatile)
error: unexpected closing delimiter: `}`
 --> src/main.rs:8:1
  |
4 | const _READ: () = unsafe {
  |                          - the nearest open delimiter
5 |     let x = 42i32;
6 |     let y = &raw const x).read_volatile();
  |                         - missing open `(` for this delimiter
7 |     assert!(x == y);
8 | };
  | ^ unexpected closing delimiter

error: could not compile `_volatile` (bin "_volatile") due to 1 previous error
warning: build failed, waiting for other jobs to finish...
error: could not compile `_volatile` (bin "_volatile" test) due to 1 previous error

Version:

rustc 1.100.0-nightly (347a1dbf4 2026-08-24)
binary: rustc
commit-hash: 347a1dbf428ab92f284e3afafacfa7eb46341a5d
commit-date: 2026-08-24
host: x86_64-unknown-linux-gnu
release: 1.100.0-nightly
LLVM version: 23.1.0

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.