Pointer shared by all enum variants loses `noalias`, `align` and `dereferenceable` attributes

Aperta
#166,433 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
5/5
Tempo stimato
Più di una settimana
Idoneità per principianti
14/100
Tipo di issue
Funzionalità
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
rust
Ambito
compilers

Direzione di ricerca

This is compiler-internals work: two places must agree, the enum layout in rustc_abi (where the shared field offset and pointee type are derived) and the call-ABI attribute computation in rustc_codegen_llvm, which today only special-cases a null niche like Option<&mut i32>. Start by reproducing the two -Copt-level=3 -Cno-prepopulate-passes IR dumps in the issue and diffing both_mut against option_mut, then read how the reference attributes (noalias, dereferenceable, align, readonly) are attached for a plain reference. Done means each row of the issue's table emits the listed attributes, checked by a codegen test under tests/codegen.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

A-codegen A-enum A-LLVM C-optimization T-compiler

When every variant of an enum stores a reference at the same offset, rustc emits that pointer to LLVM as an opaque ptr, without the attributes it would get on its own or inside Option.

#![crate_type = "lib"]

pub enum BothMut<'a> {
    A(&'a mut i32),
    B(&'a mut i32),
}

#[no_mangle]
pub fn both_mut(x: BothMut<'_>) {}

#[no_mangle]
pub fn option_mut(x: Option<&mut i32>) {}

With rustc 1.102.0-nightly (9c5648833 2026-11-10) and -Copt-level=3 -Cno-prepopulate-passes on aarch64-apple-darwin:

define void @both_mut(i64 noundef range(i64 0, 2) %x.0, ptr noundef %x.1)
define void @option_mut(ptr noalias nofree noundef align 4 dereferenceable_or_null(4) %x)

The second field of BothMut is a &mut i32 whichever variant is active, so it could be marked noalias nofree noundef align 4 dereferenceable_or_null(4), just like Option<&mut i32>. The same goes for shared references, which could also get readonly. Loads of the pointer miss !align metadata too:

#[derive(Clone, Copy)]
pub enum BothShared<'a> {
    A(&'a i32),
    B(&'a i32),
}

#[no_mangle]
pub fn load_both_shared<'a>(x: &BothShared<'a>) -> BothShared<'a> {
    *x
}
  %_0.1 = load ptr, ptr %0, align 8, !noundef !5

where !align !{i64 4} could be added.

Expected behaviour

If every inhabited variant has a pointer at the offset, the enum should get what all of them agree on:

  • the smaller pointee size and alignment;
  • the shared pointer kind, e.g. two &mut T stay &mut T, Box<T> and &mut T meet at Box<T>, while &T and &mut T have nothing in common beyond size and alignment;
  • nothing if any inhabited variant has a non-pointer, or nothing, at that offset.

Uninhabited variants can be ignored, and a null niche variant (as today) is compatible with any pointer.

Type Today Could be
enum { A(&mut i32), B(&mut i32) } noundef noalias nofree noundef align 4 dereferenceable_or_null(4)
enum { A(&i32), B(&i32) } noundef noalias nofree noundef readonly align 4 captures(address, read_provenance) dereferenceable_or_null(4)
enum { A(&mut u32), B(&mut [u64; 4]) } noundef noalias nofree noundef align 4 dereferenceable_or_null(4)
enum { A(&mut i32), B(&i32) } noundef noundef align 4 dereferenceable_or_null(4)
enum { A(Box<i32>), B(&mut i32) } noundef noalias noundef align 4 dereferenceable_or_null(4)
enum { A(&mut i32), B(*mut i32) } noundef noundef
enum { A(&i32), B(usize) } noundef noundef
enum { A(&mut i32), B(u64, Infallible) } noundef noalias nofree noundef align 4 dereferenceable_or_null(4)
enum { A(&mut i32), B, C(Infallible) } noundef noalias nofree noundef align 4 dereferenceable_or_null(4)

The last one is passed as a single pointer, like Option<&mut i32>, but misses out because it has three variants.

Lingua principale
Rust
Stelle
123k
Fork
16.6k
Merge medio
2g 17h
PR unite (30g)
532

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di rust-lang/rust

Tutte le issue di rust-lang/rust

Issue simili

Altre issue su Rust

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.