From 9adfa59d8e9432eef84297b6ed5d8f2967b9b8f9 Mon Sep 17 00:00:00 2001 From: Erick Tryzelaar Date: Sun, 3 Feb 2013 20:47:26 -0800 Subject: [PATCH] core: convert ToStr::to_str to take explicit &self --- src/libcore/num/f32.rs | 2 +- src/libcore/num/f64.rs | 2 +- src/libcore/num/float.rs | 2 +- src/libcore/num/int-template.rs | 4 +- src/libcore/num/uint-template.rs | 4 +- src/libcore/path.rs | 4 +- src/libcore/to_str.rs | 52 ++++++++++++++++-------- src/librustc/middle/liveness.rs | 4 +- src/librustc/middle/trans/closure.rs | 4 +- src/librustc/middle/trans/common.rs | 2 +- src/librustc/middle/trans/datum.rs | 2 +- src/librustc/middle/ty.rs | 18 ++++---- src/libstd/bigint.rs | 4 +- src/libstd/bitv.rs | 2 +- src/libstd/json.rs | 4 +- src/libstd/net_url.rs | 4 +- src/libstd/oldmap.rs | 2 +- src/libsyntax/ast.rs | 20 ++++----- src/libsyntax/ext/pipes/proto.rs | 4 +- src/test/auxiliary/cci_class_cast.rs | 2 +- src/test/compile-fail/issue-3973.rs | 2 +- src/test/compile-fail/multitrait.rs | 4 +- src/test/run-pass/class-separate-impl.rs | 2 +- src/test/run-pass/issue-2904.rs | 4 +- src/test/run-pass/issue-3563-3.rs | 2 +- src/test/run-pass/new-impl-syntax.rs | 4 +- 26 files changed, 88 insertions(+), 72 deletions(-) diff --git a/src/libcore/num/f32.rs b/src/libcore/num/f32.rs index 68e7c3c9df26e..24ad5c114af28 100644 --- a/src/libcore/num/f32.rs +++ b/src/libcore/num/f32.rs @@ -439,7 +439,7 @@ pub pure fn to_str_digits(num: f32, dig: uint) -> ~str { impl f32: to_str::ToStr { #[inline(always)] - pure fn to_str() -> ~str { to_str_digits(self, 8) } + pure fn to_str(&self) -> ~str { to_str_digits(*self, 8) } } impl f32: num::ToStrRadix { diff --git a/src/libcore/num/f64.rs b/src/libcore/num/f64.rs index 85f44d1b94f8d..126a48cf28073 100644 --- a/src/libcore/num/f64.rs +++ b/src/libcore/num/f64.rs @@ -463,7 +463,7 @@ pub pure fn to_str_digits(num: f64, dig: uint) -> ~str { impl f64: to_str::ToStr { #[inline(always)] - pure fn to_str() -> ~str { to_str_digits(self, 8) } + pure fn to_str(&self) -> ~str { to_str_digits(*self, 8) } } impl f64: num::ToStrRadix { diff --git a/src/libcore/num/float.rs b/src/libcore/num/float.rs index 32c7717422135..32cda029cd182 100644 --- a/src/libcore/num/float.rs +++ b/src/libcore/num/float.rs @@ -206,7 +206,7 @@ pub pure fn to_str_digits(num: float, digits: uint) -> ~str { impl float: to_str::ToStr { #[inline(always)] - pure fn to_str() -> ~str { to_str_digits(self, 8) } + pure fn to_str(&self) -> ~str { to_str_digits(*self, 8) } } impl float: num::ToStrRadix { diff --git a/src/libcore/num/int-template.rs b/src/libcore/num/int-template.rs index 06d11e23967e8..8b02f3a94c5ff 100644 --- a/src/libcore/num/int-template.rs +++ b/src/libcore/num/int-template.rs @@ -287,8 +287,8 @@ pub pure fn str(i: T) -> ~str { to_str(i) } impl T : ToStr { #[inline(always)] - pure fn to_str() -> ~str { - to_str(self) + pure fn to_str(&self) -> ~str { + to_str(*self) } } diff --git a/src/libcore/num/uint-template.rs b/src/libcore/num/uint-template.rs index 44ed9816cf94a..82c6e0170145f 100644 --- a/src/libcore/num/uint-template.rs +++ b/src/libcore/num/uint-template.rs @@ -249,8 +249,8 @@ pub pure fn str(i: T) -> ~str { to_str(i) } impl T : ToStr { #[inline(always)] - pure fn to_str() -> ~str { - to_str(self) + pure fn to_str(&self) -> ~str { + to_str(*self) } } diff --git a/src/libcore/path.rs b/src/libcore/path.rs index 2f9b2967775c3..0b64df8c1124c 100644 --- a/src/libcore/path.rs +++ b/src/libcore/path.rs @@ -368,7 +368,7 @@ impl Path { } impl PosixPath : ToStr { - pure fn to_str() -> ~str { + pure fn to_str(&self) -> ~str { let mut s = ~""; if self.is_absolute { s += "/"; @@ -531,7 +531,7 @@ impl PosixPath : GenericPath { impl WindowsPath : ToStr { - pure fn to_str() -> ~str { + pure fn to_str(&self) -> ~str { let mut s = ~""; match self.host { Some(ref h) => { s += "\\\\"; s += *h; } diff --git a/src/libcore/to_str.rs b/src/libcore/to_str.rs index 51205b0c647fd..0e46b4fd004b3 100644 --- a/src/libcore/to_str.rs +++ b/src/libcore/to_str.rs @@ -22,52 +22,68 @@ use kinds::Copy; use str; use vec; -pub trait ToStr { pub pure fn to_str() -> ~str; } +pub trait ToStr { + pure fn to_str(&self) -> ~str; +} impl bool: ToStr { #[inline(always)] - pure fn to_str() -> ~str { ::bool::to_str(self) } + pure fn to_str(&self) -> ~str { ::bool::to_str(*self) } } impl (): ToStr { #[inline(always)] - pure fn to_str() -> ~str { ~"()" } + pure fn to_str(&self) -> ~str { ~"()" } } impl ~str: ToStr { #[inline(always)] - pure fn to_str() -> ~str { copy self } + pure fn to_str(&self) -> ~str { copy *self } } impl &str: ToStr { #[inline(always)] - pure fn to_str() -> ~str { ::str::from_slice(self) } + pure fn to_str(&self) -> ~str { ::str::from_slice(*self) } } impl @str: ToStr { #[inline(always)] - pure fn to_str() -> ~str { ::str::from_slice(self) } + pure fn to_str(&self) -> ~str { ::str::from_slice(*self) } } -impl (A, B): ToStr { +impl (A, B): ToStr { #[inline(always)] - pure fn to_str() -> ~str { - let (a, b) = self; - ~"(" + a.to_str() + ~", " + b.to_str() + ~")" + pure fn to_str(&self) -> ~str { + // FIXME(#4760): this causes an llvm assertion + //let &(ref a, ref b) = self; + match *self { + (ref a, ref b) => { + ~"(" + a.to_str() + ~", " + b.to_str() + ~")" + } + } } } -impl (A, B, C): ToStr { +impl (A, B, C): ToStr { #[inline(always)] - pure fn to_str() -> ~str { - let (a, b, c) = self; - ~"(" + a.to_str() + ~", " + b.to_str() + ~", " + c.to_str() + ~")" + pure fn to_str(&self) -> ~str { + // FIXME(#4760): this causes an llvm assertion + //let &(ref a, ref b, ref c) = self; + match *self { + (ref a, ref b, ref c) => { + fmt!("(%s, %s, %s)", + (*a).to_str(), + (*b).to_str(), + (*c).to_str() + ) + } + } } } impl ~[A]: ToStr { #[inline(always)] - pure fn to_str() -> ~str { + pure fn to_str(&self) -> ~str { unsafe { // Bleh -- not really unsafe // push_str and push_char let mut acc = ~"[", first = true; - for vec::each(self) |elt| { + for self.each |elt| { unsafe { if first { first = false; } else { str::push_str(&mut acc, ~", "); } @@ -82,11 +98,11 @@ impl ~[A]: ToStr { impl @A: ToStr { #[inline(always)] - pure fn to_str() -> ~str { ~"@" + (*self).to_str() } + pure fn to_str(&self) -> ~str { ~"@" + (**self).to_str() } } impl ~A: ToStr { #[inline(always)] - pure fn to_str() -> ~str { ~"~" + (*self).to_str() } + pure fn to_str(&self) -> ~str { ~"~" + (**self).to_str() } } #[cfg(test)] diff --git a/src/librustc/middle/liveness.rs b/src/librustc/middle/liveness.rs index d14d58122a759..c13af3a32f200 100644 --- a/src/librustc/middle/liveness.rs +++ b/src/librustc/middle/liveness.rs @@ -222,11 +222,11 @@ pub fn check_crate(tcx: ty::ctxt, } impl LiveNode: to_str::ToStr { - pure fn to_str() -> ~str { fmt!("ln(%u)", *self) } + pure fn to_str(&self) -> ~str { fmt!("ln(%u)", **self) } } impl Variable: to_str::ToStr { - pure fn to_str() -> ~str { fmt!("v(%u)", *self) } + pure fn to_str(&self) -> ~str { fmt!("v(%u)", **self) } } // ______________________________________________________________________ diff --git a/src/librustc/middle/trans/closure.rs b/src/librustc/middle/trans/closure.rs index 9a115acbd8dde..4cbf6a4f4b74a 100644 --- a/src/librustc/middle/trans/closure.rs +++ b/src/librustc/middle/trans/closure.rs @@ -121,8 +121,8 @@ pub struct EnvValue { } pub impl EnvAction { - fn to_str() -> ~str { - match self { + fn to_str(&self) -> ~str { + match *self { EnvCopy => ~"EnvCopy", EnvMove => ~"EnvMove", EnvRef => ~"EnvRef" diff --git a/src/librustc/middle/trans/common.rs b/src/librustc/middle/trans/common.rs index f21a3150a1c38..571f16e4b34ee 100644 --- a/src/librustc/middle/trans/common.rs +++ b/src/librustc/middle/trans/common.rs @@ -720,7 +720,7 @@ pub impl block { fn ty_to_str(t: ty::t) -> ~str { ty_to_str(self.tcx(), t) } - fn to_str() -> ~str { + fn to_str(&self) -> ~str { match self.node_info { Some(node_info) => { fmt!("[block %d]", node_info.id) diff --git a/src/librustc/middle/trans/datum.rs b/src/librustc/middle/trans/datum.rs index c3bd34ececbd0..554cebbbcb781 100644 --- a/src/librustc/middle/trans/datum.rs +++ b/src/librustc/middle/trans/datum.rs @@ -841,7 +841,7 @@ pub impl DatumBlock { self.bcx.tcx() } - fn to_str() -> ~str { + fn to_str(&self) -> ~str { self.datum.to_str(self.ccx()) } } diff --git a/src/librustc/middle/ty.rs b/src/librustc/middle/ty.rs index 4a0448c0b2449..8610e3c85d7f5 100644 --- a/src/librustc/middle/ty.rs +++ b/src/librustc/middle/ty.rs @@ -667,7 +667,7 @@ pub impl TyVid: Vid { } pub impl TyVid: ToStr { - pure fn to_str() -> ~str { fmt!("", self.to_uint()) } + pure fn to_str(&self) -> ~str { fmt!("", self.to_uint()) } } pub impl IntVid: Vid { @@ -675,7 +675,7 @@ pub impl IntVid: Vid { } pub impl IntVid: ToStr { - pure fn to_str() -> ~str { fmt!("", self.to_uint()) } + pure fn to_str(&self) -> ~str { fmt!("", self.to_uint()) } } pub impl FloatVid: Vid { @@ -683,7 +683,7 @@ pub impl FloatVid: Vid { } pub impl FloatVid: ToStr { - pure fn to_str() -> ~str { fmt!("", self.to_uint()) } + pure fn to_str(&self) -> ~str { fmt!("", self.to_uint()) } } pub impl RegionVid: Vid { @@ -691,19 +691,19 @@ pub impl RegionVid: Vid { } pub impl RegionVid: ToStr { - pure fn to_str() -> ~str { fmt!("%?", self) } + pure fn to_str(&self) -> ~str { fmt!("%?", self) } } pub impl FnSig : ToStr { - pure fn to_str() -> ~str { + pure fn to_str(&self) -> ~str { // grr, without tcx not much we can do. return ~"(...)"; } } pub impl InferTy: ToStr { - pure fn to_str() -> ~str { - match self { + pure fn to_str(&self) -> ~str { + match *self { TyVar(ref v) => v.to_str(), IntVar(ref v) => v.to_str(), FloatVar(ref v) => v.to_str() @@ -712,8 +712,8 @@ pub impl InferTy: ToStr { } pub impl IntVarValue : ToStr { - pure fn to_str() -> ~str { - match self { + pure fn to_str(&self) -> ~str { + match *self { IntType(ref v) => v.to_str(), UintType(ref v) => v.to_str(), } diff --git a/src/libstd/bigint.rs b/src/libstd/bigint.rs index 64126ea918fc0..092a0d18a0fe4 100644 --- a/src/libstd/bigint.rs +++ b/src/libstd/bigint.rs @@ -88,7 +88,7 @@ impl BigUint : Ord { } impl BigUint : ToStr { - pure fn to_str() -> ~str { self.to_str_radix(10) } + pure fn to_str(&self) -> ~str { self.to_str_radix(10) } } impl BigUint : from_str::FromStr { @@ -605,7 +605,7 @@ impl BigInt : Ord { } impl BigInt : ToStr { - pure fn to_str() -> ~str { self.to_str_radix(10) } + pure fn to_str(&self) -> ~str { self.to_str_radix(10) } } impl BigInt : from_str::FromStr { diff --git a/src/libstd/bitv.rs b/src/libstd/bitv.rs index 0ad9d0af2ac77..69becd5b9ce7e 100644 --- a/src/libstd/bitv.rs +++ b/src/libstd/bitv.rs @@ -474,7 +474,7 @@ impl Bitv { * The resulting string has the same length as `self`, and each * character is either '0' or '1'. */ - fn to_str() -> ~str { + fn to_str(&self) -> ~str { let mut rs = ~""; for self.each() |i| { if i { rs += ~"1"; } else { rs += ~"0"; } }; rs diff --git a/src/libstd/json.rs b/src/libstd/json.rs index b9e3b29b15249..4b34f318e91b2 100644 --- a/src/libstd/json.rs +++ b/src/libstd/json.rs @@ -1172,11 +1172,11 @@ impl Option: ToJson { } impl Json: to_str::ToStr { - pure fn to_str() -> ~str { to_str(&self) } + pure fn to_str(&self) -> ~str { to_str(self) } } impl Error: to_str::ToStr { - pure fn to_str() -> ~str { + pure fn to_str(&self) -> ~str { fmt!("%u:%u: %s", self.line, self.col, *self.msg) } } diff --git a/src/libstd/net_url.rs b/src/libstd/net_url.rs index 89b19fccc1c47..b32c97c699837 100644 --- a/src/libstd/net_url.rs +++ b/src/libstd/net_url.rs @@ -718,8 +718,8 @@ pub pure fn to_str(url: &Url) -> ~str { } impl Url: to_str::ToStr { - pub pure fn to_str() -> ~str { - to_str(&self) + pub pure fn to_str(&self) -> ~str { + to_str(self) } } diff --git a/src/libstd/oldmap.rs b/src/libstd/oldmap.rs index d8fecda73a7bb..e32043a722543 100644 --- a/src/libstd/oldmap.rs +++ b/src/libstd/oldmap.rs @@ -353,7 +353,7 @@ pub mod chained { } impl T: ToStr { - pure fn to_str() -> ~str { + pure fn to_str(&self) -> ~str { unsafe { // Meh -- this should be safe do io::with_str_writer |wr| { self.to_writer(wr) } diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index a651090cf6462..bc808495ca33b 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -923,8 +923,8 @@ pub enum trait_method { pub enum int_ty { ty_i, ty_char, ty_i8, ty_i16, ty_i32, ty_i64, } pub impl int_ty : ToStr { - pure fn to_str() -> ~str { - ::ast_util::int_ty_to_str(self) + pure fn to_str(&self) -> ~str { + ::ast_util::int_ty_to_str(*self) } } @@ -959,8 +959,8 @@ pub impl int_ty : cmp::Eq { pub enum uint_ty { ty_u, ty_u8, ty_u16, ty_u32, ty_u64, } pub impl uint_ty : ToStr { - pure fn to_str() -> ~str { - ::ast_util::uint_ty_to_str(self) + pure fn to_str(&self) -> ~str { + ::ast_util::uint_ty_to_str(*self) } } @@ -993,8 +993,8 @@ pub impl uint_ty : cmp::Eq { pub enum float_ty { ty_f, ty_f32, ty_f64, } pub impl float_ty : ToStr { - pure fn to_str() -> ~str { - ::ast_util::float_ty_to_str(self) + pure fn to_str(&self) -> ~str { + ::ast_util::float_ty_to_str(*self) } } @@ -1096,8 +1096,8 @@ pub enum Onceness { } pub impl Onceness : ToStr { - pure fn to_str() -> ~str { - match self { + pure fn to_str(&self) -> ~str { + match *self { Once => ~"once", Many => ~"many" } @@ -1188,8 +1188,8 @@ pub enum purity { } pub impl purity : ToStr { - pure fn to_str() -> ~str { - match self { + pure fn to_str(&self) -> ~str { + match *self { impure_fn => ~"impure", unsafe_fn => ~"unsafe", pure_fn => ~"pure", diff --git a/src/libsyntax/ext/pipes/proto.rs b/src/libsyntax/ext/pipes/proto.rs index 5c2dd82eb7f71..6a1d93576941d 100644 --- a/src/libsyntax/ext/pipes/proto.rs +++ b/src/libsyntax/ext/pipes/proto.rs @@ -34,8 +34,8 @@ pub impl direction : cmp::Eq { } pub impl direction: ToStr { - pure fn to_str() -> ~str { - match self { + pure fn to_str(&self) -> ~str { + match *self { send => ~"Send", recv => ~"Recv" } diff --git a/src/test/auxiliary/cci_class_cast.rs b/src/test/auxiliary/cci_class_cast.rs index 1225e2fe8a747..c0140bff5b13a 100644 --- a/src/test/auxiliary/cci_class_cast.rs +++ b/src/test/auxiliary/cci_class_cast.rs @@ -18,7 +18,7 @@ pub mod kitty { } pub impl cat : ToStr { - pure fn to_str() -> ~str { copy self.name } + pure fn to_str(&self) -> ~str { copy self.name } } priv impl cat { diff --git a/src/test/compile-fail/issue-3973.rs b/src/test/compile-fail/issue-3973.rs index 6c977840c2257..5d49610a4e5be 100644 --- a/src/test/compile-fail/issue-3973.rs +++ b/src/test/compile-fail/issue-3973.rs @@ -20,7 +20,7 @@ impl Point : ToStr { //~ ERROR implements a method not defined in the trait Point { x: x, y: y } } - pure fn to_str() -> ~str { + pure fn to_str(&self) -> ~str { fmt!("(%f, %f)", self.x, self.y) } } diff --git a/src/test/compile-fail/multitrait.rs b/src/test/compile-fail/multitrait.rs index 6fe93c151e6a3..a0a9e3f0ddf27 100644 --- a/src/test/compile-fail/multitrait.rs +++ b/src/test/compile-fail/multitrait.rs @@ -14,5 +14,5 @@ struct S { impl S: Cmp, ToStr { //~ ERROR: expected `{` but found `,` fn eq(&&other: S) { false } - fn to_str() -> ~str { ~"hi" } -} \ No newline at end of file + fn to_str(&self) -> ~str { ~"hi" } +} diff --git a/src/test/run-pass/class-separate-impl.rs b/src/test/run-pass/class-separate-impl.rs index 56430678b05cd..7e59b5d7a870b 100644 --- a/src/test/run-pass/class-separate-impl.rs +++ b/src/test/run-pass/class-separate-impl.rs @@ -54,7 +54,7 @@ fn cat(in_x : uint, in_y : int, in_name: ~str) -> cat { } impl cat: ToStr { - pure fn to_str() -> ~str { copy self.name } + pure fn to_str(&self) -> ~str { copy self.name } } fn print_out(thing: T, expected: ~str) { diff --git a/src/test/run-pass/issue-2904.rs b/src/test/run-pass/issue-2904.rs index d150cff304a32..25d460656fdef 100644 --- a/src/test/run-pass/issue-2904.rs +++ b/src/test/run-pass/issue-2904.rs @@ -28,8 +28,8 @@ enum square { } impl square: to_str::ToStr { - pure fn to_str() -> ~str { - match self { + pure fn to_str(&self) -> ~str { + match *self { bot => { ~"R" } wall => { ~"#" } rock => { ~"*" } diff --git a/src/test/run-pass/issue-3563-3.rs b/src/test/run-pass/issue-3563-3.rs index 5196536db6210..078bdae314f1a 100644 --- a/src/test/run-pass/issue-3563-3.rs +++ b/src/test/run-pass/issue-3563-3.rs @@ -110,7 +110,7 @@ impl AsciiArt // Note that the %s fmt! specifier will not call this automatically. impl AsciiArt : ToStr { - pure fn to_str() -> ~str + pure fn to_str(&self) -> ~str { // Convert each line into a string. let lines = do self.lines.map |line| {str::from_chars(*line)}; diff --git a/src/test/run-pass/new-impl-syntax.rs b/src/test/run-pass/new-impl-syntax.rs index 7966fcea8795f..aad7ded42d3d0 100644 --- a/src/test/run-pass/new-impl-syntax.rs +++ b/src/test/run-pass/new-impl-syntax.rs @@ -4,7 +4,7 @@ struct Thingy { } impl ToStr for Thingy { - pure fn to_str() -> ~str { + pure fn to_str(&self) -> ~str { fmt!("{ x: %d, y: %d }", self.x, self.y) } } @@ -14,7 +14,7 @@ struct PolymorphicThingy { } impl ToStr for PolymorphicThingy { - pure fn to_str() -> ~str { + pure fn to_str(&self) -> ~str { self.x.to_str() } }