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

Confused by diverging type of block tail expression #85936

Closed
ehuss opened this issue Jun 2, 2021 · 2 comments
Closed

Confused by diverging type of block tail expression #85936

ehuss opened this issue Jun 2, 2021 · 2 comments
Labels
C-bug Category: This is a bug.

Comments

@ehuss
Copy link
Contributor

ehuss commented Jun 2, 2021

I am confused by the following, and I wanted to see if it is a bug or not. The following successfully compiles:

pub fn f() -> ! {
    loop {}
    ();
}

The following without the semicolon does not:

pub fn f() -> ! {
    loop {}
    ()  // ERROR: expected ! found unit type
}

I expected to see this happen:

My understanding is that the type of a block expression without a final expression is (), so I would expect these to both be the same — probably both should error, since I would expect both have the type () as the final expression. If not, can someone explain how the types resolve here?

Note: This was changed in #45880 (I think) where before that both were accepted.

Meta

rustc 1.54.0-nightly (ff2c947 2021-05-25)

@FabianWolff
Copy link
Contributor

I can't give you a definitive answer, but have a look at this comment in the type checking code:

// Subtle: if there is no explicit tail expression,
// that is typically equivalent to a tail expression
// of `()` -- except if the block diverges. In that
// case, there is no value supplied from the tail
// expression (assuming there are no other breaks,
// this implies that the type of the block will be
// `!`).

So it seems that this is the intended behavior, i.e. that a missing tail expression is not equivalent to ().

@ehuss
Copy link
Contributor Author

ehuss commented Jun 3, 2021

Ah, it does seem intended, thanks! I'll close this and defer to rust-lang/reference#1033 to get it documented.

@ehuss ehuss closed this as completed Jun 3, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug.
Projects
None yet
Development

No branches or pull requests

2 participants