about summary refs log tree commit diff
diff options
context:
space:
mode:
authorGuillaume Gomez <guillaume1.gomez@gmail.com>2017-05-27 21:34:59 +0200
committerGuillaume Gomez <guillaume1.gomez@gmail.com>2017-05-27 21:34:59 +0200
commit2969137a727d3fe2fea36c3a022ccb04b9b02617 (patch)
tree94f5a8d9de75c2e0e6e52705f60eca35230f1354
parent998b19740cba3a74f7281bba0f1ed9e1f8438236 (diff)
downloadrust-2969137a727d3fe2fea36c3a022ccb04b9b02617.tar.gz
rust-2969137a727d3fe2fea36c3a022ccb04b9b02617.zip
Add invalid unary operator usage error code
-rw-r--r--src/librustc_typeck/check/op.rs10
-rw-r--r--src/librustc_typeck/diagnostics.rs43
-rw-r--r--src/test/compile-fail/E0600.rs13
-rw-r--r--src/test/ui/codemap_tests/issue-28308.stderr2
-rw-r--r--src/test/ui/reachable/expr_unary.stderr2
5 files changed, 64 insertions, 6 deletions
diff --git a/src/librustc_typeck/check/op.rs b/src/librustc_typeck/check/op.rs
index 59cb61d9b97..3709260acc9 100644
--- a/src/librustc_typeck/check/op.rs
+++ b/src/librustc_typeck/check/op.rs
@@ -316,10 +316,12 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
         match self.lookup_op_method(ex, operand_ty, vec![], mname, trait_did, operand_expr) {
             Ok(t) => t,
             Err(()) => {
-                self.type_error_message(ex.span, |actual| {
-                    format!("cannot apply unary operator `{}` to type `{}`",
-                            op_str, actual)
-                }, operand_ty);
+                let actual = self.resolve_type_vars_if_possible(&operand_ty);
+                if !actual.references_error() {
+                    struct_span_err!(self.tcx.sess, ex.span, E0600,
+                                     "cannot apply unary operator `{}` to type `{}`",
+                                     op_str, actual).emit();
+                }
                 self.tcx.types.err
             }
         }
diff --git a/src/librustc_typeck/diagnostics.rs b/src/librustc_typeck/diagnostics.rs
index 612aa5a1f0d..f19eb194276 100644
--- a/src/librustc_typeck/diagnostics.rs
+++ b/src/librustc_typeck/diagnostics.rs
@@ -4052,6 +4052,49 @@ x.chocolate(); // error: no method named `chocolate` found for type `Mouth`
 ```
 "##,
 
+E0600: r##"
+An unary operator was used on a type which doesn't implement it.
+
+Example of erroneous code:
+
+```compile_fail,E0600
+enum Question {
+    Yes,
+    No,
+}
+
+!Question::Yes; // error: cannot apply unary operator `!` to type `Question`
+```
+
+In this case, `Question` would need to implement the `std::ops::Not` trait in
+order to be able to use `!` on it. Let's implement it:
+
+```
+use std::ops::Not;
+
+enum Question {
+    Yes,
+    No,
+}
+
+// We implement the `Not` trait on the enum.
+impl Not for Question {
+    type Output = bool;
+
+    fn not(self) -> bool {
+        match self {
+            Question::Yes => false, // If the `Answer` is `Yes`, then it
+                                    // returns false.
+            Question::No => true, // And here we do the opposite.
+        }
+    }
+}
+
+assert_eq!(!Question::Yes, false);
+assert_eq!(!Question::No, true);
+```
+"##,
+
 }
 
 register_diagnostics! {
diff --git a/src/test/compile-fail/E0600.rs b/src/test/compile-fail/E0600.rs
new file mode 100644
index 00000000000..5457ff26608
--- /dev/null
+++ b/src/test/compile-fail/E0600.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 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+fn main() {
+    !"a"; //~ ERROR E0600
+}
diff --git a/src/test/ui/codemap_tests/issue-28308.stderr b/src/test/ui/codemap_tests/issue-28308.stderr
index 5e611d42a14..43743b796d5 100644
--- a/src/test/ui/codemap_tests/issue-28308.stderr
+++ b/src/test/ui/codemap_tests/issue-28308.stderr
@@ -1,4 +1,4 @@
-error: cannot apply unary operator `!` to type `&'static str`
+error[E0600]: cannot apply unary operator `!` to type `&'static str`
   --> $DIR/issue-28308.rs:12:5
    |
 12 |     assert!("foo");
diff --git a/src/test/ui/reachable/expr_unary.stderr b/src/test/ui/reachable/expr_unary.stderr
index 889e3e01c6d..f47791455af 100644
--- a/src/test/ui/reachable/expr_unary.stderr
+++ b/src/test/ui/reachable/expr_unary.stderr
@@ -1,4 +1,4 @@
-error: cannot apply unary operator `!` to type `!`
+error[E0600]: cannot apply unary operator `!` to type `!`
   --> $DIR/expr_unary.rs:18:16
    |
 18 |     let x: ! = ! { return; 22 };