]> git.lizzy.rs Git - rust.git/commitdiff
allow logical negation operator (!) to be overloaded
authorAndrew Paseltiner <apaseltiner@gmail.com>
Fri, 11 Jan 2013 17:16:58 +0000 (12:16 -0500)
committerAndrew Paseltiner <apaseltiner@gmail.com>
Fri, 11 Jan 2013 19:31:44 +0000 (14:31 -0500)
src/libcore/core.rc
src/libcore/ops.rs
src/libcore/prelude.rs
src/librustc/middle/lang_items.rs
src/librustc/middle/resolve.rs

index f8658b8e46e164c10db29d9e68262528e185cc1c..81b9077fb3e0fcc56bec3fe3d4592787aa685130 100644 (file)
@@ -172,7 +172,7 @@ pub mod util;
 
 pub use kinds::{Const, Copy, Owned, Durable};
 pub use ops::{Drop};
-pub use ops::{Add, Sub, Mul, Div, Modulo, Neg};
+pub use ops::{Add, Sub, Mul, Div, Modulo, Neg, Not};
 pub use ops::{BitAnd, BitOr, BitXor};
 pub use ops::{Shl, Shr, Index};
 
index 22c363ba9c053a9399386cddce0f29ddacfe16e0..36293d6a1c0cef105a7bf00683b2870e935192c0 100644 (file)
@@ -48,6 +48,11 @@ pub trait Neg<Result> {
     pure fn neg(&self) -> Result;
 }
 
+#[lang="not"]
+pub trait Not<Result> {
+    pure fn not(&self) -> Result;
+}
+
 #[lang="bitand"]
 pub trait BitAnd<RHS,Result> {
     pure fn bitand(&self, rhs: &RHS) -> Result;
@@ -77,4 +82,3 @@ pub trait Shr<RHS,Result> {
 pub trait Index<Index,Result> {
     pure fn index(&self, index: Index) -> Result;
 }
-
index f2f6659278d44a99089f78b6431484acfbe93fb9..de23feceea5907478230cd038cfb9cb026faeebc 100644 (file)
@@ -4,7 +4,7 @@
 
 pub use kinds::{Const, Copy, Owned, Durable};
 pub use ops::{Drop};
-pub use ops::{Add, Sub, Mul, Div, Modulo, Neg};
+pub use ops::{Add, Sub, Mul, Div, Modulo, Neg, Not};
 pub use ops::{BitAnd, BitOr, BitXor};
 pub use ops::{Shl, Shr, Index};
 pub use option::{Option, Some, None};
index 6870082ffacbaa4758d70050ce500fae47c9a4a0..ae56fb7f717dad13e1660738190be6284e74e141 100644 (file)
@@ -50,36 +50,37 @@ pub enum LangItem {
     DivTraitLangItem,           // 8
     ModuloTraitLangItem,        // 9
     NegTraitLangItem,           // 10
-    BitXorTraitLangItem,        // 11
-    BitAndTraitLangItem,        // 12
-    BitOrTraitLangItem,         // 13
-    ShlTraitLangItem,           // 14
-    ShrTraitLangItem,           // 15
-    IndexTraitLangItem,         // 16
-
-    EqTraitLangItem,            // 17
-    OrdTraitLangItem,           // 18
-
-    StrEqFnLangItem,            // 19
-    UniqStrEqFnLangItem,        // 20
-    AnnihilateFnLangItem,       // 21
-    LogTypeFnLangItem,          // 22
-    FailFnLangItem,             // 23
-    FailBoundsCheckFnLangItem,  // 24
-    ExchangeMallocFnLangItem,   // 25
-    ExchangeFreeFnLangItem,     // 26
-    MallocFnLangItem,           // 27
-    FreeFnLangItem,             // 28
+    NotTraitLangItem,           // 11
+    BitXorTraitLangItem,        // 12
+    BitAndTraitLangItem,        // 13
+    BitOrTraitLangItem,         // 14
+    ShlTraitLangItem,           // 15
+    ShrTraitLangItem,           // 16
+    IndexTraitLangItem,         // 17
+
+    EqTraitLangItem,            // 18
+    OrdTraitLangItem,           // 19
+
+    StrEqFnLangItem,            // 20
+    UniqStrEqFnLangItem,        // 21
+    AnnihilateFnLangItem,       // 22
+    LogTypeFnLangItem,          // 23
+    FailFnLangItem,             // 24
+    FailBoundsCheckFnLangItem,  // 25
+    ExchangeMallocFnLangItem,   // 26
+    ExchangeFreeFnLangItem,     // 27
+    MallocFnLangItem,           // 28
+    FreeFnLangItem,             // 29
 }
 
 struct LanguageItems {
-    items: [ Option<def_id> * 29 ]
+    items: [ Option<def_id> * 30 ]
 }
 
 impl LanguageItems {
     static pub fn new() -> LanguageItems {
         LanguageItems {
-            items: [ None, ..29 ]
+            items: [ None, ..30 ]
         }
     }
 
@@ -106,25 +107,26 @@ fn each_item(&self, f: &fn(def_id: def_id, i: uint) -> bool) {
             8  => "div",
             9  => "modulo",
             10 => "neg",
-            11 => "bitxor",
-            12 => "bitand",
-            13 => "bitor",
-            14 => "shl",
-            15 => "shr",
-            16 => "index",
-            17 => "eq",
-            18 => "ord",
-
-            19 => "str_eq",
-            20 => "uniq_str_eq",
-            21 => "annihilate",
-            22 => "log_type",
-            23 => "fail_",
-            24 => "fail_bounds_check",
-            25 => "exchange_malloc",
-            26 => "exchange_free",
-            27 => "malloc",
-            28 => "free",
+            11 => "not",
+            12 => "bitxor",
+            13 => "bitand",
+            14 => "bitor",
+            15 => "shl",
+            16 => "shr",
+            17 => "index",
+            18 => "eq",
+            19 => "ord",
+
+            20 => "str_eq",
+            21 => "uniq_str_eq",
+            22 => "annihilate",
+            23 => "log_type",
+            24 => "fail_",
+            25 => "fail_bounds_check",
+            26 => "exchange_malloc",
+            27 => "exchange_free",
+            28 => "malloc",
+            29 => "free",
 
             _ => "???"
         }
@@ -167,6 +169,9 @@ pub fn modulo_trait(&const self) -> def_id {
     pub fn neg_trait(&const self) -> def_id {
         self.items[NegTraitLangItem as uint].get()
     }
+    pub fn not_trait(&const self) -> def_id {
+        self.items[NotTraitLangItem as uint].get()
+    }
     pub fn bitxor_trait(&const self) -> def_id {
         self.items[BitXorTraitLangItem as uint].get()
     }
@@ -244,6 +249,7 @@ fn LanguageItemCollector(crate: @crate,
     item_refs.insert(~"div", DivTraitLangItem as uint);
     item_refs.insert(~"modulo", ModuloTraitLangItem as uint);
     item_refs.insert(~"neg", NegTraitLangItem as uint);
+    item_refs.insert(~"not", NotTraitLangItem as uint);
     item_refs.insert(~"bitxor", BitXorTraitLangItem as uint);
     item_refs.insert(~"bitand", BitAndTraitLangItem as uint);
     item_refs.insert(~"bitor", BitOrTraitLangItem as uint);
index e2a5c7ca9f96f2d4e5f362beed9bd501143cf634..a7c579127f95478d9d3e165c72a3199470dff681 100644 (file)
@@ -54,7 +54,7 @@
 use syntax::ast::{type_value_ns, ty_param_bound, unnamed_field};
 use syntax::ast::{variant, view_item, view_item_export, view_item_import};
 use syntax::ast::{view_item_use, view_path_glob, view_path_list};
-use syntax::ast::{view_path_simple, visibility, anonymous, named};
+use syntax::ast::{view_path_simple, visibility, anonymous, named, not};
 use syntax::ast_util::{def_id_of_def, dummy_sp, local_def};
 use syntax::ast_util::{path_to_ident, walk_pat, trait_method_to_ty_method};
 use syntax::ast_util::{Privacy, Public, Private, visibility_to_privacy};
@@ -5217,6 +5217,10 @@ fn record_candidate_traits_for_expr_if_necessary(expr: @expr) {
                 self.add_fixed_trait_for_expr(expr.id,
                                               self.lang_items.neg_trait());
             }
+            expr_unary(not, _) => {
+                self.add_fixed_trait_for_expr(expr.id,
+                                              self.lang_items.not_trait());
+            }
             expr_index(*) => {
                 self.add_fixed_trait_for_expr(expr.id,
                                               self.lang_items.index_trait());