Skip to content

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

Description

@xmh0511

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.

Activity

  1. ehuss commented on Feb 27, 2025

    @ehuss
    Contributor

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions