Skip to content
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

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

Open
xmh0511 opened this issue Feb 20, 2025 · 0 comments
Open

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.

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

1 participant