From a9d3b3498e20a926d5b1662eb576d7db230ca110 Mon Sep 17 00:00:00 2001 From: F001 Date: Tue, 2 May 2017 23:31:47 -0700 Subject: [PATCH] Suggest `!` for bitwise negation when encountering a `~` --- src/libsyntax/parse/parser.rs | 13 +++++++++++++ src/test/ui/did_you_mean/issue-41679.rs | 13 +++++++++++++ src/test/ui/did_you_mean/issue-41679.stderr | 10 ++++++++++ 3 files changed, 36 insertions(+) create mode 100644 src/test/ui/did_you_mean/issue-41679.rs create mode 100644 src/test/ui/did_you_mean/issue-41679.stderr diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index d252963274e..f99f39dae6b 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -2700,6 +2700,19 @@ impl<'a> Parser<'a> { let (span, e) = self.interpolated_or_expr_span(e)?; (span, self.mk_unary(UnOp::Not, e)) } + // Suggest `!` for bitwise negation when encountering a `~` + token::Tilde => { + self.bump(); + let e = self.parse_prefix_expr(None); + 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"); + err.span_label(span_of_tilde, &"did you mean `!`?"); + err.help("use `!` instead of `~` if you meant to perform bitwise negation"); + err.emit(); + (span, self.mk_unary(UnOp::Not, e)) + } token::BinOp(token::Minus) => { self.bump(); let e = self.parse_prefix_expr(None); diff --git a/src/test/ui/did_you_mean/issue-41679.rs b/src/test/ui/did_you_mean/issue-41679.rs new file mode 100644 index 00000000000..5091b9efc34 --- /dev/null +++ b/src/test/ui/did_you_mean/issue-41679.rs @@ -0,0 +1,13 @@ +// Copyright 2017 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +fn main() { + let x = ~1; +} diff --git a/src/test/ui/did_you_mean/issue-41679.stderr b/src/test/ui/did_you_mean/issue-41679.stderr new file mode 100644 index 00000000000..5a89ec96e24 --- /dev/null +++ b/src/test/ui/did_you_mean/issue-41679.stderr @@ -0,0 +1,10 @@ +error: `~` can not be used as an unary operator + --> $DIR/issue-41679.rs:12:13 + | +12 | let x = ~1; + | ^ did you mean `!`? + | + = help: use `!` instead of `~` if you meant to perform bitwise negation + +error: aborting due to previous error +