Inline assembly lifetime extension / statement

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

维护者通常 1 天内回复

还没有人认领这个 Issue。

评估

难度
3/5
预计耗时
1-2 天
新手友好度
38/100
Issue 类型
文档
描述清晰度
基本清楚
活跃度
停滞
技术栈
rust
领域
documentation

调研方向

先从 Rust Reference 的内联汇编部分开始,然后查看链接的 URLO 讨论和 Godbolt 示例。确定语句边界和临时值生命周期延长如何应用于 asm! 操作数,并找出需要记录的确切行为。当 Reference 清楚地解释临时值相对于汇编执行何时被丢弃时,即表示完成。

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

描述

A-asm

This began as this URLO thread: https://users.rust-lang.org/t/end-of-statement-lifetime-in-inline-assembly-blocks/133373

I have have code like:

    pub fn macro_generated_function(location: &std::panic::Location) {
        unsafe {
            std::arch::asm!(
                "... {0} ...",
                in (reg) (std::ffi::CString::new(location.file()).unwrap().as_ptr()) as isize,
                options(readonly, preserves_flags, nostack, att_syntax)
            )
        };
    }

This results in a warning:

warning: a dangling pointer will be produced because the temporary `CString` will be dropped
   --> src/lib.rs:498:76
    |
498 |                 in (reg) (std::ffi::CString::new(location.file()).unwrap().as_ptr()) as isize,
    |                           ------------------------------------------------ ^^^^^^ this pointer will immediately be invalid
    |                           |
    |                           this `CString` is deallocated at the end of the statement, bind it to a variable to extend its lifetime
    |
    = note: pointers do not have a lifetime; when calling `as_ptr` the `CString` will be deallocated at the end of the statement because nothing is referencing it as far as the type system is concerned
    = help: you must make sure that the variable you bind the `CString` to lives at least as long as the pointer returned by the call to `as_ptr`
    = help: in particular, if this pointer is returned from the current function, binding the `CString` inside the function will not suffice
    = help: for more information, see <https://doc.rust-lang.org/reference/destructors.html>
    = note: `#[warn(dangling_pointers_from_temporaries)]` on by default

However, it isn't clear from the reference what the "statement" is when it comes to a macro like asm!. Is the statement the whole asm!() (in which case it is fine for my use case), or does the lifetime end before the assembly code is executed? Wouldn't lifetime extension extend this lifetime to the end of the asm!()?

"Normal" macros can after all expand to multiple statements, and it is unclear what the semantics are around a built in macro like asm!. The code does work in practise (the destructor appears to be called after the inline assembly, see godbolt) but I would like this to be properly documented (presumably at https://doc.rust-lang.org/reference/inline-assembly.html)

主要语言
Rust
星标
1.6k
派生
618
平均合并
5 小时 48 分钟
30 天内合并 PR
3

环境准备

  • 没有 Dockerfile 或 Docker Compose 文件
  • 没有 Pull Request 模板
  • 阅读贡献指南

从这里开始

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

rust-lang/reference 的其他 Issue

查看 rust-lang/reference 的全部 Issue

相似的 Issue

更多 Rust Issue

把新 issue 发到你的邮箱

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