Skip to content

No-brace restrictions should mention that they are recursive. #569

Description

@ehuss

There are various places in the grammar that restrict expressions with various types of braces. These restrictions also apply recursively into the expressions. This should be captured somehow. Examples:

if-expr.md mentions that the expression cannot be a struct expression:

if S{} {/*...*/}  // This is syntactically invalid.

However this applies recursively:

if x == S{} {/*...*/}  // This is a conditional expression with a struct inside it, also invalid.

Another example is that a final expression in Statements must be an ExpressionWithoutBlock. However, ExpressionWithoutBlock does not capture that the block restriction also applies recursively. For example {{1}+{1}} does not parse.

I don't know the exact rules here, presumably anything with brackets as the last expression? Or parsing a block ends an expression? This needs to be spelled out explicitly, and somehow captured in the grammar.

See rust-lang/rust#59981.

Also, if, if-let, and match should explain why struct expressions aren't allowed, and it should indicate that parentheses can be used as a work-around.

NOTE: This applies to all the places where structs aren't allowed, like loop expressions.

See also rust-lang/rust#59981 for some example tests that illustrate similar issues.

Also, it's not just tail expressions. Method call expressions also can't be structs, as in if S{}.foo() {}.

Another example: match () { () => 1 } + 2 the match requires parentheses.
Another example: {{1}==0} doesn't work, but {1=={0}} does.

Another example (see rust-lang/rust#88690): {"abc"}.len(); works but {1}-1 does not.

Activity

  1. changed the title [-]Struct restrictions should mention that they are recursive.[/-] [+]No-brace restrictions should mention that they are recursive.[/+] on May 2, 2019
  2. ehuss commented on May 1, 2025

    @ehuss
    ContributorAuthor

    I'm going to split this into two issues: #1808 and #1809, because these really are two separate concerns.

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

    A-grammarArea: Syntax and parsing

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions