Skip to content
This repository was archived by the owner on Apr 8, 2024. It is now read-only.

How to handle brace expression restrictions? #53

@ehuss

Description

@ehuss

Various places do not allow struct expressions. All of the following fail to parse in libsyntax, but pass in the current lyg grammar:

if S{a:1} {}
if let x=S{a:1} {}
while S{a:1} {}
while let x=S{a:1} {}
for pat in S{a:1} {}
match S{a:1} {}

This restriction applies recursively (I'm not sure how to express this), that is the following is also rejected:

if x==S{a:1} {}

if S{a:1}.foo() {}

I included a field just because rustc will give a helpful suggestion, whereas a fieldless expression gets confused by the second {} which rustc just treats an independent block, as this hilarious example shows:

#[derive(PartialEq)]
struct S;
let x = S;
if x != S{} {
    // This block is executed because it is not part of the `if` expression, 
    // even though it appears to be. 
    println!("hm, x!=S{} should be false");
}

Should this kind of ambiguity rejection be part of the syntax?

See rust-lang/rust#59981 for some examples, and where this was recently changed. See also rust-lang/reference#569.

Metadata

Metadata

Assignees

No one assigned

    Labels

    grammarIssues with the definition of the rust grammarrestrictionsIssues related to the permissiveness of the grammar

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions