From f12ff1964b3040c426263d52472a9ed503c6a4aa Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Sun, 23 Feb 2014 17:45:44 -0800 Subject: [PATCH 1/4] std: minor whitespace cleanup --- src/libcollections/hashmap.rs | 2 +- src/librustc/middle/trans/common.rs | 7 ++-- src/librustc/middle/ty.rs | 2 +- src/libstd/io/net/ip.rs | 1 - src/libstd/lib.rs | 1 - src/libsyntax/ast.rs | 38 +++++++++---------- src/libsyntax/codemap.rs | 2 +- src/libsyntax/util/interner.rs | 6 +-- .../deriving-via-extension-type-params.rs | 3 +- 9 files changed, 28 insertions(+), 34 deletions(-) diff --git a/src/libcollections/hashmap.rs b/src/libcollections/hashmap.rs index 4f0b7dfb35d..938b94fb8a4 100644 --- a/src/libcollections/hashmap.rs +++ b/src/libcollections/hashmap.rs @@ -97,7 +97,7 @@ fn resize_at(capacity: uint) -> uint { (capacity * 3) / 4 } -impl HashMap { +impl HashMap { #[inline] fn to_bucket(&self, h: uint) -> uint { // A good hash function with entropy spread over all of the diff --git a/src/librustc/middle/trans/common.rs b/src/librustc/middle/trans/common.rs index 278a7ccbc7b..74d21f114ef 100644 --- a/src/librustc/middle/trans/common.rs +++ b/src/librustc/middle/trans/common.rs @@ -722,7 +722,7 @@ pub fn is_null(val: ValueRef) -> bool { } // Used to identify cached monomorphized functions and vtables -#[deriving(Eq,Hash)] +#[deriving(Eq, Hash)] pub enum mono_param_id { mono_precise(ty::t, Option<@~[mono_id]>), mono_any, @@ -732,7 +732,7 @@ pub enum mono_param_id { datum::RvalueMode), } -#[deriving(Eq,Hash)] +#[deriving(Eq, Hash)] pub enum MonoDataClass { MonoBits, // Anything not treated differently from arbitrary integer data MonoNonNull, // Non-null pointers (used for optional-pointer optimization) @@ -754,8 +754,7 @@ pub fn mono_data_classify(t: ty::t) -> MonoDataClass { } } - -#[deriving(Eq,Hash)] +#[deriving(Eq, Hash)] pub struct mono_id_ { def: ast::DefId, params: ~[mono_param_id] diff --git a/src/librustc/middle/ty.rs b/src/librustc/middle/ty.rs index b351b5c0cb8..34e2558638d 100644 --- a/src/librustc/middle/ty.rs +++ b/src/librustc/middle/ty.rs @@ -150,7 +150,7 @@ pub struct field_ty { // Contains information needed to resolve types and (in the future) look up // the types of AST nodes. -#[deriving(Eq,Hash)] +#[deriving(Eq, Hash)] pub struct creader_cache_key { cnum: CrateNum, pos: uint, diff --git a/src/libstd/io/net/ip.rs b/src/libstd/io/net/ip.rs index e4f36764323..e29aa5e5ed1 100644 --- a/src/libstd/io/net/ip.rs +++ b/src/libstd/io/net/ip.rs @@ -55,7 +55,6 @@ pub struct SocketAddr { port: Port, } - impl fmt::Show for SocketAddr { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match self.ip { diff --git a/src/libstd/lib.rs b/src/libstd/lib.rs index d7fc85bc259..83d2ce53444 100644 --- a/src/libstd/lib.rs +++ b/src/libstd/lib.rs @@ -149,7 +149,6 @@ pub mod container; pub mod default; pub mod any; - /* Common data structures */ pub mod option; diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index 0415748097c..c095cec1e7d 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -107,7 +107,7 @@ pub struct SCTable { pub static EMPTY_CTXT : SyntaxContext = 0; pub static ILLEGAL_CTXT : SyntaxContext = 1; -#[deriving(Eq, Encodable, Decodable,Hash)] +#[deriving(Eq, Encodable, Decodable, Hash)] pub enum SyntaxContext_ { EmptyCtxt, Mark (Mrk,SyntaxContext), @@ -332,7 +332,7 @@ impl Eq for MetaItem_ { } } -#[deriving(Clone, Eq, Encodable, Decodable,Hash)] +#[deriving(Clone, Eq, Encodable, Decodable, Hash)] pub struct Block { view_items: ~[ViewItem], stmts: ~[@Stmt], @@ -467,7 +467,7 @@ pub enum Stmt_ { // FIXME (pending discussion of #1697, #2178...): local should really be // a refinement on pat. /// Local represents a `let` statement, e.g., `let : = ;` -#[deriving(Eq, Encodable, Decodable,Hash)] +#[deriving(Eq, Encodable, Decodable, Hash)] pub struct Local { ty: P, pat: @Pat, @@ -478,7 +478,7 @@ pub struct Local { pub type Decl = Spanned; -#[deriving(Eq, Encodable, Decodable,Hash)] +#[deriving(Eq, Encodable, Decodable, Hash)] pub enum Decl_ { // a local (let) binding: DeclLocal(@Local), @@ -514,7 +514,7 @@ pub enum UnsafeSource { UserProvided, } -#[deriving(Clone, Eq, Encodable, Decodable,Hash)] +#[deriving(Clone, Eq, Encodable, Decodable, Hash)] pub struct Expr { id: NodeId, node: Expr_, @@ -732,7 +732,7 @@ pub struct MutTy { mutbl: Mutability, } -#[deriving(Eq, Encodable, Decodable,Hash)] +#[deriving(Eq, Encodable, Decodable, Hash)] pub struct TypeField { ident: Ident, mt: MutTy, @@ -836,7 +836,7 @@ impl fmt::Show for Onceness { } } -#[deriving(Eq, Encodable, Decodable,Hash)] +#[deriving(Eq, Encodable, Decodable, Hash)] pub struct ClosureTy { sigil: Sigil, region: Option, @@ -967,7 +967,7 @@ pub enum ExplicitSelf_ { pub type ExplicitSelf = Spanned; -#[deriving(Eq, Encodable, Decodable,Hash)] +#[deriving(Eq, Encodable, Decodable, Hash)] pub struct Method { ident: Ident, attrs: ~[Attribute], @@ -987,7 +987,7 @@ pub struct Mod { items: ~[@Item], } -#[deriving(Clone, Eq, Encodable, Decodable,Hash)] +#[deriving(Clone, Eq, Encodable, Decodable, Hash)] pub struct ForeignMod { abis: AbiSet, view_items: ~[ViewItem], @@ -1074,14 +1074,14 @@ pub type Attribute = Spanned; // Distinguishes between Attributes that decorate items and Attributes that // are contained as statements within items. These two cases need to be // distinguished for pretty-printing. -#[deriving(Clone, Eq, Encodable, Decodable,Hash)] +#[deriving(Clone, Eq, Encodable, Decodable, Hash)] pub enum AttrStyle { AttrOuter, AttrInner, } // doc-comments are promoted to attributes that have is_sugared_doc = true -#[deriving(Clone, Eq, Encodable, Decodable,Hash)] +#[deriving(Clone, Eq, Encodable, Decodable, Hash)] pub struct Attribute_ { style: AttrStyle, value: @MetaItem, @@ -1095,13 +1095,13 @@ pub struct Attribute_ { If this impl is an ItemImpl, the impl_id is redundant (it could be the same as the impl's node id). */ -#[deriving(Clone, Eq, Encodable, Decodable,Hash)] +#[deriving(Clone, Eq, Encodable, Decodable, Hash)] pub struct TraitRef { path: Path, ref_id: NodeId, } -#[deriving(Clone, Eq, Encodable, Decodable,Hash)] +#[deriving(Clone, Eq, Encodable, Decodable, Hash)] pub enum Visibility { Public, Private, @@ -1117,7 +1117,7 @@ impl Visibility { } } -#[deriving(Clone, Eq, Encodable, Decodable,Hash)] +#[deriving(Clone, Eq, Encodable, Decodable, Hash)] pub struct StructField_ { kind: StructFieldKind, id: NodeId, @@ -1127,13 +1127,13 @@ pub struct StructField_ { pub type StructField = Spanned; -#[deriving(Clone, Eq, Encodable, Decodable,Hash)] +#[deriving(Clone, Eq, Encodable, Decodable, Hash)] pub enum StructFieldKind { NamedField(Ident, Visibility), UnnamedField // element of a tuple-like struct } -#[deriving(Eq, Encodable, Decodable,Hash)] +#[deriving(Eq, Encodable, Decodable, Hash)] pub struct StructDef { fields: ~[StructField], /* fields, not including ctor */ /* ID of the constructor. This is only used for tuple- or enum-like @@ -1173,7 +1173,7 @@ pub enum Item_ { ItemMac(Mac), } -#[deriving(Eq, Encodable, Decodable,Hash)] +#[deriving(Eq, Encodable, Decodable, Hash)] pub struct ForeignItem { ident: Ident, attrs: ~[Attribute], @@ -1183,7 +1183,7 @@ pub struct ForeignItem { vis: Visibility, } -#[deriving(Eq, Encodable, Decodable,Hash)] +#[deriving(Eq, Encodable, Decodable, Hash)] pub enum ForeignItem_ { ForeignItemFn(P, Generics), ForeignItemStatic(P, /* is_mutbl */ bool), @@ -1192,7 +1192,7 @@ pub enum ForeignItem_ { // The data we save and restore about an inlined item or method. This is not // part of the AST that we parse from a file, but it becomes part of the tree // that we trans. -#[deriving(Eq, Encodable, Decodable,Hash)] +#[deriving(Eq, Encodable, Decodable, Hash)] pub enum InlinedItem { IIItem(@Item), IIMethod(DefId /* impl id */, bool /* is provided */, @Method), diff --git a/src/libsyntax/codemap.rs b/src/libsyntax/codemap.rs index 83700e390cb..2b1e8a354ea 100644 --- a/src/libsyntax/codemap.rs +++ b/src/libsyntax/codemap.rs @@ -38,7 +38,7 @@ pub struct BytePos(u32); /// A character offset. Because of multibyte utf8 characters, a byte offset /// is not equivalent to a character offset. The CodeMap will convert BytePos /// values to CharPos values as necessary. -#[deriving(Eq,Hash, Ord)] +#[deriving(Eq, Hash, Ord)] pub struct CharPos(uint); // FIXME: Lots of boilerplate in these impls, but so far my attempts to fix diff --git a/src/libsyntax/util/interner.rs b/src/libsyntax/util/interner.rs index fa21924bbb9..4b5a05f4cb8 100644 --- a/src/libsyntax/util/interner.rs +++ b/src/libsyntax/util/interner.rs @@ -75,8 +75,7 @@ impl Interner { vect.get().len() } - pub fn find_equiv>(&self, val: &Q) - -> Option { + pub fn find_equiv>(&self, val: &Q) -> Option { let map = self.map.borrow(); match map.get().find_equiv(val) { Some(v) => Some(*v), @@ -207,8 +206,7 @@ impl StrInterner { vect.get().len() } - pub fn find_equiv>(&self, val: &Q) - -> Option { + pub fn find_equiv>(&self, val: &Q) -> Option { let map = self.map.borrow(); match map.get().find_equiv(val) { Some(v) => Some(*v), diff --git a/src/test/run-pass/deriving-via-extension-type-params.rs b/src/test/run-pass/deriving-via-extension-type-params.rs index c483816099d..2ea558b566e 100644 --- a/src/test/run-pass/deriving-via-extension-type-params.rs +++ b/src/test/run-pass/deriving-via-extension-type-params.rs @@ -10,8 +10,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -#[deriving(Eq)] -#[deriving(Hash)] +#[deriving(Eq, Hash)] struct Foo { x: int, y: T, From 848cbb4e130d7ec9609b36b3f6f7a68099cbd6a7 Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Sun, 23 Feb 2014 18:50:47 -0800 Subject: [PATCH 2/4] replace manual Hash impls with `#[deriving(Hash)]` --- src/libstd/ascii.rs | 10 +--------- src/libsyntax/abi.rs | 9 +-------- 2 files changed, 2 insertions(+), 17 deletions(-) diff --git a/src/libstd/ascii.rs b/src/libstd/ascii.rs index ac24a02c15b..d7b6ab25078 100644 --- a/src/libstd/ascii.rs +++ b/src/libstd/ascii.rs @@ -18,13 +18,12 @@ use str::OwnedStr; use container::Container; use cast; use fmt; -use hash::{Hash, sip}; use iter::Iterator; use vec::{ImmutableVector, MutableVector, Vector}; use option::{Option, Some, None}; /// Datatype to hold one ascii character. It wraps a `u8`, with the highest bit always zero. -#[deriving(Clone, Eq, Ord, TotalOrd, TotalEq)] +#[deriving(Clone, Eq, Ord, TotalOrd, TotalEq, Hash)] pub struct Ascii { priv chr: u8 } impl Ascii { @@ -306,13 +305,6 @@ impl IntoStr for ~[Ascii] { } } -impl Hash for Ascii { - #[inline] - fn hash(&self, s: &mut sip::SipState) { - self.to_byte().hash(s) - } -} - /// Trait to convert to an owned byte array by consuming self pub trait IntoBytes { /// Converts to an owned byte array by consuming self diff --git a/src/libsyntax/abi.rs b/src/libsyntax/abi.rs index 9349e5c8e98..c01f3721fad 100644 --- a/src/libsyntax/abi.rs +++ b/src/libsyntax/abi.rs @@ -8,14 +8,13 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use std::hash::{Hash, sip}; use std::fmt; use std::fmt::Show; #[deriving(Eq)] pub enum Os { OsWin32, OsMacos, OsLinux, OsAndroid, OsFreebsd, } -#[deriving(Eq)] +#[deriving(Eq, Hash)] pub enum Abi { // NB: This ordering MUST match the AbiDatas array below. // (This is ensured by the test indices_are_correct().) @@ -267,12 +266,6 @@ impl AbiSet { } } -impl Hash for Abi { - fn hash(&self, s: &mut sip::SipState) { - self.index().hash(s) - } -} - impl fmt::Show for Abi { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { self.data().name.fmt(f) From 922eb47a20c432e079444032f9ece9a5a39f2080 Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Sun, 23 Feb 2014 19:11:19 -0800 Subject: [PATCH 3/4] test: Use `#[deriving(Hash)]` in a couple tests --- src/test/run-pass/deriving-global.rs | 3 +++ src/test/run-pass/deriving-meta-multiple.rs | 6 ++++-- src/test/run-pass/deriving-meta.rs | 5 ++++- 3 files changed, 11 insertions(+), 3 deletions(-) diff --git a/src/test/run-pass/deriving-global.rs b/src/test/run-pass/deriving-global.rs index cce3575178e..af60a35cbf8 100644 --- a/src/test/run-pass/deriving-global.rs +++ b/src/test/run-pass/deriving-global.rs @@ -27,18 +27,21 @@ mod submod { // function calls, then being in a submodule will (correctly) // cause errors about unrecognised module `std` (or `extra`) #[deriving(Eq, Ord, TotalEq, TotalOrd, + Hash, Clone, DeepClone, Show, Rand, Encodable, Decodable)] enum A { A1(uint), A2(int) } #[deriving(Eq, Ord, TotalEq, TotalOrd, + Hash, Clone, DeepClone, Show, Rand, Encodable, Decodable)] struct B { x: uint, y: int } #[deriving(Eq, Ord, TotalEq, TotalOrd, + Hash, Clone, DeepClone, Show, Rand, Encodable, Decodable)] diff --git a/src/test/run-pass/deriving-meta-multiple.rs b/src/test/run-pass/deriving-meta-multiple.rs index 58ec8e35281..069f50f89f4 100644 --- a/src/test/run-pass/deriving-meta-multiple.rs +++ b/src/test/run-pass/deriving-meta-multiple.rs @@ -10,8 +10,9 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -#[deriving(Eq)] -#[deriving(Clone)] +use std::hash::hash; + +#[deriving(Eq, Clone, Hash)] struct Foo { bar: uint, baz: int @@ -22,4 +23,5 @@ pub fn main() { a == a; // check for Eq impl w/o testing its correctness a.clone(); // check for Clone impl w/o testing its correctness + hash(&a); // check for Hash impl w/o testing its correctness } diff --git a/src/test/run-pass/deriving-meta.rs b/src/test/run-pass/deriving-meta.rs index 7f31aa586cc..069f50f89f4 100644 --- a/src/test/run-pass/deriving-meta.rs +++ b/src/test/run-pass/deriving-meta.rs @@ -10,7 +10,9 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -#[deriving(Eq, Clone)] +use std::hash::hash; + +#[deriving(Eq, Clone, Hash)] struct Foo { bar: uint, baz: int @@ -21,4 +23,5 @@ pub fn main() { a == a; // check for Eq impl w/o testing its correctness a.clone(); // check for Clone impl w/o testing its correctness + hash(&a); // check for Hash impl w/o testing its correctness } From 6335a76b6eb473ea399b16fe9309db142d6b68ce Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Sun, 23 Feb 2014 19:24:27 -0800 Subject: [PATCH 4/4] rustc: rename `hash` to the proper name `state` --- src/librustc/middle/ty.rs | 56 +++++++++++++++++++-------------------- 1 file changed, 27 insertions(+), 29 deletions(-) diff --git a/src/librustc/middle/ty.rs b/src/librustc/middle/ty.rs index 34e2558638d..06efe832315 100644 --- a/src/librustc/middle/ty.rs +++ b/src/librustc/middle/ty.rs @@ -4925,13 +4925,11 @@ pub fn trait_method_of_method(tcx: ctxt, /// Creates a hash of the type `t` which will be the same no matter what crate /// context it's calculated within. This is used by the `type_id` intrinsic. pub fn hash_crate_independent(tcx: ctxt, t: t, local_hash: ~str) -> u64 { - use std::hash::{sip, Hash}; + let mut state = sip::SipState::new(0, 0); + macro_rules! byte( ($b:expr) => { ($b as u8).hash(&mut state) } ); + macro_rules! hash( ($e:expr) => { $e.hash(&mut state) } ); - let mut hash = sip::SipState::new(0, 0); - macro_rules! byte( ($b:expr) => { ($b as u8).hash(&mut hash) } ); - macro_rules! hash( ($e:expr) => { $e.hash(&mut hash) } ); - - let region = |_hash: &mut sip::SipState, r: Region| { + let region = |_state: &mut sip::SipState, r: Region| { match r { ReStatic => {} @@ -4945,27 +4943,27 @@ pub fn hash_crate_independent(tcx: ctxt, t: t, local_hash: ~str) -> u64 { } } }; - let vstore = |hash: &mut sip::SipState, v: vstore| { + let vstore = |state: &mut sip::SipState, v: vstore| { match v { - vstore_fixed(_) => 0u8.hash(hash), - vstore_uniq => 1u8.hash(hash), + vstore_fixed(_) => 0u8.hash(state), + vstore_uniq => 1u8.hash(state), vstore_slice(r) => { - 2u8.hash(hash); - region(hash, r); + 2u8.hash(state); + region(state, r); } } }; - let did = |hash: &mut sip::SipState, did: DefId| { + let did = |state: &mut sip::SipState, did: DefId| { let h = if ast_util::is_local(did) { local_hash.clone() } else { tcx.sess.cstore.get_crate_hash(did.krate) }; - h.as_bytes().hash(hash); - did.node.hash(hash); + h.as_bytes().hash(state); + did.node.hash(state); }; - let mt = |hash: &mut sip::SipState, mt: mt| { - mt.mutbl.hash(hash); + let mt = |state: &mut sip::SipState, mt: mt| { + mt.mutbl.hash(state); }; ty::walk_ty(t, |t| { match ty::get(t).sty { @@ -5001,17 +4999,17 @@ pub fn hash_crate_independent(tcx: ctxt, t: t, local_hash: ~str) -> u64 { } ty_vec(m, v) => { byte!(11); - mt(&mut hash, m); - vstore(&mut hash, v); + mt(&mut state, m); + vstore(&mut state, v); } ty_ptr(m) => { byte!(12); - mt(&mut hash, m); + mt(&mut state, m); } ty_rptr(r, m) => { byte!(13); - region(&mut hash, r); - mt(&mut hash, m); + region(&mut state, r); + mt(&mut state, m); } ty_bare_fn(ref b) => { byte!(14); @@ -5024,16 +5022,16 @@ pub fn hash_crate_independent(tcx: ctxt, t: t, local_hash: ~str) -> u64 { hash!(c.sigil); hash!(c.onceness); hash!(c.bounds); - region(&mut hash, c.region); + region(&mut state, c.region); } ty_trait(d, _, store, m, bounds) => { byte!(17); - did(&mut hash, d); + did(&mut state, d); match store { UniqTraitStore => byte!(0), RegionTraitStore(r) => { byte!(1) - region(&mut hash, r); + region(&mut state, r); } } hash!(m); @@ -5041,7 +5039,7 @@ pub fn hash_crate_independent(tcx: ctxt, t: t, local_hash: ~str) -> u64 { } ty_struct(d, _) => { byte!(18); - did(&mut hash, d); + did(&mut state, d); } ty_tup(ref inner) => { byte!(19); @@ -5050,22 +5048,22 @@ pub fn hash_crate_independent(tcx: ctxt, t: t, local_hash: ~str) -> u64 { ty_param(p) => { byte!(20); hash!(p.idx); - did(&mut hash, p.def_id); + did(&mut state, p.def_id); } ty_self(d) => { byte!(21); - did(&mut hash, d); + did(&mut state, d); } ty_infer(_) => unreachable!(), ty_err => byte!(23), ty_unboxed_vec(m) => { byte!(24); - mt(&mut hash, m); + mt(&mut state, m); } } }); - hash.result() + state.result() } impl Variance {