Skip to content

"No Struct Literal" restriction should be applied to lambda function body #43412

Closed
@matklad

Description

@matklad

Currently if || S {} {} is parsed as if (|| S {}) {}. I think it should be parsed as if (|| S) {} {}, to be consistent with handling of other similar "open" binary and prefix expressions (lambdas can be viewed as a form of a prefix expression).

Consider these expressions:

fn main() {
    if -S {};
    if 0 == -S {};
    if 0 == S {};
    if || S {} {};
    if 0 == || S {} {};
}

They are parsed as

fn main() {
    if (-S) {};
    if (0 == -S) {};
    if (0 == S) {};
    if (|| S {}) {};
    if (0 == || S {}) {};
}

ast json

The last two cases seem to be different from others and wrong. @eddyb on IRC suggested that this behavior could have been introduced accidentally during transition from lambdas with blocks as bodies to lambdas with arbitrary expressions as bodies.

If this is a bug, fixing it can render some previously valid programs invalid, but this seems rater unlikely, as bare lambdas rarely end up in conditionals. It is possible though:

struct S {}
struct E;
impl<T> PartialEq<T> for E { 
    fn eq(&self, _: &T) -> bool { true }
}

fn main() {
    if E == || S {} {}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    C-enhancementCategory: An issue proposing an enhancement or a PR with one.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions