From 93d3b8aa6b681a8add60e4a22a5c41e7f7ce7557 Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Fri, 7 Sep 2012 19:04:40 -0700 Subject: [PATCH] Convert class methods to impl methods. Stop parsing class methods --- src/compiletest/procsrv.rs | 1 - src/libcore/pipes.rs | 85 +++++++++++-------- src/libstd/comm.rs | 8 +- src/libsyntax/ext/pipes/proto.rs | 3 + src/libsyntax/parse/parser.rs | 45 +++++----- src/rustc/middle/lang_items.rs | 3 + src/rustc/middle/resolve.rs | 36 +++++--- src/rustc/middle/typeck/check/method.rs | 3 + src/rustc/middle/typeck/coherence.rs | 3 + src/test/auxiliary/cci_class_2.rs | 4 +- src/test/auxiliary/cci_class_3.rs | 9 +- src/test/auxiliary/cci_class_4.rs | 20 +++-- src/test/auxiliary/cci_class_5.rs | 5 +- src/test/auxiliary/cci_class_6.rs | 11 +-- src/test/auxiliary/cci_class_cast.rs | 55 ++++++------ .../bench/task-perf-word-count-generic.rs | 6 +- src/test/compile-fail/assign-to-method.rs | 3 + src/test/compile-fail/class-cast-to-trait.rs | 27 ++++-- src/test/compile-fail/class-missing-self.rs | 5 +- src/test/compile-fail/issue-2356.rs | 5 ++ src/test/compile-fail/issue-2487-b.rs | 3 + src/test/compile-fail/issue-3021-b.rs | 3 + .../compile-fail/mutable-class-fields-2.rs | 2 + .../private-method-cross-crate.rs | 1 + src/test/compile-fail/private-method.rs | 12 ++- src/test/compile-fail/regions-addr-of-self.rs | 2 + .../regions-addr-of-upvar-self.rs | 2 + .../class-cast-to-trait-multiple-types.rs | 39 ++++++--- src/test/run-pass/class-cast-to-trait.rs | 23 +++-- src/test/run-pass/class-exports.rs | 2 + .../class-impl-very-parameterized-trait.rs | 23 +++-- .../class-implement-trait-cross-crate.rs | 29 ++++--- src/test/run-pass/class-implement-traits.rs | 21 +++-- src/test/run-pass/class-methods.rs | 3 + src/test/run-pass/class-poly-methods.rs | 2 + src/test/run-pass/class-separate-impl.rs | 20 +++-- src/test/run-pass/class-typarams.rs | 2 + src/test/run-pass/classes-simple-method.rs | 2 + src/test/run-pass/classes.rs | 20 +++-- src/test/run-pass/issue-2288.rs | 5 +- src/test/run-pass/issue-2311-2.rs | 3 + src/test/run-pass/issue-2445-b.rs | 3 + src/test/run-pass/issue-2445.rs | 3 + src/test/run-pass/issue-2487-a.rs | 3 + src/test/run-pass/issue-2502.rs | 2 + src/test/run-pass/issue-2718.rs | 6 ++ src/test/run-pass/issue-2936.rs | 5 +- src/test/run-pass/nested-class.rs | 3 + src/test/run-pass/private-class-field.rs | 2 + src/test/run-pass/private-method.rs | 7 +- src/test/run-pass/resource-destruct.rs | 5 +- src/test/run-pass/task-comm-6.rs | 2 +- 52 files changed, 400 insertions(+), 197 deletions(-) diff --git a/src/compiletest/procsrv.rs b/src/compiletest/procsrv.rs index f2e9e03d7db..1ab3bc55d9f 100644 --- a/src/compiletest/procsrv.rs +++ b/src/compiletest/procsrv.rs @@ -1,7 +1,6 @@ import run::spawn_process; import io::{ReaderUtil, WriterUtil}; import libc::{c_int, pid_t}; -import pipes::chan; export run; diff --git a/src/libcore/pipes.rs b/src/libcore/pipes.rs index 75716e642df..0ed0904b1b9 100644 --- a/src/libcore/pipes.rs +++ b/src/libcore/pipes.rs @@ -164,7 +164,17 @@ struct PacketHeader { // This is a reinterpret_cast of a ~buffer, that can also be cast // to a buffer_header if need be. mut buffer: *libc::c_void, +} +fn PacketHeader() -> PacketHeader { + PacketHeader { + state: Empty, + blocked_task: ptr::null(), + buffer: ptr::null() + } +} + +impl PacketHeader { // Returns the old state. unsafe fn mark_blocked(this: *rust_task) -> State { rustrt::rust_task_ref(this); @@ -196,14 +206,6 @@ struct PacketHeader { } } -fn PacketHeader() -> PacketHeader { - PacketHeader { - state: Empty, - blocked_task: ptr::null(), - buffer: ptr::null() - } -} - #[doc(hidden)] type Packet = { header: PacketHeader, @@ -794,6 +796,21 @@ struct SendPacketBuffered { // "none" // } else { "some" }); } } +} + +fn SendPacketBuffered(p: *Packet) + -> SendPacketBuffered { + //debug!("take send %?", p); + SendPacketBuffered { + p: Some(p), + buffer: unsafe { + Some(BufferResource( + get_buffer(ptr::addr_of((*p).header)))) + } + } +} + +impl SendPacketBuffered { fn unwrap() -> *Packet { let mut p = None; p <-> self.p; @@ -820,18 +837,6 @@ struct SendPacketBuffered { } } -fn SendPacketBuffered(p: *Packet) - -> SendPacketBuffered { - //debug!("take send %?", p); - SendPacketBuffered { - p: Some(p), - buffer: unsafe { - Some(BufferResource( - get_buffer(ptr::addr_of((*p).header)))) - } - } -} - // XXX remove me #[cfg(stage0)] #[allow(non_camel_case_types)] @@ -858,7 +863,7 @@ fn recv_packet(p: *packet) -> RecvPacket { RecvPacket(p) } -struct RecvPacketBuffered : Selectable { +struct RecvPacketBuffered { mut p: Option<*Packet>, mut buffer: Option>, drop { @@ -875,6 +880,9 @@ struct RecvPacketBuffered : Selectable { // "none" // } else { "some" }); } } +} + +impl RecvPacketBuffered : Selectable { fn unwrap() -> *Packet { let mut p = None; p <-> self.p; @@ -1095,9 +1103,27 @@ impl Port: Recv { } } +impl Port: Selectable { + pure fn header() -> *PacketHeader unchecked { + match self.endp { + Some(endp) => endp.header(), + None => fail ~"peeking empty stream" + } + } +} + /// Treat many ports as one. -struct PortSet : Recv { +struct PortSet { mut ports: ~[pipes::Port], +} + +fn PortSet() -> PortSet{ + PortSet { + ports: ~[] + } +} + +impl PortSet : Recv { fn add(+port: pipes::Port) { vec::push(self.ports, move port) @@ -1145,21 +1171,6 @@ struct PortSet : Recv { } } -fn PortSet() -> PortSet{ - PortSet { - ports: ~[] - } -} - -impl Port: Selectable { - pure fn header() -> *PacketHeader unchecked { - match self.endp { - Some(endp) => endp.header(), - None => fail ~"peeking empty stream" - } - } -} - /// A channel that can be shared between many senders. type SharedChan = unsafe::Exclusive>; diff --git a/src/libstd/comm.rs b/src/libstd/comm.rs index d78e11452bc..3302b4955bb 100644 --- a/src/libstd/comm.rs +++ b/src/libstd/comm.rs @@ -13,10 +13,12 @@ use pipes::{Channel, Recv, Chan, Port, Selectable}; export DuplexStream; /// An extension of `pipes::stream` that allows both sending and receiving. -struct DuplexStream : Channel, Recv, Selectable { +struct DuplexStream { priv chan: Chan, priv port: Port , +} +impl DuplexStream : Channel { fn send(+x: T) { self.chan.send(x) } @@ -24,7 +26,9 @@ struct DuplexStream : Channel, Recv, Selectable { fn try_send(+x: T) -> bool { self.chan.try_send(x) } +} +impl DuplexStream : Recv { fn recv() -> U { self.port.recv() } @@ -36,7 +40,9 @@ struct DuplexStream : Channel, Recv, Selectable { pure fn peek() -> bool { self.port.peek() } +} +impl DuplexStream : Selectable { pure fn header() -> *pipes::PacketHeader { self.port.header() } diff --git a/src/libsyntax/ext/pipes/proto.rs b/src/libsyntax/ext/pipes/proto.rs index cb470aee7f3..1f674bb5395 100644 --- a/src/libsyntax/ext/pipes/proto.rs +++ b/src/libsyntax/ext/pipes/proto.rs @@ -134,6 +134,9 @@ struct protocol_ { states: DVec, mut bounded: Option, +} + +impl protocol_ { /// Get a state. fn get_state(name: ~str) -> state { diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 138b83f69f0..512a5e6459e 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -242,7 +242,9 @@ struct parser { obsolete_set: hashmap, drop {} /* do not copy the parser; its state is tied to outside state */ +} +impl parser { fn bump() { self.last_span = self.span; let next = if self.buffer_start == self.buffer_end { @@ -2776,32 +2778,25 @@ struct parser { let obsolete_let = self.eat_obsolete_ident("let"); if obsolete_let { self.obsolete(copy self.last_span, ObsoleteLet) } - if (obsolete_let || self.token_is_keyword(~"mut", copy self.token) || - !self.is_any_keyword(copy self.token)) && - !self.token_is_pound_or_doc_comment(self.token) { - let a_var = self.parse_instance_var(vis); - match self.token { - token::SEMI => { - self.obsolete(copy self.span, ObsoleteFieldTerminator); - self.bump(); - } - token::COMMA => { - self.bump(); - } - token::RBRACE => {} - _ => { - self.span_fatal(copy self.span, - fmt!("expected `;`, `,`, or '}' but \ - found `%s`", - token_to_str(self.reader, - self.token))); - } - } - return a_var; - } else { - let m = self.parse_method(vis); - return @method_member(m); + let a_var = self.parse_instance_var(vis); + match self.token { + token::SEMI => { + self.obsolete(copy self.span, ObsoleteFieldTerminator); + self.bump(); + } + token::COMMA => { + self.bump(); + } + token::RBRACE => {} + _ => { + self.span_fatal(copy self.span, + fmt!("expected `;`, `,`, or '}' but \ + found `%s`", + token_to_str(self.reader, + self.token))); + } } + return a_var; } fn parse_dtor(attrs: ~[attribute]) -> class_contents { diff --git a/src/rustc/middle/lang_items.rs b/src/rustc/middle/lang_items.rs index 084c2e82b3d..fa47f7a2169 100644 --- a/src/rustc/middle/lang_items.rs +++ b/src/rustc/middle/lang_items.rs @@ -120,6 +120,9 @@ struct LanguageItemCollector { session: session, item_refs: hashmap<~str,&mut Option>, +} + +impl LanguageItemCollector { fn match_and_collect_meta_item(item_def_id: def_id, meta_item: meta_item) { diff --git a/src/rustc/middle/resolve.rs b/src/rustc/middle/resolve.rs index b85b10e50e8..c99afbd6992 100644 --- a/src/rustc/middle/resolve.rs +++ b/src/rustc/middle/resolve.rs @@ -379,14 +379,6 @@ struct ImportResolution { mut type_target: Option, mut used: bool, - - fn target_for_namespace(namespace: Namespace) -> Option { - match namespace { - ModuleNS => return copy self.module_target, - TypeNS => return copy self.type_target, - ValueNS => return copy self.value_target - } - } } fn ImportResolution(span: span) -> ImportResolution { @@ -400,6 +392,16 @@ fn ImportResolution(span: span) -> ImportResolution { } } +impl ImportResolution { + fn target_for_namespace(namespace: Namespace) -> Option { + match namespace { + ModuleNS => return copy self.module_target, + TypeNS => return copy self.type_target, + ValueNS => return copy self.value_target + } + } +} + /// The link from a module up to its nearest parent node. enum ParentLink { NoParentLink, @@ -448,10 +450,6 @@ struct Module { // The index of the import we're resolving. mut resolved_import_count: uint, - - fn all_imports_resolved() -> bool { - return self.imports.len() == self.resolved_import_count; - } } fn Module(parent_link: ParentLink, def_id: Option) -> Module { @@ -468,6 +466,12 @@ fn Module(parent_link: ParentLink, def_id: Option) -> Module { } } +impl Module { + fn all_imports_resolved() -> bool { + return self.imports.len() == self.resolved_import_count; + } +} + // XXX: This is a workaround due to is_none in the standard library mistakenly // requiring a T:copy. @@ -518,6 +522,9 @@ struct NameBindings { mut module_span: Option, mut type_span: Option, mut value_span: Option, +} + +impl NameBindings { /// Creates a new module in this set of name bindings. fn define_module(parent_link: ParentLink, def_id: Option, @@ -627,7 +634,9 @@ fn NameBindings() -> NameBindings { /// Interns the names of the primitive types. struct PrimitiveTypeTable { primitive_types: hashmap, +} +impl PrimitiveTypeTable { fn intern(intr: ident_interner, string: @~str, primitive_type: prim_ty) { let atom = intr.intern(string); @@ -773,6 +782,9 @@ struct Resolver { export_map: ExportMap, export_map2: ExportMap2, trait_map: TraitMap, +} + +impl Resolver { /// The main name resolution procedure. fn resolve(@self, this: @Resolver) { diff --git a/src/rustc/middle/typeck/check/method.rs b/src/rustc/middle/typeck/check/method.rs index a1c5af226bf..6694160e98e 100644 --- a/src/rustc/middle/typeck/check/method.rs +++ b/src/rustc/middle/typeck/check/method.rs @@ -105,6 +105,9 @@ struct lookup { candidate_impls: hashmap, supplied_tps: ~[ty::t], include_private: bool, +} + +impl lookup { // Entrypoint: fn method() -> Option { diff --git a/src/rustc/middle/typeck/coherence.rs b/src/rustc/middle/typeck/coherence.rs index b6fcb0c385f..a75ec2c83dc 100644 --- a/src/rustc/middle/typeck/coherence.rs +++ b/src/rustc/middle/typeck/coherence.rs @@ -158,6 +158,9 @@ struct CoherenceChecker { // implementations that are defined in the same scope as their base types. privileged_implementations: hashmap, +} + +impl CoherenceChecker { // Create a mapping containing a MethodInfo for every provided // method in every trait. diff --git a/src/test/auxiliary/cci_class_2.rs b/src/test/auxiliary/cci_class_2.rs index b0fffc06cb5..30b57e5fbff 100644 --- a/src/test/auxiliary/cci_class_2.rs +++ b/src/test/auxiliary/cci_class_2.rs @@ -7,9 +7,11 @@ struct cat { how_hungry : int, - fn speak() {} } + impl cat { + fn speak() {} + } fn cat(in_x : uint, in_y : int) -> cat { cat { meows: in_x, diff --git a/src/test/auxiliary/cci_class_3.rs b/src/test/auxiliary/cci_class_3.rs index 3395de71e8a..53f1a356894 100644 --- a/src/test/auxiliary/cci_class_3.rs +++ b/src/test/auxiliary/cci_class_3.rs @@ -6,12 +6,13 @@ struct cat { } how_hungry : int, - - fn speak() { self.meows += 1u; } - fn meow_count() -> uint { self.meows } - } + impl cat { + fn speak() { self.meows += 1u; } + fn meow_count() -> uint { self.meows } + } + fn cat(in_x : uint, in_y : int) -> cat { cat { meows: in_x, diff --git a/src/test/auxiliary/cci_class_4.rs b/src/test/auxiliary/cci_class_4.rs index 3fab6722da4..585fc09e320 100644 --- a/src/test/auxiliary/cci_class_4.rs +++ b/src/test/auxiliary/cci_class_4.rs @@ -3,17 +3,13 @@ mod kitties { struct cat { priv { mut meows : uint, - fn meow() { - error!("Meow"); - self.meows += 1u; - if self.meows % 5u == 0u { - self.how_hungry += 1; - } - } } mut how_hungry : int, name : ~str, +} + +impl cat { fn speak() { self.meow(); } @@ -30,6 +26,16 @@ struct cat { } } + priv impl cat { + fn meow() { + error!("Meow"); + self.meows += 1u; + if self.meows % 5u == 0u { + self.how_hungry += 1; + } + } + } + fn cat(in_x : uint, in_y : int, in_name: ~str) -> cat { cat { meows: in_x, diff --git a/src/test/auxiliary/cci_class_5.rs b/src/test/auxiliary/cci_class_5.rs index 4f758e74ed8..f2c34722bd2 100644 --- a/src/test/auxiliary/cci_class_5.rs +++ b/src/test/auxiliary/cci_class_5.rs @@ -3,13 +3,16 @@ mod kitties { struct cat { priv { mut meows : uint, - fn nap() { for uint::range(1u, 10000u) |_i|{}} } how_hungry : int, } + impl cat { + priv fn nap() { for uint::range(1u, 10000u) |_i|{}} + } + fn cat(in_x : uint, in_y : int) -> cat { cat { meows: in_x, diff --git a/src/test/auxiliary/cci_class_6.rs b/src/test/auxiliary/cci_class_6.rs index 31506550099..272c381c73d 100644 --- a/src/test/auxiliary/cci_class_6.rs +++ b/src/test/auxiliary/cci_class_6.rs @@ -7,13 +7,14 @@ struct cat { } how_hungry : int, - - fn speak(stuff: ~[T]) { - self.meows += stuff.len(); - } - fn meow_count() -> uint { self.meows } } + impl cat { + fn speak(stuff: ~[T]) { + self.meows += stuff.len(); + } + fn meow_count() -> uint { self.meows } + } fn cat(in_x : uint, in_y : int, -in_info: ~[U]) -> cat { cat { diff --git a/src/test/auxiliary/cci_class_cast.rs b/src/test/auxiliary/cci_class_cast.rs index 0385ea991a2..56cf40fd968 100644 --- a/src/test/auxiliary/cci_class_cast.rs +++ b/src/test/auxiliary/cci_class_cast.rs @@ -3,38 +3,45 @@ use to_str::ToStr; mod kitty { -struct cat : ToStr { +struct cat { priv { mut meows : uint, - fn meow() { - error!("Meow"); - self.meows += 1u; - if self.meows % 5u == 0u { - self.how_hungry += 1; - } - } } mut how_hungry : int, name : ~str, - - fn speak() { self.meow(); } - - fn eat() -> bool { - if self.how_hungry > 0 { - error!("OM NOM NOM"); - self.how_hungry -= 2; - return true; - } - else { - error!("Not hungry!"); - return false; - } - } - - fn to_str() -> ~str { self.name } } + impl cat : ToStr { + fn to_str() -> ~str { self.name } + } + + priv impl cat { + fn meow() { + error!("Meow"); + self.meows += 1u; + if self.meows % 5u == 0u { + self.how_hungry += 1; + } + } + + } + + impl cat { + fn speak() { self.meow(); } + + fn eat() -> bool { + if self.how_hungry > 0 { + error!("OM NOM NOM"); + self.how_hungry -= 2; + return true; + } + else { + error!("Not hungry!"); + return false; + } + } + } fn cat(in_x : uint, in_y : int, in_name: ~str) -> cat { cat { meows: in_x, diff --git a/src/test/bench/task-perf-word-count-generic.rs b/src/test/bench/task-perf-word-count-generic.rs index a4dc2539eb3..a2d816ec369 100644 --- a/src/test/bench/task-perf-word-count-generic.rs +++ b/src/test/bench/task-perf-word-count-generic.rs @@ -94,7 +94,9 @@ fn reduce(&&word: ~str, get: map_reduce::getter) { struct box { mut contents: Option, +} +impl box { fn swap(f: fn(+T) -> T) { let mut tmp = None; self.contents <-> tmp; @@ -344,10 +346,12 @@ fn is_word_char(c: char) -> bool { char::is_alphabetic(c) || char::is_digit(c) || c == '_' } -struct random_word_reader: word_reader { +struct random_word_reader { mut remaining: uint, rng: rand::Rng, +} +impl random_word_reader: word_reader { fn read_word() -> Option<~str> { if self.remaining > 0 { self.remaining -= 1; diff --git a/src/test/compile-fail/assign-to-method.rs b/src/test/compile-fail/assign-to-method.rs index d59a1c8448e..4d63180a843 100644 --- a/src/test/compile-fail/assign-to-method.rs +++ b/src/test/compile-fail/assign-to-method.rs @@ -4,6 +4,9 @@ struct cat { } how_hungry : int, +} + +impl cat { fn speak() { self.meows += 1u; } } diff --git a/src/test/compile-fail/class-cast-to-trait.rs b/src/test/compile-fail/class-cast-to-trait.rs index fa819091985..58c0bfab799 100644 --- a/src/test/compile-fail/class-cast-to-trait.rs +++ b/src/test/compile-fail/class-cast-to-trait.rs @@ -3,22 +3,16 @@ trait noisy { fn speak(); } -struct cat : noisy { +struct cat { priv { mut meows : uint, - fn meow() { - error!("Meow"); - self.meows += 1u; - if self.meows % 5u == 0u { - self.how_hungry += 1; - } - } } mut how_hungry : int, name : str, +} - fn speak() { self.meow(); } +impl cat { fn eat() -> bool { if self.how_hungry > 0 { @@ -33,6 +27,21 @@ struct cat : noisy { } } +impl cat : noisy { + fn speak() { self.meow(); } + +} + +priv impl cat { + fn meow() { + error!("Meow"); + self.meows += 1u; + if self.meows % 5u == 0u { + self.how_hungry += 1; + } + } +} + fn cat(in_x : uint, in_y : int, in_name: str) -> cat { cat { meows: in_x, diff --git a/src/test/compile-fail/class-missing-self.rs b/src/test/compile-fail/class-missing-self.rs index 440152a9288..48a8c7a56b9 100644 --- a/src/test/compile-fail/class-missing-self.rs +++ b/src/test/compile-fail/class-missing-self.rs @@ -1,13 +1,16 @@ struct cat { priv { mut meows : uint, + } +} + +priv impl cat { fn sleep() { loop{} } fn meow() { error!("Meow"); meows += 1u; //~ ERROR unresolved name sleep(); //~ ERROR unresolved name } - } } diff --git a/src/test/compile-fail/issue-2356.rs b/src/test/compile-fail/issue-2356.rs index d0c265eb4ee..0335f669895 100644 --- a/src/test/compile-fail/issue-2356.rs +++ b/src/test/compile-fail/issue-2356.rs @@ -1,4 +1,9 @@ +// xfail-test Resolve code for classes knew how to do this, impls don't + struct cat { tail: int, +} + +impl cat { fn meow() { tail += 1; } //~ ERROR: Did you mean: `self.tail` } diff --git a/src/test/compile-fail/issue-2487-b.rs b/src/test/compile-fail/issue-2487-b.rs index 011c573b9b0..acf85de8bd7 100644 --- a/src/test/compile-fail/issue-2487-b.rs +++ b/src/test/compile-fail/issue-2487-b.rs @@ -2,6 +2,9 @@ struct socket { sock: int, drop { } +} + +impl socket { fn set_identity() { do closure { diff --git a/src/test/compile-fail/issue-3021-b.rs b/src/test/compile-fail/issue-3021-b.rs index 32b50b4385e..2dacbe314e2 100644 --- a/src/test/compile-fail/issue-3021-b.rs +++ b/src/test/compile-fail/issue-3021-b.rs @@ -4,6 +4,9 @@ fn siphash(k0 : u64) { struct siphash { mut v0: u64, + } + + impl siphash { fn reset() { self.v0 = k0 ^ 0x736f6d6570736575; //~ ERROR attempted dynamic environment-capture //~^ ERROR unresolved name: k0 diff --git a/src/test/compile-fail/mutable-class-fields-2.rs b/src/test/compile-fail/mutable-class-fields-2.rs index f2917044e70..68eaffc7f70 100644 --- a/src/test/compile-fail/mutable-class-fields-2.rs +++ b/src/test/compile-fail/mutable-class-fields-2.rs @@ -5,7 +5,9 @@ struct cat { } how_hungry : int, +} +impl cat { fn eat() { self.how_hungry -= 5; } diff --git a/src/test/compile-fail/private-method-cross-crate.rs b/src/test/compile-fail/private-method-cross-crate.rs index 3cf5e5da75a..2d258e58b84 100644 --- a/src/test/compile-fail/private-method-cross-crate.rs +++ b/src/test/compile-fail/private-method-cross-crate.rs @@ -1,5 +1,6 @@ // error-pattern:attempted access of field `nap` on type // xfail-fast +// xfail-test // aux-build:cci_class_5.rs use cci_class_5; use cci_class_5::kitties::*; diff --git a/src/test/compile-fail/private-method.rs b/src/test/compile-fail/private-method.rs index 545843762be..d9f070fb5bd 100644 --- a/src/test/compile-fail/private-method.rs +++ b/src/test/compile-fail/private-method.rs @@ -1,22 +1,28 @@ -// error-pattern:call to private method not allowed +// error-pattern:method `nap` is private + +mod kitties { struct cat { priv { mut meows : uint, - fn nap() { uint::range(1u, 10000u, |_i|{})} } how_hungry : int, } +impl cat { + priv fn nap() { uint::range(1u, 10000u, |_i| false)} +} + fn cat(in_x : uint, in_y : int) -> cat { cat { meows: in_x, how_hungry: in_y } } +} fn main() { - let nyan : cat = cat(52u, 99); + let nyan : kitties::cat = kitties::cat(52u, 99); nyan.nap(); } diff --git a/src/test/compile-fail/regions-addr-of-self.rs b/src/test/compile-fail/regions-addr-of-self.rs index 54fad53190e..67e828762f1 100644 --- a/src/test/compile-fail/regions-addr-of-self.rs +++ b/src/test/compile-fail/regions-addr-of-self.rs @@ -1,6 +1,8 @@ struct dog { mut cats_chased: uint, +} +impl dog { fn chase_cat() { let p: &static/mut uint = &mut self.cats_chased; //~ ERROR illegal borrow *p += 1u; diff --git a/src/test/compile-fail/regions-addr-of-upvar-self.rs b/src/test/compile-fail/regions-addr-of-upvar-self.rs index 4e51301827d..a641d929f77 100644 --- a/src/test/compile-fail/regions-addr-of-upvar-self.rs +++ b/src/test/compile-fail/regions-addr-of-upvar-self.rs @@ -1,6 +1,8 @@ struct dog { mut food: uint, +} +impl dog { fn chase_cat() { for uint::range(0u, 10u) |_i| { let p: &static/mut uint = &mut self.food; //~ ERROR illegal borrow diff --git a/src/test/run-pass/class-cast-to-trait-multiple-types.rs b/src/test/run-pass/class-cast-to-trait-multiple-types.rs index aa0268afbed..f2e35c4cd55 100644 --- a/src/test/run-pass/class-cast-to-trait-multiple-types.rs +++ b/src/test/run-pass/class-cast-to-trait-multiple-types.rs @@ -2,10 +2,16 @@ trait noisy { fn speak() -> int; } -struct dog : noisy { +struct dog { priv { barks : @mut uint, - fn bark() -> int { + } + + volume : @mut int, +} + +impl dog { + priv fn bark() -> int { debug!("Woof %u %d", *self.barks, *self.volume); *self.barks += 1u; if *self.barks % 3u == 0u { @@ -17,10 +23,9 @@ struct dog : noisy { debug!("Grrr %u %d", *self.barks, *self.volume); *self.volume } - } - - volume : @mut int, +} +impl dog : noisy { fn speak() -> int { self.bark() } } @@ -31,9 +36,24 @@ fn dog() -> dog { } } -struct cat : noisy { +struct cat { priv { meows : @mut uint, + } + + how_hungry : @mut int, + name : ~str, +} + +impl cat : noisy { + fn speak() -> int { self.meow() as int } +} + +impl cat { + fn meow_count() -> uint { *self.meows } +} + +priv impl cat { fn meow() -> uint { debug!("Meow"); *self.meows += 1u; @@ -42,13 +62,6 @@ struct cat : noisy { } *self.meows } - } - - how_hungry : @mut int, - name : ~str, - - fn speak() -> int { self.meow() as int } - fn meow_count() -> uint { *self.meows } } fn cat(in_x : uint, in_y : int, in_name: ~str) -> cat { diff --git a/src/test/run-pass/class-cast-to-trait.rs b/src/test/run-pass/class-cast-to-trait.rs index 5be8454d65b..422dd6142f0 100644 --- a/src/test/run-pass/class-cast-to-trait.rs +++ b/src/test/run-pass/class-cast-to-trait.rs @@ -2,23 +2,20 @@ trait noisy { fn speak(); } -struct cat : noisy { +struct cat { priv { mut meows : uint, - fn meow() { - error!("Meow"); - self.meows += 1u; - if self.meows % 5u == 0u { - self.how_hungry += 1; - } - } } mut how_hungry : int, name : ~str, +} +impl cat : noisy { fn speak() { self.meow(); } +} +impl cat { fn eat() -> bool { if self.how_hungry > 0 { error!("OM NOM NOM"); @@ -32,6 +29,16 @@ struct cat : noisy { } } +priv impl cat { + fn meow() { + error!("Meow"); + self.meows += 1u; + if self.meows % 5u == 0u { + self.how_hungry += 1; + } + } +} + fn cat(in_x : uint, in_y : int, in_name: ~str) -> cat { cat { meows: in_x, diff --git a/src/test/run-pass/class-exports.rs b/src/test/run-pass/class-exports.rs index 46b93b2f524..ef036811976 100644 --- a/src/test/run-pass/class-exports.rs +++ b/src/test/run-pass/class-exports.rs @@ -8,7 +8,9 @@ mod kitty { struct cat { meows: uint, name: ~str, + } + impl cat { fn get_name() -> ~str { self.name } } diff --git a/src/test/run-pass/class-impl-very-parameterized-trait.rs b/src/test/run-pass/class-impl-very-parameterized-trait.rs index 665ee3cfe25..ce6074ad4a7 100644 --- a/src/test/run-pass/class-impl-very-parameterized-trait.rs +++ b/src/test/run-pass/class-impl-very-parameterized-trait.rs @@ -14,22 +14,17 @@ impl cat_type : cmp::Eq { // for any int value that's less than the meows field // ok: T should be in scope when resolving the trait ref for map -struct cat : map { +struct cat { priv { // Yes, you can have negative meows mut meows : int, - fn meow() { - self.meows += 1; - error!("Meow %d", self.meows); - if self.meows % 5 == 0 { - self.how_hungry += 1; - } - } } mut how_hungry : int, name : T, +} +impl cat { fn speak() { self.meow(); } fn eat() -> bool { @@ -43,7 +38,9 @@ struct cat : map { return false; } } +} +impl cat : map { pure fn size() -> uint { self.meows as uint } fn insert(+k: int, +_v: T) -> bool { self.meows += k; @@ -94,6 +91,16 @@ struct cat : map { fn clear() { } } +priv impl cat { + fn meow() { + self.meows += 1; + error!("Meow %d", self.meows); + if self.meows % 5 == 0 { + self.how_hungry += 1; + } + } +} + fn cat(in_x : int, in_y : int, in_name: T) -> cat { cat { meows: in_x, diff --git a/src/test/run-pass/class-implement-trait-cross-crate.rs b/src/test/run-pass/class-implement-trait-cross-crate.rs index 0551ebc62d4..a1c2b420c46 100644 --- a/src/test/run-pass/class-implement-trait-cross-crate.rs +++ b/src/test/run-pass/class-implement-trait-cross-crate.rs @@ -3,23 +3,16 @@ use cci_class_trait; use cci_class_trait::animals::*; -struct cat : noisy { +struct cat { priv { mut meows : uint, - fn meow() { - error!("Meow"); - self.meows += 1u; - if self.meows % 5u == 0u { - self.how_hungry += 1; - } - } } mut how_hungry : int, name : ~str, +} - fn speak() { self.meow(); } - +impl cat { fn eat() -> bool { if self.how_hungry > 0 { error!("OM NOM NOM"); @@ -33,6 +26,22 @@ struct cat : noisy { } } +impl cat : noisy { + + fn speak() { self.meow(); } + +} + +priv impl cat { + fn meow() { + error!("Meow"); + self.meows += 1u; + if self.meows % 5u == 0u { + self.how_hungry += 1; + } + } +} + fn cat(in_x : uint, in_y : int, in_name: ~str) -> cat { cat { meows: in_x, diff --git a/src/test/run-pass/class-implement-traits.rs b/src/test/run-pass/class-implement-traits.rs index 1e40ae872ad..e31501d02f5 100644 --- a/src/test/run-pass/class-implement-traits.rs +++ b/src/test/run-pass/class-implement-traits.rs @@ -2,9 +2,16 @@ trait noisy { fn speak(); } -struct cat : noisy { +struct cat { priv { mut meows : uint, + } + + mut how_hungry : int, + name : ~str, +} + +priv impl cat { fn meow() { error!("Meow"); self.meows += 1u; @@ -12,13 +19,9 @@ struct cat : noisy { self.how_hungry += 1; } } - } - - mut how_hungry : int, - name : ~str, - - fn speak() { self.meow(); } +} +impl cat { fn eat() -> bool { if self.how_hungry > 0 { error!("OM NOM NOM"); @@ -32,6 +35,10 @@ struct cat : noisy { } } +impl cat : noisy { + fn speak() { self.meow(); } +} + fn cat(in_x : uint, in_y : int, in_name: ~str) -> cat { cat { meows: in_x, diff --git a/src/test/run-pass/class-methods.rs b/src/test/run-pass/class-methods.rs index e95b1dbc91c..d5cee133097 100644 --- a/src/test/run-pass/class-methods.rs +++ b/src/test/run-pass/class-methods.rs @@ -4,6 +4,9 @@ struct cat { } how_hungry : int, +} + +impl cat { fn speak() { self.meows += 1u; } fn meow_count() -> uint { self.meows } diff --git a/src/test/run-pass/class-poly-methods.rs b/src/test/run-pass/class-poly-methods.rs index 586dd3e7361..0f07b36e6c1 100644 --- a/src/test/run-pass/class-poly-methods.rs +++ b/src/test/run-pass/class-poly-methods.rs @@ -5,7 +5,9 @@ struct cat { } how_hungry : int, +} +impl cat { fn speak(stuff: ~[T]) { self.meows += stuff.len(); } diff --git a/src/test/run-pass/class-separate-impl.rs b/src/test/run-pass/class-separate-impl.rs index dcbaffc2c30..47fd98b49a8 100644 --- a/src/test/run-pass/class-separate-impl.rs +++ b/src/test/run-pass/class-separate-impl.rs @@ -5,17 +5,13 @@ use to_str::ToStr; struct cat { priv { mut meows : uint, - fn meow() { - error!("Meow"); - self.meows += 1u; - if self.meows % 5u == 0u { - self.how_hungry += 1; - } - } } mut how_hungry : int, name : ~str, +} + +impl cat { fn speak() { self.meow(); } @@ -32,6 +28,16 @@ struct cat { } } +priv impl cat { + fn meow() { + error!("Meow"); + self.meows += 1u; + if self.meows % 5u == 0u { + self.how_hungry += 1; + } + } +} + fn cat(in_x : uint, in_y : int, in_name: ~str) -> cat { cat { meows: in_x, diff --git a/src/test/run-pass/class-typarams.rs b/src/test/run-pass/class-typarams.rs index e736ac7308a..d11b1761b3c 100644 --- a/src/test/run-pass/class-typarams.rs +++ b/src/test/run-pass/class-typarams.rs @@ -4,7 +4,9 @@ struct cat { } how_hungry : int, +} +impl cat { fn speak() { self.meows += 1u; } diff --git a/src/test/run-pass/classes-simple-method.rs b/src/test/run-pass/classes-simple-method.rs index f1dc265c953..4cee896f23a 100644 --- a/src/test/run-pass/classes-simple-method.rs +++ b/src/test/run-pass/classes-simple-method.rs @@ -4,7 +4,9 @@ struct cat { } how_hungry : int, +} +impl cat { fn speak() {} } diff --git a/src/test/run-pass/classes.rs b/src/test/run-pass/classes.rs index bf58e9fe35f..0ade8e57f9c 100644 --- a/src/test/run-pass/classes.rs +++ b/src/test/run-pass/classes.rs @@ -1,17 +1,13 @@ struct cat { priv { mut meows : uint, - fn meow() { - error!("Meow"); - self.meows += 1u; - if self.meows % 5u == 0u { - self.how_hungry += 1; - } - } } mut how_hungry : int, name : ~str, +} + +impl cat { fn speak() { self.meow(); } @@ -28,6 +24,16 @@ struct cat { } } +priv impl cat { + fn meow() { + error!("Meow"); + self.meows += 1u; + if self.meows % 5u == 0u { + self.how_hungry += 1; + } + } +} + fn cat(in_x : uint, in_y : int, in_name: ~str) -> cat { cat { meows: in_x, diff --git a/src/test/run-pass/issue-2288.rs b/src/test/run-pass/issue-2288.rs index f4d045bb444..fb88ee6e8ff 100644 --- a/src/test/run-pass/issue-2288.rs +++ b/src/test/run-pass/issue-2288.rs @@ -1,8 +1,11 @@ trait clam { fn chowder(y: A); } -struct foo : clam { +struct foo { x: A, +} + +impl foo : clam { fn chowder(y: A) { } } diff --git a/src/test/run-pass/issue-2311-2.rs b/src/test/run-pass/issue-2311-2.rs index ef3752d5fa7..9c3ea2b2b7e 100644 --- a/src/test/run-pass/issue-2311-2.rs +++ b/src/test/run-pass/issue-2311-2.rs @@ -1,6 +1,9 @@ trait clam { } struct foo { x: A, +} + +impl foo { fn bar>(c: C) -> B { fail; } diff --git a/src/test/run-pass/issue-2445-b.rs b/src/test/run-pass/issue-2445-b.rs index 0b650286020..f52cec96f44 100644 --- a/src/test/run-pass/issue-2445-b.rs +++ b/src/test/run-pass/issue-2445-b.rs @@ -1,5 +1,8 @@ struct c1 { x: T, +} + +impl c1 { fn f1(x: int) { } } diff --git a/src/test/run-pass/issue-2445.rs b/src/test/run-pass/issue-2445.rs index ae6cdf34e4d..b5e21e32729 100644 --- a/src/test/run-pass/issue-2445.rs +++ b/src/test/run-pass/issue-2445.rs @@ -2,6 +2,9 @@ use dvec::DVec; struct c1 { x: T, +} + +impl c1 { fn f1(x: T) {} } diff --git a/src/test/run-pass/issue-2487-a.rs b/src/test/run-pass/issue-2487-a.rs index a8c6e5b1a0d..8a21f128a99 100644 --- a/src/test/run-pass/issue-2487-a.rs +++ b/src/test/run-pass/issue-2487-a.rs @@ -2,6 +2,9 @@ struct socket { sock: int, drop { } +} + +impl socket { fn set_identity() { do closure { diff --git a/src/test/run-pass/issue-2502.rs b/src/test/run-pass/issue-2502.rs index d58f5a8ecb2..64028890df0 100644 --- a/src/test/run-pass/issue-2502.rs +++ b/src/test/run-pass/issue-2502.rs @@ -1,6 +1,8 @@ struct font { fontbuf: &self/~[u8], +} +impl font { fn buf() -> &self/~[u8] { self.fontbuf } diff --git a/src/test/run-pass/issue-2718.rs b/src/test/run-pass/issue-2718.rs index d878d3fcf4b..6f6b6e2cd39 100644 --- a/src/test/run-pass/issue-2718.rs +++ b/src/test/run-pass/issue-2718.rs @@ -145,6 +145,9 @@ mod pipes { sender_terminate(option::unwrap(p)) } } + } + + impl send_packet { fn unwrap() -> *packet { let mut p = None; p <-> self.p; @@ -167,6 +170,9 @@ mod pipes { receiver_terminate(option::unwrap(p)) } } + } + + impl recv_packet { fn unwrap() -> *packet { let mut p = None; p <-> self.p; diff --git a/src/test/run-pass/issue-2936.rs b/src/test/run-pass/issue-2936.rs index e76a3088f81..c4b6ef99e70 100644 --- a/src/test/run-pass/issue-2936.rs +++ b/src/test/run-pass/issue-2936.rs @@ -6,8 +6,11 @@ fn foo>(b: U) -> T { b.get_bar() } -struct cbar : bar { +struct cbar { x: int, +} + +impl cbar : bar { fn get_bar() -> int { self.x } diff --git a/src/test/run-pass/nested-class.rs b/src/test/run-pass/nested-class.rs index 6c30ee106d4..4fc49383adc 100644 --- a/src/test/run-pass/nested-class.rs +++ b/src/test/run-pass/nested-class.rs @@ -2,6 +2,9 @@ fn main() { struct b { i: int, + } + + impl b { fn do_stuff() -> int { return 37; } } diff --git a/src/test/run-pass/private-class-field.rs b/src/test/run-pass/private-class-field.rs index 4b54a473faa..735545a580c 100644 --- a/src/test/run-pass/private-class-field.rs +++ b/src/test/run-pass/private-class-field.rs @@ -4,7 +4,9 @@ struct cat { } how_hungry : int, +} +impl cat { fn meow_count() -> uint { self.meows } } diff --git a/src/test/run-pass/private-method.rs b/src/test/run-pass/private-method.rs index ba11df94256..23789676549 100644 --- a/src/test/run-pass/private-method.rs +++ b/src/test/run-pass/private-method.rs @@ -1,17 +1,22 @@ struct cat { priv { mut meows : uint, - fn nap() { for uint::range(1u, 10u) |_i| { }} } how_hungry : int, +} +impl cat { fn play() { self.meows += 1u; self.nap(); } } +priv impl cat { + fn nap() { for uint::range(1u, 10u) |_i| { }} +} + fn cat(in_x : uint, in_y : int) -> cat { cat { meows: in_x, diff --git a/src/test/run-pass/resource-destruct.rs b/src/test/run-pass/resource-destruct.rs index 4d51f1f5541..3f3417fd703 100644 --- a/src/test/run-pass/resource-destruct.rs +++ b/src/test/run-pass/resource-destruct.rs @@ -1,9 +1,12 @@ struct shrinky_pointer { i: @@mut int, - fn look_at() -> int { return **(self.i); } drop { log(error, ~"Hello!"); **(self.i) -= 1; } } +impl shrinky_pointer { + fn look_at() -> int { return **(self.i); } +} + fn shrinky_pointer(i: @@mut int) -> shrinky_pointer { shrinky_pointer { i: i diff --git a/src/test/run-pass/task-comm-6.rs b/src/test/run-pass/task-comm-6.rs index 8927254886a..82529221943 100644 --- a/src/test/run-pass/task-comm-6.rs +++ b/src/test/run-pass/task-comm-6.rs @@ -1,6 +1,6 @@ use std; use pipes::send; -use pipes::chan; +use pipes::Chan; use pipes::recv; fn main() { test00(); }