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

未关闭
#166,433 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
5/5
预计耗时
一周以上
新手友好度
14/100
Issue 类型
功能
描述清晰度
描述清楚
活跃度
活跃
技术栈
rust
领域
compilers

调研方向

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.

由索引模型根据 Issue 内容生成。

描述

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.

主要语言
Rust
星标
123k
派生
16.6k
平均合并
2 天 17 小时
30 天内合并 PR
532

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

rust-lang/rust 的其他 Issue

查看 rust-lang/rust 的全部 Issue

相似的 Issue

更多 Rust Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。