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

Document when the return type of a block expression implicitly becomes ! #1033

Open
smarnach opened this issue Jun 2, 2021 · 4 comments
Open
Labels
C-bug Incorrect statements, terminology, or rendering issues

Comments

@smarnach
Copy link

smarnach commented Jun 2, 2021

The reference currently states

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

However, the type of this block appears to be !:

{
    loop {}
    5;
}

whereas the type of this block is i32

{
    loop {}
    5
}

So it looks like the presence of an expression of type ! changes the default type of the block from () to !.

As a side note, the doucmentation could also make clearer that "omitting the final expression" essentially means terminating the block with a semicolon.

@ehuss
Copy link
Contributor

ehuss commented Jun 2, 2021

Thanks for opening the issue! I'm uncertain if this is a bug or not, so I figured I'd open rust-lang/rust#85936. There are some complexities around never coercion that I do not understand.

@smarnach
Copy link
Author

smarnach commented Jun 2, 2021

I think it's intentional, since otherwise { return; } would be of type (), which would be really inconvenient in if/else statements, since it would force that type on the other branch.

@ehuss
Copy link
Contributor

ehuss commented Jun 3, 2021

Hm, yea. I don't know anything about how type checking works, but it would be great to get some documentation on this.

On the linked issue someone pointed out this comment:

https://github.com/rust-lang/rust/blob/a93699f20a433797a4b84787b9652300dd7b2ad2/compiler/rustc_typeck/src/check/fn_ctxt/checks.rs#L654-L660

@traviscross
Copy link
Contributor

traviscross commented Jul 16, 2024

@traviscross traviscross added the C-bug Incorrect statements, terminology, or rendering issues label Jul 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Incorrect statements, terminology, or rendering issues
Projects
None yet
Development

No branches or pull requests

3 participants