Skip to content

Propagate restrictions against struct literals to the RHS of assignment #17290

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

Merged
merged 3 commits into from
Sep 17, 2014

Conversation

bkoropoff
Copy link
Contributor

This prevents confusing errors when accidentally using an assignment in an if expression. For example:

fn main() {
    let x = 1u;
    if x = x {
        println!("{}", x);
    }
}

Previously, this yielded:

test.rs:4:16: 4:17 error: expected `:`, found `!`
test.rs:4         println!("{}", x);
                         ^

With this change, it now yields:

test.rs:3:8: 3:13 error: mismatched types: expected `bool`, found `()` (expected bool, found ())
test.rs:3     if x = x {
                 ^~~~~

Closes issue #17283

r? @nick29581

RESTRICT_NO_STRUCT_LITERAL
} else {
UNRESTRICTED
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would probably be cleaner code to give the enum variants here int values and treat them like flags.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at the RESTRICT_NO_BAR_OR_DOUBLEBAR_OP variant, it seems like a reasonable way to refactor, but it might be better as a separate PR.

@bkoropoff bkoropoff force-pushed the issue-17283 branch 2 times, most recently from 2adc5c8 to b04a566 Compare September 16, 2014 05:48
@bkoropoff
Copy link
Contributor Author

New version force-pushed, now with more bitflags.

static Unrestricted = 0b0000,
static ResStmtExpr = 0b0001,
static ResNoBarOp = 0b0010,
static ResNoDoubleBarOp = 0b0100,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please spell out Restrict for the flags and don't lose the BarOr from this one - longer names ftw

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Losing BarOr was intentional, as it can just set both flags to indicate it doesn't want either.

Although, looking at it now, it doesn't actually seem to be set anywhere in the parser. I guess I'll just drop it.

@nrc
Copy link
Member

nrc commented Sep 17, 2014

Nice, thanks for the bitflags conversion! r=me with the changes requested.

This makes having multiple restrictions at once cleaner.
Also drop NO_DOUBLEBAR restriction since it is never used.
This prevents confusing errors when accidentally using an assignment
in an `if` expression.  For example:

```rust
fn main() {
    let x = 1u;
    if x = x {
        println!("{}", x);
    }
}
```

Previously, this yielded:

```
test.rs:4:16: 4:17 error: expected `:`, found `!`
test.rs:4         println!("{}", x);
                         ^
```

With this change, it now yields:

```
test.rs:3:8: 3:13 error: mismatched types: expected `bool`, found `()` (expected bool, found ())
test.rs:3     if x = x {
                 ^~~~~
```

Closes issue rust-lang#17283
alexcrichton added a commit to alexcrichton/rust that referenced this pull request Sep 17, 2014
@bors bors merged commit 0e230c0 into rust-lang:master Sep 17, 2014
@bkoropoff bkoropoff deleted the issue-17283 branch September 20, 2014 21:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants