Skip to content

fix(spi): take register block pointer via ptr() instead of regs() - #6022

Merged
bugadani merged 2 commits into
mainfrom
nightly-ci
Aug 3, 2026
Merged

fix(spi): take register block pointer via ptr() instead of regs()#6022
bugadani merged 2 commits into
mainfrom
nightly-ci

Conversation

@JurajSadel

Copy link
Copy Markdown
Contributor

rust-lang/rust#160012 now makes const evaluation check the validity of a reference at the point it is created. A MMIO address is an integer with no provenance, so the reference is not dereferenceable and const evaluation now fails with:

    error[E0080]: reference not dereferenceable: reference must be dereferenceable for 244 bytes, but got 0x60024000[noalloc] which is a dangling pointer (it has no provenance)
  Error:    --> /home/runner/work/esp-hal/esp-hal/esp-hal/src/spi/master/low_level/mod.rs:975:37
      |
  975 |                     register_block: crate::peripherals::$peri::regs(),
      |                                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ evaluation of `<peripherals::SPI2<'_> as spi::master::low_level::Instance>::parts::INFO` failed inside this call
      |
  note: inside `peripherals::SPI2::<'_>::regs::<'_>`
     --> /home/runner/work/esp-hal/esp-hal/esp-hal/src/peripherals/mod.rs:162:26
      |
  162 |                   unsafe { &*Self::PTR }
      |                            ^^^^^^^^^^^ the failure occurred here
  ...
  175 | / for_each_peripheral! {
  176 | |     // Define stable peripheral singletons
  177 | |     (@peri_type $(#[$meta:meta])* $name:ident <= $from_pac:tt $interrupts:tt) => {
  178 | |         create_peripheral!( $(#[$meta])* $name <= $from_pac $interrupts);
  ...   |
  277 | |     };
  278 | | }

closes #6020

@JurajSadel JurajSadel added the skip-changelog No changelog modification needed label Aug 2, 2026
#[instability::unstable]
pub const fn regs<'a>() -> &'a <pac::$base as core::ops::Deref>::Target {
pub fn regs<'a>() -> &'a <pac::$base as core::ops::Deref>::Target {
unsafe { &*Self::PTR }

@RalfJung RalfJung Aug 3, 2026

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

FWIW, doing this on a pointer without provenance is Undefined Behavior both in const-eval and at runtime (except if Target is a zero-sized type). Similarly, writing to and reading from a pointer without provenance is UB (except if the read/write is volatile, or zero-sized).

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.

We are aware, but the underlying PAC does the same and until std2rust fixes is, we really don't have a good solution.

@bugadani bugadani mentioned this pull request Aug 3, 2026
@bugadani
bugadani enabled auto-merge August 3, 2026 08:46
@bugadani
bugadani added this pull request to the merge queue Aug 3, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Aug 3, 2026
@bugadani bugadani added the skip-ci-non-code-change Don't trigger CI run. label Aug 3, 2026
@bugadani
bugadani enabled auto-merge August 3, 2026 09:37
@bugadani
bugadani added this pull request to the merge queue Aug 3, 2026
Merged via the queue into main with commit cc277b2 Aug 3, 2026
54 of 56 checks passed
@JurajSadel
JurajSadel deleted the nightly-ci branch August 3, 2026 10:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

skip-changelog No changelog modification needed skip-ci-non-code-change Don't trigger CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Nightly CI Failure

3 participants