Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add invalid unary operator usage error code
  • Loading branch information
GuillaumeGomez committed May 27, 2017
1 parent 998b197 commit 2969137
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 6 deletions.
10 changes: 6 additions & 4 deletions src/librustc_typeck/check/op.rs
Expand Up @@ -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
}
}
Expand Down
43 changes: 43 additions & 0 deletions src/librustc_typeck/diagnostics.rs
Expand Up @@ -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! {
Expand Down
13 changes: 13 additions & 0 deletions 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
}
2 changes: 1 addition & 1 deletion 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");
Expand Down
2 changes: 1 addition & 1 deletion 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 };
Expand Down

0 comments on commit 2969137

Please sign in to comment.