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

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

Closed
matklad opened this issue Jul 22, 2017 · 2 comments · Fixed by #43425
Closed

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

matklad opened this issue Jul 22, 2017 · 2 comments · Fixed by #43425
Labels
C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@matklad
Copy link
Member

matklad commented Jul 22, 2017

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 {} {}
}
@eddyb
Copy link
Member

eddyb commented Jul 22, 2017

cc @rust-lang/compiler

@eddyb
Copy link
Member

eddyb commented Jul 22, 2017

Oh and I didn't mean I had introduced the distinction accidentally but rather that before I made that simplification, the code was block-oriented so the need for it to obey typical expression rules may have been overlooked and I didn't think about it (to consider fixing it).

@Mark-Simulacrum Mark-Simulacrum added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Jul 26, 2017
@shepmaster shepmaster added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Aug 25, 2017
bors added a commit that referenced this issue Aug 31, 2017
Lambda expressions honor no struct literal restriction

This is a fix for #43412 if we decide that it is indeed a bug :)

closes #43412
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants