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

Suggest ! for bitwise negation when encountering a ~ #41679

Closed
bstrie opened this issue May 1, 2017 · 4 comments
Closed

Suggest ! for bitwise negation when encountering a ~ #41679

bstrie opened this issue May 1, 2017 · 4 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints

Comments

@bstrie
Copy link
Contributor

bstrie commented May 1, 2017

Here's a program:

fn main() {
    let x = ~1;
}

Its output:

error: expected expression, found `~`
 --> <anon>:2:13
  |
2 |     let x = ~1;
  |             ^

error: aborting due to previous error

Given that this is our only deviation from C-style bitwise operator syntax, it would be nice to have a note of some kind to suggest ! here.

Ideally the note would only appear when the ~ looks like it's being applied as a unary operator on a numeric type, but if that would require too much machinery then something simpler will suffice.

@bstrie bstrie added the A-diagnostics Area: Messages for errors, warnings, and lints label May 1, 2017
@bstrie
Copy link
Contributor Author

bstrie commented May 1, 2017

And no, I'm not concerned about potential confusion with the ancient owned pointer syntax. :P

@estebank
Copy link
Contributor

estebank commented May 2, 2017

If anyone wants to tackle this, you'll need to modify libsyntax/parse/parser.rs.

@F001
Copy link
Contributor

F001 commented May 2, 2017

I would like to have a try. It seems that I need to emit a struct_span_err with help() message. Is it enough?

@bstrie
Copy link
Contributor Author

bstrie commented May 2, 2017

@F001 Worth asking on #rust-internals on irc.mozilla.org, or internals.rust-lang.org if you'd prefer something other than IRC.

frewsxcv added a commit to frewsxcv/rust that referenced this issue May 5, 2017
Suggest `!` for bitwise negation when encountering a `~`

Fix rust-lang#41679

Here is a program

```rust
fn main() {
    let x = ~1;
}
```

It's output:
```
error: `~` can not be used as an unary operator
 --> /home/fcc/temp/test.rs:4:13
  |
4 |     let x = ~1;
  |             ^^
  |
  = help: use `!` instead of `~` if you meant to bitwise negation
```

cc @bstrie
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints
Projects
None yet
Development

No branches or pull requests

3 participants