From 0de7aa55062485835bc45b962f306dddf42f372d Mon Sep 17 00:00:00 2001 From: Andrew Paseltiner Date: Fri, 11 Jan 2013 12:16:58 -0500 Subject: [PATCH] allow logical negation operator (!) to be overloaded --- src/libcore/core.rc | 2 +- src/libcore/ops.rs | 6 ++- src/libcore/prelude.rs | 2 +- src/librustc/middle/lang_items.rs | 82 +++++++++++++++++-------------- src/librustc/middle/resolve.rs | 6 ++- 5 files changed, 56 insertions(+), 42 deletions(-) diff --git a/src/libcore/core.rc b/src/libcore/core.rc index f8658b8e46e..81b9077fb3e 100644 --- a/src/libcore/core.rc +++ b/src/libcore/core.rc @@ -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}; diff --git a/src/libcore/ops.rs b/src/libcore/ops.rs index 22c363ba9c0..36293d6a1c0 100644 --- a/src/libcore/ops.rs +++ b/src/libcore/ops.rs @@ -48,6 +48,11 @@ pub trait Neg { pure fn neg(&self) -> Result; } +#[lang="not"] +pub trait Not { + pure fn not(&self) -> Result; +} + #[lang="bitand"] pub trait BitAnd { pure fn bitand(&self, rhs: &RHS) -> Result; @@ -77,4 +82,3 @@ pub trait Shr { pub trait Index { pure fn index(&self, index: Index) -> Result; } - diff --git a/src/libcore/prelude.rs b/src/libcore/prelude.rs index f2f6659278d..de23feceea5 100644 --- a/src/libcore/prelude.rs +++ b/src/libcore/prelude.rs @@ -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}; diff --git a/src/librustc/middle/lang_items.rs b/src/librustc/middle/lang_items.rs index 6870082ffac..ae56fb7f717 100644 --- a/src/librustc/middle/lang_items.rs +++ b/src/librustc/middle/lang_items.rs @@ -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 + NotTraitLangItem, // 11 + BitXorTraitLangItem, // 12 + BitAndTraitLangItem, // 13 + BitOrTraitLangItem, // 14 + ShlTraitLangItem, // 15 + ShrTraitLangItem, // 16 + IndexTraitLangItem, // 17 - EqTraitLangItem, // 17 - OrdTraitLangItem, // 18 + EqTraitLangItem, // 18 + OrdTraitLangItem, // 19 - StrEqFnLangItem, // 19 - UniqStrEqFnLangItem, // 20 - AnnihilateFnLangItem, // 21 - LogTypeFnLangItem, // 22 - FailFnLangItem, // 23 - FailBoundsCheckFnLangItem, // 24 - ExchangeMallocFnLangItem, // 25 - ExchangeFreeFnLangItem, // 26 - MallocFnLangItem, // 27 - FreeFnLangItem, // 28 + StrEqFnLangItem, // 20 + UniqStrEqFnLangItem, // 21 + AnnihilateFnLangItem, // 22 + LogTypeFnLangItem, // 23 + FailFnLangItem, // 24 + FailBoundsCheckFnLangItem, // 25 + ExchangeMallocFnLangItem, // 26 + ExchangeFreeFnLangItem, // 27 + MallocFnLangItem, // 28 + FreeFnLangItem, // 29 } struct LanguageItems { - items: [ Option * 29 ] + items: [ Option * 30 ] } impl LanguageItems { static pub fn new() -> LanguageItems { LanguageItems { - items: [ None, ..29 ] + items: [ None, ..30 ] } } @@ -106,25 +107,26 @@ impl LanguageItems { 8 => "div", 9 => "modulo", 10 => "neg", - 11 => "bitxor", - 12 => "bitand", - 13 => "bitor", - 14 => "shl", - 15 => "shr", - 16 => "index", - 17 => "eq", - 18 => "ord", + 11 => "not", + 12 => "bitxor", + 13 => "bitand", + 14 => "bitor", + 15 => "shl", + 16 => "shr", + 17 => "index", + 18 => "eq", + 19 => "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", + 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 @@ impl LanguageItems { 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); diff --git a/src/librustc/middle/resolve.rs b/src/librustc/middle/resolve.rs index e2a5c7ca9f9..a7c579127f9 100644 --- a/src/librustc/middle/resolve.rs +++ b/src/librustc/middle/resolve.rs @@ -54,7 +54,7 @@ use syntax::ast::{ty_str, ty_u, ty_u16, ty_u32, ty_u64, ty_u8, ty_uint}; 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 @@ impl Resolver { 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());