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
@MabezDev MabezDev added the esp-hal-backport Backport this PR to the latest esp-hal-x.y.x branch.” label Aug 4, 2026
@github-actions

github-actions Bot commented Aug 4, 2026

Copy link
Copy Markdown

Created backport PR for esp-hal-1.1.x:

Please cherry-pick the changes locally and resolve any conflicts.

git fetch origin backport-6022-to-esp-hal-1.1.x
git worktree add --checkout .worktree/backport-6022-to-esp-hal-1.1.x backport-6022-to-esp-hal-1.1.x
cd .worktree/backport-6022-to-esp-hal-1.1.x
git reset --hard HEAD^
git cherry-pick -x cc277b29c9ef4f15a2fc69aeee67c53e676eb189
git push --force-with-lease

MabezDev added a commit that referenced this pull request Aug 5, 2026
Manual backport of #6022 to esp-hal-1.1.x.

rust-lang/rust#160012 made const evaluation check that a reference is
dereferenceable at the point it is created. An MMIO address has no
provenance, so initialising `Info` in a `static` through `regs()` no
longer compiles:

    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)

Take the raw pointer via `ptr()` instead, and drop `const` from
`regs()` so it cannot be called from const context again.

The upstream PR also carries a `cargo fmt` commit that reflows around
79 unrelated files with a newer rustfmt. That commit is deliberately
omitted here.
MabezDev added a commit that referenced this pull request Aug 5, 2026
)

Manual backport of #6022 to esp-hal-1.1.x.

rust-lang/rust#160012 made const evaluation check that a reference is
dereferenceable at the point it is created. An MMIO address has no
provenance, so initialising `Info` in a `static` through `regs()` no
longer compiles:

    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)

Take the raw pointer via `ptr()` instead, and drop `const` from
`regs()` so it cannot be called from const context again.

The upstream PR also carries a `cargo fmt` commit that reflows around
79 unrelated files with a newer rustfmt. That commit is deliberately
omitted here.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

esp-hal-backport Backport this PR to the latest esp-hal-x.y.x branch.” 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

4 participants