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 ~ #41722

Merged
merged 1 commit into from
May 6, 2017
Merged

Suggest ! for bitwise negation when encountering a ~ #41722

merged 1 commit into from
May 6, 2017

Conversation

F001
Copy link
Contributor

@F001 F001 commented May 3, 2017

Fix #41679

Here is a program

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

@rust-highfive
Copy link
Collaborator

Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @nikomatsakis (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

Please see the contribution instructions for more information.

@kennytm
Copy link
Member

kennytm commented May 3, 2017

You should add a test case (probably in src/test/ui/did_you_mean).

@F001
Copy link
Contributor Author

F001 commented May 3, 2017

Thank you @kennytm . Test case added.

let (span, e) = self.interpolated_or_expr_span(e)?;
let mut err = self.diagnostic().struct_span_err(lo.to(span),
"`~` can not be used as an unary operator");
err.help("use `!` instead of `~` if you meant to bitwise negation");
Copy link
Contributor

Choose a reason for hiding this comment

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

Could you also add err.span_label(span_of_tilda, "did you mean !?") and use span_of_tilda for the primary error as well?

Copy link
Contributor

Choose a reason for hiding this comment

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

meant to bitwise negation

Redundant "to"

Copy link
Member

Choose a reason for hiding this comment

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

meant to bitwise negation

Alternatively meant to perform bitwise negation.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed. Thanks.

Copy link
Contributor

Choose a reason for hiding this comment

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

By span_of_tilda I actually meant the span of ~ itself (let span_of_tilda = self.span; self.bump();)

@@ -2700,6 +2700,17 @@ impl<'a> Parser<'a> {
let (span, e) = self.interpolated_or_expr_span(e)?;
(span, self.mk_unary(UnOp::Not, e))
}
// fix issue #41679: Suggest `!` for bitwise negation when encountering a `~`
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: could you remove "fix issue #41679:"? (Not useful for a reader and the history is available from git if necessary.)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ok.

@@ -201,6 +201,7 @@ impl Token {
OpenDelim(..) => true, // tuple, array or block
Literal(..) => true, // literal
Not => true, // operator not
Tilde => true, // operator not (incorrect)
Copy link
Contributor

Choose a reason for hiding this comment

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

This is slightly risky, can_begin_expr can affect syntax accepted by parser in general, not only diagnostics.
~ is not used anywhere right now, so it's ok for now, but it may be used in the future.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I agree. I have deleted this line, and the test works fine. At least for this case, it is not necessary to modify can_begin_expr .

Copy link
Contributor

Choose a reason for hiding this comment

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

This will remove the suggestion from something like return ~0, but that's more or less ok.

@nagisa
Copy link
Member

nagisa commented May 3, 2017

I had hoped we could avoid adding a token here.

@petrochenkov I did use ~ a number of times as a separator in macros by example. You sure there’s no way it will affect them?

@estebank
Copy link
Contributor

estebank commented May 3, 2017

I had hoped we could avoid adding a token here.

@nagisa Tilde is already a token, it wasn't added in this PR.

@aidanhs aidanhs added the S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. label May 3, 2017
@F001
Copy link
Contributor Author

F001 commented May 4, 2017

@aidanhs Comments are resolved. Do you have other concerns?

@petrochenkov
Copy link
Contributor

@nagisa
I could imagine this breaking something like

macro_rules! m {
    ($x: expr) => {};
    (~ $x: expr) => {};
}

fn main() {
    m!(~x); // ERROR expected expression, found `~`
}

but it doesn't seem to work already.

@petrochenkov
Copy link
Contributor

r=me modulo #41722 (comment)

@F001
Copy link
Contributor Author

F001 commented May 5, 2017

r? @petrochenkov
Thanks very much for your patient guidance!

@petrochenkov
Copy link
Contributor

@bors r+

@bors
Copy link
Contributor

bors commented May 5, 2017

📌 Commit a9d3b34 has been approved by petrochenkov

let (span, e) = self.interpolated_or_expr_span(e)?;
let span_of_tilde = lo;
let mut err = self.diagnostic().struct_span_err(span_of_tilde,
"`~` can not be used as an unary operator");
Copy link
Contributor

Choose a reason for hiding this comment

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

Bit of a late review, but won't hurt. Anyway, this should be using "a" because unary doesn't start with a vowel sound (As in, it starts with "you").

Copy link
Contributor

Choose a reason for hiding this comment

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

@acdenisSK Looks like this PR has already been merged, would you like to submit a minor PR to fix this?

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah, i guess, gimme a sec.

frewsxcv added a commit to frewsxcv/rust that referenced this pull request 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
bors added a commit that referenced this pull request May 5, 2017
Rollup of 9 pull requests

- Successful merges: #41064, #41307, #41512, #41582, #41678, #41722, #41734, #41761, #41763
- Failed merges:
@bors bors merged commit a9d3b34 into rust-lang:master May 6, 2017
@bors
Copy link
Contributor

bors commented May 6, 2017

⌛ Testing commit a9d3b34 with merge 42a4f37...

@F001 F001 deleted the warnTilde branch May 7, 2017 02:13
@lambda-fairy
Copy link
Contributor

@petrochenkov I believe that's covered by #27832

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author.
Projects
None yet
Development

Successfully merging this pull request may close these issues.