Skip to content

what is the type of block expression that contains a diverging expression #1738

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
xmh0511 opened this issue Feb 20, 2025 · 1 comment
Closed

Comments

@xmh0511
Copy link

xmh0511 commented Feb 20, 2025

Consider this example:

fn show()->std::io::Result<()>{
    let r = panic!();
    let i = 0;
}

According [items.fn.body], the function's body is conceptually rewritten as

return {
       let r = panic!();
       let i = 0;
};

However, https://doc.rust-lang.org/stable/reference/expressions/block-expr.html#block-expressions says:

The type of a block is the type of the final operand, or () if the final operand is omitted.

In this case, the final operand is (). However, the code could be compiled only if the block type is a never type !`, which could be coerced to any output type.

we may need to add the extra case to define the type of the block

If the final operand is evaluated, the type of a block is the type of the final operand, or () if the final operand is omitted. Otherwise, if the block contains any diverging expression, the block type is a never type.

@ehuss
Copy link
Contributor

ehuss commented Feb 27, 2025

Yea, if any statement diverges, and the block is missing a tail expression, then I believe tail expression is also treated as !. I'm going to close as a duplicate of #1033 which has some more details.

@ehuss ehuss closed this as completed Feb 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants