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

Abierto
#166,433 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
5/5
Tiempo estimado
Más de una semana
Aptitud para principiantes
14/100
Tipo de issue
Nueva funcionalidad
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
rust
Área
compilers

Línea de trabajo

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.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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.

Lenguaje dominante
Rust
Estrellas
123k
Forks
16.6k
Merge medio
2 d 17 h
PR fusionados (30 d)
532

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de rust-lang/rust

Todos los issues de rust-lang/rust

Issues similares

Más issues de Rust

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.