Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

What are the divergence rules around const eval?

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

还没有人认领这个 Issue。

评估

难度
4/5
预计耗时
3-5 天
新手友好度
32/100
Issue 类型
文档
描述清晰度
需要澄清
活跃度
停滞
技术栈
rust
领域
documentation

调研方向

从 Rust Reference pull request 2067 添加的 divergence 章节开始,然后重现 const-block 示例,并比较 cargo check 与 cargo build。调查关于 const expressions、divergence propagation 和 semicolon cases 所列出的问题。完成的标准是规则已厘清,并在 Reference 中用清晰的示例记录下来。

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

描述

A-const-eval A-divergence

https://github.com/rust-lang/reference/pull/2067 added a chapter on divergence, but we noted some uncertain behavior around const expressions. I had some specific questions that hopefully have clear answers:

  • Can a const expression have type !?
  • Can a const expression diverge?
  • If it is possible to diverge:
    • Does the divergence of a const expression propagate to its outer block?
    • Are there any differences between different kinds of const expressions (like a static/const items, const contexts, const {} blocks)?

For this purpose, I think we can ignore uninhabited-static which looks like it was not intended.

We struggled a little with examples because there is different behavior between cargo check and cargo build. We were initially assuming that if it passed cargo check it was also passing typechecking, but there might be something more complex going on there.

cc @jackh726


Jack said:

What I'm seeing is actually that const blocks (along with async blocks) is that they do not propagate their divergence because they essentially act as independent function bodies. Of course, divergence can exist inside:

fn f() {
    const {
        panic!();
        let _x = 0;
    }
}

and also:

my best guess is that const eval is just masking typecheck here: we need to figure out the type of the const block, but can never do that because const eval doesn't complete.


Example 1: diverging const block

Considering the following:

fn trailing_const_panic() -> ! {
    const { panic!() } // OK, seems const block has ! type
}
fn non_trailing_statement_const_panic() -> ! {
    const { panic!() };
    // ERROR, expected `!`, found `()`
}

I was confused on this (the semicolon). Jack says:

This has a pretty simple explanation: the former results in an expected type of ! for the const block so the type of the const block is ! - the latter has no expectation because of the semicolon, so the type of the block ! fallbacks to ().

but I don't understand that. My base assumption was that a const {} block would have the same rules and behavior as a normal block. And indeed, if you remove the const from non_trailing_statement_const_panic, it successfully compiles. Why would its type fall back to () if it is const?

Example 2: Read from a place

@traviscross came up with a variation that includes a read from a place, and the semicolon is moved inside the const {} block:

fn read_from_place_with_semicolon() -> ! {
    let _x = (const {
        panic!();
    },).0;
}

fn read_from_place_without_semicolon() -> ! { //~ ERROR: Mismatched types.
    let _x = (const {
        panic!()   // No semicolon
    },).0;
}
主要语言
Rust
星标
1.6k
派生
607
平均合并
6 小时 47 分钟
30 天内合并 PR
14

贡献指南

打开贡献指南

从这里开始

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

rust-lang/reference 的其他 Issue

查看 rust-lang/reference 的全部 Issue

相似的 Issue

更多 Rust Issue

把新 issue 发到你的邮箱

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