Surprising behavior of aliasing model: references that are transmuted into existence might not have the expected properties

未关闭
#593 29 条评论 1 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

评估

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

调研方向

The issue provides no repository file or test entry point. Start with the Rust example and its discussion of Stacked Borrows and Tree Borrows; the work would need to establish an explicit decision about references copied with ptr::copy_nonoverlapping and document the outcome.

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

描述

A-aliasing-model

This code does not have UB under Stacked Borrows and Tree Borrows:

fn main() {
    let mut data = 1;
    let x = &mut data;
    let mut y = &mut 2;
    unsafe { std::ptr::copy_nonoverlapping(&raw const x, &raw mut y, 1); }
    *x = 3;
    *y = 4;
    *x = 5;
    *y = 6;
}

This is expected behavior. The copy_nonoverlapping makes y a perfect copy of x, those two just contain the same pointer. Mutable references are unique in the sense that no "other" pointer is allowed to be used while they are live -- but here, there's no "other" pointer, it's twice the same pointer.

Within the framework that SB and TB are built in, I don't see any way to avoid that. I don't think *x = 3 should mutate x itself, so it follows that x and y just are and remain the same pointer.

I am filing this issue mostly to remind us that when we move towards making decisions around the aliasing model, we should explicitly discuss this case. But maybe someone has an idea for how a model could make this UB?

主要语言
没有语言数据
星标
760
派生
68
PR 合并指标
30 天内没有已合并 PR

环境准备

这个项目没有提供开发容器、Dockerfile 或贡献指南,环境需要你自己搭建:先看它的 README,通用步骤见我们的新手贡献指南。

从这里开始

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

rust-lang/unsafe-code-guidelines 的其他 Issue

查看 rust-lang/unsafe-code-guidelines 的全部 Issue

相似的 Issue

更多 Compilers Issue

把新 issue 发到你的邮箱

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