Skip to content

Trailing typecast syntax error when used in functions #46466

Closed
@vojtechkral

Description

@vojtechkral

This sort of construct causes a syntax error:

fn foo() -> u32 {
    if true { 3i32 } else { 4i32 } as u32
}
error: expected expression, found keyword `as`

Playground link

The same syntax works fine in an assignment form:

let foo = if true { 3i32 } else { 4i32 } as u32;

And so it is surprising that it doesn't work in a function context.
When parentheses are added, it parses in the function context too:

fn foo() -> u32 {
    (if true { 3i32 } else { 4i32 }) as u32
}

Playground link

The same applies to using match and presumably possibly other expressions as well.

Seems like a bug?

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-parserArea: The lexing & parsing of Rust source code to an ASTC-bugCategory: This is a bug.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions