From 9c5101542d2a4983ade627bc593e16c0bb5f23ba Mon Sep 17 00:00:00 2001 From: Alexis Beingessner Date: Fri, 21 Nov 2014 20:47:40 -0500 Subject: [PATCH 01/44] Conventions and cleanup for Bitv and BitvSet Part of #18424 This commit changes the semantics of `reserve` and `capacity` for Bitv and BitvSet to match conventions. It also introduces the notion of `reserve_index` and `reserve_index_exact` for collections with maximum-index-based capacity semantics. Deprecates free function constructors in favour of functions on Bitv itself. Changes `Bitv::pop` to return an Option rather than panicking. Deprecates and renames several methods in favour of conventions. Marks several blessed methods as unstable. This commit also substantially refactors Bitv and BitvSet's implementations. The new implementation is simpler, cleaner, better documented, and more robust against overflows. It also reduces coupling between Bitv and BitvSet. Tests have been seperated into seperate submodules. Fixes #16958 [breaking-change] --- src/libcollections/bit.rs | 1545 +++++++++++++++------------ src/test/run-pass/bitv-perf-test.rs | 4 +- src/test/run-pass/issue-11736.rs | 2 +- 3 files changed, 860 insertions(+), 691 deletions(-) diff --git a/src/libcollections/bit.rs b/src/libcollections/bit.rs index f59fb1c5d3da..41a5ccb7a064 100644 --- a/src/libcollections/bit.rs +++ b/src/libcollections/bit.rs @@ -11,6 +11,16 @@ // FIXME(Gankro): Bitv and BitvSet are very tightly coupled. Ideally (for maintenance), // they should be in separate files/modules, with BitvSet only using Bitv's public API. +// First rule of Bitv club: almost everything can actually overflow because we're working with +// bits and not bytes. +// +// Second rule of Bitv club: the last "block" of bits may be partially used. We must ensure that +// those unused bits are zeroed out, as other methods will assume this is the case. It may be +// the case that this isn't a great design, but having "undefined" bits is headache-inducing. +// +// Third rule of Bitv club: BitvSet is fairly tightly coupled to Bitv's implementation details. +// Make sure any changes to Bitv are properly addressed in BitvSet. + //! Collections implemented with bit vectors. //! //! # Examples @@ -31,7 +41,7 @@ //! let primes = { //! // Assume all numbers are prime to begin, and then we //! // cross off non-primes progressively -//! let mut bv = Bitv::with_capacity(max_prime, true); +//! let mut bv = Bitv::from_elem(max_prime, true); //! //! // Neither 0 nor 1 are prime //! bv.set(0, false); @@ -68,18 +78,19 @@ use core::cmp; use core::default::Default; use core::fmt; -use core::iter::{Chain, Enumerate, Repeat, Skip, Take, repeat}; +use core::iter::{Cloned, Chain, Enumerate, Repeat, Skip, Take}; use core::iter; use core::num::Int; -use core::slice; -use core::u32; +use core::slice::{Items, MutItems}; +use core::{u32, uint}; use std::hash; use vec::Vec; -// FIXME(conventions): look, we just need to refactor this whole thing. Inside and out. +type Blocks<'a> = Cloned> +type MutBlocks<'a> MutItems<'a, u32>; +type MatchWords<'a> = Chain>, Skip>>>>; -type MatchWords<'a> = Chain, Skip>>>>; // Take two BitV's, and return iterators of their words, where the shorter one // has been padded with 0's fn match_words <'a,'b>(a: &'a Bitv, b: &'b Bitv) -> (MatchWords<'a>, MatchWords<'b>) { @@ -88,11 +99,11 @@ fn match_words <'a,'b>(a: &'a Bitv, b: &'b Bitv) -> (MatchWords<'a>, MatchWords< // have to uselessly pretend to pad the longer one for type matching if a_len < b_len { - (a.mask_words(0).chain(repeat(0u32).enumerate().take(b_len).skip(a_len)), - b.mask_words(0).chain(repeat(0u32).enumerate().take(0).skip(0))) + (a.blocks().enumerate().chain(iter::repeat(0u32).enumerate().take(b_len).skip(a_len)), + b.blocks().enumerate().chain(iter::repeat(0u32).enumerate().take(0).skip(0))) } else { - (a.mask_words(0).chain(repeat(0u32).enumerate().take(0).skip(0)), - b.mask_words(0).chain(repeat(0u32).enumerate().take(a_len).skip(b_len))) + (a.blocks().enumerate().chain(iter::repeat(0u32).enumerate().take(0).skip(0)), + b.blocks().enumerate().chain(iter::repeat(0u32).enumerate().take(a_len).skip(b_len))) } } @@ -106,7 +117,7 @@ fn match_words <'a,'b>(a: &'a Bitv, b: &'b Bitv) -> (MatchWords<'a>, MatchWords< /// ```rust /// use collections::Bitv; /// -/// let mut bv = Bitv::with_capacity(10, false); +/// let mut bv = Bitv::from_elem(10, false); /// /// // insert all primes less than 10 /// bv.set(2, true); @@ -133,10 +144,11 @@ pub struct Bitv { nbits: uint } +// FIXME(Gankro): NopeNopeNopeNopeNope (wait for IndexGet to be a thing) impl Index for Bitv { #[inline] fn index<'a>(&'a self, i: &uint) -> &'a bool { - if self.get(*i) { + if self.get(*i).expect("index out of bounds") { &TRUE } else { &FALSE @@ -144,35 +156,27 @@ fn index<'a>(&'a self, i: &uint) -> &'a bool { } } -struct MaskWords<'a> { - iter: slice::Items<'a, u32>, - next_word: Option<&'a u32>, - last_word_mask: u32, - offset: uint -} - -impl<'a> Iterator<(uint, u32)> for MaskWords<'a> { - /// Returns (offset, word) - #[inline] - fn next(&mut self) -> Option<(uint, u32)> { - let ret = self.next_word; - match ret { - Some(&w) => { - self.next_word = self.iter.next(); - self.offset += 1; - // The last word may need to be masked - if self.next_word.is_none() { - Some((self.offset - 1, w & self.last_word_mask)) - } else { - Some((self.offset - 1, w)) - } - }, - None => None - } +/// Computes how many blocks are needed to store that many bits +fn blocks_for_bits(bits: uint) -> uint { + // If we want 17 bits, dividing by 32 will produce 0. So we add 1 to make sure we + // reserve enough. But if we want exactly a multiple of 32, this will actually allocate + // one too many. So we need to check if that's the case. We can do that by computing if + // bitwise AND by `32 - 1` is 0. But LLVM should be able to optimize the semantically + // superior modulo operator on a power of two to this. + // + // Note that we can technically avoid this branch with the expression + // `(nbits + u32::BITS - 1) / 32::BITS`, but if nbits is almost uint::MAX this will overflow. + if bits % u32::BITS == 0 { + bits / u32::BITS + } else { + bits / u32::BITS + 1 } + } impl Bitv { + /// Applies the given operation to the blocks of self and other, and sets self to + /// be the result. #[inline] fn process(&mut self, other: &Bitv, mut op: F) -> bool where F: FnMut(u32, u32) -> u32 { let len = other.storage.len(); @@ -182,8 +186,7 @@ fn process(&mut self, other: &Bitv, mut op: F) -> bool where F: FnMut(u32, u3 // `op` is a bitwise operation, since any bits that should've // been masked were fine to change anyway. `b` is masked to // make sure its unmasked bits do not cause damage. - for (a, (_, b)) in self.storage.iter_mut() - .zip(other.mask_words(0)) { + for (a, b) in self.blocks_mut().zip(other.blocks()) { let w = op(*a, b); if *a != w { changed = true; @@ -193,22 +196,27 @@ fn process(&mut self, other: &Bitv, mut op: F) -> bool where F: FnMut(u32, u3 changed } - #[inline] - fn mask_words<'a>(&'a self, mut start: uint) -> MaskWords<'a> { - if start > self.storage.len() { - start = self.storage.len(); - } - let mut iter = self.storage[start..].iter(); - MaskWords { - next_word: iter.next(), - iter: iter, - last_word_mask: { - let rem = self.nbits % u32::BITS; - if rem > 0 { - (1 << rem) - 1 - } else { !0 } - }, - offset: start + /// Iterator over mutable refs to the underlying blocks of data. + fn blocks_mut(&mut self) -> MutBlocks { + let blocks = blocks_for_bits(self.len()); + self.storage[..blocks].iter_mut() + } + + /// Iterator over the underlying blocks of data + fn blocks(&self) -> Blocks { + let blocks = blocks_for_bits(self.len()); + self.storage[..blocks].iter().cloned() + } + + /// An operation might screw up the unused bits in the last block of the Bitv. + /// It's assumed to be all 0's. This fixes it up. + fn fix_last_block(&mut self) { + let len = self.len(); + let extra_bits = len % u32::BITS; + if extra_bits > 0 { + let mask = (1 << extra_bits) - 1; + let storage_len = self.storage.len(); + self.storage[storage_len - 1] &= mask; } } @@ -226,61 +234,111 @@ pub fn new() -> Bitv { } /// Creates a `Bitv` that holds `nbits` elements, setting each element - /// to `init`. + /// to `bit`. /// /// # Examples /// /// ``` /// use std::collections::Bitv; /// - /// let mut bv = Bitv::with_capacity(10u, false); + /// let mut bv = Bitv::from_elem(10u, false); /// assert_eq!(bv.len(), 10u); /// for x in bv.iter() { /// assert_eq!(x, false); /// } /// ``` - pub fn with_capacity(nbits: uint, init: bool) -> Bitv { + pub fn from_elem(nbits: uint, bit: bool) -> Bitv { + let nblocks = blocks_for_bits(nbits); let mut bitv = Bitv { - storage: Vec::from_elem((nbits + u32::BITS - 1) / u32::BITS, - if init { !0u32 } else { 0u32 }), + storage: Vec::from_elem(nblocks, if bit { !0u32 } else { 0u32 }), nbits: nbits }; - // Zero out any unused bits in the highest word if necessary - let used_bits = bitv.nbits % u32::BITS; - if init && used_bits != 0 { - let largest_used_word = (bitv.nbits + u32::BITS - 1) / u32::BITS - 1; - bitv.storage[largest_used_word] &= (1 << used_bits) - 1; - } - + bitv.fix_last_block(); bitv } - /// Retrieves the value at index `i`. + /// Constructs a new, empty `Bitv` with the specified capacity. /// - /// # Panics + /// The bitvector will be able to hold at least `capacity` bits without + /// reallocating. If `capacity` is 0, it will not allocate. /// - /// Panics if `i` is out of bounds. + /// It is important to note that this function does not specify the + /// *length* of the returned bitvector, but only the *capacity*. + #[unstable = "matches collection reform specification, waiting for dust to settle"] + pub fn with_capacity(nbits: uint) -> Bitv { + Bitv { + storage: Vec::with_capacity(blocks_for_bits(nbits)), + nbits: 0, + } + } + + /// Transforms a byte-vector into a `Bitv`. Each byte becomes eight bits, + /// with the most significant bits of each byte coming first. Each + /// bit becomes `true` if equal to 1 or `false` if equal to 0. /// /// # Examples /// /// ``` - /// use std::collections::bitv; + /// use std::collections::Bitv; /// - /// let bv = bitv::from_bytes(&[0b01100000]); - /// assert_eq!(bv.get(0), false); - /// assert_eq!(bv.get(1), true); + /// let bv = Bitv::from_bytes(&[0b10100000, 0b00010010]); + /// assert!(bv.eq_vec(&[true, false, true, false, + /// false, false, false, false, + /// false, false, false, true, + /// false, false, true, false])); + /// ``` + pub fn from_bytes(bytes: &[u8]) -> Bitv { + Bitv::from_fn(bytes.len() * 8, |i| { + let b = bytes[i / 8] as u32; + let offset = i % 8; + b >> (7 - offset) & 1 == 1 + }) + } + + /// Creates a `Bitv` of the specified length where the value at each + /// index is `f(index)`. + /// + /// # Examples + /// + /// ``` + /// use std::collections::Bitv; + /// + /// let bv = Bitv::from_fn(5, |i| { i % 2 == 0 }); + /// assert!(bv.eq_vec(&[true, false, true, false, true])); + /// ``` + pub fn from_fn(len: uint, mut f: F) -> Bitv where F: FnMut(uint) -> bool { + let mut bitv = Bitv::from_elem(len, false); + for i in range(0u, len) { + bitv.set(i, f(i)); + } + bitv + } + + /// Retrieves the value at index `i`, or `None` if the index is out of bounds. + /// + /// # Examples + /// + /// ``` + /// use std::collections::Bitv; + /// + /// let bv = Bitv::from_bytes(&[0b01100000]); + /// assert_eq!(bv.get(0), Some(false)); + /// assert_eq!(bv.get(1), Some(true)); + /// assert_eq!(bv.get(100), None); /// /// // Can also use array indexing /// assert_eq!(bv[1], true); /// ``` #[inline] - pub fn get(&self, i: uint) -> bool { + #[unstable = "panic semantics are likely to change in the future"] + pub fn get(&self, i: uint) -> Option { assert!(i < self.nbits); let w = i / u32::BITS; let b = i % u32::BITS; - let x = self.storage[w] & (1 << b); - x != 0 + self.storage.get(w).map(|block| + (block & (1 << b)) != 0 + ) } /// Sets the value of a bit at an index `i`. @@ -294,11 +352,12 @@ pub fn get(&self, i: uint) -> bool { /// ``` /// use std::collections::Bitv; /// - /// let mut bv = Bitv::with_capacity(5, false); + /// let mut bv = Bitv::from_elem(5, false); /// bv.set(3, true); /// assert_eq!(bv[3], true); /// ``` #[inline] + #[unstable = "panic semantics are likely to change in the future"] pub fn set(&mut self, i: uint, x: bool) { assert!(i < self.nbits); let w = i / u32::BITS; @@ -314,18 +373,19 @@ pub fn set(&mut self, i: uint, x: bool) { /// # Examples /// /// ``` - /// use std::collections::bitv; + /// use std::collections::Bitv; /// /// let before = 0b01100000; /// let after = 0b11111111; /// - /// let mut bv = bitv::from_bytes(&[before]); + /// let mut bv = Bitv::from_bytes(&[before]); /// bv.set_all(); - /// assert_eq!(bv, bitv::from_bytes(&[after])); + /// assert_eq!(bv, Bitv::from_bytes(&[after])); /// ``` #[inline] pub fn set_all(&mut self) { for w in self.storage.iter_mut() { *w = !0u32; } + self.fix_last_block(); } /// Flips all bits. @@ -333,18 +393,19 @@ pub fn set_all(&mut self) { /// # Examples /// /// ``` - /// use std::collections::bitv; + /// use std::collections::Bitv; /// /// let before = 0b01100000; /// let after = 0b10011111; /// - /// let mut bv = bitv::from_bytes(&[before]); + /// let mut bv = Bitv::from_bytes(&[before]); /// bv.negate(); - /// assert_eq!(bv, bitv::from_bytes(&[after])); + /// assert_eq!(bv, Bitv::from_bytes(&[after])); /// ``` #[inline] pub fn negate(&mut self) { for w in self.storage.iter_mut() { *w = !*w; } + self.fix_last_block(); } /// Calculates the union of two bitvectors. This acts like the bitwise `or` @@ -360,17 +421,17 @@ pub fn negate(&mut self) { /// # Examples /// /// ``` - /// use std::collections::bitv; + /// use std::collections::Bitv; /// /// let a = 0b01100100; /// let b = 0b01011010; /// let res = 0b01111110; /// - /// let mut a = bitv::from_bytes(&[a]); - /// let b = bitv::from_bytes(&[b]); + /// let mut a = Bitv::from_bytes(&[a]); + /// let b = Bitv::from_bytes(&[b]); /// /// assert!(a.union(&b)); - /// assert_eq!(a, bitv::from_bytes(&[res])); + /// assert_eq!(a, Bitv::from_bytes(&[res])); /// ``` #[inline] pub fn union(&mut self, other: &Bitv) -> bool { @@ -390,17 +451,17 @@ pub fn union(&mut self, other: &Bitv) -> bool { /// # Examples /// /// ``` - /// use std::collections::bitv; + /// use std::collections::Bitv; /// /// let a = 0b01100100; /// let b = 0b01011010; /// let res = 0b01000000; /// - /// let mut a = bitv::from_bytes(&[a]); - /// let b = bitv::from_bytes(&[b]); + /// let mut a = Bitv::from_bytes(&[a]); + /// let b = Bitv::from_bytes(&[b]); /// /// assert!(a.intersect(&b)); - /// assert_eq!(a, bitv::from_bytes(&[res])); + /// assert_eq!(a, Bitv::from_bytes(&[res])); /// ``` #[inline] pub fn intersect(&mut self, other: &Bitv) -> bool { @@ -420,24 +481,24 @@ pub fn intersect(&mut self, other: &Bitv) -> bool { /// # Examples /// /// ``` - /// use std::collections::bitv; + /// use std::collections::Bitv; /// /// let a = 0b01100100; /// let b = 0b01011010; /// let a_b = 0b00100100; // a - b /// let b_a = 0b00011010; // b - a /// - /// let mut bva = bitv::from_bytes(&[a]); - /// let bvb = bitv::from_bytes(&[b]); + /// let mut bva = Bitv::from_bytes(&[a]); + /// let bvb = Bitv::from_bytes(&[b]); /// /// assert!(bva.difference(&bvb)); - /// assert_eq!(bva, bitv::from_bytes(&[a_b])); + /// assert_eq!(bva, Bitv::from_bytes(&[a_b])); /// - /// let bva = bitv::from_bytes(&[a]); - /// let mut bvb = bitv::from_bytes(&[b]); + /// let bva = Bitv::from_bytes(&[a]); + /// let mut bvb = Bitv::from_bytes(&[b]); /// /// assert!(bvb.difference(&bva)); - /// assert_eq!(bvb, bitv::from_bytes(&[b_a])); + /// assert_eq!(bvb, Bitv::from_bytes(&[b_a])); /// ``` #[inline] pub fn difference(&mut self, other: &Bitv) -> bool { @@ -451,20 +512,21 @@ pub fn difference(&mut self, other: &Bitv) -> bool { /// ``` /// use std::collections::Bitv; /// - /// let mut bv = Bitv::with_capacity(5, true); + /// let mut bv = Bitv::from_elem(5, true); /// assert_eq!(bv.all(), true); /// /// bv.set(1, false); /// assert_eq!(bv.all(), false); /// ``` - #[inline] pub fn all(&self) -> bool { let mut last_word = !0u32; - // Check that every word but the last is all-ones... - self.mask_words(0).all(|(_, elem)| - { let tmp = last_word; last_word = elem; tmp == !0u32 }) && - // ...and that the last word is ones as far as it needs to be - (last_word == ((1 << self.nbits % u32::BITS) - 1) || last_word == !0u32) + // Check that every block but the last is all-ones... + self.blocks().all(|elem| { + let tmp = last_word; + last_word = elem; + tmp == !0u32 + // and then check the last one has enough ones + }) && (last_word == ((1 << self.nbits % u32::BITS) - 1) || last_word == !0u32) } /// Returns an iterator over the elements of the vector in order. @@ -472,12 +534,13 @@ pub fn all(&self) -> bool { /// # Examples /// /// ``` - /// use std::collections::bitv; + /// use std::collections::Bitv; /// - /// let bv = bitv::from_bytes(&[0b01110100, 0b10010010]); + /// let bv = Bitv::from_bytes(&[0b01110100, 0b10010010]); /// assert_eq!(bv.iter().filter(|x| *x).count(), 7); /// ``` #[inline] + #[unstable = "matches collection reform specification, waiting for dust to settle"] pub fn iter<'a>(&'a self) -> Bits<'a> { Bits {bitv: self, next_idx: 0, end_idx: self.nbits} } @@ -489,14 +552,14 @@ pub fn iter<'a>(&'a self) -> Bits<'a> { /// ``` /// use std::collections::Bitv; /// - /// let mut bv = Bitv::with_capacity(10, false); + /// let mut bv = Bitv::from_elem(10, false); /// assert_eq!(bv.none(), true); /// /// bv.set(3, true); /// assert_eq!(bv.none(), false); /// ``` pub fn none(&self) -> bool { - self.mask_words(0).all(|(_, w)| w == 0) + self.blocks().all(|w| w == 0) } /// Returns `true` if any bit is 1. @@ -506,7 +569,7 @@ pub fn none(&self) -> bool { /// ``` /// use std::collections::Bitv; /// - /// let mut bv = Bitv::with_capacity(10, false); + /// let mut bv = Bitv::from_elem(10, false); /// assert_eq!(bv.any(), false); /// /// bv.set(3, true); @@ -527,12 +590,12 @@ pub fn any(&self) -> bool { /// ``` /// use std::collections::Bitv; /// - /// let mut bv = Bitv::with_capacity(3, true); + /// let mut bv = Bitv::from_elem(3, true); /// bv.set(1, false); /// /// assert_eq!(bv.to_bytes(), vec!(0b10100000)); /// - /// let mut bv = Bitv::with_capacity(9, false); + /// let mut bv = Bitv::from_elem(9, false); /// bv.set(2, true); /// bv.set(8, true); /// @@ -544,7 +607,7 @@ fn bit (bitv: &Bitv, byte: uint, bit: uint) -> u8 { if offset >= bitv.nbits { 0 } else { - bitv.get(offset) as u8 << (7 - bit) + bitv[offset] as u8 << (7 - bit) } } @@ -562,19 +625,10 @@ fn bit (bitv: &Bitv, byte: uint, bit: uint) -> u8 { ) } - /// Transforms `self` into a `Vec` by turning each bit into a `bool`. - /// - /// # Examples - /// - /// ``` - /// use std::collections::bitv; - /// - /// let bv = bitv::from_bytes(&[0b10100000]); - /// assert_eq!(bv.to_bools(), vec!(true, false, true, false, - /// false, false, false, false)); - /// ``` + /// Deprecated: Use `iter().collect()`. + #[deprecated = "Use `iter().collect()`"] pub fn to_bools(&self) -> Vec { - Vec::from_fn(self.nbits, |i| self.get(i)) + Vec::from_fn(self.nbits, |i| self[i]) } /// Compares a `Bitv` to a slice of `bool`s. @@ -587,9 +641,9 @@ pub fn to_bools(&self) -> Vec { /// # Examples /// /// ``` - /// use std::collections::bitv; + /// use std::collections::Bitv; /// - /// let bv = bitv::from_bytes(&[0b10100000]); + /// let bv = Bitv::from_bytes(&[0b10100000]); /// /// assert!(bv.eq_vec(&[true, false, true, false, /// false, false, false, false])); @@ -598,7 +652,7 @@ pub fn eq_vec(&self, v: &[bool]) -> bool { assert_eq!(self.nbits, v.len()); let mut i = 0; while i < self.nbits { - if self.get(i) != v[i] { return false; } + if self[i] != v[i] { return false; } i = i + 1; } true @@ -612,9 +666,9 @@ pub fn eq_vec(&self, v: &[bool]) -> bool { /// # Examples /// /// ``` - /// use std::collections::bitv; + /// use std::collections::Bitv; /// - /// let mut bv = bitv::from_bytes(&[0b01001011]); + /// let mut bv = Bitv::from_bytes(&[0b01001011]); /// bv.truncate(2); /// assert!(bv.eq_vec(&[false, true])); /// ``` @@ -622,32 +676,72 @@ pub fn eq_vec(&self, v: &[bool]) -> bool { pub fn truncate(&mut self, len: uint) { if len < self.len() { self.nbits = len; - let word_len = (len + u32::BITS - 1) / u32::BITS; - self.storage.truncate(word_len); - if len % u32::BITS > 0 { - let mask = (1 << len % u32::BITS) - 1; - self.storage[word_len - 1] &= mask; - } + self.storage.truncate(blocks_for_bits(len)); + self.fix_last_block(); } } - /// Grows the vector to be able to store `size` bits without resizing. + /// Reserves capacity for at least `additional` more bits to be inserted in the given + /// `Bitv`. The collection may reserve more space to avoid frequent reallocations. + /// + /// # Panics + /// + /// Panics if the new capacity overflows `uint`. /// /// # Examples /// /// ``` /// use std::collections::Bitv; /// - /// let mut bv = Bitv::with_capacity(3, false); + /// let mut bv = Bitv::from_elem(3, false); /// bv.reserve(10); /// assert_eq!(bv.len(), 3); - /// assert!(bv.capacity() >= 10); + /// assert!(bv.capacity() >= 13); /// ``` - pub fn reserve(&mut self, size: uint) { - let old_size = self.storage.len(); - let new_size = (size + u32::BITS - 1) / u32::BITS; - if old_size < new_size { - self.storage.grow(new_size - old_size, 0); + #[unstable = "matches collection reform specification, waiting for dust to settle"] + pub fn reserve(&mut self, additional: uint) { + let desired_cap = self.len().checked_add(additional).expect("capacity overflow"); + match self.storage.len().checked_mul(u32::BITS) { + None => {} // Vec has more initialized capacity than we can ever use + Some(initialized_cap) => { + if desired_cap > initialized_cap { + self.storage.reserve(blocks_for_bits(desired_cap - initialized_cap)); + } + } + } + } + + /// Reserves the minimum capacity for exactly `additional` more bits to be inserted in the + /// given `Bitv`. Does nothing if the capacity is already sufficient. + /// + /// Note that the allocator may give the collection more space than it requests. Therefore + /// capacity can not be relied upon to be precisely minimal. Prefer `reserve` if future + /// insertions are expected. + /// + /// # Panics + /// + /// Panics if the new capacity overflows `uint`. + /// + /// # Examples + /// + /// ``` + /// use std::collections::Bitv; + /// + /// let mut bv = Bitv::from_elem(3, false); + /// bv.reserve(10); + /// assert_eq!(bv.len(), 3); + /// assert!(bv.capacity() >= 13); + /// ``` + #[unstable = "matches collection reform specification, waiting for dust to settle"] + pub fn reserve_exact(&mut self, additional: uint) { + let desired_cap = self.len().checked_add(additional).expect("capacity overflow"); + match self.storage.len().checked_mul(u32::BITS) { + None => {} // Vec has more initialized capacity than we can ever use + Some(initialized_cap) => { + if desired_cap > initialized_cap { + self.storage.reserve_exact(blocks_for_bits(desired_cap - initialized_cap)); + } + } } } @@ -664,83 +758,89 @@ pub fn reserve(&mut self, size: uint) { /// assert!(bv.capacity() >= 10); /// ``` #[inline] + #[unstable = "matches collection reform specification, waiting for dust to settle"] pub fn capacity(&self) -> uint { - self.storage.len() * u32::BITS + self.storage.capacity().checked_mul(u32::BITS).unwrap_or(uint::MAX) } /// Grows the `Bitv` in-place, adding `n` copies of `value` to the `Bitv`. /// + /// # Panics + /// + /// Panics if the new len overflows a `uint`. + /// /// # Examples /// /// ``` - /// use std::collections::bitv; + /// use std::collections::Bitv; /// - /// let mut bv = bitv::from_bytes(&[0b01001011]); + /// let mut bv = Bitv::from_bytes(&[0b01001011]); /// bv.grow(2, true); /// assert_eq!(bv.len(), 10); /// assert_eq!(bv.to_bytes(), vec!(0b01001011, 0b11000000)); /// ``` pub fn grow(&mut self, n: uint, value: bool) { - let new_nbits = self.nbits + n; - let new_nwords = (new_nbits + u32::BITS - 1) / u32::BITS; + // Note: we just bulk set all the bits in the last word in this fn in multiple places + // which is technically wrong if not all of these bits are to be used. However, at the end + // of this fn we call `fix_last_block` at the end of this fn, which should fix this. + + let new_nbits = self.nbits.checked_add(n).expect("capacity overflow"); + let new_nblocks = blocks_for_bits(new_nbits); let full_value = if value { !0 } else { 0 }; + // Correct the old tail word - let old_last_word = (self.nbits + u32::BITS - 1) / u32::BITS - 1; + let old_last_word = blocks_for_bits(self.nbits) - 1; if self.nbits % u32::BITS > 0 { let overhang = self.nbits % u32::BITS; // # of already-used bits - let mask = !((1 << overhang) - 1); // e.g. 5 unused bits => 111110....0 + let mask = !((1 << overhang) - 1); // e.g. 5 unused bits => 111110..0 if value { self.storage[old_last_word] |= mask; } else { self.storage[old_last_word] &= !mask; } } + // Fill in words after the old tail word - let stop_idx = cmp::min(self.storage.len(), new_nwords); + let stop_idx = cmp::min(self.storage.len(), new_nblocks); for idx in range(old_last_word + 1, stop_idx) { self.storage[idx] = full_value; } - // Allocate new words, if needed - if new_nwords > self.storage.len() { - let to_add = new_nwords - self.storage.len(); - self.storage.grow(to_add, full_value); - // Zero out and unused bits in the new tail word - if value { - let tail_word = new_nwords - 1; - let used_bits = new_nbits % u32::BITS; - self.storage[tail_word] &= (1 << used_bits) - 1; - } + // Allocate new words, if needed + if new_nblocks > self.storage.len() { + let to_add = new_nblocks - self.storage.len(); + self.storage.grow(to_add, full_value); } + // Adjust internal bit count self.nbits = new_nbits; + + self.fix_last_block(); } - /// Shortens by one element and returns the removed element. - /// - /// # Panics - /// - /// Assert if empty. + /// Removes the last bit from the Bitv, and returns it. Returns None if the Bitv is empty. /// /// # Examples /// /// ``` - /// use std::collections::bitv; + /// use std::collections::Bitv; /// - /// let mut bv = bitv::from_bytes(&[0b01001001]); - /// assert_eq!(bv.pop(), true); - /// assert_eq!(bv.pop(), false); + /// let mut bv = Bitv::from_bytes(&[0b01001001]); + /// assert_eq!(bv.pop(), Some(true)); + /// assert_eq!(bv.pop(), Some(false)); /// assert_eq!(bv.len(), 6); - /// assert_eq!(bv.to_bytes(), vec!(0b01001000)); /// ``` - pub fn pop(&mut self) -> bool { - let ret = self.get(self.nbits - 1); - // If we are unusing a whole word, make sure it is zeroed out - if self.nbits % u32::BITS == 1 { - self.storage[self.nbits / u32::BITS] = 0; + #[unstable = "matches collection reform specification, waiting for dust to settle"] + pub fn pop(&mut self) -> Option { + if self.is_empty() { + None + } else { + let ret = self[self.nbits - 1]; + // Second rule of Bitv Club + self.set(self.nbits - 1, false); + self.nbits -= 1; + Some(ret) } - self.nbits -= 1; - ret } /// Pushes a `bool` onto the end. @@ -755,10 +855,11 @@ pub fn pop(&mut self) -> bool { /// bv.push(false); /// assert!(bv.eq_vec(&[true, false])); /// ``` + #[unstable = "matches collection reform specification, waiting for dust to settle"] pub fn push(&mut self, elem: bool) { let insert_pos = self.nbits; - self.nbits += 1; - if self.storage.len() * u32::BITS < self.nbits { + self.nbits = self.nbits.checked_add(1).expect("Capacity overflow"); + if self.storage.len().checked_mul(u32::BITS).unwrap_or(uint::MAX) < self.nbits { self.storage.push(0); } self.set(insert_pos, elem); @@ -782,46 +883,16 @@ pub fn clear(&mut self) { } } -/// Transforms a byte-vector into a `Bitv`. Each byte becomes eight bits, -/// with the most significant bits of each byte coming first. Each -/// bit becomes `true` if equal to 1 or `false` if equal to 0. -/// -/// # Examples -/// -/// ``` -/// use std::collections::bitv; -/// -/// let bv = bitv::from_bytes(&[0b10100000, 0b00010010]); -/// assert!(bv.eq_vec(&[true, false, true, false, -/// false, false, false, false, -/// false, false, false, true, -/// false, false, true, false])); -/// ``` +/// Deprecated: Now a static method on Bitv. +#[deprecated = "Now a static method on Bitv"] pub fn from_bytes(bytes: &[u8]) -> Bitv { - from_fn(bytes.len() * 8, |i| { - let b = bytes[i / 8] as u32; - let offset = i % 8; - b >> (7 - offset) & 1 == 1 - }) + Bitv::from_bytes(bytes) } -/// Creates a `Bitv` of the specified length where the value at each -/// index is `f(index)`. -/// -/// # Examples -/// -/// ``` -/// use std::collections::bitv::from_fn; -/// -/// let bv = from_fn(5, |i| { i % 2 == 0 }); -/// assert!(bv.eq_vec(&[true, false, true, false, true])); -/// ``` +/// Deprecated: Now a static method on Bitv. +#[deprecated = "Now a static method on Bitv"] pub fn from_fn(len: uint, mut f: F) -> Bitv where F: FnMut(uint) -> bool { - let mut bitv = Bitv::with_capacity(len, false); - for i in range(0u, len) { - bitv.set(i, f(i)); - } - bitv + Bitv::from_fn(len, f) } #[stable] @@ -843,8 +914,7 @@ impl Extend for Bitv { #[inline] fn extend>(&mut self, mut iterator: I) { let (min, _) = iterator.size_hint(); - let nbits = self.nbits; - self.reserve(nbits + min); + self.reserve(min); for element in iterator { self.push(element) } @@ -890,7 +960,7 @@ fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { impl hash::Hash for Bitv { fn hash(&self, state: &mut S) { self.nbits.hash(state); - for (_, elem) in self.mask_words(0) { + for elem in self.blocks() { elem.hash(state); } } @@ -902,7 +972,7 @@ fn eq(&self, other: &Bitv) -> bool { if self.nbits != other.nbits { return false; } - self.mask_words(0).zip(other.mask_words(0)).all(|((_, w1), (_, w2))| w1 == w2) + self.blocks().zip(other.blocks()).all(|(w1, w2)| w1 == w2) } } @@ -921,7 +991,7 @@ fn next(&mut self) -> Option { if self.next_idx != self.end_idx { let idx = self.next_idx; self.next_idx += 1; - Some(self.bitv.get(idx)) + Some(self.bitv[idx]) } else { None } @@ -938,7 +1008,7 @@ impl<'a> DoubleEndedIterator for Bits<'a> { fn next_back(&mut self) -> Option { if self.next_idx != self.end_idx { self.end_idx -= 1; - Some(self.bitv.get(self.end_idx)) + Some(self.bitv[self.end_idx]) } else { None } @@ -958,7 +1028,7 @@ fn idx(&mut self, index: uint) -> Option { if index >= self.indexable() { None } else { - Some(self.bitv.get(index)) + Some(self.bitv[index]) } } } @@ -974,7 +1044,6 @@ fn idx(&mut self, index: uint) -> Option { /// /// ``` /// use std::collections::{BitvSet, Bitv}; -/// use std::collections::bitv; /// /// // It's a regular set /// let mut s = BitvSet::new(); @@ -989,7 +1058,7 @@ fn idx(&mut self, index: uint) -> Option { /// } /// /// // Can initialize from a `Bitv` -/// let other = BitvSet::from_bitv(bitv::from_bytes(&[0b11010000])); +/// let other = BitvSet::from_bitv(Bitv::from_bytes(&[0b11010000])); /// /// s.union_with(&other); /// @@ -1000,10 +1069,12 @@ fn idx(&mut self, index: uint) -> Option { /// /// // Can convert back to a `Bitv` /// let bv: Bitv = s.into_bitv(); -/// assert!(bv.get(3)); +/// assert!(bv[3]); /// ``` #[deriving(Clone)] -pub struct BitvSet(Bitv); +pub struct BitvSet { + bitv: Bitv, +} impl Default for BitvSet { #[inline] @@ -1021,8 +1092,7 @@ fn from_iter>(iterator: I) -> BitvSet { impl Extend for BitvSet { #[inline] fn extend>(&mut self, iterator: I) { - let &BitvSet(ref mut self_bitv) = self; - self_bitv.extend(iterator); + self.bitv.extend(iterator); } } @@ -1053,45 +1123,47 @@ fn eq(&self, other: &BitvSet) -> bool { impl cmp::Eq for BitvSet {} impl BitvSet { - /// Creates a new bit vector set with initially no contents. + /// Creates a new empty `BitvSet`. /// /// # Examples /// /// ``` /// use std::collections::BitvSet; + /// /// let mut s = BitvSet::new(); /// ``` #[inline] #[unstable = "matches collection reform specification, waiting for dust to settle"] pub fn new() -> BitvSet { - BitvSet(Bitv::new()) + BitvSet { bitv: Bitv::new() } } - /// Creates a new bit vector set with initially no contents, able to + /// Creates a new `BitvSet` with initially no contents, able to /// hold `nbits` elements without resizing. /// /// # Examples /// /// ``` /// use std::collections::BitvSet; + /// /// let mut s = BitvSet::with_capacity(100); /// assert!(s.capacity() >= 100); /// ``` #[inline] #[unstable = "matches collection reform specification, waiting for dust to settle"] pub fn with_capacity(nbits: uint) -> BitvSet { - let bitv = Bitv::with_capacity(nbits, false); + let bitv = Bitv::from_elem(nbits, false); BitvSet::from_bitv(bitv) } - /// Creates a new bit vector set from the given bit vector. + /// Creates a new `BitvSet` from the given bit vector. /// /// # Examples /// /// ``` - /// use std::collections::{bitv, BitvSet}; + /// use std::collections::{Bitv, BitvSet}; /// - /// let bv = bitv::from_bytes(&[0b01100000]); + /// let bv = Bitv::from_bytes(&[0b01100000]); /// let s = BitvSet::from_bitv(bv); /// /// // Print 1, 2 in arbitrary order @@ -1100,10 +1172,8 @@ pub fn with_capacity(nbits: uint) -> BitvSet { /// } /// ``` #[inline] - pub fn from_bitv(mut bitv: Bitv) -> BitvSet { - // Mark every bit as valid - bitv.nbits = bitv.capacity(); - BitvSet(bitv) + pub fn from_bitv(bitv: Bitv) -> BitvSet { + BitvSet { bitv: bitv } } /// Returns the capacity in bits for this bit vector. Inserting any @@ -1120,11 +1190,15 @@ pub fn from_bitv(mut bitv: Bitv) -> BitvSet { #[inline] #[unstable = "matches collection reform specification, waiting for dust to settle"] pub fn capacity(&self) -> uint { - let &BitvSet(ref bitv) = self; - bitv.capacity() + self.bitv.capacity() } - /// Grows the underlying vector to be able to store `size` bits. + /// Reserves capacity for an element to be inserted at `index` in the given + /// `Bitv`. The collection may reserve more space to avoid frequent reallocations. + /// + /// # Panics + /// + /// Panics if the new capacity overflows `uint`. /// /// # Examples /// @@ -1132,17 +1206,46 @@ pub fn capacity(&self) -> uint { /// use std::collections::BitvSet; /// /// let mut s = BitvSet::new(); - /// s.reserve(10); - /// assert!(s.capacity() >= 10); + /// s.reserve_index(10); + /// assert!(s.capacity() >= 11); /// ``` - pub fn reserve(&mut self, size: uint) { - let &BitvSet(ref mut bitv) = self; - bitv.reserve(size); - if bitv.nbits < size { - bitv.nbits = bitv.capacity(); + #[unstable = "matches collection reform specification, waiting for dust to settle"] + pub fn reserve_index(&mut self, index: uint) { + let len = self.bitv.len(); + if index >= len { + self.bitv.reserve(index - len + 1); } } + /// Reserves the minimum capacity for an element to be inserted at `index` + /// in the given `BitvSet`. Does nothing if the capacity is already sufficient. + /// + /// Note that the allocator may give the collection more space than it requests. Therefore + /// capacity can not be relied upon to be precisely minimal. Prefer `reserve_index` if future + /// insertions are expected. + /// + /// # Panics + /// + /// Panics if the new capacity overflows `uint`. + /// + /// # Examples + /// + /// ``` + /// use std::collections::BitvSet; + /// + /// let mut s = BitvSet::new(); + /// s.reserve_index_exact(10); + /// assert!(s.capacity() >= 11); + /// ``` + #[unstable = "matches collection reform specification, waiting for dust to settle"] + pub fn reserve_index_exact(&mut self, index: uint) { + let len = self.bitv.len(); + if index >= len { + self.bitv.reserve_exact(index - len + 1); + } + } + + /// Consumes this set to return the underlying bit vector. /// /// # Examples @@ -1155,13 +1258,12 @@ pub fn reserve(&mut self, size: uint) { /// s.insert(3); /// /// let bv = s.into_bitv(); - /// assert!(bv.get(0)); - /// assert!(bv.get(3)); + /// assert!(bv[0]); + /// assert!(bv[3]); /// ``` #[inline] pub fn into_bitv(self) -> Bitv { - let BitvSet(bitv) = self; - bitv + self.bitv } /// Returns a reference to the underlying bit vector. @@ -1179,18 +1281,22 @@ pub fn into_bitv(self) -> Bitv { /// ``` #[inline] pub fn get_ref<'a>(&'a self) -> &'a Bitv { - let &BitvSet(ref bitv) = self; - bitv + &self.bitv } #[inline] fn other_op(&mut self, other: &BitvSet, mut f: F) where F: FnMut(u32, u32) -> u32 { - // Expand the vector if necessary - self.reserve(other.capacity()); - // Unwrap Bitvs - let &BitvSet(ref mut self_bitv) = self; - let &BitvSet(ref other_bitv) = other; + let self_bitv = &mut self.bitv; + let other_bitv = &other.bitv; + + let self_len = self_bitv.len(); + let other_len = other_bitv.len(); + + // Expand the vector if necessary + if self_len < other_len { + self_bitv.grow(other_len - self_len, false); + } // virtually pad other with 0's for equal lengths let mut other_words = { @@ -1227,7 +1333,7 @@ fn other_op(&mut self, other: &BitvSet, mut f: F) where F: FnMut(u32, u32) -> #[inline] #[unstable = "matches collection reform specification, waiting for dust to settle"] pub fn shrink_to_fit(&mut self) { - let &BitvSet(ref mut bitv) = self; + let bitv = &mut self.bitv; // Obtain original length let old_len = bitv.storage.len(); // Obtain coarse trailing zero length @@ -1243,10 +1349,9 @@ pub fn shrink_to_fit(&mut self) { /// # Examples /// /// ``` - /// use std::collections::BitvSet; - /// use std::collections::bitv; + /// use std::collections::{Bitv, BitvSet}; /// - /// let s = BitvSet::from_bitv(bitv::from_bytes(&[0b01001010])); + /// let s = BitvSet::from_bitv(Bitv::from_bytes(&[0b01001010])); /// /// // Print 1, 4, 6 in arbitrary order /// for x in s.iter() { @@ -1265,11 +1370,10 @@ pub fn iter<'a>(&'a self) -> BitPositions<'a> { /// # Examples /// /// ``` - /// use std::collections::BitvSet; - /// use std::collections::bitv; + /// use std::collections::{Bitv, BitvSet}; /// - /// let a = BitvSet::from_bitv(bitv::from_bytes(&[0b01101000])); - /// let b = BitvSet::from_bitv(bitv::from_bytes(&[0b10100000])); + /// let a = BitvSet::from_bitv(Bitv::from_bytes(&[0b01101000])); + /// let b = BitvSet::from_bitv(Bitv::from_bytes(&[0b10100000])); /// /// // Print 0, 1, 2, 4 in arbitrary order /// for x in a.union(&b) { @@ -1296,11 +1400,10 @@ fn or(w1: u32, w2: u32) -> u32 { w1 | w2 } /// # Examples /// /// ``` - /// use std::collections::BitvSet; - /// use std::collections::bitv; + /// use std::collections::{Bitv, BitvSet}; /// - /// let a = BitvSet::from_bitv(bitv::from_bytes(&[0b01101000])); - /// let b = BitvSet::from_bitv(bitv::from_bytes(&[0b10100000])); + /// let a = BitvSet::from_bitv(Bitv::from_bytes(&[0b01101000])); + /// let b = BitvSet::from_bitv(Bitv::from_bytes(&[0b10100000])); /// /// // Print 2 /// for x in a.intersection(&b) { @@ -1311,8 +1414,7 @@ fn or(w1: u32, w2: u32) -> u32 { w1 | w2 } #[unstable = "matches collection reform specification, waiting for dust to settle"] pub fn intersection<'a>(&'a self, other: &'a BitvSet) -> Take> { fn bitand(w1: u32, w2: u32) -> u32 { w1 & w2 } - - let min = cmp::min(self.capacity(), other.capacity()); + let min = cmp::min(self.bitv.len(), other.bitv.len()); TwoBitPositions { set: self, other: other, @@ -1328,11 +1430,10 @@ fn bitand(w1: u32, w2: u32) -> u32 { w1 & w2 } /// # Examples /// /// ``` - /// use std::collections::BitvSet; - /// use std::collections::bitv; + /// use std::collections::{BitvSet, Bitv}; /// - /// let a = BitvSet::from_bitv(bitv::from_bytes(&[0b01101000])); - /// let b = BitvSet::from_bitv(bitv::from_bytes(&[0b10100000])); + /// let a = BitvSet::from_bitv(Bitv::from_bytes(&[0b01101000])); + /// let b = BitvSet::from_bitv(Bitv::from_bytes(&[0b10100000])); /// /// // Print 1, 4 in arbitrary order /// for x in a.difference(&b) { @@ -1367,11 +1468,10 @@ fn diff(w1: u32, w2: u32) -> u32 { w1 & !w2 } /// # Examples /// /// ``` - /// use std::collections::BitvSet; - /// use std::collections::bitv; + /// use std::collections::{BitvSet, Bitv}; /// - /// let a = BitvSet::from_bitv(bitv::from_bytes(&[0b01101000])); - /// let b = BitvSet::from_bitv(bitv::from_bytes(&[0b10100000])); + /// let a = BitvSet::from_bitv(Bitv::from_bytes(&[0b01101000])); + /// let b = BitvSet::from_bitv(Bitv::from_bytes(&[0b10100000])); /// /// // Print 0, 1, 4 in arbitrary order /// for x in a.symmetric_difference(&b) { @@ -1397,16 +1497,15 @@ fn bitxor(w1: u32, w2: u32) -> u32 { w1 ^ w2 } /// # Examples /// /// ``` - /// use std::collections::BitvSet; - /// use std::collections::bitv; + /// use std::collections::{BitvSet, Bitv}; /// /// let a = 0b01101000; /// let b = 0b10100000; /// let res = 0b11101000; /// - /// let mut a = BitvSet::from_bitv(bitv::from_bytes(&[a])); - /// let b = BitvSet::from_bitv(bitv::from_bytes(&[b])); - /// let res = BitvSet::from_bitv(bitv::from_bytes(&[res])); + /// let mut a = BitvSet::from_bitv(Bitv::from_bytes(&[a])); + /// let b = BitvSet::from_bitv(Bitv::from_bytes(&[b])); + /// let res = BitvSet::from_bitv(Bitv::from_bytes(&[res])); /// /// a.union_with(&b); /// assert_eq!(a, res); @@ -1421,16 +1520,15 @@ pub fn union_with(&mut self, other: &BitvSet) { /// # Examples /// /// ``` - /// use std::collections::BitvSet; - /// use std::collections::bitv; + /// use std::collections::{BitvSet, Bitv}; /// /// let a = 0b01101000; /// let b = 0b10100000; /// let res = 0b00100000; /// - /// let mut a = BitvSet::from_bitv(bitv::from_bytes(&[a])); - /// let b = BitvSet::from_bitv(bitv::from_bytes(&[b])); - /// let res = BitvSet::from_bitv(bitv::from_bytes(&[res])); + /// let mut a = BitvSet::from_bitv(Bitv::from_bytes(&[a])); + /// let b = BitvSet::from_bitv(Bitv::from_bytes(&[b])); + /// let res = BitvSet::from_bitv(Bitv::from_bytes(&[res])); /// /// a.intersect_with(&b); /// assert_eq!(a, res); @@ -1446,24 +1544,23 @@ pub fn intersect_with(&mut self, other: &BitvSet) { /// # Examples /// /// ``` - /// use std::collections::BitvSet; - /// use std::collections::bitv; + /// use std::collections::{BitvSet, Bitv}; /// /// let a = 0b01101000; /// let b = 0b10100000; /// let a_b = 0b01001000; // a - b /// let b_a = 0b10000000; // b - a /// - /// let mut bva = BitvSet::from_bitv(bitv::from_bytes(&[a])); - /// let bvb = BitvSet::from_bitv(bitv::from_bytes(&[b])); - /// let bva_b = BitvSet::from_bitv(bitv::from_bytes(&[a_b])); - /// let bvb_a = BitvSet::from_bitv(bitv::from_bytes(&[b_a])); + /// let mut bva = BitvSet::from_bitv(Bitv::from_bytes(&[a])); + /// let bvb = BitvSet::from_bitv(Bitv::from_bytes(&[b])); + /// let bva_b = BitvSet::from_bitv(Bitv::from_bytes(&[a_b])); + /// let bvb_a = BitvSet::from_bitv(Bitv::from_bytes(&[b_a])); /// /// bva.difference_with(&bvb); /// assert_eq!(bva, bva_b); /// - /// let bva = BitvSet::from_bitv(bitv::from_bytes(&[a])); - /// let mut bvb = BitvSet::from_bitv(bitv::from_bytes(&[b])); + /// let bva = BitvSet::from_bitv(Bitv::from_bytes(&[a])); + /// let mut bvb = BitvSet::from_bitv(Bitv::from_bytes(&[b])); /// /// bvb.difference_with(&bva); /// assert_eq!(bvb, bvb_a); @@ -1479,16 +1576,15 @@ pub fn difference_with(&mut self, other: &BitvSet) { /// # Examples /// /// ``` - /// use std::collections::BitvSet; - /// use std::collections::bitv; + /// use std::collections::{BitvSet, Bitv}; /// /// let a = 0b01101000; /// let b = 0b10100000; /// let res = 0b11001000; /// - /// let mut a = BitvSet::from_bitv(bitv::from_bytes(&[a])); - /// let b = BitvSet::from_bitv(bitv::from_bytes(&[b])); - /// let res = BitvSet::from_bitv(bitv::from_bytes(&[res])); + /// let mut a = BitvSet::from_bitv(Bitv::from_bytes(&[a])); + /// let b = BitvSet::from_bitv(Bitv::from_bytes(&[b])); + /// let res = BitvSet::from_bitv(Bitv::from_bytes(&[res])); /// /// a.symmetric_difference_with(&b); /// assert_eq!(a, res); @@ -1502,32 +1598,29 @@ pub fn symmetric_difference_with(&mut self, other: &BitvSet) { #[inline] #[unstable = "matches collection reform specification, waiting for dust to settle"] pub fn len(&self) -> uint { - let &BitvSet(ref bitv) = self; - bitv.storage.iter().fold(0, |acc, &n| acc + n.count_ones()) + self.bitv.blocks().fold(0, |acc, n| acc + n.count_ones()) } /// Returns whether there are no bits set in this set #[inline] #[unstable = "matches collection reform specification, waiting for dust to settle"] pub fn is_empty(&self) -> bool { - let &BitvSet(ref bitv) = self; - bitv.storage.iter().all(|&n| n == 0) + self.bitv.none() } /// Clears all bits in this set #[inline] #[unstable = "matches collection reform specification, waiting for dust to settle"] pub fn clear(&mut self) { - let &BitvSet(ref mut bitv) = self; - bitv.clear(); + self.bitv.clear(); } /// Returns `true` if this set contains the specified integer. #[inline] #[unstable = "matches collection reform specification, waiting for dust to settle"] pub fn contains(&self, value: &uint) -> bool { - let &BitvSet(ref bitv) = self; - *value < bitv.nbits && bitv.get(*value) + let bitv = &self.bitv; + *value < bitv.nbits && bitv[*value] } /// Returns `true` if the set has no elements in common with `other`. @@ -1542,14 +1635,14 @@ pub fn is_disjoint(&self, other: &BitvSet) -> bool { #[inline] #[unstable = "matches collection reform specification, waiting for dust to settle"] pub fn is_subset(&self, other: &BitvSet) -> bool { - let &BitvSet(ref self_bitv) = self; - let &BitvSet(ref other_bitv) = other; + let self_bitv = &self.bitv; + let other_bitv = &other.bitv; + let other_blocks = blocks_for_bits(other_bitv.len()); // Check that `self` intersect `other` is self - self_bitv.mask_words(0).zip(other_bitv.mask_words(0)) - .all(|((_, w1), (_, w2))| w1 & w2 == w1) && - // Check that `self` setminus `other` is empty - self_bitv.mask_words(other_bitv.storage.len()).all(|(_, w)| w == 0) + self_bitv.blocks().zip(other_bitv.blocks()).all(|(w1, w2)| w1 & w2 == w1) && + // Make sure if `self` has any more blocks than `other`, they're all 0 + self_bitv.blocks().skip(other_blocks).all(|w| w == 0) } /// Returns `true` if the set is a superset of another. @@ -1568,13 +1661,12 @@ pub fn insert(&mut self, value: uint) -> bool { } // Ensure we have enough space to hold the new element - if value >= self.capacity() { - let new_cap = cmp::max(value + 1, self.capacity() * 2); - self.reserve(new_cap); + let len = self.bitv.len(); + if value >= len { + self.bitv.grow(value - len + 1, false) } - let &BitvSet(ref mut bitv) = self; - bitv.set(value, true); + self.bitv.set(value, true); return true; } @@ -1585,8 +1677,9 @@ pub fn remove(&mut self, value: &uint) -> bool { if !self.contains(value) { return false; } - let &BitvSet(ref mut bitv) = self; - bitv.set(*value, false); + + self.bitv.set(*value, false); + return true; } } @@ -1631,7 +1724,7 @@ pub struct TwoBitPositions<'a> { impl<'a> Iterator for BitPositions<'a> { fn next(&mut self) -> Option { - while self.next_idx < self.set.capacity() { + while self.next_idx < self.set.bitv.len() { let idx = self.next_idx; self.next_idx += 1; @@ -1645,18 +1738,18 @@ fn next(&mut self) -> Option { #[inline] fn size_hint(&self) -> (uint, Option) { - (0, Some(self.set.capacity() - self.next_idx)) + (0, Some(self.set.bitv.len() - self.next_idx)) } } impl<'a> Iterator for TwoBitPositions<'a> { fn next(&mut self) -> Option { - while self.next_idx < self.set.capacity() || - self.next_idx < self.other.capacity() { + while self.next_idx < self.set.bitv.len() || + self.next_idx < self.other.bitv.len() { let bit_idx = self.next_idx % u32::BITS; if bit_idx == 0 { - let &BitvSet(ref s_bitv) = self.set; - let &BitvSet(ref o_bitv) = self.other; + let s_bitv = &self.set.bitv; + let o_bitv = &self.other.bitv; // Merging the two words is a bit of an awkward dance since // one Bitv might be longer than the other let word_idx = self.next_idx / u32::BITS; @@ -1679,11 +1772,15 @@ fn next(&mut self) -> Option { #[inline] fn size_hint(&self) -> (uint, Option) { - let cap = cmp::max(self.set.capacity(), self.other.capacity()); + let cap = cmp::max(self.set.bitv.len(), self.other.bitv.len()); (0, Some(cap - self.next_idx)) } } + + + + #[cfg(test)] mod tests { use prelude::*; @@ -1696,14 +1793,12 @@ mod tests { use super::{Bitv, BitvSet, from_fn, from_bytes}; use bitv; - static BENCH_BITS : uint = 1 << 14; - #[test] fn test_to_str() { let zerolen = Bitv::new(); assert_eq!(zerolen.to_string(), ""); - let eightbits = Bitv::with_capacity(8u, false); + let eightbits = Bitv::from_elem(8u, false); assert_eq!(eightbits.to_string(), "00000000") } @@ -1716,15 +1811,15 @@ fn test_0_elements() { #[test] fn test_1_element() { - let mut act = Bitv::with_capacity(1u, false); + let mut act = Bitv::from_elem(1u, false); assert!(act.eq_vec(&[false])); - act = Bitv::with_capacity(1u, true); + act = Bitv::from_elem(1u, true); assert!(act.eq_vec(&[true])); } #[test] fn test_2_elements() { - let mut b = bitv::Bitv::with_capacity(2, false); + let mut b = Bitv::from_elem(2, false); b.set(0, true); b.set(1, false); assert_eq!(b.to_string(), "10"); @@ -1735,16 +1830,16 @@ fn test_10_elements() { let mut act; // all 0 - act = Bitv::with_capacity(10u, false); + act = Bitv::from_elem(10u, false); assert!((act.eq_vec( &[false, false, false, false, false, false, false, false, false, false]))); // all 1 - act = Bitv::with_capacity(10u, true); + act = Bitv::from_elem(10u, true); assert!((act.eq_vec(&[true, true, true, true, true, true, true, true, true, true]))); // mixed - act = Bitv::with_capacity(10u, false); + act = Bitv::from_elem(10u, false); act.set(0u, true); act.set(1u, true); act.set(2u, true); @@ -1753,7 +1848,7 @@ fn test_10_elements() { assert!((act.eq_vec(&[true, true, true, true, true, false, false, false, false, false]))); // mixed - act = Bitv::with_capacity(10u, false); + act = Bitv::from_elem(10u, false); act.set(5u, true); act.set(6u, true); act.set(7u, true); @@ -1762,7 +1857,7 @@ fn test_10_elements() { assert!((act.eq_vec(&[false, false, false, false, false, true, true, true, true, true]))); // mixed - act = Bitv::with_capacity(10u, false); + act = Bitv::from_elem(10u, false); act.set(0u, true); act.set(3u, true); act.set(6u, true); @@ -1775,21 +1870,21 @@ fn test_31_elements() { let mut act; // all 0 - act = Bitv::with_capacity(31u, false); + act = Bitv::from_elem(31u, false); assert!(act.eq_vec( &[false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false])); // all 1 - act = Bitv::with_capacity(31u, true); + act = Bitv::from_elem(31u, true); assert!(act.eq_vec( &[true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true])); // mixed - act = Bitv::with_capacity(31u, false); + act = Bitv::from_elem(31u, false); act.set(0u, true); act.set(1u, true); act.set(2u, true); @@ -1804,7 +1899,7 @@ fn test_31_elements() { false, false, false, false, false, false, false])); // mixed - act = Bitv::with_capacity(31u, false); + act = Bitv::from_elem(31u, false); act.set(16u, true); act.set(17u, true); act.set(18u, true); @@ -1819,7 +1914,7 @@ fn test_31_elements() { false, false, false, false, false, false, false])); // mixed - act = Bitv::with_capacity(31u, false); + act = Bitv::from_elem(31u, false); act.set(24u, true); act.set(25u, true); act.set(26u, true); @@ -1833,7 +1928,7 @@ fn test_31_elements() { false, false, true, true, true, true, true, true, true])); // mixed - act = Bitv::with_capacity(31u, false); + act = Bitv::from_elem(31u, false); act.set(3u, true); act.set(17u, true); act.set(30u, true); @@ -1848,21 +1943,21 @@ fn test_32_elements() { let mut act; // all 0 - act = Bitv::with_capacity(32u, false); + act = Bitv::from_elem(32u, false); assert!(act.eq_vec( &[false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false])); // all 1 - act = Bitv::with_capacity(32u, true); + act = Bitv::from_elem(32u, true); assert!(act.eq_vec( &[true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true])); // mixed - act = Bitv::with_capacity(32u, false); + act = Bitv::from_elem(32u, false); act.set(0u, true); act.set(1u, true); act.set(2u, true); @@ -1877,7 +1972,7 @@ fn test_32_elements() { false, false, false, false, false, false, false, false])); // mixed - act = Bitv::with_capacity(32u, false); + act = Bitv::from_elem(32u, false); act.set(16u, true); act.set(17u, true); act.set(18u, true); @@ -1892,7 +1987,7 @@ fn test_32_elements() { false, false, false, false, false, false, false, false])); // mixed - act = Bitv::with_capacity(32u, false); + act = Bitv::from_elem(32u, false); act.set(24u, true); act.set(25u, true); act.set(26u, true); @@ -1907,7 +2002,7 @@ fn test_32_elements() { false, false, true, true, true, true, true, true, true, true])); // mixed - act = Bitv::with_capacity(32u, false); + act = Bitv::from_elem(32u, false); act.set(3u, true); act.set(17u, true); act.set(30u, true); @@ -1923,21 +2018,21 @@ fn test_33_elements() { let mut act; // all 0 - act = Bitv::with_capacity(33u, false); + act = Bitv::from_elem(33u, false); assert!(act.eq_vec( &[false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false])); // all 1 - act = Bitv::with_capacity(33u, true); + act = Bitv::from_elem(33u, true); assert!(act.eq_vec( &[true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true])); // mixed - act = Bitv::with_capacity(33u, false); + act = Bitv::from_elem(33u, false); act.set(0u, true); act.set(1u, true); act.set(2u, true); @@ -1952,7 +2047,7 @@ fn test_33_elements() { false, false, false, false, false, false, false, false, false])); // mixed - act = Bitv::with_capacity(33u, false); + act = Bitv::from_elem(33u, false); act.set(16u, true); act.set(17u, true); act.set(18u, true); @@ -1967,7 +2062,7 @@ fn test_33_elements() { false, false, false, false, false, false, false, false, false])); // mixed - act = Bitv::with_capacity(33u, false); + act = Bitv::from_elem(33u, false); act.set(24u, true); act.set(25u, true); act.set(26u, true); @@ -1982,7 +2077,7 @@ fn test_33_elements() { false, false, true, true, true, true, true, true, true, true, false])); // mixed - act = Bitv::with_capacity(33u, false); + act = Bitv::from_elem(33u, false); act.set(3u, true); act.set(17u, true); act.set(30u, true); @@ -1996,24 +2091,24 @@ fn test_33_elements() { #[test] fn test_equal_differing_sizes() { - let v0 = Bitv::with_capacity(10u, false); - let v1 = Bitv::with_capacity(11u, false); + let v0 = Bitv::from_elem(10u, false); + let v1 = Bitv::from_elem(11u, false); assert!(v0 != v1); } #[test] fn test_equal_greatly_differing_sizes() { - let v0 = Bitv::with_capacity(10u, false); - let v1 = Bitv::with_capacity(110u, false); + let v0 = Bitv::from_elem(10u, false); + let v1 = Bitv::from_elem(110u, false); assert!(v0 != v1); } #[test] fn test_equal_sneaky_small() { - let mut a = bitv::Bitv::with_capacity(1, false); + let mut a = Bitv::from_elem(1, false); a.set(0, true); - let mut b = bitv::Bitv::with_capacity(1, true); + let mut b = Bitv::from_elem(1, true); b.set(0, true); assert_eq!(a, b); @@ -2021,12 +2116,12 @@ fn test_equal_sneaky_small() { #[test] fn test_equal_sneaky_big() { - let mut a = bitv::Bitv::with_capacity(100, false); + let mut a = Bitv::from_elem(100, false); for i in range(0u, 100) { a.set(i, true); } - let mut b = bitv::Bitv::with_capacity(100, true); + let mut b = Bitv::from_elem(100, true); for i in range(0u, 100) { b.set(i, true); } @@ -2036,18 +2131,18 @@ fn test_equal_sneaky_big() { #[test] fn test_from_bytes() { - let bitv = from_bytes(&[0b10110110, 0b00000000, 0b11111111]); + let bitv = Bitv::from_bytes(&[0b10110110, 0b00000000, 0b11111111]); let str = concat!("10110110", "00000000", "11111111"); assert_eq!(bitv.to_string(), str); } #[test] fn test_to_bytes() { - let mut bv = Bitv::with_capacity(3, true); + let mut bv = Bitv::from_elem(3, true); bv.set(1, false); assert_eq!(bv.to_bytes(), vec!(0b10100000)); - let mut bv = Bitv::with_capacity(9, false); + let mut bv = Bitv::from_elem(9, false); bv.set(2, true); bv.set(8, true); assert_eq!(bv.to_bytes(), vec!(0b00100000, 0b10000000)); @@ -2060,21 +2155,10 @@ fn test_from_bools() { assert_eq!(bitv.to_string(), "1011"); } - #[test] - fn test_bitv_set_from_bools() { - let bools = vec![true, false, true, true]; - let a: BitvSet = bools.iter().map(|n| *n).collect(); - let mut b = BitvSet::new(); - b.insert(0); - b.insert(2); - b.insert(3); - assert_eq!(a, b); - } - #[test] fn test_to_bools() { let bools = vec!(false, false, true, false, false, true, true, false); - assert_eq!(from_bytes(&[0b00100110]).iter().collect::>(), bools); + assert_eq!(Bitv::from_bytes(&[0b00100110]).iter().collect::>(), bools); } #[test] @@ -2120,49 +2204,386 @@ fn test_bitv_set_frombitv_init() { #[test] fn test_small_difference() { - let mut b1 = Bitv::with_capacity(3, false); - let mut b2 = Bitv::with_capacity(3, false); + let mut b1 = Bitv::from_elem(3, false); + let mut b2 = Bitv::from_elem(3, false); b1.set(0, true); b1.set(1, true); b2.set(1, true); b2.set(2, true); assert!(b1.difference(&b2)); - assert!(b1.get(0)); - assert!(!b1.get(1)); - assert!(!b1.get(2)); + assert!(b1[0]); + assert!(!b1[1]); + assert!(!b1[2]); } #[test] fn test_big_difference() { - let mut b1 = Bitv::with_capacity(100, false); - let mut b2 = Bitv::with_capacity(100, false); + let mut b1 = Bitv::from_elem(100, false); + let mut b2 = Bitv::from_elem(100, false); b1.set(0, true); b1.set(40, true); b2.set(40, true); b2.set(80, true); assert!(b1.difference(&b2)); - assert!(b1.get(0)); - assert!(!b1.get(40)); - assert!(!b1.get(80)); + assert!(b1[0]); + assert!(!b1[40]); + assert!(!b1[80]); } #[test] fn test_small_clear() { - let mut b = Bitv::with_capacity(14, true); + let mut b = Bitv::from_elem(14, true); b.clear(); assert!(b.none()); } #[test] fn test_big_clear() { - let mut b = Bitv::with_capacity(140, true); + let mut b = Bitv::from_elem(140, true); b.clear(); assert!(b.none()); } + #[test] + fn test_bitv_lt() { + let mut a = Bitv::from_elem(5u, false); + let mut b = Bitv::from_elem(5u, false); + + assert!(!(a < b) && !(b < a)); + b.set(2, true); + assert!(a < b); + a.set(3, true); + assert!(a < b); + a.set(2, true); + assert!(!(a < b) && b < a); + b.set(0, true); + assert!(a < b); + } + + #[test] + fn test_ord() { + let mut a = Bitv::from_elem(5u, false); + let mut b = Bitv::from_elem(5u, false); + + assert!(a <= b && a >= b); + a.set(1, true); + assert!(a > b && a >= b); + assert!(b < a && b <= a); + b.set(1, true); + b.set(2, true); + assert!(b > a && b >= a); + assert!(a < b && a <= b); + } + + + #[test] + fn test_small_bitv_tests() { + let v = Bitv::from_bytes(&[0]); + assert!(!v.all()); + assert!(!v.any()); + assert!(v.none()); + + let v = Bitv::from_bytes(&[0b00010100]); + assert!(!v.all()); + assert!(v.any()); + assert!(!v.none()); + + let v = Bitv::from_bytes(&[0xFF]); + assert!(v.all()); + assert!(v.any()); + assert!(!v.none()); + } + + #[test] + fn test_big_bitv_tests() { + let v = Bitv::from_bytes(&[ // 88 bits + 0, 0, 0, 0, + 0, 0, 0, 0, + 0, 0, 0]); + assert!(!v.all()); + assert!(!v.any()); + assert!(v.none()); + + let v = Bitv::from_bytes(&[ // 88 bits + 0, 0, 0b00010100, 0, + 0, 0, 0, 0b00110100, + 0, 0, 0]); + assert!(!v.all()); + assert!(v.any()); + assert!(!v.none()); + + let v = Bitv::from_bytes(&[ // 88 bits + 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF, 0xFF, + 0xFF, 0xFF, 0xFF]); + assert!(v.all()); + assert!(v.any()); + assert!(!v.none()); + } + + #[test] + fn test_bitv_push_pop() { + let mut s = Bitv::from_elem(5 * u32::BITS - 2, false); + assert_eq!(s.len(), 5 * u32::BITS - 2); + assert_eq!(s[5 * u32::BITS - 3], false); + s.push(true); + s.push(true); + assert_eq!(s[5 * u32::BITS - 2], true); + assert_eq!(s[5 * u32::BITS - 1], true); + // Here the internal vector will need to be extended + s.push(false); + assert_eq!(s[5 * u32::BITS], false); + s.push(false); + assert_eq!(s[5 * u32::BITS + 1], false); + assert_eq!(s.len(), 5 * u32::BITS + 2); + // Pop it all off + assert_eq!(s.pop(), Some(false)); + assert_eq!(s.pop(), Some(false)); + assert_eq!(s.pop(), Some(true)); + assert_eq!(s.pop(), Some(true)); + assert_eq!(s.len(), 5 * u32::BITS - 2); + } + + #[test] + fn test_bitv_truncate() { + let mut s = Bitv::from_elem(5 * u32::BITS, true); + + assert_eq!(s, Bitv::from_elem(5 * u32::BITS, true)); + assert_eq!(s.len(), 5 * u32::BITS); + s.truncate(4 * u32::BITS); + assert_eq!(s, Bitv::from_elem(4 * u32::BITS, true)); + assert_eq!(s.len(), 4 * u32::BITS); + // Truncating to a size > s.len() should be a noop + s.truncate(5 * u32::BITS); + assert_eq!(s, Bitv::from_elem(4 * u32::BITS, true)); + assert_eq!(s.len(), 4 * u32::BITS); + s.truncate(3 * u32::BITS - 10); + assert_eq!(s, Bitv::from_elem(3 * u32::BITS - 10, true)); + assert_eq!(s.len(), 3 * u32::BITS - 10); + s.truncate(0); + assert_eq!(s, Bitv::from_elem(0, true)); + assert_eq!(s.len(), 0); + } + + #[test] + fn test_bitv_reserve() { + let mut s = Bitv::from_elem(5 * u32::BITS, true); + // Check capacity + assert!(s.capacity() >= 5 * u32::BITS); + s.reserve(2 * u32::BITS); + assert!(s.capacity() >= 7 * u32::BITS); + s.reserve(7 * u32::BITS); + assert!(s.capacity() >= 12 * u32::BITS); + s.reserve_exact(7 * u32::BITS); + assert!(s.capacity() >= 12 * u32::BITS); + s.reserve(7 * u32::BITS + 1); + assert!(s.capacity() >= 12 * u32::BITS + 1); + // Check that length hasn't changed + assert_eq!(s.len(), 5 * u32::BITS); + s.push(true); + s.push(false); + s.push(true); + assert_eq!(s[5 * u32::BITS - 1], true); + assert_eq!(s[5 * u32::BITS - 0], true); + assert_eq!(s[5 * u32::BITS + 1], false); + assert_eq!(s[5 * u32::BITS + 2], true); + } + + #[test] + fn test_bitv_grow() { + let mut bitv = Bitv::from_bytes(&[0b10110110, 0b00000000, 0b10101010]); + bitv.grow(32, true); + assert_eq!(bitv, Bitv::from_bytes(&[0b10110110, 0b00000000, 0b10101010, + 0xFF, 0xFF, 0xFF, 0xFF])); + bitv.grow(64, false); + assert_eq!(bitv, Bitv::from_bytes(&[0b10110110, 0b00000000, 0b10101010, + 0xFF, 0xFF, 0xFF, 0xFF, 0, 0, 0, 0, 0, 0, 0, 0])); + bitv.grow(16, true); + assert_eq!(bitv, Bitv::from_bytes(&[0b10110110, 0b00000000, 0b10101010, + 0xFF, 0xFF, 0xFF, 0xFF, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF])); + } + + #[test] + fn test_bitv_extend() { + let mut bitv = Bitv::from_bytes(&[0b10110110, 0b00000000, 0b11111111]); + let ext = Bitv::from_bytes(&[0b01001001, 0b10010010, 0b10111101]); + bitv.extend(ext.iter()); + assert_eq!(bitv, Bitv::from_bytes(&[0b10110110, 0b00000000, 0b11111111, + 0b01001001, 0b10010010, 0b10111101])); + } +} + + + + +#[cfg(test)] +mod bitv_bench { + use std::prelude::*; + use std::rand; + use std::rand::Rng; + use std::u32; + use test::{Bencher, black_box}; + + use super::Bitv; + + static BENCH_BITS : uint = 1 << 14; + + fn rng() -> rand::IsaacRng { + let seed: &[_] = &[1, 2, 3, 4, 5, 6, 7, 8, 9, 0]; + rand::SeedableRng::from_seed(seed) + } + + #[bench] + fn bench_uint_small(b: &mut Bencher) { + let mut r = rng(); + let mut bitv = 0 as uint; + b.iter(|| { + for _ in range(0u, 100) { + bitv |= 1 << ((r.next_u32() as uint) % u32::BITS); + } + black_box(&bitv) + }); + } + + #[bench] + fn bench_bitv_set_big_fixed(b: &mut Bencher) { + let mut r = rng(); + let mut bitv = Bitv::from_elem(BENCH_BITS, false); + b.iter(|| { + for _ in range(0u, 100) { + bitv.set((r.next_u32() as uint) % BENCH_BITS, true); + } + black_box(&bitv) + }); + } + + #[bench] + fn bench_bitv_set_big_variable(b: &mut Bencher) { + let mut r = rng(); + let mut bitv = Bitv::from_elem(BENCH_BITS, false); + b.iter(|| { + for _ in range(0u, 100) { + bitv.set((r.next_u32() as uint) % BENCH_BITS, r.gen()); + } + black_box(&bitv); + }); + } + + #[bench] + fn bench_bitv_set_small(b: &mut Bencher) { + let mut r = rng(); + let mut bitv = Bitv::from_elem(u32::BITS, false); + b.iter(|| { + for _ in range(0u, 100) { + bitv.set((r.next_u32() as uint) % u32::BITS, true); + } + black_box(&bitv); + }); + } + + #[bench] + fn bench_bitv_big_union(b: &mut Bencher) { + let mut b1 = Bitv::from_elem(BENCH_BITS, false); + let b2 = Bitv::from_elem(BENCH_BITS, false); + b.iter(|| { + b1.union(&b2) + }) + } + + #[bench] + fn bench_bitv_small_iter(b: &mut Bencher) { + let bitv = Bitv::from_elem(u32::BITS, false); + b.iter(|| { + let mut sum = 0u; + for _ in range(0u, 10) { + for pres in bitv.iter() { + sum += pres as uint; + } + } + sum + }) + } + + #[bench] + fn bench_bitv_big_iter(b: &mut Bencher) { + let bitv = Bitv::from_elem(BENCH_BITS, false); + b.iter(|| { + let mut sum = 0u; + for pres in bitv.iter() { + sum += pres as uint; + } + sum + }) + } +} + + + + + + + +#[cfg(test)] +mod bitv_set_test { + use std::prelude::*; + use std::iter::range_step; + + use super::{Bitv, BitvSet}; + use vec::Vec; + + #[test] + fn test_bitv_set_show() { + let mut s = BitvSet::new(); + s.insert(1); + s.insert(10); + s.insert(50); + s.insert(2); + assert_eq!("{1, 2, 10, 50}", s.to_string()); + } + + #[test] + fn test_bitv_set_from_bools() { + let bools = vec![true, false, true, true]; + let a: BitvSet = bools.iter().map(|n| *n).collect(); + let mut b = BitvSet::new(); + b.insert(0); + b.insert(2); + b.insert(3); + assert_eq!(a, b); + } + + #[test] + fn test_bitv_set_iterator() { + let bools = [true, false, true, true]; + let bitv: BitvSet = bools.iter().map(|n| *n).collect(); + + let idxs: Vec = bitv.iter().collect(); + assert_eq!(idxs, vec!(0, 2, 3)); + + let long: BitvSet = range(0u, 10000).map(|n| n % 2 == 0).collect(); + let real = range_step(0, 10000, 2).collect::>(); + + let idxs: Vec = long.iter().collect(); + assert_eq!(idxs, real); + } + + #[test] + fn test_bitv_set_frombitv_init() { + let bools = [true, false]; + let lengths = [10, 64, 100]; + for &b in bools.iter() { + for &l in lengths.iter() { + let bitset = BitvSet::from_bitv(Bitv::from_elem(l, b)); + assert_eq!(bitset.contains(&1u), b) + assert_eq!(bitset.contains(&(l-1u)), b) + assert!(!bitset.contains(&l)) + } + } + } + #[test] fn test_bitv_masking() { - let b = Bitv::with_capacity(140, true); + let b = Bitv::from_elem(140, true); let mut bs = BitvSet::from_bitv(b); assert!(bs.contains(&139)); assert!(!bs.contains(&140)); @@ -2175,22 +2596,14 @@ fn test_bitv_masking() { #[test] fn test_bitv_set_basic() { - // calculate nbits with u32::BITS granularity - fn calc_nbits(bits: uint) -> uint { - u32::BITS * ((bits + u32::BITS - 1) / u32::BITS) - } - let mut b = BitvSet::new(); - assert_eq!(b.capacity(), calc_nbits(0)); assert!(b.insert(3)); - assert_eq!(b.capacity(), calc_nbits(3)); assert!(!b.insert(3)); assert!(b.contains(&3)); assert!(b.insert(4)); assert!(!b.insert(4)); assert!(b.contains(&3)); assert!(b.insert(400)); - assert_eq!(b.capacity(), calc_nbits(400)); assert!(!b.insert(400)); assert!(b.contains(&400)); assert_eq!(b.len(), 3); @@ -2312,10 +2725,10 @@ fn test_bitv_set_subset() { #[test] fn test_bitv_set_is_disjoint() { - let a = BitvSet::from_bitv(from_bytes(&[0b10100010])); - let b = BitvSet::from_bitv(from_bytes(&[0b01000000])); + let a = BitvSet::from_bitv(Bitv::from_bytes(&[0b10100010])); + let b = BitvSet::from_bitv(Bitv::from_bytes(&[0b01000000])); let c = BitvSet::new(); - let d = BitvSet::from_bitv(from_bytes(&[0b00110000])); + let d = BitvSet::from_bitv(Bitv::from_bytes(&[0b00110000])); assert!(!a.is_disjoint(&d)); assert!(!d.is_disjoint(&a)); @@ -2335,13 +2748,13 @@ fn test_bitv_set_union_with() { a.insert(0); let mut b = BitvSet::new(); b.insert(5); - let expected = BitvSet::from_bitv(from_bytes(&[0b10000100])); + let expected = BitvSet::from_bitv(Bitv::from_bytes(&[0b10000100])); a.union_with(&b); assert_eq!(a, expected); // Standard - let mut a = BitvSet::from_bitv(from_bytes(&[0b10100010])); - let mut b = BitvSet::from_bitv(from_bytes(&[0b01100010])); + let mut a = BitvSet::from_bitv(Bitv::from_bytes(&[0b10100010])); + let mut b = BitvSet::from_bitv(Bitv::from_bytes(&[0b01100010])); let c = a.clone(); a.union_with(&b); b.union_with(&c); @@ -2352,8 +2765,8 @@ fn test_bitv_set_union_with() { #[test] fn test_bitv_set_intersect_with() { // Explicitly 0'ed bits - let mut a = BitvSet::from_bitv(from_bytes(&[0b10100010])); - let mut b = BitvSet::from_bitv(from_bytes(&[0b00000000])); + let mut a = BitvSet::from_bitv(Bitv::from_bytes(&[0b10100010])); + let mut b = BitvSet::from_bitv(Bitv::from_bytes(&[0b00000000])); let c = a.clone(); a.intersect_with(&b); b.intersect_with(&c); @@ -2361,7 +2774,7 @@ fn test_bitv_set_intersect_with() { assert!(b.is_empty()); // Uninitialized bits should behave like 0's - let mut a = BitvSet::from_bitv(from_bytes(&[0b10100010])); + let mut a = BitvSet::from_bitv(Bitv::from_bytes(&[0b10100010])); let mut b = BitvSet::new(); let c = a.clone(); a.intersect_with(&b); @@ -2370,8 +2783,8 @@ fn test_bitv_set_intersect_with() { assert!(b.is_empty()); // Standard - let mut a = BitvSet::from_bitv(from_bytes(&[0b10100010])); - let mut b = BitvSet::from_bitv(from_bytes(&[0b01100010])); + let mut a = BitvSet::from_bitv(Bitv::from_bytes(&[0b10100010])); + let mut b = BitvSet::from_bitv(Bitv::from_bytes(&[0b01100010])); let c = a.clone(); a.intersect_with(&b); b.intersect_with(&c); @@ -2382,20 +2795,20 @@ fn test_bitv_set_intersect_with() { #[test] fn test_bitv_set_difference_with() { // Explicitly 0'ed bits - let mut a = BitvSet::from_bitv(from_bytes(&[0b00000000])); - let b = BitvSet::from_bitv(from_bytes(&[0b10100010])); + let mut a = BitvSet::from_bitv(Bitv::from_bytes(&[0b00000000])); + let b = BitvSet::from_bitv(Bitv::from_bytes(&[0b10100010])); a.difference_with(&b); assert!(a.is_empty()); // Uninitialized bits should behave like 0's let mut a = BitvSet::new(); - let b = BitvSet::from_bitv(from_bytes(&[0b11111111])); + let b = BitvSet::from_bitv(Bitv::from_bytes(&[0b11111111])); a.difference_with(&b); assert!(a.is_empty()); // Standard - let mut a = BitvSet::from_bitv(from_bytes(&[0b10100010])); - let mut b = BitvSet::from_bitv(from_bytes(&[0b01100010])); + let mut a = BitvSet::from_bitv(Bitv::from_bytes(&[0b10100010])); + let mut b = BitvSet::from_bitv(Bitv::from_bytes(&[0b01100010])); let c = a.clone(); a.difference_with(&b); b.difference_with(&c); @@ -2412,19 +2825,19 @@ fn test_bitv_set_symmetric_difference_with() { let mut b = BitvSet::new(); b.insert(1); b.insert(5); - let expected = BitvSet::from_bitv(from_bytes(&[0b10000100])); + let expected = BitvSet::from_bitv(Bitv::from_bytes(&[0b10000100])); a.symmetric_difference_with(&b); assert_eq!(a, expected); - let mut a = BitvSet::from_bitv(from_bytes(&[0b10100010])); + let mut a = BitvSet::from_bitv(Bitv::from_bytes(&[0b10100010])); let b = BitvSet::new(); let c = a.clone(); a.symmetric_difference_with(&b); assert_eq!(a, c); // Standard - let mut a = BitvSet::from_bitv(from_bytes(&[0b11100010])); - let mut b = BitvSet::from_bitv(from_bytes(&[0b01101010])); + let mut a = BitvSet::from_bitv(Bitv::from_bytes(&[0b11100010])); + let mut b = BitvSet::from_bitv(Bitv::from_bytes(&[0b01101010])); let c = a.clone(); a.symmetric_difference_with(&b); b.symmetric_difference_with(&c); @@ -2434,8 +2847,8 @@ fn test_bitv_set_symmetric_difference_with() { #[test] fn test_bitv_set_eq() { - let a = BitvSet::from_bitv(from_bytes(&[0b10100010])); - let b = BitvSet::from_bitv(from_bytes(&[0b00000000])); + let a = BitvSet::from_bitv(Bitv::from_bytes(&[0b10100010])); + let b = BitvSet::from_bitv(Bitv::from_bytes(&[0b00000000])); let c = BitvSet::new(); assert!(a == a); @@ -2448,8 +2861,8 @@ fn test_bitv_set_eq() { #[test] fn test_bitv_set_cmp() { - let a = BitvSet::from_bitv(from_bytes(&[0b10100010])); - let b = BitvSet::from_bitv(from_bytes(&[0b00000000])); + let a = BitvSet::from_bitv(Bitv::from_bytes(&[0b10100010])); + let b = BitvSet::from_bitv(Bitv::from_bytes(&[0b00000000])); let c = BitvSet::new(); assert_eq!(a.cmp(&b), Greater); @@ -2473,38 +2886,6 @@ fn test_bitv_remove() { assert!(a.insert(1000)); assert!(a.remove(&1000)); a.shrink_to_fit(); - assert_eq!(a.capacity(), u32::BITS); - } - - #[test] - fn test_bitv_lt() { - let mut a = Bitv::with_capacity(5u, false); - let mut b = Bitv::with_capacity(5u, false); - - assert!(!(a < b) && !(b < a)); - b.set(2, true); - assert!(a < b); - a.set(3, true); - assert!(a < b); - a.set(2, true); - assert!(!(a < b) && b < a); - b.set(0, true); - assert!(a < b); - } - - #[test] - fn test_ord() { - let mut a = Bitv::with_capacity(5u, false); - let mut b = Bitv::with_capacity(5u, false); - - assert!(a <= b && a >= b); - a.set(1, true); - assert!(a > b && a >= b); - assert!(b < a && b <= a); - b.set(1, true); - b.set(2, true); - assert!(b > a && b >= a); - assert!(a < b && a <= b); } #[test] @@ -2525,206 +2906,29 @@ fn test_bitv_clone() { assert!(a.remove(&1000)); assert!(b.contains(&1000)); } +} - #[test] - fn test_small_bitv_tests() { - let v = from_bytes(&[0]); - assert!(!v.all()); - assert!(!v.any()); - assert!(v.none()); - let v = from_bytes(&[0b00010100]); - assert!(!v.all()); - assert!(v.any()); - assert!(!v.none()); - let v = from_bytes(&[0xFF]); - assert!(v.all()); - assert!(v.any()); - assert!(!v.none()); - } - #[test] - fn test_big_bitv_tests() { - let v = from_bytes(&[ // 88 bits - 0, 0, 0, 0, - 0, 0, 0, 0, - 0, 0, 0]); - assert!(!v.all()); - assert!(!v.any()); - assert!(v.none()); - let v = from_bytes(&[ // 88 bits - 0, 0, 0b00010100, 0, - 0, 0, 0, 0b00110100, - 0, 0, 0]); - assert!(!v.all()); - assert!(v.any()); - assert!(!v.none()); +#[cfg(test)] +mod bitv_set_bench { + use std::prelude::*; + use std::rand; + use std::rand::Rng; + use std::u32; + use test::{Bencher, black_box}; - let v = from_bytes(&[ // 88 bits - 0xFF, 0xFF, 0xFF, 0xFF, - 0xFF, 0xFF, 0xFF, 0xFF, - 0xFF, 0xFF, 0xFF]); - assert!(v.all()); - assert!(v.any()); - assert!(!v.none()); - } + use super::{Bitv, BitvSet}; - #[test] - fn test_bitv_push_pop() { - let mut s = Bitv::with_capacity(5 * u32::BITS - 2, false); - assert_eq!(s.len(), 5 * u32::BITS - 2); - assert_eq!(s.get(5 * u32::BITS - 3), false); - s.push(true); - s.push(true); - assert_eq!(s.get(5 * u32::BITS - 2), true); - assert_eq!(s.get(5 * u32::BITS - 1), true); - // Here the internal vector will need to be extended - s.push(false); - assert_eq!(s.get(5 * u32::BITS), false); - s.push(false); - assert_eq!(s.get(5 * u32::BITS + 1), false); - assert_eq!(s.len(), 5 * u32::BITS + 2); - // Pop it all off - assert_eq!(s.pop(), false); - assert_eq!(s.pop(), false); - assert_eq!(s.pop(), true); - assert_eq!(s.pop(), true); - assert_eq!(s.len(), 5 * u32::BITS - 2); - } - - #[test] - fn test_bitv_truncate() { - let mut s = Bitv::with_capacity(5 * u32::BITS, true); - - assert_eq!(s, Bitv::with_capacity(5 * u32::BITS, true)); - assert_eq!(s.len(), 5 * u32::BITS); - s.truncate(4 * u32::BITS); - assert_eq!(s, Bitv::with_capacity(4 * u32::BITS, true)); - assert_eq!(s.len(), 4 * u32::BITS); - // Truncating to a size > s.len() should be a noop - s.truncate(5 * u32::BITS); - assert_eq!(s, Bitv::with_capacity(4 * u32::BITS, true)); - assert_eq!(s.len(), 4 * u32::BITS); - s.truncate(3 * u32::BITS - 10); - assert_eq!(s, Bitv::with_capacity(3 * u32::BITS - 10, true)); - assert_eq!(s.len(), 3 * u32::BITS - 10); - s.truncate(0); - assert_eq!(s, Bitv::with_capacity(0, true)); - assert_eq!(s.len(), 0); - } - - #[test] - fn test_bitv_reserve() { - let mut s = Bitv::with_capacity(5 * u32::BITS, true); - // Check capacity - assert_eq!(s.capacity(), 5 * u32::BITS); - s.reserve(2 * u32::BITS); - assert_eq!(s.capacity(), 5 * u32::BITS); - s.reserve(7 * u32::BITS); - assert_eq!(s.capacity(), 7 * u32::BITS); - s.reserve(7 * u32::BITS); - assert_eq!(s.capacity(), 7 * u32::BITS); - s.reserve(7 * u32::BITS + 1); - assert_eq!(s.capacity(), 8 * u32::BITS); - // Check that length hasn't changed - assert_eq!(s.len(), 5 * u32::BITS); - s.push(true); - s.push(false); - s.push(true); - assert_eq!(s.get(5 * u32::BITS - 1), true); - assert_eq!(s.get(5 * u32::BITS - 0), true); - assert_eq!(s.get(5 * u32::BITS + 1), false); - assert_eq!(s.get(5 * u32::BITS + 2), true); - } - - #[test] - fn test_bitv_grow() { - let mut bitv = from_bytes(&[0b10110110, 0b00000000, 0b10101010]); - bitv.grow(32, true); - assert_eq!(bitv, from_bytes(&[0b10110110, 0b00000000, 0b10101010, - 0xFF, 0xFF, 0xFF, 0xFF])); - bitv.grow(64, false); - assert_eq!(bitv, from_bytes(&[0b10110110, 0b00000000, 0b10101010, - 0xFF, 0xFF, 0xFF, 0xFF, 0, 0, 0, 0, 0, 0, 0, 0])); - bitv.grow(16, true); - assert_eq!(bitv, from_bytes(&[0b10110110, 0b00000000, 0b10101010, - 0xFF, 0xFF, 0xFF, 0xFF, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF])); - } - - #[test] - fn test_bitv_extend() { - let mut bitv = from_bytes(&[0b10110110, 0b00000000, 0b11111111]); - let ext = from_bytes(&[0b01001001, 0b10010010, 0b10111101]); - bitv.extend(ext.iter()); - assert_eq!(bitv, from_bytes(&[0b10110110, 0b00000000, 0b11111111, - 0b01001001, 0b10010010, 0b10111101])); - } - - #[test] - fn test_bitv_set_show() { - let mut s = BitvSet::new(); - s.insert(1); - s.insert(10); - s.insert(50); - s.insert(2); - assert_eq!("{1, 2, 10, 50}", s.to_string()); - } + static BENCH_BITS : uint = 1 << 14; fn rng() -> rand::IsaacRng { let seed: &[_] = &[1, 2, 3, 4, 5, 6, 7, 8, 9, 0]; rand::SeedableRng::from_seed(seed) } - #[bench] - fn bench_uint_small(b: &mut Bencher) { - let mut r = rng(); - let mut bitv = 0 as uint; - b.iter(|| { - for _ in range(0u, 100) { - bitv |= 1 << ((r.next_u32() as uint) % u32::BITS); - } - black_box(&bitv) - }); - } - - #[bench] - fn bench_bitv_set_big_fixed(b: &mut Bencher) { - let mut r = rng(); - let mut bitv = Bitv::with_capacity(BENCH_BITS, false); - b.iter(|| { - for _ in range(0u, 100) { - bitv.set((r.next_u32() as uint) % BENCH_BITS, true); - } - black_box(&bitv) - }); - } - - #[bench] - fn bench_bitv_set_big_variable(b: &mut Bencher) { - let mut r = rng(); - let mut bitv = Bitv::with_capacity(BENCH_BITS, false); - b.iter(|| { - for _ in range(0u, 100) { - bitv.set((r.next_u32() as uint) % BENCH_BITS, r.gen()); - } - black_box(&bitv); - }); - } - - #[bench] - fn bench_bitv_set_small(b: &mut Bencher) { - let mut r = rng(); - let mut bitv = Bitv::with_capacity(u32::BITS, false); - b.iter(|| { - for _ in range(0u, 100) { - bitv.set((r.next_u32() as uint) % u32::BITS, true); - } - black_box(&bitv); - }); - } - #[bench] fn bench_bitvset_small(b: &mut Bencher) { let mut r = rng(); @@ -2749,44 +2953,9 @@ fn bench_bitvset_big(b: &mut Bencher) { }); } - #[bench] - fn bench_bitv_big_union(b: &mut Bencher) { - let mut b1 = Bitv::with_capacity(BENCH_BITS, false); - let b2 = Bitv::with_capacity(BENCH_BITS, false); - b.iter(|| { - b1.union(&b2) - }) - } - - #[bench] - fn bench_bitv_small_iter(b: &mut Bencher) { - let bitv = Bitv::with_capacity(u32::BITS, false); - b.iter(|| { - let mut sum = 0u; - for _ in range(0u, 10) { - for pres in bitv.iter() { - sum += pres as uint; - } - } - sum - }) - } - - #[bench] - fn bench_bitv_big_iter(b: &mut Bencher) { - let bitv = Bitv::with_capacity(BENCH_BITS, false); - b.iter(|| { - let mut sum = 0u; - for pres in bitv.iter() { - sum += pres as uint; - } - sum - }) - } - #[bench] fn bench_bitvset_iter(b: &mut Bencher) { - let bitv = BitvSet::from_bitv(from_fn(BENCH_BITS, + let bitv = BitvSet::from_bitv(Bitv::from_fn(BENCH_BITS, |idx| {idx % 3 == 0})); b.iter(|| { let mut sum = 0u; diff --git a/src/test/run-pass/bitv-perf-test.rs b/src/test/run-pass/bitv-perf-test.rs index 281167ff46c8..c5f69f249db6 100644 --- a/src/test/run-pass/bitv-perf-test.rs +++ b/src/test/run-pass/bitv-perf-test.rs @@ -13,8 +13,8 @@ use std::collections::Bitv; fn bitv_test() { - let mut v1 = box Bitv::with_capacity(31, false); - let v2 = box Bitv::with_capacity(31, true); + let mut v1 = box Bitv::from_elem(31, false); + let v2 = box Bitv::from_elem(31, true); v1.union(&*v2); } diff --git a/src/test/run-pass/issue-11736.rs b/src/test/run-pass/issue-11736.rs index 912a62b5b0f6..bc4ceb38de33 100644 --- a/src/test/run-pass/issue-11736.rs +++ b/src/test/run-pass/issue-11736.rs @@ -16,7 +16,7 @@ fn main() { // Generate sieve of Eratosthenes for n up to 1e6 let n = 1000000u; - let mut sieve = Bitv::with_capacity(n+1, true); + let mut sieve = Bitv::from_elem(n+1, true); let limit: uint = (n as f32).sqrt() as uint; for i in range(2, limit+1) { if sieve[i] { From 8f194de95d00fe540d848cc9a7d3e049ce3684ab Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Tue, 9 Dec 2014 21:47:10 -0800 Subject: [PATCH 02/44] bitv: correct build failures - Fix typos on Blocks and MutBlocks. - Use slice_to_mut() for creating blocks_mut(). - Deref the block parameter in get(). - Access nbits separately from mutating set in pop(). --- src/libcollections/bit.rs | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/libcollections/bit.rs b/src/libcollections/bit.rs index 41a5ccb7a064..9ea7d52b7c6f 100644 --- a/src/libcollections/bit.rs +++ b/src/libcollections/bit.rs @@ -87,8 +87,8 @@ use vec::Vec; -type Blocks<'a> = Cloned> -type MutBlocks<'a> MutItems<'a, u32>; +type Blocks<'a> = Cloned>; +type MutBlocks<'a> = MutItems<'a, u32>; type MatchWords<'a> = Chain>, Skip>>>>; // Take two BitV's, and return iterators of their words, where the shorter one @@ -199,7 +199,7 @@ fn process(&mut self, other: &Bitv, mut op: F) -> bool where F: FnMut(u32, u3 /// Iterator over mutable refs to the underlying blocks of data. fn blocks_mut(&mut self) -> MutBlocks { let blocks = blocks_for_bits(self.len()); - self.storage[..blocks].iter_mut() + self.storage.slice_to_mut(blocks).iter_mut() } /// Iterator over the underlying blocks of data @@ -336,7 +336,7 @@ pub fn get(&self, i: uint) -> Option { assert!(i < self.nbits); let w = i / u32::BITS; let b = i % u32::BITS; - self.storage.get(w).map(|block| + self.storage.get(w).map(|&block| (block & (1 << b)) != 0 ) } @@ -835,10 +835,11 @@ pub fn pop(&mut self) -> Option { if self.is_empty() { None } else { - let ret = self[self.nbits - 1]; + let i = self.nbits - 1; + let ret = self[i]; // Second rule of Bitv Club - self.set(self.nbits - 1, false); - self.nbits -= 1; + self.set(i, false); + self.nbits = i; Some(ret) } } From 3deb97f5d02ddbdbf20a587815fe84934cda948c Mon Sep 17 00:00:00 2001 From: Josh Stone Date: Tue, 9 Dec 2014 22:06:52 -0800 Subject: [PATCH 03/44] bitv: Fix all() for nbits that are multiples of u32::BITS The old logic would be ok with *either* 0 or all 1s in the last word, because it didn't compute a proper mask for the case where nbits is an exact multiple of u32::BITS. Add mask_for_bits() to compute this properly, and use it in all(). Add all/none assertions to most of the tests. Note in particular, the all-zero bitv in test_32_elements() was incorrectly all()==true before this patch. --- src/libcollections/bit.rs | 51 +++++++++++++++++++++++++++++++++------ 1 file changed, 43 insertions(+), 8 deletions(-) diff --git a/src/libcollections/bit.rs b/src/libcollections/bit.rs index 9ea7d52b7c6f..5cf5183d25d5 100644 --- a/src/libcollections/bit.rs +++ b/src/libcollections/bit.rs @@ -174,6 +174,12 @@ fn blocks_for_bits(bits: uint) -> uint { } +/// Computes the bitmask for the final word of the vector +fn mask_for_bits(bits: uint) -> u32 { + // Note especially that a perfect multiple of u32::BITS should mask all 1s. + !0u32 >> (u32::BITS - bits % u32::BITS) % u32::BITS +} + impl Bitv { /// Applies the given operation to the blocks of self and other, and sets self to /// be the result. @@ -526,7 +532,7 @@ pub fn all(&self) -> bool { last_word = elem; tmp == !0u32 // and then check the last one has enough ones - }) && (last_word == ((1 << self.nbits % u32::BITS) - 1) || last_word == !0u32) + }) && (last_word == mask_for_bits(self.nbits)) } /// Returns an iterator over the elements of the vector in order. @@ -788,15 +794,15 @@ pub fn grow(&mut self, n: uint, value: bool) { let new_nblocks = blocks_for_bits(new_nbits); let full_value = if value { !0 } else { 0 }; - // Correct the old tail word + // Correct the old tail word, setting or clearing formerly unused bits let old_last_word = blocks_for_bits(self.nbits) - 1; if self.nbits % u32::BITS > 0 { - let overhang = self.nbits % u32::BITS; // # of already-used bits - let mask = !((1 << overhang) - 1); // e.g. 5 unused bits => 111110..0 + let mask = mask_for_bits(self.nbits); if value { - self.storage[old_last_word] |= mask; + self.storage[old_last_word] |= !mask; } else { - self.storage[old_last_word] &= !mask; + // Extra bits are already supposed to be zero by invariant, but play it safe... + self.storage[old_last_word] &= mask; } } @@ -1808,14 +1814,17 @@ fn test_0_elements() { let act = Bitv::new(); let exp = Vec::from_elem(0u, false); assert!(act.eq_vec(exp.as_slice())); + assert!(act.none() && act.all()); } #[test] fn test_1_element() { let mut act = Bitv::from_elem(1u, false); assert!(act.eq_vec(&[false])); + assert!(act.none() && !act.all()); act = Bitv::from_elem(1u, true); assert!(act.eq_vec(&[true])); + assert!(!act.none() && act.all()); } #[test] @@ -1824,6 +1833,7 @@ fn test_2_elements() { b.set(0, true); b.set(1, false); assert_eq!(b.to_string(), "10"); + assert!(!b.none() && !b.all()); } #[test] @@ -1834,10 +1844,12 @@ fn test_10_elements() { act = Bitv::from_elem(10u, false); assert!((act.eq_vec( &[false, false, false, false, false, false, false, false, false, false]))); + assert!(act.none() && !act.all()); // all 1 act = Bitv::from_elem(10u, true); assert!((act.eq_vec(&[true, true, true, true, true, true, true, true, true, true]))); + assert!(!act.none() && act.all()); // mixed act = Bitv::from_elem(10u, false); @@ -1847,6 +1859,7 @@ fn test_10_elements() { act.set(3u, true); act.set(4u, true); assert!((act.eq_vec(&[true, true, true, true, true, false, false, false, false, false]))); + assert!(!act.none() && !act.all()); // mixed act = Bitv::from_elem(10u, false); @@ -1856,6 +1869,7 @@ fn test_10_elements() { act.set(8u, true); act.set(9u, true); assert!((act.eq_vec(&[false, false, false, false, false, true, true, true, true, true]))); + assert!(!act.none() && !act.all()); // mixed act = Bitv::from_elem(10u, false); @@ -1864,6 +1878,7 @@ fn test_10_elements() { act.set(6u, true); act.set(9u, true); assert!((act.eq_vec(&[true, false, false, true, false, false, true, false, false, true]))); + assert!(!act.none() && !act.all()); } #[test] @@ -1876,6 +1891,7 @@ fn test_31_elements() { &[false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false])); + assert!(act.none() && !act.all()); // all 1 act = Bitv::from_elem(31u, true); @@ -1883,6 +1899,7 @@ fn test_31_elements() { &[true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true])); + assert!(!act.none() && act.all()); // mixed act = Bitv::from_elem(31u, false); @@ -1898,6 +1915,7 @@ fn test_31_elements() { &[true, true, true, true, true, true, true, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false])); + assert!(!act.none() && !act.all()); // mixed act = Bitv::from_elem(31u, false); @@ -1913,6 +1931,7 @@ fn test_31_elements() { &[false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true, true, true, true, true, true, true, true, false, false, false, false, false, false, false])); + assert!(!act.none() && !act.all()); // mixed act = Bitv::from_elem(31u, false); @@ -1927,6 +1946,7 @@ fn test_31_elements() { &[false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true, true, true, true, true, true, true])); + assert!(!act.none() && !act.all()); // mixed act = Bitv::from_elem(31u, false); @@ -1937,6 +1957,7 @@ fn test_31_elements() { &[false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, true])); + assert!(!act.none() && !act.all()); } #[test] @@ -1949,6 +1970,7 @@ fn test_32_elements() { &[false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false])); + assert!(act.none() && !act.all()); // all 1 act = Bitv::from_elem(32u, true); @@ -1956,6 +1978,7 @@ fn test_32_elements() { &[true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true])); + assert!(!act.none() && act.all()); // mixed act = Bitv::from_elem(32u, false); @@ -1971,6 +1994,7 @@ fn test_32_elements() { &[true, true, true, true, true, true, true, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false])); + assert!(!act.none() && !act.all()); // mixed act = Bitv::from_elem(32u, false); @@ -1986,6 +2010,7 @@ fn test_32_elements() { &[false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true, true, true, true, true, true, true, true, false, false, false, false, false, false, false, false])); + assert!(!act.none() && !act.all()); // mixed act = Bitv::from_elem(32u, false); @@ -2001,6 +2026,7 @@ fn test_32_elements() { &[false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true, true, true, true, true, true, true, true])); + assert!(!act.none() && !act.all()); // mixed act = Bitv::from_elem(32u, false); @@ -2012,6 +2038,7 @@ fn test_32_elements() { &[false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, true, true])); + assert!(!act.none() && !act.all()); } #[test] @@ -2024,6 +2051,7 @@ fn test_33_elements() { &[false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false])); + assert!(act.none() && !act.all()); // all 1 act = Bitv::from_elem(33u, true); @@ -2031,6 +2059,7 @@ fn test_33_elements() { &[true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true])); + assert!(!act.none() && act.all()); // mixed act = Bitv::from_elem(33u, false); @@ -2046,6 +2075,7 @@ fn test_33_elements() { &[true, true, true, true, true, true, true, true, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false])); + assert!(!act.none() && !act.all()); // mixed act = Bitv::from_elem(33u, false); @@ -2061,6 +2091,7 @@ fn test_33_elements() { &[false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true, true, true, true, true, true, true, true, false, false, false, false, false, false, false, false, false])); + assert!(!act.none() && !act.all()); // mixed act = Bitv::from_elem(33u, false); @@ -2076,6 +2107,7 @@ fn test_33_elements() { &[false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, false, true, true, true, true, true, true, true, true, false])); + assert!(!act.none() && !act.all()); // mixed act = Bitv::from_elem(33u, false); @@ -2088,6 +2120,7 @@ fn test_33_elements() { &[false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, false, true, false, false, false, false, false, false, false, false, false, false, false, false, true, true, true])); + assert!(!act.none() && !act.all()); } #[test] @@ -2234,15 +2267,17 @@ fn test_big_difference() { #[test] fn test_small_clear() { let mut b = Bitv::from_elem(14, true); + assert!(!b.none() && b.all()); b.clear(); - assert!(b.none()); + assert!(b.none() && !b.all()); } #[test] fn test_big_clear() { let mut b = Bitv::from_elem(140, true); + assert!(!b.none() && b.all()); b.clear(); - assert!(b.none()); + assert!(b.none() && !b.all()); } #[test] From e84a3833077825893b5bf0fb5abf07bdbd58d988 Mon Sep 17 00:00:00 2001 From: Tobias Bucher Date: Sat, 13 Dec 2014 02:13:06 +0100 Subject: [PATCH 04/44] Add a new invariant to `Bitv` The length of the underlying vector must now be exactly as long as it needs to be. --- src/libcollections/bit.rs | 152 +++++++++++++++++++++----------------- 1 file changed, 83 insertions(+), 69 deletions(-) diff --git a/src/libcollections/bit.rs b/src/libcollections/bit.rs index 5cf5183d25d5..3f33d85ba569 100644 --- a/src/libcollections/bit.rs +++ b/src/libcollections/bit.rs @@ -8,18 +8,25 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -// FIXME(Gankro): Bitv and BitvSet are very tightly coupled. Ideally (for maintenance), -// they should be in separate files/modules, with BitvSet only using Bitv's public API. +// FIXME(Gankro): Bitv and BitvSet are very tightly coupled. Ideally (for +// maintenance), they should be in separate files/modules, with BitvSet only +// using Bitv's public API. This will be hard for performance though, because +// `Bitv` will not want to leak its internal representation while its internal +// representation as `u32`s must be assumed for best performance. -// First rule of Bitv club: almost everything can actually overflow because we're working with -// bits and not bytes. -// -// Second rule of Bitv club: the last "block" of bits may be partially used. We must ensure that -// those unused bits are zeroed out, as other methods will assume this is the case. It may be -// the case that this isn't a great design, but having "undefined" bits is headache-inducing. -// -// Third rule of Bitv club: BitvSet is fairly tightly coupled to Bitv's implementation details. -// Make sure any changes to Bitv are properly addressed in BitvSet. +// FIXME(tbu-): `Bitv`'s methods shouldn't be `union`, `intersection`, but +// rather `or` and `and`. + +// (1) Be careful, most things can overflow here because the amount of bits in +// memory can overflow `uint`. +// (2) Make sure that the underlying vector has no excess length: +// E. g. `nbits == 16`, `storage.len() == 2` would be excess length, +// because the last word isn't used at all. This is important because some +// methods rely on it (for *CORRECTNESS*). +// (3) Make sure that the unused bits in the last word are zeroed out, again +// other methods rely on it for *CORRECTNESS*. +// (4) `BitvSet` is tightly coupled with `Bitv`, so any changes you make in +// `Bitv` will need to be reflected in `BitvSet`. //! Collections implemented with bit vectors. //! @@ -82,10 +89,10 @@ use core::iter; use core::num::Int; use core::slice::{Items, MutItems}; -use core::{u32, uint}; -use std::hash; +use core::{u8, u32, uint}; -use vec::Vec; +use hash; +use Vec; type Blocks<'a> = Cloned>; type MutBlocks<'a> = MutItems<'a, u32>; @@ -181,17 +188,15 @@ fn mask_for_bits(bits: uint) -> u32 { } impl Bitv { - /// Applies the given operation to the blocks of self and other, and sets self to - /// be the result. + /// Applies the given operation to the blocks of self and other, and sets + /// self to be the result. This relies on the caller not to corrupt the + /// last word. #[inline] fn process(&mut self, other: &Bitv, mut op: F) -> bool where F: FnMut(u32, u32) -> u32 { - let len = other.storage.len(); - assert_eq!(self.storage.len(), len); + assert_eq!(self.len(), other.len()); + // This could theoretically be a `debug_assert!`. + assert_eq!(self.storage.len(), other.storage.len()); let mut changed = false; - // Notice: `a` is *not* masked here, which is fine as long as - // `op` is a bitwise operation, since any bits that should've - // been masked were fine to change anyway. `b` is masked to - // make sure its unmasked bits do not cause damage. for (a, b) in self.blocks_mut().zip(other.blocks()) { let w = op(*a, b); if *a != w { @@ -204,21 +209,20 @@ fn process(&mut self, other: &Bitv, mut op: F) -> bool where F: FnMut(u32, u3 /// Iterator over mutable refs to the underlying blocks of data. fn blocks_mut(&mut self) -> MutBlocks { - let blocks = blocks_for_bits(self.len()); - self.storage.slice_to_mut(blocks).iter_mut() + // (2) + self.storage.iter_mut() } /// Iterator over the underlying blocks of data fn blocks(&self) -> Blocks { - let blocks = blocks_for_bits(self.len()); - self.storage[..blocks].iter().cloned() + // (2) + self.storage.iter().cloned() } - /// An operation might screw up the unused bits in the last block of the Bitv. - /// It's assumed to be all 0's. This fixes it up. + /// An operation might screw up the unused bits in the last block of the + /// `Bitv`. As per (3), it's assumed to be all 0s. This method fixes it up. fn fix_last_block(&mut self) { - let len = self.len(); - let extra_bits = len % u32::BITS; + let extra_bits = self.len() % u32::BITS; if extra_bits > 0 { let mask = (1 << extra_bits) - 1; let storage_len = self.storage.len(); @@ -259,7 +263,6 @@ pub fn from_elem(nbits: uint, bit: bool) -> Bitv { storage: Vec::from_elem(nblocks, if bit { !0u32 } else { 0u32 }), nbits: nbits }; - bitv.fix_last_block(); bitv } @@ -295,15 +298,33 @@ pub fn with_capacity(nbits: uint) -> Bitv { /// false, false, true, false])); /// ``` pub fn from_bytes(bytes: &[u8]) -> Bitv { - Bitv::from_fn(bytes.len() * 8, |i| { - let b = bytes[i / 8] as u32; - let offset = i % 8; - b >> (7 - offset) & 1 == 1 - }) + let len = bytes.len().checked_mul(u8::BITS).expect("capacity overflow"); + let mut bitv = Bitv::with_capacity(len); + let complete_words = bytes.len() / 4; + let extra_bytes = bytes.len() % 4; + + for i in range(0, complete_words) { + bitv.storage.push( + (bytes[i * 4 + 0] as u32 << 0) | + (bytes[i * 4 + 1] as u32 << 8) | + (bytes[i * 4 + 2] as u32 << 16) | + (bytes[i * 4 + 3] as u32 << 24) + ); + } + + if extra_bytes > 0 { + let mut last_word = 0u32; + for (i, &byte) in bytes[complete_words*4..].iter().enumerate() { + last_word |= byte as u32 << (i * 8); + } + bitv.storage.push(last_word); + } + + bitv } - /// Creates a `Bitv` of the specified length where the value at each - /// index is `f(index)`. + /// Creates a `Bitv` of the specified length where the value at each index + /// is `f(index)`. /// /// # Examples /// @@ -339,7 +360,9 @@ pub fn from_fn(len: uint, mut f: F) -> Bitv where F: FnMut(uint) -> bool { #[inline] #[unstable = "panic semantics are likely to change in the future"] pub fn get(&self, i: uint) -> Option { - assert!(i < self.nbits); + if i >= self.nbits { + return None; + } let w = i / u32::BITS; let b = i % u32::BITS; self.storage.get(w).map(|&block| @@ -548,7 +571,7 @@ pub fn all(&self) -> bool { #[inline] #[unstable = "matches collection reform specification, waiting for dust to settle"] pub fn iter<'a>(&'a self) -> Bits<'a> { - Bits {bitv: self, next_idx: 0, end_idx: self.nbits} + Bits { bitv: self, next_idx: 0, end_idx: self.nbits } } /// Returns `true` if all bits are 0. @@ -608,7 +631,7 @@ pub fn any(&self) -> bool { /// assert_eq!(bv.to_bytes(), vec!(0b00100000, 0b10000000)); /// ``` pub fn to_bytes(&self) -> Vec { - fn bit (bitv: &Bitv, byte: uint, bit: uint) -> u8 { + fn bit(bitv: &Bitv, byte: uint, bit: uint) -> u8 { let offset = byte * 8 + bit; if offset >= bitv.nbits { 0 @@ -634,7 +657,7 @@ fn bit (bitv: &Bitv, byte: uint, bit: uint) -> u8 { /// Deprecated: Use `iter().collect()`. #[deprecated = "Use `iter().collect()`"] pub fn to_bools(&self) -> Vec { - Vec::from_fn(self.nbits, |i| self[i]) + self.iter().collect() } /// Compares a `Bitv` to a slice of `bool`s. @@ -656,12 +679,7 @@ pub fn to_bools(&self) -> Vec { /// ``` pub fn eq_vec(&self, v: &[bool]) -> bool { assert_eq!(self.nbits, v.len()); - let mut i = 0; - while i < self.nbits { - if self[i] != v[i] { return false; } - i = i + 1; - } - true + iter::order::eq(self.iter(), v.iter().cloned()) } /// Shortens a `Bitv`, dropping excess elements. @@ -682,6 +700,7 @@ pub fn eq_vec(&self, v: &[bool]) -> bool { pub fn truncate(&mut self, len: uint) { if len < self.len() { self.nbits = len; + // This fixes (2). self.storage.truncate(blocks_for_bits(len)); self.fix_last_block(); } @@ -707,13 +726,9 @@ pub fn truncate(&mut self, len: uint) { #[unstable = "matches collection reform specification, waiting for dust to settle"] pub fn reserve(&mut self, additional: uint) { let desired_cap = self.len().checked_add(additional).expect("capacity overflow"); - match self.storage.len().checked_mul(u32::BITS) { - None => {} // Vec has more initialized capacity than we can ever use - Some(initialized_cap) => { - if desired_cap > initialized_cap { - self.storage.reserve(blocks_for_bits(desired_cap - initialized_cap)); - } - } + let storage_len = self.storage.len(); + if desired_cap > self.capacity() { + self.storage.reserve(blocks_for_bits(desired_cap) - storage_len); } } @@ -741,13 +756,9 @@ pub fn reserve(&mut self, additional: uint) { #[unstable = "matches collection reform specification, waiting for dust to settle"] pub fn reserve_exact(&mut self, additional: uint) { let desired_cap = self.len().checked_add(additional).expect("capacity overflow"); - match self.storage.len().checked_mul(u32::BITS) { - None => {} // Vec has more initialized capacity than we can ever use - Some(initialized_cap) => { - if desired_cap > initialized_cap { - self.storage.reserve_exact(blocks_for_bits(desired_cap - initialized_cap)); - } - } + let storage_len = self.storage.len(); + if desired_cap > self.capacity() { + self.storage.reserve_exact(blocks_for_bits(desired_cap) - storage_len); } } @@ -801,8 +812,7 @@ pub fn grow(&mut self, n: uint, value: bool) { if value { self.storage[old_last_word] |= !mask; } else { - // Extra bits are already supposed to be zero by invariant, but play it safe... - self.storage[old_last_word] &= mask; + // Extra bits are already zero by invariant. } } @@ -843,9 +853,13 @@ pub fn pop(&mut self) -> Option { } else { let i = self.nbits - 1; let ret = self[i]; - // Second rule of Bitv Club + // (3) self.set(i, false); self.nbits = i; + if self.nbits % u32::BITS == 0 { + // (2) + self.storage.pop(); + } Some(ret) } } @@ -864,11 +878,11 @@ pub fn pop(&mut self) -> Option { /// ``` #[unstable = "matches collection reform specification, waiting for dust to settle"] pub fn push(&mut self, elem: bool) { - let insert_pos = self.nbits; - self.nbits = self.nbits.checked_add(1).expect("Capacity overflow"); - if self.storage.len().checked_mul(u32::BITS).unwrap_or(uint::MAX) < self.nbits { + if self.nbits % u32::BITS == 0 { self.storage.push(0); } + let insert_pos = self.nbits; + self.nbits = self.nbits.checked_add(1).expect("Capacity overflow"); self.set(insert_pos, elem); } @@ -958,7 +972,7 @@ fn cmp(&self, other: &Bitv) -> Ordering { impl fmt::Show for Bitv { fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { for bit in self.iter() { - try!(write!(fmt, "{}", if bit { 1u } else { 0u })); + try!(write!(fmt, "{}", if bit { 1u32 } else { 0u32 })); } Ok(()) } From 24329d72935c2753454e2187e632579c2405b6fc Mon Sep 17 00:00:00 2001 From: Tobias Bucher Date: Mon, 15 Dec 2014 15:57:34 +0100 Subject: [PATCH 05/44] Change `Extend` and `FromIterator` functionality of `BitvSet` Also fix up some tests from last commit. --- src/libcollections/bit.rs | 46 ++++++++++++++++++++++++--------------- 1 file changed, 29 insertions(+), 17 deletions(-) diff --git a/src/libcollections/bit.rs b/src/libcollections/bit.rs index 3f33d85ba569..dd3369ea1da7 100644 --- a/src/libcollections/bit.rs +++ b/src/libcollections/bit.rs @@ -98,6 +98,14 @@ type MutBlocks<'a> = MutItems<'a, u32>; type MatchWords<'a> = Chain>, Skip>>>>; +fn reverse_bits(byte: u8) -> u8 { + let mut result = 0; + for i in range(0, u8::BITS) { + result |= ((byte >> i) & 1) << (u8::BITS - 1 - i); + } + result +} + // Take two BitV's, and return iterators of their words, where the shorter one // has been padded with 0's fn match_words <'a,'b>(a: &'a Bitv, b: &'b Bitv) -> (MatchWords<'a>, MatchWords<'b>) { @@ -303,19 +311,21 @@ pub fn from_bytes(bytes: &[u8]) -> Bitv { let complete_words = bytes.len() / 4; let extra_bytes = bytes.len() % 4; + bitv.nbits = len; + for i in range(0, complete_words) { bitv.storage.push( - (bytes[i * 4 + 0] as u32 << 0) | - (bytes[i * 4 + 1] as u32 << 8) | - (bytes[i * 4 + 2] as u32 << 16) | - (bytes[i * 4 + 3] as u32 << 24) + (reverse_bits(bytes[i * 4 + 0]) as u32 << 0) | + (reverse_bits(bytes[i * 4 + 1]) as u32 << 8) | + (reverse_bits(bytes[i * 4 + 2]) as u32 << 16) | + (reverse_bits(bytes[i * 4 + 3]) as u32 << 24) ); } if extra_bytes > 0 { let mut last_word = 0u32; for (i, &byte) in bytes[complete_words*4..].iter().enumerate() { - last_word |= byte as u32 << (i * 8); + last_word |= reverse_bits(byte) as u32 << (i * 8); } bitv.storage.push(last_word); } @@ -1102,18 +1112,20 @@ impl Default for BitvSet { fn default() -> BitvSet { BitvSet::new() } } -impl FromIterator for BitvSet { - fn from_iter>(iterator: I) -> BitvSet { +impl FromIterator for BitvSet { + fn from_iter>(iterator: I) -> BitvSet { let mut ret = BitvSet::new(); ret.extend(iterator); ret } } -impl Extend for BitvSet { +impl Extend for BitvSet { #[inline] - fn extend>(&mut self, iterator: I) { - self.bitv.extend(iterator); + fn extend>(&mut self, mut iterator: I) { + for i in iterator { + self.insert(i); + } } } @@ -2592,9 +2604,9 @@ fn test_bitv_set_show() { } #[test] - fn test_bitv_set_from_bools() { - let bools = vec![true, false, true, true]; - let a: BitvSet = bools.iter().map(|n| *n).collect(); + fn test_bitv_set_from_uints() { + let uints = vec![0, 2, 2, 3]; + let a: BitvSet = uints.into_iter().collect(); let mut b = BitvSet::new(); b.insert(0); b.insert(2); @@ -2604,13 +2616,13 @@ fn test_bitv_set_from_bools() { #[test] fn test_bitv_set_iterator() { - let bools = [true, false, true, true]; - let bitv: BitvSet = bools.iter().map(|n| *n).collect(); + let uints = vec![0, 2, 2, 3]; + let bitv: BitvSet = uints.into_iter().collect(); let idxs: Vec = bitv.iter().collect(); - assert_eq!(idxs, vec!(0, 2, 3)); + assert_eq!(idxs, vec![0, 2, 3]); - let long: BitvSet = range(0u, 10000).map(|n| n % 2 == 0).collect(); + let long: BitvSet = range(0u, 10000).filter(|&n| n % 2 == 0).collect(); let real = range_step(0, 10000, 2).collect::>(); let idxs: Vec = long.iter().collect(); From c9010bff6c5d8ccc8d3009d377f1cb5826b25d69 Mon Sep 17 00:00:00 2001 From: Barosl Lee Date: Sun, 21 Dec 2014 05:28:49 +0900 Subject: [PATCH 06/44] Fix error message on invalid field names for a struct variant Fixes #19922. --- src/librustc_typeck/check/mod.rs | 34 ++++++++++++++++++++-------- src/test/compile-fail/issue-19922.rs | 18 +++++++++++++++ 2 files changed, 42 insertions(+), 10 deletions(-) create mode 100644 src/test/compile-fail/issue-19922.rs diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index bbc33826f355..01819aff2068 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -3419,7 +3419,8 @@ fn check_struct_or_variant_fields<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>, substitutions: subst::Substs<'tcx>, field_types: &[ty::field_ty], ast_fields: &[ast::Field], - check_completeness: bool) { + check_completeness: bool, + enum_id_opt: Option) { let tcx = fcx.ccx.tcx; let mut class_field_map = FnvHashMap::new(); @@ -3438,13 +3439,24 @@ fn check_struct_or_variant_fields<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>, match pair { None => { fcx.type_error_message( - field.ident.span, - |actual| { - format!("structure `{}` has no field named `{}`", - actual, token::get_ident(field.ident.node)) - }, - struct_ty, - None); + field.ident.span, + |actual| match enum_id_opt { + Some(enum_id) => { + let variant_type = ty::enum_variant_with_id(tcx, + enum_id, + class_id); + format!("struct variant `{}::{}` has no field named `{}`", + actual, variant_type.name.as_str(), + token::get_ident(field.ident.node)) + } + None => { + format!("structure `{}` has no field named `{}`", + actual, + token::get_ident(field.ident.node)) + } + }, + struct_ty, + None); error_happened = true; } Some((_, true)) => { @@ -3525,7 +3537,8 @@ fn check_struct_constructor(fcx: &FnCtxt, struct_substs, class_fields.as_slice(), fields, - base_expr.is_none()); + base_expr.is_none(), + None); if ty::type_is_error(fcx.node_ty(id)) { struct_type = ty::mk_err(); } @@ -3567,7 +3580,8 @@ fn check_struct_enum_variant(fcx: &FnCtxt, substitutions, variant_fields.as_slice(), fields, - true); + true, + Some(enum_id)); fcx.write_ty(id, enum_type); } diff --git a/src/test/compile-fail/issue-19922.rs b/src/test/compile-fail/issue-19922.rs new file mode 100644 index 000000000000..e3ced3028098 --- /dev/null +++ b/src/test/compile-fail/issue-19922.rs @@ -0,0 +1,18 @@ +// Copyright 2014 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +enum Homura { + Akemi { madoka: () } +} + +fn main() { + let homura = Homura::Akemi { kaname: () }; + //~^ ERROR struct variant `Homura::Akemi` has no field named `kaname` +} From 7d55249c91eb556f13f7e5b7043b06747c5782af Mon Sep 17 00:00:00 2001 From: Tshepang Lekhonkhobe Date: Sun, 21 Dec 2014 09:32:32 +0200 Subject: [PATCH 07/44] doc: add missing "by default" That sentence made it look like there was no option for using 'mut' --- src/doc/guide.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/doc/guide.md b/src/doc/guide.md index d739ad105fc8..d34ec45fb871 100644 --- a/src/doc/guide.md +++ b/src/doc/guide.md @@ -1011,8 +1011,8 @@ in the original declaration. Finally, because fields have names, we can access the field through dot notation: `origin.x`. -The values in structs are immutable, like other bindings in Rust. However, you -can use `mut` to make them mutable: +The values in structs are immutable by default, like other bindings in Rust. +Use `mut` to make them mutable: ```{rust} struct Point { From ee9de3b10290290ae1a56d6fbc80307869fc5d01 Mon Sep 17 00:00:00 2001 From: Florian Gilcher Date: Wed, 10 Dec 2014 12:17:14 +0100 Subject: [PATCH 08/44] Fully remove `notrust` markers from rustdoc Internally refactor all mentions of `notrust` to the positive statement `rust`. --- src/librustdoc/html/markdown.rs | 40 ++++++++++++++++----------------- 1 file changed, 19 insertions(+), 21 deletions(-) diff --git a/src/librustdoc/html/markdown.rs b/src/librustdoc/html/markdown.rs index 8b2f644dfe33..5581874ea3af 100644 --- a/src/librustdoc/html/markdown.rs +++ b/src/librustdoc/html/markdown.rs @@ -174,7 +174,7 @@ pub fn render(w: &mut fmt::Formatter, s: &str, print_toc: bool) -> fmt::Result { let rlang = slice::from_raw_buf(&(*lang).data, (*lang).size as uint); let rlang = str::from_utf8(rlang).unwrap(); - if LangString::parse(rlang).notrust { + if !LangString::parse(rlang).rust { (my_opaque.dfltblk)(ob, orig_text, lang, opaque as *mut libc::c_void); true @@ -320,7 +320,7 @@ pub fn find_testable_code(doc: &str, tests: &mut ::test::Collector) { let s = str::from_utf8(lang).unwrap(); LangString::parse(s) }; - if block_info.notrust { return } + if !block_info.rust { return } let text = slice::from_raw_buf(&(*text).data, (*text).size as uint); let opaque = opaque as *mut hoedown_html_renderer_state; let tests = &mut *((*opaque).opaque as *mut ::test::Collector); @@ -373,7 +373,7 @@ struct LangString { should_fail: bool, no_run: bool, ignore: bool, - notrust: bool, + rust: bool, test_harness: bool, } @@ -383,7 +383,7 @@ fn all_false() -> LangString { should_fail: false, no_run: false, ignore: false, - notrust: false, + rust: false, test_harness: false, } } @@ -403,14 +403,13 @@ fn parse(string: &str) -> LangString { "should_fail" => { data.should_fail = true; seen_rust_tags = true; }, "no_run" => { data.no_run = true; seen_rust_tags = true; }, "ignore" => { data.ignore = true; seen_rust_tags = true; }, - "notrust" => { data.notrust = true; seen_rust_tags = true; }, - "rust" => { data.notrust = false; seen_rust_tags = true; }, + "rust" => { data.rust = true; seen_rust_tags = true; }, "test_harness" => { data.test_harness = true; seen_rust_tags = true; } _ => { seen_other_tags = true } } } - data.notrust |= seen_other_tags && !seen_rust_tags; + data.rust |= !seen_other_tags || seen_rust_tags; data } @@ -452,28 +451,27 @@ mod tests { #[test] fn test_lang_string_parse() { fn t(s: &str, - should_fail: bool, no_run: bool, ignore: bool, notrust: bool, test_harness: bool) { + should_fail: bool, no_run: bool, ignore: bool, rust: bool, test_harness: bool) { assert_eq!(LangString::parse(s), LangString { should_fail: should_fail, no_run: no_run, ignore: ignore, - notrust: notrust, + rust: rust, test_harness: test_harness, }) } - t("", false,false,false,false,false); - t("rust", false,false,false,false,false); - t("sh", false,false,false,true,false); - t("notrust", false,false,false,true,false); - t("ignore", false,false,true,false,false); - t("should_fail", true,false,false,false,false); - t("no_run", false,true,false,false,false); - t("test_harness", false,false,false,false,true); - t("{.no_run .example}", false,true,false,false,false); - t("{.sh .should_fail}", true,false,false,false,false); - t("{.example .rust}", false,false,false,false,false); - t("{.test_harness .rust}", false,false,false,false,true); + t("", false,false,false,true,false); + t("rust", false,false,false,true,false); + t("sh", false,false,false,false,false); + t("ignore", false,false,true,true,false); + t("should_fail", true,false,false,true,false); + t("no_run", false,true,false,true,false); + t("test_harness", false,false,false,true,true); + t("{.no_run .example}", false,true,false,true,false); + t("{.sh .should_fail}", true,false,false,true,false); + t("{.example .rust}", false,false,false,true,false); + t("{.test_harness .rust}", false,false,false,true,true); } #[test] From 4908017d59da8694b9ceaf743baf1163c1e19086 Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Wed, 10 Dec 2014 09:02:31 -0800 Subject: [PATCH 09/44] std: Stabilize the std::str module This commit starts out by consolidating all `str` extension traits into one `StrExt` trait to be included in the prelude. This means that `UnicodeStrPrelude`, `StrPrelude`, and `StrAllocating` have all been merged into one `StrExt` exported by the standard library. Some functionality is currently duplicated with the `StrExt` present in libcore. This commit also currently avoids any methods which require any form of pattern to operate. These functions will be stabilized via a separate RFC. Next, stability of methods and structures are as follows: Stable * from_utf8_unchecked * CowString - after moving to std::string * StrExt::as_bytes * StrExt::as_ptr * StrExt::bytes/Bytes - also made a struct instead of a typedef * StrExt::char_indices/CharIndices - CharOffsets was renamed * StrExt::chars/Chars * StrExt::is_empty * StrExt::len * StrExt::lines/Lines * StrExt::lines_any/LinesAny * StrExt::slice_unchecked * StrExt::trim * StrExt::trim_left * StrExt::trim_right * StrExt::words/Words - also made a struct instead of a typedef Unstable * from_utf8 - the error type was changed to a `Result`, but the error type has yet to prove itself * from_c_str - this function will be handled by the c_str RFC * FromStr - this trait will have an associated error type eventually * StrExt::escape_default - needs iterators at least, unsure if it should make the cut * StrExt::escape_unicode - needs iterators at least, unsure if it should make the cut * StrExt::slice_chars - this function has yet to prove itself * StrExt::slice_shift_char - awaiting conventions about slicing and shifting * StrExt::graphemes/Graphemes - this functionality may only be in libunicode * StrExt::grapheme_indices/GraphemeIndices - this functionality may only be in libunicode * StrExt::width - this functionality may only be in libunicode * StrExt::utf16_units - this functionality may only be in libunicode * StrExt::nfd_chars - this functionality may only be in libunicode * StrExt::nfkd_chars - this functionality may only be in libunicode * StrExt::nfc_chars - this functionality may only be in libunicode * StrExt::nfkc_chars - this functionality may only be in libunicode * StrExt::is_char_boundary - naming is uncertain with container conventions * StrExt::char_range_at - naming is uncertain with container conventions * StrExt::char_range_at_reverse - naming is uncertain with container conventions * StrExt::char_at - naming is uncertain with container conventions * StrExt::char_at_reverse - naming is uncertain with container conventions * StrVector::concat - this functionality may be replaced with iterators, but it's not certain at this time * StrVector::connect - as with concat, may be deprecated in favor of iterators Deprecated * StrAllocating and UnicodeStrPrelude have been merged into StrExit * eq_slice - compiler implementation detail * from_str - use the inherent parse() method * is_utf8 - call from_utf8 instead * replace - call the method instead * truncate_utf16_at_nul - this is an implementation detail of windows and does not need to be exposed. * utf8_char_width - moved to libunicode * utf16_items - moved to libunicode * is_utf16 - moved to libunicode * Utf16Items - moved to libunicode * Utf16Item - moved to libunicode * Utf16Encoder - moved to libunicode * AnyLines - renamed to LinesAny and made a struct * SendStr - use CowString<'static> instead * str::raw - all functionality is deprecated * StrExt::into_string - call to_string() instead * StrExt::repeat - use iterators instead * StrExt::char_len - use .chars().count() instead * StrExt::is_alphanumeric - use .chars().all(..) * StrExt::is_whitespace - use .chars().all(..) Pending deprecation -- while slicing syntax is being worked out, these methods are all #[unstable] * Str - while currently used for generic programming, this trait will be replaced with one of [], deref coercions, or a generic conversion trait. * StrExt::slice - use slicing syntax instead * StrExt::slice_to - use slicing syntax instead * StrExt::slice_from - use slicing syntax instead * StrExt::lev_distance - deprecated with no replacement Awaiting stabilization due to patterns and/or matching * StrExt::contains * StrExt::contains_char * StrExt::split * StrExt::splitn * StrExt::split_terminator * StrExt::rsplitn * StrExt::match_indices * StrExt::split_str * StrExt::starts_with * StrExt::ends_with * StrExt::trim_chars * StrExt::trim_left_chars * StrExt::trim_right_chars * StrExt::find * StrExt::rfind * StrExt::find_str * StrExt::subslice_offset --- src/libcollections/str.rs | 1122 +++++++++++++++++++++++++---- src/libcollections/string.rs | 74 +- src/libcore/fmt/float.rs | 2 +- src/libcore/fmt/mod.rs | 15 +- src/libcore/num/mod.rs | 2 +- src/libcore/prelude.rs | 2 +- src/libcore/str.rs | 1010 ++++---------------------- src/librustc/lib.rs | 1 + src/librustc/util/lev_distance.rs | 63 ++ src/librustc_resolve/lib.rs | 7 +- src/libstd/error.rs | 18 +- src/libstd/os.rs | 2 +- src/libstd/sys/windows/os.rs | 39 +- src/libunicode/lib.rs | 10 +- src/libunicode/u_str.rs | 222 +++++- 15 files changed, 1511 insertions(+), 1078 deletions(-) create mode 100644 src/librustc/util/lev_distance.rs diff --git a/src/libcollections/str.rs b/src/libcollections/str.rs index bb03575b3ac2..8c9346639b34 100644 --- a/src/libcollections/str.rs +++ b/src/libcollections/str.rs @@ -55,34 +55,31 @@ use self::RecompositionState::*; use self::DecompositionType::*; +use core::prelude::*; + use core::borrow::{BorrowFrom, Cow, ToOwned}; -use core::clone::Clone; +use core::cmp::{mod, Equiv, PartialEq, Eq, PartialOrd, Ord, Ordering}; use core::default::Default; use core::fmt; use core::hash; -use core::char::Char; -use core::cmp::{mod, Eq, Equiv, Ord, Ordering, PartialEq, PartialOrd}; -use core::iter::{range, AdditiveIterator, Iterator, IteratorExt}; -use core::kinds::Sized; -use core::option::Option::{mod, Some, None}; -use core::slice::{AsSlice, SliceExt}; +use core::iter::AdditiveIterator; +use core::iter::{mod, range, Iterator, IteratorExt}; +use core::str as core_str; +use unicode::str::{UnicodeStr, Utf16Encoder}; use ring_buf::RingBuf; -use string::String; +use string::{String, ToString}; use unicode; use vec::Vec; -pub use core::str::{from_utf8, CharEq, Chars, CharOffsets}; +pub use core::str::{from_utf8, CharEq, Chars, CharIndices}; pub use core::str::{Bytes, CharSplits}; -pub use core::str::{CharSplitsN, AnyLines, MatchIndices, StrSplits}; -pub use core::str::{Utf16Encoder, Utf16CodeUnits}; -pub use core::str::{eq_slice, is_utf8, is_utf16, Utf16Items}; -pub use core::str::{Utf16Item, ScalarValue, LoneSurrogate, utf16_items}; -pub use core::str::{truncate_utf16_at_nul, utf8_char_width, CharRange}; -pub use core::str::{FromStr, from_str}; -pub use core::str::{Str, StrPrelude}; +pub use core::str::{CharSplitsN, Lines, LinesAny, MatchIndices, StrSplits}; +pub use core::str::{CharRange}; +pub use core::str::{FromStr, from_str, Utf8Error}; +pub use core::str::Str; pub use core::str::{from_utf8_unchecked, from_c_str}; -pub use unicode::str::{UnicodeStrPrelude, Words, Graphemes, GraphemeIndices}; +pub use unicode::str::{Words, Graphemes, GraphemeIndices}; // FIXME(conventions): ensure bit/char conventions are followed by str's API @@ -91,6 +88,7 @@ */ /// Methods for vectors of strings. +#[unstable = "functionality may be replaced with iterators"] pub trait StrVector for Sized? { /// Concatenates a vector of strings. /// @@ -117,6 +115,7 @@ pub trait StrVector for Sized? { fn connect(&self, sep: &str) -> String; } +#[allow(deprecated)] impl StrVector for [S] { fn concat(&self) -> String { if self.is_empty() { @@ -129,7 +128,7 @@ fn concat(&self) -> String { let mut result = String::with_capacity(len); for s in self.iter() { - result.push_str(s.as_slice()) + result.push_str(s.as_slice()); } result @@ -379,6 +378,21 @@ fn next(&mut self) -> Option { } } +/// External iterator for a string's UTF16 codeunits. +/// Use with the `std::iter` module. +#[deriving(Clone)] +pub struct Utf16Units<'a> { + encoder: Utf16Encoder> +} + +impl<'a> Iterator for Utf16Units<'a> { + #[inline] + fn next(&mut self) -> Option { self.encoder.next() } + + #[inline] + fn size_hint(&self) -> (uint, Option) { self.encoder.size_hint() } +} + /// Replaces all occurrences of one string with another. /// /// # Arguments @@ -399,16 +413,9 @@ fn next(&mut self) -> Option { /// let new_string = str::replace(string, "or", "str"); /// assert_eq!(new_string.as_slice(), "strange"); /// ``` +#[deprecated = "call the inherent method instead"] pub fn replace(s: &str, from: &str, to: &str) -> String { - let mut result = String::new(); - let mut last_end = 0; - for (start, end) in s.match_indices(from) { - result.push_str(unsafe { s.slice_unchecked(last_end, start) }); - result.push_str(to); - last_end = end; - } - result.push_str(unsafe { s.slice_unchecked(last_end, s.len()) }); - result + s.replace(from, to) } /* @@ -434,7 +441,7 @@ macro_rules! utf8_acc_cont_byte { /// A string type that can hold either a `String` or a `&str`. /// This can be useful as an optimization when an allocation is sometimes /// needed but not always. -#[deprecated = "use std::str::CowString"] +#[deprecated = "use stding::string::CowString"] pub enum MaybeOwned<'a> { /// A borrowed string. Slice(&'a str), @@ -443,9 +450,10 @@ pub enum MaybeOwned<'a> { } /// A specialization of `CowString` to be sendable. +#[deprecated = "use std::string::CowString<'static>"] pub type SendStr = CowString<'static>; -#[deprecated = "use std::str::CowString"] +#[deprecated = "use std::string::CowString"] impl<'a> MaybeOwned<'a> { /// Returns `true` if this `MaybeOwned` wraps an owned string. /// @@ -483,6 +491,7 @@ pub fn is_slice(&self) -> bool { /// Return the number of bytes in this string. #[inline] + #[allow(deprecated)] pub fn len(&self) -> uint { self.as_slice().len() } /// Returns true if the string contains no bytes @@ -545,7 +554,8 @@ impl<'a> IntoMaybeOwned<'a> for MaybeOwned<'a> { fn into_maybe_owned(self) -> MaybeOwned<'a> { self } } -#[deprecated = "use std::str::CowString"] +#[deprecated = "use std::string::CowString"] +#[allow(deprecated)] impl<'a> PartialEq for MaybeOwned<'a> { #[inline] fn eq(&self, other: &MaybeOwned) -> bool { @@ -553,10 +563,10 @@ fn eq(&self, other: &MaybeOwned) -> bool { } } -#[deprecated = "use std::str::CowString"] +#[deprecated = "use std::string::CowString"] impl<'a> Eq for MaybeOwned<'a> {} -#[deprecated = "use std::str::CowString"] +#[deprecated = "use std::string::CowString"] impl<'a> PartialOrd for MaybeOwned<'a> { #[inline] fn partial_cmp(&self, other: &MaybeOwned) -> Option { @@ -564,16 +574,17 @@ fn partial_cmp(&self, other: &MaybeOwned) -> Option { } } -#[deprecated = "use std::str::CowString"] +#[deprecated = "use std::string::CowString"] impl<'a> Ord for MaybeOwned<'a> { #[inline] + #[allow(deprecated)] fn cmp(&self, other: &MaybeOwned) -> Ordering { self.as_slice().cmp(other.as_slice()) } } #[allow(deprecated)] -#[deprecated = "use std::str::CowString"] +#[deprecated = "use std::string::CowString"] impl<'a, S: Str> Equiv for MaybeOwned<'a> { #[inline] fn equiv(&self, other: &S) -> bool { @@ -581,9 +592,9 @@ fn equiv(&self, other: &S) -> bool { } } -#[deprecated = "use std::str::CowString"] +#[deprecated = "use std::string::CowString"] +#[allow(deprecated)] impl<'a> Str for MaybeOwned<'a> { - #[allow(deprecated)] #[inline] fn as_slice<'b>(&'b self) -> &'b str { match *self { @@ -593,19 +604,7 @@ fn as_slice<'b>(&'b self) -> &'b str { } } -#[deprecated = "use std::str::CowString"] -impl<'a> StrAllocating for MaybeOwned<'a> { - #[allow(deprecated)] - #[inline] - fn into_string(self) -> String { - match self { - Slice(s) => String::from_str(s), - Owned(s) => s - } - } -} - -#[deprecated = "use std::str::CowString"] +#[deprecated = "use std::string::CowString"] impl<'a> Clone for MaybeOwned<'a> { #[allow(deprecated)] #[inline] @@ -617,14 +616,15 @@ fn clone(&self) -> MaybeOwned<'a> { } } -#[deprecated = "use std::str::CowString"] +#[deprecated = "use std::string::CowString"] impl<'a> Default for MaybeOwned<'a> { #[allow(deprecated)] #[inline] fn default() -> MaybeOwned<'a> { Slice("") } } -#[deprecated = "use std::str::CowString"] +#[deprecated = "use std::string::CowString"] +#[allow(deprecated)] impl<'a, H: hash::Writer> hash::Hash for MaybeOwned<'a> { #[inline] fn hash(&self, hasher: &mut H) { @@ -632,7 +632,7 @@ fn hash(&self, hasher: &mut H) { } } -#[deprecated = "use std::str::CowString"] +#[deprecated = "use std::string::CowString"] impl<'a> fmt::Show for MaybeOwned<'a> { #[inline] fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { @@ -650,10 +650,11 @@ fn borrow_from(owned: &String) -> &str { owned[] } #[unstable = "trait is unstable"] impl ToOwned for str { - fn to_owned(&self) -> String { self.into_string() } + fn to_owned(&self) -> String { self.to_string() } } /// Unsafe string operations. +#[deprecated] pub mod raw { pub use core::str::raw::{from_utf8, c_str_to_static_slice, slice_bytes}; pub use core::str::raw::{slice_unchecked}; @@ -664,46 +665,25 @@ pub mod raw { */ /// A clone-on-write string +#[deprecated = "use std::string::CowString instead"] pub type CowString<'a> = Cow<'a, String, str>; -impl<'a> Str for CowString<'a> { - #[inline] - fn as_slice<'b>(&'b self) -> &'b str { - (**self).as_slice() - } -} - /* Section: Trait implementations */ /// Any string that can be represented as a slice. -pub trait StrAllocating: Str { - /// Converts `self` into a `String`, not making a copy if possible. - fn into_string(self) -> String; - +pub trait StrExt for Sized?: Slice { /// Escapes each char in `s` with `char::escape_default`. + #[unstable = "return type may change to be an iterator"] fn escape_default(&self) -> String { - let me = self.as_slice(); - let mut out = String::with_capacity(me.len()); - for c in me.chars() { - for c in c.escape_default() { - out.push(c); - } - } - out + self.chars().flat_map(|c| c.escape_default()).collect() } /// Escapes each char in `s` with `char::escape_unicode`. + #[unstable = "return type may change to be an iterator"] fn escape_unicode(&self) -> String { - let me = self.as_slice(); - let mut out = String::with_capacity(me.len()); - for c in me.chars() { - for c in c.escape_unicode() { - out.push(c); - } - } - out + self.chars().flat_map(|c| c.escape_unicode()).collect() } /// Replaces all occurrences of one string with another. @@ -730,25 +710,31 @@ fn escape_unicode(&self) -> String { /// // not found, so no change. /// assert_eq!(s.replace("cookie monster", "little lamb"), s); /// ``` + #[unstable = "awaiting pattern/matcher stabilization"] fn replace(&self, from: &str, to: &str) -> String { - replace(self.as_slice(), from, to) + let mut result = String::new(); + let mut last_end = 0; + for (start, end) in self.match_indices(from) { + result.push_str(unsafe { self.slice_unchecked(last_end, start) }); + result.push_str(to); + last_end = end; + } + result.push_str(unsafe { self.slice_unchecked(last_end, self.len()) }); + result } /// Given a string, makes a new string with repeated copies of it. + #[deprecated = "user repeat(self).take(n).collect() instead"] fn repeat(&self, nn: uint) -> String { - let me = self.as_slice(); - let mut ret = String::with_capacity(nn * me.len()); - for _ in range(0, nn) { - ret.push_str(me); - } - ret + iter::repeat(self[]).take(nn).collect() } /// Returns the Levenshtein Distance between two strings. + #[deprecated = "this function will be removed"] fn lev_distance(&self, t: &str) -> uint { - let me = self.as_slice(); - if me.is_empty() { return t.char_len(); } - if t.is_empty() { return me.char_len(); } + let me = self[]; + if me.is_empty() { return t.chars().count(); } + if t.is_empty() { return me.chars().count(); } let mut dcol = Vec::from_fn(t.len() + 1, |x| x); let mut t_last = 0; @@ -780,9 +766,10 @@ fn lev_distance(&self, t: &str) -> uint { /// Returns an iterator over the string in Unicode Normalization Form D /// (canonical decomposition). #[inline] + #[unstable = "this functionality may only be provided by libunicode"] fn nfd_chars<'a>(&'a self) -> Decompositions<'a> { Decompositions { - iter: self.as_slice().chars(), + iter: self[].chars(), buffer: Vec::new(), sorted: false, kind: Canonical @@ -792,9 +779,10 @@ fn nfd_chars<'a>(&'a self) -> Decompositions<'a> { /// Returns an iterator over the string in Unicode Normalization Form KD /// (compatibility decomposition). #[inline] + #[unstable = "this functionality may only be provided by libunicode"] fn nfkd_chars<'a>(&'a self) -> Decompositions<'a> { Decompositions { - iter: self.as_slice().chars(), + iter: self[].chars(), buffer: Vec::new(), sorted: false, kind: Compatible @@ -804,6 +792,7 @@ fn nfkd_chars<'a>(&'a self) -> Decompositions<'a> { /// An Iterator over the string in Unicode Normalization Form C /// (canonical decomposition followed by canonical composition). #[inline] + #[unstable = "this functionality may only be provided by libunicode"] fn nfc_chars<'a>(&'a self) -> Recompositions<'a> { Recompositions { iter: self.nfd_chars(), @@ -817,6 +806,7 @@ fn nfc_chars<'a>(&'a self) -> Recompositions<'a> { /// An Iterator over the string in Unicode Normalization Form KC /// (compatibility decomposition followed by canonical composition). #[inline] + #[unstable = "this functionality may only be provided by libunicode"] fn nfkc_chars<'a>(&'a self) -> Recompositions<'a> { Recompositions { iter: self.nfkd_chars(), @@ -826,15 +816,912 @@ fn nfkc_chars<'a>(&'a self) -> Recompositions<'a> { last_ccc: None } } -} -impl<'a> StrAllocating for &'a str { + /// Returns true if one string contains another + /// + /// # Arguments + /// + /// - needle - The string to look for + /// + /// # Example + /// + /// ```rust + /// assert!("bananas".contains("nana")); + /// ``` + #[unstable = "awaiting pattern/matcher stabilization"] + fn contains(&self, needle: &str) -> bool { + core_str::StrExt::contains(self[], needle) + } + + /// Returns true if a string contains a char. + /// + /// # Arguments + /// + /// - needle - The char to look for + /// + /// # Example + /// + /// ```rust + /// assert!("hello".contains_char('e')); + /// ``` + #[unstable = "awaiting pattern/matcher stabilization"] + fn contains_char(&self, needle: char) -> bool { + core_str::StrExt::contains_char(self[], needle) + } + + /// An iterator over the characters of `self`. Note, this iterates + /// over Unicode code-points, not Unicode graphemes. + /// + /// # Example + /// + /// ```rust + /// let v: Vec = "abc åäö".chars().collect(); + /// assert_eq!(v, vec!['a', 'b', 'c', ' ', 'å', 'ä', 'ö']); + /// ``` + #[stable] + fn chars(&self) -> Chars { + core_str::StrExt::chars(self[]) + } + + /// An iterator over the bytes of `self` + /// + /// # Example + /// + /// ```rust + /// let v: Vec = "bors".bytes().collect(); + /// assert_eq!(v, b"bors".to_vec()); + /// ``` + #[stable] + fn bytes(&self) -> Bytes { + core_str::StrExt::bytes(self[]) + } + + /// An iterator over the characters of `self` and their byte offsets. + #[stable] + fn char_indices(&self) -> CharIndices { + core_str::StrExt::char_indices(self[]) + } + + /// An iterator over substrings of `self`, separated by characters + /// matched by `sep`. + /// + /// # Example + /// + /// ```rust + /// let v: Vec<&str> = "Mary had a little lamb".split(' ').collect(); + /// assert_eq!(v, vec!["Mary", "had", "a", "little", "lamb"]); + /// + /// let v: Vec<&str> = "abc1def2ghi".split(|c: char| c.is_numeric()).collect(); + /// assert_eq!(v, vec!["abc", "def", "ghi"]); + /// + /// let v: Vec<&str> = "lionXXtigerXleopard".split('X').collect(); + /// assert_eq!(v, vec!["lion", "", "tiger", "leopard"]); + /// + /// let v: Vec<&str> = "".split('X').collect(); + /// assert_eq!(v, vec![""]); + /// ``` + #[unstable = "awaiting pattern/matcher stabilization"] + fn split(&self, sep: Sep) -> CharSplits { + core_str::StrExt::split(self[], sep) + } + + /// An iterator over substrings of `self`, separated by characters + /// matched by `sep`, restricted to splitting at most `count` + /// times. + /// + /// # Example + /// + /// ```rust + /// let v: Vec<&str> = "Mary had a little lambda".splitn(2, ' ').collect(); + /// assert_eq!(v, vec!["Mary", "had", "a little lambda"]); + /// + /// let v: Vec<&str> = "abc1def2ghi".splitn(1, |c: char| c.is_numeric()).collect(); + /// assert_eq!(v, vec!["abc", "def2ghi"]); + /// + /// let v: Vec<&str> = "lionXXtigerXleopard".splitn(2, 'X').collect(); + /// assert_eq!(v, vec!["lion", "", "tigerXleopard"]); + /// + /// let v: Vec<&str> = "abcXdef".splitn(0, 'X').collect(); + /// assert_eq!(v, vec!["abcXdef"]); + /// + /// let v: Vec<&str> = "".splitn(1, 'X').collect(); + /// assert_eq!(v, vec![""]); + /// ``` + #[unstable = "awaiting pattern/matcher stabilization"] + fn splitn(&self, count: uint, sep: Sep) -> CharSplitsN { + core_str::StrExt::splitn(self[], count, sep) + } + + /// An iterator over substrings of `self`, separated by characters + /// matched by `sep`. + /// + /// Equivalent to `split`, except that the trailing substring + /// is skipped if empty (terminator semantics). + /// + /// # Example + /// + /// ```rust + /// let v: Vec<&str> = "A.B.".split_terminator('.').collect(); + /// assert_eq!(v, vec!["A", "B"]); + /// + /// let v: Vec<&str> = "A..B..".split_terminator('.').collect(); + /// assert_eq!(v, vec!["A", "", "B", ""]); + /// + /// let v: Vec<&str> = "Mary had a little lamb".split(' ').rev().collect(); + /// assert_eq!(v, vec!["lamb", "little", "a", "had", "Mary"]); + /// + /// let v: Vec<&str> = "abc1def2ghi".split(|c: char| c.is_numeric()).rev().collect(); + /// assert_eq!(v, vec!["ghi", "def", "abc"]); + /// + /// let v: Vec<&str> = "lionXXtigerXleopard".split('X').rev().collect(); + /// assert_eq!(v, vec!["leopard", "tiger", "", "lion"]); + /// ``` + #[unstable = "awaiting pattern/matcher stabilization"] + fn split_terminator(&self, sep: Sep) -> CharSplits { + core_str::StrExt::split_terminator(self[], sep) + } + + /// An iterator over substrings of `self`, separated by characters + /// matched by `sep`, starting from the end of the string. + /// Restricted to splitting at most `count` times. + /// + /// # Example + /// + /// ```rust + /// let v: Vec<&str> = "Mary had a little lamb".rsplitn(2, ' ').collect(); + /// assert_eq!(v, vec!["lamb", "little", "Mary had a"]); + /// + /// let v: Vec<&str> = "abc1def2ghi".rsplitn(1, |c: char| c.is_numeric()).collect(); + /// assert_eq!(v, vec!["ghi", "abc1def"]); + /// + /// let v: Vec<&str> = "lionXXtigerXleopard".rsplitn(2, 'X').collect(); + /// assert_eq!(v, vec!["leopard", "tiger", "lionX"]); + /// ``` + #[unstable = "awaiting pattern/matcher stabilization"] + fn rsplitn(&self, count: uint, sep: Sep) -> CharSplitsN { + core_str::StrExt::rsplitn(self[], count, sep) + } + + /// An iterator over the start and end indices of the disjoint + /// matches of `sep` within `self`. + /// + /// That is, each returned value `(start, end)` satisfies + /// `self.slice(start, end) == sep`. For matches of `sep` within + /// `self` that overlap, only the indices corresponding to the + /// first match are returned. + /// + /// # Example + /// + /// ```rust + /// let v: Vec<(uint, uint)> = "abcXXXabcYYYabc".match_indices("abc").collect(); + /// assert_eq!(v, vec![(0,3), (6,9), (12,15)]); + /// + /// let v: Vec<(uint, uint)> = "1abcabc2".match_indices("abc").collect(); + /// assert_eq!(v, vec![(1,4), (4,7)]); + /// + /// let v: Vec<(uint, uint)> = "ababa".match_indices("aba").collect(); + /// assert_eq!(v, vec![(0, 3)]); // only the first `aba` + /// ``` + #[unstable = "awaiting pattern/matcher stabilization"] + fn match_indices<'a>(&'a self, sep: &'a str) -> MatchIndices<'a> { + core_str::StrExt::match_indices(self[], sep) + } + + /// An iterator over the substrings of `self` separated by `sep`. + /// + /// # Example + /// + /// ```rust + /// let v: Vec<&str> = "abcXXXabcYYYabc".split_str("abc").collect(); + /// assert_eq!(v, vec!["", "XXX", "YYY", ""]); + /// + /// let v: Vec<&str> = "1abcabc2".split_str("abc").collect(); + /// assert_eq!(v, vec!["1", "", "2"]); + /// ``` + #[unstable = "awaiting pattern/matcher stabilization"] + fn split_str<'a>(&'a self, s: &'a str) -> StrSplits<'a> { + core_str::StrExt::split_str(self[], s) + } + + /// An iterator over the lines of a string (subsequences separated + /// by `\n`). This does not include the empty string after a + /// trailing `\n`. + /// + /// # Example + /// + /// ```rust + /// let four_lines = "foo\nbar\n\nbaz\n"; + /// let v: Vec<&str> = four_lines.lines().collect(); + /// assert_eq!(v, vec!["foo", "bar", "", "baz"]); + /// ``` + #[stable] + fn lines(&self) -> Lines { + core_str::StrExt::lines(self[]) + } + + /// An iterator over the lines of a string, separated by either + /// `\n` or `\r\n`. As with `.lines()`, this does not include an + /// empty trailing line. + /// + /// # Example + /// + /// ```rust + /// let four_lines = "foo\r\nbar\n\r\nbaz\n"; + /// let v: Vec<&str> = four_lines.lines_any().collect(); + /// assert_eq!(v, vec!["foo", "bar", "", "baz"]); + /// ``` + #[stable] + fn lines_any(&self) -> LinesAny { + core_str::StrExt::lines_any(self[]) + } + + /// Returns the number of Unicode code points (`char`) that a + /// string holds. + /// + /// This does not perform any normalization, and is `O(n)`, since + /// UTF-8 is a variable width encoding of code points. + /// + /// *Warning*: The number of code points in a string does not directly + /// correspond to the number of visible characters or width of the + /// visible text due to composing characters, and double- and + /// zero-width ones. + /// + /// See also `.len()` for the byte length. + /// + /// # Example + /// + /// ```rust + /// // composed forms of `ö` and `é` + /// let c = "Löwe 老虎 Léopard"; // German, Simplified Chinese, French + /// // decomposed forms of `ö` and `é` + /// let d = "Lo\u0308we 老虎 Le\u0301opard"; + /// + /// assert_eq!(c.char_len(), 15); + /// assert_eq!(d.char_len(), 17); + /// + /// assert_eq!(c.len(), 21); + /// assert_eq!(d.len(), 23); + /// + /// // the two strings *look* the same + /// println!("{}", c); + /// println!("{}", d); + /// ``` + #[deprecated = "call .chars().count() instead"] + fn char_len(&self) -> uint { + core_str::StrExt::char_len(self[]) + } + + /// Returns a slice of the given string from the byte range + /// [`begin`..`end`). + /// + /// This operation is `O(1)`. + /// + /// Panics when `begin` and `end` do not point to valid characters + /// or point beyond the last character of the string. + /// + /// See also `slice_to` and `slice_from` for slicing prefixes and + /// suffixes of strings, and `slice_chars` for slicing based on + /// code point counts. + /// + /// # Example + /// + /// ```rust + /// let s = "Löwe 老虎 Léopard"; + /// assert_eq!(s.slice(0, 1), "L"); + /// + /// assert_eq!(s.slice(1, 9), "öwe 老"); + /// + /// // these will panic: + /// // byte 2 lies within `ö`: + /// // s.slice(2, 3); + /// + /// // byte 8 lies within `老` + /// // s.slice(1, 8); + /// + /// // byte 100 is outside the string + /// // s.slice(3, 100); + /// ``` + #[unstable = "use slice notation [a..b] instead"] + fn slice(&self, begin: uint, end: uint) -> &str { + core_str::StrExt::slice(self[], begin, end) + } + + /// Returns a slice of the string from `begin` to its end. + /// + /// Equivalent to `self.slice(begin, self.len())`. + /// + /// Panics when `begin` does not point to a valid character, or is + /// out of bounds. + /// + /// See also `slice`, `slice_to` and `slice_chars`. + #[unstable = "use slice notation [a..] instead"] + fn slice_from(&self, begin: uint) -> &str { + core_str::StrExt::slice_from(self[], begin) + } + + /// Returns a slice of the string from the beginning to byte + /// `end`. + /// + /// Equivalent to `self.slice(0, end)`. + /// + /// Panics when `end` does not point to a valid character, or is + /// out of bounds. + /// + /// See also `slice`, `slice_from` and `slice_chars`. + #[unstable = "use slice notation [0..a] instead"] + fn slice_to(&self, end: uint) -> &str { + core_str::StrExt::slice_to(self[], end) + } + + /// Returns a slice of the string from the character range + /// [`begin`..`end`). + /// + /// That is, start at the `begin`-th code point of the string and + /// continue to the `end`-th code point. This does not detect or + /// handle edge cases such as leaving a combining character as the + /// first code point of the string. + /// + /// Due to the design of UTF-8, this operation is `O(end)`. + /// See `slice`, `slice_to` and `slice_from` for `O(1)` + /// variants that use byte indices rather than code point + /// indices. + /// + /// Panics if `begin` > `end` or the either `begin` or `end` are + /// beyond the last character of the string. + /// + /// # Example + /// + /// ```rust + /// let s = "Löwe 老虎 Léopard"; + /// assert_eq!(s.slice_chars(0, 4), "Löwe"); + /// assert_eq!(s.slice_chars(5, 7), "老虎"); + /// ``` + #[unstable = "may have yet to prove its worth"] + fn slice_chars(&self, begin: uint, end: uint) -> &str { + core_str::StrExt::slice_chars(self[], begin, end) + } + + /// Takes a bytewise (not UTF-8) slice from a string. + /// + /// Returns the substring from [`begin`..`end`). + /// + /// Caller must check both UTF-8 character boundaries and the boundaries of + /// the entire slice as well. + #[stable] + unsafe fn slice_unchecked(&self, begin: uint, end: uint) -> &str { + core_str::StrExt::slice_unchecked(self[], begin, end) + } + + /// Returns true if `needle` is a prefix of the string. + /// + /// # Example + /// + /// ```rust + /// assert!("banana".starts_with("ba")); + /// ``` + #[unstable = "awaiting pattern/matcher stabilization"] + fn starts_with(&self, needle: &str) -> bool { + core_str::StrExt::starts_with(self[], needle) + } + + /// Returns true if `needle` is a suffix of the string. + /// + /// # Example + /// + /// ```rust + /// assert!("banana".ends_with("nana")); + /// ``` + #[unstable = "awaiting pattern/matcher stabilization"] + fn ends_with(&self, needle: &str) -> bool { + core_str::StrExt::ends_with(self[], needle) + } + + /// Returns a string with characters that match `to_trim` removed from the left and the right. + /// + /// # Arguments + /// + /// * to_trim - a character matcher + /// + /// # Example + /// + /// ```rust + /// assert_eq!("11foo1bar11".trim_chars('1'), "foo1bar") + /// let x: &[_] = &['1', '2']; + /// assert_eq!("12foo1bar12".trim_chars(x), "foo1bar") + /// assert_eq!("123foo1bar123".trim_chars(|c: char| c.is_numeric()), "foo1bar") + /// ``` + #[unstable = "awaiting pattern/matcher stabilization"] + fn trim_chars(&self, to_trim: C) -> &str { + core_str::StrExt::trim_chars(self[], to_trim) + } + + /// Returns a string with leading `chars_to_trim` removed. + /// + /// # Arguments + /// + /// * to_trim - a character matcher + /// + /// # Example + /// + /// ```rust + /// assert_eq!("11foo1bar11".trim_left_chars('1'), "foo1bar11") + /// let x: &[_] = &['1', '2']; + /// assert_eq!("12foo1bar12".trim_left_chars(x), "foo1bar12") + /// assert_eq!("123foo1bar123".trim_left_chars(|c: char| c.is_numeric()), "foo1bar123") + /// ``` + #[unstable = "awaiting pattern/matcher stabilization"] + fn trim_left_chars(&self, to_trim: C) -> &str { + core_str::StrExt::trim_left_chars(self[], to_trim) + } + + /// Returns a string with trailing `chars_to_trim` removed. + /// + /// # Arguments + /// + /// * to_trim - a character matcher + /// + /// # Example + /// + /// ```rust + /// assert_eq!("11foo1bar11".trim_right_chars('1'), "11foo1bar") + /// let x: &[_] = &['1', '2']; + /// assert_eq!("12foo1bar12".trim_right_chars(x), "12foo1bar") + /// assert_eq!("123foo1bar123".trim_right_chars(|c: char| c.is_numeric()), "123foo1bar") + /// ``` + #[unstable = "awaiting pattern/matcher stabilization"] + fn trim_right_chars(&self, to_trim: C) -> &str { + core_str::StrExt::trim_right_chars(self[], to_trim) + } + + /// Check that `index`-th byte lies at the start and/or end of a + /// UTF-8 code point sequence. + /// + /// The start and end of the string (when `index == self.len()`) + /// are considered to be boundaries. + /// + /// Panics if `index` is greater than `self.len()`. + /// + /// # Example + /// + /// ```rust + /// let s = "Löwe 老虎 Léopard"; + /// assert!(s.is_char_boundary(0)); + /// // start of `老` + /// assert!(s.is_char_boundary(6)); + /// assert!(s.is_char_boundary(s.len())); + /// + /// // second byte of `ö` + /// assert!(!s.is_char_boundary(2)); + /// + /// // third byte of `老` + /// assert!(!s.is_char_boundary(8)); + /// ``` + #[unstable = "naming is uncertain with container conventions"] + fn is_char_boundary(&self, index: uint) -> bool { + core_str::StrExt::is_char_boundary(self[], index) + } + + /// Pluck a character out of a string and return the index of the next + /// character. + /// + /// This function can be used to iterate over the Unicode characters of a + /// string. + /// + /// # Example + /// + /// This example manually iterates through the characters of a + /// string; this should normally be done by `.chars()` or + /// `.char_indices`. + /// + /// ```rust + /// use std::str::CharRange; + /// + /// let s = "中华Việt Nam"; + /// let mut i = 0u; + /// while i < s.len() { + /// let CharRange {ch, next} = s.char_range_at(i); + /// println!("{}: {}", i, ch); + /// i = next; + /// } + /// ``` + /// + /// This outputs: + /// + /// ```text + /// 0: 中 + /// 3: 华 + /// 6: V + /// 7: i + /// 8: ệ + /// 11: t + /// 12: + /// 13: N + /// 14: a + /// 15: m + /// ``` + /// + /// # Arguments + /// + /// * s - The string + /// * i - The byte offset of the char to extract + /// + /// # Return value + /// + /// A record {ch: char, next: uint} containing the char value and the byte + /// index of the next Unicode character. + /// + /// # Panics + /// + /// If `i` is greater than or equal to the length of the string. + /// If `i` is not the index of the beginning of a valid UTF-8 character. + #[unstable = "naming is uncertain with container conventions"] + fn char_range_at(&self, start: uint) -> CharRange { + core_str::StrExt::char_range_at(self[], start) + } + + /// Given a byte position and a str, return the previous char and its position. + /// + /// This function can be used to iterate over a Unicode string in reverse. + /// + /// Returns 0 for next index if called on start index 0. + /// + /// # Panics + /// + /// If `i` is greater than the length of the string. + /// If `i` is not an index following a valid UTF-8 character. + #[unstable = "naming is uncertain with container conventions"] + fn char_range_at_reverse(&self, start: uint) -> CharRange { + core_str::StrExt::char_range_at_reverse(self[], start) + } + + /// Plucks the character starting at the `i`th byte of a string. + /// + /// # Example + /// + /// ```rust + /// let s = "abπc"; + /// assert_eq!(s.char_at(1), 'b'); + /// assert_eq!(s.char_at(2), 'π'); + /// assert_eq!(s.char_at(4), 'c'); + /// ``` + /// + /// # Panics + /// + /// If `i` is greater than or equal to the length of the string. + /// If `i` is not the index of the beginning of a valid UTF-8 character. + #[unstable = "naming is uncertain with container conventions"] + fn char_at(&self, i: uint) -> char { + core_str::StrExt::char_at(self[], i) + } + + /// Plucks the character ending at the `i`th byte of a string. + /// + /// # Panics + /// + /// If `i` is greater than the length of the string. + /// If `i` is not an index following a valid UTF-8 character. + #[unstable = "naming is uncertain with container conventions"] + fn char_at_reverse(&self, i: uint) -> char { + core_str::StrExt::char_at_reverse(self[], i) + } + + /// Work with the byte buffer of a string as a byte slice. + /// + /// # Example + /// + /// ```rust + /// assert_eq!("bors".as_bytes(), b"bors"); + /// ``` + #[stable] + fn as_bytes(&self) -> &[u8] { + core_str::StrExt::as_bytes(self[]) + } + + /// Returns the byte index of the first character of `self` that + /// matches `search`. + /// + /// # Return value + /// + /// `Some` containing the byte index of the last matching character + /// or `None` if there is no match + /// + /// # Example + /// + /// ```rust + /// let s = "Löwe 老虎 Léopard"; + /// + /// assert_eq!(s.find('L'), Some(0)); + /// assert_eq!(s.find('é'), Some(14)); + /// + /// // the first space + /// assert_eq!(s.find(|c: char| c.is_whitespace()), Some(5)); + /// + /// // neither are found + /// let x: &[_] = &['1', '2']; + /// assert_eq!(s.find(x), None); + /// ``` + #[unstable = "awaiting pattern/matcher stabilization"] + fn find(&self, search: C) -> Option { + core_str::StrExt::find(self[], search) + } + + /// Returns the byte index of the last character of `self` that + /// matches `search`. + /// + /// # Return value + /// + /// `Some` containing the byte index of the last matching character + /// or `None` if there is no match. + /// + /// # Example + /// + /// ```rust + /// let s = "Löwe 老虎 Léopard"; + /// + /// assert_eq!(s.rfind('L'), Some(13)); + /// assert_eq!(s.rfind('é'), Some(14)); + /// + /// // the second space + /// assert_eq!(s.rfind(|c: char| c.is_whitespace()), Some(12)); + /// + /// // searches for an occurrence of either `1` or `2`, but neither are found + /// let x: &[_] = &['1', '2']; + /// assert_eq!(s.rfind(x), None); + /// ``` + #[unstable = "awaiting pattern/matcher stabilization"] + fn rfind(&self, search: C) -> Option { + core_str::StrExt::rfind(self[], search) + } + + /// Returns the byte index of the first matching substring + /// + /// # Arguments + /// + /// * `needle` - The string to search for + /// + /// # Return value + /// + /// `Some` containing the byte index of the first matching substring + /// or `None` if there is no match. + /// + /// # Example + /// + /// ```rust + /// let s = "Löwe 老虎 Léopard"; + /// + /// assert_eq!(s.find_str("老虎 L"), Some(6)); + /// assert_eq!(s.find_str("muffin man"), None); + /// ``` + #[unstable = "awaiting pattern/matcher stabilization"] + fn find_str(&self, needle: &str) -> Option { + core_str::StrExt::find_str(self[], needle) + } + + /// Retrieves the first character from a string slice and returns + /// it. This does not allocate a new string; instead, it returns a + /// slice that point one character beyond the character that was + /// shifted. If the string does not contain any characters, + /// None is returned instead. + /// + /// # Example + /// + /// ```rust + /// let s = "Löwe 老虎 Léopard"; + /// let (c, s1) = s.slice_shift_char().unwrap(); + /// assert_eq!(c, 'L'); + /// assert_eq!(s1, "öwe 老虎 Léopard"); + /// + /// let (c, s2) = s1.slice_shift_char().unwrap(); + /// assert_eq!(c, 'ö'); + /// assert_eq!(s2, "we 老虎 Léopard"); + /// ``` + #[unstable = "awaiting conventions about shifting and slices"] + fn slice_shift_char(&self) -> Option<(char, &str)> { + core_str::StrExt::slice_shift_char(self[]) + } + + /// Returns the byte offset of an inner slice relative to an enclosing outer slice. + /// + /// Panics if `inner` is not a direct slice contained within self. + /// + /// # Example + /// + /// ```rust + /// let string = "a\nb\nc"; + /// let lines: Vec<&str> = string.lines().collect(); + /// + /// assert!(string.subslice_offset(lines[0]) == 0); // &"a" + /// assert!(string.subslice_offset(lines[1]) == 2); // &"b" + /// assert!(string.subslice_offset(lines[2]) == 4); // &"c" + /// ``` + #[unstable = "awaiting pattern/matcher stabilization"] + fn subslice_offset(&self, inner: &str) -> uint { + core_str::StrExt::subslice_offset(self[], inner) + } + + /// Return an unsafe pointer to the strings buffer. + /// + /// The caller must ensure that the string outlives this pointer, + /// and that it is not reallocated (e.g. by pushing to the + /// string). + #[stable] #[inline] - fn into_string(self) -> String { - String::from_str(self) + fn as_ptr(&self) -> *const u8 { + core_str::StrExt::as_ptr(self[]) + } + + /// Return an iterator of `u16` over the string encoded as UTF-16. + #[unstable = "this functionality may only be provided by libunicode"] + fn utf16_units(&self) -> Utf16Units { + Utf16Units { encoder: Utf16Encoder::new(self[].chars()) } + } + + /// Return the number of bytes in this string + /// + /// # Example + /// + /// ``` + /// assert_eq!("foo".len(), 3); + /// assert_eq!("ƒoo".len(), 4); + /// ``` + #[stable] + #[inline] + fn len(&self) -> uint { + core_str::StrExt::len(self[]) + } + + /// Returns true if this slice contains no bytes + /// + /// # Example + /// + /// ``` + /// assert!("".is_empty()); + /// ``` + #[inline] + #[stable] + fn is_empty(&self) -> bool { + core_str::StrExt::is_empty(self[]) + } + + /// Parse this string into the specified type. + /// + /// # Example + /// + /// ``` + /// assert_eq!("4".parse::(), Some(4)); + /// assert_eq!("j".parse::(), None); + /// ``` + #[inline] + #[unstable = "this method was just created"] + fn parse(&self) -> Option { + FromStr::from_str(self[]) + } + + /// Returns an iterator over the + /// [grapheme clusters](http://www.unicode.org/reports/tr29/#Grapheme_Cluster_Boundaries) + /// of the string. + /// + /// If `is_extended` is true, the iterator is over the *extended grapheme clusters*; + /// otherwise, the iterator is over the *legacy grapheme clusters*. + /// [UAX#29](http://www.unicode.org/reports/tr29/#Grapheme_Cluster_Boundaries) + /// recommends extended grapheme cluster boundaries for general processing. + /// + /// # Example + /// + /// ```rust + /// let gr1 = "a\u0310e\u0301o\u0308\u0332".graphemes(true).collect::>(); + /// let b: &[_] = &["a\u0310", "e\u0301", "o\u0308\u0332"]; + /// assert_eq!(gr1.as_slice(), b); + /// let gr2 = "a\r\nb🇷🇺🇸🇹".graphemes(true).collect::>(); + /// let b: &[_] = &["a", "\r\n", "b", "🇷🇺🇸🇹"]; + /// assert_eq!(gr2.as_slice(), b); + /// ``` + #[unstable = "this functionality may only be provided by libunicode"] + fn graphemes(&self, is_extended: bool) -> Graphemes { + UnicodeStr::graphemes(self[], is_extended) + } + + /// Returns an iterator over the grapheme clusters of self and their byte offsets. + /// See `graphemes()` method for more information. + /// + /// # Example + /// + /// ```rust + /// let gr_inds = "a̐éö̲\r\n".grapheme_indices(true).collect::>(); + /// let b: &[_] = &[(0u, "a̐"), (3, "é"), (6, "ö̲"), (11, "\r\n")]; + /// assert_eq!(gr_inds.as_slice(), b); + /// ``` + #[unstable = "this functionality may only be provided by libunicode"] + fn grapheme_indices(&self, is_extended: bool) -> GraphemeIndices { + UnicodeStr::grapheme_indices(self[], is_extended) + } + + /// An iterator over the words of a string (subsequences separated + /// by any sequence of whitespace). Sequences of whitespace are + /// collapsed, so empty "words" are not included. + /// + /// # Example + /// + /// ```rust + /// let some_words = " Mary had\ta little \n\t lamb"; + /// let v: Vec<&str> = some_words.words().collect(); + /// assert_eq!(v, vec!["Mary", "had", "a", "little", "lamb"]); + /// ``` + #[stable] + fn words(&self) -> Words { + UnicodeStr::words(self[]) + } + + /// Returns true if the string contains only whitespace. + /// + /// Whitespace characters are determined by `char::is_whitespace`. + /// + /// # Example + /// + /// ```rust + /// assert!(" \t\n".is_whitespace()); + /// assert!("".is_whitespace()); + /// + /// assert!( !"abc".is_whitespace()); + /// ``` + #[deprecated = "use .chars().all(|c| c.is_whitespace())"] + fn is_whitespace(&self) -> bool { + UnicodeStr::is_whitespace(self[]) + } + + /// Returns true if the string contains only alphanumeric code + /// points. + /// + /// Alphanumeric characters are determined by `char::is_alphanumeric`. + /// + /// # Example + /// + /// ```rust + /// assert!("Löwe老虎Léopard123".is_alphanumeric()); + /// assert!("".is_alphanumeric()); + /// + /// assert!( !" &*~".is_alphanumeric()); + /// ``` + #[deprecated = "use .chars().all(|c| c.is_alphanumeric())"] + fn is_alphanumeric(&self) -> bool { + UnicodeStr::is_alphanumeric(self[]) + } + + /// Returns a string's displayed width in columns, treating control + /// characters as zero-width. + /// + /// `is_cjk` determines behavior for characters in the Ambiguous category: + /// if `is_cjk` is `true`, these are 2 columns wide; otherwise, they are 1. + /// In CJK locales, `is_cjk` should be `true`, else it should be `false`. + /// [Unicode Standard Annex #11](http://www.unicode.org/reports/tr11/) + /// recommends that these characters be treated as 1 column (i.e., + /// `is_cjk` = `false`) if the locale is unknown. + #[unstable = "this functionality may only be provided by libunicode"] + fn width(&self, is_cjk: bool) -> uint { + UnicodeStr::width(self[], is_cjk) + } + + /// Returns a string with leading and trailing whitespace removed. + #[stable] + fn trim(&self) -> &str { + UnicodeStr::trim(self[]) + } + + /// Returns a string with leading whitespace removed. + #[stable] + fn trim_left(&self) -> &str { + UnicodeStr::trim_left(self[]) + } + + /// Returns a string with trailing whitespace removed. + #[stable] + fn trim_right(&self) -> &str { + UnicodeStr::trim_right(self[]) } } +impl StrExt for str {} + #[cfg(test)] mod tests { use prelude::*; @@ -1541,28 +2428,6 @@ fn test_contains_char() { assert!(!"".contains_char('a')); } - #[test] - fn test_truncate_utf16_at_nul() { - let v = []; - let b: &[u16] = &[]; - assert_eq!(truncate_utf16_at_nul(&v), b); - - let v = [0, 2, 3]; - assert_eq!(truncate_utf16_at_nul(&v), b); - - let v = [1, 0, 3]; - let b: &[u16] = &[1]; - assert_eq!(truncate_utf16_at_nul(&v), b); - - let v = [1, 2, 0]; - let b: &[u16] = &[1, 2]; - assert_eq!(truncate_utf16_at_nul(&v), b); - - let v = [1, 2, 3]; - let b: &[u16] = &[1, 2, 3]; - assert_eq!(truncate_utf16_at_nul(&v), b); - } - #[test] fn test_char_at() { let s = "ศไทย中华Việt Nam"; @@ -1814,27 +2679,6 @@ fn test_words() { assert_eq!(words, vec!["Märy", "häd", "ä", "little", "lämb", "Little", "lämb"]) } - #[test] - fn test_lev_distance() { - use core::char::{ from_u32, MAX }; - // Test bytelength agnosticity - for c in range(0u32, MAX as u32) - .filter_map(|i| from_u32(i)) - .map(|i| String::from_char(1, i)) { - assert_eq!(c[].lev_distance(c[]), 0); - } - - let a = "\nMäry häd ä little lämb\n\nLittle lämb\n"; - let b = "\nMary häd ä little lämb\n\nLittle lämb\n"; - let c = "Mary häd ä little lämb\n\nLittle lämb\n"; - assert_eq!(a.lev_distance(b), 1); - assert_eq!(b.lev_distance(a), 1); - assert_eq!(a.lev_distance(c), 2); - assert_eq!(c.lev_distance(a), 2); - assert_eq!(b.lev_distance(c), 1); - assert_eq!(c.lev_distance(b), 1); - } - #[test] fn test_nfd_chars() { macro_rules! t { diff --git a/src/libcollections/string.rs b/src/libcollections/string.rs index db59424cedd6..0e2b514d92d4 100644 --- a/src/libcollections/string.rs +++ b/src/libcollections/string.rs @@ -21,13 +21,12 @@ use core::mem; use core::ptr; use core::ops; -// FIXME: ICE's abound if you import the `Slice` type while importing `Slice` trait use core::raw::Slice as RawSlice; +use unicode::str as unicode_str; +use unicode::str::Utf16Item; use slice::CloneSliceExt; -use str; -use str::{CharRange, CowString, FromStr, StrAllocating}; -use str::MaybeOwned::Owned; +use str::{mod, CharRange, FromStr, StrExt, Owned, Utf8Error}; use vec::{DerefVec, Vec, as_vec}; /// A growable string stored as a UTF-8 encoded buffer. @@ -87,8 +86,10 @@ pub fn from_str(string: &str) -> String { /// Returns the vector as a string buffer, if possible, taking care not to /// copy it. /// - /// Returns `Err` with the original vector if the vector contains invalid - /// UTF-8. + /// # Failure + /// + /// If the given vector is not valid UTF-8, then the original vector and the + /// corresponding error is returned. /// /// # Examples /// @@ -103,11 +104,10 @@ pub fn from_str(string: &str) -> String { /// ``` #[inline] #[unstable = "error type may change"] - pub fn from_utf8(vec: Vec) -> Result> { - if str::is_utf8(vec.as_slice()) { - Ok(String { vec: vec }) - } else { - Err(vec) + pub fn from_utf8(vec: Vec) -> Result, Utf8Error)> { + match str::from_utf8(vec.as_slice()) { + Ok(..) => Ok(String { vec: vec }), + Err(e) => Err((vec, e)) } } @@ -123,8 +123,9 @@ pub fn from_utf8(vec: Vec) -> Result> { /// ``` #[unstable = "return type may change"] pub fn from_utf8_lossy<'a>(v: &'a [u8]) -> CowString<'a> { - if str::is_utf8(v) { - return Cow::Borrowed(unsafe { mem::transmute(v) }) + match str::from_utf8(v) { + Ok(s) => return Cow::Borrowed(s), + Err(..) => {} } static TAG_CONT_U8: u8 = 128u8; @@ -173,7 +174,7 @@ macro_rules! error(() => ({ if byte < 128u8 { // subseqidx handles this } else { - let w = str::utf8_char_width(byte); + let w = unicode_str::utf8_char_width(byte); match w { 2 => { @@ -235,7 +236,7 @@ macro_rules! error(() => ({ res.as_mut_vec().push_all(v[subseqidx..total]) }; } - Cow::Owned(res.into_string()) + Cow::Owned(res) } /// Decode a UTF-16 encoded vector `v` into a `String`, returning `None` @@ -256,10 +257,10 @@ macro_rules! error(() => ({ #[unstable = "error value in return may change"] pub fn from_utf16(v: &[u16]) -> Option { let mut s = String::with_capacity(v.len()); - for c in str::utf16_items(v) { + for c in unicode_str::utf16_items(v) { match c { - str::ScalarValue(c) => s.push(c), - str::LoneSurrogate(_) => return None + Utf16Item::ScalarValue(c) => s.push(c), + Utf16Item::LoneSurrogate(_) => return None } } Some(s) @@ -281,7 +282,7 @@ pub fn from_utf16(v: &[u16]) -> Option { /// ``` #[stable] pub fn from_utf16_lossy(v: &[u16]) -> String { - str::utf16_items(v).map(|c| c.to_char_lossy()).collect() + unicode_str::utf16_items(v).map(|c| c.to_char_lossy()).collect() } /// Convert a vector of `char`s to a `String`. @@ -812,21 +813,12 @@ fn ne(&self, other: &CowString<'a>) -> bool { PartialEq::ne(&**self, &**other) } } #[experimental = "waiting on Str stabilization"] +#[allow(deprecated)] impl Str for String { #[inline] #[stable] fn as_slice<'a>(&'a self) -> &'a str { - unsafe { - mem::transmute(self.vec.as_slice()) - } - } -} - -#[experimental = "waiting on StrAllocating stabilization"] -impl StrAllocating for String { - #[inline] - fn into_string(self) -> String { - self + unsafe { mem::transmute(self.vec.as_slice()) } } } @@ -841,7 +833,7 @@ fn default() -> String { #[experimental = "waiting on Show stabilization"] impl fmt::Show for String { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - self.as_slice().fmt(f) + (*self).fmt(f) } } @@ -849,7 +841,7 @@ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { impl hash::Hash for String { #[inline] fn hash(&self, hasher: &mut H) { - self.as_slice().hash(hasher) + (*self).hash(hasher) } } @@ -873,7 +865,7 @@ fn add(mut self, other: &str) -> String { impl ops::Slice for String { #[inline] fn as_slice_<'a>(&'a self) -> &'a str { - self.as_slice() + unsafe { mem::transmute(self.vec.as_slice()) } } #[inline] @@ -894,7 +886,9 @@ fn slice_or_fail<'a>(&'a self, from: &uint, to: &uint) -> &'a str { #[experimental = "waiting on Deref stabilization"] impl ops::Deref for String { - fn deref<'a>(&'a self) -> &'a str { self.as_slice() } + fn deref<'a>(&'a self) -> &'a str { + unsafe { mem::transmute(self.vec[]) } + } } /// Wrapper type providing a `&String` reference via `Deref`. @@ -1015,6 +1009,18 @@ pub unsafe fn from_utf8(bytes: Vec) -> String { } } +/// A clone-on-write string +#[stable] +pub type CowString<'a> = Cow<'a, String, str>; + +#[allow(deprecated)] +impl<'a> Str for CowString<'a> { + #[inline] + fn as_slice<'b>(&'b self) -> &'b str { + (**self).as_slice() + } +} + #[cfg(test)] mod tests { use prelude::*; diff --git a/src/libcore/fmt/float.rs b/src/libcore/fmt/float.rs index d849bfa24c13..9ab450efd227 100644 --- a/src/libcore/fmt/float.rs +++ b/src/libcore/fmt/float.rs @@ -23,7 +23,7 @@ use ops::FnOnce; use result::Result::Ok; use slice::{mod, SliceExt}; -use str::StrPrelude; +use str::StrExt; /// A flag that specifies whether to use exponential (scientific) notation. pub enum ExponentFormat { diff --git a/src/libcore/fmt/mod.rs b/src/libcore/fmt/mod.rs index 79fb11f38543..29815e2fc854 100644 --- a/src/libcore/fmt/mod.rs +++ b/src/libcore/fmt/mod.rs @@ -24,7 +24,7 @@ use result; use slice::SliceExt; use slice; -use str::StrPrelude; +use str::{StrExt, Utf8Error}; pub use self::num::radix; pub use self::num::Radix; @@ -795,5 +795,18 @@ fn fmt(&self, f: &mut Formatter) -> Result { } } +impl Show for Utf8Error { + fn fmt(&self, f: &mut Formatter) -> Result { + match *self { + Utf8Error::InvalidByte(n) => { + write!(f, "invalid utf-8: invalid byte at index {}", n) + } + Utf8Error::TooShort => { + write!(f, "invalid utf-8: byte slice too short") + } + } + } +} + // If you expected tests to be here, look instead at the run-pass/ifmt.rs test, // it's a lot easier than creating all of the rt::Piece structures here. diff --git a/src/libcore/num/mod.rs b/src/libcore/num/mod.rs index 84d1d8e459a8..60735879213d 100644 --- a/src/libcore/num/mod.rs +++ b/src/libcore/num/mod.rs @@ -32,7 +32,7 @@ use ops::{Not, BitAnd, BitOr, BitXor, Shl, Shr}; use option::Option; use option::Option::{Some, None}; -use str::{FromStr, from_str, StrPrelude}; +use str::{FromStr, from_str, StrExt}; /// Simultaneous division and remainder #[inline] diff --git a/src/libcore/prelude.rs b/src/libcore/prelude.rs index ff3fc870beb8..f6abc8da79c0 100644 --- a/src/libcore/prelude.rs +++ b/src/libcore/prelude.rs @@ -60,7 +60,7 @@ pub use ptr::RawPtr; pub use result::Result; pub use result::Result::{Ok, Err}; -pub use str::{Str, StrPrelude}; +pub use str::{Str, StrExt}; pub use tuple::{Tuple1, Tuple2, Tuple3, Tuple4}; pub use tuple::{Tuple5, Tuple6, Tuple7, Tuple8}; pub use tuple::{Tuple9, Tuple10, Tuple11, Tuple12}; diff --git a/src/libcore/str.rs b/src/libcore/str.rs index a89a7970ae9c..60d4262a9b12 100644 --- a/src/libcore/str.rs +++ b/src/libcore/str.rs @@ -16,31 +16,30 @@ #![doc(primitive = "str")] -pub use self::Utf16Item::*; -pub use self::Searcher::{Naive, TwoWay, TwoWayLong}; +use self::Searcher::{Naive, TwoWay, TwoWayLong}; -use char::Char; -use char; +use char::{mod, Char}; use clone::Clone; -use cmp::{Eq, mod}; +use cmp::{mod, Eq}; use default::Default; -use iter::{Map, Iterator, IteratorExt, DoubleEndedIterator}; -use iter::{DoubleEndedIteratorExt, ExactSizeIterator}; use iter::range; -use kinds::Sized; +use iter::{DoubleEndedIteratorExt, ExactSizeIterator}; +use iter::{Map, Iterator, IteratorExt, DoubleEndedIterator}; +use kinds::{Copy, Sized}; use mem; use num::Int; -use option::Option; -use option::Option::{None, Some}; use ops::{Fn, FnMut}; +use option::Option::{mod, None, Some}; use ptr::RawPtr; use raw::{Repr, Slice}; +use result::Result::{mod, Ok, Err}; use slice::{mod, SliceExt}; use uint; /// A trait to abstract the idea of creating a new instance of a type from a /// string. -#[experimental = "might need to return Result"] +// FIXME(#17307): there should be an `E` associated type for a `Result` return +#[unstable = "will return a Result once associated types are working"] pub trait FromStr { /// Parses a string `s` to return an optional value of this type. If the /// string is ill-formatted, the None is returned. @@ -48,6 +47,7 @@ pub trait FromStr { } /// A utility function that just calls FromStr::from_str +#[deprecated = "call the .parse() method on the string instead"] pub fn from_str(s: &str) -> Option { FromStr::from_str(s) } @@ -78,22 +78,38 @@ fn from_str(s: &str) -> Option { Section: Creating a string */ -/// Converts a slice of bytes to a string slice without performing any allocations. +/// Errors which can occur when attempting to interpret a byte slice as a `str`. +pub enum Utf8Error { + /// An invalid byte was detected at the byte offset given. + /// + /// The offset is guaranteed to be in bounds of the slice in question, and + /// the byte at the specified offset was the first invalid byte in the + /// sequence detected. + InvalidByte(uint), + + /// The byte slice was invalid because more bytes were needed but no more + /// bytes were available. + TooShort, +} + +/// Converts a slice of bytes to a string slice without performing any +/// allocations. /// /// Once the slice has been validated as utf-8, it is transmuted in-place and /// returned as a '&str' instead of a '&[u8]' /// -/// Returns None if the slice is not utf-8. -pub fn from_utf8<'a>(v: &'a [u8]) -> Option<&'a str> { - if is_utf8(v) { - Some(unsafe { from_utf8_unchecked(v) }) - } else { - None - } +/// # Failure +/// +/// Returns `Err` if the slice is not utf-8 with a description as to why the +/// provided slice is not utf-8. +pub fn from_utf8(v: &[u8]) -> Result<&str, Utf8Error> { + try!(run_utf8_validation_iterator(&mut v.iter())); + Ok(unsafe { from_utf8_unchecked(v) }) } /// Converts a slice of bytes to a string slice without checking /// that the string contains valid UTF-8. +#[stable] pub unsafe fn from_utf8_unchecked<'a>(v: &'a [u8]) -> &'a str { mem::transmute(v) } @@ -111,6 +127,7 @@ pub unsafe fn from_utf8_unchecked<'a>(v: &'a [u8]) -> &'a str { /// # Panics /// /// This function will panic if the string pointed to by `s` is not valid UTF-8. +#[unstable = "may change location based on the outcome of the c_str module"] pub unsafe fn from_c_str(s: *const i8) -> &'static str { let s = s as *const u8; let mut len = 0u; @@ -118,10 +135,11 @@ pub unsafe fn from_c_str(s: *const i8) -> &'static str { len += 1u; } let v: &'static [u8] = ::mem::transmute(Slice { data: s, len: len }); - from_utf8(v).expect("from_c_str passed invalid utf-8 data") + from_utf8(v).ok().expect("from_c_str passed invalid utf-8 data") } /// Something that can be used to compare against a character +#[unstable = "definition may change as pattern-related methods are stabilized"] pub trait CharEq { /// Determine if the splitter should split at the given character fn matches(&mut self, char) -> bool; @@ -273,12 +291,12 @@ fn next_back(&mut self) -> Option { /// External iterator for a string's characters and their byte offsets. /// Use with the `std::iter` module. #[deriving(Clone)] -pub struct CharOffsets<'a> { +pub struct CharIndices<'a> { front_offset: uint, iter: Chars<'a>, } -impl<'a> Iterator<(uint, char)> for CharOffsets<'a> { +impl<'a> Iterator<(uint, char)> for CharIndices<'a> { #[inline] fn next(&mut self) -> Option<(uint, char)> { let (pre_len, _) = self.iter.iter.size_hint(); @@ -299,7 +317,7 @@ fn size_hint(&self) -> (uint, Option) { } } -impl<'a> DoubleEndedIterator<(uint, char)> for CharOffsets<'a> { +impl<'a> DoubleEndedIterator<(uint, char)> for CharIndices<'a> { #[inline] fn next_back(&mut self) -> Option<(uint, char)> { match self.iter.next_back() { @@ -315,13 +333,15 @@ fn next_back(&mut self) -> Option<(uint, char)> { /// External iterator for a string's bytes. /// Use with the `std::iter` module. -pub type Bytes<'a> = Map<&'a u8, u8, slice::Items<'a, u8>, BytesFn>; +#[stable] +pub struct Bytes<'a> { + inner: Map<&'a u8, u8, slice::Items<'a, u8>, BytesFn>, +} /// A temporary new type wrapper that ensures that the `Bytes` iterator /// is cloneable. #[deriving(Copy)] -#[experimental = "iterator type instability"] -pub struct BytesFn(fn(&u8) -> u8); +struct BytesFn(fn(&u8) -> u8); impl<'a> Fn(&'a u8) -> u8 for BytesFn { extern "rust-call" fn call(&self, (ptr,): (&'a u8,)) -> u8 { @@ -355,8 +375,17 @@ pub struct CharSplitsN<'a, Sep> { invert: bool, } +/// An iterator over the lines of a string, separated by `\n`. +#[stable] +pub struct Lines<'a> { + inner: CharSplits<'a, char>, +} + /// An iterator over the lines of a string, separated by either `\n` or (`\r\n`). -pub type AnyLines<'a> = Map<&'a str, &'a str, CharSplits<'a, char>, fn(&str) -> &str>; +#[stable] +pub struct LinesAny<'a> { + inner: Map<&'a str, &'a str, Lines<'a>, fn(&str) -> &str>, +} impl<'a, Sep> CharSplits<'a, Sep> { #[inline] @@ -799,63 +828,6 @@ fn next(&mut self) -> Option<&'a str> { } } -/// External iterator for a string's UTF16 codeunits. -/// Use with the `std::iter` module. -#[deriving(Clone)] -pub struct Utf16CodeUnits<'a> { - encoder: Utf16Encoder> -} - -impl<'a> Iterator for Utf16CodeUnits<'a> { - #[inline] - fn next(&mut self) -> Option { self.encoder.next() } - - #[inline] - fn size_hint(&self) -> (uint, Option) { self.encoder.size_hint() } -} - - -/// Iterator adaptor for encoding `char`s to UTF-16. -#[deriving(Clone)] -pub struct Utf16Encoder { - chars: I, - extra: u16 -} - -impl Utf16Encoder { - /// Create an UTF-16 encoder from any `char` iterator. - pub fn new(chars: I) -> Utf16Encoder where I: Iterator { - Utf16Encoder { chars: chars, extra: 0 } - } -} - -impl Iterator for Utf16Encoder where I: Iterator { - #[inline] - fn next(&mut self) -> Option { - if self.extra != 0 { - let tmp = self.extra; - self.extra = 0; - return Some(tmp); - } - - let mut buf = [0u16, ..2]; - self.chars.next().map(|ch| { - let n = ch.encode_utf16(buf[mut]).unwrap_or(0); - if n == 2 { self.extra = buf[1]; } - buf[0] - }) - } - - #[inline] - fn size_hint(&self) -> (uint, Option) { - let (low, high) = self.chars.size_hint(); - // every char gets either one u16 or two u16, - // so this iterator is between 1 or 2 times as - // long as the underlying iterator. - (low, high.and_then(|n| n.checked_mul(2))) - } -} - /* Section: Comparing strings */ @@ -880,7 +852,7 @@ fn eq_slice_(a: &str, b: &str) -> bool { /// to compare &[u8] byte slices that are not necessarily valid UTF-8. #[lang="str_eq"] #[inline] -pub fn eq_slice(a: &str, b: &str) -> bool { +fn eq_slice(a: &str, b: &str) -> bool { eq_slice_(a, b) } @@ -893,32 +865,37 @@ pub fn eq_slice(a: &str, b: &str) -> bool { /// `iter` reset such that it is pointing at the first byte in the /// invalid sequence. #[inline(always)] -fn run_utf8_validation_iterator(iter: &mut slice::Items) -> bool { +fn run_utf8_validation_iterator(iter: &mut slice::Items) + -> Result<(), Utf8Error> { + let whole = iter.as_slice(); loop { // save the current thing we're pointing at. let old = *iter; // restore the iterator we had at the start of this codepoint. - macro_rules! err ( () => { {*iter = old; return false} }); + macro_rules! err (() => { { + *iter = old; + return Err(Utf8Error::InvalidByte(whole.len() - iter.as_slice().len())) + } }); macro_rules! next ( () => { - match iter.next() { - Some(a) => *a, - // we needed data, but there was none: error! - None => err!() - } - }); + match iter.next() { + Some(a) => *a, + // we needed data, but there was none: error! + None => return Err(Utf8Error::TooShort), + } + }); let first = match iter.next() { Some(&b) => b, // we're at the end of the iterator and a codepoint // boundary at the same time, so this string is valid. - None => return true + None => return Ok(()) }; // ASCII characters are always valid, so only large // bytes need more examination. if first >= 128 { - let w = utf8_char_width(first); + let w = UTF8_CHAR_WIDTH[first as uint] as uint; let second = next!(); // 2-byte encoding is for codepoints \u{0080} to \u{07ff} // first C2 80 last DF BF @@ -964,125 +941,9 @@ macro_rules! next ( () => { } /// Determines if a vector of bytes contains valid UTF-8. +#[deprecated = "call from_utf8 instead"] pub fn is_utf8(v: &[u8]) -> bool { - run_utf8_validation_iterator(&mut v.iter()) -} - -/// Determines if a vector of `u16` contains valid UTF-16 -pub fn is_utf16(v: &[u16]) -> bool { - let mut it = v.iter(); - macro_rules! next ( ($ret:expr) => { - match it.next() { Some(u) => *u, None => return $ret } - } - ); - loop { - let u = next!(true); - - match char::from_u32(u as u32) { - Some(_) => {} - None => { - let u2 = next!(false); - if u < 0xD7FF || u > 0xDBFF || - u2 < 0xDC00 || u2 > 0xDFFF { return false; } - } - } - } -} - -/// An iterator that decodes UTF-16 encoded codepoints from a vector -/// of `u16`s. -#[deriving(Clone)] -pub struct Utf16Items<'a> { - iter: slice::Items<'a, u16> -} -/// The possibilities for values decoded from a `u16` stream. -#[deriving(Copy, PartialEq, Eq, Clone, Show)] -pub enum Utf16Item { - /// A valid codepoint. - ScalarValue(char), - /// An invalid surrogate without its pair. - LoneSurrogate(u16) -} - -impl Utf16Item { - /// Convert `self` to a `char`, taking `LoneSurrogate`s to the - /// replacement character (U+FFFD). - #[inline] - pub fn to_char_lossy(&self) -> char { - match *self { - ScalarValue(c) => c, - LoneSurrogate(_) => '\u{FFFD}' - } - } -} - -impl<'a> Iterator for Utf16Items<'a> { - fn next(&mut self) -> Option { - let u = match self.iter.next() { - Some(u) => *u, - None => return None - }; - - if u < 0xD800 || 0xDFFF < u { - // not a surrogate - Some(ScalarValue(unsafe {mem::transmute(u as u32)})) - } else if u >= 0xDC00 { - // a trailing surrogate - Some(LoneSurrogate(u)) - } else { - // preserve state for rewinding. - let old = self.iter; - - let u2 = match self.iter.next() { - Some(u2) => *u2, - // eof - None => return Some(LoneSurrogate(u)) - }; - if u2 < 0xDC00 || u2 > 0xDFFF { - // not a trailing surrogate so we're not a valid - // surrogate pair, so rewind to redecode u2 next time. - self.iter = old; - return Some(LoneSurrogate(u)) - } - - // all ok, so lets decode it. - let c = ((u - 0xD800) as u32 << 10 | (u2 - 0xDC00) as u32) + 0x1_0000; - Some(ScalarValue(unsafe {mem::transmute(c)})) - } - } - - #[inline] - fn size_hint(&self) -> (uint, Option) { - let (low, high) = self.iter.size_hint(); - // we could be entirely valid surrogates (2 elements per - // char), or entirely non-surrogates (1 element per char) - (low / 2, high) - } -} - -/// Create an iterator over the UTF-16 encoded codepoints in `v`, -/// returning invalid surrogates as `LoneSurrogate`s. -/// -/// # Example -/// -/// ```rust -/// use std::str; -/// use std::str::{ScalarValue, LoneSurrogate}; -/// -/// // 𝄞music -/// let v = [0xD834, 0xDD1E, 0x006d, 0x0075, -/// 0x0073, 0xDD1E, 0x0069, 0x0063, -/// 0xD834]; -/// -/// assert_eq!(str::utf16_items(&v).collect::>(), -/// vec![ScalarValue('𝄞'), -/// ScalarValue('m'), ScalarValue('u'), ScalarValue('s'), -/// LoneSurrogate(0xDD1E), -/// ScalarValue('i'), ScalarValue('c'), -/// LoneSurrogate(0xD834)]); -/// ``` -pub fn utf16_items<'a>(v: &'a [u16]) -> Utf16Items<'a> { - Utf16Items { iter : v.iter() } + run_utf8_validation_iterator(&mut v.iter()).is_ok() } /// Return a slice of `v` ending at (and not including) the first NUL @@ -1103,6 +964,7 @@ pub fn utf16_items<'a>(v: &'a [u16]) -> Utf16Items<'a> { /// let b: &[_] = &['a' as u16, 'b' as u16]; /// assert_eq!(str::truncate_utf16_at_nul(&v), b); /// ``` +#[deprecated = "this function will be removed"] pub fn truncate_utf16_at_nul<'a>(v: &'a [u16]) -> &'a [u16] { match v.iter().position(|c| *c == 0) { // don't include the 0 @@ -1133,6 +995,7 @@ pub fn truncate_utf16_at_nul<'a>(v: &'a [u16]) -> &'a [u16] { /// Given a first byte, determine how many bytes are in this UTF-8 character #[inline] +#[deprecated = "this function has moved to libunicode"] pub fn utf8_char_width(b: u8) -> uint { return UTF8_CHAR_WIDTH[b as uint] as uint; } @@ -1141,6 +1004,7 @@ pub fn utf8_char_width(b: u8) -> uint { /// the next `char` in a string. This can be used as a data structure /// for iterating over the UTF-8 bytes of a string. #[deriving(Copy)] +#[unstable = "naming is uncertain with container conventions"] pub struct CharRange { /// Current `char` pub ch: char, @@ -1159,7 +1023,7 @@ pub mod raw { use ptr::RawPtr; use raw::Slice; use slice::SliceExt; - use str::{is_utf8, StrPrelude}; + use str::StrExt; /// Converts a slice of bytes to a string slice without checking /// that the string contains valid UTF-8. @@ -1181,8 +1045,7 @@ pub unsafe fn c_str_to_static_slice(s: *const i8) -> &'static str { curr = s.offset(len as int); } let v = Slice { data: s, len: len }; - assert!(is_utf8(::mem::transmute(v))); - ::mem::transmute(v) + super::from_utf8(::mem::transmute(v)).unwrap() } /// Takes a bytewise (not UTF-8) slice from a string. @@ -1225,7 +1088,7 @@ pub mod traits { use option::Option; use option::Option::Some; use ops; - use str::{Str, StrPrelude, eq_slice}; + use str::{Str, StrExt, eq_slice}; impl Ord for str { #[inline] @@ -1291,707 +1154,70 @@ fn slice_or_fail<'a>(&'a self, from: &uint, to: &uint) -> &'a str { } /// Any string that can be represented as a slice +#[unstable = "Instead of taking this bound generically, this trait will be \ + replaced with one of slicing syntax, deref coercions, or \ + a more generic conversion trait"] pub trait Str for Sized? { /// Work with `self` as a slice. fn as_slice<'a>(&'a self) -> &'a str; } +#[allow(deprecated)] impl Str for str { #[inline] fn as_slice<'a>(&'a self) -> &'a str { self } } +#[allow(deprecated)] impl<'a, Sized? S> Str for &'a S where S: Str { #[inline] fn as_slice(&self) -> &str { Str::as_slice(*self) } } /// Methods for string slices -pub trait StrPrelude for Sized? { - /// Returns true if one string contains another - /// - /// # Arguments - /// - /// - needle - The string to look for - /// - /// # Example - /// - /// ```rust - /// assert!("bananas".contains("nana")); - /// ``` +#[allow(missing_docs)] +pub trait StrExt for Sized? { + // NB there are no docs here are they're all located on the StrExt trait in + // libcollections, not here. + fn contains(&self, needle: &str) -> bool; - - /// Returns true if a string contains a char. - /// - /// # Arguments - /// - /// - needle - The char to look for - /// - /// # Example - /// - /// ```rust - /// assert!("hello".contains_char('e')); - /// ``` fn contains_char(&self, needle: char) -> bool; - - /// An iterator over the characters of `self`. Note, this iterates - /// over Unicode code-points, not Unicode graphemes. - /// - /// # Example - /// - /// ```rust - /// let v: Vec = "abc åäö".chars().collect(); - /// assert_eq!(v, vec!['a', 'b', 'c', ' ', 'å', 'ä', 'ö']); - /// ``` fn chars<'a>(&'a self) -> Chars<'a>; - - /// An iterator over the bytes of `self` - /// - /// # Example - /// - /// ```rust - /// let v: Vec = "bors".bytes().collect(); - /// assert_eq!(v, b"bors".to_vec()); - /// ``` fn bytes<'a>(&'a self) -> Bytes<'a>; - - /// An iterator over the characters of `self` and their byte offsets. - fn char_indices<'a>(&'a self) -> CharOffsets<'a>; - - /// An iterator over substrings of `self`, separated by characters - /// matched by `sep`. - /// - /// # Example - /// - /// ```rust - /// # #![feature(unboxed_closures)] - /// - /// # fn main() { - /// let v: Vec<&str> = "Mary had a little lamb".split(' ').collect(); - /// assert_eq!(v, vec!["Mary", "had", "a", "little", "lamb"]); - /// - /// let v: Vec<&str> = "abc1def2ghi".split(|&: c: char| c.is_numeric()).collect(); - /// assert_eq!(v, vec!["abc", "def", "ghi"]); - /// - /// let v: Vec<&str> = "lionXXtigerXleopard".split('X').collect(); - /// assert_eq!(v, vec!["lion", "", "tiger", "leopard"]); - /// - /// let v: Vec<&str> = "".split('X').collect(); - /// assert_eq!(v, vec![""]); - /// # } - /// ``` + fn char_indices<'a>(&'a self) -> CharIndices<'a>; fn split<'a, Sep: CharEq>(&'a self, sep: Sep) -> CharSplits<'a, Sep>; - - /// An iterator over substrings of `self`, separated by characters - /// matched by `sep`, restricted to splitting at most `count` - /// times. - /// - /// # Example - /// - /// ```rust - /// # #![feature(unboxed_closures)] - /// - /// # fn main() { - /// let v: Vec<&str> = "Mary had a little lambda".splitn(2, ' ').collect(); - /// assert_eq!(v, vec!["Mary", "had", "a little lambda"]); - /// - /// let v: Vec<&str> = "abc1def2ghi".splitn(1, |&: c: char| c.is_numeric()).collect(); - /// assert_eq!(v, vec!["abc", "def2ghi"]); - /// - /// let v: Vec<&str> = "lionXXtigerXleopard".splitn(2, 'X').collect(); - /// assert_eq!(v, vec!["lion", "", "tigerXleopard"]); - /// - /// let v: Vec<&str> = "abcXdef".splitn(0, 'X').collect(); - /// assert_eq!(v, vec!["abcXdef"]); - /// - /// let v: Vec<&str> = "".splitn(1, 'X').collect(); - /// assert_eq!(v, vec![""]); - /// # } - /// ``` fn splitn<'a, Sep: CharEq>(&'a self, count: uint, sep: Sep) -> CharSplitsN<'a, Sep>; - - /// An iterator over substrings of `self`, separated by characters - /// matched by `sep`. - /// - /// Equivalent to `split`, except that the trailing substring - /// is skipped if empty (terminator semantics). - /// - /// # Example - /// - /// ```rust - /// # #![feature(unboxed_closures)] - /// - /// # fn main() { - /// let v: Vec<&str> = "A.B.".split_terminator('.').collect(); - /// assert_eq!(v, vec!["A", "B"]); - /// - /// let v: Vec<&str> = "A..B..".split_terminator('.').collect(); - /// assert_eq!(v, vec!["A", "", "B", ""]); - /// - /// let v: Vec<&str> = "Mary had a little lamb".split(' ').rev().collect(); - /// assert_eq!(v, vec!["lamb", "little", "a", "had", "Mary"]); - /// - /// let v: Vec<&str> = "abc1def2ghi".split(|&: c: char| c.is_numeric()).rev().collect(); - /// assert_eq!(v, vec!["ghi", "def", "abc"]); - /// - /// let v: Vec<&str> = "lionXXtigerXleopard".split('X').rev().collect(); - /// assert_eq!(v, vec!["leopard", "tiger", "", "lion"]); - /// # } - /// ``` fn split_terminator<'a, Sep: CharEq>(&'a self, sep: Sep) -> CharSplits<'a, Sep>; - - /// An iterator over substrings of `self`, separated by characters - /// matched by `sep`, starting from the end of the string. - /// Restricted to splitting at most `count` times. - /// - /// # Example - /// - /// ```rust - /// # #![feature(unboxed_closures)] - /// - /// # fn main() { - /// let v: Vec<&str> = "Mary had a little lamb".rsplitn(2, ' ').collect(); - /// assert_eq!(v, vec!["lamb", "little", "Mary had a"]); - /// - /// let v: Vec<&str> = "abc1def2ghi".rsplitn(1, |&: c: char| c.is_numeric()).collect(); - /// assert_eq!(v, vec!["ghi", "abc1def"]); - /// - /// let v: Vec<&str> = "lionXXtigerXleopard".rsplitn(2, 'X').collect(); - /// assert_eq!(v, vec!["leopard", "tiger", "lionX"]); - /// # } - /// ``` fn rsplitn<'a, Sep: CharEq>(&'a self, count: uint, sep: Sep) -> CharSplitsN<'a, Sep>; - - /// An iterator over the start and end indices of the disjoint - /// matches of `sep` within `self`. - /// - /// That is, each returned value `(start, end)` satisfies - /// `self.slice(start, end) == sep`. For matches of `sep` within - /// `self` that overlap, only the indices corresponding to the - /// first match are returned. - /// - /// # Example - /// - /// ```rust - /// let v: Vec<(uint, uint)> = "abcXXXabcYYYabc".match_indices("abc").collect(); - /// assert_eq!(v, vec![(0,3), (6,9), (12,15)]); - /// - /// let v: Vec<(uint, uint)> = "1abcabc2".match_indices("abc").collect(); - /// assert_eq!(v, vec![(1,4), (4,7)]); - /// - /// let v: Vec<(uint, uint)> = "ababa".match_indices("aba").collect(); - /// assert_eq!(v, vec![(0, 3)]); // only the first `aba` - /// ``` fn match_indices<'a>(&'a self, sep: &'a str) -> MatchIndices<'a>; - - /// An iterator over the substrings of `self` separated by `sep`. - /// - /// # Example - /// - /// ```rust - /// let v: Vec<&str> = "abcXXXabcYYYabc".split_str("abc").collect(); - /// assert_eq!(v, vec!["", "XXX", "YYY", ""]); - /// - /// let v: Vec<&str> = "1abcabc2".split_str("abc").collect(); - /// assert_eq!(v, vec!["1", "", "2"]); - /// ``` fn split_str<'a>(&'a self, &'a str) -> StrSplits<'a>; - - /// An iterator over the lines of a string (subsequences separated - /// by `\n`). This does not include the empty string after a - /// trailing `\n`. - /// - /// # Example - /// - /// ```rust - /// let four_lines = "foo\nbar\n\nbaz\n"; - /// let v: Vec<&str> = four_lines.lines().collect(); - /// assert_eq!(v, vec!["foo", "bar", "", "baz"]); - /// ``` - fn lines<'a>(&'a self) -> CharSplits<'a, char>; - - /// An iterator over the lines of a string, separated by either - /// `\n` or `\r\n`. As with `.lines()`, this does not include an - /// empty trailing line. - /// - /// # Example - /// - /// ```rust - /// let four_lines = "foo\r\nbar\n\r\nbaz\n"; - /// let v: Vec<&str> = four_lines.lines_any().collect(); - /// assert_eq!(v, vec!["foo", "bar", "", "baz"]); - /// ``` - fn lines_any<'a>(&'a self) -> AnyLines<'a>; - - /// Returns the number of Unicode code points (`char`) that a - /// string holds. - /// - /// This does not perform any normalization, and is `O(n)`, since - /// UTF-8 is a variable width encoding of code points. - /// - /// *Warning*: The number of code points in a string does not directly - /// correspond to the number of visible characters or width of the - /// visible text due to composing characters, and double- and - /// zero-width ones. - /// - /// See also `.len()` for the byte length. - /// - /// # Example - /// - /// ```rust - /// // composed forms of `ö` and `é` - /// let c = "Löwe 老虎 Léopard"; // German, Simplified Chinese, French - /// // decomposed forms of `ö` and `é` - /// let d = "Lo\u{0308}we 老虎 Le\u{0301}opard"; - /// - /// assert_eq!(c.char_len(), 15); - /// assert_eq!(d.char_len(), 17); - /// - /// assert_eq!(c.len(), 21); - /// assert_eq!(d.len(), 23); - /// - /// // the two strings *look* the same - /// println!("{}", c); - /// println!("{}", d); - /// ``` + fn lines<'a>(&'a self) -> Lines<'a>; + fn lines_any<'a>(&'a self) -> LinesAny<'a>; fn char_len(&self) -> uint; - - /// Returns a slice of the given string from the byte range - /// [`begin`..`end`). - /// - /// This operation is `O(1)`. - /// - /// Panics when `begin` and `end` do not point to valid characters - /// or point beyond the last character of the string. - /// - /// See also `slice_to` and `slice_from` for slicing prefixes and - /// suffixes of strings, and `slice_chars` for slicing based on - /// code point counts. - /// - /// # Example - /// - /// ```rust - /// let s = "Löwe 老虎 Léopard"; - /// assert_eq!(s.slice(0, 1), "L"); - /// - /// assert_eq!(s.slice(1, 9), "öwe 老"); - /// - /// // these will panic: - /// // byte 2 lies within `ö`: - /// // s.slice(2, 3); - /// - /// // byte 8 lies within `老` - /// // s.slice(1, 8); - /// - /// // byte 100 is outside the string - /// // s.slice(3, 100); - /// ``` fn slice<'a>(&'a self, begin: uint, end: uint) -> &'a str; - - /// Returns a slice of the string from `begin` to its end. - /// - /// Equivalent to `self.slice(begin, self.len())`. - /// - /// Panics when `begin` does not point to a valid character, or is - /// out of bounds. - /// - /// See also `slice`, `slice_to` and `slice_chars`. fn slice_from<'a>(&'a self, begin: uint) -> &'a str; - - /// Returns a slice of the string from the beginning to byte - /// `end`. - /// - /// Equivalent to `self.slice(0, end)`. - /// - /// Panics when `end` does not point to a valid character, or is - /// out of bounds. - /// - /// See also `slice`, `slice_from` and `slice_chars`. fn slice_to<'a>(&'a self, end: uint) -> &'a str; - - /// Returns a slice of the string from the character range - /// [`begin`..`end`). - /// - /// That is, start at the `begin`-th code point of the string and - /// continue to the `end`-th code point. This does not detect or - /// handle edge cases such as leaving a combining character as the - /// first code point of the string. - /// - /// Due to the design of UTF-8, this operation is `O(end)`. - /// See `slice`, `slice_to` and `slice_from` for `O(1)` - /// variants that use byte indices rather than code point - /// indices. - /// - /// Panics if `begin` > `end` or the either `begin` or `end` are - /// beyond the last character of the string. - /// - /// # Example - /// - /// ```rust - /// let s = "Löwe 老虎 Léopard"; - /// assert_eq!(s.slice_chars(0, 4), "Löwe"); - /// assert_eq!(s.slice_chars(5, 7), "老虎"); - /// ``` fn slice_chars<'a>(&'a self, begin: uint, end: uint) -> &'a str; - - /// Takes a bytewise (not UTF-8) slice from a string. - /// - /// Returns the substring from [`begin`..`end`). - /// - /// Caller must check both UTF-8 character boundaries and the boundaries of - /// the entire slice as well. unsafe fn slice_unchecked<'a>(&'a self, begin: uint, end: uint) -> &'a str; - - /// Returns true if `needle` is a prefix of the string. - /// - /// # Example - /// - /// ```rust - /// assert!("banana".starts_with("ba")); - /// ``` fn starts_with(&self, needle: &str) -> bool; - - /// Returns true if `needle` is a suffix of the string. - /// - /// # Example - /// - /// ```rust - /// assert!("banana".ends_with("nana")); - /// ``` fn ends_with(&self, needle: &str) -> bool; - - /// Returns a string with characters that match `to_trim` removed from the left and the right. - /// - /// # Arguments - /// - /// * to_trim - a character matcher - /// - /// # Example - /// - /// ```rust - /// # #![feature(unboxed_closures)] - /// - /// # fn main() { - /// assert_eq!("11foo1bar11".trim_chars('1'), "foo1bar"); - /// let x: &[_] = &['1', '2']; - /// assert_eq!("12foo1bar12".trim_chars(x), "foo1bar"); - /// assert_eq!("123foo1bar123".trim_chars(|&: c: char| c.is_numeric()), "foo1bar"); - /// # } - /// ``` fn trim_chars<'a, C: CharEq>(&'a self, to_trim: C) -> &'a str; - - /// Returns a string with leading `chars_to_trim` removed. - /// - /// # Arguments - /// - /// * to_trim - a character matcher - /// - /// # Example - /// - /// ```rust - /// # #![feature(unboxed_closures)] - /// - /// # fn main() { - /// assert_eq!("11foo1bar11".trim_left_chars('1'), "foo1bar11"); - /// let x: &[_] = &['1', '2']; - /// assert_eq!("12foo1bar12".trim_left_chars(x), "foo1bar12"); - /// assert_eq!("123foo1bar123".trim_left_chars(|&: c: char| c.is_numeric()), "foo1bar123"); - /// # } - /// ``` fn trim_left_chars<'a, C: CharEq>(&'a self, to_trim: C) -> &'a str; - - /// Returns a string with trailing `chars_to_trim` removed. - /// - /// # Arguments - /// - /// * to_trim - a character matcher - /// - /// # Example - /// - /// ```rust - /// # #![feature(unboxed_closures)] - /// - /// # fn main() { - /// assert_eq!("11foo1bar11".trim_right_chars('1'), "11foo1bar"); - /// let x: &[_] = &['1', '2']; - /// assert_eq!("12foo1bar12".trim_right_chars(x), "12foo1bar"); - /// assert_eq!("123foo1bar123".trim_right_chars(|&: c: char| c.is_numeric()), "123foo1bar"); - /// # } - /// ``` fn trim_right_chars<'a, C: CharEq>(&'a self, to_trim: C) -> &'a str; - - /// Check that `index`-th byte lies at the start and/or end of a - /// UTF-8 code point sequence. - /// - /// The start and end of the string (when `index == self.len()`) - /// are considered to be boundaries. - /// - /// Panics if `index` is greater than `self.len()`. - /// - /// # Example - /// - /// ```rust - /// let s = "Löwe 老虎 Léopard"; - /// assert!(s.is_char_boundary(0)); - /// // start of `老` - /// assert!(s.is_char_boundary(6)); - /// assert!(s.is_char_boundary(s.len())); - /// - /// // second byte of `ö` - /// assert!(!s.is_char_boundary(2)); - /// - /// // third byte of `老` - /// assert!(!s.is_char_boundary(8)); - /// ``` fn is_char_boundary(&self, index: uint) -> bool; - - /// Pluck a character out of a string and return the index of the next - /// character. - /// - /// This function can be used to iterate over the Unicode characters of a - /// string. - /// - /// # Example - /// - /// This example manually iterates through the characters of a - /// string; this should normally be done by `.chars()` or - /// `.char_indices`. - /// - /// ```rust - /// use std::str::CharRange; - /// - /// let s = "中华Việt Nam"; - /// let mut i = 0u; - /// while i < s.len() { - /// let CharRange {ch, next} = s.char_range_at(i); - /// println!("{}: {}", i, ch); - /// i = next; - /// } - /// ``` - /// - /// This outputs: - /// - /// ```text - /// 0: 中 - /// 3: 华 - /// 6: V - /// 7: i - /// 8: ệ - /// 11: t - /// 12: - /// 13: N - /// 14: a - /// 15: m - /// ``` - /// - /// # Arguments - /// - /// * s - The string - /// * i - The byte offset of the char to extract - /// - /// # Return value - /// - /// A record {ch: char, next: uint} containing the char value and the byte - /// index of the next Unicode character. - /// - /// # Panics - /// - /// If `i` is greater than or equal to the length of the string. - /// If `i` is not the index of the beginning of a valid UTF-8 character. fn char_range_at(&self, start: uint) -> CharRange; - - /// Given a byte position and a str, return the previous char and its position. - /// - /// This function can be used to iterate over a Unicode string in reverse. - /// - /// Returns 0 for next index if called on start index 0. - /// - /// # Panics - /// - /// If `i` is greater than the length of the string. - /// If `i` is not an index following a valid UTF-8 character. fn char_range_at_reverse(&self, start: uint) -> CharRange; - - /// Plucks the character starting at the `i`th byte of a string. - /// - /// # Example - /// - /// ```rust - /// let s = "abπc"; - /// assert_eq!(s.char_at(1), 'b'); - /// assert_eq!(s.char_at(2), 'π'); - /// assert_eq!(s.char_at(4), 'c'); - /// ``` - /// - /// # Panics - /// - /// If `i` is greater than or equal to the length of the string. - /// If `i` is not the index of the beginning of a valid UTF-8 character. fn char_at(&self, i: uint) -> char; - - /// Plucks the character ending at the `i`th byte of a string. - /// - /// # Panics - /// - /// If `i` is greater than the length of the string. - /// If `i` is not an index following a valid UTF-8 character. fn char_at_reverse(&self, i: uint) -> char; - - /// Work with the byte buffer of a string as a byte slice. - /// - /// # Example - /// - /// ```rust - /// assert_eq!("bors".as_bytes(), b"bors"); - /// ``` fn as_bytes<'a>(&'a self) -> &'a [u8]; - - /// Returns the byte index of the first character of `self` that - /// matches `search`. - /// - /// # Return value - /// - /// `Some` containing the byte index of the last matching character - /// or `None` if there is no match - /// - /// # Example - /// - /// ```rust - /// # #![feature(unboxed_closures)] - /// - /// # fn main() { - /// let s = "Löwe 老虎 Léopard"; - /// - /// assert_eq!(s.find('L'), Some(0)); - /// assert_eq!(s.find('é'), Some(14)); - /// - /// // the first space - /// assert_eq!(s.find(|&: c: char| c.is_whitespace()), Some(5)); - /// - /// // neither are found - /// let x: &[_] = &['1', '2']; - /// assert_eq!(s.find(x), None); - /// # } - /// ``` fn find(&self, search: C) -> Option; - - /// Returns the byte index of the last character of `self` that - /// matches `search`. - /// - /// # Return value - /// - /// `Some` containing the byte index of the last matching character - /// or `None` if there is no match. - /// - /// # Example - /// - /// ```rust - /// # #![feature(unboxed_closures)] - /// - /// # fn main() { - /// let s = "Löwe 老虎 Léopard"; - /// - /// assert_eq!(s.rfind('L'), Some(13)); - /// assert_eq!(s.rfind('é'), Some(14)); - /// - /// // the second space - /// assert_eq!(s.rfind(|&: c: char| c.is_whitespace()), Some(12)); - /// - /// // searches for an occurrence of either `1` or `2`, but neither are found - /// let x: &[_] = &['1', '2']; - /// assert_eq!(s.rfind(x), None); - /// # } - /// ``` fn rfind(&self, search: C) -> Option; - - /// Returns the byte index of the first matching substring - /// - /// # Arguments - /// - /// * `needle` - The string to search for - /// - /// # Return value - /// - /// `Some` containing the byte index of the first matching substring - /// or `None` if there is no match. - /// - /// # Example - /// - /// ```rust - /// let s = "Löwe 老虎 Léopard"; - /// - /// assert_eq!(s.find_str("老虎 L"), Some(6)); - /// assert_eq!(s.find_str("muffin man"), None); - /// ``` fn find_str(&self, &str) -> Option; - - /// Retrieves the first character from a string slice and returns - /// it. This does not allocate a new string; instead, it returns a - /// slice that point one character beyond the character that was - /// shifted. If the string does not contain any characters, - /// None is returned instead. - /// - /// # Example - /// - /// ```rust - /// let s = "Löwe 老虎 Léopard"; - /// let (c, s1) = s.slice_shift_char().unwrap(); - /// assert_eq!(c, 'L'); - /// assert_eq!(s1, "öwe 老虎 Léopard"); - /// - /// let (c, s2) = s1.slice_shift_char().unwrap(); - /// assert_eq!(c, 'ö'); - /// assert_eq!(s2, "we 老虎 Léopard"); - /// ``` fn slice_shift_char<'a>(&'a self) -> Option<(char, &'a str)>; - - /// Returns the byte offset of an inner slice relative to an enclosing outer slice. - /// - /// Panics if `inner` is not a direct slice contained within self. - /// - /// # Example - /// - /// ```rust - /// let string = "a\nb\nc"; - /// let lines: Vec<&str> = string.lines().collect(); - /// - /// assert!(string.subslice_offset(lines[0]) == 0); // &"a" - /// assert!(string.subslice_offset(lines[1]) == 2); // &"b" - /// assert!(string.subslice_offset(lines[2]) == 4); // &"c" - /// ``` fn subslice_offset(&self, inner: &str) -> uint; - - /// Return an unsafe pointer to the strings buffer. - /// - /// The caller must ensure that the string outlives this pointer, - /// and that it is not reallocated (e.g. by pushing to the - /// string). fn as_ptr(&self) -> *const u8; - - /// Return an iterator of `u16` over the string encoded as UTF-16. - fn utf16_units<'a>(&'a self) -> Utf16CodeUnits<'a>; - - /// Return the number of bytes in this string - /// - /// # Example - /// - /// ``` - /// assert_eq!("foo".len(), 3); - /// assert_eq!("ƒoo".len(), 4); - /// ``` - #[experimental = "not triaged yet"] fn len(&self) -> uint; - - /// Returns true if this slice contains no bytes - /// - /// # Example - /// - /// ``` - /// assert!("".is_empty()); - /// ``` - #[inline] - #[experimental = "not triaged yet"] - fn is_empty(&self) -> bool { self.len() == 0 } + fn is_empty(&self) -> bool; } #[inline(never)] @@ -2001,7 +1227,7 @@ fn slice_error_fail(s: &str, begin: uint, end: uint) -> ! { begin, end, s); } -impl StrPrelude for str { +impl StrExt for str { #[inline] fn contains(&self, needle: &str) -> bool { self.find_str(needle).is_some() @@ -2021,12 +1247,12 @@ fn chars(&self) -> Chars { fn bytes(&self) -> Bytes { fn deref(&x: &u8) -> u8 { x } - self.as_bytes().iter().map(BytesFn(deref)) + Bytes { inner: self.as_bytes().iter().map(BytesFn(deref)) } } #[inline] - fn char_indices(&self) -> CharOffsets { - CharOffsets{front_offset: 0, iter: self.chars()} + fn char_indices(&self) -> CharIndices { + CharIndices { front_offset: 0, iter: self.chars() } } #[inline] @@ -2089,18 +1315,18 @@ fn split_str<'a>(&'a self, sep: &'a str) -> StrSplits<'a> { } #[inline] - fn lines(&self) -> CharSplits { - self.split_terminator('\n') + fn lines(&self) -> Lines { + Lines { inner: self.split_terminator('\n') } } - fn lines_any(&self) -> AnyLines { + fn lines_any(&self) -> LinesAny { fn f(line: &str) -> &str { let l = line.len(); if l > 0 && line.as_bytes()[l - 1] == b'\r' { line.slice(0, l - 1) } else { line } } - self.lines().map(f) + LinesAny { inner: self.lines().map(f) } } #[inline] @@ -2353,12 +1579,10 @@ fn as_ptr(&self) -> *const u8 { } #[inline] - fn utf16_units(&self) -> Utf16CodeUnits { - Utf16CodeUnits { encoder: Utf16Encoder::new(self.chars()) } - } + fn len(&self) -> uint { self.repr().len } #[inline] - fn len(&self) -> uint { self.repr().len } + fn is_empty(&self) -> bool { self.len() == 0 } } #[stable] @@ -2367,3 +1591,29 @@ impl<'a> Default for &'a str { fn default() -> &'a str { "" } } + +impl<'a> Iterator<&'a str> for Lines<'a> { + #[inline] + fn next(&mut self) -> Option<&'a str> { self.inner.next() } +} +impl<'a> DoubleEndedIterator<&'a str> for Lines<'a> { + #[inline] + fn next_back(&mut self) -> Option<&'a str> { self.inner.next_back() } +} +impl<'a> Iterator<&'a str> for LinesAny<'a> { + #[inline] + fn next(&mut self) -> Option<&'a str> { self.inner.next() } +} +impl<'a> DoubleEndedIterator<&'a str> for LinesAny<'a> { + #[inline] + fn next_back(&mut self) -> Option<&'a str> { self.inner.next_back() } +} +impl<'a> Iterator for Bytes<'a> { + #[inline] + fn next(&mut self) -> Option { self.inner.next() } +} +impl<'a> DoubleEndedIterator for Bytes<'a> { + #[inline] + fn next_back(&mut self) -> Option { self.inner.next_back() } +} +impl<'a> ExactSizeIterator for Bytes<'a> {} diff --git a/src/librustc/lib.rs b/src/librustc/lib.rs index 463dcddaf94f..4a1dd121516d 100644 --- a/src/librustc/lib.rs +++ b/src/librustc/lib.rs @@ -115,6 +115,7 @@ pub mod util { pub mod ppaux; pub mod nodemap; pub mod snapshot_vec; + pub mod lev_distance; } pub mod lib { diff --git a/src/librustc/util/lev_distance.rs b/src/librustc/util/lev_distance.rs new file mode 100644 index 000000000000..24e988374440 --- /dev/null +++ b/src/librustc/util/lev_distance.rs @@ -0,0 +1,63 @@ +// Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +use std::cmp; + +pub fn lev_distance(me: &str, t: &str) -> uint { + if me.is_empty() { return t.chars().count(); } + if t.is_empty() { return me.chars().count(); } + + let mut dcol = Vec::from_fn(t.len() + 1, |x| x); + let mut t_last = 0; + + for (i, sc) in me.chars().enumerate() { + + let mut current = i; + dcol[0] = current + 1; + + for (j, tc) in t.chars().enumerate() { + + let next = dcol[j + 1]; + + if sc == tc { + dcol[j + 1] = current; + } else { + dcol[j + 1] = cmp::min(current, next); + dcol[j + 1] = cmp::min(dcol[j + 1], dcol[j]) + 1; + } + + current = next; + t_last = j; + } + } + + dcol[t_last + 1] +} + +#[test] +fn test_lev_distance() { + use std::char::{ from_u32, MAX }; + // Test bytelength agnosticity + for c in range(0u32, MAX as u32) + .filter_map(|i| from_u32(i)) + .map(|i| String::from_char(1, i)) { + assert_eq!(lev_distance(c[], c[]), 0); + } + + let a = "\nMäry häd ä little lämb\n\nLittle lämb\n"; + let b = "\nMary häd ä little lämb\n\nLittle lämb\n"; + let c = "Mary häd ä little lämb\n\nLittle lämb\n"; + assert_eq!(lev_distance(a, b), 1); + assert_eq!(lev_distance(b, a), 1); + assert_eq!(lev_distance(a, c), 2); + assert_eq!(lev_distance(c, a), 2); + assert_eq!(lev_distance(b, c), 1); + assert_eq!(lev_distance(c, b), 1); +} diff --git a/src/librustc_resolve/lib.rs b/src/librustc_resolve/lib.rs index ac8d5d1e977b..d4a0b49436dd 100644 --- a/src/librustc_resolve/lib.rs +++ b/src/librustc_resolve/lib.rs @@ -57,6 +57,7 @@ use rustc::middle::subst::{ParamSpace, FnSpace, TypeSpace}; use rustc::middle::ty::{CaptureModeMap, Freevar, FreevarMap, TraitMap}; use rustc::util::nodemap::{NodeMap, NodeSet, DefIdSet, FnvHashMap}; +use rustc::util::lev_distance::lev_distance; use syntax::ast::{Arm, BindByRef, BindByValue, BindingMode, Block, Crate, CrateNum}; use syntax::ast::{DeclItem, DefId, Expr, ExprAgain, ExprBreak, ExprField}; @@ -96,8 +97,8 @@ use std::rc::{Rc, Weak}; use std::uint; -mod check_unused; -mod record_exports; +// Definition mapping +pub type DefMap = RefCell>; #[deriving(Copy)] struct BindingInfo { @@ -5539,7 +5540,7 @@ fn find_best_match_for_name(&mut self, name: &str, max_distance: uint) let mut smallest = 0; for (i, other) in maybes.iter().enumerate() { - values[i] = name.lev_distance(other.get()); + values[i] = lev_distance(name, other.get()); if values[i] <= values[smallest] { smallest = i; diff --git a/src/libstd/error.rs b/src/libstd/error.rs index 9ad2655f6e9d..cd7d9aacc901 100644 --- a/src/libstd/error.rs +++ b/src/libstd/error.rs @@ -78,10 +78,9 @@ //! } //! ``` -use option::Option; -use option::Option::None; -use kinds::Send; -use string::String; +use prelude::*; + +use str::Utf8Error; /// Base functionality for all errors in Rust. pub trait Error: Send { @@ -107,3 +106,14 @@ fn from_error(err: E) -> E { err } } + +impl Error for Utf8Error { + fn description(&self) -> &str { + match *self { + Utf8Error::TooShort => "invalid utf-8: not enough bytes", + Utf8Error::InvalidByte(..) => "invalid utf-8: corrupt contents", + } + } + + fn detail(&self) -> Option { Some(self.to_string()) } +} diff --git a/src/libstd/os.rs b/src/libstd/os.rs index 258e8964a9fd..a16ee982f5c0 100644 --- a/src/libstd/os.rs +++ b/src/libstd/os.rs @@ -729,7 +729,7 @@ fn real_args() -> Vec { // Push it onto the list. let ptr = ptr as *const u16; let buf = slice::from_raw_buf(&ptr, len); - let opt_s = String::from_utf16(::str::truncate_utf16_at_nul(buf)); + let opt_s = String::from_utf16(os_imp::truncate_utf16_at_nul(buf)); opt_s.expect("CommandLineToArgvW returned invalid UTF-16") }); diff --git a/src/libstd/sys/windows/os.rs b/src/libstd/sys/windows/os.rs index e2220b7b67bb..e1016048e58c 100644 --- a/src/libstd/sys/windows/os.rs +++ b/src/libstd/sys/windows/os.rs @@ -31,6 +31,16 @@ const BUF_BYTES : uint = 2048u; +/// Return a slice of `v` ending at (and not including) the first NUL +/// (0). +pub fn truncate_utf16_at_nul<'a>(v: &'a [u16]) -> &'a [u16] { + match v.iter().position(|c| *c == 0) { + // don't include the 0 + Some(i) => v[..i], + None => v + } +} + pub fn errno() -> uint { use libc::types::os::arch::extra::DWORD; @@ -87,7 +97,7 @@ fn FormatMessageW(flags: DWORD, return format!("OS Error {} (FormatMessageW() returned error {})", errnum, fm_err); } - let msg = String::from_utf16(::str::truncate_utf16_at_nul(&buf)); + let msg = String::from_utf16(truncate_utf16_at_nul(&buf)); match msg { Some(msg) => format!("OS Error {}: {}", errnum, msg), None => format!("OS Error {} (FormatMessageW() returned invalid UTF-16)", errnum), @@ -294,3 +304,30 @@ pub fn page_size() -> uint { return info.dwPageSize as uint; } } + +#[cfg(test)] +mod tests { + use super::truncate_utf16_at_nul; + + #[test] + fn test_truncate_utf16_at_nul() { + let v = []; + let b: &[u16] = &[]; + assert_eq!(truncate_utf16_at_nul(&v), b); + + let v = [0, 2, 3]; + assert_eq!(truncate_utf16_at_nul(&v), b); + + let v = [1, 0, 3]; + let b: &[u16] = &[1]; + assert_eq!(truncate_utf16_at_nul(&v), b); + + let v = [1, 2, 0]; + let b: &[u16] = &[1, 2]; + assert_eq!(truncate_utf16_at_nul(&v), b); + + let v = [1, 2, 3]; + let b: &[u16] = &[1, 2, 3]; + assert_eq!(truncate_utf16_at_nul(&v), b); + } +} diff --git a/src/libunicode/lib.rs b/src/libunicode/lib.rs index 1f75daa7bdec..d33362ec2329 100644 --- a/src/libunicode/lib.rs +++ b/src/libunicode/lib.rs @@ -28,8 +28,7 @@ html_root_url = "http://doc.rust-lang.org/nightly/", html_playground_url = "http://play.rust-lang.org/")] #![no_std] -#![feature(globs)] -#![feature(unboxed_closures)] +#![feature(globs, macro_rules, slicing_syntax, unboxed_closures)] extern crate core; @@ -74,11 +73,14 @@ pub mod char { } pub mod str { - pub use u_str::{UnicodeStrPrelude, Words, Graphemes, GraphemeIndices}; + pub use u_str::{UnicodeStr, Words, Graphemes, GraphemeIndices}; + pub use u_str::{utf8_char_width, is_utf16, Utf16Items, Utf16Item}; + pub use u_str::{utf16_items, Utf16Encoder}; } -// this lets us use #[deriving(Clone)] +// this lets us use #[deriving(..)] mod std { pub use core::clone; pub use core::cmp; + pub use core::fmt; } diff --git a/src/libunicode/u_str.rs b/src/libunicode/u_str.rs index 5e98109c432a..5d7d29516282 100644 --- a/src/libunicode/u_str.rs +++ b/src/libunicode/u_str.rs @@ -15,24 +15,36 @@ //! This module provides functionality to `str` that requires the Unicode methods provided by the //! UnicodeChar trait. -use self::GraphemeState::*; +use core::prelude::*; + +use core::char; use core::cmp; -use core::slice::SliceExt; -use core::iter::{Filter, AdditiveIterator, Iterator, IteratorExt}; use core::iter::{DoubleEndedIterator, DoubleEndedIteratorExt}; +use core::iter::{Filter, AdditiveIterator, Iterator, IteratorExt}; +use core::iter::{Filter, AdditiveIterator}; use core::kinds::Sized; -use core::option::Option; +use core::mem; +use core::num::Int; use core::option::Option::{None, Some}; +use core::option::Option; +use core::slice::SliceExt; +use core::slice; use core::str::{CharSplits, StrPrelude}; +use core::str::{CharSplits}; + use u_char::UnicodeChar; use tables::grapheme::GraphemeCat; /// An iterator over the words of a string, separated by a sequence of whitespace /// FIXME: This should be opaque -pub type Words<'a> = Filter<&'a str, CharSplits<'a, fn(char) -> bool>, fn(&&str) -> bool>; +#[stable] +pub struct Words<'a> { + inner: Filter<'a, &'a str, CharSplits<'a, |char|:'a -> bool>, + fn(&&str) -> bool>, +} /// Methods for Unicode string slices -pub trait UnicodeStrPrelude for Sized? { +pub trait UnicodeStr for Sized? { /// Returns an iterator over the /// [grapheme clusters](http://www.unicode.org/reports/tr29/#Grapheme_Cluster_Boundaries) /// of the string. @@ -77,6 +89,7 @@ pub trait UnicodeStrPrelude for Sized? { /// let v: Vec<&str> = some_words.words().collect(); /// assert_eq!(v, vec!["Mary", "had", "a", "little", "lamb"]); /// ``` + #[stable] fn words<'a>(&'a self) -> Words<'a>; /// Returns true if the string contains only whitespace. @@ -129,7 +142,7 @@ pub trait UnicodeStrPrelude for Sized? { fn trim_right<'a>(&'a self) -> &'a str; } -impl UnicodeStrPrelude for str { +impl UnicodeStr for str { #[inline] fn graphemes(&self, is_extended: bool) -> Graphemes { Graphemes { string: self, extended: is_extended, cat: None, catb: None } @@ -145,7 +158,7 @@ fn words(&self) -> Words { fn is_not_empty(s: &&str) -> bool { !s.is_empty() } fn is_whitespace(c: char) -> bool { c.is_whitespace() } - self.split(is_whitespace).filter(is_not_empty) + Words { inner: self.split(is_whitespace).filter(is_not_empty) } } #[inline] @@ -428,3 +441,196 @@ fn next_back(&mut self) -> Option<&'a str> { Some(retstr) } } + +// https://tools.ietf.org/html/rfc3629 +static UTF8_CHAR_WIDTH: [u8, ..256] = [ +1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, +1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // 0x1F +1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, +1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // 0x3F +1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, +1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // 0x5F +1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, +1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, // 0x7F +0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, +0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 0x9F +0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, +0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 0xBF +0,0,2,2,2,2,2,2,2,2,2,2,2,2,2,2, +2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2, // 0xDF +3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3, // 0xEF +4,4,4,4,4,0,0,0,0,0,0,0,0,0,0,0, // 0xFF +]; + +/// Given a first byte, determine how many bytes are in this UTF-8 character +#[inline] +pub fn utf8_char_width(b: u8) -> uint { + return UTF8_CHAR_WIDTH[b as uint] as uint; +} + +/// Determines if a vector of `u16` contains valid UTF-16 +pub fn is_utf16(v: &[u16]) -> bool { + let mut it = v.iter(); + macro_rules! next ( ($ret:expr) => { + match it.next() { Some(u) => *u, None => return $ret } + } + ) + loop { + let u = next!(true); + + match char::from_u32(u as u32) { + Some(_) => {} + None => { + let u2 = next!(false); + if u < 0xD7FF || u > 0xDBFF || + u2 < 0xDC00 || u2 > 0xDFFF { return false; } + } + } + } +} + +/// An iterator that decodes UTF-16 encoded codepoints from a vector +/// of `u16`s. +#[deriving(Clone)] +pub struct Utf16Items<'a> { + iter: slice::Items<'a, u16> +} +/// The possibilities for values decoded from a `u16` stream. +#[deriving(PartialEq, Eq, Clone, Show)] +pub enum Utf16Item { + /// A valid codepoint. + ScalarValue(char), + /// An invalid surrogate without its pair. + LoneSurrogate(u16) +} + +impl Copy for Utf16Item {} + +impl Utf16Item { + /// Convert `self` to a `char`, taking `LoneSurrogate`s to the + /// replacement character (U+FFFD). + #[inline] + pub fn to_char_lossy(&self) -> char { + match *self { + Utf16Item::ScalarValue(c) => c, + Utf16Item::LoneSurrogate(_) => '\uFFFD' + } + } +} + +impl<'a> Iterator for Utf16Items<'a> { + fn next(&mut self) -> Option { + let u = match self.iter.next() { + Some(u) => *u, + None => return None + }; + + if u < 0xD800 || 0xDFFF < u { + // not a surrogate + Some(Utf16Item::ScalarValue(unsafe {mem::transmute(u as u32)})) + } else if u >= 0xDC00 { + // a trailing surrogate + Some(Utf16Item::LoneSurrogate(u)) + } else { + // preserve state for rewinding. + let old = self.iter; + + let u2 = match self.iter.next() { + Some(u2) => *u2, + // eof + None => return Some(Utf16Item::LoneSurrogate(u)) + }; + if u2 < 0xDC00 || u2 > 0xDFFF { + // not a trailing surrogate so we're not a valid + // surrogate pair, so rewind to redecode u2 next time. + self.iter = old; + return Some(Utf16Item::LoneSurrogate(u)) + } + + // all ok, so lets decode it. + let c = ((u - 0xD800) as u32 << 10 | (u2 - 0xDC00) as u32) + 0x1_0000; + Some(Utf16Item::ScalarValue(unsafe {mem::transmute(c)})) + } + } + + #[inline] + fn size_hint(&self) -> (uint, Option) { + let (low, high) = self.iter.size_hint(); + // we could be entirely valid surrogates (2 elements per + // char), or entirely non-surrogates (1 element per char) + (low / 2, high) + } +} + +/// Create an iterator over the UTF-16 encoded codepoints in `v`, +/// returning invalid surrogates as `LoneSurrogate`s. +/// +/// # Example +/// +/// ```rust +/// use std::str; +/// use std::str::{ScalarValue, LoneSurrogate}; +/// +/// // 𝄞music +/// let v = [0xD834, 0xDD1E, 0x006d, 0x0075, +/// 0x0073, 0xDD1E, 0x0069, 0x0063, +/// 0xD834]; +/// +/// assert_eq!(str::utf16_items(&v).collect::>(), +/// vec![ScalarValue('𝄞'), +/// ScalarValue('m'), ScalarValue('u'), ScalarValue('s'), +/// LoneSurrogate(0xDD1E), +/// ScalarValue('i'), ScalarValue('c'), +/// LoneSurrogate(0xD834)]); +/// ``` +pub fn utf16_items<'a>(v: &'a [u16]) -> Utf16Items<'a> { + Utf16Items { iter : v.iter() } +} + +/// Iterator adaptor for encoding `char`s to UTF-16. +#[deriving(Clone)] +pub struct Utf16Encoder { + chars: I, + extra: u16 +} + +impl Utf16Encoder { + /// Create an UTF-16 encoder from any `char` iterator. + pub fn new(chars: I) -> Utf16Encoder where I: Iterator { + Utf16Encoder { chars: chars, extra: 0 } + } +} + +impl Iterator for Utf16Encoder where I: Iterator { + #[inline] + fn next(&mut self) -> Option { + if self.extra != 0 { + let tmp = self.extra; + self.extra = 0; + return Some(tmp); + } + + let mut buf = [0u16, ..2]; + self.chars.next().map(|ch| { + let n = ch.encode_utf16(buf[mut]).unwrap_or(0); + if n == 2 { self.extra = buf[1]; } + buf[0] + }) + } + + #[inline] + fn size_hint(&self) -> (uint, Option) { + let (low, high) = self.chars.size_hint(); + // every char gets either one u16 or two u16, + // so this iterator is between 1 or 2 times as + // long as the underlying iterator. + (low, high.and_then(|n| n.checked_mul(2))) + } +} + +impl<'a> Iterator<&'a str> for Words<'a> { + fn next(&mut self) -> Option<&'a str> { self.inner.next() } +} +impl<'a> DoubleEndedIterator<&'a str> for Words<'a> { + fn next_back(&mut self) -> Option<&'a str> { self.inner.next_back() } +} From abf492d44f0a3b705be8c0920bfb4771f039b843 Mon Sep 17 00:00:00 2001 From: Chase Southwood Date: Fri, 19 Dec 2014 17:53:40 -0600 Subject: [PATCH 10/44] Misc Stabilization for collections This commit: *Renames `BinaryHeap::top` to `BinaryHeap::peek` *Stabilizes `front/back/front_mut/back_mut` in `DList` and `RingBuf` *Stabilizes `swap` in `RingBuf` Because of the method renaming, this is a [breaking-change]. --- src/libcollections/binary_heap.rs | 45 ++++++++++++++++--------------- src/libcollections/dlist.rs | 8 +++--- src/libcollections/ring_buf.rs | 9 ++++--- 3 files changed, 32 insertions(+), 30 deletions(-) diff --git a/src/libcollections/binary_heap.rs b/src/libcollections/binary_heap.rs index 0840e8ec881c..051001cf3c6b 100644 --- a/src/libcollections/binary_heap.rs +++ b/src/libcollections/binary_heap.rs @@ -272,15 +272,16 @@ pub fn into_iter(self) -> MoveItems { /// use std::collections::BinaryHeap; /// /// let mut heap = BinaryHeap::new(); - /// assert_eq!(heap.top(), None); + /// assert_eq!(heap.peek(), None); /// /// heap.push(1i); /// heap.push(5i); /// heap.push(2i); - /// assert_eq!(heap.top(), Some(&5i)); + /// assert_eq!(heap.peek(), Some(&5i)); /// /// ``` - pub fn top(&self) -> Option<&T> { + #[stable] + pub fn peek(&self) -> Option<&T> { self.data.get(0) } @@ -388,7 +389,7 @@ pub fn pop(&mut self) -> Option { /// heap.push(1i); /// /// assert_eq!(heap.len(), 3); - /// assert_eq!(heap.top(), Some(&5i)); + /// assert_eq!(heap.peek(), Some(&5i)); /// ``` #[unstable = "matches collection reform specification, waiting for dust to settle"] pub fn push(&mut self, item: T) { @@ -412,7 +413,7 @@ pub fn push(&mut self, item: T) { /// assert_eq!(heap.push_pop(3i), 5); /// assert_eq!(heap.push_pop(9i), 9); /// assert_eq!(heap.len(), 2); - /// assert_eq!(heap.top(), Some(&3i)); + /// assert_eq!(heap.peek(), Some(&3i)); /// ``` pub fn push_pop(&mut self, mut item: T) -> T { match self.data.get_mut(0) { @@ -442,7 +443,7 @@ pub fn push_pop(&mut self, mut item: T) -> T { /// assert_eq!(heap.replace(1i), None); /// assert_eq!(heap.replace(3i), Some(1i)); /// assert_eq!(heap.len(), 1); - /// assert_eq!(heap.top(), Some(&3i)); + /// assert_eq!(heap.peek(), Some(&3i)); /// ``` pub fn replace(&mut self, mut item: T) -> Option { if !self.is_empty() { @@ -714,13 +715,13 @@ fn test_move_iter_reverse() { } #[test] - fn test_top_and_pop() { + fn test_peek_and_pop() { let data = vec!(2u, 4, 6, 2, 1, 8, 10, 3, 5, 7, 0, 9, 1); let mut sorted = data.clone(); sorted.sort(); let mut heap = BinaryHeap::from_vec(data); while !heap.is_empty() { - assert_eq!(heap.top().unwrap(), sorted.last().unwrap()); + assert_eq!(heap.peek().unwrap(), sorted.last().unwrap()); assert_eq!(heap.pop().unwrap(), sorted.pop().unwrap()); } } @@ -729,44 +730,44 @@ fn test_top_and_pop() { fn test_push() { let mut heap = BinaryHeap::from_vec(vec!(2i, 4, 9)); assert_eq!(heap.len(), 3); - assert!(*heap.top().unwrap() == 9); + assert!(*heap.peek().unwrap() == 9); heap.push(11); assert_eq!(heap.len(), 4); - assert!(*heap.top().unwrap() == 11); + assert!(*heap.peek().unwrap() == 11); heap.push(5); assert_eq!(heap.len(), 5); - assert!(*heap.top().unwrap() == 11); + assert!(*heap.peek().unwrap() == 11); heap.push(27); assert_eq!(heap.len(), 6); - assert!(*heap.top().unwrap() == 27); + assert!(*heap.peek().unwrap() == 27); heap.push(3); assert_eq!(heap.len(), 7); - assert!(*heap.top().unwrap() == 27); + assert!(*heap.peek().unwrap() == 27); heap.push(103); assert_eq!(heap.len(), 8); - assert!(*heap.top().unwrap() == 103); + assert!(*heap.peek().unwrap() == 103); } #[test] fn test_push_unique() { let mut heap = BinaryHeap::from_vec(vec!(box 2i, box 4, box 9)); assert_eq!(heap.len(), 3); - assert!(*heap.top().unwrap() == box 9); + assert!(*heap.peek().unwrap() == box 9); heap.push(box 11); assert_eq!(heap.len(), 4); - assert!(*heap.top().unwrap() == box 11); + assert!(*heap.peek().unwrap() == box 11); heap.push(box 5); assert_eq!(heap.len(), 5); - assert!(*heap.top().unwrap() == box 11); + assert!(*heap.peek().unwrap() == box 11); heap.push(box 27); assert_eq!(heap.len(), 6); - assert!(*heap.top().unwrap() == box 27); + assert!(*heap.peek().unwrap() == box 27); heap.push(box 3); assert_eq!(heap.len(), 7); - assert!(*heap.top().unwrap() == box 27); + assert!(*heap.peek().unwrap() == box 27); heap.push(box 103); assert_eq!(heap.len(), 8); - assert!(*heap.top().unwrap() == box 103); + assert!(*heap.peek().unwrap() == box 103); } #[test] @@ -831,9 +832,9 @@ fn test_empty_pop() { } #[test] - fn test_empty_top() { + fn test_empty_peek() { let empty = BinaryHeap::::new(); - assert!(empty.top().is_none()); + assert!(empty.peek().is_none()); } #[test] diff --git a/src/libcollections/dlist.rs b/src/libcollections/dlist.rs index d3c1a0f81a33..d5e667687265 100644 --- a/src/libcollections/dlist.rs +++ b/src/libcollections/dlist.rs @@ -451,7 +451,7 @@ pub fn clear(&mut self) { /// Provides a reference to the front element, or `None` if the list is /// empty. #[inline] - #[unstable = "matches collection reform specification, waiting for dust to settle"] + #[stable] pub fn front(&self) -> Option<&T> { self.list_head.as_ref().map(|head| &head.value) } @@ -459,7 +459,7 @@ pub fn front(&self) -> Option<&T> { /// Provides a mutable reference to the front element, or `None` if the list /// is empty. #[inline] - #[unstable = "matches collection reform specification, waiting for dust to settle"] + #[stable] pub fn front_mut(&mut self) -> Option<&mut T> { self.list_head.as_mut().map(|head| &mut head.value) } @@ -467,7 +467,7 @@ pub fn front_mut(&mut self) -> Option<&mut T> { /// Provides a reference to the back element, or `None` if the list is /// empty. #[inline] - #[unstable = "matches collection reform specification, waiting for dust to settle"] + #[stable] pub fn back(&self) -> Option<&T> { self.list_tail.resolve_immut().as_ref().map(|tail| &tail.value) } @@ -475,7 +475,7 @@ pub fn back(&self) -> Option<&T> { /// Provides a mutable reference to the back element, or `None` if the list /// is empty. #[inline] - #[unstable = "matches collection reform specification, waiting for dust to settle"] + #[stable] pub fn back_mut(&mut self) -> Option<&mut T> { self.list_tail.resolve().map(|tail| &mut tail.value) } diff --git a/src/libcollections/ring_buf.rs b/src/libcollections/ring_buf.rs index aa0e33248fcc..29fb863b6bea 100644 --- a/src/libcollections/ring_buf.rs +++ b/src/libcollections/ring_buf.rs @@ -228,6 +228,7 @@ pub fn get_mut(&mut self, i: uint) -> Option<&mut T> { /// assert_eq!(buf[0], 5); /// assert_eq!(buf[2], 3); /// ``` + #[stable] pub fn swap(&mut self, i: uint, j: uint) { assert!(i < self.len()); assert!(j < self.len()); @@ -546,7 +547,7 @@ pub fn clear(&mut self) { /// d.push_back(2i); /// assert_eq!(d.front(), Some(&1i)); /// ``` - #[unstable = "matches collection reform specification, waiting for dust to settle"] + #[stable] pub fn front(&self) -> Option<&T> { if !self.is_empty() { Some(&self[0]) } else { None } } @@ -570,7 +571,7 @@ pub fn front(&self) -> Option<&T> { /// } /// assert_eq!(d.front(), Some(&9i)); /// ``` - #[unstable = "matches collection reform specification, waiting for dust to settle"] + #[stable] pub fn front_mut(&mut self) -> Option<&mut T> { if !self.is_empty() { Some(&mut self[0]) } else { None } } @@ -590,7 +591,7 @@ pub fn front_mut(&mut self) -> Option<&mut T> { /// d.push_back(2i); /// assert_eq!(d.back(), Some(&2i)); /// ``` - #[unstable = "matches collection reform specification, waiting for dust to settle"] + #[stable] pub fn back(&self) -> Option<&T> { if !self.is_empty() { Some(&self[self.len() - 1]) } else { None } } @@ -614,7 +615,7 @@ pub fn back(&self) -> Option<&T> { /// } /// assert_eq!(d.back(), Some(&9i)); /// ``` - #[unstable = "matches collection reform specification, waiting for dust to settle"] + #[stable] pub fn back_mut(&mut self) -> Option<&mut T> { let len = self.len(); if !self.is_empty() { Some(&mut self[len - 1]) } else { None } From 023572b9577a388eaf354962635766dc9708244b Mon Sep 17 00:00:00 2001 From: klutzy Date: Wed, 17 Dec 2014 22:02:50 +0900 Subject: [PATCH 11/44] pprust: Fix asm options --- src/libsyntax/print/pprust.rs | 28 +++++++++++++++++++++++----- src/test/pretty/asm-options.rs | 21 +++++++++++++++++++++ 2 files changed, 44 insertions(+), 5 deletions(-) create mode 100644 src/test/pretty/asm-options.rs diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index a9717a526ad9..66ef7a9aee11 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -1786,11 +1786,7 @@ pub fn print_expr(&mut self, expr: &ast::Expr) -> IoResult<()> { } } ast::ExprInlineAsm(ref a) => { - if a.volatile { - try!(word(&mut self.s, "__volatile__ asm!")); - } else { - try!(word(&mut self.s, "asm!")); - } + try!(word(&mut self.s, "asm!")); try!(self.popen()); try!(self.print_string(a.asm.get(), a.asm_str_style)); try!(self.word_space(":")); @@ -1828,6 +1824,28 @@ pub fn print_expr(&mut self, expr: &ast::Expr) -> IoResult<()> { try!(s.print_string(co.get(), ast::CookedStr)); Ok(()) })); + + let mut options = vec!(); + if a.volatile { + options.push("volatile"); + } + if a.alignstack { + options.push("alignstack"); + } + if a.dialect == ast::AsmDialect::AsmIntel { + options.push("intel"); + } + + if options.len() > 0 { + try!(space(&mut self.s)); + try!(self.word_space(":")); + try!(self.commasep(Inconsistent, &*options, + |s, &co| { + try!(s.print_string(co, ast::CookedStr)); + Ok(()) + })); + } + try!(self.pclose()); } ast::ExprMac(ref m) => try!(self.print_mac(m, token::Paren)), diff --git a/src/test/pretty/asm-options.rs b/src/test/pretty/asm-options.rs new file mode 100644 index 000000000000..bc9f89a3d15f --- /dev/null +++ b/src/test/pretty/asm-options.rs @@ -0,0 +1,21 @@ +// Copyright 2014 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![feature(asm)] + +// pp-exact + +pub fn main() { + unsafe { + asm!("" : : : : "volatile"); + asm!("" : : : : "alignstack"); + asm!("" : : : : "intel"); + } +} From db3989c3db26bc3b5d4d2fda20eb1bbe1d2296ed Mon Sep 17 00:00:00 2001 From: Chase Southwood Date: Wed, 17 Dec 2014 19:10:09 -0600 Subject: [PATCH 12/44] Implement BitOps for HashSet --- src/libstd/collections/hash/set.rs | 119 ++++++++++++++++++++++++++++- 1 file changed, 115 insertions(+), 4 deletions(-) diff --git a/src/libstd/collections/hash/set.rs b/src/libstd/collections/hash/set.rs index f587669d3dac..0024c98b0744 100644 --- a/src/libstd/collections/hash/set.rs +++ b/src/libstd/collections/hash/set.rs @@ -11,21 +11,20 @@ // ignore-lexer-test FIXME #15883 use borrow::BorrowFrom; +use clone::Clone; use cmp::{Eq, Equiv, PartialEq}; use core::kinds::Sized; use default::Default; use fmt::Show; use fmt; use hash::{Hash, Hasher, RandomSipHasher}; -use iter::{Iterator, IteratorExt, FromIterator, Map, Chain, Extend}; +use iter::{Iterator, IteratorExt, IteratorCloneExt, FromIterator, Map, Chain, Extend}; +use ops::{BitOr, BitAnd, BitXor, Sub}; use option::Option::{Some, None, mod}; use result::Result::{Ok, Err}; use super::map::{mod, HashMap, MoveEntries, Keys, INITIAL_CAPACITY}; -// FIXME(conventions): implement BitOr, BitAnd, BitXor, and Sub - - // Future Optimization (FIXME!) // ============================= // @@ -618,6 +617,118 @@ fn default() -> HashSet { } } +#[unstable = "matches collection reform specification, waiting for dust to settle"] +impl<'a, 'b, T: Eq + Hash + Clone, S, H: Hasher + Default> +BitOr<&'b HashSet, HashSet> for &'a HashSet { + /// Returns the union of `self` and `rhs` as a new `HashSet`. + /// + /// # Examples + /// + /// ``` + /// use std::collections::HashSet; + /// + /// let a: HashSet = vec![1, 2, 3].into_iter().collect(); + /// let b: HashSet = vec![3, 4, 5].into_iter().collect(); + /// + /// let set: HashSet = &a | &b; + /// + /// let mut i = 0; + /// let expected = [1, 2, 3, 4, 5]; + /// for x in set.iter() { + /// assert!(expected.contains(x)); + /// i += 1; + /// } + /// assert_eq!(i, expected.len()); + /// ``` + fn bitor(self, rhs: &HashSet) -> HashSet { + self.union(rhs).cloned().collect() + } +} + +#[unstable = "matches collection reform specification, waiting for dust to settle"] +impl<'a, 'b, T: Eq + Hash + Clone, S, H: Hasher + Default> +BitAnd<&'b HashSet, HashSet> for &'a HashSet { + /// Returns the intersection of `self` and `rhs` as a new `HashSet`. + /// + /// # Examples + /// + /// ``` + /// use std::collections::HashSet; + /// + /// let a: HashSet = vec![1, 2, 3].into_iter().collect(); + /// let b: HashSet = vec![2, 3, 4].into_iter().collect(); + /// + /// let set: HashSet = &a & &b; + /// + /// let mut i = 0; + /// let expected = [2, 3]; + /// for x in set.iter() { + /// assert!(expected.contains(x)); + /// i += 1; + /// } + /// assert_eq!(i, expected.len()); + /// ``` + fn bitand(self, rhs: &HashSet) -> HashSet { + self.intersection(rhs).cloned().collect() + } +} + +#[unstable = "matches collection reform specification, waiting for dust to settle"] +impl<'a, 'b, T: Eq + Hash + Clone, S, H: Hasher + Default> +BitXor<&'b HashSet, HashSet> for &'a HashSet { + /// Returns the symmetric difference of `self` and `rhs` as a new `HashSet`. + /// + /// # Examples + /// + /// ``` + /// use std::collections::HashSet; + /// + /// let a: HashSet = vec![1, 2, 3].into_iter().collect(); + /// let b: HashSet = vec![3, 4, 5].into_iter().collect(); + /// + /// let set: HashSet = &a ^ &b; + /// + /// let mut i = 0; + /// let expected = [1, 2, 4, 5]; + /// for x in set.iter() { + /// assert!(expected.contains(x)); + /// i += 1; + /// } + /// assert_eq!(i, expected.len()); + /// ``` + fn bitxor(self, rhs: &HashSet) -> HashSet { + self.symmetric_difference(rhs).cloned().collect() + } +} + +#[unstable = "matches collection reform specification, waiting for dust to settle"] +impl<'a, 'b, T: Eq + Hash + Clone, S, H: Hasher + Default> +Sub<&'b HashSet, HashSet> for &'a HashSet { + /// Returns the difference of `self` and `rhs` as a new `HashSet`. + /// + /// # Examples + /// + /// ``` + /// use std::collections::HashSet; + /// + /// let a: HashSet = vec![1, 2, 3].into_iter().collect(); + /// let b: HashSet = vec![3, 4, 5].into_iter().collect(); + /// + /// let set: HashSet = &a - &b; + /// + /// let mut i = 0; + /// let expected = [1, 2]; + /// for x in set.iter() { + /// assert!(expected.contains(x)); + /// i += 1; + /// } + /// assert_eq!(i, expected.len()); + /// ``` + fn sub(self, rhs: &HashSet) -> HashSet { + self.difference(rhs).cloned().collect() + } +} + /// HashSet iterator pub struct Iter<'a, K: 'a> { iter: Keys<'a, K, ()> From 082bfde412176249dc7328e771a2a15d202824cf Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Wed, 10 Dec 2014 19:46:38 -0800 Subject: [PATCH 13/44] Fallout of std::str stabilization --- src/compiletest/compiletest.rs | 6 +- src/compiletest/header.rs | 6 +- src/compiletest/runtest.rs | 2 +- src/doc/guide.md | 24 +- src/doc/reference.md | 2 +- src/libcollections/lib.rs | 4 +- src/libcollections/str.rs | 125 +++++---- src/libcollections/string.rs | 22 +- src/libcore/option.rs | 6 +- src/libcore/result.rs | 2 +- src/libcore/str.rs | 36 +-- src/libcoretest/lib.rs | 1 + src/libcoretest/str.rs | 2 +- src/libfmt_macros/lib.rs | 21 +- src/libgetopts/lib.rs | 61 ++--- src/libgraphviz/lib.rs | 14 +- src/liblog/directive.rs | 2 +- src/liblog/lib.rs | 8 +- src/libregex/parse.rs | 57 ++-- src/libregex/re.rs | 28 +- src/librustc/lint/builtin.rs | 54 ++-- src/librustc/lint/context.rs | 56 ++-- src/librustc/metadata/creader.rs | 40 +-- src/librustc/metadata/csearch.rs | 2 +- src/librustc/metadata/decoder.rs | 6 +- src/librustc/metadata/encoder.rs | 88 +++--- src/librustc/metadata/loader.rs | 36 +-- src/librustc/metadata/tydecode.rs | 12 +- src/librustc/middle/astconv_util.rs | 2 +- src/librustc/middle/astencode.rs | 6 +- src/librustc/middle/cfg/construct.rs | 6 +- src/librustc/middle/cfg/graphviz.rs | 5 +- src/librustc/middle/check_loop.rs | 6 +- src/librustc/middle/check_match.rs | 18 +- src/librustc/middle/check_static.rs | 6 +- src/librustc/middle/check_static_recursion.rs | 2 +- src/librustc/middle/const_eval.rs | 8 +- src/librustc/middle/dataflow.rs | 6 +- src/librustc/middle/dependency_format.rs | 6 +- src/librustc/middle/expr_use_visitor.rs | 8 +- src/librustc/middle/infer/combine.rs | 4 +- src/librustc/middle/infer/error_reporting.rs | 74 ++--- .../middle/infer/higher_ranked/mod.rs | 4 +- src/librustc/middle/infer/mod.rs | 2 +- .../middle/infer/region_inference/graphviz.rs | 2 +- .../middle/infer/region_inference/mod.rs | 16 +- src/librustc/middle/liveness.rs | 12 +- src/librustc/middle/mem_categorization.rs | 8 +- src/librustc/middle/privacy.rs | 12 +- src/librustc/middle/reachable.rs | 10 +- src/librustc/middle/resolve_lifetime.rs | 6 +- src/librustc/middle/subst.rs | 4 +- src/librustc/middle/traits/coherence.rs | 2 +- src/librustc/middle/traits/select.rs | 15 +- src/librustc/middle/ty.rs | 68 ++--- src/librustc/plugin/load.rs | 6 +- src/librustc/session/config.rs | 58 ++-- src/librustc/session/mod.rs | 4 +- src/librustc/util/common.rs | 3 +- src/librustc/util/ppaux.rs | 24 +- src/librustc_back/archive.rs | 42 +-- src/librustc_back/rpath.rs | 16 +- src/librustc_back/svh.rs | 4 +- src/librustc_back/target/mod.rs | 4 +- src/librustc_borrowck/borrowck/check_loans.rs | 42 +-- src/librustc_borrowck/borrowck/fragments.rs | 38 +-- .../borrowck/gather_loans/mod.rs | 2 +- .../borrowck/gather_loans/move_error.rs | 8 +- src/librustc_borrowck/borrowck/mod.rs | 38 +-- src/librustc_borrowck/graphviz.rs | 4 +- src/librustc_driver/driver.rs | 30 +- src/librustc_driver/lib.rs | 28 +- src/librustc_driver/pretty.rs | 30 +- src/librustc_resolve/lib.rs | 126 ++++----- src/librustc_trans/back/link.rs | 128 ++++----- src/librustc_trans/back/lto.rs | 20 +- src/librustc_trans/back/write.rs | 88 +++--- src/librustc_trans/save/mod.rs | 146 +++++----- src/librustc_trans/save/recorder.rs | 22 +- src/librustc_trans/save/span_utils.rs | 4 +- src/librustc_trans/trans/_match.rs | 36 +-- src/librustc_trans/trans/adt.rs | 58 ++-- src/librustc_trans/trans/asm.rs | 14 +- src/librustc_trans/trans/base.rs | 104 +++---- src/librustc_trans/trans/builder.rs | 8 +- src/librustc_trans/trans/cabi.rs | 4 +- src/librustc_trans/trans/callee.rs | 12 +- src/librustc_trans/trans/cleanup.rs | 10 +- src/librustc_trans/trans/closure.rs | 22 +- src/librustc_trans/trans/common.rs | 14 +- src/librustc_trans/trans/consts.rs | 44 +-- src/librustc_trans/trans/context.rs | 6 +- src/librustc_trans/trans/controlflow.rs | 14 +- src/librustc_trans/trans/datum.rs | 2 +- src/librustc_trans/trans/debuginfo.rs | 138 +++++----- src/librustc_trans/trans/expr.rs | 50 ++-- src/librustc_trans/trans/foreign.rs | 30 +- src/librustc_trans/trans/glue.rs | 14 +- src/librustc_trans/trans/intrinsic.rs | 2 +- src/librustc_trans/trans/meth.rs | 10 +- src/librustc_trans/trans/monomorphize.rs | 22 +- src/librustc_trans/trans/type_.rs | 2 +- src/librustc_trans/trans/type_of.rs | 14 +- src/librustc_typeck/astconv.rs | 32 +-- src/librustc_typeck/check/method/mod.rs | 6 +- src/librustc_typeck/check/method/probe.rs | 2 +- src/librustc_typeck/check/mod.rs | 62 ++--- src/librustc_typeck/check/regionck.rs | 14 +- src/librustc_typeck/check/regionmanip.rs | 2 +- src/librustc_typeck/check/vtable.rs | 12 +- src/librustc_typeck/coherence/mod.rs | 4 +- src/librustc_typeck/collect.rs | 40 +-- src/librustc_typeck/lib.rs | 7 +- src/librustc_typeck/variance.rs | 8 +- src/librustdoc/externalfiles.rs | 2 +- src/librustdoc/html/format.rs | 10 +- src/librustdoc/html/highlight.rs | 2 +- src/librustdoc/html/render.rs | 8 +- src/librustdoc/passes.rs | 6 +- src/libserialize/json.rs | 192 ++++++------- src/libserialize/lib.rs | 1 + src/libserialize/serialize.rs | 4 +- src/libstd/ascii.rs | 2 +- src/libstd/c_str.rs | 2 +- src/libstd/dynamic_lib.rs | 2 +- src/libstd/failure.rs | 2 +- src/libstd/io/mod.rs | 7 +- src/libstd/io/net/ip.rs | 2 +- src/libstd/io/process.rs | 4 +- src/libstd/io/stdio.rs | 2 +- src/libstd/num/strconv.rs | 2 +- src/libstd/os.rs | 12 +- src/libstd/path/mod.rs | 19 +- src/libstd/path/posix.rs | 5 +- src/libstd/path/windows.rs | 144 +++++----- src/libstd/prelude.rs | 4 +- src/libstd/rt/backtrace.rs | 3 +- src/libstd/rt/mod.rs | 2 +- src/libstd/rt/unwind.rs | 2 +- src/libstd/rt/util.rs | 23 +- src/libstd/sys/common/backtrace.rs | 11 +- src/libstd/sys/windows/backtrace.rs | 2 +- src/libstd/sys/windows/fs.rs | 3 +- src/libstd/sys/windows/os.rs | 4 +- src/libstd/sys/windows/process.rs | 2 +- src/libstd/sys/windows/tty.rs | 2 +- src/libsyntax/ast.rs | 2 +- src/libsyntax/ast_map/mod.rs | 38 +-- src/libsyntax/ast_util.rs | 14 +- src/libsyntax/attr.rs | 13 +- src/libsyntax/codemap.rs | 12 +- src/libsyntax/diagnostic.rs | 38 +-- src/libsyntax/diagnostics/plugin.rs | 6 +- src/libsyntax/ext/asm.rs | 3 +- src/libsyntax/ext/base.rs | 10 +- src/libsyntax/ext/build.rs | 3 +- src/libsyntax/ext/concat.rs | 8 +- src/libsyntax/ext/concat_idents.rs | 2 +- src/libsyntax/ext/deriving/bounds.rs | 3 +- src/libsyntax/ext/deriving/clone.rs | 9 +- src/libsyntax/ext/deriving/decodable.rs | 2 +- src/libsyntax/ext/deriving/encodable.rs | 3 +- src/libsyntax/ext/deriving/generic/mod.rs | 46 ++-- src/libsyntax/ext/deriving/mod.rs | 2 +- src/libsyntax/ext/deriving/show.rs | 2 +- src/libsyntax/ext/env.rs | 8 +- src/libsyntax/ext/expand.rs | 57 ++-- src/libsyntax/ext/format.rs | 27 +- src/libsyntax/ext/quote.rs | 6 +- src/libsyntax/ext/source_util.rs | 16 +- src/libsyntax/ext/tt/macro_parser.rs | 21 +- src/libsyntax/ext/tt/macro_rules.rs | 12 +- src/libsyntax/ext/tt/transcribe.rs | 4 +- src/libsyntax/feature_gate.rs | 10 +- src/libsyntax/parse/attr.rs | 3 +- src/libsyntax/parse/lexer/comments.rs | 16 +- src/libsyntax/parse/lexer/mod.rs | 18 +- src/libsyntax/parse/mod.rs | 54 ++-- src/libsyntax/parse/obsolete.rs | 4 +- src/libsyntax/parse/parser.rs | 148 +++++----- src/libsyntax/parse/token.rs | 24 +- src/libsyntax/print/pp.rs | 6 +- src/libsyntax/print/pprust.rs | 259 +++++++++--------- src/libsyntax/std_inject.rs | 8 +- src/libsyntax/test.rs | 26 +- src/libsyntax/util/interner.rs | 30 +- src/libterm/terminfo/mod.rs | 2 +- src/libterm/terminfo/searcher.rs | 4 +- src/libtest/lib.rs | 8 +- src/libunicode/u_str.rs | 111 +------- src/test/run-pass/issue-19340-1.rs | 2 +- src/test/run-pass/issue-19340-2.rs | 2 +- src/test/run-pass/issue-19367.rs | 8 +- 193 files changed, 2143 insertions(+), 2230 deletions(-) diff --git a/src/compiletest/compiletest.rs b/src/compiletest/compiletest.rs index 59be0152d587..bdbfbfd7c89d 100644 --- a/src/compiletest/compiletest.rs +++ b/src/compiletest/compiletest.rs @@ -152,7 +152,7 @@ fn opt_path(m: &getopts::Matches, nm: &str) -> Path { matches.opt_str("ratchet-metrics").map(|s| Path::new(s)), ratchet_noise_percent: matches.opt_str("ratchet-noise-percent") - .and_then(|s| from_str::(s.as_slice())), + .and_then(|s| s.as_slice().parse::()), runtool: matches.opt_str("runtool"), host_rustcflags: matches.opt_str("host-rustcflags"), target_rustcflags: matches.opt_str("target-rustcflags"), @@ -190,9 +190,7 @@ pub fn log_config(config: &Config) { logv(c, format!("filter: {}", opt_str(&config.filter .as_ref() - .map(|re| { - re.to_string().into_string() - })))); + .map(|re| re.to_string())))); logv(c, format!("runtool: {}", opt_str(&config.runtool))); logv(c, format!("host-rustcflags: {}", opt_str(&config.host_rustcflags))); diff --git a/src/compiletest/header.rs b/src/compiletest/header.rs index 60ef76528e84..27be6c6d8356 100644 --- a/src/compiletest/header.rs +++ b/src/compiletest/header.rs @@ -351,8 +351,8 @@ pub fn gdb_version_to_int(version_string: &str) -> int { panic!("{}", error_string); } - let major: int = from_str(components[0]).expect(error_string); - let minor: int = from_str(components[1]).expect(error_string); + let major: int = components[0].parse().expect(error_string); + let minor: int = components[1].parse().expect(error_string); return major * 1000 + minor; } @@ -362,6 +362,6 @@ pub fn lldb_version_to_int(version_string: &str) -> int { "Encountered LLDB version string with unexpected format: {}", version_string); let error_string = error_string.as_slice(); - let major: int = from_str(version_string).expect(error_string); + let major: int = version_string.parse().expect(error_string); return major; } diff --git a/src/compiletest/runtest.rs b/src/compiletest/runtest.rs index 567734b0dab7..bf72250c4705 100644 --- a/src/compiletest/runtest.rs +++ b/src/compiletest/runtest.rs @@ -1361,7 +1361,7 @@ fn split_maybe_args(argstr: &Option) -> Vec { s.as_slice() .split(' ') .filter_map(|s| { - if s.is_whitespace() { + if s.chars().all(|c| c.is_whitespace()) { None } else { Some(s.to_string()) diff --git a/src/doc/guide.md b/src/doc/guide.md index 1cd100e15980..3963ce6b85d3 100644 --- a/src/doc/guide.md +++ b/src/doc/guide.md @@ -2257,10 +2257,10 @@ a function for that: let input = io::stdin().read_line() .ok() .expect("Failed to read line"); -let input_num: Option = from_str(input.as_slice()); +let input_num: Option = input.parse(); ``` -The `from_str` function takes in a `&str` value and converts it into something. +The `parse` function takes in a `&str` value and converts it into something. We tell it what kind of something with a type hint. Remember our type hint with `random()`? It looked like this: @@ -2279,8 +2279,8 @@ In this case, we say `x` is a `uint` explicitly, so Rust is able to properly tell `random()` what to generate. In a similar fashion, both of these work: ```{rust,ignore} -let input_num = from_str::("5"); // input_num: Option -let input_num: Option = from_str("5"); // input_num: Option +let input_num = "5".parse::(); // input_num: Option +let input_num: Option = "5".parse(); // input_num: Option ``` Anyway, with us now converting our input to a number, our code looks like this: @@ -2301,7 +2301,7 @@ fn main() { let input = io::stdin().read_line() .ok() .expect("Failed to read line"); - let input_num: Option = from_str(input.as_slice()); + let input_num: Option = input.parse(); println!("You guessed: {}", input_num); @@ -2350,7 +2350,7 @@ fn main() { let input = io::stdin().read_line() .ok() .expect("Failed to read line"); - let input_num: Option = from_str(input.as_slice()); + let input_num: Option = input.parse(); let num = match input_num { Some(num) => num, @@ -2395,7 +2395,7 @@ Uh, what? But we did! ... actually, we didn't. See, when you get a line of input from `stdin()`, you get all the input. Including the `\n` character from you pressing Enter. -Therefore, `from_str()` sees the string `"5\n"` and says "nope, that's not a +Therefore, `parse()` sees the string `"5\n"` and says "nope, that's not a number; there's non-number stuff in there!" Luckily for us, `&str`s have an easy method we can use defined on them: `trim()`. One small modification, and our code looks like this: @@ -2416,7 +2416,7 @@ fn main() { let input = io::stdin().read_line() .ok() .expect("Failed to read line"); - let input_num: Option = from_str(input.as_slice().trim()); + let input_num: Option = input.trim().parse(); let num = match input_num { Some(num) => num, @@ -2491,7 +2491,7 @@ fn main() { let input = io::stdin().read_line() .ok() .expect("Failed to read line"); - let input_num: Option = from_str(input.as_slice().trim()); + let input_num: Option = input.trim().parse(); let num = match input_num { Some(num) => num, @@ -2566,7 +2566,7 @@ fn main() { let input = io::stdin().read_line() .ok() .expect("Failed to read line"); - let input_num: Option = from_str(input.as_slice().trim()); + let input_num: Option = input.trim().parse(); let num = match input_num { Some(num) => num, @@ -2621,7 +2621,7 @@ fn main() { let input = io::stdin().read_line() .ok() .expect("Failed to read line"); - let input_num: Option = from_str(input.as_slice().trim()); + let input_num: Option = input.trim().parse(); let num = match input_num { Some(num) => num, @@ -2697,7 +2697,7 @@ fn main() { let input = io::stdin().read_line() .ok() .expect("Failed to read line"); - let input_num: Option = from_str(input.as_slice().trim()); + let input_num: Option = input.trim().parse(); let num = match input_num { Some(num) => num, diff --git a/src/doc/reference.md b/src/doc/reference.md index 722230d37557..97184d534983 100644 --- a/src/doc/reference.md +++ b/src/doc/reference.md @@ -3177,7 +3177,7 @@ Some examples of call expressions: # fn add(x: int, y: int) -> int { 0 } let x: int = add(1, 2); -let pi: Option = from_str("3.14"); +let pi: Option = "3.14".parse(); ``` ### Lambda expressions diff --git a/src/libcollections/lib.rs b/src/libcollections/lib.rs index 75d179319f7c..363d30abd034 100644 --- a/src/libcollections/lib.rs +++ b/src/libcollections/lib.rs @@ -121,7 +121,7 @@ mod prelude { // in core and collections (may differ). pub use slice::{PartialEqSliceExt, OrdSliceExt}; pub use slice::{AsSlice, SliceExt}; - pub use str::{from_str, Str, StrPrelude}; + pub use str::{from_str, Str}; // from other crates. pub use alloc::boxed::Box; @@ -129,7 +129,7 @@ mod prelude { // from collections. pub use slice::{CloneSliceExt, VectorVector}; - pub use str::{IntoMaybeOwned, UnicodeStrPrelude, StrAllocating, StrVector}; + pub use str::{IntoMaybeOwned, StrVector}; pub use string::{String, ToString}; pub use vec::Vec; } diff --git a/src/libcollections/str.rs b/src/libcollections/str.rs index 8c9346639b34..5feae5e558ed 100644 --- a/src/libcollections/str.rs +++ b/src/libcollections/str.rs @@ -55,25 +55,31 @@ use self::RecompositionState::*; use self::DecompositionType::*; -use core::prelude::*; - use core::borrow::{BorrowFrom, Cow, ToOwned}; -use core::cmp::{mod, Equiv, PartialEq, Eq, PartialOrd, Ord, Ordering}; +use core::char::Char; +use core::clone::Clone; +use core::cmp::{Equiv, PartialEq, Eq, PartialOrd, Ord, Ordering}; +use core::cmp; use core::default::Default; use core::fmt; use core::hash; use core::iter::AdditiveIterator; use core::iter::{mod, range, Iterator, IteratorExt}; +use core::kinds::Sized; +use core::ops; +use core::option::Option::{mod, Some, None}; +use core::slice::AsSlice; use core::str as core_str; use unicode::str::{UnicodeStr, Utf16Encoder}; use ring_buf::RingBuf; -use string::{String, ToString}; +use slice::SliceExt; +use string::String; use unicode; use vec::Vec; pub use core::str::{from_utf8, CharEq, Chars, CharIndices}; -pub use core::str::{Bytes, CharSplits}; +pub use core::str::{Bytes, CharSplits, is_utf8}; pub use core::str::{CharSplitsN, Lines, LinesAny, MatchIndices, StrSplits}; pub use core::str::{CharRange}; pub use core::str::{FromStr, from_str, Utf8Error}; @@ -408,6 +414,7 @@ fn size_hint(&self) -> (uint, Option) { self.encoder.size_hint() } /// # Examples /// /// ```rust +/// # #![allow(deprecated)] /// use std::str; /// let string = "orange"; /// let new_string = str::replace(string, "or", "str"); @@ -441,7 +448,7 @@ macro_rules! utf8_acc_cont_byte { /// A string type that can hold either a `String` or a `&str`. /// This can be useful as an optimization when an allocation is sometimes /// needed but not always. -#[deprecated = "use stding::string::CowString"] +#[deprecated = "use std::string::CowString"] pub enum MaybeOwned<'a> { /// A borrowed string. Slice(&'a str), @@ -650,7 +657,11 @@ fn borrow_from(owned: &String) -> &str { owned[] } #[unstable = "trait is unstable"] impl ToOwned for str { - fn to_owned(&self) -> String { self.to_string() } + fn to_owned(&self) -> String { + unsafe { + String::from_utf8_unchecked(self.as_bytes().to_owned()) + } + } } /// Unsafe string operations. @@ -673,7 +684,7 @@ pub mod raw { */ /// Any string that can be represented as a slice. -pub trait StrExt for Sized?: Slice { +pub trait StrExt for Sized?: ops::Slice { /// Escapes each char in `s` with `char::escape_default`. #[unstable = "return type may change to be an iterator"] fn escape_default(&self) -> String { @@ -724,7 +735,7 @@ fn replace(&self, from: &str, to: &str) -> String { } /// Given a string, makes a new string with repeated copies of it. - #[deprecated = "user repeat(self).take(n).collect() instead"] + #[deprecated = "use repeat(self).take(n).collect() instead"] fn repeat(&self, nn: uint) -> String { iter::repeat(self[]).take(nn).collect() } @@ -766,7 +777,7 @@ fn lev_distance(&self, t: &str) -> uint { /// Returns an iterator over the string in Unicode Normalization Form D /// (canonical decomposition). #[inline] - #[unstable = "this functionality may only be provided by libunicode"] + #[unstable = "this functionality may be moved to libunicode"] fn nfd_chars<'a>(&'a self) -> Decompositions<'a> { Decompositions { iter: self[].chars(), @@ -779,7 +790,7 @@ fn nfd_chars<'a>(&'a self) -> Decompositions<'a> { /// Returns an iterator over the string in Unicode Normalization Form KD /// (compatibility decomposition). #[inline] - #[unstable = "this functionality may only be provided by libunicode"] + #[unstable = "this functionality may be moved to libunicode"] fn nfkd_chars<'a>(&'a self) -> Decompositions<'a> { Decompositions { iter: self[].chars(), @@ -792,7 +803,7 @@ fn nfkd_chars<'a>(&'a self) -> Decompositions<'a> { /// An Iterator over the string in Unicode Normalization Form C /// (canonical decomposition followed by canonical composition). #[inline] - #[unstable = "this functionality may only be provided by libunicode"] + #[unstable = "this functionality may be moved to libunicode"] fn nfc_chars<'a>(&'a self) -> Recompositions<'a> { Recompositions { iter: self.nfd_chars(), @@ -806,7 +817,7 @@ fn nfc_chars<'a>(&'a self) -> Recompositions<'a> { /// An Iterator over the string in Unicode Normalization Form KC /// (compatibility decomposition followed by canonical composition). #[inline] - #[unstable = "this functionality may only be provided by libunicode"] + #[unstable = "this functionality may be moved to libunicode"] fn nfkc_chars<'a>(&'a self) -> Recompositions<'a> { Recompositions { iter: self.nfkd_chars(), @@ -891,7 +902,7 @@ fn char_indices(&self) -> CharIndices { /// let v: Vec<&str> = "Mary had a little lamb".split(' ').collect(); /// assert_eq!(v, vec!["Mary", "had", "a", "little", "lamb"]); /// - /// let v: Vec<&str> = "abc1def2ghi".split(|c: char| c.is_numeric()).collect(); + /// let v: Vec<&str> = "abc1def2ghi".split(|&: c: char| c.is_numeric()).collect(); /// assert_eq!(v, vec!["abc", "def", "ghi"]); /// /// let v: Vec<&str> = "lionXXtigerXleopard".split('X').collect(); @@ -915,7 +926,7 @@ fn split(&self, sep: Sep) -> CharSplits { /// let v: Vec<&str> = "Mary had a little lambda".splitn(2, ' ').collect(); /// assert_eq!(v, vec!["Mary", "had", "a little lambda"]); /// - /// let v: Vec<&str> = "abc1def2ghi".splitn(1, |c: char| c.is_numeric()).collect(); + /// let v: Vec<&str> = "abc1def2ghi".splitn(1, |&: c: char| c.is_numeric()).collect(); /// assert_eq!(v, vec!["abc", "def2ghi"]); /// /// let v: Vec<&str> = "lionXXtigerXleopard".splitn(2, 'X').collect(); @@ -950,7 +961,7 @@ fn splitn(&self, count: uint, sep: Sep) -> CharSplitsN { /// let v: Vec<&str> = "Mary had a little lamb".split(' ').rev().collect(); /// assert_eq!(v, vec!["lamb", "little", "a", "had", "Mary"]); /// - /// let v: Vec<&str> = "abc1def2ghi".split(|c: char| c.is_numeric()).rev().collect(); + /// let v: Vec<&str> = "abc1def2ghi".split(|&: c: char| c.is_numeric()).rev().collect(); /// assert_eq!(v, vec!["ghi", "def", "abc"]); /// /// let v: Vec<&str> = "lionXXtigerXleopard".split('X').rev().collect(); @@ -971,7 +982,7 @@ fn split_terminator(&self, sep: Sep) -> CharSplits { /// let v: Vec<&str> = "Mary had a little lamb".rsplitn(2, ' ').collect(); /// assert_eq!(v, vec!["lamb", "little", "Mary had a"]); /// - /// let v: Vec<&str> = "abc1def2ghi".rsplitn(1, |c: char| c.is_numeric()).collect(); + /// let v: Vec<&str> = "abc1def2ghi".rsplitn(1, |&: c: char| c.is_numeric()).collect(); /// assert_eq!(v, vec!["ghi", "abc1def"]); /// /// let v: Vec<&str> = "lionXXtigerXleopard".rsplitn(2, 'X').collect(); @@ -1071,10 +1082,11 @@ fn lines_any(&self) -> LinesAny { /// # Example /// /// ```rust + /// # #![allow(deprecated)] /// // composed forms of `ö` and `é` /// let c = "Löwe 老虎 Léopard"; // German, Simplified Chinese, French /// // decomposed forms of `ö` and `é` - /// let d = "Lo\u0308we 老虎 Le\u0301opard"; + /// let d = "Lo\u{0308}we 老虎 Le\u{0301}opard"; /// /// assert_eq!(c.char_len(), 15); /// assert_eq!(d.char_len(), 17); @@ -1225,10 +1237,10 @@ fn ends_with(&self, needle: &str) -> bool { /// # Example /// /// ```rust - /// assert_eq!("11foo1bar11".trim_chars('1'), "foo1bar") + /// assert_eq!("11foo1bar11".trim_chars('1'), "foo1bar"); /// let x: &[_] = &['1', '2']; - /// assert_eq!("12foo1bar12".trim_chars(x), "foo1bar") - /// assert_eq!("123foo1bar123".trim_chars(|c: char| c.is_numeric()), "foo1bar") + /// assert_eq!("12foo1bar12".trim_chars(x), "foo1bar"); + /// assert_eq!("123foo1bar123".trim_chars(|&: c: char| c.is_numeric()), "foo1bar"); /// ``` #[unstable = "awaiting pattern/matcher stabilization"] fn trim_chars(&self, to_trim: C) -> &str { @@ -1244,10 +1256,10 @@ fn trim_chars(&self, to_trim: C) -> &str { /// # Example /// /// ```rust - /// assert_eq!("11foo1bar11".trim_left_chars('1'), "foo1bar11") + /// assert_eq!("11foo1bar11".trim_left_chars('1'), "foo1bar11"); /// let x: &[_] = &['1', '2']; - /// assert_eq!("12foo1bar12".trim_left_chars(x), "foo1bar12") - /// assert_eq!("123foo1bar123".trim_left_chars(|c: char| c.is_numeric()), "foo1bar123") + /// assert_eq!("12foo1bar12".trim_left_chars(x), "foo1bar12"); + /// assert_eq!("123foo1bar123".trim_left_chars(|&: c: char| c.is_numeric()), "foo1bar123"); /// ``` #[unstable = "awaiting pattern/matcher stabilization"] fn trim_left_chars(&self, to_trim: C) -> &str { @@ -1263,10 +1275,10 @@ fn trim_left_chars(&self, to_trim: C) -> &str { /// # Example /// /// ```rust - /// assert_eq!("11foo1bar11".trim_right_chars('1'), "11foo1bar") + /// assert_eq!("11foo1bar11".trim_right_chars('1'), "11foo1bar"); /// let x: &[_] = &['1', '2']; - /// assert_eq!("12foo1bar12".trim_right_chars(x), "12foo1bar") - /// assert_eq!("123foo1bar123".trim_right_chars(|c: char| c.is_numeric()), "123foo1bar") + /// assert_eq!("12foo1bar12".trim_right_chars(x), "12foo1bar"); + /// assert_eq!("123foo1bar123".trim_right_chars(|&: c: char| c.is_numeric()), "123foo1bar"); /// ``` #[unstable = "awaiting pattern/matcher stabilization"] fn trim_right_chars(&self, to_trim: C) -> &str { @@ -1434,7 +1446,7 @@ fn as_bytes(&self) -> &[u8] { /// assert_eq!(s.find('é'), Some(14)); /// /// // the first space - /// assert_eq!(s.find(|c: char| c.is_whitespace()), Some(5)); + /// assert_eq!(s.find(|&: c: char| c.is_whitespace()), Some(5)); /// /// // neither are found /// let x: &[_] = &['1', '2']; @@ -1462,7 +1474,7 @@ fn find(&self, search: C) -> Option { /// assert_eq!(s.rfind('é'), Some(14)); /// /// // the second space - /// assert_eq!(s.rfind(|c: char| c.is_whitespace()), Some(12)); + /// assert_eq!(s.rfind(|&: c: char| c.is_whitespace()), Some(12)); /// /// // searches for an occurrence of either `1` or `2`, but neither are found /// let x: &[_] = &['1', '2']; @@ -1609,8 +1621,8 @@ fn parse(&self) -> Option { /// # Example /// /// ```rust - /// let gr1 = "a\u0310e\u0301o\u0308\u0332".graphemes(true).collect::>(); - /// let b: &[_] = &["a\u0310", "e\u0301", "o\u0308\u0332"]; + /// let gr1 = "a\u{310}e\u{301}o\u{308}\u{332}".graphemes(true).collect::>(); + /// let b: &[_] = &["a\u{310}", "e\u{301}", "o\u{308}\u{332}"]; /// assert_eq!(gr1.as_slice(), b); /// let gr2 = "a\r\nb🇷🇺🇸🇹".graphemes(true).collect::>(); /// let b: &[_] = &["a", "\r\n", "b", "🇷🇺🇸🇹"]; @@ -1659,6 +1671,7 @@ fn words(&self) -> Words { /// # Example /// /// ```rust + /// # #![allow(deprecated)] /// assert!(" \t\n".is_whitespace()); /// assert!("".is_whitespace()); /// @@ -1677,6 +1690,7 @@ fn is_whitespace(&self) -> bool { /// # Example /// /// ```rust + /// # #![allow(deprecated)] /// assert!("Löwe老虎Léopard123".is_alphanumeric()); /// assert!("".is_alphanumeric()); /// @@ -1718,25 +1732,39 @@ fn trim_left(&self) -> &str { fn trim_right(&self) -> &str { UnicodeStr::trim_right(self[]) } + + /// Deprecated, call `.to_owned()` instead from the `std::borrow::ToOwned` + /// trait. + #[deprecated = "call `.to_owned()` on `std::borrow::ToOwned` instead"] + fn into_string(&self) -> String { + self[].to_owned() + } } impl StrExt for str {} #[cfg(test)] mod tests { - use prelude::*; - use core::default::Default; - use core::iter::AdditiveIterator; - use super::{eq_slice, from_utf8, is_utf8, is_utf16, raw}; - use super::truncate_utf16_at_nul; - use super::MaybeOwned::{Owned, Slice}; + use std::iter::AdditiveIterator; + use std::iter::range; + use std::default::Default; + use std::char::Char; + use std::clone::Clone; + use std::cmp::{Ord, PartialOrd, Equiv}; + use std::cmp::Ordering::{Equal, Greater, Less}; + use std::option::Option::{mod, Some, None}; + use std::result::Result::{Ok, Err}; + use std::ptr::RawPtr; + use std::iter::{Iterator, IteratorExt, DoubleEndedIteratorExt}; - #[test] - fn test_eq_slice() { - assert!((eq_slice("foobar".slice(0, 3), "foo"))); - assert!((eq_slice("barfoo".slice(3, 6), "foo"))); - assert!((!eq_slice("foo1", "foo2"))); - } + use super::*; + use super::MaybeOwned::{Owned, Slice}; + use std::slice::{AsSlice, SliceExt}; + use string::{String, ToString}; + use vec::Vec; + use slice::CloneSliceExt; + + use unicode::char::UnicodeChar; #[test] fn test_le() { @@ -2267,6 +2295,7 @@ fn test_is_utf8() { #[test] fn test_is_utf16() { + use unicode::str::is_utf16; macro_rules! pos ( ($($e:expr),*) => { { $(assert!(is_utf16($e));)* } }); // non-surrogates @@ -3229,13 +3258,13 @@ fn sum_len(v: &[&str]) -> uint { #[test] fn test_str_from_utf8() { let xs = b"hello"; - assert_eq!(from_utf8(xs), Some("hello")); + assert_eq!(from_utf8(xs), Ok("hello")); let xs = "ศไทย中华Việt Nam".as_bytes(); - assert_eq!(from_utf8(xs), Some("ศไทย中华Việt Nam")); + assert_eq!(from_utf8(xs), Ok("ศไทย中华Việt Nam")); let xs = b"hello\xFF"; - assert_eq!(from_utf8(xs), None); + assert_eq!(from_utf8(xs), Err(Utf8Error::TooShort)); } #[test] @@ -3284,8 +3313,8 @@ fn test_maybe_owned_clone() { #[test] fn test_maybe_owned_into_string() { - assert_eq!(Slice("abcde").into_string(), String::from_str("abcde")); - assert_eq!(Owned(String::from_str("abcde")).into_string(), + assert_eq!(Slice("abcde").to_string(), String::from_str("abcde")); + assert_eq!(Owned(String::from_str("abcde")).to_string(), String::from_str("abcde")); } diff --git a/src/libcollections/string.rs b/src/libcollections/string.rs index 0e2b514d92d4..6c2659b13f72 100644 --- a/src/libcollections/string.rs +++ b/src/libcollections/string.rs @@ -26,7 +26,7 @@ use unicode::str::Utf16Item; use slice::CloneSliceExt; -use str::{mod, CharRange, FromStr, StrExt, Owned, Utf8Error}; +use str::{mod, CharRange, FromStr, Utf8Error}; use vec::{DerefVec, Vec, as_vec}; /// A growable string stored as a UTF-8 encoded buffer. @@ -94,13 +94,16 @@ pub fn from_str(string: &str) -> String { /// # Examples /// /// ```rust + /// # #![allow(deprecated)] + /// use std::str::Utf8Error; + /// /// let hello_vec = vec![104, 101, 108, 108, 111]; /// let s = String::from_utf8(hello_vec); /// assert_eq!(s, Ok("hello".to_string())); /// /// let invalid_vec = vec![240, 144, 128]; /// let s = String::from_utf8(invalid_vec); - /// assert_eq!(s, Err(vec![240, 144, 128])); + /// assert_eq!(s, Err((vec![240, 144, 128], Utf8Error::TooShort))); /// ``` #[inline] #[unstable = "error type may change"] @@ -833,7 +836,7 @@ fn default() -> String { #[experimental = "waiting on Show stabilization"] impl fmt::Show for String { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - (*self).fmt(f) + (**self).fmt(f) } } @@ -841,7 +844,7 @@ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { impl hash::Hash for String { #[inline] fn hash(&self, hasher: &mut H) { - (*self).hash(hasher) + (**self).hash(hasher) } } @@ -1026,6 +1029,7 @@ mod tests { use prelude::*; use test::Bencher; + use str::{StrExt, Utf8Error}; use str; use super::as_string; @@ -1044,14 +1048,16 @@ fn test_from_str() { #[test] fn test_from_utf8() { let xs = b"hello".to_vec(); - assert_eq!(String::from_utf8(xs), Ok(String::from_str("hello"))); + assert_eq!(String::from_utf8(xs), + Ok(String::from_str("hello"))); let xs = "ศไทย中华Việt Nam".as_bytes().to_vec(); - assert_eq!(String::from_utf8(xs), Ok(String::from_str("ศไทย中华Việt Nam"))); + assert_eq!(String::from_utf8(xs), + Ok(String::from_str("ศไทย中华Việt Nam"))); let xs = b"hello\xFF".to_vec(); assert_eq!(String::from_utf8(xs), - Err(b"hello\xFF".to_vec())); + Err((b"hello\xFF".to_vec(), Utf8Error::TooShort))); } #[test] @@ -1141,7 +1147,7 @@ fn test_from_utf16() { let s_as_utf16 = s.utf16_units().collect::>(); let u_as_string = String::from_utf16(u.as_slice()).unwrap(); - assert!(str::is_utf16(u.as_slice())); + assert!(::unicode::str::is_utf16(u.as_slice())); assert_eq!(s_as_utf16, u); assert_eq!(u_as_string, s); diff --git a/src/libcore/option.rs b/src/libcore/option.rs index 8adbba8b94b8..d831a57893bd 100644 --- a/src/libcore/option.rs +++ b/src/libcore/option.rs @@ -718,15 +718,15 @@ impl Option { /// # Example /// /// Convert a string to an integer, turning poorly-formed strings - /// into 0 (the default value for integers). `from_str` converts + /// into 0 (the default value for integers). `parse` converts /// a string to any other type that implements `FromStr`, returning /// `None` on error. /// /// ``` /// let good_year_from_input = "1909"; /// let bad_year_from_input = "190blarg"; - /// let good_year = from_str(good_year_from_input).unwrap_or_default(); - /// let bad_year = from_str(bad_year_from_input).unwrap_or_default(); + /// let good_year = good_year_from_input.parse().unwrap_or_default(); + /// let bad_year = bad_year_from_input.parse().unwrap_or_default(); /// /// assert_eq!(1909i, good_year); /// assert_eq!(0i, bad_year); diff --git a/src/libcore/result.rs b/src/libcore/result.rs index b59734a7d988..8014b4dc89d7 100644 --- a/src/libcore/result.rs +++ b/src/libcore/result.rs @@ -458,7 +458,7 @@ pub fn as_mut_slice(&mut self) -> &mut [T] { /// let line: IoResult = buffer.read_line(); /// // Convert the string line to a number using `map` and `from_str` /// let val: IoResult = line.map(|line| { - /// from_str::(line.as_slice().trim_right()).unwrap_or(0) + /// line.as_slice().trim_right().parse::().unwrap_or(0) /// }); /// // Add the value if there were no errors, otherwise add 0 /// sum += val.ok().unwrap_or(0); diff --git a/src/libcore/str.rs b/src/libcore/str.rs index 60d4262a9b12..bfccc1e3f735 100644 --- a/src/libcore/str.rs +++ b/src/libcore/str.rs @@ -18,14 +18,13 @@ use self::Searcher::{Naive, TwoWay, TwoWayLong}; -use char::{mod, Char}; use clone::Clone; use cmp::{mod, Eq}; use default::Default; use iter::range; use iter::{DoubleEndedIteratorExt, ExactSizeIterator}; use iter::{Map, Iterator, IteratorExt, DoubleEndedIterator}; -use kinds::{Copy, Sized}; +use kinds::Sized; use mem; use num::Int; use ops::{Fn, FnMut}; @@ -60,9 +59,9 @@ impl FromStr for bool { /// # Examples /// /// ```rust - /// assert_eq!(from_str::("true"), Some(true)); - /// assert_eq!(from_str::("false"), Some(false)); - /// assert_eq!(from_str::("not even a boolean"), None); + /// assert_eq!("true".parse(), Some(true)); + /// assert_eq!("false".parse(), Some(false)); + /// assert_eq!("not even a boolean".parse::(), None); /// ``` #[inline] fn from_str(s: &str) -> Option { @@ -79,6 +78,7 @@ fn from_str(s: &str) -> Option { */ /// Errors which can occur when attempting to interpret a byte slice as a `str`. +#[deriving(Copy, Eq, PartialEq, Clone)] pub enum Utf8Error { /// An invalid byte was detected at the byte offset given. /// @@ -334,6 +334,7 @@ fn next_back(&mut self) -> Option<(uint, char)> { /// External iterator for a string's bytes. /// Use with the `std::iter` module. #[stable] +#[deriving(Clone)] pub struct Bytes<'a> { inner: Map<&'a u8, u8, slice::Items<'a, u8>, BytesFn>, } @@ -946,24 +947,7 @@ pub fn is_utf8(v: &[u8]) -> bool { run_utf8_validation_iterator(&mut v.iter()).is_ok() } -/// Return a slice of `v` ending at (and not including) the first NUL -/// (0). -/// -/// # Example -/// -/// ```rust -/// use std::str; -/// -/// // "abcd" -/// let mut v = ['a' as u16, 'b' as u16, 'c' as u16, 'd' as u16]; -/// // no NULs so no change -/// assert_eq!(str::truncate_utf16_at_nul(&v), v.as_slice()); -/// -/// // "ab\0d" -/// v[2] = 0; -/// let b: &[_] = &['a' as u16, 'b' as u16]; -/// assert_eq!(str::truncate_utf16_at_nul(&v), b); -/// ``` +/// Deprecated function #[deprecated = "this function will be removed"] pub fn truncate_utf16_at_nul<'a>(v: &'a [u16]) -> &'a [u16] { match v.iter().position(|c| *c == 0) { @@ -1595,6 +1579,8 @@ fn default() -> &'a str { "" } impl<'a> Iterator<&'a str> for Lines<'a> { #[inline] fn next(&mut self) -> Option<&'a str> { self.inner.next() } + #[inline] + fn size_hint(&self) -> (uint, Option) { self.inner.size_hint() } } impl<'a> DoubleEndedIterator<&'a str> for Lines<'a> { #[inline] @@ -1603,6 +1589,8 @@ fn next_back(&mut self) -> Option<&'a str> { self.inner.next_back() } impl<'a> Iterator<&'a str> for LinesAny<'a> { #[inline] fn next(&mut self) -> Option<&'a str> { self.inner.next() } + #[inline] + fn size_hint(&self) -> (uint, Option) { self.inner.size_hint() } } impl<'a> DoubleEndedIterator<&'a str> for LinesAny<'a> { #[inline] @@ -1611,6 +1599,8 @@ fn next_back(&mut self) -> Option<&'a str> { self.inner.next_back() } impl<'a> Iterator for Bytes<'a> { #[inline] fn next(&mut self) -> Option { self.inner.next() } + #[inline] + fn size_hint(&self) -> (uint, Option) { self.inner.size_hint() } } impl<'a> DoubleEndedIterator for Bytes<'a> { #[inline] diff --git a/src/libcoretest/lib.rs b/src/libcoretest/lib.rs index 05d862d7bc7f..44029ebb7fa0 100644 --- a/src/libcoretest/lib.rs +++ b/src/libcoretest/lib.rs @@ -13,6 +13,7 @@ extern crate core; extern crate test; extern crate libc; +extern crate unicode; mod any; mod atomic; diff --git a/src/libcoretest/str.rs b/src/libcoretest/str.rs index 763fcccdbfdc..63d6e14a4a6b 100644 --- a/src/libcoretest/str.rs +++ b/src/libcoretest/str.rs @@ -117,7 +117,7 @@ fn test_rev_split_char_iterator_no_trailing() { #[test] fn test_utf16_code_units() { - use core::str::Utf16Encoder; + use unicode::str::Utf16Encoder; assert_eq!(Utf16Encoder::new(vec!['é', '\U0001F4A9'].into_iter()).collect::>(), vec![0xE9, 0xD83D, 0xDCA9]) } diff --git a/src/libfmt_macros/lib.rs b/src/libfmt_macros/lib.rs index 106e467c1691..c284fb7c9e33 100644 --- a/src/libfmt_macros/lib.rs +++ b/src/libfmt_macros/lib.rs @@ -23,7 +23,8 @@ html_root_url = "http://doc.rust-lang.org/nightly/", html_playground_url = "http://play.rust-lang.org/")] -#![feature(macro_rules, globs)] +#![feature(macro_rules, globs, slicing_syntax)] + pub use self::Piece::*; pub use self::Position::*; pub use self::Alignment::*; @@ -136,7 +137,7 @@ pub enum Count<'a> { /// necessary there's probably lots of room for improvement performance-wise. pub struct Parser<'a> { input: &'a str, - cur: str::CharOffsets<'a>, + cur: str::CharIndices<'a>, /// Error messages accumulated during parsing pub errors: Vec, } @@ -208,13 +209,11 @@ fn must_consume(&mut self, c: char) { self.cur.next(); } Some((_, other)) => { - self.err(format!("expected `{}`, found `{}`", - c, - other).as_slice()); + self.err(format!("expected `{}`, found `{}`", c, other)[]); } None => { self.err(format!("expected `{}` but string was terminated", - c).as_slice()); + c)[]); } } } @@ -237,12 +236,12 @@ fn string(&mut self, start: uint) -> &'a str { // we may not consume the character, so clone the iterator match self.cur.clone().next() { Some((pos, '}')) | Some((pos, '{')) => { - return self.input.slice(start, pos); + return self.input[start..pos]; } Some(..) => { self.cur.next(); } None => { self.cur.next(); - return self.input.slice(start, self.input.len()); + return self.input[start..self.input.len()]; } } } @@ -282,7 +281,7 @@ fn format(&mut self) -> FormatSpec<'a> { flags: 0, precision: CountImplied, width: CountImplied, - ty: self.input.slice(0, 0), + ty: self.input[0..0], }; if !self.consume(':') { return spec } @@ -391,7 +390,7 @@ fn word(&mut self) -> &'a str { self.cur.next(); pos } - Some(..) | None => { return self.input.slice(0, 0); } + Some(..) | None => { return self.input[0..0]; } }; let mut end; loop { @@ -403,7 +402,7 @@ fn word(&mut self) -> &'a str { None => { end = self.input.len(); break } } } - self.input.slice(start, end) + self.input[start..end] } /// Optionally parses an integer at the current position. This doesn't deal diff --git a/src/libgetopts/lib.rs b/src/libgetopts/lib.rs index e362c67cc509..0426f2693762 100644 --- a/src/libgetopts/lib.rs +++ b/src/libgetopts/lib.rs @@ -85,7 +85,7 @@ html_favicon_url = "http://www.rust-lang.org/favicon.ico", html_root_url = "http://doc.rust-lang.org/nightly/", html_playground_url = "http://play.rust-lang.org/")] -#![feature(globs, phase)] +#![feature(globs, phase, slicing_syntax)] #![feature(unboxed_closures)] #![deny(missing_docs)] @@ -101,9 +101,8 @@ use self::LengthLimit::*; use std::fmt; -use std::result::Result::{Err, Ok}; +use std::iter::repeat; use std::result; -use std::string::String; /// Name of an option. Either a string or a single char. #[deriving(Clone, PartialEq, Eq)] @@ -282,7 +281,7 @@ pub fn long_to_short(&self) -> Opt { impl Matches { fn opt_vals(&self, nm: &str) -> Vec { - match find_opt(self.opts.as_slice(), Name::from_str(nm)) { + match find_opt(self.opts[], Name::from_str(nm)) { Some(id) => self.vals[id].clone(), None => panic!("No option '{}' defined", nm) } @@ -310,8 +309,7 @@ pub fn opt_count(&self, nm: &str) -> uint { /// Returns true if any of several options were matched. pub fn opts_present(&self, names: &[String]) -> bool { for nm in names.iter() { - match find_opt(self.opts.as_slice(), - Name::from_str(nm.as_slice())) { + match find_opt(self.opts.as_slice(), Name::from_str(nm[])) { Some(id) if !self.vals[id].is_empty() => return true, _ => (), }; @@ -322,7 +320,7 @@ pub fn opts_present(&self, names: &[String]) -> bool { /// Returns the string argument supplied to one of several matching options or `None`. pub fn opts_str(&self, names: &[String]) -> Option { for nm in names.iter() { - match self.opt_val(nm.as_slice()) { + match self.opt_val(nm[]) { Some(Val(ref s)) => return Some(s.clone()), _ => () } @@ -587,7 +585,7 @@ pub fn getopts(args: &[String], optgrps: &[OptGroup]) -> Result { while i < l { let cur = args[i].clone(); let curlen = cur.len(); - if !is_arg(cur.as_slice()) { + if !is_arg(cur[]) { free.push(cur); } else if cur == "--" { let mut j = i + 1; @@ -597,7 +595,7 @@ pub fn getopts(args: &[String], optgrps: &[OptGroup]) -> Result { let mut names; let mut i_arg = None; if cur.as_bytes()[1] == b'-' { - let tail = cur.slice(2, curlen); + let tail = cur[2..curlen]; let tail_eq: Vec<&str> = tail.split('=').collect(); if tail_eq.len() <= 1 { names = vec!(Long(tail.to_string())); @@ -633,7 +631,7 @@ pub fn getopts(args: &[String], optgrps: &[OptGroup]) -> Result { }; if arg_follows && range.next < curlen { - i_arg = Some(cur.slice(range.next, curlen).to_string()); + i_arg = Some(cur[range.next..curlen].to_string()); break; } @@ -660,7 +658,7 @@ pub fn getopts(args: &[String], optgrps: &[OptGroup]) -> Result { .push(Val((i_arg.clone()) .unwrap())); } else if name_pos < names.len() || i + 1 == l || - is_arg(args[i + 1].as_slice()) { + is_arg(args[i + 1][]) { vals[optid].push(Given); } else { i += 1; @@ -702,7 +700,7 @@ pub fn getopts(args: &[String], optgrps: &[OptGroup]) -> Result { /// Derive a usage message from a set of long options. pub fn usage(brief: &str, opts: &[OptGroup]) -> String { - let desc_sep = format!("\n{}", " ".repeat(24)); + let desc_sep = format!("\n{}", repeat(" ").take(24).collect::()); let rows = opts.iter().map(|optref| { let OptGroup{short_name, @@ -712,14 +710,14 @@ pub fn usage(brief: &str, opts: &[OptGroup]) -> String { hasarg, ..} = (*optref).clone(); - let mut row = " ".repeat(4); + let mut row = repeat(" ").take(4).collect::(); // short option match short_name.len() { 0 => {} 1 => { row.push('-'); - row.push_str(short_name.as_slice()); + row.push_str(short_name[]); row.push(' '); } _ => panic!("the short name should only be 1 ascii char long"), @@ -730,7 +728,7 @@ pub fn usage(brief: &str, opts: &[OptGroup]) -> String { 0 => {} _ => { row.push_str("--"); - row.push_str(long_name.as_slice()); + row.push_str(long_name[]); row.push(' '); } } @@ -738,23 +736,23 @@ pub fn usage(brief: &str, opts: &[OptGroup]) -> String { // arg match hasarg { No => {} - Yes => row.push_str(hint.as_slice()), + Yes => row.push_str(hint[]), Maybe => { row.push('['); - row.push_str(hint.as_slice()); + row.push_str(hint[]); row.push(']'); } } // FIXME: #5516 should be graphemes not codepoints // here we just need to indent the start of the description - let rowlen = row.char_len(); + let rowlen = row.chars().count(); if rowlen < 24 { for _ in range(0, 24 - rowlen) { row.push(' '); } } else { - row.push_str(desc_sep.as_slice()) + row.push_str(desc_sep[]); } // Normalize desc to contain words separated by one space character @@ -766,16 +764,14 @@ pub fn usage(brief: &str, opts: &[OptGroup]) -> String { // FIXME: #5516 should be graphemes not codepoints let mut desc_rows = Vec::new(); - each_split_within(desc_normalized_whitespace.as_slice(), - 54, - |substr| { + each_split_within(desc_normalized_whitespace[], 54, |substr| { desc_rows.push(substr.to_string()); true }); // FIXME: #5516 should be graphemes not codepoints // wrapped description - row.push_str(desc_rows.connect(desc_sep.as_slice()).as_slice()); + row.push_str(desc_rows.connect(desc_sep[])[]); row }); @@ -794,10 +790,10 @@ fn format_option(opt: &OptGroup) -> String { // Use short_name is possible, but fallback to long_name. if opt.short_name.len() > 0 { line.push('-'); - line.push_str(opt.short_name.as_slice()); + line.push_str(opt.short_name[]); } else { line.push_str("--"); - line.push_str(opt.long_name.as_slice()); + line.push_str(opt.long_name[]); } if opt.hasarg != No { @@ -805,7 +801,7 @@ fn format_option(opt: &OptGroup) -> String { if opt.hasarg == Maybe { line.push('['); } - line.push_str(opt.hint.as_slice()); + line.push_str(opt.hint[]); if opt.hasarg == Maybe { line.push(']'); } @@ -827,8 +823,7 @@ pub fn short_usage(program_name: &str, opts: &[OptGroup]) -> String { line.push_str(opts.iter() .map(format_option) .collect::>() - .connect(" ") - .as_slice()); + .connect(" ")[]); line } @@ -891,9 +886,9 @@ fn each_split_within(ss: &str, lim: uint, mut it: F) -> bool where (B, Cr, UnderLim) => { B } (B, Cr, OverLim) if (i - last_start + 1) > lim => panic!("word starting with {} longer than limit!", - ss.slice(last_start, i + 1)), + ss[last_start..i + 1]), (B, Cr, OverLim) => { - *cont = it(ss.slice(slice_start, last_end)); + *cont = it(ss[slice_start..last_end]); slice_start = last_start; B } @@ -903,7 +898,7 @@ fn each_split_within(ss: &str, lim: uint, mut it: F) -> bool where } (B, Ws, OverLim) => { last_end = i; - *cont = it(ss.slice(slice_start, last_end)); + *cont = it(ss[slice_start..last_end]); A } @@ -912,14 +907,14 @@ fn each_split_within(ss: &str, lim: uint, mut it: F) -> bool where B } (C, Cr, OverLim) => { - *cont = it(ss.slice(slice_start, last_end)); + *cont = it(ss[slice_start..last_end]); slice_start = i; last_start = i; last_end = i; B } (C, Ws, OverLim) => { - *cont = it(ss.slice(slice_start, last_end)); + *cont = it(ss[slice_start..last_end]); A } (C, Ws, UnderLim) => { diff --git a/src/libgraphviz/lib.rs b/src/libgraphviz/lib.rs index 34e19aa4a03e..ce3df1090bd5 100644 --- a/src/libgraphviz/lib.rs +++ b/src/libgraphviz/lib.rs @@ -448,8 +448,8 @@ fn escape_str(s: &str) -> String { /// Renders text as string suitable for a label in a .dot file. pub fn escape(&self) -> String { match self { - &LabelStr(ref s) => (&**s).escape_default(), - &EscStr(ref s) => LabelText::escape_str(s.as_slice()), + &LabelStr(ref s) => s.escape_default(), + &EscStr(ref s) => LabelText::escape_str(s[]), } } @@ -475,10 +475,10 @@ pub fn prefix_line(self, prefix: LabelText) -> LabelText<'static> { /// Puts `suffix` on a line below this label, with a blank line separator. pub fn suffix_line(self, suffix: LabelText) -> LabelText<'static> { - let mut prefix = self.pre_escaped_content().into_string(); + let mut prefix = self.pre_escaped_content().into_owned(); let suffix = suffix.pre_escaped_content(); prefix.push_str(r"\n\n"); - prefix.push_str(suffix.as_slice()); + prefix.push_str(suffix[]); EscStr(prefix.into_cow()) } } @@ -671,7 +671,7 @@ fn id_name<'a>(n: &Node) -> Id<'a> { impl<'a> Labeller<'a, Node, &'a Edge> for LabelledGraph { fn graph_id(&'a self) -> Id<'a> { - Id::new(self.name.as_slice()).unwrap() + Id::new(self.name[]).unwrap() } fn node_id(&'a self, n: &Node) -> Id<'a> { id_name(n) @@ -735,7 +735,7 @@ fn target(&'a self, edge: & &'a Edge) -> Node { fn test_input(g: LabelledGraph) -> IoResult { let mut writer = Vec::new(); render(&g, &mut writer).unwrap(); - (&mut writer.as_slice()).read_to_string() + (&mut writer[]).read_to_string() } // All of the tests use raw-strings as the format for the expected outputs, @@ -847,7 +847,7 @@ fn left_aligned_text() { edge(1, 3, ";"), edge(2, 3, ";" ))); render(&g, &mut writer).unwrap(); - let r = (&mut writer.as_slice()).read_to_string(); + let r = (&mut writer[]).read_to_string(); assert_eq!(r.unwrap(), r#"digraph syntax_tree { diff --git a/src/liblog/directive.rs b/src/liblog/directive.rs index d1db0ec89a16..2b25a64affee 100644 --- a/src/liblog/directive.rs +++ b/src/liblog/directive.rs @@ -23,7 +23,7 @@ pub struct LogDirective { /// Parse an individual log level that is either a number or a symbolic log level fn parse_log_level(level: &str) -> Option { - from_str::(level).or_else(|| { + level.parse::().or_else(|| { let pos = LOG_LEVEL_NAMES.iter().position(|&name| name.eq_ignore_ascii_case(level)); pos.map(|p| p as u32 + 1) }).map(|p| cmp::min(p, ::MAX_LOG_LEVEL)) diff --git a/src/liblog/lib.rs b/src/liblog/lib.rs index 2bf9af902718..bc655c219f32 100644 --- a/src/liblog/lib.rs +++ b/src/liblog/lib.rs @@ -164,7 +164,7 @@ html_favicon_url = "http://www.rust-lang.org/favicon.ico", html_root_url = "http://doc.rust-lang.org/nightly/", html_playground_url = "http://play.rust-lang.org/")] -#![feature(macro_rules, unboxed_closures)] +#![feature(macro_rules, unboxed_closures, slicing_syntax)] #![deny(missing_docs)] extern crate regex; @@ -280,7 +280,7 @@ pub fn log(level: u32, loc: &'static LogLocation, args: &fmt::Arguments) { // Test the literal string from args against the current filter, if there // is one. match unsafe { FILTER.as_ref() } { - Some(filter) if !filter.is_match(args.to_string().as_slice()) => return, + Some(filter) if !filter.is_match(args.to_string()[]) => return, _ => {} } @@ -375,7 +375,7 @@ fn enabled(level: u32, // Search for the longest match, the vector is assumed to be pre-sorted. for directive in iter.rev() { match directive.name { - Some(ref name) if !module.starts_with(name.as_slice()) => {}, + Some(ref name) if !module.starts_with(name[]) => {}, Some(..) | None => { return level <= directive.level } @@ -390,7 +390,7 @@ fn enabled(level: u32, /// `Once` primitive (and this function is called from that primitive). fn init() { let (mut directives, filter) = match os::getenv("RUST_LOG") { - Some(spec) => directive::parse_logging_spec(spec.as_slice()), + Some(spec) => directive::parse_logging_spec(spec[]), None => (Vec::new(), None), }; diff --git a/src/libregex/parse.rs b/src/libregex/parse.rs index 78558a322665..0cd8df73c37c 100644 --- a/src/libregex/parse.rs +++ b/src/libregex/parse.rs @@ -286,7 +286,7 @@ fn noteof(&mut self, expected: &str) -> Result<(), Error> { true => Ok(()), false => { self.err(format!("Expected {} but got EOF.", - expected).as_slice()) + expected)[]) } } } @@ -295,10 +295,10 @@ fn expect(&mut self, expected: char) -> Result<(), Error> { match self.next_char() { true if self.cur() == expected => Ok(()), true => self.err(format!("Expected '{}' but got '{}'.", - expected, self.cur()).as_slice()), + expected, self.cur())[]), false => { self.err(format!("Expected '{}' but got EOF.", - expected).as_slice()) + expected)[]) } } } @@ -443,14 +443,14 @@ fn parse_class(&mut self) -> Result<(), Error> { Literal(c3, _) => c2 = c3, // allow literal escapes below ast => return self.err(format!("Expected a literal, but got {}.", - ast).as_slice()), + ast)[]), } } if c2 < c { return self.err(format!("Invalid character class \ range '{}-{}'", c, - c2).as_slice()) + c2)[]) } ranges.push((c, self.cur())) } else { @@ -488,7 +488,7 @@ fn try_parse_ascii(&mut self) -> Option { FLAG_EMPTY }; let name = self.slice(name_start, closer - 1); - match find_class(ASCII_CLASSES, name.as_slice()) { + match find_class(ASCII_CLASSES, name[]) { None => None, Some(ranges) => { self.chari = closer; @@ -513,7 +513,7 @@ fn parse_counted(&mut self) -> Result<(), Error> { return self.err(format!("No closing brace for counted \ repetition starting at position \ {}.", - start).as_slice()) + start)[]) } }; self.chari = closer; @@ -524,7 +524,7 @@ fn parse_counted(&mut self) -> Result<(), Error> { // Parse the min and max values from the regex. let (mut min, mut max): (uint, Option); if !inner.contains(",") { - min = try!(self.parse_uint(inner.as_slice())); + min = try!(self.parse_uint(inner[])); max = Some(min); } else { let pieces: Vec<&str> = inner.splitn(1, ',').collect(); @@ -546,19 +546,19 @@ fn parse_counted(&mut self) -> Result<(), Error> { if min > MAX_REPEAT { return self.err(format!( "{} exceeds maximum allowed repetitions ({})", - min, MAX_REPEAT).as_slice()); + min, MAX_REPEAT)[]); } if max.is_some() { let m = max.unwrap(); if m > MAX_REPEAT { return self.err(format!( "{} exceeds maximum allowed repetitions ({})", - m, MAX_REPEAT).as_slice()); + m, MAX_REPEAT)[]); } if m < min { return self.err(format!( "Max repetitions ({}) cannot be smaller than min \ - repetitions ({}).", m, min).as_slice()); + repetitions ({}).", m, min)[]); } } @@ -622,8 +622,7 @@ fn parse_escape(&mut self) -> Result { Ok(AstClass(ranges, flags)) } _ => { - self.err(format!("Invalid escape sequence '\\\\{}'", - c).as_slice()) + self.err(format!("Invalid escape sequence '\\\\{}'", c)[]) } } } @@ -643,7 +642,7 @@ fn parse_unicode_name(&mut self) -> Result { Some(i) => i, None => return self.err(format!( "Missing '}}' for unclosed '{{' at position {}", - self.chari).as_slice()), + self.chari)[]), }; if closer - self.chari + 1 == 0 { return self.err("No Unicode class name found.") @@ -657,10 +656,10 @@ fn parse_unicode_name(&mut self) -> Result { name = self.slice(self.chari + 1, self.chari + 2); self.chari += 1; } - match find_class(UNICODE_CLASSES, name.as_slice()) { + match find_class(UNICODE_CLASSES, name[]) { None => { return self.err(format!("Could not find Unicode class '{}'", - name).as_slice()) + name)[]) } Some(ranges) => { Ok(AstClass(ranges, negated | (self.flags & FLAG_NOCASE))) @@ -683,11 +682,11 @@ fn parse_octal(&mut self) -> Result { } } let s = self.slice(start, end); - match num::from_str_radix::(s.as_slice(), 8) { + match num::from_str_radix::(s[], 8) { Some(n) => Ok(Literal(try!(self.char_from_u32(n)), FLAG_EMPTY)), None => { self.err(format!("Could not parse '{}' as octal number.", - s).as_slice()) + s)[]) } } } @@ -705,12 +704,12 @@ fn parse_hex(&mut self) -> Result { None => { return self.err(format!("Missing '}}' for unclosed \ '{{' at position {}", - start).as_slice()) + start)[]) } Some(i) => i, }; self.chari = closer; - self.parse_hex_digits(self.slice(start, closer).as_slice()) + self.parse_hex_digits(self.slice(start, closer)[]) } // Parses a two-digit hex number. @@ -730,8 +729,7 @@ fn parse_hex_digits(&self, s: &str) -> Result { match num::from_str_radix::(s, 16) { Some(n) => Ok(Literal(try!(self.char_from_u32(n)), FLAG_EMPTY)), None => { - self.err(format!("Could not parse '{}' as hex number.", - s).as_slice()) + self.err(format!("Could not parse '{}' as hex number.", s)[]) } } } @@ -757,7 +755,7 @@ fn parse_named_capture(&mut self) -> Result<(), Error> { } if self.names.contains(&name) { return self.err(format!("Duplicate capture group name '{}'.", - name).as_slice()) + name)[]) } self.names.push(name.clone()); self.chari = closer; @@ -791,7 +789,7 @@ fn parse_group_opts(&mut self) -> Result<(), Error> { if sign < 0 { return self.err(format!( "Cannot negate flags twice in '{}'.", - self.slice(start, self.chari + 1)).as_slice()) + self.slice(start, self.chari + 1))[]) } sign = -1; saw_flag = false; @@ -802,7 +800,7 @@ fn parse_group_opts(&mut self) -> Result<(), Error> { if !saw_flag { return self.err(format!( "A valid flag does not follow negation in '{}'", - self.slice(start, self.chari + 1)).as_slice()) + self.slice(start, self.chari + 1))[]) } flags = flags ^ flags; } @@ -814,7 +812,7 @@ fn parse_group_opts(&mut self) -> Result<(), Error> { return Ok(()) } _ => return self.err(format!( - "Unrecognized flag '{}'.", self.cur()).as_slice()), + "Unrecognized flag '{}'.", self.cur())[]), } } } @@ -908,11 +906,11 @@ fn build_from(&mut self, from: uint, mut mk: F) -> Result where } fn parse_uint(&self, s: &str) -> Result { - match from_str::(s) { + match s.parse::() { Some(i) => Ok(i), None => { self.err(format!("Expected an unsigned integer but got '{}'.", - s).as_slice()) + s)[]) } } } @@ -922,8 +920,7 @@ fn char_from_u32(&self, n: u32) -> Result { Some(c) => Ok(c), None => { self.err(format!("Could not decode '{}' to unicode \ - character.", - n).as_slice()) + character.", n)[]) } } } diff --git a/src/libregex/re.rs b/src/libregex/re.rs index 151587e423ab..4e23e92c77ea 100644 --- a/src/libregex/re.rs +++ b/src/libregex/re.rs @@ -417,7 +417,7 @@ pub fn splitn<'r, 't>(&'r self, text: &'t str, limit: uint) /// # extern crate regex; #[phase(plugin)] extern crate regex_macros; /// # fn main() { /// let re = regex!("[^01]+"); - /// assert_eq!(re.replace("1078910", "").as_slice(), "1010"); + /// assert_eq!(re.replace("1078910", ""), "1010"); /// # } /// ``` /// @@ -435,7 +435,7 @@ pub fn splitn<'r, 't>(&'r self, text: &'t str, limit: uint) /// let result = re.replace("Springsteen, Bruce", |&: caps: &Captures| { /// format!("{} {}", caps.at(2).unwrap_or(""), caps.at(1).unwrap_or("")) /// }); - /// assert_eq!(result.as_slice(), "Bruce Springsteen"); + /// assert_eq!(result, "Bruce Springsteen"); /// # } /// ``` /// @@ -450,7 +450,7 @@ pub fn splitn<'r, 't>(&'r self, text: &'t str, limit: uint) /// # fn main() { /// let re = regex!(r"(?P[^,\s]+),\s+(?P\S+)"); /// let result = re.replace("Springsteen, Bruce", "$first $last"); - /// assert_eq!(result.as_slice(), "Bruce Springsteen"); + /// assert_eq!(result, "Bruce Springsteen"); /// # } /// ``` /// @@ -469,7 +469,7 @@ pub fn splitn<'r, 't>(&'r self, text: &'t str, limit: uint) /// /// let re = regex!(r"(?P[^,\s]+),\s+(\S+)"); /// let result = re.replace("Springsteen, Bruce", NoExpand("$2 $last")); - /// assert_eq!(result.as_slice(), "$2 $last"); + /// assert_eq!(result, "$2 $last"); /// # } /// ``` pub fn replace(&self, text: &str, rep: R) -> String { @@ -505,19 +505,19 @@ pub fn replacen } let (s, e) = cap.pos(0).unwrap(); // captures only reports matches - new.push_str(text.slice(last_match, s)); - new.push_str(rep.reg_replace(&cap).as_slice()); + new.push_str(text[last_match..s]); + new.push_str(rep.reg_replace(&cap)[]); last_match = e; } - new.push_str(text.slice(last_match, text.len())); + new.push_str(text[last_match..text.len()]); return new; } /// Returns the original string of this regex. pub fn as_str<'a>(&'a self) -> &'a str { match *self { - Dynamic(ExDynamic { ref original, .. }) => original.as_slice(), - Native(ExNative { ref original, .. }) => original.as_slice(), + Dynamic(ExDynamic { ref original, .. }) => original[], + Native(ExNative { ref original, .. }) => original[], } } @@ -608,13 +608,13 @@ fn next(&mut self) -> Option<&'t str> { if self.last >= text.len() { None } else { - let s = text.slice(self.last, text.len()); + let s = text[self.last..text.len()]; self.last = text.len(); Some(s) } } Some((s, e)) => { - let matched = text.slice(self.last, s); + let matched = text[self.last..s]; self.last = e; Some(matched) } @@ -642,7 +642,7 @@ fn next(&mut self) -> Option<&'t str> { } else { self.cur += 1; if self.cur >= self.limit { - Some(text.slice(self.splits.last, text.len())) + Some(text[self.splits.last..text.len()]) } else { self.splits.next() } @@ -769,13 +769,13 @@ pub fn expand(&self, text: &str) -> String { let pre = refs.at(1).unwrap_or(""); let name = refs.at(2).unwrap_or(""); format!("{}{}", pre, - match from_str::(name.as_slice()) { + match name.parse::() { None => self.name(name).unwrap_or("").to_string(), Some(i) => self.at(i).unwrap_or("").to_string(), }) }); let re = Regex::new(r"\$\$").unwrap(); - re.replace_all(text.as_slice(), NoExpand("$")) + re.replace_all(text[], NoExpand("$")) } /// Returns the number of captured groups. diff --git a/src/librustc/lint/builtin.rs b/src/librustc/lint/builtin.rs index fddd49c8d88f..0fd69ea25bc0 100644 --- a/src/librustc/lint/builtin.rs +++ b/src/librustc/lint/builtin.rs @@ -250,10 +250,12 @@ fn check_expr(&mut self, cx: &Context, e: &ast::Expr) { let (min, max) = float_ty_range(t); let lit_val: f64 = match lit.node { ast::LitFloat(ref v, _) | - ast::LitFloatUnsuffixed(ref v) => match from_str(v.get()) { - Some(f) => f, - None => return - }, + ast::LitFloatUnsuffixed(ref v) => { + match v.parse() { + Some(f) => f, + None => return + } + } _ => panic!() }; if lit_val < min || lit_val > max { @@ -507,7 +509,7 @@ fn check_heap_type<'a, 'tcx>(&self, cx: &Context<'a, 'tcx>, if n_uniq > 0 { let s = ty_to_string(cx.tcx, ty); let m = format!("type uses owned (Box type) pointers: {}", s); - cx.span_lint(BOX_POINTERS, span, m.as_slice()); + cx.span_lint(BOX_POINTERS, span, m[]); } } } @@ -587,7 +589,7 @@ fn get_lints(&self) -> LintArray { } fn check_item(&mut self, cx: &Context, item: &ast::Item) { - if !attr::contains_name(item.attrs.as_slice(), "automatically_derived") { + if !attr::contains_name(item.attrs[], "automatically_derived") { return } let did = match item.node { @@ -766,11 +768,11 @@ fn check_stmt(&mut self, cx: &Context, s: &ast::Stmt) { ty::ty_enum(did, _) => { if ast_util::is_local(did) { if let ast_map::NodeItem(it) = cx.tcx.map.get(did.node) { - warned |= check_must_use(cx, it.attrs.as_slice(), s.span); + warned |= check_must_use(cx, it.attrs[], s.span); } } else { csearch::get_item_attrs(&cx.sess().cstore, did, |attrs| { - warned |= check_must_use(cx, attrs.as_slice(), s.span); + warned |= check_must_use(cx, attrs[], s.span); }); } } @@ -792,7 +794,7 @@ fn check_must_use(cx: &Context, attrs: &[ast::Attribute], sp: Span) -> bool { msg.push_str(s.get()); } } - cx.span_lint(UNUSED_MUST_USE, sp, msg.as_slice()); + cx.span_lint(UNUSED_MUST_USE, sp, msg[]); return true; } } @@ -838,7 +840,7 @@ fn to_camel_case(s: &str) -> String { } else { format!("{} `{}` should have a camel case name such as `{}`", sort, s, c) }; - cx.span_lint(NON_CAMEL_CASE_TYPES, span, m.as_slice()); + cx.span_lint(NON_CAMEL_CASE_TYPES, span, m[]); } } } @@ -978,7 +980,7 @@ fn to_snake_case(str: &str) -> String { if !is_snake_case(ident) { cx.span_lint(NON_SNAKE_CASE, span, format!("{} `{}` should have a snake case name such as `{}`", - sort, s, to_snake_case(s.get())).as_slice()); + sort, s, to_snake_case(s.get()))[]); } } } @@ -1065,7 +1067,7 @@ fn check_item(&mut self, cx: &Context, it: &ast::Item) { format!("static constant `{}` should have an uppercase name \ such as `{}`", s.get(), s.get().chars().map(|c| c.to_uppercase()) - .collect::().as_slice()).as_slice()); + .collect::()[])[]); } } _ => {} @@ -1082,7 +1084,7 @@ fn check_pat(&mut self, cx: &Context, p: &ast::Pat) { format!("static constant in pattern `{}` should have an uppercase \ name such as `{}`", s.get(), s.get().chars().map(|c| c.to_uppercase()) - .collect::().as_slice()).as_slice()); + .collect::()[])[]); } } _ => {} @@ -1107,7 +1109,7 @@ fn check_unused_parens_core(&self, cx: &Context, value: &ast::Expr, msg: &str, if !necessary { cx.span_lint(UNUSED_PARENS, value.span, format!("unnecessary parentheses around {}", - msg).as_slice()) + msg)[]) } } @@ -1209,7 +1211,7 @@ fn check_view_item(&mut self, cx: &Context, view_item: &ast::ViewItem) { let m = format!("braces around {} is unnecessary", token::get_ident(*name).get()); cx.span_lint(UNUSED_IMPORT_BRACES, view_item.span, - m.as_slice()); + m[]); }, _ => () } @@ -1248,7 +1250,7 @@ fn check_pat(&mut self, cx: &Context, pat: &ast::Pat) { if ident.node.as_str() == fieldpat.node.ident.as_str() { cx.span_lint(NON_SHORTHAND_FIELD_PATTERNS, fieldpat.span, format!("the `{}:` in this pattern is redundant and can \ - be removed", ident.node.as_str()).as_slice()) + be removed", ident.node.as_str())[]) } } } @@ -1352,7 +1354,7 @@ fn get_lints(&self) -> LintArray { fn check_expr(&mut self, cx: &Context, e: &ast::Expr) { if let ast::ExprMatch(_, ref arms, _) = e.node { for a in arms.iter() { - self.check_unused_mut_pat(cx, a.pats.as_slice()) + self.check_unused_mut_pat(cx, a.pats[]) } } } @@ -1473,7 +1475,7 @@ fn check_missing_docs_attrs(&self, }); if !has_doc { cx.span_lint(MISSING_DOCS, sp, - format!("missing documentation for {}", desc).as_slice()); + format!("missing documentation for {}", desc)[]); } } } @@ -1487,7 +1489,7 @@ fn enter_lint_attrs(&mut self, _: &Context, attrs: &[ast::Attribute]) { let doc_hidden = self.doc_hidden() || attrs.iter().any(|attr| { attr.check_name("doc") && match attr.meta_item_list() { None => false, - Some(l) => attr::contains_name(l.as_slice(), "hidden"), + Some(l) => attr::contains_name(l[], "hidden"), } }); self.doc_hidden_stack.push(doc_hidden); @@ -1509,7 +1511,7 @@ fn check_struct_def_post(&mut self, _: &Context, } fn check_crate(&mut self, cx: &Context, krate: &ast::Crate) { - self.check_missing_docs_attrs(cx, None, krate.attrs.as_slice(), + self.check_missing_docs_attrs(cx, None, krate.attrs[], krate.span, "crate"); } @@ -1523,7 +1525,7 @@ fn check_item(&mut self, cx: &Context, it: &ast::Item) { ast::ItemTy(..) => "a type alias", _ => return }; - self.check_missing_docs_attrs(cx, Some(it.id), it.attrs.as_slice(), + self.check_missing_docs_attrs(cx, Some(it.id), it.attrs[], it.span, desc); } @@ -1536,13 +1538,13 @@ fn check_fn(&mut self, cx: &Context, // Otherwise, doc according to privacy. This will also check // doc for default methods defined on traits. - self.check_missing_docs_attrs(cx, Some(m.id), m.attrs.as_slice(), + self.check_missing_docs_attrs(cx, Some(m.id), m.attrs[], m.span, "a method"); } } fn check_ty_method(&mut self, cx: &Context, tm: &ast::TypeMethod) { - self.check_missing_docs_attrs(cx, Some(tm.id), tm.attrs.as_slice(), + self.check_missing_docs_attrs(cx, Some(tm.id), tm.attrs[], tm.span, "a type method"); } @@ -1552,14 +1554,14 @@ fn check_struct_field(&mut self, cx: &Context, sf: &ast::StructField) { let cur_struct_def = *self.struct_def_stack.last() .expect("empty struct_def_stack"); self.check_missing_docs_attrs(cx, Some(cur_struct_def), - sf.node.attrs.as_slice(), sf.span, + sf.node.attrs[], sf.span, "a struct field") } } } fn check_variant(&mut self, cx: &Context, v: &ast::Variant, _: &ast::Generics) { - self.check_missing_docs_attrs(cx, Some(v.node.id), v.node.attrs.as_slice(), + self.check_missing_docs_attrs(cx, Some(v.node.id), v.node.attrs[], v.span, "a variant"); assert!(!self.in_variant); self.in_variant = true; @@ -1675,7 +1677,7 @@ fn lint(&self, cx: &Context, id: ast::DefId, span: Span) { _ => format!("use of {} item", label) }; - cx.span_lint(lint, span, msg.as_slice()); + cx.span_lint(lint, span, msg[]); } fn is_internal(&self, cx: &Context, span: Span) -> bool { diff --git a/src/librustc/lint/context.rs b/src/librustc/lint/context.rs index d8d9d653e62f..ffae485364a8 100644 --- a/src/librustc/lint/context.rs +++ b/src/librustc/lint/context.rs @@ -104,7 +104,7 @@ pub fn new() -> LintStore { } pub fn get_lints<'t>(&'t self) -> &'t [(&'static Lint, bool)] { - self.lints.as_slice() + self.lints[] } pub fn get_lint_groups<'t>(&'t self) -> Vec<(&'static str, Vec, bool)> { @@ -124,11 +124,11 @@ pub fn register_pass(&mut self, sess: Option<&Session>, match (sess, from_plugin) { // We load builtin lints first, so a duplicate is a compiler bug. // Use early_error when handling -W help with no crate. - (None, _) => early_error(msg.as_slice()), - (Some(sess), false) => sess.bug(msg.as_slice()), + (None, _) => early_error(msg[]), + (Some(sess), false) => sess.bug(msg[]), // A duplicate name from a plugin is a user error. - (Some(sess), true) => sess.err(msg.as_slice()), + (Some(sess), true) => sess.err(msg[]), } } @@ -149,11 +149,11 @@ pub fn register_group(&mut self, sess: Option<&Session>, match (sess, from_plugin) { // We load builtin lints first, so a duplicate is a compiler bug. // Use early_error when handling -W help with no crate. - (None, _) => early_error(msg.as_slice()), - (Some(sess), false) => sess.bug(msg.as_slice()), + (None, _) => early_error(msg[]), + (Some(sess), false) => sess.bug(msg[]), // A duplicate name from a plugin is a user error. - (Some(sess), true) => sess.err(msg.as_slice()), + (Some(sess), true) => sess.err(msg[]), } } } @@ -260,8 +260,8 @@ fn find_lint(&self, lint_name: &str, sess: &Session, span: Option) let warning = format!("lint {} has been renamed to {}", lint_name, new_name); match span { - Some(span) => sess.span_warn(span, warning.as_slice()), - None => sess.warn(warning.as_slice()), + Some(span) => sess.span_warn(span, warning[]), + None => sess.warn(warning[]), }; Some(lint_id) } @@ -271,13 +271,13 @@ fn find_lint(&self, lint_name: &str, sess: &Session, span: Option) pub fn process_command_line(&mut self, sess: &Session) { for &(ref lint_name, level) in sess.opts.lint_opts.iter() { - match self.find_lint(lint_name.as_slice(), sess, None) { + match self.find_lint(lint_name[], sess, None) { Some(lint_id) => self.set_level(lint_id, (level, CommandLine)), None => { match self.lint_groups.iter().map(|(&x, pair)| (x, pair.0.clone())) .collect::>>() - .get(lint_name.as_slice()) { + .get(lint_name[]) { Some(v) => { v.iter() .map(|lint_id: &LintId| @@ -285,7 +285,7 @@ pub fn process_command_line(&mut self, sess: &Session) { .collect::>(); } None => sess.err(format!("unknown {} flag: {}", - level.as_str(), lint_name).as_slice()), + level.as_str(), lint_name)[]), } } } @@ -396,10 +396,10 @@ pub fn raw_emit_lint(sess: &Session, lint: &'static Lint, if level == Forbid { level = Deny; } match (level, span) { - (Warn, Some(sp)) => sess.span_warn(sp, msg.as_slice()), - (Warn, None) => sess.warn(msg.as_slice()), - (Deny, Some(sp)) => sess.span_err(sp, msg.as_slice()), - (Deny, None) => sess.err(msg.as_slice()), + (Warn, Some(sp)) => sess.span_warn(sp, msg[]), + (Warn, None) => sess.warn(msg[]), + (Deny, Some(sp)) => sess.span_err(sp, msg[]), + (Deny, None) => sess.err(msg[]), _ => sess.bug("impossible level in raw_emit_lint"), } @@ -492,7 +492,7 @@ fn with_lint_attrs(&mut self, None => { self.span_lint(builtin::UNKNOWN_LINTS, span, format!("unknown `{}` attribute: `{}`", - level.as_str(), lint_name).as_slice()); + level.as_str(), lint_name)[]); continue; } } @@ -508,7 +508,7 @@ fn with_lint_attrs(&mut self, self.tcx.sess.span_err(span, format!("{}({}) overruled by outer forbid({})", level.as_str(), lint_name, - lint_name).as_slice()); + lint_name)[]); } else if now != level { let src = self.lints.get_level_source(lint_id).1; self.level_stack.push((lint_id, (now, src))); @@ -543,7 +543,7 @@ fn visit_ids(&mut self, f: F) where impl<'a, 'tcx, 'v> Visitor<'v> for Context<'a, 'tcx> { fn visit_item(&mut self, it: &ast::Item) { - self.with_lint_attrs(it.attrs.as_slice(), |cx| { + self.with_lint_attrs(it.attrs[], |cx| { run_lints!(cx, check_item, it); cx.visit_ids(|v| v.visit_item(it)); visit::walk_item(cx, it); @@ -551,14 +551,14 @@ fn visit_item(&mut self, it: &ast::Item) { } fn visit_foreign_item(&mut self, it: &ast::ForeignItem) { - self.with_lint_attrs(it.attrs.as_slice(), |cx| { + self.with_lint_attrs(it.attrs[], |cx| { run_lints!(cx, check_foreign_item, it); visit::walk_foreign_item(cx, it); }) } fn visit_view_item(&mut self, i: &ast::ViewItem) { - self.with_lint_attrs(i.attrs.as_slice(), |cx| { + self.with_lint_attrs(i.attrs[], |cx| { run_lints!(cx, check_view_item, i); cx.visit_ids(|v| v.visit_view_item(i)); visit::walk_view_item(cx, i); @@ -584,7 +584,7 @@ fn visit_fn(&mut self, fk: FnKind<'v>, decl: &'v ast::FnDecl, body: &'v ast::Block, span: Span, id: ast::NodeId) { match fk { visit::FkMethod(_, _, m) => { - self.with_lint_attrs(m.attrs.as_slice(), |cx| { + self.with_lint_attrs(m.attrs[], |cx| { run_lints!(cx, check_fn, fk, decl, body, span, id); cx.visit_ids(|v| { v.visit_fn(fk, decl, body, span, id); @@ -600,7 +600,7 @@ fn visit_fn(&mut self, fk: FnKind<'v>, decl: &'v ast::FnDecl, } fn visit_ty_method(&mut self, t: &ast::TypeMethod) { - self.with_lint_attrs(t.attrs.as_slice(), |cx| { + self.with_lint_attrs(t.attrs[], |cx| { run_lints!(cx, check_ty_method, t); visit::walk_ty_method(cx, t); }) @@ -617,14 +617,14 @@ fn visit_struct_def(&mut self, } fn visit_struct_field(&mut self, s: &ast::StructField) { - self.with_lint_attrs(s.node.attrs.as_slice(), |cx| { + self.with_lint_attrs(s.node.attrs[], |cx| { run_lints!(cx, check_struct_field, s); visit::walk_struct_field(cx, s); }) } fn visit_variant(&mut self, v: &ast::Variant, g: &ast::Generics) { - self.with_lint_attrs(v.node.attrs.as_slice(), |cx| { + self.with_lint_attrs(v.node.attrs[], |cx| { run_lints!(cx, check_variant, v, g); visit::walk_variant(cx, v, g); run_lints!(cx, check_variant_post, v, g); @@ -718,7 +718,7 @@ fn visit_id(&mut self, id: ast::NodeId) { None => {} Some(lints) => { for (lint_id, span, msg) in lints.into_iter() { - self.span_lint(lint_id.lint, span, msg.as_slice()) + self.span_lint(lint_id.lint, span, msg[]) } } } @@ -764,7 +764,7 @@ pub fn check_crate(tcx: &ty::ctxt, let mut cx = Context::new(tcx, krate, exported_items); // Visit the whole crate. - cx.with_lint_attrs(krate.attrs.as_slice(), |cx| { + cx.with_lint_attrs(krate.attrs[], |cx| { cx.visit_id(ast::CRATE_NODE_ID); cx.visit_ids(|v| { v.visited_outermost = true; @@ -784,7 +784,7 @@ pub fn check_crate(tcx: &ty::ctxt, for &(lint, span, ref msg) in v.iter() { tcx.sess.span_bug(span, format!("unprocessed lint {} at {}: {}", - lint.as_str(), tcx.map.node_to_string(*id), *msg).as_slice()) + lint.as_str(), tcx.map.node_to_string(*id), *msg)[]) } } diff --git a/src/librustc/metadata/creader.rs b/src/librustc/metadata/creader.rs index 323b084afdc3..98b57511957f 100644 --- a/src/librustc/metadata/creader.rs +++ b/src/librustc/metadata/creader.rs @@ -95,11 +95,11 @@ fn warn_if_multiple_versions(diag: &SpanHandler, cstore: &CStore) { for (name, dupes) in map.into_iter() { if dupes.len() == 1 { continue } diag.handler().warn( - format!("using multiple versions of crate `{}`", name).as_slice()); + format!("using multiple versions of crate `{}`", name)[]); for dupe in dupes.into_iter() { let data = cstore.get_crate_data(dupe); diag.span_note(data.span, "used here"); - loader::note_crate_name(diag, data.name().as_slice()); + loader::note_crate_name(diag, data.name()[]); } } } @@ -117,7 +117,7 @@ fn should_link(i: &ast::ViewItem) -> bool { i.attrs.iter().all(|attr| { attr.name().get() != "phase" || attr.meta_item_list().map_or(false, |phases| { - attr::contains_name(phases.as_slice(), "link") + attr::contains_name(phases[], "link") }) }) } @@ -131,8 +131,8 @@ fn visit_view_item(e: &mut Env, i: &ast::ViewItem) { Some(info) => { let (cnum, _, _) = resolve_crate(e, &None, - info.ident.as_slice(), - info.name.as_slice(), + info.ident[], + info.name[], None, i.span); e.sess.cstore.add_extern_mod_stmt_cnum(info.id, cnum); @@ -157,7 +157,7 @@ fn extract_crate_info(e: &Env, i: &ast::ViewItem) -> Option { let name = match *path_opt { Some((ref path_str, _)) => { let name = path_str.get().to_string(); - validate_crate_name(Some(e.sess), name.as_slice(), + validate_crate_name(Some(e.sess), name[], Some(i.span)); name } @@ -188,7 +188,7 @@ pub fn validate_crate_name(sess: Option<&Session>, s: &str, sp: Option) { for c in s.chars() { if c.is_alphanumeric() { continue } if c == '_' || c == '-' { continue } - err(format!("invalid character `{}` in crate name: `{}`", c, s).as_slice()); + err(format!("invalid character `{}` in crate name: `{}`", c, s)[]); } match sess { Some(sess) => sess.abort_if_errors(), @@ -246,7 +246,7 @@ fn visit_item(e: &Env, i: &ast::Item) { } else { e.sess.span_err(m.span, format!("unknown kind: `{}`", - k).as_slice()); + k)[]); cstore::NativeUnknown } } @@ -327,7 +327,7 @@ fn existing_match(e: &Env, name: &str, match e.sess.opts.externs.get(name) { Some(locs) => { let found = locs.iter().any(|l| { - let l = fs::realpath(&Path::new(l.as_slice())).ok(); + let l = fs::realpath(&Path::new(l[])).ok(); l == source.dylib || l == source.rlib }); if found { @@ -405,7 +405,7 @@ fn resolve_crate<'a>(e: &mut Env, crate_name: name, hash: hash.map(|a| &*a), filesearch: e.sess.target_filesearch(), - triple: e.sess.opts.target_triple.as_slice(), + triple: e.sess.opts.target_triple[], root: root, rejected_via_hash: vec!(), rejected_via_triple: vec!(), @@ -431,8 +431,8 @@ fn resolve_crate_deps(e: &mut Env, decoder::get_crate_deps(cdata).iter().map(|dep| { debug!("resolving dep crate {} hash: `{}`", dep.name, dep.hash); let (local_cnum, _, _) = resolve_crate(e, root, - dep.name.as_slice(), - dep.name.as_slice(), + dep.name[], + dep.name[], Some(&dep.hash), span); (dep.cnum, local_cnum) @@ -455,14 +455,14 @@ pub fn new(sess: &'a Session) -> PluginMetadataReader<'a> { pub fn read_plugin_metadata(&mut self, krate: &ast::ViewItem) -> PluginMetadata { let info = extract_crate_info(&self.env, krate).unwrap(); - let target_triple = self.env.sess.opts.target_triple.as_slice(); + let target_triple = self.env.sess.opts.target_triple[]; let is_cross = target_triple != config::host_triple(); let mut should_link = info.should_link && !is_cross; let mut load_ctxt = loader::Context { sess: self.env.sess, span: krate.span, - ident: info.ident.as_slice(), - crate_name: info.name.as_slice(), + ident: info.ident[], + crate_name: info.name[], hash: None, filesearch: self.env.sess.host_filesearch(), triple: config::host_triple(), @@ -483,7 +483,7 @@ pub fn read_plugin_metadata(&mut self, krate: &ast::ViewItem) -> PluginMetadata let message = format!("crate `{}` contains a plugin_registrar fn but \ only a version for triple `{}` could be found (need {})", info.ident, target_triple, config::host_triple()); - self.env.sess.span_err(krate.span, message.as_slice()); + self.env.sess.span_err(krate.span, message[]); // need to abort now because the syntax expansion // code will shortly attempt to load and execute // code from the found library. @@ -502,7 +502,7 @@ pub fn read_plugin_metadata(&mut self, krate: &ast::ViewItem) -> PluginMetadata let message = format!("plugin crate `{}` only found in rlib format, \ but must be available in dylib format", info.ident); - self.env.sess.span_err(krate.span, message.as_slice()); + self.env.sess.span_err(krate.span, message[]); // No need to abort because the loading code will just ignore this // empty dylib. } @@ -511,11 +511,11 @@ pub fn read_plugin_metadata(&mut self, krate: &ast::ViewItem) -> PluginMetadata macros: macros, registrar_symbol: registrar, }; - if should_link && existing_match(&self.env, info.name.as_slice(), + if should_link && existing_match(&self.env, info.name[], None).is_none() { // register crate now to avoid double-reading metadata - register_crate(&mut self.env, &None, info.ident.as_slice(), - info.name.as_slice(), krate.span, library); + register_crate(&mut self.env, &None, info.ident[], + info.name[], krate.span, library); } pc } diff --git a/src/librustc/metadata/csearch.rs b/src/librustc/metadata/csearch.rs index b702f4925d84..13342bf82cfe 100644 --- a/src/librustc/metadata/csearch.rs +++ b/src/librustc/metadata/csearch.rs @@ -95,7 +95,7 @@ pub fn get_item_path(tcx: &ty::ctxt, def: ast::DefId) -> Vec // FIXME #1920: This path is not always correct if the crate is not linked // into the root namespace. - let mut r = vec![ast_map::PathMod(token::intern(cdata.name.as_slice()))]; + let mut r = vec![ast_map::PathMod(token::intern(cdata.name[]))]; r.push_all(path.as_slice()); r } diff --git a/src/librustc/metadata/decoder.rs b/src/librustc/metadata/decoder.rs index d8168814c6cd..b869501237c3 100644 --- a/src/librustc/metadata/decoder.rs +++ b/src/librustc/metadata/decoder.rs @@ -221,7 +221,7 @@ fn each_reexport(d: rbml::Doc, f: F) -> bool where fn variant_disr_val(d: rbml::Doc) -> Option { reader::maybe_get_doc(d, tag_disr_val).and_then(|val_doc| { reader::with_doc_data(val_doc, |data| { - str::from_utf8(data).and_then(from_str) + str::from_utf8(data).ok().and_then(|s| s.parse()) }) }) } @@ -1160,7 +1160,7 @@ fn docstr(doc: rbml::Doc, tag_: uint) -> String { } reader::tagged_docs(depsdoc, tag_crate_dep, |depdoc| { let name = docstr(depdoc, tag_crate_dep_crate_name); - let hash = Svh::new(docstr(depdoc, tag_crate_dep_hash).as_slice()); + let hash = Svh::new(docstr(depdoc, tag_crate_dep_hash)[]); deps.push(CrateDep { cnum: crate_num, name: name, @@ -1345,7 +1345,7 @@ pub fn get_dylib_dependency_formats(cdata: Cmd) if spec.len() == 0 { continue } let cnum = spec.split(':').nth(0).unwrap(); let link = spec.split(':').nth(1).unwrap(); - let cnum = from_str(cnum).unwrap(); + let cnum = cnum.parse().unwrap(); let cnum = match cdata.cnum_map.get(&cnum) { Some(&n) => n, None => panic!("didn't find a crate in the cnum_map") diff --git a/src/librustc/metadata/encoder.rs b/src/librustc/metadata/encoder.rs index e5dae926db95..6782b3a74813 100644 --- a/src/librustc/metadata/encoder.rs +++ b/src/librustc/metadata/encoder.rs @@ -95,7 +95,7 @@ fn encode_impl_type_basename(rbml_w: &mut Encoder, name: ast::Ident) { } pub fn encode_def_id(rbml_w: &mut Encoder, id: DefId) { - rbml_w.wr_tagged_str(tag_def_id, def_to_string(id).as_slice()); + rbml_w.wr_tagged_str(tag_def_id, def_to_string(id)[]); } #[deriving(Clone)] @@ -154,7 +154,7 @@ fn encode_variant_id(rbml_w: &mut Encoder, vid: DefId) { rbml_w.end_tag(); rbml_w.start_tag(tag_mod_child); - rbml_w.wr_str(s.as_slice()); + rbml_w.wr_str(s[]); rbml_w.end_tag(); } @@ -264,7 +264,7 @@ fn encode_symbol(ecx: &EncodeContext, } None => { ecx.diag.handler().bug( - format!("encode_symbol: id not found {}", id).as_slice()); + format!("encode_symbol: id not found {}", id)[]); } } rbml_w.end_tag(); @@ -332,8 +332,8 @@ fn encode_enum_variant_info(ecx: &EncodeContext, encode_name(rbml_w, variant.node.name.name); encode_parent_item(rbml_w, local_def(id)); encode_visibility(rbml_w, variant.node.vis); - encode_attributes(rbml_w, variant.node.attrs.as_slice()); - encode_repr_attrs(rbml_w, ecx, variant.node.attrs.as_slice()); + encode_attributes(rbml_w, variant.node.attrs[]); + encode_repr_attrs(rbml_w, ecx, variant.node.attrs[]); let stab = stability::lookup(ecx.tcx, ast_util::local_def(variant.node.id)); encode_stability(rbml_w, stab); @@ -344,9 +344,9 @@ fn encode_enum_variant_info(ecx: &EncodeContext, let fields = ty::lookup_struct_fields(ecx.tcx, def_id); let idx = encode_info_for_struct(ecx, rbml_w, - fields.as_slice(), + fields[], index); - encode_struct_fields(rbml_w, fields.as_slice(), def_id); + encode_struct_fields(rbml_w, fields[], def_id); encode_index(rbml_w, idx, write_i64); } } @@ -386,12 +386,12 @@ fn encode_reexported_static_method(rbml_w: &mut Encoder, exp.name, token::get_name(method_name)); rbml_w.start_tag(tag_items_data_item_reexport); rbml_w.start_tag(tag_items_data_item_reexport_def_id); - rbml_w.wr_str(def_to_string(method_def_id).as_slice()); + rbml_w.wr_str(def_to_string(method_def_id)[]); rbml_w.end_tag(); rbml_w.start_tag(tag_items_data_item_reexport_name); rbml_w.wr_str(format!("{}::{}", exp.name, - token::get_name(method_name)).as_slice()); + token::get_name(method_name))[]); rbml_w.end_tag(); rbml_w.end_tag(); } @@ -529,7 +529,7 @@ fn encode_reexports(ecx: &EncodeContext, id); rbml_w.start_tag(tag_items_data_item_reexport); rbml_w.start_tag(tag_items_data_item_reexport_def_id); - rbml_w.wr_str(def_to_string(exp.def_id).as_slice()); + rbml_w.wr_str(def_to_string(exp.def_id)[]); rbml_w.end_tag(); rbml_w.start_tag(tag_items_data_item_reexport_name); rbml_w.wr_str(exp.name.as_str()); @@ -562,13 +562,13 @@ fn encode_info_for_mod(ecx: &EncodeContext, // Encode info about all the module children. for item in md.items.iter() { rbml_w.start_tag(tag_mod_child); - rbml_w.wr_str(def_to_string(local_def(item.id)).as_slice()); + rbml_w.wr_str(def_to_string(local_def(item.id))[]); rbml_w.end_tag(); each_auxiliary_node_id(&**item, |auxiliary_node_id| { rbml_w.start_tag(tag_mod_child); rbml_w.wr_str(def_to_string(local_def( - auxiliary_node_id)).as_slice()); + auxiliary_node_id))[]); rbml_w.end_tag(); true }); @@ -580,7 +580,7 @@ fn encode_info_for_mod(ecx: &EncodeContext, did, ecx.tcx.map.node_to_string(did)); rbml_w.start_tag(tag_mod_impl); - rbml_w.wr_str(def_to_string(local_def(did)).as_slice()); + rbml_w.wr_str(def_to_string(local_def(did))[]); rbml_w.end_tag(); } } @@ -615,7 +615,7 @@ fn encode_visibility(rbml_w: &mut Encoder, visibility: ast::Visibility) { ast::Public => 'y', ast::Inherited => 'i', }; - rbml_w.wr_str(ch.to_string().as_slice()); + rbml_w.wr_str(ch.to_string()[]); rbml_w.end_tag(); } @@ -627,7 +627,7 @@ fn encode_unboxed_closure_kind(rbml_w: &mut Encoder, ty::FnMutUnboxedClosureKind => 'm', ty::FnOnceUnboxedClosureKind => 'o', }; - rbml_w.wr_str(ch.to_string().as_slice()); + rbml_w.wr_str(ch.to_string()[]); rbml_w.end_tag(); } @@ -788,7 +788,7 @@ fn encode_generics<'a, 'tcx>(rbml_w: &mut Encoder, rbml_w.end_tag(); rbml_w.wr_tagged_str(tag_region_param_def_def_id, - def_to_string(param.def_id).as_slice()); + def_to_string(param.def_id)[]); rbml_w.wr_tagged_u64(tag_region_param_def_space, param.space.to_uint() as u64); @@ -864,9 +864,9 @@ fn encode_info_for_method<'a, 'tcx>(ecx: &EncodeContext<'a, 'tcx>, encode_path(rbml_w, impl_path.chain(Some(elem).into_iter())); match ast_item_opt { Some(&ast::MethodImplItem(ref ast_method)) => { - encode_attributes(rbml_w, ast_method.attrs.as_slice()); + encode_attributes(rbml_w, ast_method.attrs[]); let any_types = !pty.generics.types.is_empty(); - if any_types || is_default_impl || should_inline(ast_method.attrs.as_slice()) { + if any_types || is_default_impl || should_inline(ast_method.attrs[]) { encode_inlined_item(ecx, rbml_w, IIImplItemRef(local_def(parent_id), ast_item_opt.unwrap())); } @@ -909,7 +909,7 @@ fn encode_info_for_associated_type(ecx: &EncodeContext, match typedef_opt { None => {} Some(typedef) => { - encode_attributes(rbml_w, typedef.attrs.as_slice()); + encode_attributes(rbml_w, typedef.attrs[]); encode_type(ecx, rbml_w, ty::node_id_to_type(ecx.tcx, typedef.id)); } @@ -1043,7 +1043,7 @@ fn add_to_index(item: &ast::Item, rbml_w: &Encoder, encode_path(rbml_w, path); encode_visibility(rbml_w, vis); encode_stability(rbml_w, stab); - encode_attributes(rbml_w, item.attrs.as_slice()); + encode_attributes(rbml_w, item.attrs[]); rbml_w.end_tag(); } ast::ItemConst(_, _) => { @@ -1069,8 +1069,8 @@ fn add_to_index(item: &ast::Item, rbml_w: &Encoder, encode_bounds_and_type(rbml_w, ecx, &lookup_item_type(tcx, def_id)); encode_name(rbml_w, item.ident.name); encode_path(rbml_w, path); - encode_attributes(rbml_w, item.attrs.as_slice()); - if tps_len > 0u || should_inline(item.attrs.as_slice()) { + encode_attributes(rbml_w, item.attrs[]); + if tps_len > 0u || should_inline(item.attrs[]) { encode_inlined_item(ecx, rbml_w, IIItemRef(item)); } if tps_len == 0 { @@ -1086,7 +1086,7 @@ fn add_to_index(item: &ast::Item, rbml_w: &Encoder, encode_info_for_mod(ecx, rbml_w, m, - item.attrs.as_slice(), + item.attrs[], item.id, path, item.ident, @@ -1103,7 +1103,7 @@ fn add_to_index(item: &ast::Item, rbml_w: &Encoder, // Encode all the items in this module. for foreign_item in fm.items.iter() { rbml_w.start_tag(tag_mod_child); - rbml_w.wr_str(def_to_string(local_def(foreign_item.id)).as_slice()); + rbml_w.wr_str(def_to_string(local_def(foreign_item.id))[]); rbml_w.end_tag(); } encode_visibility(rbml_w, vis); @@ -1131,8 +1131,8 @@ fn add_to_index(item: &ast::Item, rbml_w: &Encoder, encode_item_variances(rbml_w, ecx, item.id); encode_bounds_and_type(rbml_w, ecx, &lookup_item_type(tcx, def_id)); encode_name(rbml_w, item.ident.name); - encode_attributes(rbml_w, item.attrs.as_slice()); - encode_repr_attrs(rbml_w, ecx, item.attrs.as_slice()); + encode_attributes(rbml_w, item.attrs[]); + encode_repr_attrs(rbml_w, ecx, item.attrs[]); for v in (*enum_definition).variants.iter() { encode_variant_id(rbml_w, local_def(v.node.id)); } @@ -1149,7 +1149,7 @@ fn add_to_index(item: &ast::Item, rbml_w: &Encoder, encode_enum_variant_info(ecx, rbml_w, item.id, - (*enum_definition).variants.as_slice(), + (*enum_definition).variants[], index); } ast::ItemStruct(ref struct_def, _) => { @@ -1161,7 +1161,7 @@ fn add_to_index(item: &ast::Item, rbml_w: &Encoder, class itself */ let idx = encode_info_for_struct(ecx, rbml_w, - fields.as_slice(), + fields[], index); /* Index the class*/ @@ -1175,16 +1175,16 @@ fn add_to_index(item: &ast::Item, rbml_w: &Encoder, encode_item_variances(rbml_w, ecx, item.id); encode_name(rbml_w, item.ident.name); - encode_attributes(rbml_w, item.attrs.as_slice()); + encode_attributes(rbml_w, item.attrs[]); encode_path(rbml_w, path.clone()); encode_stability(rbml_w, stab); encode_visibility(rbml_w, vis); - encode_repr_attrs(rbml_w, ecx, item.attrs.as_slice()); + encode_repr_attrs(rbml_w, ecx, item.attrs[]); /* Encode def_ids for each field and method for methods, write all the stuff get_trait_method needs to know*/ - encode_struct_fields(rbml_w, fields.as_slice(), def_id); + encode_struct_fields(rbml_w, fields[], def_id); encode_inlined_item(ecx, rbml_w, IIItemRef(item)); @@ -1216,7 +1216,7 @@ fn add_to_index(item: &ast::Item, rbml_w: &Encoder, encode_family(rbml_w, 'i'); encode_bounds_and_type(rbml_w, ecx, &lookup_item_type(tcx, def_id)); encode_name(rbml_w, item.ident.name); - encode_attributes(rbml_w, item.attrs.as_slice()); + encode_attributes(rbml_w, item.attrs[]); encode_unsafety(rbml_w, unsafety); match ty.node { ast::TyPath(ref path, _) if path.segments @@ -1319,7 +1319,7 @@ fn add_to_index(item: &ast::Item, rbml_w: &Encoder, encode_generics(rbml_w, ecx, &trait_def.generics, tag_item_generics); encode_trait_ref(rbml_w, ecx, &*trait_def.trait_ref, tag_item_trait_ref); encode_name(rbml_w, item.ident.name); - encode_attributes(rbml_w, item.attrs.as_slice()); + encode_attributes(rbml_w, item.attrs[]); encode_visibility(rbml_w, vis); encode_stability(rbml_w, stab); for &method_def_id in ty::trait_item_def_ids(tcx, def_id).iter() { @@ -1337,7 +1337,7 @@ fn add_to_index(item: &ast::Item, rbml_w: &Encoder, rbml_w.end_tag(); rbml_w.start_tag(tag_mod_child); - rbml_w.wr_str(def_to_string(method_def_id.def_id()).as_slice()); + rbml_w.wr_str(def_to_string(method_def_id.def_id())[]); rbml_w.end_tag(); } encode_path(rbml_w, path.clone()); @@ -1422,14 +1422,14 @@ fn add_to_index(item: &ast::Item, rbml_w: &Encoder, }; match trait_item { &ast::RequiredMethod(ref m) => { - encode_attributes(rbml_w, m.attrs.as_slice()); + encode_attributes(rbml_w, m.attrs[]); encode_trait_item(rbml_w); encode_item_sort(rbml_w, 'r'); encode_method_argument_names(rbml_w, &*m.decl); } &ast::ProvidedMethod(ref m) => { - encode_attributes(rbml_w, m.attrs.as_slice()); + encode_attributes(rbml_w, m.attrs[]); encode_trait_item(rbml_w); encode_item_sort(rbml_w, 'p'); encode_inlined_item(ecx, rbml_w, IITraitItemRef(def_id, trait_item)); @@ -1438,7 +1438,7 @@ fn add_to_index(item: &ast::Item, rbml_w: &Encoder, &ast::TypeTraitItem(ref associated_type) => { encode_attributes(rbml_w, - associated_type.attrs.as_slice()); + associated_type.attrs[]); encode_item_sort(rbml_w, 't'); } } @@ -1802,7 +1802,7 @@ fn encode_macro_def(ecx: &EncodeContext, let def = ecx.tcx.sess.codemap().span_to_snippet(*span) .expect("Unable to find source for macro"); rbml_w.start_tag(tag_macro_def); - rbml_w.wr_str(def.as_slice()); + rbml_w.wr_str(def[]); rbml_w.end_tag(); } @@ -1849,7 +1849,7 @@ impl<'a, 'b, 'v> Visitor<'v> for StructFieldVisitor<'a, 'b> { fn visit_struct_field(&mut self, field: &ast::StructField) { self.rbml_w.start_tag(tag_struct_field); self.rbml_w.wr_tagged_u32(tag_struct_field_id, field.node.id); - encode_attributes(self.rbml_w, field.node.attrs.as_slice()); + encode_attributes(self.rbml_w, field.node.attrs[]); self.rbml_w.end_tag(); } } @@ -1921,13 +1921,13 @@ fn encode_misc_info(ecx: &EncodeContext, rbml_w.start_tag(tag_misc_info_crate_items); for item in krate.module.items.iter() { rbml_w.start_tag(tag_mod_child); - rbml_w.wr_str(def_to_string(local_def(item.id)).as_slice()); + rbml_w.wr_str(def_to_string(local_def(item.id))[]); rbml_w.end_tag(); each_auxiliary_node_id(&**item, |auxiliary_node_id| { rbml_w.start_tag(tag_mod_child); rbml_w.wr_str(def_to_string(local_def( - auxiliary_node_id)).as_slice()); + auxiliary_node_id))[]); rbml_w.end_tag(); true }); @@ -2096,17 +2096,17 @@ struct Stats { let mut rbml_w = writer::Encoder::new(wr); - encode_crate_name(&mut rbml_w, ecx.link_meta.crate_name.as_slice()); + encode_crate_name(&mut rbml_w, ecx.link_meta.crate_name[]); encode_crate_triple(&mut rbml_w, tcx.sess .opts .target_triple - .as_slice()); + []); encode_hash(&mut rbml_w, &ecx.link_meta.crate_hash); encode_dylib_dependency_formats(&mut rbml_w, &ecx); let mut i = rbml_w.writer.tell().unwrap(); - encode_attributes(&mut rbml_w, krate.attrs.as_slice()); + encode_attributes(&mut rbml_w, krate.attrs[]); stats.attr_bytes = rbml_w.writer.tell().unwrap() - i; i = rbml_w.writer.tell().unwrap(); diff --git a/src/librustc/metadata/loader.rs b/src/librustc/metadata/loader.rs index bc34b0b45e96..5f554eb9c1e5 100644 --- a/src/librustc/metadata/loader.rs +++ b/src/librustc/metadata/loader.rs @@ -316,14 +316,14 @@ pub fn report_load_errs(&mut self) { &Some(ref r) => format!("{} which `{}` depends on", message, r.ident) }; - self.sess.span_err(self.span, message.as_slice()); + self.sess.span_err(self.span, message[]); if self.rejected_via_triple.len() > 0 { let mismatches = self.rejected_via_triple.iter(); for (i, &CrateMismatch{ ref path, ref got }) in mismatches.enumerate() { self.sess.fileline_note(self.span, format!("crate `{}`, path #{}, triple {}: {}", - self.ident, i+1, got, path.display()).as_slice()); + self.ident, i+1, got, path.display())[]); } } if self.rejected_via_hash.len() > 0 { @@ -333,7 +333,7 @@ pub fn report_load_errs(&mut self) { for (i, &CrateMismatch{ ref path, .. }) in mismatches.enumerate() { self.sess.fileline_note(self.span, format!("crate `{}` path {}{}: {}", - self.ident, "#", i+1, path.display()).as_slice()); + self.ident, "#", i+1, path.display())[]); } match self.root { &None => {} @@ -341,7 +341,7 @@ pub fn report_load_errs(&mut self) { for (i, path) in r.paths().iter().enumerate() { self.sess.fileline_note(self.span, format!("crate `{}` path #{}: {}", - r.ident, i+1, path.display()).as_slice()); + r.ident, i+1, path.display())[]); } } } @@ -387,7 +387,7 @@ fn find_library_crate(&mut self) -> Option { None => return FileDoesntMatch, Some(file) => file, }; - let (hash, rlib) = if file.starts_with(rlib_prefix.as_slice()) && + let (hash, rlib) = if file.starts_with(rlib_prefix[]) && file.ends_with(".rlib") { (file.slice(rlib_prefix.len(), file.len() - ".rlib".len()), true) @@ -448,26 +448,26 @@ fn find_library_crate(&mut self) -> Option { _ => { self.sess.span_err(self.span, format!("multiple matching crates for `{}`", - self.crate_name).as_slice()); + self.crate_name)[]); self.sess.note("candidates:"); for lib in libraries.iter() { match lib.dylib { Some(ref p) => { self.sess.note(format!("path: {}", - p.display()).as_slice()); + p.display())[]); } None => {} } match lib.rlib { Some(ref p) => { self.sess.note(format!("path: {}", - p.display()).as_slice()); + p.display())[]); } None => {} } let data = lib.metadata.as_slice(); let name = decoder::get_crate_name(data); - note_crate_name(self.sess.diagnostic(), name.as_slice()); + note_crate_name(self.sess.diagnostic(), name[]); } None } @@ -521,11 +521,11 @@ fn extract_one(&mut self, m: HashSet, flavor: &str, format!("multiple {} candidates for `{}` \ found", flavor, - self.crate_name).as_slice()); + self.crate_name)[]); self.sess.span_note(self.span, format!(r"candidate #1: {}", ret.as_ref().unwrap() - .display()).as_slice()); + .display())[]); error = 1; ret = None; } @@ -533,7 +533,7 @@ fn extract_one(&mut self, m: HashSet, flavor: &str, error += 1; self.sess.span_note(self.span, format!(r"candidate #{}: {}", error, - lib.display()).as_slice()); + lib.display())[]); continue } *slot = Some(metadata); @@ -608,17 +608,17 @@ fn find_commandline_library(&mut self) -> Option { let mut rlibs = HashSet::new(); let mut dylibs = HashSet::new(); { - let mut locs = locs.iter().map(|l| Path::new(l.as_slice())).filter(|loc| { + let mut locs = locs.iter().map(|l| Path::new(l[])).filter(|loc| { if !loc.exists() { sess.err(format!("extern location for {} does not exist: {}", - self.crate_name, loc.display()).as_slice()); + self.crate_name, loc.display())[]); return false; } let file = match loc.filename_str() { Some(file) => file, None => { sess.err(format!("extern location for {} is not a file: {}", - self.crate_name, loc.display()).as_slice()); + self.crate_name, loc.display())[]); return false; } }; @@ -626,12 +626,12 @@ fn find_commandline_library(&mut self) -> Option { return true } else { let (ref prefix, ref suffix) = dylibname; - if file.starts_with(prefix.as_slice()) && file.ends_with(suffix.as_slice()) { + if file.starts_with(prefix[]) && file.ends_with(suffix[]) { return true } } sess.err(format!("extern location for {} is of an unknown type: {}", - self.crate_name, loc.display()).as_slice()); + self.crate_name, loc.display())[]); false }); @@ -664,7 +664,7 @@ fn find_commandline_library(&mut self) -> Option { } pub fn note_crate_name(diag: &SpanHandler, name: &str) { - diag.handler().note(format!("crate name: {}", name).as_slice()); + diag.handler().note(format!("crate name: {}", name)[]); } impl ArchiveMetadata { diff --git a/src/librustc/metadata/tydecode.rs b/src/librustc/metadata/tydecode.rs index 9d3a2c1d6677..7683506f0f4a 100644 --- a/src/librustc/metadata/tydecode.rs +++ b/src/librustc/metadata/tydecode.rs @@ -233,7 +233,7 @@ fn parse_trait_store(st: &mut PState, conv: conv_did) -> ty::TraitStore { '&' => ty::RegionTraitStore(parse_region(st, conv), parse_mutability(st)), c => { st.tcx.sess.bug(format!("parse_trait_store(): bad input '{}'", - c).as_slice()) + c)[]) } } } @@ -287,7 +287,7 @@ fn parse_bound_region(st: &mut PState, conv: conv_did) -> ty::BoundRegion { } '[' => { let def = parse_def(st, RegionParameter, |x,y| conv(x,y)); - let ident = token::str_to_ident(parse_str(st, ']').as_slice()); + let ident = token::str_to_ident(parse_str(st, ']')[]); ty::BrNamed(def, ident.name) } 'f' => { @@ -318,7 +318,7 @@ fn parse_region(st: &mut PState, conv: conv_did) -> ty::Region { assert_eq!(next(st), '|'); let index = parse_uint(st); assert_eq!(next(st), '|'); - let nm = token::str_to_ident(parse_str(st, ']').as_slice()); + let nm = token::str_to_ident(parse_str(st, ']')[]); ty::ReEarlyBound(node_id, space, index, nm.name) } 'f' => { @@ -560,7 +560,7 @@ fn parse_abi_set(st: &mut PState) -> abi::Abi { assert_eq!(next(st), '['); scan(st, |c| c == ']', |bytes| { let abi_str = str::from_utf8(bytes).unwrap(); - abi::lookup(abi_str.as_slice()).expect(abi_str) + abi::lookup(abi_str[]).expect(abi_str) }) } @@ -639,12 +639,12 @@ pub fn parse_def_id(buf: &[u8]) -> ast::DefId { let crate_part = buf[0u..colon_idx]; let def_part = buf[colon_idx + 1u..len]; - let crate_num = match str::from_utf8(crate_part).and_then(from_str::) { + let crate_num = match str::from_utf8(crate_part).ok().and_then(|s| s.parse::()) { Some(cn) => cn as ast::CrateNum, None => panic!("internal error: parse_def_id: crate number expected, found {}", crate_part) }; - let def_num = match str::from_utf8(def_part).and_then(from_str::) { + let def_num = match str::from_utf8(def_part).ok().and_then(|s| s.parse::()) { Some(dn) => dn as ast::NodeId, None => panic!("internal error: parse_def_id: id expected, found {}", def_part) diff --git a/src/librustc/middle/astconv_util.rs b/src/librustc/middle/astconv_util.rs index 6b90bcd60e75..060e2f67faf9 100644 --- a/src/librustc/middle/astconv_util.rs +++ b/src/librustc/middle/astconv_util.rs @@ -48,7 +48,7 @@ pub fn ast_ty_to_prim_ty<'tcx>(tcx: &ty::ctxt<'tcx>, ast_ty: &ast::Ty) None => { tcx.sess.span_bug(ast_ty.span, format!("unbound path {}", - path.repr(tcx)).as_slice()) + path.repr(tcx))[]) } Some(&d) => d }; diff --git a/src/librustc/middle/astencode.rs b/src/librustc/middle/astencode.rs index 69fbd59fd924..ce86d6805b26 100644 --- a/src/librustc/middle/astencode.rs +++ b/src/librustc/middle/astencode.rs @@ -132,7 +132,7 @@ pub fn decode_inlined_item<'tcx>(cdata: &cstore::crate_metadata, // Do an Option dance to use the path after it is moved below. let s = ast_map::path_to_string(ast_map::Values(path.iter())); path_as_str = Some(s); - path_as_str.as_ref().map(|x| x.as_slice()) + path_as_str.as_ref().map(|x| x[]) }); let mut ast_dsr = reader::Decoder::new(ast_doc); let from_id_range = Decodable::decode(&mut ast_dsr).unwrap(); @@ -1876,7 +1876,7 @@ fn decode_side_tables(dcx: &DecodeContext, None => { dcx.tcx.sess.bug( format!("unknown tag found in side tables: {:x}", - tag).as_slice()); + tag)[]); } Some(value) => { let val_doc = entry_doc.get(c::tag_table_val as uint); @@ -1961,7 +1961,7 @@ fn decode_side_tables(dcx: &DecodeContext, _ => { dcx.tcx.sess.bug( format!("unknown tag found in side tables: {:x}", - tag).as_slice()); + tag)[]); } } } diff --git a/src/librustc/middle/cfg/construct.rs b/src/librustc/middle/cfg/construct.rs index 82bed2540317..2d50757782db 100644 --- a/src/librustc/middle/cfg/construct.rs +++ b/src/librustc/middle/cfg/construct.rs @@ -362,7 +362,7 @@ fn expr(&mut self, expr: &ast::Expr, pred: CFGIndex) -> CFGIndex { let mut cond_exit = discr_exit; for arm in arms.iter() { cond_exit = self.add_dummy_node(&[cond_exit]); // 2 - let pats_exit = self.pats_any(arm.pats.as_slice(), + let pats_exit = self.pats_any(arm.pats[], cond_exit); // 3 let guard_exit = self.opt_expr(&arm.guard, pats_exit); // 4 @@ -617,14 +617,14 @@ fn find_scope(&self, self.tcx.sess.span_bug( expr.span, format!("no loop scope for id {}", - loop_id).as_slice()); + loop_id)[]); } r => { self.tcx.sess.span_bug( expr.span, format!("bad entry `{}` in def_map for label", - r).as_slice()); + r)[]); } } } diff --git a/src/librustc/middle/cfg/graphviz.rs b/src/librustc/middle/cfg/graphviz.rs index e33f44967f1a..13bd22a67c41 100644 --- a/src/librustc/middle/cfg/graphviz.rs +++ b/src/librustc/middle/cfg/graphviz.rs @@ -50,7 +50,7 @@ fn replace_newline_with_backslash_l(s: String) -> String { } impl<'a, 'ast> dot::Labeller<'a, Node<'a>, Edge<'a>> for LabelledCFG<'a, 'ast> { - fn graph_id(&'a self) -> dot::Id<'a> { dot::Id::new(self.name.as_slice()).unwrap() } + fn graph_id(&'a self) -> dot::Id<'a> { dot::Id::new(self.name[]).unwrap() } fn node_id(&'a self, &(i,_): &Node<'a>) -> dot::Id<'a> { dot::Id::new(format!("N{}", i.node_id())).unwrap() @@ -83,8 +83,7 @@ fn edge_label(&self, e: &Edge<'a>) -> dot::LabelText<'a> { let s = self.ast_map.node_to_string(node_id); // left-aligns the lines let s = replace_newline_with_backslash_l(s); - label.push_str(format!("exiting scope_{} {}", i, - s.as_slice()).as_slice()); + label.push_str(format!("exiting scope_{} {}", i, s[])[]); } dot::EscStr(label.into_cow()) } diff --git a/src/librustc/middle/check_loop.rs b/src/librustc/middle/check_loop.rs index cb454f94dc74..5a08d7c179d1 100644 --- a/src/librustc/middle/check_loop.rs +++ b/src/librustc/middle/check_loop.rs @@ -74,13 +74,11 @@ fn require_loop(&self, name: &str, span: Span) { Loop => {} Closure => { self.sess.span_err(span, - format!("`{}` inside of a closure", - name).as_slice()); + format!("`{}` inside of a closure", name)[]); } Normal => { self.sess.span_err(span, - format!("`{}` outside of loop", - name).as_slice()); + format!("`{}` outside of loop", name)[]); } } } diff --git a/src/librustc/middle/check_match.rs b/src/librustc/middle/check_match.rs index 9a94eb979314..da1bd09ceffd 100644 --- a/src/librustc/middle/check_match.rs +++ b/src/librustc/middle/check_match.rs @@ -162,7 +162,7 @@ fn check_expr(cx: &mut MatchCheckCtxt, ex: &ast::Expr) { // First, check legality of move bindings. check_legality_of_move_bindings(cx, arm.guard.is_some(), - arm.pats.as_slice()); + arm.pats[]); // Second, if there is a guard on each arm, make sure it isn't // assigning or borrowing anything mutably. @@ -199,7 +199,7 @@ fn check_expr(cx: &mut MatchCheckCtxt, ex: &ast::Expr) { } // Fourth, check for unreachable arms. - check_arms(cx, inlined_arms.as_slice(), source); + check_arms(cx, inlined_arms[], source); // Finally, check if the whole match expression is exhaustive. // Check for empty enum, because is_useful only works on inhabited types. @@ -231,7 +231,7 @@ fn check_expr(cx: &mut MatchCheckCtxt, ex: &ast::Expr) { pat.span, format!("refutable pattern in `for` loop binding: \ `{}` not covered", - pat_to_string(uncovered_pat)).as_slice()); + pat_to_string(uncovered_pat))[]); }); // Check legality of move bindings. @@ -304,7 +304,7 @@ fn check_arms(cx: &MatchCheckCtxt, for pat in pats.iter() { let v = vec![&**pat]; - match is_useful(cx, &seen, v.as_slice(), LeaveOutWitness) { + match is_useful(cx, &seen, v[], LeaveOutWitness) { NotUseful => { match source { ast::MatchSource::IfLetDesugar { .. } => { @@ -356,7 +356,7 @@ fn raw_pat<'a>(p: &'a Pat) -> &'a Pat { fn check_exhaustive(cx: &MatchCheckCtxt, sp: Span, matrix: &Matrix) { match is_useful(cx, matrix, &[DUMMY_WILD_PAT], ConstructWitness) { UsefulWithWitness(pats) => { - let witness = match pats.as_slice() { + let witness = match pats[] { [ref witness] => &**witness, [] => DUMMY_WILD_PAT, _ => unreachable!() @@ -610,7 +610,7 @@ fn is_useful(cx: &MatchCheckCtxt, UsefulWithWitness(pats) => UsefulWithWitness({ let arity = constructor_arity(cx, &c, left_ty); let mut result = { - let pat_slice = pats.as_slice(); + let pat_slice = pats[]; let subpats = Vec::from_fn(arity, |i| { pat_slice.get(i).map_or(DUMMY_WILD_PAT, |p| &**p) }); @@ -657,10 +657,10 @@ fn is_useful_specialized(cx: &MatchCheckCtxt, &Matrix(ref m): &Matrix, witness: WitnessPreference) -> Usefulness { let arity = constructor_arity(cx, &ctor, lty); let matrix = Matrix(m.iter().filter_map(|r| { - specialize(cx, r.as_slice(), &ctor, 0u, arity) + specialize(cx, r[], &ctor, 0u, arity) }).collect()); match specialize(cx, v, &ctor, 0u, arity) { - Some(v) => is_useful(cx, &matrix, v.as_slice(), witness), + Some(v) => is_useful(cx, &matrix, v[], witness), None => NotUseful } } @@ -1047,7 +1047,7 @@ fn check_legality_of_move_bindings(cx: &MatchCheckCtxt, format!("binding pattern {} is not an \ identifier: {}", p.id, - p.node).as_slice()); + p.node)[]); } } } diff --git a/src/librustc/middle/check_static.rs b/src/librustc/middle/check_static.rs index 21e94d69366d..6ff34d625005 100644 --- a/src/librustc/middle/check_static.rs +++ b/src/librustc/middle/check_static.rs @@ -112,7 +112,7 @@ fn check_static_mut_type(&self, e: &ast::Expr) { }; self.tcx.sess.span_err(e.span, format!("mutable statics are not allowed \ - to have {}", suffix).as_slice()); + to have {}", suffix)[]); } fn check_static_type(&self, e: &ast::Expr) { @@ -168,7 +168,7 @@ fn visit_expr(&mut self, e: &ast::Expr) { ty::ty_enum(did, _) if ty::has_dtor(self.tcx, did) => { self.tcx.sess.span_err(e.span, format!("{} are not allowed to have \ - destructors", self.msg()).as_slice()) + destructors", self.msg())[]) } _ => {} } @@ -232,7 +232,7 @@ fn visit_expr(&mut self, e: &ast::Expr) { let msg = "constants cannot refer to other statics, \ insert an intermediate constant \ instead"; - self.tcx.sess.span_err(e.span, msg.as_slice()); + self.tcx.sess.span_err(e.span, msg[]); } _ => {} } diff --git a/src/librustc/middle/check_static_recursion.rs b/src/librustc/middle/check_static_recursion.rs index 90242a3252ec..c36b4aa7f231 100644 --- a/src/librustc/middle/check_static_recursion.rs +++ b/src/librustc/middle/check_static_recursion.rs @@ -105,7 +105,7 @@ fn visit_expr(&mut self, e: &ast::Expr) { _ => { self.sess.span_err(e.span, format!("expected item, found {}", - self.ast_map.node_to_string(def_id.node)).as_slice()); + self.ast_map.node_to_string(def_id.node))[]); return; }, } diff --git a/src/librustc/middle/const_eval.rs b/src/librustc/middle/const_eval.rs index 62f1a30f8e74..5b89912dd03f 100644 --- a/src/librustc/middle/const_eval.rs +++ b/src/librustc/middle/const_eval.rs @@ -117,7 +117,7 @@ fn variant_expr<'a>(variants: &'a [P], id: ast::NodeId) None => None, Some(ast_map::NodeItem(it)) => match it.node { ast::ItemEnum(ast::EnumDef { ref variants }, _) => { - variant_expr(variants.as_slice(), variant_def.node) + variant_expr(variants[], variant_def.node) } _ => None }, @@ -138,7 +138,7 @@ fn variant_expr<'a>(variants: &'a [P], id: ast::NodeId) // NOTE this doesn't do the right thing, it compares inlined // NodeId's to the original variant_def's NodeId, but they // come from different crates, so they will likely never match. - variant_expr(variants.as_slice(), variant_def.node).map(|e| e.id) + variant_expr(variants[], variant_def.node).map(|e| e.id) } _ => None }, @@ -364,7 +364,7 @@ pub fn const_expr_to_pat(tcx: &ty::ctxt, expr: &Expr) -> P { pub fn eval_const_expr(tcx: &ty::ctxt, e: &Expr) -> const_val { match eval_const_expr_partial(tcx, e) { Ok(r) => r, - Err(s) => tcx.sess.span_fatal(e.span, s.as_slice()) + Err(s) => tcx.sess.span_fatal(e.span, s[]) } } @@ -603,7 +603,7 @@ pub fn lit_to_const(lit: &ast::Lit) -> const_val { ast::LitInt(n, ast::UnsignedIntLit(_)) => const_uint(n), ast::LitFloat(ref n, _) | ast::LitFloatUnsuffixed(ref n) => { - const_float(from_str::(n.get()).unwrap() as f64) + const_float(n.get().parse::().unwrap() as f64) } ast::LitBool(b) => const_bool(b) } diff --git a/src/librustc/middle/dataflow.rs b/src/librustc/middle/dataflow.rs index 17ebd1b94a70..a2d417ca345d 100644 --- a/src/librustc/middle/dataflow.rs +++ b/src/librustc/middle/dataflow.rs @@ -311,7 +311,7 @@ pub fn each_bit_for_node(&self, e: EntryOrExit, cfgidx: CFGIndex, f: F) -> bo let mut t = on_entry.to_vec(); self.apply_gen_kill(cfgidx, t.as_mut_slice()); temp_bits = t; - temp_bits.as_slice() + temp_bits[] } }; debug!("{} each_bit_for_node({}, cfgidx={}) bits={}", @@ -420,7 +420,7 @@ pub fn add_kills_from_flow_exits(&mut self, cfg: &cfg::CFG) { let bits = self.kills.slice_mut(start, end); debug!("{} add_kills_from_flow_exits flow_exit={} bits={} [before]", self.analysis_name, flow_exit, mut_bits_to_string(bits)); - bits.clone_from_slice(orig_kills.as_slice()); + bits.clone_from_slice(orig_kills[]); debug!("{} add_kills_from_flow_exits flow_exit={} bits={} [after]", self.analysis_name, flow_exit, mut_bits_to_string(bits)); } @@ -553,7 +553,7 @@ fn bits_to_string(words: &[uint]) -> String { let mut v = word; for _ in range(0u, uint::BYTES) { result.push(sep); - result.push_str(format!("{:02x}", v & 0xFF).as_slice()); + result.push_str(format!("{:02x}", v & 0xFF)[]); v >>= 8; sep = '-'; } diff --git a/src/librustc/middle/dependency_format.rs b/src/librustc/middle/dependency_format.rs index 3cb628c2e65c..6b56ece28bdb 100644 --- a/src/librustc/middle/dependency_format.rs +++ b/src/librustc/middle/dependency_format.rs @@ -118,7 +118,7 @@ fn calculate_type(sess: &session::Session, let src = sess.cstore.get_used_crate_source(cnum).unwrap(); if src.rlib.is_some() { return } sess.err(format!("dependency `{}` not found in rlib format", - data.name).as_slice()); + data.name)[]); }); return Vec::new(); } @@ -197,7 +197,7 @@ fn calculate_type(sess: &session::Session, match kind { cstore::RequireStatic => "rlib", cstore::RequireDynamic => "dylib", - }).as_slice()); + })[]); } } } @@ -222,7 +222,7 @@ fn add_library(sess: &session::Session, let data = sess.cstore.get_crate_data(cnum); sess.err(format!("cannot satisfy dependencies so `{}` only \ shows up once", - data.name).as_slice()); + data.name)[]); sess.help("having upstream crates all available in one format \ will likely make this go away"); } diff --git a/src/librustc/middle/expr_use_visitor.rs b/src/librustc/middle/expr_use_visitor.rs index abc3c8d0d8fa..4ee0064b0e6a 100644 --- a/src/librustc/middle/expr_use_visitor.rs +++ b/src/librustc/middle/expr_use_visitor.rs @@ -678,7 +678,7 @@ fn walk_callee(&mut self, call: &ast::Expr, callee: &ast::Expr) { self.tcx().sess.span_bug( callee.span, format!("unexpected callee type {}", - callee_ty.repr(self.tcx())).as_slice()) + callee_ty.repr(self.tcx()))[]) } }; match overloaded_call_type { @@ -869,7 +869,7 @@ fn walk_autoderefs(&mut self, ty::ty_rptr(r, ref m) => (m.mutbl, r), _ => self.tcx().sess.span_bug(expr.span, format!("bad overloaded deref type {}", - method_ty.repr(self.tcx())).as_slice()) + method_ty.repr(self.tcx()))[]) }; let bk = ty::BorrowKind::from_mutbl(m); self.delegate.borrow(expr.id, expr.span, cmt, @@ -1186,7 +1186,7 @@ fn walk_pat(&mut self, // pattern. let msg = format!("Pattern has unexpected type: {}", def); - tcx.sess.span_bug(pat.span, msg.as_slice()) + tcx.sess.span_bug(pat.span, msg[]) } Some(def) => { @@ -1195,7 +1195,7 @@ fn walk_pat(&mut self, // should not resolve. let msg = format!("Pattern has unexpected def: {}", def); - tcx.sess.span_bug(pat.span, msg.as_slice()) + tcx.sess.span_bug(pat.span, msg[]) } } } diff --git a/src/librustc/middle/infer/combine.rs b/src/librustc/middle/infer/combine.rs index 82ddbcee5a72..11ab44ba09ff 100644 --- a/src/librustc/middle/infer/combine.rs +++ b/src/librustc/middle/infer/combine.rs @@ -141,7 +141,7 @@ fn substs_variances(&self, for _ in a_regions.iter() { invariance.push(ty::Invariant); } - invariance.as_slice() + invariance[] } }; @@ -411,7 +411,7 @@ pub fn super_tys<'tcx, C: Combine<'tcx>>(this: &C, format!("{}: bot and var types should have been handled ({},{})", this.tag(), a.repr(this.infcx().tcx), - b.repr(this.infcx().tcx)).as_slice()); + b.repr(this.infcx().tcx))[]); } (&ty::ty_err, _) | (_, &ty::ty_err) => { diff --git a/src/librustc/middle/infer/error_reporting.rs b/src/librustc/middle/infer/error_reporting.rs index b4c1c0b396b6..0ea3d415ec5c 100644 --- a/src/librustc/middle/infer/error_reporting.rs +++ b/src/librustc/middle/infer/error_reporting.rs @@ -199,9 +199,9 @@ fn report_region_errors(&self, ref trace_origins, ref same_regions) => { if !same_regions.is_empty() { - self.report_processed_errors(var_origins.as_slice(), - trace_origins.as_slice(), - same_regions.as_slice()); + self.report_processed_errors(var_origins[], + trace_origins[], + same_regions[]); } } } @@ -374,7 +374,7 @@ fn report_type_error(&self, trace: TypeTrace<'tcx>, terr: &ty::type_err<'tcx>) { format!("{}: {} ({})", message_root_str, expected_found_str, - ty::type_err_to_str(self.tcx, terr)).as_slice()); + ty::type_err_to_str(self.tcx, terr))[]); match trace.origin { infer::MatchExpressionArm(_, arm_span) => @@ -438,13 +438,13 @@ fn report_param_bound_failure(&self, origin.span(), format!( "the parameter type `{}` may not live long enough", - param_ty.user_string(self.tcx)).as_slice()); + param_ty.user_string(self.tcx))[]); self.tcx.sess.span_help( origin.span(), format!( "consider adding an explicit lifetime bound `{}: {}`...", param_ty.user_string(self.tcx), - sub.user_string(self.tcx)).as_slice()); + sub.user_string(self.tcx))[]); } ty::ReStatic => { @@ -453,12 +453,12 @@ fn report_param_bound_failure(&self, origin.span(), format!( "the parameter type `{}` may not live long enough", - param_ty.user_string(self.tcx)).as_slice()); + param_ty.user_string(self.tcx))[]); self.tcx.sess.span_help( origin.span(), format!( "consider adding an explicit lifetime bound `{}: 'static`...", - param_ty.user_string(self.tcx)).as_slice()); + param_ty.user_string(self.tcx))[]); } _ => { @@ -467,16 +467,16 @@ fn report_param_bound_failure(&self, origin.span(), format!( "the parameter type `{}` may not live long enough", - param_ty.user_string(self.tcx)).as_slice()); + param_ty.user_string(self.tcx))[]); self.tcx.sess.span_help( origin.span(), format!( "consider adding an explicit lifetime bound to `{}`", - param_ty.user_string(self.tcx)).as_slice()); + param_ty.user_string(self.tcx))[]); note_and_explain_region( self.tcx, format!("the parameter type `{}` must be valid for ", - param_ty.user_string(self.tcx)).as_slice(), + param_ty.user_string(self.tcx))[], sub, "..."); } @@ -518,7 +518,7 @@ fn report_concrete_failure(&self, ty::local_var_name_str(self.tcx, upvar_id.var_id) .get() - .to_string()).as_slice()); + .to_string())[]); note_and_explain_region( self.tcx, "...the borrowed pointer is valid for ", @@ -530,7 +530,7 @@ fn report_concrete_failure(&self, ty::local_var_name_str(self.tcx, upvar_id.var_id) .get() - .to_string()).as_slice(), + .to_string())[], sup, ""); } @@ -576,7 +576,7 @@ fn report_concrete_failure(&self, outlive the enclosing closure", ty::local_var_name_str(self.tcx, id).get() - .to_string()).as_slice()); + .to_string())[]); note_and_explain_region( self.tcx, "captured variable is valid for ", @@ -618,7 +618,7 @@ fn report_concrete_failure(&self, span, format!("the type `{}` does not fulfill the \ required lifetime", - self.ty_to_string(ty)).as_slice()); + self.ty_to_string(ty))[]); note_and_explain_region(self.tcx, "type must outlive ", sub, @@ -644,7 +644,7 @@ fn report_concrete_failure(&self, span, format!("the type `{}` (provided as the value of \ a type parameter) is not valid at this point", - self.ty_to_string(ty)).as_slice()); + self.ty_to_string(ty))[]); note_and_explain_region(self.tcx, "type must outlive ", sub, @@ -710,7 +710,7 @@ fn report_concrete_failure(&self, span, format!("type of expression contains references \ that are not valid during the expression: `{}`", - self.ty_to_string(t)).as_slice()); + self.ty_to_string(t))[]); note_and_explain_region( self.tcx, "type is only valid for ", @@ -732,7 +732,7 @@ fn report_concrete_failure(&self, span, format!("in type `{}`, reference has a longer lifetime \ than the data it references", - self.ty_to_string(ty)).as_slice()); + self.ty_to_string(ty))[]); note_and_explain_region( self.tcx, "the pointer is valid for ", @@ -857,7 +857,7 @@ fn give_suggestion(&self, same_regions: &[SameRegions]) { let (fn_decl, generics, unsafety, ident, expl_self, span) = node_inner.expect("expect item fn"); let taken = lifetimes_in_scope(self.tcx, scope_id); - let life_giver = LifeGiver::with_taken(taken.as_slice()); + let life_giver = LifeGiver::with_taken(taken[]); let rebuilder = Rebuilder::new(self.tcx, fn_decl, expl_self, generics, same_regions, &life_giver); let (fn_decl, expl_self, generics) = rebuilder.rebuild(); @@ -933,7 +933,7 @@ fn rebuild(&self) } expl_self_opt = self.rebuild_expl_self(expl_self_opt, lifetime, &anon_nums, ®ion_names); - inputs = self.rebuild_args_ty(inputs.as_slice(), lifetime, + inputs = self.rebuild_args_ty(inputs[], lifetime, &anon_nums, ®ion_names); output = self.rebuild_output(&output, lifetime, &anon_nums, ®ion_names); ty_params = self.rebuild_ty_params(ty_params, lifetime, @@ -968,7 +968,7 @@ fn pick_lifetime(&self, names.push(lt_name); } names.sort(); - let name = token::str_to_ident(names[0].as_slice()).name; + let name = token::str_to_ident(names[0][]).name; return (name_to_dummy_lifetime(name), Kept); } return (self.life_giver.give_lifetime(), Fresh); @@ -1219,7 +1219,7 @@ fn rebuild_arg_ty_or_output(&self, .sess .fatal(format!( "unbound path {}", - pprust::path_to_string(path)).as_slice()) + pprust::path_to_string(path))[]) } Some(&d) => d }; @@ -1417,7 +1417,7 @@ fn give_expl_lifetime_param(&self, opt_explicit_self, generics); let msg = format!("consider using an explicit lifetime \ parameter as shown: {}", suggested_fn); - self.tcx.sess.span_help(span, msg.as_slice()); + self.tcx.sess.span_help(span, msg[]); } fn report_inference_failure(&self, @@ -1455,7 +1455,7 @@ fn report_inference_failure(&self, var_origin.span(), format!("cannot infer an appropriate lifetime{} \ due to conflicting requirements", - var_description).as_slice()); + var_description)[]); } fn note_region_origin(&self, origin: &SubregionOrigin<'tcx>) { @@ -1500,7 +1500,7 @@ fn note_region_origin(&self, origin: &SubregionOrigin<'tcx>) { self.tcx.sess.span_note( trace.origin.span(), format!("...so that {} ({})", - desc, values_str).as_slice()); + desc, values_str)[]); } None => { // Really should avoid printing this error at @@ -1509,7 +1509,7 @@ fn note_region_origin(&self, origin: &SubregionOrigin<'tcx>) { // doing right now. - nmatsakis self.tcx.sess.span_note( trace.origin.span(), - format!("...so that {}", desc).as_slice()); + format!("...so that {}", desc)[]); } } } @@ -1526,7 +1526,7 @@ fn note_region_origin(&self, origin: &SubregionOrigin<'tcx>) { "...so that closure can access `{}`", ty::local_var_name_str(self.tcx, upvar_id.var_id) .get() - .to_string()).as_slice()) + .to_string())[]) } infer::InfStackClosure(span) => { self.tcx.sess.span_note( @@ -1551,7 +1551,7 @@ fn note_region_origin(&self, origin: &SubregionOrigin<'tcx>) { does not outlive the enclosing closure", ty::local_var_name_str( self.tcx, - id).get().to_string()).as_slice()); + id).get().to_string())[]); } infer::IndexSlice(span) => { self.tcx.sess.span_note( @@ -1595,7 +1595,7 @@ fn note_region_origin(&self, origin: &SubregionOrigin<'tcx>) { span, format!("...so type `{}` of expression is valid during the \ expression", - self.ty_to_string(t)).as_slice()); + self.ty_to_string(t))[]); } infer::BindingTypeIsNotValidAtDecl(span) => { self.tcx.sess.span_note( @@ -1607,14 +1607,14 @@ fn note_region_origin(&self, origin: &SubregionOrigin<'tcx>) { span, format!("...so that the reference type `{}` \ does not outlive the data it points at", - self.ty_to_string(ty)).as_slice()); + self.ty_to_string(ty))[]); } infer::RelateParamBound(span, t) => { self.tcx.sess.span_note( span, format!("...so that the type `{}` \ will meet the declared lifetime bounds", - self.ty_to_string(t)).as_slice()); + self.ty_to_string(t))[]); } infer::RelateDefaultParamBound(span, t) => { self.tcx.sess.span_note( @@ -1622,13 +1622,13 @@ fn note_region_origin(&self, origin: &SubregionOrigin<'tcx>) { format!("...so that type parameter \ instantiated with `{}`, \ will meet its declared lifetime bounds", - self.ty_to_string(t)).as_slice()); + self.ty_to_string(t))[]); } infer::RelateRegionParamBound(span) => { self.tcx.sess.span_note( span, format!("...so that the declared lifetime parameter bounds \ - are satisfied").as_slice()); + are satisfied")[]); } } } @@ -1677,7 +1677,7 @@ fn lifetimes_in_scope(tcx: &ty::ctxt, Some(node) => match node { ast_map::NodeItem(item) => match item.node { ast::ItemFn(_, _, _, ref gen, _) => { - taken.push_all(gen.lifetimes.as_slice()); + taken.push_all(gen.lifetimes[]); None }, _ => None @@ -1685,7 +1685,7 @@ fn lifetimes_in_scope(tcx: &ty::ctxt, ast_map::NodeImplItem(ii) => { match *ii { ast::MethodImplItem(ref m) => { - taken.push_all(m.pe_generics().lifetimes.as_slice()); + taken.push_all(m.pe_generics().lifetimes[]); Some(m.id) } ast::TypeImplItem(_) => None, @@ -1744,10 +1744,10 @@ fn give_lifetime(&self) -> ast::Lifetime { let mut lifetime; loop { let mut s = String::from_str("'"); - s.push_str(num_to_string(self.counter.get()).as_slice()); + s.push_str(num_to_string(self.counter.get())[]); if !self.taken.contains(&s) { lifetime = name_to_dummy_lifetime( - token::str_to_ident(s.as_slice()).name); + token::str_to_ident(s[]).name); self.generated.borrow_mut().push(lifetime); break; } diff --git a/src/librustc/middle/infer/higher_ranked/mod.rs b/src/librustc/middle/infer/higher_ranked/mod.rs index ab0f98ec74a7..2a19f37e7d41 100644 --- a/src/librustc/middle/infer/higher_ranked/mod.rs +++ b/src/librustc/middle/infer/higher_ranked/mod.rs @@ -189,7 +189,7 @@ fn generalize_region(infcx: &InferCtxt, span, format!("region {} is not associated with \ any bound region from A!", - r0).as_slice()) + r0)[]) } } @@ -339,7 +339,7 @@ fn var_ids<'tcx, T: Combine<'tcx>>(combiner: &T, r => { combiner.infcx().tcx.sess.span_bug( combiner.trace().origin.span(), - format!("found non-region-vid: {}", r).as_slice()); + format!("found non-region-vid: {}", r)[]); } }).collect() } diff --git a/src/librustc/middle/infer/mod.rs b/src/librustc/middle/infer/mod.rs index 25eadae5b92f..6d031c865075 100644 --- a/src/librustc/middle/infer/mod.rs +++ b/src/librustc/middle/infer/mod.rs @@ -992,7 +992,7 @@ pub fn type_error_message_str_with_expected(&self, self.tcx.sess.span_err(sp, format!("{}{}", mk_msg(resolved_expected.map(|t| self.ty_to_string(t)), actual_ty), - error_str).as_slice()); + error_str)[]); for err in err.iter() { ty::note_and_explain_type_err(self.tcx, *err) diff --git a/src/librustc/middle/infer/region_inference/graphviz.rs b/src/librustc/middle/infer/region_inference/graphviz.rs index 3e55f6fa896b..0ca1a593ce7f 100644 --- a/src/librustc/middle/infer/region_inference/graphviz.rs +++ b/src/librustc/middle/infer/region_inference/graphviz.rs @@ -60,7 +60,7 @@ pub fn maybe_print_constraints_for<'a, 'tcx>(region_vars: &RegionVarBindings<'a, } let requested_node : Option = - os::getenv("RUST_REGION_GRAPH_NODE").and_then(|s|from_str(s.as_slice())); + os::getenv("RUST_REGION_GRAPH_NODE").and_then(|s| s.parse()); if requested_node.is_some() && requested_node != Some(subject_node) { return; diff --git a/src/librustc/middle/infer/region_inference/mod.rs b/src/librustc/middle/infer/region_inference/mod.rs index bcaf39cc8dbd..661f7e56429e 100644 --- a/src/librustc/middle/infer/region_inference/mod.rs +++ b/src/librustc/middle/infer/region_inference/mod.rs @@ -464,7 +464,7 @@ pub fn make_subregion(&self, origin.span(), format!("cannot relate bound region: {} <= {}", sub.repr(self.tcx), - sup.repr(self.tcx)).as_slice()); + sup.repr(self.tcx))[]); } (_, ReStatic) => { // all regions are subregions of static, so we can ignore this @@ -724,7 +724,7 @@ fn lub_concrete_regions(&self, a: Region, b: Region) -> Region { self.tcx.sess.bug( format!("cannot relate bound region: LUB({}, {})", a.repr(self.tcx), - b.repr(self.tcx)).as_slice()); + b.repr(self.tcx))[]); } (ReStatic, _) | (_, ReStatic) => { @@ -741,7 +741,7 @@ fn lub_concrete_regions(&self, a: Region, b: Region) -> Region { format!("lub_concrete_regions invoked with \ non-concrete regions: {}, {}", a, - b).as_slice()); + b)[]); } (ReFree(ref fr), ReScope(s_id)) | @@ -824,7 +824,7 @@ fn glb_concrete_regions(&self, self.tcx.sess.bug( format!("cannot relate bound region: GLB({}, {})", a.repr(self.tcx), - b.repr(self.tcx)).as_slice()); + b.repr(self.tcx))[]); } (ReStatic, r) | (r, ReStatic) => { @@ -844,7 +844,7 @@ fn glb_concrete_regions(&self, format!("glb_concrete_regions invoked with \ non-concrete regions: {}, {}", a, - b).as_slice()); + b)[]); } (ReFree(ref fr), ReScope(s_id)) | @@ -965,7 +965,7 @@ fn infer_variable_values(&self, self.expansion(var_data.as_mut_slice()); self.contraction(var_data.as_mut_slice()); let values = - self.extract_values_and_collect_conflicts(var_data.as_slice(), + self.extract_values_and_collect_conflicts(var_data[], errors); self.collect_concrete_region_errors(&values, errors); values @@ -1403,7 +1403,7 @@ fn free_regions_first(a: &RegionAndOrigin, for var {}, lower_bounds={}, upper_bounds={}", node_idx, lower_bounds.repr(self.tcx), - upper_bounds.repr(self.tcx)).as_slice()); + upper_bounds.repr(self.tcx))[]); } fn collect_error_for_contracting_node( @@ -1447,7 +1447,7 @@ fn collect_error_for_contracting_node( format!("collect_error_for_contracting_node() could not find error \ for var {}, upper_bounds={}", node_idx, - upper_bounds.repr(self.tcx)).as_slice()); + upper_bounds.repr(self.tcx))[]); } fn collect_concrete_regions(&self, diff --git a/src/librustc/middle/liveness.rs b/src/librustc/middle/liveness.rs index b76d798941ef..798daf8d5410 100644 --- a/src/librustc/middle/liveness.rs +++ b/src/librustc/middle/liveness.rs @@ -323,7 +323,7 @@ fn variable(&self, node_id: NodeId, span: Span) -> Variable { self.tcx .sess .span_bug(span, format!("no variable registered for id {}", - node_id).as_slice()); + node_id)[]); } } } @@ -594,7 +594,7 @@ fn live_node(&self, node_id: NodeId, span: Span) -> LiveNode { self.ir.tcx.sess.span_bug( span, format!("no live node registered for node {}", - node_id).as_slice()); + node_id)[]); } } } @@ -1129,7 +1129,7 @@ fn propagate_through_expr(&mut self, expr: &Expr, succ: LiveNode) // Uninteresting cases: just propagate in rev exec order ast::ExprVec(ref exprs) => { - self.propagate_through_exprs(exprs.as_slice(), succ) + self.propagate_through_exprs(exprs[], succ) } ast::ExprRepeat(ref element, ref count) => { @@ -1154,7 +1154,7 @@ fn propagate_through_expr(&mut self, expr: &Expr, succ: LiveNode) } else { succ }; - let succ = self.propagate_through_exprs(args.as_slice(), succ); + let succ = self.propagate_through_exprs(args[], succ); self.propagate_through_expr(&**f, succ) } @@ -1167,11 +1167,11 @@ fn propagate_through_expr(&mut self, expr: &Expr, succ: LiveNode) } else { succ }; - self.propagate_through_exprs(args.as_slice(), succ) + self.propagate_through_exprs(args[], succ) } ast::ExprTup(ref exprs) => { - self.propagate_through_exprs(exprs.as_slice(), succ) + self.propagate_through_exprs(exprs[], succ) } ast::ExprBinary(op, ref l, ref r) if ast_util::lazy_binop(op) => { diff --git a/src/librustc/middle/mem_categorization.rs b/src/librustc/middle/mem_categorization.rs index dce75579ca0a..1c2ceea77163 100644 --- a/src/librustc/middle/mem_categorization.rs +++ b/src/librustc/middle/mem_categorization.rs @@ -238,7 +238,7 @@ pub fn deref_kind<'tcx>(tcx: &ty::ctxt<'tcx>, t: Ty<'tcx>) -> deref_kind { None => { tcx.sess.bug( format!("deref_kind() invoked on non-derefable type {}", - ty_to_string(tcx, t)).as_slice()); + ty_to_string(tcx, t))[]); } } } @@ -635,7 +635,7 @@ pub fn cat_def(&self, span, format!("Upvar of non-closure {} - {}", fn_node_id, - ty.repr(self.tcx())).as_slice()); + ty.repr(self.tcx()))[]); } } } @@ -917,7 +917,7 @@ fn cat_deref(&self, self.tcx().sess.span_bug( node.span(), format!("Explicit deref of non-derefable type: {}", - base_cmt.ty.repr(self.tcx())).as_slice()); + base_cmt.ty.repr(self.tcx()))[]); } } } @@ -996,7 +996,7 @@ pub fn cat_index(&self, self.tcx().sess.span_bug( elt.span(), format!("Explicit index of non-index type `{}`", - base_cmt.ty.repr(self.tcx())).as_slice()); + base_cmt.ty.repr(self.tcx()))[]); } } } diff --git a/src/librustc/middle/privacy.rs b/src/librustc/middle/privacy.rs index f8b4ae73a1cd..6f63ae166fe4 100644 --- a/src/librustc/middle/privacy.rs +++ b/src/librustc/middle/privacy.rs @@ -615,10 +615,10 @@ fn report_error(&self, result: CheckResult) -> bool { match result { None => true, Some((span, msg, note)) => { - self.tcx.sess.span_err(span, msg.as_slice()); + self.tcx.sess.span_err(span, msg[]); match note { Some((span, msg)) => { - self.tcx.sess.span_note(span, msg.as_slice()) + self.tcx.sess.span_note(span, msg[]) } None => {}, } @@ -720,7 +720,7 @@ fn check_field(&mut self, UnnamedField(idx) => format!("field #{} of {} is private", idx + 1, struct_desc), }; - self.tcx.sess.span_err(span, msg.as_slice()); + self.tcx.sess.span_err(span, msg[]); } // Given the ID of a method, checks to ensure it's in scope. @@ -742,7 +742,7 @@ fn check_static_method(&mut self, method_id, None, format!("method `{}`", - string).as_slice())); + string)[])); } // Checks that a path is in scope. @@ -759,9 +759,7 @@ fn check_path(&mut self, span: Span, path_id: ast::NodeId, path: &ast::Path) { self.ensure_public(span, def, Some(origdid), - format!("{} `{}`", - tyname, - name).as_slice()) + format!("{} `{}`", tyname, name)[]) }; match self.last_private_map[path_id] { diff --git a/src/librustc/middle/reachable.rs b/src/librustc/middle/reachable.rs index 38d3b859c9d2..4d83075480bc 100644 --- a/src/librustc/middle/reachable.rs +++ b/src/librustc/middle/reachable.rs @@ -50,7 +50,7 @@ fn generics_require_inlining(generics: &ast::Generics) -> bool { // monomorphized or it was marked with `#[inline]`. This will only return // true for functions. fn item_might_be_inlined(item: &ast::Item) -> bool { - if attributes_specify_inlining(item.attrs.as_slice()) { + if attributes_specify_inlining(item.attrs[]) { return true } @@ -65,7 +65,7 @@ fn item_might_be_inlined(item: &ast::Item) -> bool { fn method_might_be_inlined(tcx: &ty::ctxt, method: &ast::Method, impl_src: ast::DefId) -> bool { - if attributes_specify_inlining(method.attrs.as_slice()) || + if attributes_specify_inlining(method.attrs[]) || generics_require_inlining(method.pe_generics()) { return true } @@ -202,7 +202,7 @@ fn def_id_represents_local_inlined_item(&self, def_id: ast::DefId) -> bool { ast::MethodImplItem(ref method) => { if generics_require_inlining(method.pe_generics()) || attributes_specify_inlining( - method.attrs.as_slice()) { + method.attrs[]) { true } else { let impl_did = self.tcx @@ -249,7 +249,7 @@ fn propagate(&mut self) { None => { self.tcx.sess.bug(format!("found unmapped ID in worklist: \ {}", - search_item).as_slice()) + search_item)[]) } } } @@ -341,7 +341,7 @@ fn propagate_node(&mut self, node: &ast_map::Node, .bug(format!("found unexpected thingy in worklist: {}", self.tcx .map - .node_to_string(search_item)).as_slice()) + .node_to_string(search_item))[]) } } } diff --git a/src/librustc/middle/resolve_lifetime.rs b/src/librustc/middle/resolve_lifetime.rs index be191801626a..bc9dc6b399d4 100644 --- a/src/librustc/middle/resolve_lifetime.rs +++ b/src/librustc/middle/resolve_lifetime.rs @@ -409,7 +409,7 @@ fn unresolved_lifetime_ref(&self, lifetime_ref: &ast::Lifetime) { self.sess.span_err( lifetime_ref.span, format!("use of undeclared lifetime name `{}`", - token::get_name(lifetime_ref.name)).as_slice()); + token::get_name(lifetime_ref.name))[]); } fn check_lifetime_defs(&mut self, old_scope: Scope, lifetimes: &Vec) { @@ -423,7 +423,7 @@ fn check_lifetime_defs(&mut self, old_scope: Scope, lifetimes: &Vec ty::Region { (space={}, index={})", region_name.as_str(), self.root_ty.repr(self.tcx()), - space, i).as_slice()); + space, i)[]); } } } @@ -677,7 +677,7 @@ fn ty_for_param(&self, p: ty::ParamTy, source_ty: Ty<'tcx>) -> Ty<'tcx> { p.space, p.idx, self.root_ty.repr(self.tcx()), - self.substs.repr(self.tcx())).as_slice()); + self.substs.repr(self.tcx()))[]); } }; diff --git a/src/librustc/middle/traits/coherence.rs b/src/librustc/middle/traits/coherence.rs index 9804f6d222af..d48685ce27d8 100644 --- a/src/librustc/middle/traits/coherence.rs +++ b/src/librustc/middle/traits/coherence.rs @@ -154,7 +154,7 @@ pub fn ty_is_local<'tcx>(tcx: &ty::ctxt<'tcx>, ty: Ty<'tcx>) -> bool { ty::ty_err => { tcx.sess.bug( format!("ty_is_local invoked on unexpected type: {}", - ty.repr(tcx)).as_slice()) + ty.repr(tcx))[]) } } } diff --git a/src/librustc/middle/traits/select.rs b/src/librustc/middle/traits/select.rs index 8ba28b61006b..2b42849a87b6 100644 --- a/src/librustc/middle/traits/select.rs +++ b/src/librustc/middle/traits/select.rs @@ -709,7 +709,7 @@ fn assemble_candidates_from_caller_bounds(&mut self, let all_bounds = util::transitive_bounds( - self.tcx(), caller_trait_refs.as_slice()); + self.tcx(), caller_trait_refs[]); let matching_bounds = all_bounds.filter( @@ -762,7 +762,7 @@ fn assemble_unboxed_closure_candidates(&mut self, self.tcx().sess.span_bug( obligation.cause.span, format!("No entry for unboxed closure: {}", - closure_def_id.repr(self.tcx())).as_slice()); + closure_def_id.repr(self.tcx()))[]); } }; @@ -1281,7 +1281,7 @@ fn builtin_bound(&mut self, self.tcx().sess.bug( format!( "asked to assemble builtin bounds of unexpected type: {}", - self_ty.repr(self.tcx())).as_slice()); + self_ty.repr(self.tcx()))[]); } }; @@ -1436,7 +1436,7 @@ fn confirm_builtin_candidate(&mut self, self.tcx().sess.span_bug( obligation.cause.span, format!("builtin bound for {} was ambig", - obligation.repr(self.tcx())).as_slice()); + obligation.repr(self.tcx()))[]); } } } @@ -1554,7 +1554,7 @@ fn confirm_fn_pointer_candidate(&mut self, self.tcx().sess.span_bug( obligation.cause.span, format!("Fn pointer candidate for inappropriate self type: {}", - self_ty.repr(self.tcx())).as_slice()); + self_ty.repr(self.tcx()))[]); } }; @@ -1595,7 +1595,7 @@ fn confirm_unboxed_closure_candidate(&mut self, self.tcx().sess.span_bug( obligation.cause.span, format!("No entry for unboxed closure: {}", - closure_def_id.repr(self.tcx())).as_slice()); + closure_def_id.repr(self.tcx()))[]); } }; @@ -1692,8 +1692,7 @@ fn rematch_impl(&mut self, self.tcx().sess.bug( format!("Impl {} was matchable against {} but now is not", impl_def_id.repr(self.tcx()), - obligation.repr(self.tcx())) - .as_slice()); + obligation.repr(self.tcx()))[]); } } } diff --git a/src/librustc/middle/ty.rs b/src/librustc/middle/ty.rs index 50a6fb9d0ca3..edaf2f167216 100644 --- a/src/librustc/middle/ty.rs +++ b/src/librustc/middle/ty.rs @@ -1891,7 +1891,7 @@ pub fn for_item(cx: &ctxt<'tcx>, id: NodeId) -> ParameterEnvironment<'tcx> { _ => { cx.sess.bug(format!("ParameterEnvironment::from_item(): \ `{}` is not an item", - cx.map.node_to_string(id)).as_slice()) + cx.map.node_to_string(id))[]) } } } @@ -1960,7 +1960,7 @@ pub fn trait_did(&self, cx: &ctxt) -> ast::DefId { }; match result { Ok(trait_did) => trait_did, - Err(err) => cx.sess.fatal(err.as_slice()), + Err(err) => cx.sess.fatal(err[]), } } } @@ -2596,7 +2596,7 @@ pub fn sequence_element_type<'tcx>(cx: &ctxt<'tcx>, ty: Ty<'tcx>) -> Ty<'tcx> { ty_str => mk_mach_uint(ast::TyU8), ty_open(ty) => sequence_element_type(cx, ty), _ => cx.sess.bug(format!("sequence_element_type called on non-sequence value: {}", - ty_to_string(cx, ty)).as_slice()), + ty_to_string(cx, ty))[]), } } @@ -2972,7 +2972,7 @@ fn tc_ty<'tcx>(cx: &ctxt<'tcx>, ty_struct(did, ref substs) => { let flds = struct_fields(cx, did, substs); let mut res = - TypeContents::union(flds.as_slice(), + TypeContents::union(flds[], |f| tc_mt(cx, f.mt, cache)); if !lookup_repr_hints(cx, did).contains(&attr::ReprExtern) { @@ -2989,21 +2989,21 @@ fn tc_ty<'tcx>(cx: &ctxt<'tcx>, // FIXME(#14449): `borrowed_contents` below assumes `&mut` // unboxed closure. let upvars = unboxed_closure_upvars(cx, did, substs); - TypeContents::union(upvars.as_slice(), + TypeContents::union(upvars[], |f| tc_ty(cx, f.ty, cache)) | borrowed_contents(r, MutMutable) } ty_tup(ref tys) => { - TypeContents::union(tys.as_slice(), + TypeContents::union(tys[], |ty| tc_ty(cx, *ty, cache)) } ty_enum(did, ref substs) => { let variants = substd_enum_variants(cx, did, substs); let mut res = - TypeContents::union(variants.as_slice(), |variant| { - TypeContents::union(variant.args.as_slice(), + TypeContents::union(variants[], |variant| { + TypeContents::union(variant.args[], |arg_ty| { tc_ty(cx, *arg_ty, cache) }) @@ -3068,7 +3068,7 @@ fn tc_ty<'tcx>(cx: &ctxt<'tcx>, kind_bounds_to_contents( cx, tp_def.bounds.builtin_bounds, - tp_def.bounds.trait_bounds.as_slice()) + tp_def.bounds.trait_bounds[]) } ty_infer(_) => { @@ -3658,7 +3658,7 @@ pub fn close_type<'tcx>(cx: &ctxt<'tcx>, ty: Ty<'tcx>) -> Ty<'tcx> { match ty.sty { ty_open(ty) => mk_rptr(cx, ReStatic, mt {ty: ty, mutbl:ast::MutImmutable}), _ => cx.sess.bug(format!("Trying to close a non-open type {}", - ty_to_string(cx, ty)).as_slice()) + ty_to_string(cx, ty))[]) } } @@ -3759,7 +3759,7 @@ pub fn node_id_to_trait_ref<'tcx>(cx: &ctxt<'tcx>, id: ast::NodeId) Some(ty) => ty.clone(), None => cx.sess.bug( format!("node_id_to_trait_ref: no trait ref for node `{}`", - cx.map.node_to_string(id)).as_slice()) + cx.map.node_to_string(id))[]) } } @@ -3772,7 +3772,7 @@ pub fn node_id_to_type<'tcx>(cx: &ctxt<'tcx>, id: ast::NodeId) -> Ty<'tcx> { Some(ty) => ty, None => cx.sess.bug( format!("node_id_to_type: no type for node `{}`", - cx.map.node_to_string(id)).as_slice()) + cx.map.node_to_string(id))[]) } } @@ -3865,7 +3865,7 @@ pub fn ty_region(tcx: &ctxt, tcx.sess.span_bug( span, format!("ty_region() invoked on an inappropriate ty: {}", - s).as_slice()); + s)[]); } } } @@ -3926,11 +3926,11 @@ pub fn expr_span(cx: &ctxt, id: NodeId) -> Span { Some(f) => { cx.sess.bug(format!("Node id {} is not an expr: {}", id, - f).as_slice()); + f)[]); } None => { cx.sess.bug(format!("Node id {} is not present \ - in the node map", id).as_slice()); + in the node map", id)[]); } } } @@ -3946,14 +3946,14 @@ pub fn local_var_name_str(cx: &ctxt, id: NodeId) -> InternedString { cx.sess.bug( format!("Variable id {} maps to {}, not local", id, - pat).as_slice()); + pat)[]); } } } r => { cx.sess.bug(format!("Variable id {} maps to {}, not local", id, - r).as_slice()); + r)[]); } } } @@ -3996,7 +3996,7 @@ pub fn adjust_ty<'tcx, F>(cx: &ctxt<'tcx>, cx.sess.bug( format!("add_env adjustment on non-bare-fn: \ {}", - b).as_slice()); + b)[]); } } } @@ -4024,7 +4024,7 @@ pub fn adjust_ty<'tcx, F>(cx: &ctxt<'tcx>, {}", i, ty_to_string(cx, adjusted_ty)) - .as_slice()); + []); } } } @@ -4087,7 +4087,7 @@ pub fn unsize_ty<'tcx>(cx: &ctxt<'tcx>, } _ => cx.sess.span_bug(span, format!("UnsizeLength with bad sty: {}", - ty_to_string(cx, ty)).as_slice()) + ty_to_string(cx, ty))[]) }, &UnsizeStruct(box ref k, tp_index) => match ty.sty { ty_struct(did, ref substs) => { @@ -4099,7 +4099,7 @@ pub fn unsize_ty<'tcx>(cx: &ctxt<'tcx>, } _ => cx.sess.span_bug(span, format!("UnsizeStruct with bad sty: {}", - ty_to_string(cx, ty)).as_slice()) + ty_to_string(cx, ty))[]) }, &UnsizeVtable(TyTrait { ref principal, bounds }, _) => { mk_trait(cx, (*principal).clone(), bounds) @@ -4112,7 +4112,7 @@ pub fn resolve_expr(tcx: &ctxt, expr: &ast::Expr) -> def::Def { Some(&def) => def, None => { tcx.sess.span_bug(expr.span, format!( - "no def-map entry for expr {}", expr.id).as_slice()); + "no def-map entry for expr {}", expr.id)[]); } } } @@ -4206,7 +4206,7 @@ pub fn expr_kind(tcx: &ctxt, expr: &ast::Expr) -> ExprKind { expr.span, format!("uncategorized def for expr {}: {}", expr.id, - def).as_slice()); + def)[]); } } } @@ -4331,7 +4331,7 @@ pub fn field_idx_strict(tcx: &ctxt, name: ast::Name, fields: &[field]) token::get_name(name), fields.iter() .map(|f| token::get_name(f.name).get().to_string()) - .collect::>()).as_slice()); + .collect::>())[]); } pub fn impl_or_trait_item_idx(id: ast::Name, trait_items: &[ImplOrTraitItem]) @@ -4565,7 +4565,7 @@ pub fn provided_trait_methods<'tcx>(cx: &ctxt<'tcx>, id: ast::DefId) match item.node { ItemTrait(_, _, _, _, ref ms) => { let (_, p) = - ast_util::split_trait_methods(ms.as_slice()); + ast_util::split_trait_methods(ms[]); p.iter() .map(|m| { match impl_or_trait_item( @@ -4584,14 +4584,14 @@ pub fn provided_trait_methods<'tcx>(cx: &ctxt<'tcx>, id: ast::DefId) _ => { cx.sess.bug(format!("provided_trait_methods: `{}` is \ not a trait", - id).as_slice()) + id)[]) } } } _ => { cx.sess.bug(format!("provided_trait_methods: `{}` is not a \ trait", - id).as_slice()) + id)[]) } } } else { @@ -4827,7 +4827,7 @@ pub fn from_ast_variant(cx: &ctxt<'tcx>, }, ast::StructVariantKind(ref struct_def) => { - let fields: &[StructField] = struct_def.fields.as_slice(); + let fields: &[StructField] = struct_def.fields[]; assert!(fields.len() > 0); @@ -4978,7 +4978,7 @@ pub fn enum_variants<'tcx>(cx: &ctxt<'tcx>, id: ast::DefId) cx.sess .span_err(e.span, format!("expected constant: {}", - *err).as_slice()); + *err)[]); } }, None => {} @@ -5258,7 +5258,7 @@ pub fn lookup_struct_fields(cx: &ctxt, did: ast::DefId) -> Vec { _ => { cx.sess.bug( format!("ID not mapped to struct fields: {}", - cx.map.node_to_string(did.node)).as_slice()); + cx.map.node_to_string(did.node))[]); } } } else { @@ -5291,7 +5291,7 @@ pub fn struct_fields<'tcx>(cx: &ctxt<'tcx>, did: ast::DefId, substs: &Substs<'tc pub fn tup_fields<'tcx>(v: &[Ty<'tcx>]) -> Vec> { v.iter().enumerate().map(|(i, &f)| { field { - name: token::intern(i.to_string().as_slice()), + name: token::intern(i.to_string()[]), mt: mt { ty: f, mutbl: MutImmutable @@ -5470,7 +5470,7 @@ pub fn eval_repeat_count(tcx: &ctxt, count_expr: &ast::Expr) -> uint { }; tcx.sess.span_err(count_expr.span, format!( "expected positive integer for repeat count, found {}", - found).as_slice()); + found)[]); } Err(_) => { let found = match count_expr.node { @@ -5485,7 +5485,7 @@ pub fn eval_repeat_count(tcx: &ctxt, count_expr: &ast::Expr) -> uint { }; tcx.sess.span_err(count_expr.span, format!( "expected constant integer for repeat count, found {}", - found).as_slice()); + found)[]); } } 0 @@ -6244,7 +6244,7 @@ pub fn with_freevars(tcx: &ty::ctxt, fid: ast::NodeId, f: F) -> T where { match tcx.freevars.borrow().get(&fid) { None => f(&[]), - Some(d) => f(d.as_slice()) + Some(d) => f(d[]) } } diff --git a/src/librustc/plugin/load.rs b/src/librustc/plugin/load.rs index 5c2fe0854ee7..a2e334543206 100644 --- a/src/librustc/plugin/load.rs +++ b/src/librustc/plugin/load.rs @@ -141,17 +141,17 @@ fn dylink_registrar(&mut self, vi: &ast::ViewItem, path: Path, symbol: String) { // this is fatal: there are almost certainly macros we need // inside this crate, so continue would spew "macro undefined" // errors - Err(err) => self.sess.span_fatal(vi.span, err.as_slice()) + Err(err) => self.sess.span_fatal(vi.span, err[]) }; unsafe { let registrar = - match lib.symbol(symbol.as_slice()) { + match lib.symbol(symbol[]) { Ok(registrar) => { mem::transmute::<*mut u8,PluginRegistrarFun>(registrar) } // again fatal if we can't register macros - Err(err) => self.sess.span_fatal(vi.span, err.as_slice()) + Err(err) => self.sess.span_fatal(vi.span, err[]) }; self.plugins.registrars.push(registrar); diff --git a/src/librustc/session/config.rs b/src/librustc/session/config.rs index 0652645907bc..335b74890631 100644 --- a/src/librustc/session/config.rs +++ b/src/librustc/session/config.rs @@ -555,17 +555,17 @@ pub fn build_codegen_options(matches: &getopts::Matches) -> CodegenOptions match (value, opt_type_desc) { (Some(..), None) => { early_error(format!("codegen option `{}` takes no \ - value", key).as_slice()) + value", key)[]) } (None, Some(type_desc)) => { early_error(format!("codegen option `{0}` requires \ {1} (-C {0}=)", - key, type_desc).as_slice()) + key, type_desc)[]) } (Some(value), Some(type_desc)) => { early_error(format!("incorrect value `{}` for codegen \ option `{}` - {} was expected", - value, key, type_desc).as_slice()) + value, key, type_desc)[]) } (None, None) => unreachable!() } @@ -575,7 +575,7 @@ pub fn build_codegen_options(matches: &getopts::Matches) -> CodegenOptions } if !found { early_error(format!("unknown codegen option: `{}`", - key).as_slice()); + key)[]); } } return cg; @@ -588,10 +588,10 @@ pub fn default_lib_output() -> CrateType { pub fn default_configuration(sess: &Session) -> ast::CrateConfig { use syntax::parse::token::intern_and_get_ident as intern; - let end = sess.target.target.target_endian.as_slice(); - let arch = sess.target.target.arch.as_slice(); - let wordsz = sess.target.target.target_word_size.as_slice(); - let os = sess.target.target.target_os.as_slice(); + let end = sess.target.target.target_endian[]; + let arch = sess.target.target.arch[]; + let wordsz = sess.target.target.target_word_size[]; + let os = sess.target.target.target_os[]; let fam = match sess.target.target.options.is_like_windows { true => InternedString::new("windows"), @@ -627,23 +627,23 @@ pub fn build_configuration(sess: &Session) -> ast::CrateConfig { append_configuration(&mut user_cfg, InternedString::new("test")) } let mut v = user_cfg.into_iter().collect::>(); - v.push_all(default_cfg.as_slice()); + v.push_all(default_cfg[]); v } pub fn build_target_config(opts: &Options, sp: &SpanHandler) -> Config { - let target = match Target::search(opts.target_triple.as_slice()) { + let target = match Target::search(opts.target_triple[]) { Ok(t) => t, Err(e) => { - sp.handler().fatal((format!("Error loading target specification: {}", e)).as_slice()); + sp.handler().fatal((format!("Error loading target specification: {}", e))[]); } }; - let (int_type, uint_type) = match target.target_word_size.as_slice() { + let (int_type, uint_type) = match target.target_word_size[] { "32" => (ast::TyI32, ast::TyU32), "64" => (ast::TyI64, ast::TyU64), w => sp.handler().fatal((format!("target specification was invalid: unrecognized \ - target-word-size {}", w)).as_slice()) + target-word-size {}", w))[]) }; Config { @@ -756,7 +756,7 @@ pub fn build_session_options(matches: &getopts::Matches) -> Options { let unparsed_crate_types = matches.opt_strs("crate-type"); let crate_types = parse_crate_types_from_list(unparsed_crate_types) - .unwrap_or_else(|e| early_error(e.as_slice())); + .unwrap_or_else(|e| early_error(e[])); let mut lint_opts = vec!(); let mut describe_lints = false; @@ -766,7 +766,7 @@ pub fn build_session_options(matches: &getopts::Matches) -> Options { if lint_name == "help" { describe_lints = true; } else { - lint_opts.push((lint_name.replace("-", "_").into_string(), level)); + lint_opts.push((lint_name.replace("-", "_"), level)); } } } @@ -784,7 +784,7 @@ pub fn build_session_options(matches: &getopts::Matches) -> Options { } if this_bit == 0 { early_error(format!("unknown debug flag: {}", - *debug_flag).as_slice()) + *debug_flag)[]) } debugging_opts |= this_bit; } @@ -829,7 +829,7 @@ pub fn build_session_options(matches: &getopts::Matches) -> Options { "dep-info" => OutputTypeDepInfo, _ => { early_error(format!("unknown emission type: `{}`", - part).as_slice()) + part)[]) } }; output_types.push(output_type) @@ -868,7 +868,7 @@ pub fn build_session_options(matches: &getopts::Matches) -> Options { Some(arg) => { early_error(format!("optimization level needs to be \ between 0-3 (instead was `{}`)", - arg).as_slice()); + arg)[]); } } } else { @@ -906,7 +906,7 @@ pub fn build_session_options(matches: &getopts::Matches) -> Options { Some(arg) => { early_error(format!("debug info level needs to be between \ 0-2 (instead was `{}`)", - arg).as_slice()); + arg)[]); } } } else { @@ -923,7 +923,7 @@ pub fn build_session_options(matches: &getopts::Matches) -> Options { }; let addl_lib_search_paths = matches.opt_strs("L").iter().map(|s| { - Path::new(s.as_slice()) + Path::new(s[]) }).collect(); let libs = matches.opt_strs("l").into_iter().map(|s| { @@ -937,7 +937,7 @@ pub fn build_session_options(matches: &getopts::Matches) -> Options { (_, s) => { early_error(format!("unknown library kind `{}`, expected \ one of dylib, framework, or static", - s).as_slice()); + s)[]); } }; (name.to_string(), kind) @@ -982,7 +982,7 @@ pub fn build_session_options(matches: &getopts::Matches) -> Options { // --debuginfo"); } - let color = match matches.opt_str("color").as_ref().map(|s| s.as_slice()) { + let color = match matches.opt_str("color").as_ref().map(|s| s[]) { Some("auto") => Auto, Some("always") => Always, Some("never") => Never, @@ -992,7 +992,7 @@ pub fn build_session_options(matches: &getopts::Matches) -> Options { Some(arg) => { early_error(format!("argument for --color must be auto, always \ or never (instead was `{}`)", - arg).as_slice()) + arg)[]) } }; @@ -1093,7 +1093,7 @@ mod test { #[test] fn test_switch_implies_cfg_test() { let matches = - &match getopts(&["--test".to_string()], optgroups().as_slice()) { + &match getopts(&["--test".to_string()], optgroups()[]) { Ok(m) => m, Err(f) => panic!("test_switch_implies_cfg_test: {}", f) }; @@ -1101,7 +1101,7 @@ fn test_switch_implies_cfg_test() { let sessopts = build_session_options(matches); let sess = build_session(sessopts, None, registry); let cfg = build_configuration(&sess); - assert!((attr::contains_name(cfg.as_slice(), "test"))); + assert!((attr::contains_name(cfg[], "test"))); } // When the user supplies --test and --cfg test, don't implicitly add @@ -1110,7 +1110,7 @@ fn test_switch_implies_cfg_test() { fn test_switch_implies_cfg_test_unless_cfg_test() { let matches = &match getopts(&["--test".to_string(), "--cfg=test".to_string()], - optgroups().as_slice()) { + optgroups()[]) { Ok(m) => m, Err(f) => { panic!("test_switch_implies_cfg_test_unless_cfg_test: {}", f) @@ -1130,7 +1130,7 @@ fn test_can_print_warnings() { { let matches = getopts(&[ "-Awarnings".to_string() - ], optgroups().as_slice()).unwrap(); + ], optgroups()[]).unwrap(); let registry = diagnostics::registry::Registry::new(&[]); let sessopts = build_session_options(&matches); let sess = build_session(sessopts, None, registry); @@ -1141,7 +1141,7 @@ fn test_can_print_warnings() { let matches = getopts(&[ "-Awarnings".to_string(), "-Dwarnings".to_string() - ], optgroups().as_slice()).unwrap(); + ], optgroups()[]).unwrap(); let registry = diagnostics::registry::Registry::new(&[]); let sessopts = build_session_options(&matches); let sess = build_session(sessopts, None, registry); @@ -1151,7 +1151,7 @@ fn test_can_print_warnings() { { let matches = getopts(&[ "-Adead_code".to_string() - ], optgroups().as_slice()).unwrap(); + ], optgroups()[]).unwrap(); let registry = diagnostics::registry::Registry::new(&[]); let sessopts = build_session_options(&matches); let sess = build_session(sessopts, None, registry); diff --git a/src/librustc/session/mod.rs b/src/librustc/session/mod.rs index 8516ece202c7..37bdd1673e9c 100644 --- a/src/librustc/session/mod.rs +++ b/src/librustc/session/mod.rs @@ -172,7 +172,7 @@ pub fn codemap<'a>(&'a self) -> &'a codemap::CodeMap { // cases later on pub fn impossible_case(&self, sp: Span, msg: &str) -> ! { self.span_bug(sp, - format!("impossible case reached: {}", msg).as_slice()); + format!("impossible case reached: {}", msg)[]); } pub fn verbose(&self) -> bool { self.debugging_opt(config::VERBOSE) } pub fn time_passes(&self) -> bool { self.debugging_opt(config::TIME_PASSES) } @@ -211,7 +211,7 @@ pub fn sysroot<'a>(&'a self) -> &'a Path { } pub fn target_filesearch<'a>(&'a self) -> filesearch::FileSearch<'a> { filesearch::FileSearch::new(self.sysroot(), - self.opts.target_triple.as_slice(), + self.opts.target_triple[], &self.opts.addl_lib_search_paths) } pub fn host_filesearch<'a>(&'a self) -> filesearch::FileSearch<'a> { diff --git a/src/librustc/util/common.rs b/src/librustc/util/common.rs index bc6fb1be0758..e1448364a9e0 100644 --- a/src/librustc/util/common.rs +++ b/src/librustc/util/common.rs @@ -14,6 +14,7 @@ use std::collections::HashMap; use std::fmt::Show; use std::hash::{Hash, Hasher}; +use std::iter::repeat; use std::time::Duration; use syntax::ast; @@ -48,7 +49,7 @@ pub fn time(do_it: bool, what: &str, u: U, f: F) -> T where }; let rv = rv.unwrap(); - println!("{}time: {}.{:03} \t{}", " ".repeat(old), + println!("{}time: {}.{:03} \t{}", repeat(" ").take(old).collect::(), dur.num_seconds(), dur.num_milliseconds() % 1000, what); DEPTH.with(|slot| slot.set(old)); diff --git a/src/librustc/util/ppaux.rs b/src/librustc/util/ppaux.rs index 85a06125e23a..5f61c04d3668 100644 --- a/src/librustc/util/ppaux.rs +++ b/src/librustc/util/ppaux.rs @@ -55,12 +55,12 @@ pub fn note_and_explain_region(cx: &ctxt, (ref str, Some(span)) => { cx.sess.span_note( span, - format!("{}{}{}", prefix, *str, suffix).as_slice()); + format!("{}{}{}", prefix, *str, suffix)[]); Some(span) } (ref str, None) => { cx.sess.note( - format!("{}{}{}", prefix, *str, suffix).as_slice()); + format!("{}{}{}", prefix, *str, suffix)[]); None } } @@ -269,7 +269,7 @@ fn bare_fn_to_string<'tcx>(cx: &ctxt<'tcx>, }; if abi != abi::Rust { - s.push_str(format!("extern {} ", abi.to_string()).as_slice()); + s.push_str(format!("extern {} ", abi.to_string())[]); }; s.push_str("fn"); @@ -293,7 +293,7 @@ fn closure_to_string<'tcx>(cx: &ctxt<'tcx>, cty: &ty::ClosureTy<'tcx>) -> String match cty.store { ty::UniqTraitStore => {} ty::RegionTraitStore(region, _) => { - s.push_str(region_to_string(cx, "", true, region).as_slice()); + s.push_str(region_to_string(cx, "", true, region)[]); } } @@ -312,7 +312,7 @@ fn closure_to_string<'tcx>(cx: &ctxt<'tcx>, cty: &ty::ClosureTy<'tcx>) -> String assert_eq!(cty.onceness, ast::Once); s.push_str("proc"); push_sig_to_string(cx, &mut s, '(', ')', &cty.sig, - bounds_str.as_slice()); + bounds_str[]); } ty::RegionTraitStore(..) => { match cty.onceness { @@ -320,7 +320,7 @@ fn closure_to_string<'tcx>(cx: &ctxt<'tcx>, cty: &ty::ClosureTy<'tcx>) -> String ast::Once => s.push_str("once ") } push_sig_to_string(cx, &mut s, '|', '|', &cty.sig, - bounds_str.as_slice()); + bounds_str[]); } } @@ -353,7 +353,7 @@ fn push_sig_to_string<'tcx>(cx: &ctxt<'tcx>, ty::FnConverging(t) => { if !ty::type_is_nil(t) { s.push_str(" -> "); - s.push_str(ty_to_string(cx, t).as_slice()); + s.push_str(ty_to_string(cx, t)[]); } } ty::FnDiverging => { @@ -390,7 +390,7 @@ fn infer_ty_to_string(cx: &ctxt, ty: ty::InferTy) -> String { } ty_rptr(r, ref tm) => { let mut buf = region_ptr_to_string(cx, r); - buf.push_str(mt_to_string(cx, tm).as_slice()); + buf.push_str(mt_to_string(cx, tm)[]); buf } ty_open(typ) => @@ -400,7 +400,7 @@ fn infer_ty_to_string(cx: &ctxt, ty: ty::InferTy) -> String { .iter() .map(|elem| ty_to_string(cx, *elem)) .collect::>(); - match strs.as_slice() { + match strs[] { [ref string] => format!("({},)", string), strs => format!("({})", strs.connect(", ")) } @@ -551,7 +551,7 @@ pub fn parameterized<'tcx>(cx: &ctxt<'tcx>, pub fn ty_to_short_str<'tcx>(cx: &ctxt<'tcx>, typ: Ty<'tcx>) -> String { let mut s = typ.repr(cx).to_string(); if s.len() >= 32u { - s = s.slice(0u, 32u).to_string(); + s = s[0u..32u].to_string(); } return s; } @@ -616,7 +616,7 @@ fn repr(&self, tcx: &ctxt<'tcx>) -> String { impl<'tcx, T:Repr<'tcx>> Repr<'tcx> for OwnedSlice { fn repr(&self, tcx: &ctxt<'tcx>) -> String { - repr_vec(tcx, self.as_slice()) + repr_vec(tcx, self[]) } } @@ -624,7 +624,7 @@ fn repr(&self, tcx: &ctxt<'tcx>) -> String { // autoderef cannot convert the &[T] handler impl<'tcx, T:Repr<'tcx>> Repr<'tcx> for Vec { fn repr(&self, tcx: &ctxt<'tcx>) -> String { - repr_vec(tcx, self.as_slice()) + repr_vec(tcx, self[]) } } diff --git a/src/librustc_back/archive.rs b/src/librustc_back/archive.rs index 3a4510703166..0bd4265e487a 100644 --- a/src/librustc_back/archive.rs +++ b/src/librustc_back/archive.rs @@ -53,7 +53,7 @@ fn run_ar(handler: &ErrorHandler, maybe_ar_prog: &Option, args: &str, cwd: Option<&Path>, paths: &[&Path]) -> ProcessOutput { let ar = match *maybe_ar_prog { - Some(ref ar) => ar.as_slice(), + Some(ref ar) => ar[], None => "ar" }; let mut cmd = Command::new(ar); @@ -75,22 +75,22 @@ fn run_ar(handler: &ErrorHandler, maybe_ar_prog: &Option, if !o.status.success() { handler.err(format!("{} failed with: {}", cmd, - o.status).as_slice()); + o.status)[]); handler.note(format!("stdout ---\n{}", str::from_utf8(o.output - .as_slice()).unwrap()) - .as_slice()); + []).unwrap()) + []); handler.note(format!("stderr ---\n{}", str::from_utf8(o.error - .as_slice()).unwrap()) - .as_slice()); + []).unwrap()) + []); handler.abort_if_errors(); } o }, Err(e) => { - handler.err(format!("could not exec `{}`: {}", ar.as_slice(), - e).as_slice()); + handler.err(format!("could not exec `{}`: {}", ar[], + e)[]); handler.abort_if_errors(); panic!("rustc::back::archive::run_ar() should not reach this point"); } @@ -106,16 +106,16 @@ pub fn find_library(name: &str, osprefix: &str, ossuffix: &str, for path in search_paths.iter() { debug!("looking for {} inside {}", name, path.display()); - let test = path.join(oslibname.as_slice()); + let test = path.join(oslibname[]); if test.exists() { return test } if oslibname != unixlibname { - let test = path.join(unixlibname.as_slice()); + let test = path.join(unixlibname[]); if test.exists() { return test } } } handler.fatal(format!("could not find native static library `{}`, \ perhaps an -L flag is missing?", - name).as_slice()); + name)[]); } impl<'a> Archive<'a> { @@ -147,7 +147,7 @@ pub fn remove_file(&mut self, file: &str) { /// Lists all files in an archive pub fn files(&self) -> Vec { let output = run_ar(self.handler, &self.maybe_ar_prog, "t", None, &[&self.dst]); - let output = str::from_utf8(output.output.as_slice()).unwrap(); + let output = str::from_utf8(output.output[]).unwrap(); // use lines_any because windows delimits output with `\r\n` instead of // just `\n` output.lines_any().map(|s| s.to_string()).collect() @@ -179,9 +179,9 @@ pub fn create(config: ArchiveConfig<'a>) -> ArchiveBuilder<'a> { /// search in the relevant locations for a library named `name`. pub fn add_native_library(&mut self, name: &str) -> io::IoResult<()> { let location = find_library(name, - self.archive.slib_prefix.as_slice(), - self.archive.slib_suffix.as_slice(), - self.archive.lib_search_paths.as_slice(), + self.archive.slib_prefix[], + self.archive.slib_suffix[], + self.archive.lib_search_paths[], self.archive.handler); self.add_archive(&location, name, |_| false) } @@ -197,12 +197,12 @@ pub fn add_rlib(&mut self, rlib: &Path, name: &str, // as simple comparison is not enough - there // might be also an extra name suffix let obj_start = format!("{}", name); - let obj_start = obj_start.as_slice(); + let obj_start = obj_start[]; // Ignoring all bytecode files, no matter of // name let bc_ext = ".bytecode.deflate"; - self.add_archive(rlib, name.as_slice(), |fname: &str| { + self.add_archive(rlib, name[], |fname: &str| { let skip_obj = lto && fname.starts_with(obj_start) && fname.ends_with(".o"); skip_obj || fname.ends_with(bc_ext) || fname == METADATA_FILENAME @@ -239,7 +239,7 @@ pub fn build(self) -> Archive<'a> { // allow running `ar s file.a` to update symbols only. if self.should_update_symbols { run_ar(self.archive.handler, &self.archive.maybe_ar_prog, - "s", Some(self.work_dir.path()), args.as_slice()); + "s", Some(self.work_dir.path()), args[]); } return self.archive; } @@ -259,7 +259,7 @@ pub fn build(self) -> Archive<'a> { // Add the archive members seen so far, without updating the // symbol table (`S`). run_ar(self.archive.handler, &self.archive.maybe_ar_prog, - "cruS", Some(self.work_dir.path()), args.as_slice()); + "cruS", Some(self.work_dir.path()), args[]); args.clear(); args.push(&abs_dst); @@ -274,7 +274,7 @@ pub fn build(self) -> Archive<'a> { // necessary. let flags = if self.should_update_symbols { "crus" } else { "cruS" }; run_ar(self.archive.handler, &self.archive.maybe_ar_prog, - flags, Some(self.work_dir.path()), args.as_slice()); + flags, Some(self.work_dir.path()), args[]); self.archive } @@ -316,7 +316,7 @@ fn add_archive(&mut self, archive: &Path, name: &str, mut skip: F) -> io::IoR } else { filename }; - let new_filename = self.work_dir.path().join(filename.as_slice()); + let new_filename = self.work_dir.path().join(filename[]); try!(fs::rename(file, &new_filename)); self.members.push(Path::new(filename)); } diff --git a/src/librustc_back/rpath.rs b/src/librustc_back/rpath.rs index 1f8549098d94..1056ac928e68 100644 --- a/src/librustc_back/rpath.rs +++ b/src/librustc_back/rpath.rs @@ -44,15 +44,15 @@ pub fn get_rpath_flags(config: RPathConfig) -> Vec where l.map(|p| p.clone()) }).collect::>(); - let rpaths = get_rpaths(config, libs.as_slice()); - flags.push_all(rpaths_to_flags(rpaths.as_slice()).as_slice()); + let rpaths = get_rpaths(config, libs[]); + flags.push_all(rpaths_to_flags(rpaths[])[]); flags } fn rpaths_to_flags(rpaths: &[String]) -> Vec { let mut ret = Vec::new(); for rpath in rpaths.iter() { - ret.push(format!("-Wl,-rpath,{}", (*rpath).as_slice())); + ret.push(format!("-Wl,-rpath,{}", (*rpath)[])); } return ret; } @@ -82,14 +82,14 @@ fn log_rpaths(desc: &str, rpaths: &[String]) { } } - log_rpaths("relative", rel_rpaths.as_slice()); - log_rpaths("fallback", fallback_rpaths.as_slice()); + log_rpaths("relative", rel_rpaths[]); + log_rpaths("fallback", fallback_rpaths[]); let mut rpaths = rel_rpaths; - rpaths.push_all(fallback_rpaths.as_slice()); + rpaths.push_all(fallback_rpaths[]); // Remove duplicates - let rpaths = minimize_rpaths(rpaths.as_slice()); + let rpaths = minimize_rpaths(rpaths[]); return rpaths; } @@ -140,7 +140,7 @@ fn minimize_rpaths(rpaths: &[String]) -> Vec { let mut set = HashSet::new(); let mut minimized = Vec::new(); for rpath in rpaths.iter() { - if set.insert(rpath.as_slice()) { + if set.insert(rpath[]) { minimized.push(rpath.clone()); } } diff --git a/src/librustc_back/svh.rs b/src/librustc_back/svh.rs index 98fa659ba55e..d40c9ee8af6a 100644 --- a/src/librustc_back/svh.rs +++ b/src/librustc_back/svh.rs @@ -65,7 +65,7 @@ pub fn new(hash: &str) -> Svh { } pub fn as_str<'a>(&'a self) -> &'a str { - self.hash.as_slice() + self.hash[] } pub fn calculate(metadata: &Vec, krate: &ast::Crate) -> Svh { @@ -358,7 +358,7 @@ fn visit_mac(&mut self, macro: &Mac) { fn macro_name(macro: &Mac) -> token::InternedString { match ¯o.node { &MacInvocTT(ref path, ref _tts, ref _stx_ctxt) => { - let s = path.segments.as_slice(); + let s = path.segments[]; assert_eq!(s.len(), 1); content(s[0].identifier) } diff --git a/src/librustc_back/target/mod.rs b/src/librustc_back/target/mod.rs index d12cb356e3fa..99a25bebf40a 100644 --- a/src/librustc_back/target/mod.rs +++ b/src/librustc_back/target/mod.rs @@ -224,7 +224,7 @@ pub fn from_json(obj: Json) -> Target { Some(val) => val, None => handler.fatal((format!("Field {} in target specification is required", name)) - .as_slice()) + []) } }; @@ -365,7 +365,7 @@ macro_rules! load_specific ( let target_path = os::getenv("RUST_TARGET_PATH").unwrap_or(String::new()); - let paths = os::split_paths(target_path.as_slice()); + let paths = os::split_paths(target_path[]); // FIXME 16351: add a sane default search path? for dir in paths.iter() { diff --git a/src/librustc_borrowck/borrowck/check_loans.rs b/src/librustc_borrowck/borrowck/check_loans.rs index 3bf817b42b06..568bb023b68a 100644 --- a/src/librustc_borrowck/borrowck/check_loans.rs +++ b/src/librustc_borrowck/borrowck/check_loans.rs @@ -469,7 +469,7 @@ pub fn report_error_if_loan_conflicts_with_restriction(&self, new_loan.span, format!("cannot borrow `{}`{} as mutable \ more than once at a time", - nl, new_loan_msg).as_slice()) + nl, new_loan_msg)[]) } (ty::UniqueImmBorrow, _) => { @@ -477,7 +477,7 @@ pub fn report_error_if_loan_conflicts_with_restriction(&self, new_loan.span, format!("closure requires unique access to `{}` \ but {} is already borrowed{}", - nl, ol_pronoun, old_loan_msg).as_slice()); + nl, ol_pronoun, old_loan_msg)[]); } (_, ty::UniqueImmBorrow) => { @@ -485,7 +485,7 @@ pub fn report_error_if_loan_conflicts_with_restriction(&self, new_loan.span, format!("cannot borrow `{}`{} as {} because \ previous closure requires unique access", - nl, new_loan_msg, new_loan.kind.to_user_str()).as_slice()); + nl, new_loan_msg, new_loan.kind.to_user_str())[]); } (_, _) => { @@ -498,7 +498,7 @@ pub fn report_error_if_loan_conflicts_with_restriction(&self, new_loan.kind.to_user_str(), ol_pronoun, old_loan.kind.to_user_str(), - old_loan_msg).as_slice()); + old_loan_msg)[]); } } @@ -507,7 +507,7 @@ pub fn report_error_if_loan_conflicts_with_restriction(&self, self.bccx.span_note( span, format!("borrow occurs due to use of `{}` in closure", - nl).as_slice()); + nl)[]); } _ => { } } @@ -556,7 +556,7 @@ pub fn report_error_if_loan_conflicts_with_restriction(&self, self.bccx.span_note( old_loan.span, - format!("{}; {}", borrow_summary, rule_summary).as_slice()); + format!("{}; {}", borrow_summary, rule_summary)[]); let old_loan_span = self.tcx().map.span(old_loan.kill_scope.node_id()); self.bccx.span_end_note(old_loan_span, @@ -626,13 +626,13 @@ fn check_for_copy_of_frozen_path(&self, self.bccx.span_err( span, format!("cannot use `{}` because it was mutably borrowed", - self.bccx.loan_path_to_string(copy_path).as_slice()) - .as_slice()); + self.bccx.loan_path_to_string(copy_path)[]) + []); self.bccx.span_note( loan_span, format!("borrow of `{}` occurs here", - self.bccx.loan_path_to_string(&*loan_path).as_slice()) - .as_slice()); + self.bccx.loan_path_to_string(&*loan_path)[]) + []); } } } @@ -651,20 +651,20 @@ fn check_for_move_of_borrowed_path(&self, let err_message = match move_kind { move_data::Captured => format!("cannot move `{}` into closure because it is borrowed", - self.bccx.loan_path_to_string(move_path).as_slice()), + self.bccx.loan_path_to_string(move_path)[]), move_data::Declared | move_data::MoveExpr | move_data::MovePat => format!("cannot move out of `{}` because it is borrowed", - self.bccx.loan_path_to_string(move_path).as_slice()) + self.bccx.loan_path_to_string(move_path)[]) }; - self.bccx.span_err(span, err_message.as_slice()); + self.bccx.span_err(span, err_message[]); self.bccx.span_note( loan_span, format!("borrow of `{}` occurs here", - self.bccx.loan_path_to_string(&*loan_path).as_slice()) - .as_slice()); + self.bccx.loan_path_to_string(&*loan_path)[]) + []); } } } @@ -814,7 +814,7 @@ fn check_assignment(&self, self.bccx.span_err( assignment_span, format!("cannot assign to {}", - self.bccx.cmt_to_string(&*assignee_cmt)).as_slice()); + self.bccx.cmt_to_string(&*assignee_cmt))[]); self.bccx.span_help( self.tcx().map.span(upvar_id.closure_expr_id), "consider changing this closure to take self by mutable reference"); @@ -823,7 +823,7 @@ fn check_assignment(&self, assignment_span, format!("cannot assign to {} {}", assignee_cmt.mutbl.to_user_str(), - self.bccx.cmt_to_string(&*assignee_cmt)).as_slice()); + self.bccx.cmt_to_string(&*assignee_cmt))[]); } } _ => match opt_loan_path(&assignee_cmt) { @@ -833,14 +833,14 @@ fn check_assignment(&self, format!("cannot assign to {} {} `{}`", assignee_cmt.mutbl.to_user_str(), self.bccx.cmt_to_string(&*assignee_cmt), - self.bccx.loan_path_to_string(&*lp)).as_slice()); + self.bccx.loan_path_to_string(&*lp))[]); } None => { self.bccx.span_err( assignment_span, format!("cannot assign to {} {}", assignee_cmt.mutbl.to_user_str(), - self.bccx.cmt_to_string(&*assignee_cmt)).as_slice()); + self.bccx.cmt_to_string(&*assignee_cmt))[]); } } } @@ -960,10 +960,10 @@ pub fn report_illegal_mutation(&self, self.bccx.span_err( span, format!("cannot assign to `{}` because it is borrowed", - self.bccx.loan_path_to_string(loan_path)).as_slice()); + self.bccx.loan_path_to_string(loan_path))[]); self.bccx.span_note( loan.span, format!("borrow of `{}` occurs here", - self.bccx.loan_path_to_string(loan_path)).as_slice()); + self.bccx.loan_path_to_string(loan_path))[]); } } diff --git a/src/librustc_borrowck/borrowck/fragments.rs b/src/librustc_borrowck/borrowck/fragments.rs index 25ed51825554..dbbc52cf3623 100644 --- a/src/librustc_borrowck/borrowck/fragments.rs +++ b/src/librustc_borrowck/borrowck/fragments.rs @@ -124,12 +124,12 @@ pub fn instrument_move_fragments<'tcx>(this: &MoveData<'tcx>, let attrs : &[ast::Attribute]; attrs = match tcx.map.find(id) { Some(ast_map::NodeItem(ref item)) => - item.attrs.as_slice(), + item.attrs[], Some(ast_map::NodeImplItem(&ast::MethodImplItem(ref m))) => - m.attrs.as_slice(), + m.attrs[], Some(ast_map::NodeTraitItem(&ast::ProvidedMethod(ref m))) => - m.attrs.as_slice(), - _ => [].as_slice(), + m.attrs[], + _ => [][], }; let span_err = @@ -145,7 +145,7 @@ pub fn instrument_move_fragments<'tcx>(this: &MoveData<'tcx>, for (i, mpi) in vec_rc.iter().enumerate() { let render = || this.path_loan_path(*mpi).user_string(tcx); if span_err { - tcx.sess.span_err(sp, format!("{}: `{}`", kind, render()).as_slice()); + tcx.sess.span_err(sp, format!("{}: `{}`", kind, render())[]); } if print { println!("id:{} {}[{}] `{}`", id, kind, i, render()); @@ -157,7 +157,7 @@ pub fn instrument_move_fragments<'tcx>(this: &MoveData<'tcx>, for (i, f) in vec_rc.iter().enumerate() { let render = || f.loan_path_user_string(this, tcx); if span_err { - tcx.sess.span_err(sp, format!("{}: `{}`", kind, render()).as_slice()); + tcx.sess.span_err(sp, format!("{}: `{}`", kind, render())[]); } if print { println!("id:{} {}[{}] `{}`", id, kind, i, render()); @@ -199,11 +199,11 @@ pub fn fixup_fragment_sets<'tcx>(this: &MoveData<'tcx>, tcx: &ty::ctxt<'tcx>) { // First, filter out duplicates moved.sort(); moved.dedup(); - debug!("fragments 1 moved: {}", path_lps(moved.as_slice())); + debug!("fragments 1 moved: {}", path_lps(moved[])); assigned.sort(); assigned.dedup(); - debug!("fragments 1 assigned: {}", path_lps(assigned.as_slice())); + debug!("fragments 1 assigned: {}", path_lps(assigned[])); // Second, build parents from the moved and assigned. for m in moved.iter() { @@ -223,14 +223,14 @@ pub fn fixup_fragment_sets<'tcx>(this: &MoveData<'tcx>, tcx: &ty::ctxt<'tcx>) { parents.sort(); parents.dedup(); - debug!("fragments 2 parents: {}", path_lps(parents.as_slice())); + debug!("fragments 2 parents: {}", path_lps(parents[])); // Third, filter the moved and assigned fragments down to just the non-parents - moved.retain(|f| non_member(*f, parents.as_slice())); - debug!("fragments 3 moved: {}", path_lps(moved.as_slice())); + moved.retain(|f| non_member(*f, parents[])); + debug!("fragments 3 moved: {}", path_lps(moved[])); - assigned.retain(|f| non_member(*f, parents.as_slice())); - debug!("fragments 3 assigned: {}", path_lps(assigned.as_slice())); + assigned.retain(|f| non_member(*f, parents[])); + debug!("fragments 3 assigned: {}", path_lps(assigned[])); // Fourth, build the leftover from the moved, assigned, and parents. for m in moved.iter() { @@ -248,16 +248,16 @@ pub fn fixup_fragment_sets<'tcx>(this: &MoveData<'tcx>, tcx: &ty::ctxt<'tcx>) { unmoved.sort(); unmoved.dedup(); - debug!("fragments 4 unmoved: {}", frag_lps(unmoved.as_slice())); + debug!("fragments 4 unmoved: {}", frag_lps(unmoved[])); // Fifth, filter the leftover fragments down to its core. unmoved.retain(|f| match *f { AllButOneFrom(_) => true, - Just(mpi) => non_member(mpi, parents.as_slice()) && - non_member(mpi, moved.as_slice()) && - non_member(mpi, assigned.as_slice()) + Just(mpi) => non_member(mpi, parents[]) && + non_member(mpi, moved[]) && + non_member(mpi, assigned[]) }); - debug!("fragments 5 unmoved: {}", frag_lps(unmoved.as_slice())); + debug!("fragments 5 unmoved: {}", frag_lps(unmoved[])); // Swap contents back in. fragments.unmoved_fragments = unmoved; @@ -434,7 +434,7 @@ fn add_fragment_siblings_for_extension<'tcx>(this: &MoveData<'tcx>, let msg = format!("type {} ({}) is not fragmentable", parent_ty.repr(tcx), sty_and_variant_info); let opt_span = origin_id.and_then(|id|tcx.map.opt_span(id)); - tcx.sess.opt_span_bug(opt_span, msg.as_slice()) + tcx.sess.opt_span_bug(opt_span, msg[]) } } } diff --git a/src/librustc_borrowck/borrowck/gather_loans/mod.rs b/src/librustc_borrowck/borrowck/gather_loans/mod.rs index 08d12f8282bd..d7f50ccc6ba3 100644 --- a/src/librustc_borrowck/borrowck/gather_loans/mod.rs +++ b/src/librustc_borrowck/borrowck/gather_loans/mod.rs @@ -310,7 +310,7 @@ fn guarantee_valid(&mut self, self.tcx().sess.span_bug( cmt.span, format!("invalid borrow lifetime: {}", - loan_region).as_slice()); + loan_region)[]); } }; debug!("loan_scope = {}", loan_scope); diff --git a/src/librustc_borrowck/borrowck/gather_loans/move_error.rs b/src/librustc_borrowck/borrowck/gather_loans/move_error.rs index fbe78152a609..73b345a70af4 100644 --- a/src/librustc_borrowck/borrowck/gather_loans/move_error.rs +++ b/src/librustc_borrowck/borrowck/gather_loans/move_error.rs @@ -120,7 +120,7 @@ fn report_cannot_move_out_of<'a, 'tcx>(bccx: &BorrowckCtxt<'a, 'tcx>, bccx.span_err( move_from.span, format!("cannot move out of {}", - bccx.cmt_to_string(&*move_from)).as_slice()); + bccx.cmt_to_string(&*move_from))[]); } mc::cat_downcast(ref b, _) | @@ -132,7 +132,7 @@ fn report_cannot_move_out_of<'a, 'tcx>(bccx: &BorrowckCtxt<'a, 'tcx>, move_from.span, format!("cannot move out of type `{}`, \ which defines the `Drop` trait", - b.ty.user_string(bccx.tcx)).as_slice()); + b.ty.user_string(bccx.tcx))[]); }, _ => panic!("this path should not cause illegal move") } @@ -155,10 +155,10 @@ fn note_move_destination(bccx: &BorrowckCtxt, format!("to prevent the move, \ use `ref {0}` or `ref mut {0}` to capture value by \ reference", - pat_name).as_slice()); + pat_name)[]); } else { bccx.span_note(move_to_span, format!("and here (use `ref {0}` or `ref mut {0}`)", - pat_name).as_slice()); + pat_name)[]); } } diff --git a/src/librustc_borrowck/borrowck/mod.rs b/src/librustc_borrowck/borrowck/mod.rs index 9be87b533f29..a13001b79685 100644 --- a/src/librustc_borrowck/borrowck/mod.rs +++ b/src/librustc_borrowck/borrowck/mod.rs @@ -146,7 +146,7 @@ fn borrowck_fn(this: &mut BorrowckCtxt, check_loans::check_loans(this, &loan_dfcx, flowed_moves, - all_loans.as_slice(), + all_loans[], id, decl, body); @@ -527,7 +527,7 @@ pub fn cat_expr(&self, expr: &ast::Expr) -> mc::cmt<'tcx> { pub fn report(&self, err: BckError<'tcx>) { self.span_err( err.span, - self.bckerr_to_string(&err).as_slice()); + self.bckerr_to_string(&err)[]); self.note_and_explain_bckerr(err); } @@ -549,7 +549,7 @@ pub fn report_use_of_moved_value(&self, use_span, format!("{} of possibly uninitialized variable: `{}`", verb, - self.loan_path_to_string(lp)).as_slice()); + self.loan_path_to_string(lp))[]); (self.loan_path_to_string(moved_lp), String::new()) } @@ -591,7 +591,7 @@ pub fn report_use_of_moved_value(&self, format!("{} of {}moved value: `{}`", verb, msg, - nl).as_slice()); + nl)[]); (ol, moved_lp_msg) } }; @@ -610,7 +610,7 @@ pub fn report_use_of_moved_value(&self, self.tcx.sess.bug(format!("MoveExpr({}) maps to \ {}, not Expr", the_move.id, - r).as_slice()) + r)[]) } }; let (suggestion, _) = move_suggestion(self.tcx, param_env, expr_ty, @@ -621,7 +621,7 @@ pub fn report_use_of_moved_value(&self, ol, moved_lp_msg, expr_ty.user_string(self.tcx), - suggestion).as_slice()); + suggestion)[]); } move_data::MovePat => { @@ -632,7 +632,7 @@ pub fn report_use_of_moved_value(&self, which is moved by default", ol, moved_lp_msg, - pat_ty.user_string(self.tcx)).as_slice()); + pat_ty.user_string(self.tcx))[]); self.tcx.sess.span_help(span, "use `ref` to override"); } @@ -648,7 +648,7 @@ pub fn report_use_of_moved_value(&self, self.tcx.sess.bug(format!("Captured({}) maps to \ {}, not Expr", the_move.id, - r).as_slice()) + r)[]) } }; let (suggestion, help) = move_suggestion(self.tcx, @@ -663,7 +663,7 @@ pub fn report_use_of_moved_value(&self, ol, moved_lp_msg, expr_ty.user_string(self.tcx), - suggestion).as_slice()); + suggestion)[]); self.tcx.sess.span_help(expr_span, help); } } @@ -696,7 +696,7 @@ pub fn report_reassigned_immutable_variable(&self, self.tcx.sess.span_err( span, format!("re-assignment of immutable variable `{}`", - self.loan_path_to_string(lp)).as_slice()); + self.loan_path_to_string(lp))[]); self.tcx.sess.span_note(assign.span, "prior assignment occurs here"); } @@ -822,12 +822,12 @@ pub fn report_aliasability_violation(&self, self.tcx.sess.span_err( span, format!("{} in an aliasable location", - prefix).as_slice()); + prefix)[]); } mc::AliasableClosure(id) => { self.tcx.sess.span_err(span, format!("{} in a captured outer \ - variable in an `Fn` closure", prefix).as_slice()); + variable in an `Fn` closure", prefix)[]); span_help!(self.tcx.sess, self.tcx.map.span(id), "consider changing this closure to take self by mutable reference"); } @@ -835,12 +835,12 @@ pub fn report_aliasability_violation(&self, mc::AliasableStaticMut(..) => { self.tcx.sess.span_err( span, - format!("{} in a static location", prefix).as_slice()); + format!("{} in a static location", prefix)[]); } mc::AliasableBorrowed => { self.tcx.sess.span_err( span, - format!("{} in a `&` reference", prefix).as_slice()); + format!("{} in a `&` reference", prefix)[]); } } @@ -908,12 +908,12 @@ pub fn note_and_explain_bckerr(&self, err: BckError<'tcx>) { note_and_explain_region( self.tcx, format!("{} would have to be valid for ", - descr).as_slice(), + descr)[], loan_scope, "..."); note_and_explain_region( self.tcx, - format!("...but {} is only valid for ", descr).as_slice(), + format!("...but {} is only valid for ", descr)[], ptr_scope, ""); } @@ -933,7 +933,7 @@ pub fn append_loan_path_to_string(&self, out.push('('); self.append_loan_path_to_string(&**lp_base, out); out.push_str(DOWNCAST_PRINTED_OPERATOR); - out.push_str(ty::item_path_str(self.tcx, variant_def_id).as_slice()); + out.push_str(ty::item_path_str(self.tcx, variant_def_id)[]); out.push(')'); } @@ -947,7 +947,7 @@ pub fn append_loan_path_to_string(&self, } mc::PositionalField(idx) => { out.push('.'); - out.push_str(idx.to_string().as_slice()); + out.push_str(idx.to_string()[]); } } } @@ -979,7 +979,7 @@ pub fn append_autoderefd_loan_path_to_string(&self, out.push('('); self.append_autoderefd_loan_path_to_string(&**lp_base, out); out.push(':'); - out.push_str(ty::item_path_str(self.tcx, variant_def_id).as_slice()); + out.push_str(ty::item_path_str(self.tcx, variant_def_id)[]); out.push(')'); } diff --git a/src/librustc_borrowck/graphviz.rs b/src/librustc_borrowck/graphviz.rs index 3427be1443b3..e2813c8e9882 100644 --- a/src/librustc_borrowck/graphviz.rs +++ b/src/librustc_borrowck/graphviz.rs @@ -59,7 +59,7 @@ fn dataflow_for(&self, e: EntryOrExit, n: &Node<'a>) -> String { if seen_one { sets.push_str(" "); } else { seen_one = true; } sets.push_str(variant.short_name()); sets.push_str(": "); - sets.push_str(self.dataflow_for_variant(e, n, variant).as_slice()); + sets.push_str(self.dataflow_for_variant(e, n, variant)[]); } sets } @@ -88,7 +88,7 @@ fn build_set(&self, set.push_str(", "); } let loan_str = self.borrowck_ctxt.loan_path_to_string(&*lp); - set.push_str(loan_str.as_slice()); + set.push_str(loan_str[]); saw_some = true; true }); diff --git a/src/librustc_driver/driver.rs b/src/librustc_driver/driver.rs index 60b890b03709..20bb9c2f4fd1 100644 --- a/src/librustc_driver/driver.rs +++ b/src/librustc_driver/driver.rs @@ -58,12 +58,12 @@ pub fn compile_input(sess: Session, let outputs = build_output_filenames(input, outdir, output, - krate.attrs.as_slice(), + krate.attrs[], &sess); - let id = link::find_crate_name(Some(&sess), krate.attrs.as_slice(), + let id = link::find_crate_name(Some(&sess), krate.attrs[], input); let expanded_crate - = match phase_2_configure_and_expand(&sess, krate, id.as_slice(), + = match phase_2_configure_and_expand(&sess, krate, id[], addl_plugins) { None => return, Some(k) => k @@ -75,7 +75,7 @@ pub fn compile_input(sess: Session, let mut forest = ast_map::Forest::new(expanded_crate); let ast_map = assign_node_ids_and_map(&sess, &mut forest); - write_out_deps(&sess, input, &outputs, id.as_slice()); + write_out_deps(&sess, input, &outputs, id[]); if stop_after_phase_2(&sess) { return; } @@ -163,9 +163,9 @@ pub fn phase_2_configure_and_expand(sess: &Session, let time_passes = sess.time_passes(); *sess.crate_types.borrow_mut() = - collect_crate_types(sess, krate.attrs.as_slice()); + collect_crate_types(sess, krate.attrs[]); *sess.crate_metadata.borrow_mut() = - collect_crate_metadata(sess, krate.attrs.as_slice()); + collect_crate_metadata(sess, krate.attrs[]); time(time_passes, "gated feature checking", (), |_| { let (features, unknown_features) = @@ -257,8 +257,8 @@ pub fn phase_2_configure_and_expand(sess: &Session, if cfg!(windows) { _old_path = os::getenv("PATH").unwrap_or(_old_path); let mut new_path = sess.host_filesearch().get_dylib_search_paths(); - new_path.extend(os::split_paths(_old_path.as_slice()).into_iter()); - os::setenv("PATH", os::join_paths(new_path.as_slice()).unwrap()); + new_path.extend(os::split_paths(_old_path[]).into_iter()); + os::setenv("PATH", os::join_paths(new_path[]).unwrap()); } let cfg = syntax::ext::expand::ExpansionConfig { crate_name: crate_name.to_string(), @@ -503,7 +503,7 @@ pub fn phase_5_run_llvm_passes(sess: &Session, time(sess.time_passes(), "LLVM passes", (), |_| write::run_passes(sess, trans, - sess.opts.output_types.as_slice(), + sess.opts.output_types[], outputs)); } @@ -517,14 +517,14 @@ pub fn phase_6_link_output(sess: &Session, outputs: &OutputFilenames) { let old_path = os::getenv("PATH").unwrap_or_else(||String::new()); let mut new_path = sess.host_filesearch().get_tools_search_paths(); - new_path.extend(os::split_paths(old_path.as_slice()).into_iter()); - os::setenv("PATH", os::join_paths(new_path.as_slice()).unwrap()); + new_path.extend(os::split_paths(old_path[]).into_iter()); + os::setenv("PATH", os::join_paths(new_path[]).unwrap()); time(sess.time_passes(), "linking", (), |_| link::link_binary(sess, trans, outputs, - trans.link.crate_name.as_slice())); + trans.link.crate_name[])); os::setenv("PATH", old_path); } @@ -613,7 +613,7 @@ fn write_out_deps(sess: &Session, // write Makefile-compatible dependency rules let files: Vec = sess.codemap().files.borrow() .iter().filter(|fmap| fmap.is_real_file()) - .map(|fmap| escape_dep_filename(fmap.name.as_slice())) + .map(|fmap| escape_dep_filename(fmap.name[])) .collect(); let mut file = try!(io::File::create(&deps_filename)); for path in out_filenames.iter() { @@ -627,7 +627,7 @@ fn write_out_deps(sess: &Session, Ok(()) => {} Err(e) => { sess.fatal(format!("error writing dependencies to `{}`: {}", - deps_filename.display(), e).as_slice()); + deps_filename.display(), e)[]); } } } @@ -698,7 +698,7 @@ pub fn collect_crate_types(session: &Session, if !res { session.warn(format!("dropping unsupported crate type `{}` \ for target `{}`", - *crate_type, session.opts.target_triple).as_slice()); + *crate_type, session.opts.target_triple)[]); } res diff --git a/src/librustc_driver/lib.rs b/src/librustc_driver/lib.rs index 6944c733456f..1fb90d7860e4 100644 --- a/src/librustc_driver/lib.rs +++ b/src/librustc_driver/lib.rs @@ -55,6 +55,7 @@ use std::any::AnyRefExt; use std::io; +use std::iter::repeat; use std::os; use std::thread; @@ -88,12 +89,12 @@ fn run_compiler(args: &[String]) { let descriptions = diagnostics::registry::Registry::new(&DIAGNOSTICS); match matches.opt_str("explain") { Some(ref code) => { - match descriptions.find_description(code.as_slice()) { + match descriptions.find_description(code[]) { Some(ref description) => { println!("{}", description); } None => { - early_error(format!("no extended information for {}", code).as_slice()); + early_error(format!("no extended information for {}", code)[]); } } return; @@ -119,7 +120,7 @@ fn run_compiler(args: &[String]) { early_error("no input filename given"); } 1u => { - let ifile = matches.free[0].as_slice(); + let ifile = matches.free[0][]; if ifile == "-" { let contents = io::stdin().read_to_end().unwrap(); let src = String::from_utf8(contents).unwrap(); @@ -138,7 +139,7 @@ fn run_compiler(args: &[String]) { } let pretty = matches.opt_default("pretty", "normal").map(|a| { - pretty::parse_pretty(&sess, a.as_slice()) + pretty::parse_pretty(&sess, a[]) }); match pretty.into_iter().next() { Some((ppm, opt_uii)) => { @@ -261,7 +262,8 @@ fn sort_lint_groups(lints: Vec<(&'static str, Vec, bool)>) .map(|&s| s.name.width(true)) .max().unwrap_or(0); let padded = |x: &str| { - let mut s = " ".repeat(max_name_len - x.char_len()); + let mut s = repeat(" ").take(max_name_len - x.chars().count()) + .collect::(); s.push_str(x); s }; @@ -274,7 +276,7 @@ fn sort_lint_groups(lints: Vec<(&'static str, Vec, bool)>) for lint in lints.into_iter() { let name = lint.name_lower().replace("_", "-"); println!(" {} {:7.7} {}", - padded(name.as_slice()), lint.default_level.as_str(), lint.desc); + padded(name[]), lint.default_level.as_str(), lint.desc); } println!("\n"); }; @@ -287,7 +289,8 @@ fn sort_lint_groups(lints: Vec<(&'static str, Vec, bool)>) .map(|&(s, _)| s.width(true)) .max().unwrap_or(0); let padded = |x: &str| { - let mut s = " ".repeat(max_name_len - x.char_len()); + let mut s = repeat(" ").take(max_name_len - x.chars().count()) + .collect::(); s.push_str(x); s }; @@ -303,7 +306,7 @@ fn sort_lint_groups(lints: Vec<(&'static str, Vec, bool)>) let desc = to.into_iter().map(|x| x.as_str().replace("_", "-")) .collect::>().connect(", "); println!(" {} {}", - padded(name.as_slice()), desc); + padded(name[]), desc); } println!("\n"); }; @@ -367,10 +370,10 @@ pub fn handle_options(mut args: Vec) -> Option { } let matches = - match getopts::getopts(args.as_slice(), config::optgroups().as_slice()) { + match getopts::getopts(args[], config::optgroups()[]) { Ok(m) => m, Err(f) => { - early_error(f.to_string().as_slice()); + early_error(f.to_string()[]); } }; @@ -518,7 +521,7 @@ pub fn monitor(f: F) { "run with `RUST_BACKTRACE=1` for a backtrace".to_string(), ]; for note in xs.iter() { - emitter.emit(None, note.as_slice(), None, diagnostic::Note) + emitter.emit(None, note[], None, diagnostic::Note) } match r.read_to_string() { @@ -526,8 +529,7 @@ pub fn monitor(f: F) { Err(e) => { emitter.emit(None, format!("failed to read internal \ - stderr: {}", - e).as_slice(), + stderr: {}", e)[], None, diagnostic::Error) } diff --git a/src/librustc_driver/pretty.rs b/src/librustc_driver/pretty.rs index 2eb9d2c67a7c..4b10ca92e705 100644 --- a/src/librustc_driver/pretty.rs +++ b/src/librustc_driver/pretty.rs @@ -71,10 +71,10 @@ pub fn parse_pretty(sess: &Session, name: &str) -> (PpMode, Option`, `typed`, `identified`, \ - or `expanded,identified`; got {}", name).as_slice()); + or `expanded,identified`; got {}", name)[]); } }; - let opt_second = opt_second.and_then::(from_str); + let opt_second = opt_second.and_then(|s| s.parse::()); (first, opt_second) } @@ -276,7 +276,7 @@ fn post(&self, try!(pp::word(&mut s.s, ppaux::ty_to_string( tcx, - ty::expr_ty(tcx, expr)).as_slice())); + ty::expr_ty(tcx, expr))[])); s.pclose() } _ => Ok(()) @@ -311,7 +311,7 @@ pub enum UserIdentifiedItem { impl FromStr for UserIdentifiedItem { fn from_str(s: &str) -> Option { - from_str(s).map(ItemViaNode).or_else(|| { + s.parse().map(ItemViaNode).or_else(|| { let v : Vec<_> = s.split_str("::") .map(|x|x.to_string()) .collect(); @@ -322,7 +322,7 @@ fn from_str(s: &str) -> Option { enum NodesMatchingUII<'a, 'ast: 'a> { NodesMatchingDirect(option::IntoIter), - NodesMatchingSuffix(ast_map::NodesMatchingSuffix<'a, 'ast, String>), + NodesMatchingSuffix(ast_map::NodesMatchingSuffix<'a, 'ast>), } impl<'a, 'ast> Iterator for NodesMatchingUII<'a, 'ast> { @@ -348,7 +348,7 @@ fn all_matching_node_ids<'a, 'ast>(&'a self, map: &'a ast_map::Map<'ast>) ItemViaNode(node_id) => NodesMatchingDirect(Some(node_id).into_iter()), ItemViaPath(ref parts) => - NodesMatchingSuffix(map.nodes_matching_suffix(parts.as_slice())), + NodesMatchingSuffix(map.nodes_matching_suffix(parts[])), } } @@ -360,7 +360,7 @@ fn to_one_node_id(self, user_option: &str, sess: &Session, map: &ast_map::Map) - user_option, self.reconstructed_input(), is_wrong_because); - sess.fatal(message.as_slice()) + sess.fatal(message[]) }; let mut saw_node = ast::DUMMY_NODE_ID; @@ -414,12 +414,12 @@ pub fn pretty_print_input(sess: Session, opt_uii: Option, ofile: Option) { let krate = driver::phase_1_parse_input(&sess, cfg, input); - let id = link::find_crate_name(Some(&sess), krate.attrs.as_slice(), input); + let id = link::find_crate_name(Some(&sess), krate.attrs[], input); let is_expanded = needs_expansion(&ppm); let compute_ast_map = needs_ast_map(&ppm, &opt_uii); let krate = if compute_ast_map { - match driver::phase_2_configure_and_expand(&sess, krate, id.as_slice(), None) { + match driver::phase_2_configure_and_expand(&sess, krate, id[], None) { None => return, Some(k) => k } @@ -438,7 +438,7 @@ pub fn pretty_print_input(sess: Session, }; let src_name = driver::source_name(input); - let src = sess.codemap().get_filemap(src_name.as_slice()) + let src = sess.codemap().get_filemap(src_name[]) .src.as_bytes().to_vec(); let mut rdr = MemReader::new(src); @@ -499,7 +499,7 @@ pub fn pretty_print_input(sess: Session, debug!("pretty printing flow graph for {}", opt_uii); let uii = opt_uii.unwrap_or_else(|| { sess.fatal(format!("`pretty flowgraph=..` needs NodeId (int) or - unique path suffix (b::c::d)").as_slice()) + unique path suffix (b::c::d)")[]) }); let ast_map = ast_map.expect("--pretty flowgraph missing ast_map"); @@ -507,7 +507,7 @@ pub fn pretty_print_input(sess: Session, let node = ast_map.find(nodeid).unwrap_or_else(|| { sess.fatal(format!("--pretty flowgraph couldn't find id: {}", - nodeid).as_slice()) + nodeid)[]) }); let code = blocks::Code::from_node(node); @@ -526,8 +526,8 @@ pub fn pretty_print_input(sess: Session, // point to what was found, if there's an // accessible span. match ast_map.opt_span(nodeid) { - Some(sp) => sess.span_fatal(sp, message.as_slice()), - None => sess.fatal(message.as_slice()) + Some(sp) => sess.span_fatal(sp, message[]), + None => sess.fatal(message[]) } } } @@ -587,7 +587,7 @@ fn expand_err_details(r: io::IoResult<()>) -> io::IoResult<()> { let m = "graphviz::render failed"; io::IoError { detail: Some(match orig_detail { - None => m.into_string(), + None => m.to_string(), Some(d) => format!("{}: {}", m, d) }), ..ioerr diff --git a/src/librustc_resolve/lib.rs b/src/librustc_resolve/lib.rs index d4a0b49436dd..bf9e9294307e 100644 --- a/src/librustc_resolve/lib.rs +++ b/src/librustc_resolve/lib.rs @@ -97,8 +97,8 @@ use std::rc::{Rc, Weak}; use std::uint; -// Definition mapping -pub type DefMap = RefCell>; +mod check_unused; +mod record_exports; #[deriving(Copy)] struct BindingInfo { @@ -1119,14 +1119,14 @@ fn add_child(&self, self.resolve_error(sp, format!("duplicate definition of {} `{}`", namespace_error_to_string(duplicate_type), - token::get_name(name)).as_slice()); + token::get_name(name))[]); { let r = child.span_for_namespace(ns); for sp in r.iter() { self.session.span_note(*sp, format!("first definition of {} `{}` here", namespace_error_to_string(duplicate_type), - token::get_name(name)).as_slice()); + token::get_name(name))[]); } } } @@ -2147,7 +2147,7 @@ fn build_import_directive(&mut self, debug!("(building import directive) building import \ directive: {}::{}", self.names_to_string(module_.imports.borrow().last().unwrap() - .module_path.as_slice()), + .module_path[]), token::get_name(target)); let mut import_resolutions = module_.import_resolutions @@ -2265,10 +2265,10 @@ fn resolve_imports_for_module(&mut self, module: Rc) { let msg = format!("unresolved import `{}`{}", self.import_path_to_string( import_directive.module_path - .as_slice(), + [], import_directive.subclass), help); - self.resolve_error(span, msg.as_slice()); + self.resolve_error(span, msg[]); } Indeterminate => break, // Bail out. We'll come around next time. Success(()) => () // Good. Continue. @@ -2298,7 +2298,7 @@ fn path_names_to_string(&self, path: &Path) -> String { .iter() .map(|seg| seg.identifier.name) .collect(); - self.names_to_string(names.as_slice()) + self.names_to_string(names[]) } fn import_directive_subclass_to_string(&mut self, @@ -2340,7 +2340,7 @@ fn resolve_import_for_module(&mut self, debug!("(resolving import for module) resolving import `{}::...` in \ `{}`", - self.names_to_string(module_path.as_slice()), + self.names_to_string(module_path[]), self.module_to_string(&*module_)); // First, resolve the module path for the directive, if necessary. @@ -2349,7 +2349,7 @@ fn resolve_import_for_module(&mut self, Some((self.graph_root.get_module(), LastMod(AllPublic))) } else { match self.resolve_module_path(module_.clone(), - module_path.as_slice(), + module_path[], DontUseLexicalScope, import_directive.span, ImportSearch) { @@ -2941,7 +2941,7 @@ fn check_for_conflicting_import(&mut self, ValueNS => "value", }, token::get_name(name).get()); - self.session.span_err(import_span, msg.as_slice()); + self.session.span_err(import_span, msg[]); } Some(_) | None => {} } @@ -2956,7 +2956,7 @@ fn check_that_import_is_importable(&mut self, if !name_bindings.defined_in_namespace_with(namespace, IMPORTABLE) { let msg = format!("`{}` is not directly importable", token::get_name(name)); - self.session.span_err(import_span, msg.as_slice()); + self.session.span_err(import_span, msg[]); } } @@ -2981,7 +2981,7 @@ fn check_for_conflicts_between_imports_and_items(&mut self, crate in this module \ (maybe you meant `use {0}::*`?)", token::get_name(name).get()); - self.session.span_err(import_span, msg.as_slice()); + self.session.span_err(import_span, msg[]); } Some(_) | None => {} } @@ -3003,7 +3003,7 @@ fn check_for_conflicts_between_imports_and_items(&mut self, let msg = format!("import `{}` conflicts with value \ in this module", token::get_name(name).get()); - self.session.span_err(import_span, msg.as_slice()); + self.session.span_err(import_span, msg[]); if let Some(span) = value.value_span { self.session.span_note(span, "conflicting value here"); @@ -3021,7 +3021,7 @@ fn check_for_conflicts_between_imports_and_items(&mut self, let msg = format!("import `{}` conflicts with type in \ this module", token::get_name(name).get()); - self.session.span_err(import_span, msg.as_slice()); + self.session.span_err(import_span, msg[]); if let Some(span) = ty.type_span { self.session.span_note(span, "note conflicting type here") @@ -3034,7 +3034,7 @@ fn check_for_conflicts_between_imports_and_items(&mut self, let msg = format!("inherent implementations \ are only allowed on types \ defined in the current module"); - self.session.span_err(span, msg.as_slice()); + self.session.span_err(span, msg[]); self.session.span_note(import_span, "import from other module here") } @@ -3043,7 +3043,7 @@ fn check_for_conflicts_between_imports_and_items(&mut self, let msg = format!("import `{}` conflicts with existing \ submodule", token::get_name(name).get()); - self.session.span_err(import_span, msg.as_slice()); + self.session.span_err(import_span, msg[]); if let Some(span) = ty.type_span { self.session.span_note(span, "note conflicting module here") @@ -3073,7 +3073,7 @@ fn check_for_conflicts_between_external_crates(&self, .span_err(span, format!("an external crate named `{}` has already \ been imported into this module", - token::get_name(name).get()).as_slice()); + token::get_name(name).get())[]); } } @@ -3092,7 +3092,7 @@ fn check_for_conflicts_between_external_crates_and_items(&self, format!("the name `{}` conflicts with an external \ crate that has been imported into this \ module", - token::get_name(name).get()).as_slice()); + token::get_name(name).get())[]); } } @@ -3140,7 +3140,7 @@ fn search_parent_externals(needle: Name, module: &Rc) let segment_name = token::get_name(name); let module_name = self.module_to_string(&*search_module); let mut span = span; - let msg = if "???" == module_name.as_slice() { + let msg = if "???" == module_name[] { span.hi = span.lo + Pos::from_uint(segment_name.get().len()); match search_parent_externals(name, @@ -3253,14 +3253,14 @@ fn resolve_module_path(&mut self, match module_prefix_result { Failed(None) => { let mpath = self.names_to_string(module_path); - let mpath = mpath.as_slice(); + let mpath = mpath[]; match mpath.rfind(':') { Some(idx) => { let msg = format!("Could not find `{}` in `{}`", // idx +- 1 to account for the // colons on either side - mpath.slice_from(idx + 1), - mpath.slice_to(idx - 1)); + mpath[idx + 1..], + mpath[0..idx - 1]); return Failed(Some((span, msg))); }, None => { @@ -3431,7 +3431,7 @@ fn resolve_item_in_lexical_scope(&mut self, true) { Failed(Some((span, msg))) => self.resolve_error(span, format!("failed to resolve. {}", - msg)), + msg)[]), Failed(None) => (), // Continue up the search chain. Indeterminate => { // We couldn't see through the higher scope because of an @@ -3686,8 +3686,8 @@ fn report_unresolved_imports(&mut self, module_: Rc) { "unresolved import"); } else { let err = format!("unresolved import (maybe you meant `{}::*`?)", - sn.slice(0, sn.len())); - self.resolve_error((*imports)[index].span, err.as_slice()); + sn); + self.resolve_error((*imports)[index].span, err[]); } } @@ -3779,7 +3779,7 @@ fn upvarify(&self, match def_like { DlDef(d @ DefUpvar(..)) => { self.session.span_bug(span, - format!("unexpected {} in bindings", d).as_slice()) + format!("unexpected {} in bindings", d)[]) } DlDef(d @ DefLocal(_)) => { let node_id = d.def_id().node; @@ -3995,7 +3995,7 @@ fn resolve_item(&mut self, item: &Item) { generics, implemented_traits, &**self_type, - impl_items.as_slice()); + impl_items[]); } ItemTrait(_, ref generics, ref unbound, ref bounds, ref trait_items) => { @@ -4080,7 +4080,7 @@ fn resolve_item(&mut self, item: &Item) { ItemStruct(ref struct_def, ref generics) => { self.resolve_struct(item.id, generics, - struct_def.fields.as_slice()); + struct_def.fields[]); } ItemMod(ref module_) => { @@ -4153,7 +4153,7 @@ fn with_type_parameter_rib(&mut self, type_parameters: TypeParameters, f: F) parameter in this type \ parameter list", token::get_name( - name)).as_slice()) + name))[]) } seen_bindings.insert(name); @@ -4330,7 +4330,7 @@ fn resolve_trait_reference(&mut self, }; let msg = format!("attempt to {} a nonexistent trait `{}`", usage_str, path_str); - self.resolve_error(trait_reference.path.span, msg.as_slice()); + self.resolve_error(trait_reference.path.span, msg[]); } Some(def) => { match def { @@ -4342,14 +4342,14 @@ fn resolve_trait_reference(&mut self, self.resolve_error(trait_reference.path.span, format!("`{}` is not a trait", self.path_names_to_string( - &trait_reference.path))); + &trait_reference.path))[]); // If it's a typedef, give a note if let DefTy(..) = def { self.session.span_note( trait_reference.path.span, format!("`type` aliases cannot be used for traits") - .as_slice()); + []); } } } @@ -4546,7 +4546,7 @@ fn check_trait_item(&self, name: Name, span: Span) { self.resolve_error(span, format!("method `{}` is not a member of trait `{}`", token::get_name(name), - path_str).as_slice()); + path_str)[]); } } } @@ -4613,7 +4613,7 @@ fn check_consistent_bindings(&mut self, arm: &Arm) { format!("variable `{}` from pattern #1 is \ not bound in pattern #{}", token::get_name(key), - i + 1).as_slice()); + i + 1)[]); } Some(binding_i) => { if binding_0.binding_mode != binding_i.binding_mode { @@ -4622,7 +4622,7 @@ fn check_consistent_bindings(&mut self, arm: &Arm) { format!("variable `{}` is bound with different \ mode in pattern #{} than in pattern #1", token::get_name(key), - i + 1).as_slice()); + i + 1)[]); } } } @@ -4635,7 +4635,7 @@ fn check_consistent_bindings(&mut self, arm: &Arm) { format!("variable `{}` from pattern {}{} is \ not bound in pattern {}1", token::get_name(key), - "#", i + 1, "#").as_slice()); + "#", i + 1, "#")[]); } } } @@ -4752,7 +4752,7 @@ fn resolve_type(&mut self, ty: &Ty) { None => { let msg = format!("use of undeclared type name `{}`", self.path_names_to_string(path)); - self.resolve_error(ty.span, msg.as_slice()); + self.resolve_error(ty.span, msg[]); } } } @@ -4832,7 +4832,7 @@ struct or enum variant", format!("declaration of `{}` shadows an enum \ variant or unit-like struct in \ scope", - token::get_name(renamed)).as_slice()); + token::get_name(renamed))[]); } FoundConst(ref def, lp) if mode == RefutableMode => { debug!("(resolving pattern) resolving `{}` to \ @@ -4884,7 +4884,7 @@ struct or enum variant", list", token::get_ident( ident)) - .as_slice()) + []) } else if bindings_list.get(&renamed) == Some(&pat_id) { // Then this is a duplicate variable in the @@ -4893,7 +4893,7 @@ struct or enum variant", format!("identifier `{}` is bound \ more than once in the same \ pattern", - token::get_ident(ident)).as_slice()); + token::get_ident(ident))[]); } // Else, not bound in the same pattern: do // nothing. @@ -4922,7 +4922,7 @@ struct or enum variant", path.segments .last() .unwrap() - .identifier)).as_slice()); + .identifier))[]); } None => { self.resolve_error(path.span, @@ -4931,7 +4931,7 @@ struct or enum variant", path.segments .last() .unwrap() - .identifier)).as_slice()); + .identifier))[]); } } @@ -4962,7 +4962,7 @@ struct or enum variant", def: {}", result); let msg = format!("`{}` does not name a structure", self.path_names_to_string(path)); - self.resolve_error(path.span, msg.as_slice()); + self.resolve_error(path.span, msg[]); } } } @@ -5024,7 +5024,7 @@ fn resolve_bare_identifier_pattern(&mut self, name: Name, span: Span) match err { Some((span, msg)) => { self.resolve_error(span, format!("failed to resolve: {}", - msg)); + msg)[]); } None => () } @@ -5220,7 +5220,7 @@ fn resolve_module_relative_path(&mut self, let last_private; let module = self.current_module.clone(); match self.resolve_module_path(module, - module_path.as_slice(), + module_path[], UseLexicalScope, path.span, PathSearch) { @@ -5235,7 +5235,7 @@ fn resolve_module_relative_path(&mut self, }; self.resolve_error(span, format!("failed to resolve. {}", - msg.as_slice())); + msg)[]); return None; } Indeterminate => panic!("indeterminate unexpected"), @@ -5278,7 +5278,7 @@ fn resolve_crate_relative_path(&mut self, let containing_module; let last_private; match self.resolve_module_path_from_root(root_module, - module_path.as_slice(), + module_path[], 0, path.span, PathSearch, @@ -5288,13 +5288,13 @@ fn resolve_crate_relative_path(&mut self, Some((span, msg)) => (span, msg), None => { let msg = format!("Use of undeclared module `::{}`", - self.names_to_string(module_path.as_slice())); + self.names_to_string(module_path[])); (path.span, msg) } }; self.resolve_error(span, format!("failed to resolve. {}", - msg.as_slice())); + msg)[]); return None; } @@ -5335,7 +5335,7 @@ fn resolve_identifier_in_local_ribs(&mut self, } TypeNS => { let name = ident.name; - self.search_ribs(self.type_ribs.as_slice(), name, span) + self.search_ribs(self.type_ribs[], name, span) } }; @@ -5389,7 +5389,8 @@ fn resolve_item_by_name_in_lexical_scope(&mut self, Failed(err) => { match err { Some((span, msg)) => - self.resolve_error(span, format!("failed to resolve. {}", msg)), + self.resolve_error(span, format!("failed to resolve. {}", + msg)[]), None => () } @@ -5409,9 +5410,9 @@ fn with_no_errors(&mut self, f: F) -> T where rs } - fn resolve_error(&self, span: Span, s: T) { + fn resolve_error(&self, span: Span, s: &str) { if self.emit_errors { - self.session.span_err(span, s.as_slice()); + self.session.span_err(span, s); } } @@ -5446,7 +5447,7 @@ fn get_module(this: &mut Resolver, span: Span, name_path: &[ast::Name]) } } else { match this.resolve_module_path(root, - name_path.as_slice(), + name_path[], UseLexicalScope, span, PathSearch) { @@ -5484,7 +5485,7 @@ fn get_module(this: &mut Resolver, span: Span, name_path: &[ast::Name]) let name_path = path.segments.iter().map(|seg| seg.identifier.name).collect::>(); // Look for a method in the current self type's impl module. - match get_module(self, path.span, name_path.as_slice()) { + match get_module(self, path.span, name_path[]) { Some(module) => match module.children.borrow().get(&name) { Some(binding) => { let p_str = self.path_names_to_string(&path); @@ -5695,7 +5696,7 @@ fn resolve_expr(&mut self, expr: &Expr) { def: {}", result); let msg = format!("`{}` does not name a structure", self.path_names_to_string(path)); - self.resolve_error(path.span, msg.as_slice()); + self.resolve_error(path.span, msg[]); } } @@ -5751,13 +5752,13 @@ fn resolve_expr(&mut self, expr: &Expr) { ExprBreak(Some(label)) | ExprAgain(Some(label)) => { let renamed = mtwt::resolve(label); - match self.search_ribs(self.label_ribs.as_slice(), + match self.search_ribs(self.label_ribs[], renamed, expr.span) { None => { self.resolve_error( expr.span, format!("use of undeclared label `{}`", - token::get_ident(label)).as_slice()) + token::get_ident(label))[]) } Some(DlDef(def @ DefLabel(_))) => { // Since this def is a label, it is never read. @@ -5893,7 +5894,7 @@ fn record_def(&mut self, node_id: NodeId, (def, lp): (Def, LastPrivate)) { then {}", node_id, *entry.get(), - def).as_slice()); + def)[]); }, Vacant(entry) => { entry.set(def); }, } @@ -5909,7 +5910,7 @@ fn enforce_default_binding_mode(&mut self, self.resolve_error(pat.span, format!("cannot use `ref` binding mode \ with {}", - descr).as_slice()); + descr)[]); } } } @@ -5945,8 +5946,7 @@ fn collect_mod(names: &mut Vec, module: &Module) { return "???".to_string(); } self.names_to_string(names.into_iter().rev() - .collect::>() - .as_slice()) + .collect::>()[]) } #[allow(dead_code)] // useful for debugging diff --git a/src/librustc_trans/back/link.rs b/src/librustc_trans/back/link.rs index 5617110bfecf..ec61d3a69537 100644 --- a/src/librustc_trans/back/link.rs +++ b/src/librustc_trans/back/link.rs @@ -126,7 +126,7 @@ pub fn find_crate_name(sess: Option<&Session>, attrs: &[ast::Attribute], input: &Input) -> String { let validate = |s: String, span: Option| { - creader::validate_crate_name(sess, s.as_slice(), span); + creader::validate_crate_name(sess, s[], span); s }; @@ -144,7 +144,7 @@ pub fn find_crate_name(sess: Option<&Session>, let msg = format!("--crate-name and #[crate_name] are \ required to match, but `{}` != `{}`", s, name); - sess.span_err(attr.span, msg.as_slice()); + sess.span_err(attr.span, msg[]); } } return validate(s.clone(), None); @@ -190,17 +190,17 @@ fn symbol_hash<'tcx>(tcx: &ty::ctxt<'tcx>, // to be independent of one another in the crate. symbol_hasher.reset(); - symbol_hasher.input_str(link_meta.crate_name.as_slice()); + symbol_hasher.input_str(link_meta.crate_name[]); symbol_hasher.input_str("-"); symbol_hasher.input_str(link_meta.crate_hash.as_str()); for meta in tcx.sess.crate_metadata.borrow().iter() { - symbol_hasher.input_str(meta.as_slice()); + symbol_hasher.input_str(meta[]); } symbol_hasher.input_str("-"); - symbol_hasher.input_str(encoder::encoded_ty(tcx, t).as_slice()); + symbol_hasher.input_str(encoder::encoded_ty(tcx, t)[]); // Prefix with 'h' so that it never blends into adjacent digits let mut hash = String::from_str("h"); - hash.push_str(truncated_hash_result(symbol_hasher).as_slice()); + hash.push_str(truncated_hash_result(symbol_hasher)[]); hash } @@ -249,7 +249,7 @@ pub fn sanitize(s: &str) -> String { let mut tstr = String::new(); for c in c.escape_unicode() { tstr.push(c) } result.push('$'); - result.push_str(tstr.slice_from(1)); + result.push_str(tstr[1..]); } } } @@ -258,7 +258,7 @@ pub fn sanitize(s: &str) -> String { if result.len() > 0u && result.as_bytes()[0] != '_' as u8 && ! (result.as_bytes()[0] as char).is_xid_start() { - return format!("_{}", result.as_slice()); + return format!("_{}", result[]); } return result; @@ -284,12 +284,12 @@ pub fn mangle>(mut path: PI, fn push(n: &mut String, s: &str) { let sani = sanitize(s); - n.push_str(format!("{}{}", sani.len(), sani).as_slice()); + n.push_str(format!("{}{}", sani.len(), sani)[]); } // First, connect each component with pairs. for e in path { - push(&mut n, token::get_name(e.name()).get().as_slice()) + push(&mut n, token::get_name(e.name()).get()[]) } match hash { @@ -327,17 +327,17 @@ pub fn mangle_exported_name<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, path: PathEl hash.push(EXTRA_CHARS.as_bytes()[extra2] as char); hash.push(EXTRA_CHARS.as_bytes()[extra3] as char); - exported_name(path, hash.as_slice()) + exported_name(path, hash[]) } pub fn mangle_internal_name_by_type_and_seq<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, t: Ty<'tcx>, name: &str) -> String { let s = ppaux::ty_to_string(ccx.tcx(), t); - let path = [PathName(token::intern(s.as_slice())), + let path = [PathName(token::intern(s[])), gensym_name(name)]; let hash = get_symbol_hash(ccx, t); - mangle(ast_map::Values(path.iter()), Some(hash.as_slice())) + mangle(ast_map::Values(path.iter()), Some(hash[])) } pub fn mangle_internal_name_by_path_and_seq(path: PathElems, flav: &str) -> String { @@ -357,7 +357,7 @@ pub fn remove(sess: &Session, path: &Path) { Err(e) => { sess.err(format!("failed to remove {}: {}", path.display(), - e).as_slice()); + e)[]); } } } @@ -372,7 +372,7 @@ pub fn link_binary(sess: &Session, for &crate_type in sess.crate_types.borrow().iter() { if invalid_output_for_target(sess, crate_type) { sess.bug(format!("invalid output type `{}` for target os `{}`", - crate_type, sess.opts.target_triple).as_slice()); + crate_type, sess.opts.target_triple)[]); } let out_file = link_binary_output(sess, trans, crate_type, outputs, crate_name); @@ -437,8 +437,8 @@ pub fn filename_for_input(sess: &Session, out_filename.with_filename(format!("lib{}.rlib", libname)) } config::CrateTypeDylib => { - let (prefix, suffix) = (sess.target.target.options.dll_prefix.as_slice(), - sess.target.target.options.dll_suffix.as_slice()); + let (prefix, suffix) = (sess.target.target.options.dll_prefix[], + sess.target.target.options.dll_suffix[]); out_filename.with_filename(format!("{}{}{}", prefix, libname, @@ -448,7 +448,7 @@ pub fn filename_for_input(sess: &Session, out_filename.with_filename(format!("lib{}.a", libname)) } config::CrateTypeExecutable => { - let suffix = sess.target.target.options.exe_suffix.as_slice(); + let suffix = sess.target.target.options.exe_suffix[]; out_filename.with_filename(format!("{}{}", libname, suffix)) } } @@ -477,12 +477,12 @@ fn link_binary_output(sess: &Session, if !out_is_writeable { sess.fatal(format!("output file {} is not writeable -- check its \ permissions.", - out_filename.display()).as_slice()); + out_filename.display())[]); } else if !obj_is_writeable { sess.fatal(format!("object file {} is not writeable -- check its \ permissions.", - obj_filename.display()).as_slice()); + obj_filename.display())[]); } match crate_type { @@ -507,7 +507,7 @@ fn archive_search_paths(sess: &Session) -> Vec { let mut rustpath = filesearch::rust_path(); rustpath.push(sess.target_filesearch().get_lib_path()); let mut search: Vec = sess.opts.addl_lib_search_paths.borrow().clone(); - search.push_all(rustpath.as_slice()); + search.push_all(rustpath[]); return search; } @@ -536,7 +536,7 @@ fn link_rlib<'a>(sess: &'a Session, for &(ref l, kind) in sess.cstore.get_used_libraries().borrow().iter() { match kind { cstore::NativeStatic => { - ab.add_native_library(l.as_slice()).unwrap(); + ab.add_native_library(l[]).unwrap(); } cstore::NativeFramework | cstore::NativeUnknown => {} } @@ -584,12 +584,12 @@ fn link_rlib<'a>(sess: &'a Session, let tmpdir = TempDir::new("rustc").ok().expect("needs a temp dir"); let metadata = tmpdir.path().join(METADATA_FILENAME); match fs::File::create(&metadata).write(trans.metadata - .as_slice()) { + []) { Ok(..) => {} Err(e) => { sess.err(format!("failed to write {}: {}", metadata.display(), - e).as_slice()); + e)[]); sess.abort_if_errors(); } } @@ -605,27 +605,27 @@ fn link_rlib<'a>(sess: &'a Session, // extension to it. This is to work around a bug in LLDB that // would cause it to crash if the name of a file in an archive // was exactly 16 bytes. - let bc_filename = obj_filename.with_extension(format!("{}.bc", i).as_slice()); + let bc_filename = obj_filename.with_extension(format!("{}.bc", i)[]); let bc_deflated_filename = obj_filename.with_extension( - format!("{}.bytecode.deflate", i).as_slice()); + format!("{}.bytecode.deflate", i)[]); let bc_data = match fs::File::open(&bc_filename).read_to_end() { Ok(buffer) => buffer, Err(e) => sess.fatal(format!("failed to read bytecode: {}", - e).as_slice()) + e)[]) }; - let bc_data_deflated = match flate::deflate_bytes(bc_data.as_slice()) { + let bc_data_deflated = match flate::deflate_bytes(bc_data[]) { Some(compressed) => compressed, None => sess.fatal(format!("failed to compress bytecode from {}", - bc_filename.display()).as_slice()) + bc_filename.display())[]) }; let mut bc_file_deflated = match fs::File::create(&bc_deflated_filename) { Ok(file) => file, Err(e) => { sess.fatal(format!("failed to create compressed bytecode \ - file: {}", e).as_slice()) + file: {}", e)[]) } }; @@ -634,7 +634,7 @@ fn link_rlib<'a>(sess: &'a Session, Ok(()) => {} Err(e) => { sess.err(format!("failed to write compressed bytecode: \ - {}", e).as_slice()); + {}", e)[]); sess.abort_if_errors() } }; @@ -674,7 +674,7 @@ fn write_rlib_bytecode_object_v1(writer: &mut T, try! { writer.write(RLIB_BYTECODE_OBJECT_MAGIC) }; try! { writer.write_le_u32(1) }; try! { writer.write_le_u64(bc_data_deflated_size) }; - try! { writer.write(bc_data_deflated.as_slice()) }; + try! { writer.write(bc_data_deflated[]) }; let number_of_bytes_written_so_far = RLIB_BYTECODE_OBJECT_MAGIC.len() + // magic id @@ -725,11 +725,11 @@ fn link_staticlib(sess: &Session, obj_filename: &Path, out_filename: &Path) { let p = match *path { Some(ref p) => p.clone(), None => { sess.err(format!("could not find rlib for: `{}`", - name).as_slice()); + name)[]); continue } }; - ab.add_rlib(&p, name.as_slice(), sess.lto()).unwrap(); + ab.add_rlib(&p, name[], sess.lto()).unwrap(); let native_libs = csearch::get_native_libraries(&sess.cstore, cnum); all_native_libs.extend(native_libs.into_iter()); @@ -751,7 +751,7 @@ fn link_staticlib(sess: &Session, obj_filename: &Path, out_filename: &Path) { cstore::NativeUnknown => "library", cstore::NativeFramework => "framework", }; - sess.note(format!("{}: {}", name, *lib).as_slice()); + sess.note(format!("{}: {}", name, *lib)[]); } } @@ -765,12 +765,12 @@ fn link_natively(sess: &Session, trans: &CrateTranslation, dylib: bool, // The invocations of cc share some flags across platforms let pname = get_cc_prog(sess); - let mut cmd = Command::new(pname.as_slice()); + let mut cmd = Command::new(pname[]); - cmd.args(sess.target.target.options.pre_link_args.as_slice()); + cmd.args(sess.target.target.options.pre_link_args[]); link_args(&mut cmd, sess, dylib, tmpdir.path(), trans, obj_filename, out_filename); - cmd.args(sess.target.target.options.post_link_args.as_slice()); + cmd.args(sess.target.target.options.post_link_args[]); if !sess.target.target.options.no_compiler_rt { cmd.arg("-lcompiler-rt"); } @@ -790,11 +790,11 @@ fn link_natively(sess: &Session, trans: &CrateTranslation, dylib: bool, if !prog.status.success() { sess.err(format!("linking with `{}` failed: {}", pname, - prog.status).as_slice()); - sess.note(format!("{}", &cmd).as_slice()); + prog.status)[]); + sess.note(format!("{}", &cmd)[]); let mut output = prog.error.clone(); - output.push_all(prog.output.as_slice()); - sess.note(str::from_utf8(output.as_slice()).unwrap()); + output.push_all(prog.output[]); + sess.note(str::from_utf8(output[]).unwrap()); sess.abort_if_errors(); } debug!("linker stderr:\n{}", String::from_utf8(prog.error).unwrap()); @@ -803,7 +803,7 @@ fn link_natively(sess: &Session, trans: &CrateTranslation, dylib: bool, Err(e) => { sess.err(format!("could not exec the linker `{}`: {}", pname, - e).as_slice()); + e)[]); sess.abort_if_errors(); } } @@ -815,7 +815,7 @@ fn link_natively(sess: &Session, trans: &CrateTranslation, dylib: bool, match Command::new("dsymutil").arg(out_filename).output() { Ok(..) => {} Err(e) => { - sess.err(format!("failed to run dsymutil: {}", e).as_slice()); + sess.err(format!("failed to run dsymutil: {}", e)[]); sess.abort_if_errors(); } } @@ -864,7 +864,7 @@ fn link_args(cmd: &mut Command, let mut v = b"-Wl,-force_load,".to_vec(); v.push_all(morestack.as_vec()); - cmd.arg(v.as_slice()); + cmd.arg(v[]); } else { cmd.args(&["-Wl,--whole-archive", "-lmorestack", "-Wl,--no-whole-archive"]); } @@ -989,7 +989,7 @@ fn link_args(cmd: &mut Command, if sess.opts.cg.rpath { let mut v = "-Wl,-install_name,@rpath/".as_bytes().to_vec(); v.push_all(out_filename.filename().unwrap()); - cmd.arg(v.as_slice()); + cmd.arg(v[]); } } else { cmd.arg("-shared"); @@ -1001,7 +1001,7 @@ fn link_args(cmd: &mut Command, // addl_lib_search_paths if sess.opts.cg.rpath { let sysroot = sess.sysroot(); - let target_triple = sess.opts.target_triple.as_slice(); + let target_triple = sess.opts.target_triple[]; let get_install_prefix_lib_path = |:| { let install_prefix = option_env!("CFG_PREFIX").expect("CFG_PREFIX"); let tlib = filesearch::relative_target_lib_path(sysroot, target_triple); @@ -1018,14 +1018,14 @@ fn link_args(cmd: &mut Command, get_install_prefix_lib_path: get_install_prefix_lib_path, realpath: ::util::fs::realpath }; - cmd.args(rpath::get_rpath_flags(rpath_config).as_slice()); + cmd.args(rpath::get_rpath_flags(rpath_config)[]); } // Finally add all the linker arguments provided on the command line along // with any #[link_args] attributes found inside the crate let empty = Vec::new(); - cmd.args(sess.opts.cg.link_args.as_ref().unwrap_or(&empty).as_slice()); - cmd.args(used_link_args.as_slice()); + cmd.args(sess.opts.cg.link_args.as_ref().unwrap_or(&empty)[]); + cmd.args(used_link_args[]); } // # Native library linking @@ -1083,14 +1083,14 @@ fn add_local_native_libraries(cmd: &mut Command, sess: &Session) { } else { // -force_load is the OSX equivalent of --whole-archive, but it // involves passing the full path to the library to link. - let lib = archive::find_library(l.as_slice(), - sess.target.target.options.staticlib_prefix.as_slice(), - sess.target.target.options.staticlib_suffix.as_slice(), - search_path.as_slice(), + let lib = archive::find_library(l[], + sess.target.target.options.staticlib_prefix[], + sess.target.target.options.staticlib_suffix[], + search_path[], &sess.diagnostic().handler); let mut v = b"-Wl,-force_load,".to_vec(); v.push_all(lib.as_vec()); - cmd.arg(v.as_slice()); + cmd.arg(v[]); } } if takes_hints { @@ -1103,7 +1103,7 @@ fn add_local_native_libraries(cmd: &mut Command, sess: &Session) { cmd.arg(format!("-l{}", l)); } cstore::NativeFramework => { - cmd.arg("-framework").arg(l.as_slice()); + cmd.arg("-framework").arg(l[]); } cstore::NativeStatic => unreachable!(), } @@ -1184,9 +1184,9 @@ fn add_static_crate(cmd: &mut Command, sess: &Session, tmpdir: &Path, // against the archive. if sess.lto() { let name = cratepath.filename_str().unwrap(); - let name = name.slice(3, name.len() - 5); // chop off lib/.rlib + let name = name[3..name.len() - 5]; // chop off lib/.rlib time(sess.time_passes(), - format!("altering {}.rlib", name).as_slice(), + format!("altering {}.rlib", name)[], (), |()| { let dst = tmpdir.join(cratepath.filename().unwrap()); match fs::copy(&cratepath, &dst) { @@ -1195,7 +1195,7 @@ fn add_static_crate(cmd: &mut Command, sess: &Session, tmpdir: &Path, sess.err(format!("failed to copy {} to {}: {}", cratepath.display(), dst.display(), - e).as_slice()); + e)[]); sess.abort_if_errors(); } } @@ -1207,7 +1207,7 @@ fn add_static_crate(cmd: &mut Command, sess: &Session, tmpdir: &Path, Err(e) => { sess.err(format!("failed to chmod {} when preparing \ for LTO: {}", dst.display(), - e).as_slice()); + e)[]); sess.abort_if_errors(); } } @@ -1221,9 +1221,9 @@ fn add_static_crate(cmd: &mut Command, sess: &Session, tmpdir: &Path, maybe_ar_prog: sess.opts.cg.ar.clone() }; let mut archive = Archive::open(config); - archive.remove_file(format!("{}.o", name).as_slice()); + archive.remove_file(format!("{}.o", name)[]); let files = archive.files(); - if files.iter().any(|s| s.as_slice().ends_with(".o")) { + if files.iter().any(|s| s[].ends_with(".o")) { cmd.arg(dst); } }); @@ -1245,7 +1245,7 @@ fn add_dynamic_crate(cmd: &mut Command, sess: &Session, cratepath: Path) { let mut v = "-l".as_bytes().to_vec(); v.push_all(unlib(&sess.target, cratepath.filestem().unwrap())); - cmd.arg(v.as_slice()); + cmd.arg(v[]); } } @@ -1287,7 +1287,7 @@ fn add_upstream_native_libraries(cmd: &mut Command, sess: &Session) { } cstore::NativeFramework => { cmd.arg("-framework"); - cmd.arg(lib.as_slice()); + cmd.arg(lib[]); } cstore::NativeStatic => { sess.bug("statics shouldn't be propagated"); diff --git a/src/librustc_trans/back/lto.rs b/src/librustc_trans/back/lto.rs index b9357280d068..1271330897e7 100644 --- a/src/librustc_trans/back/lto.rs +++ b/src/librustc_trans/back/lto.rs @@ -53,21 +53,21 @@ pub fn run(sess: &session::Session, llmod: ModuleRef, Some(p) => p, None => { sess.fatal(format!("could not find rlib for: `{}`", - name).as_slice()); + name)[]); } }; let archive = ArchiveRO::open(&path).expect("wanted an rlib"); let file = path.filename_str().unwrap(); - let file = file.slice(3, file.len() - 5); // chop off lib/.rlib + let file = file[3..file.len() - 5]; // chop off lib/.rlib debug!("reading {}", file); for i in iter::count(0u, 1) { let bc_encoded = time(sess.time_passes(), - format!("check for {}.{}.bytecode.deflate", name, i).as_slice(), + format!("check for {}.{}.bytecode.deflate", name, i)[], (), |_| { archive.read(format!("{}.{}.bytecode.deflate", - file, i).as_slice()) + file, i)[]) }); let bc_encoded = match bc_encoded { Some(data) => data, @@ -75,7 +75,7 @@ pub fn run(sess: &session::Session, llmod: ModuleRef, if i == 0 { // No bitcode was found at all. sess.fatal(format!("missing compressed bytecode in {}", - path.display()).as_slice()); + path.display())[]); } // No more bitcode files to read. break; @@ -98,12 +98,12 @@ pub fn run(sess: &session::Session, llmod: ModuleRef, Some(inflated) => inflated, None => { sess.fatal(format!("failed to decompress bc of `{}`", - name).as_slice()) + name)[]) } } } else { sess.fatal(format!("Unsupported bytecode format version {}", - version).as_slice()) + version)[]) } }) } else { @@ -114,7 +114,7 @@ pub fn run(sess: &session::Session, llmod: ModuleRef, Some(bc) => bc, None => { sess.fatal(format!("failed to decompress bc of `{}`", - name).as_slice()) + name)[]) } } }) @@ -123,7 +123,7 @@ pub fn run(sess: &session::Session, llmod: ModuleRef, let ptr = bc_decoded.as_slice().as_ptr(); debug!("linking {}, part {}", name, i); time(sess.time_passes(), - format!("ll link {}.{}", name, i).as_slice(), + format!("ll link {}.{}", name, i)[], (), |()| unsafe { if !llvm::LLVMRustLinkInExternalBitcode(llmod, @@ -131,7 +131,7 @@ pub fn run(sess: &session::Session, llmod: ModuleRef, bc_decoded.len() as libc::size_t) { write::llvm_err(sess.diagnostic().handler(), format!("failed to load bc of `{}`", - name.as_slice())); + name[])); } }); } diff --git a/src/librustc_trans/back/write.rs b/src/librustc_trans/back/write.rs index 60b5b32e89f5..5be66d429209 100644 --- a/src/librustc_trans/back/write.rs +++ b/src/librustc_trans/back/write.rs @@ -46,13 +46,13 @@ pub fn llvm_err(handler: &diagnostic::Handler, msg: String) -> ! { unsafe { let cstr = llvm::LLVMRustGetLastError(); if cstr == ptr::null() { - handler.fatal(msg.as_slice()); + handler.fatal(msg[]); } else { let err = CString::new(cstr, true); let err = String::from_utf8_lossy(err.as_bytes()); handler.fatal(format!("{}: {}", - msg.as_slice(), - err.as_slice()).as_slice()); + msg[], + err[])[]); } } } @@ -103,13 +103,13 @@ fn dump(&mut self, handler: &Handler) { match diag.code { Some(ref code) => { handler.emit_with_code(None, - diag.msg.as_slice(), - code.as_slice(), + diag.msg[], + code[], diag.lvl); }, None => { handler.emit(None, - diag.msg.as_slice(), + diag.msg[], diag.lvl); }, } @@ -164,8 +164,8 @@ fn get_llvm_opt_level(optimize: config::OptLevel) -> llvm::CodeGenOptLevel { fn create_target_machine(sess: &Session) -> TargetMachineRef { let reloc_model_arg = match sess.opts.cg.relocation_model { - Some(ref s) => s.as_slice(), - None => sess.target.target.options.relocation_model.as_slice() + Some(ref s) => s[], + None => sess.target.target.options.relocation_model[] }; let reloc_model = match reloc_model_arg { "pic" => llvm::RelocPIC, @@ -176,7 +176,7 @@ fn create_target_machine(sess: &Session) -> TargetMachineRef { sess.err(format!("{} is not a valid relocation mode", sess.opts .cg - .relocation_model).as_slice()); + .relocation_model)[]); sess.abort_if_errors(); unreachable!(); } @@ -197,8 +197,8 @@ fn create_target_machine(sess: &Session) -> TargetMachineRef { let fdata_sections = ffunction_sections; let code_model_arg = match sess.opts.cg.code_model { - Some(ref s) => s.as_slice(), - None => sess.target.target.options.code_model.as_slice() + Some(ref s) => s[], + None => sess.target.target.options.code_model[] }; let code_model = match code_model_arg { @@ -211,19 +211,19 @@ fn create_target_machine(sess: &Session) -> TargetMachineRef { sess.err(format!("{} is not a valid code model", sess.opts .cg - .code_model).as_slice()); + .code_model)[]); sess.abort_if_errors(); unreachable!(); } }; - let triple = sess.target.target.llvm_target.as_slice(); + let triple = sess.target.target.llvm_target[]; let tm = unsafe { triple.with_c_str(|t| { let cpu = match sess.opts.cg.target_cpu { - Some(ref s) => s.as_slice(), - None => sess.target.target.options.cpu.as_slice() + Some(ref s) => s[], + None => sess.target.target.options.cpu[] }; cpu.with_c_str(|cpu| { target_feature(sess).with_c_str(|features| { @@ -350,13 +350,13 @@ struct HandlerFreeVars<'a> { match cgcx.lto_ctxt { Some((sess, _)) => { sess.codemap().with_expn_info(ExpnId::from_llvm_cookie(cookie), |info| match info { - Some(ei) => sess.span_err(ei.call_site, msg.as_slice()), - None => sess.err(msg.as_slice()), + Some(ei) => sess.span_err(ei.call_site, msg[]), + None => sess.err(msg[]), }); } None => { - cgcx.handler.err(msg.as_slice()); + cgcx.handler.err(msg[]); cgcx.handler.note("build without -C codegen-units for more exact errors"); } } @@ -380,8 +380,8 @@ struct HandlerFreeVars<'a> { cgcx.handler.note(format!("optimization {} for {} at {}: {}", opt.kind.describe(), pass_name, - if loc.is_empty() { "[unknown]" } else { loc.as_slice() }, - llvm::twine_to_string(opt.message)).as_slice()); + if loc.is_empty() { "[unknown]" } else { loc[] }, + llvm::twine_to_string(opt.message))[]); } } @@ -413,7 +413,7 @@ unsafe fn optimize_and_codegen(cgcx: &CodegenContext, if config.emit_no_opt_bc { let ext = format!("{}.no-opt.bc", name_extra); - output_names.with_extension(ext.as_slice()).with_c_str(|buf| { + output_names.with_extension(ext[]).with_c_str(|buf| { llvm::LLVMWriteBitcodeToFile(llmod, buf); }) } @@ -445,7 +445,7 @@ unsafe fn optimize_and_codegen(cgcx: &CodegenContext, pass.with_c_str(|s| { if !llvm::LLVMRustAddPass(mpm, s) { cgcx.handler.warn(format!("unknown pass {}, ignoring", - *pass).as_slice()); + *pass)[]); } }) } @@ -467,7 +467,7 @@ unsafe fn optimize_and_codegen(cgcx: &CodegenContext, if config.emit_lto_bc { let name = format!("{}.lto.bc", name_extra); - output_names.with_extension(name.as_slice()).with_c_str(|buf| { + output_names.with_extension(name[]).with_c_str(|buf| { llvm::LLVMWriteBitcodeToFile(llmod, buf); }) } @@ -501,7 +501,7 @@ unsafe fn with_codegen(tm: TargetMachineRef, if config.emit_bc { let ext = format!("{}.bc", name_extra); - output_names.with_extension(ext.as_slice()).with_c_str(|buf| { + output_names.with_extension(ext[]).with_c_str(|buf| { llvm::LLVMWriteBitcodeToFile(llmod, buf); }) } @@ -509,7 +509,7 @@ unsafe fn with_codegen(tm: TargetMachineRef, time(config.time_passes, "codegen passes", (), |()| { if config.emit_ir { let ext = format!("{}.ll", name_extra); - output_names.with_extension(ext.as_slice()).with_c_str(|output| { + output_names.with_extension(ext[]).with_c_str(|output| { with_codegen(tm, llmod, config.no_builtins, |cpm| { llvm::LLVMRustPrintModule(cpm, llmod, output); }) @@ -517,14 +517,14 @@ unsafe fn with_codegen(tm: TargetMachineRef, } if config.emit_asm { - let path = output_names.with_extension(format!("{}.s", name_extra).as_slice()); + let path = output_names.with_extension(format!("{}.s", name_extra)[]); with_codegen(tm, llmod, config.no_builtins, |cpm| { write_output_file(cgcx.handler, tm, cpm, llmod, &path, llvm::AssemblyFileType); }); } if config.emit_obj { - let path = output_names.with_extension(format!("{}.o", name_extra).as_slice()); + let path = output_names.with_extension(format!("{}.o", name_extra)[]); with_codegen(tm, llmod, config.no_builtins, |cpm| { write_output_file(cgcx.handler, tm, cpm, llmod, &path, llvm::ObjectFileType); }); @@ -638,7 +638,7 @@ pub fn run_passes(sess: &Session, // Process the work items, optionally using worker threads. if sess.opts.cg.codegen_units == 1 { - run_work_singlethreaded(sess, trans.reachable.as_slice(), work_items); + run_work_singlethreaded(sess, trans.reachable[], work_items); } else { run_work_multithreaded(sess, work_items, sess.opts.cg.codegen_units); } @@ -666,7 +666,7 @@ pub fn run_passes(sess: &Session, // 2) Multiple codegen units, with `-o some_name`. We have // no good solution for this case, so warn the user. sess.warn(format!("ignoring -o because multiple .{} files were produced", - ext).as_slice()); + ext)[]); } else { // 3) Multiple codegen units, but no `-o some_name`. We // just leave the `foo.0.x` files in place. @@ -699,20 +699,20 @@ pub fn run_passes(sess: &Session, }; let pname = get_cc_prog(sess); - let mut cmd = Command::new(pname.as_slice()); + let mut cmd = Command::new(pname[]); - cmd.args(sess.target.target.options.pre_link_args.as_slice()); + cmd.args(sess.target.target.options.pre_link_args[]); cmd.arg("-nostdlib"); for index in range(0, trans.modules.len()) { - cmd.arg(crate_output.with_extension(format!("{}.o", index).as_slice())); + cmd.arg(crate_output.with_extension(format!("{}.o", index)[])); } cmd.arg("-r") .arg("-o") .arg(windows_output_path.as_ref().unwrap_or(output_path)); - cmd.args(sess.target.target.options.post_link_args.as_slice()); + cmd.args(sess.target.target.options.post_link_args[]); if (sess.opts.debugging_opts & config::PRINT_LINK_ARGS) != 0 { println!("{}", &cmd); @@ -725,14 +725,14 @@ pub fn run_passes(sess: &Session, Ok(status) => { if !status.success() { sess.err(format!("linking of {} with `{}` failed", - output_path.display(), cmd).as_slice()); + output_path.display(), cmd)[]); sess.abort_if_errors(); } }, Err(e) => { sess.err(format!("could not exec the linker `{}`: {}", pname, - e).as_slice()); + e)[]); sess.abort_if_errors(); }, } @@ -817,12 +817,12 @@ pub fn run_passes(sess: &Session, for i in range(0, trans.modules.len()) { if modules_config.emit_obj { let ext = format!("{}.o", i); - remove(sess, &crate_output.with_extension(ext.as_slice())); + remove(sess, &crate_output.with_extension(ext[])); } if modules_config.emit_bc && !keep_numbered_bitcode { let ext = format!("{}.bc", i); - remove(sess, &crate_output.with_extension(ext.as_slice())); + remove(sess, &crate_output.with_extension(ext[])); } } @@ -948,7 +948,7 @@ fn run_work_multithreaded(sess: &Session, pub fn run_assembler(sess: &Session, outputs: &OutputFilenames) { let pname = get_cc_prog(sess); - let mut cmd = Command::new(pname.as_slice()); + let mut cmd = Command::new(pname[]); cmd.arg("-c").arg("-o").arg(outputs.path(config::OutputTypeObject)) .arg(outputs.temp_path(config::OutputTypeAssembly)); @@ -959,18 +959,18 @@ pub fn run_assembler(sess: &Session, outputs: &OutputFilenames) { if !prog.status.success() { sess.err(format!("linking with `{}` failed: {}", pname, - prog.status).as_slice()); - sess.note(format!("{}", &cmd).as_slice()); + prog.status)[]); + sess.note(format!("{}", &cmd)[]); let mut note = prog.error.clone(); - note.push_all(prog.output.as_slice()); - sess.note(str::from_utf8(note.as_slice()).unwrap()); + note.push_all(prog.output[]); + sess.note(str::from_utf8(note[]).unwrap()); sess.abort_if_errors(); } }, Err(e) => { sess.err(format!("could not exec the linker `{}`: {}", pname, - e).as_slice()); + e)[]); sess.abort_if_errors(); } } @@ -1003,7 +1003,7 @@ unsafe fn configure_llvm(sess: &Session) { if sess.print_llvm_passes() { add("-debug-pass=Structure"); } for arg in sess.opts.cg.llvm_args.iter() { - add((*arg).as_slice()); + add((*arg)[]); } } diff --git a/src/librustc_trans/save/mod.rs b/src/librustc_trans/save/mod.rs index 1a4f06663ef3..0183aa8c2aab 100644 --- a/src/librustc_trans/save/mod.rs +++ b/src/librustc_trans/save/mod.rs @@ -94,7 +94,7 @@ fn dump_crate_info(&mut self, name: &str, krate: &ast::Crate) { // dump info about all the external crates referenced from this crate self.sess.cstore.iter_crate_data(|n, cmd| { - self.fmt.external_crate_str(krate.span, cmd.name.as_slice(), n); + self.fmt.external_crate_str(krate.span, cmd.name[], n); }); self.fmt.recorder.record("end_external_crates\n"); } @@ -143,7 +143,7 @@ fn write_sub_paths(&mut self, path: &ast::Path) { for &(ref span, ref qualname) in sub_paths.iter() { self.fmt.sub_mod_ref_str(path.span, *span, - qualname.as_slice(), + qualname[], self.cur_scope); } } @@ -161,7 +161,7 @@ fn write_sub_paths_truncated(&mut self, path: &ast::Path) { for &(ref span, ref qualname) in sub_paths.iter() { self.fmt.sub_mod_ref_str(path.span, *span, - qualname.as_slice(), + qualname[], self.cur_scope); } } @@ -180,7 +180,7 @@ fn write_sub_path_trait_truncated(&mut self, path: &ast::Path) { let (ref span, ref qualname) = sub_paths[len-2]; self.fmt.sub_type_ref_str(path.span, *span, - qualname.as_slice()); + qualname[]); // write the other sub-paths if len <= 2 { @@ -190,7 +190,7 @@ fn write_sub_path_trait_truncated(&mut self, path: &ast::Path) { for &(ref span, ref qualname) in sub_paths.iter() { self.fmt.sub_mod_ref_str(path.span, *span, - qualname.as_slice(), + qualname[], self.cur_scope); } } @@ -199,7 +199,7 @@ fn write_sub_path_trait_truncated(&mut self, path: &ast::Path) { fn lookup_type_ref(&self, ref_id: NodeId) -> Option { if !self.analysis.ty_cx.def_map.borrow().contains_key(&ref_id) { self.sess.bug(format!("def_map has no key for {} in lookup_type_ref", - ref_id).as_slice()); + ref_id)[]); } let def = (*self.analysis.ty_cx.def_map.borrow())[ref_id]; match def { @@ -212,7 +212,7 @@ fn lookup_def_kind(&self, ref_id: NodeId, span: Span) -> Option { let def_map = self.analysis.ty_cx.def_map.borrow(); if !def_map.contains_key(&ref_id) { self.sess.span_bug(span, format!("def_map has no key for {} in lookup_def_kind", - ref_id).as_slice()); + ref_id)[]); } let def = (*def_map)[ref_id]; match def { @@ -241,7 +241,7 @@ fn lookup_def_kind(&self, ref_id: NodeId, span: Span) -> Option { def::DefMethod(..) | def::DefPrimTy(_) => { self.sess.span_bug(span, format!("lookup_def_kind for unexpected item: {}", - def).as_slice()); + def)[]); }, } } @@ -262,8 +262,8 @@ fn process_formals(&mut self, formals: &Vec, qualname: &str) { span_utils.span_for_last_ident(p.span), id, qualname, - path_to_string(p).as_slice(), - typ.as_slice()); + path_to_string(p)[], + typ[]); } self.collected_paths.clear(); } @@ -285,14 +285,14 @@ fn process_method(&mut self, method: &ast::Method) { match item.node { ast::ItemImpl(_, _, _, ref ty, _) => { let mut result = String::from_str("<"); - result.push_str(ty_to_string(&**ty).as_slice()); + result.push_str(ty_to_string(&**ty)[]); match ty::trait_of_item(&self.analysis.ty_cx, ast_util::local_def(method.id)) { Some(def_id) => { result.push_str(" as "); result.push_str( - ty::item_path_str(&self.analysis.ty_cx, def_id).as_slice()); + ty::item_path_str(&self.analysis.ty_cx, def_id)[]); }, None => {} } @@ -302,7 +302,7 @@ fn process_method(&mut self, method: &ast::Method) { _ => { self.sess.span_bug(method.span, format!("Container {} for method {} not an impl?", - impl_id.node, method.id).as_slice()); + impl_id.node, method.id)[]); }, } }, @@ -312,7 +312,7 @@ fn process_method(&mut self, method: &ast::Method) { impl_id.node, method.id, self.analysis.ty_cx.map.get(impl_id.node) - ).as_slice()); + )[]); }, }, None => match ty::trait_of_item(&self.analysis.ty_cx, @@ -328,20 +328,20 @@ fn process_method(&mut self, method: &ast::Method) { _ => { self.sess.span_bug(method.span, format!("Could not find container {} for method {}", - def_id.node, method.id).as_slice()); + def_id.node, method.id)[]); } } }, None => { self.sess.span_bug(method.span, format!("Could not find container for method {}", - method.id).as_slice()); + method.id)[]); }, }, }; qualname.push_str(get_ident(method.pe_ident()).get()); - let qualname = qualname.as_slice(); + let qualname = qualname[]; // record the decl for this def (if it has one) let decl_id = ty::trait_item_of_item(&self.analysis.ty_cx, @@ -430,13 +430,13 @@ fn process_struct_field_def(&mut self, Some(sub_span) => self.fmt.field_str(field.span, Some(sub_span), field.node.id, - name.get().as_slice(), - qualname.as_slice(), - typ.as_slice(), + name.get()[], + qualname[], + typ[], scope_id), None => self.sess.span_bug(field.span, format!("Could not find sub-span for field {}", - qualname).as_slice()), + qualname)[]), } }, _ => (), @@ -463,7 +463,7 @@ fn process_generic_params(&mut self, generics:&ast::Generics, self.fmt.typedef_str(full_span, Some(*param_ss), param.id, - name.as_slice(), + name[], ""); } self.visit_generics(generics); @@ -480,10 +480,10 @@ fn process_fn(&mut self, self.fmt.fn_str(item.span, sub_span, item.id, - qualname.as_slice(), + qualname[], self.cur_scope); - self.process_formals(&decl.inputs, qualname.as_slice()); + self.process_formals(&decl.inputs, qualname[]); // walk arg and return types for arg in decl.inputs.iter() { @@ -497,7 +497,7 @@ fn process_fn(&mut self, // walk the body self.nest(item.id, |v| v.visit_block(&*body)); - self.process_generic_params(ty_params, item.span, qualname.as_slice(), item.id); + self.process_generic_params(ty_params, item.span, qualname[], item.id); } fn process_static(&mut self, @@ -519,9 +519,9 @@ fn process_static(&mut self, sub_span, item.id, get_ident(item.ident).get(), - qualname.as_slice(), - value.as_slice(), - ty_to_string(&*typ).as_slice(), + qualname[], + value[], + ty_to_string(&*typ)[], self.cur_scope); // walk type and init value @@ -542,9 +542,9 @@ fn process_const(&mut self, sub_span, item.id, get_ident(item.ident).get(), - qualname.as_slice(), + qualname[], "", - ty_to_string(&*typ).as_slice(), + ty_to_string(&*typ)[], self.cur_scope); // walk type and init value @@ -568,17 +568,17 @@ fn process_struct(&mut self, sub_span, item.id, ctor_id, - qualname.as_slice(), + qualname[], self.cur_scope, - val.as_slice()); + val[]); // fields for field in def.fields.iter() { - self.process_struct_field_def(field, qualname.as_slice(), item.id); + self.process_struct_field_def(field, qualname[], item.id); self.visit_ty(&*field.node.ty); } - self.process_generic_params(ty_params, item.span, qualname.as_slice(), item.id); + self.process_generic_params(ty_params, item.span, qualname[], item.id); } fn process_enum(&mut self, @@ -591,12 +591,12 @@ fn process_enum(&mut self, Some(sub_span) => self.fmt.enum_str(item.span, Some(sub_span), item.id, - enum_name.as_slice(), + enum_name[], self.cur_scope, - val.as_slice()), + val[]), None => self.sess.span_bug(item.span, format!("Could not find subspan for enum {}", - enum_name).as_slice()), + enum_name)[]), } for variant in enum_definition.variants.iter() { let name = get_ident(variant.node.name); @@ -612,9 +612,9 @@ fn process_enum(&mut self, self.span.span_for_first_ident(variant.span), variant.node.id, name, - qualname.as_slice(), - enum_name.as_slice(), - val.as_slice(), + qualname[], + enum_name[], + val[], item.id); for arg in args.iter() { self.visit_ty(&*arg.ty); @@ -630,20 +630,20 @@ fn process_enum(&mut self, self.span.span_for_first_ident(variant.span), variant.node.id, ctor_id, - qualname.as_slice(), - enum_name.as_slice(), - val.as_slice(), + qualname[], + enum_name[], + val[], item.id); for field in struct_def.fields.iter() { - self.process_struct_field_def(field, enum_name.as_slice(), variant.node.id); + self.process_struct_field_def(field, enum_name[], variant.node.id); self.visit_ty(&*field.node.ty); } } } } - self.process_generic_params(ty_params, item.span, enum_name.as_slice(), item.id); + self.process_generic_params(ty_params, item.span, enum_name[], item.id); } fn process_impl(&mut self, @@ -703,9 +703,9 @@ fn process_trait(&mut self, self.fmt.trait_str(item.span, sub_span, item.id, - qualname.as_slice(), + qualname[], self.cur_scope, - val.as_slice()); + val[]); // super-traits for super_bound in trait_refs.iter() { @@ -737,7 +737,7 @@ fn process_trait(&mut self, } // walk generics and methods - self.process_generic_params(generics, item.span, qualname.as_slice(), item.id); + self.process_generic_params(generics, item.span, qualname[], item.id); for method in methods.iter() { self.visit_trait_item(method) } @@ -755,9 +755,9 @@ fn process_mod(&mut self, self.fmt.mod_str(item.span, sub_span, item.id, - qualname.as_slice(), + qualname[], self.cur_scope, - filename.as_slice()); + filename[]); self.nest(item.id, |v| visit::walk_mod(v, m)); } @@ -773,7 +773,7 @@ fn process_path(&mut self, if !def_map.contains_key(&ex.id) { self.sess.span_bug(ex.span, format!("def_map has no key for {} in visit_expr", - ex.id).as_slice()); + ex.id)[]); } let def = &(*def_map)[ex.id]; let sub_span = self.span.span_for_last_ident(ex.span); @@ -840,7 +840,7 @@ fn process_path(&mut self, self.cur_scope), _ => self.sess.span_bug(ex.span, format!("Unexpected def kind while looking up path in '{}'", - self.span.snippet(ex.span)).as_slice()), + self.span.snippet(ex.span))[]), } // modules or types in the path prefix match *def { @@ -961,7 +961,7 @@ fn process_method_call(&mut self, self.cur_scope); // walk receiver and args - visit::walk_exprs(self, args.as_slice()); + visit::walk_exprs(self, args[]); } fn process_pat(&mut self, p:&ast::Pat) { @@ -978,7 +978,7 @@ fn process_pat(&mut self, p:&ast::Pat) { None => { self.sess.span_bug(p.span, format!("Could not find struct_def for `{}`", - self.span.snippet(p.span)).as_slice()); + self.span.snippet(p.span))[]); } }; for &Spanned { node: ref field, span } in fields.iter() { @@ -1062,11 +1062,11 @@ fn visit_item(&mut self, item: &ast::Item) { self.fmt.typedef_str(item.span, sub_span, item.id, - qualname.as_slice(), - value.as_slice()); + qualname[], + value[]); self.visit_ty(&**ty); - self.process_generic_params(ty_params, item.span, qualname.as_slice(), item.id); + self.process_generic_params(ty_params, item.span, qualname[], item.id); }, ast::ItemMac(_) => (), _ => visit::walk_item(self, item), @@ -1123,12 +1123,12 @@ fn visit_trait_item(&mut self, tm: &ast::TraitItem) { None => { self.sess.span_bug(method_type.span, format!("Could not find trait for method {}", - method_type.id).as_slice()); + method_type.id)[]); }, }; qualname.push_str(get_ident(method_type.ident).get()); - let qualname = qualname.as_slice(); + let qualname = qualname[]; let sub_span = self.span.sub_span_after_keyword(method_type.span, keywords::Fn); self.fmt.method_decl_str(method_type.span, @@ -1243,7 +1243,7 @@ fn visit_view_item(&mut self, i: &ast::ViewItem) { id, cnum, name, - s.as_slice(), + s[], self.cur_scope); }, } @@ -1349,8 +1349,8 @@ fn visit_expr(&mut self, ex: &ast::Expr) { } let mut id = String::from_str("$"); - id.push_str(ex.id.to_string().as_slice()); - self.process_formals(&decl.inputs, id.as_slice()); + id.push_str(ex.id.to_string()[]); + self.process_formals(&decl.inputs, id[]); // walk arg and return types for arg in decl.inputs.iter() { @@ -1393,7 +1393,7 @@ fn visit_arm(&mut self, arm: &ast::Arm) { // process collected paths for &(id, ref p, ref immut, ref_kind) in self.collected_paths.iter() { let value = if *immut { - self.span.snippet(p.span).into_string() + self.span.snippet(p.span).to_string() } else { "".to_string() }; @@ -1402,15 +1402,15 @@ fn visit_arm(&mut self, arm: &ast::Arm) { if !def_map.contains_key(&id) { self.sess.span_bug(p.span, format!("def_map has no key for {} in visit_arm", - id).as_slice()); + id)[]); } let def = &(*def_map)[id]; match *def { def::DefLocal(id) => self.fmt.variable_str(p.span, sub_span, id, - path_to_string(p).as_slice(), - value.as_slice(), + path_to_string(p)[], + value[], ""), def::DefVariant(_,id,_) => self.fmt.ref_str(ref_kind, p.span, @@ -1462,9 +1462,9 @@ fn visit_local(&mut self, l: &ast::Local) { self.fmt.variable_str(p.span, sub_span, id, - path_to_string(p).as_slice(), - value.as_slice(), - typ.as_slice()); + path_to_string(p)[], + value[], + typ[]); } self.collected_paths.clear(); @@ -1482,7 +1482,7 @@ pub fn process_crate(sess: &Session, return; } - let cratename = match attr::find_crate_name(krate.attrs.as_slice()) { + let cratename = match attr::find_crate_name(krate.attrs[]) { Some(name) => name.get().to_string(), None => { info!("Could not find crate name, using 'unknown_crate'"); @@ -1503,7 +1503,7 @@ pub fn process_crate(sess: &Session, match fs::mkdir_recursive(&root_path, io::USER_RWX) { Err(e) => sess.err(format!("Could not create directory {}: {}", - root_path.display(), e).as_slice()), + root_path.display(), e)[]), _ => (), } @@ -1520,7 +1520,7 @@ pub fn process_crate(sess: &Session, Ok(f) => box f, Err(e) => { let disp = root_path.display(); - sess.fatal(format!("Could not open {}: {}", disp, e).as_slice()); + sess.fatal(format!("Could not open {}: {}", disp, e)[]); } }; root_path.pop(); @@ -1546,7 +1546,7 @@ pub fn process_crate(sess: &Session, cur_scope: 0 }; - visitor.dump_crate_info(cratename.as_slice(), krate); + visitor.dump_crate_info(cratename[], krate); visit::walk_crate(&mut visitor, krate); } diff --git a/src/librustc_trans/save/recorder.rs b/src/librustc_trans/save/recorder.rs index 37d9e5d99407..08670864ade9 100644 --- a/src/librustc_trans/save/recorder.rs +++ b/src/librustc_trans/save/recorder.rs @@ -41,7 +41,7 @@ pub fn dump_span(&mut self, assert!(self.dump_spans); let result = format!("span,kind,{},{},text,\"{}\"\n", kind, su.extent_str(span), escape(su.snippet(span))); - self.record(result.as_slice()); + self.record(result[]); } } @@ -158,15 +158,15 @@ pub fn make_values_str(&self, if values.len() != fields.len() { self.span.sess.span_bug(span, format!( "Mismatch between length of fields for '{}', expected '{}', found '{}'", - kind, fields.len(), values.len()).as_slice()); + kind, fields.len(), values.len())[]); } let values = values.iter().map(|s| { // Never take more than 1020 chars if s.len() > 1020 { - s.slice_to(1020) + s[..1020] } else { - s.as_slice() + s[] } }); @@ -182,7 +182,7 @@ pub fn make_values_str(&self, } ))); Some(strs.fold(String::new(), |mut s, ss| { - s.push_str(ss.as_slice()); + s.push_str(ss[]); s })) } @@ -196,7 +196,7 @@ pub fn record_without_span(&mut self, if needs_span { self.span.sess.span_bug(span, format!( "Called record_without_span for '{}' which does requires a span", - label).as_slice()); + label)[]); } assert!(!dump_spans); @@ -210,9 +210,9 @@ pub fn record_without_span(&mut self, }; let mut result = String::from_str(label); - result.push_str(values_str.as_slice()); + result.push_str(values_str[]); result.push_str("\n"); - self.recorder.record(result.as_slice()); + self.recorder.record(result[]); } pub fn record_with_span(&mut self, @@ -235,7 +235,7 @@ pub fn record_with_span(&mut self, if !needs_span { self.span.sess.span_bug(span, format!("Called record_with_span for '{}' \ - which does not require a span", label).as_slice()); + which does not require a span", label)[]); } let values_str = match self.make_values_str(label, fields, values, span) { @@ -243,7 +243,7 @@ pub fn record_with_span(&mut self, None => return, }; let result = format!("{},{}{}\n", label, self.span.extent_str(sub_span), values_str); - self.recorder.record(result.as_slice()); + self.recorder.record(result[]); } pub fn check_and_record(&mut self, @@ -273,7 +273,7 @@ pub fn variable_str(&mut self, // variable def's node id let mut qualname = String::from_str(name); qualname.push_str("$"); - qualname.push_str(id.to_string().as_slice()); + qualname.push_str(id.to_string()[]); self.check_and_record(Variable, span, sub_span, diff --git a/src/librustc_trans/save/span_utils.rs b/src/librustc_trans/save/span_utils.rs index 49e8e0fd3471..a92d3c06e64f 100644 --- a/src/librustc_trans/save/span_utils.rs +++ b/src/librustc_trans/save/span_utils.rs @@ -218,7 +218,7 @@ pub fn sub_span_for_type_name(&self, span: Span) -> Option { let loc = self.sess.codemap().lookup_char_pos(span.lo); self.sess.span_bug(span, format!("Mis-counted brackets when breaking path? Parsing '{}' in {}, line {}", - self.snippet(span), loc.file.name, loc.line).as_slice()); + self.snippet(span), loc.file.name, loc.line)[]); } if result.is_none() && prev.tok.is_ident() && bracket_count == 0 { return self.make_sub_span(span, Some(prev.sp)); @@ -244,7 +244,7 @@ pub fn spans_with_brackets(&self, span: Span, nesting: int, limit: int) -> Vec(bcx: Block<'blk, 'tcx>, let _indenter = indenter(); m.iter().filter_map(|br| { - e(br.pats.as_slice()).map(|pats| { + e(br.pats[]).map(|pats| { let this = br.pats[col]; let mut bound_ptrs = br.bound_ptrs.clone(); match this.node { @@ -548,7 +548,7 @@ fn enter_opt<'a, 'p, 'blk, 'tcx>( param_env: param_env, }; enter_match(bcx, dm, m, col, val, |pats| - check_match::specialize(&mcx, pats.as_slice(), &ctor, col, variant_size) + check_match::specialize(&mcx, pats[], &ctor, col, variant_size) ) } @@ -790,7 +790,7 @@ fn compare_str<'blk, 'tcx>(cx: Block<'blk, 'tcx>, let did = langcall(cx, None, format!("comparison of `{}`", - cx.ty_to_string(rhs_t)).as_slice(), + cx.ty_to_string(rhs_t))[], StrEqFnLangItem); callee::trans_lang_call(cx, did, &[lhs, rhs], None) } @@ -943,7 +943,7 @@ fn compile_submatch<'a, 'p, 'blk, 'tcx>(bcx: Block<'blk, 'tcx>, if has_nested_bindings(m, col) { let expanded = expand_nested_bindings(bcx, m, col, val); compile_submatch_continue(bcx, - expanded.as_slice(), + expanded[], vals, chk, col, @@ -1035,8 +1035,8 @@ fn compile_submatch_continue<'a, 'p, 'blk, 'tcx>(mut bcx: Block<'blk, 'tcx>, field_vals.len()) ); let mut vals = field_vals; - vals.push_all(vals_left.as_slice()); - compile_submatch(bcx, pats.as_slice(), vals.as_slice(), chk, has_genuine_default); + vals.push_all(vals_left[]); + compile_submatch(bcx, pats[], vals[], chk, has_genuine_default); return; } _ => () @@ -1189,10 +1189,10 @@ fn compile_submatch_continue<'a, 'p, 'blk, 'tcx>(mut bcx: Block<'blk, 'tcx>, } let opt_ms = enter_opt(opt_cx, pat_id, dm, m, opt, col, size, val); let mut opt_vals = unpacked; - opt_vals.push_all(vals_left.as_slice()); + opt_vals.push_all(vals_left[]); compile_submatch(opt_cx, - opt_ms.as_slice(), - opt_vals.as_slice(), + opt_ms[], + opt_vals[], branch_chk.as_ref().unwrap_or(chk), has_genuine_default); } @@ -1211,8 +1211,8 @@ fn compile_submatch_continue<'a, 'p, 'blk, 'tcx>(mut bcx: Block<'blk, 'tcx>, } _ => { compile_submatch(else_cx, - defaults.as_slice(), - vals_left.as_slice(), + defaults[], + vals_left[], chk, has_genuine_default); } @@ -1333,7 +1333,7 @@ fn create_bindings_map<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, pat: &ast::Pat, "__llmatch"); trmode = TrByCopy(alloca_no_lifetime(bcx, llvariable_ty, - bcx.ident(ident).as_slice())); + bcx.ident(ident)[])); } ast::BindByValue(_) => { // in this case, the final type of the variable will be T, @@ -1341,13 +1341,13 @@ fn create_bindings_map<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, pat: &ast::Pat, // above llmatch = alloca_no_lifetime(bcx, llvariable_ty.ptr_to(), - bcx.ident(ident).as_slice()); + bcx.ident(ident)[]); trmode = TrByMove; } ast::BindByRef(_) => { llmatch = alloca_no_lifetime(bcx, llvariable_ty, - bcx.ident(ident).as_slice()); + bcx.ident(ident)[]); trmode = TrByRef; } }; @@ -1415,7 +1415,7 @@ fn trans_match_inner<'blk, 'tcx>(scope_cx: Block<'blk, 'tcx>, && arm.pats.last().unwrap().node == ast::PatWild(ast::PatWildSingle) }); - compile_submatch(bcx, matches.as_slice(), &[discr_datum.val], &chk, has_default); + compile_submatch(bcx, matches[], &[discr_datum.val], &chk, has_default); let mut arm_cxs = Vec::new(); for arm_data in arm_datas.iter() { @@ -1429,7 +1429,7 @@ fn trans_match_inner<'blk, 'tcx>(scope_cx: Block<'blk, 'tcx>, arm_cxs.push(bcx); } - bcx = scope_cx.fcx.join_blocks(match_id, arm_cxs.as_slice()); + bcx = scope_cx.fcx.join_blocks(match_id, arm_cxs[]); return bcx; } @@ -1581,7 +1581,7 @@ fn mk_binding_alloca<'blk, 'tcx, A, F>(bcx: Block<'blk, 'tcx>, let var_ty = node_id_type(bcx, p_id); // Allocate memory on stack for the binding. - let llval = alloc_ty(bcx, var_ty, bcx.ident(*ident).as_slice()); + let llval = alloc_ty(bcx, var_ty, bcx.ident(*ident)[]); // Subtle: be sure that we *populate* the memory *before* // we schedule the cleanup. @@ -1619,7 +1619,7 @@ fn bind_irrefutable_pat<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, if bcx.sess().asm_comments() { add_comment(bcx, format!("bind_irrefutable_pat(pat={})", - pat.repr(bcx.tcx())).as_slice()); + pat.repr(bcx.tcx()))[]); } let _indenter = indenter(); diff --git a/src/librustc_trans/trans/adt.rs b/src/librustc_trans/trans/adt.rs index f7edb281b9ed..9794611dd847 100644 --- a/src/librustc_trans/trans/adt.rs +++ b/src/librustc_trans/trans/adt.rs @@ -156,7 +156,7 @@ fn represent_type_uncached<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, t: Ty<'tcx>) -> Repr<'tcx> { match t.sty { ty::ty_tup(ref elems) => { - Univariant(mk_struct(cx, elems.as_slice(), false, t), false) + Univariant(mk_struct(cx, elems[], false, t), false) } ty::ty_struct(def_id, ref substs) => { let fields = ty::lookup_struct_fields(cx.tcx(), def_id); @@ -167,16 +167,16 @@ fn represent_type_uncached<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, let dtor = ty::ty_dtor(cx.tcx(), def_id).has_drop_flag(); if dtor { ftys.push(ty::mk_bool()); } - Univariant(mk_struct(cx, ftys.as_slice(), packed, t), dtor) + Univariant(mk_struct(cx, ftys[], packed, t), dtor) } ty::ty_unboxed_closure(def_id, _, ref substs) => { let upvars = ty::unboxed_closure_upvars(cx.tcx(), def_id, substs); let upvar_types = upvars.iter().map(|u| u.ty).collect::>(); - Univariant(mk_struct(cx, upvar_types.as_slice(), false, t), false) + Univariant(mk_struct(cx, upvar_types[], false, t), false) } ty::ty_enum(def_id, ref substs) => { let cases = get_cases(cx.tcx(), def_id, substs); - let hint = *ty::lookup_repr_hints(cx.tcx(), def_id).as_slice().get(0) + let hint = *ty::lookup_repr_hints(cx.tcx(), def_id)[].get(0) .unwrap_or(&attr::ReprAny); let dtor = ty::ty_dtor(cx.tcx(), def_id).has_drop_flag(); @@ -186,7 +186,7 @@ fn represent_type_uncached<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, // (Typechecking will reject discriminant-sizing attrs.) assert_eq!(hint, attr::ReprAny); let ftys = if dtor { vec!(ty::mk_bool()) } else { vec!() }; - return Univariant(mk_struct(cx, ftys.as_slice(), false, t), + return Univariant(mk_struct(cx, ftys[], false, t), dtor); } @@ -209,7 +209,7 @@ fn represent_type_uncached<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, cx.sess().bug(format!("non-C-like enum {} with specified \ discriminants", ty::item_path_str(cx.tcx(), - def_id)).as_slice()); + def_id))[]); } if cases.len() == 1 { @@ -218,7 +218,7 @@ fn represent_type_uncached<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, assert_eq!(hint, attr::ReprAny); let mut ftys = cases[0].tys.clone(); if dtor { ftys.push(ty::mk_bool()); } - return Univariant(mk_struct(cx, ftys.as_slice(), false, t), + return Univariant(mk_struct(cx, ftys[], false, t), dtor); } @@ -227,7 +227,7 @@ fn represent_type_uncached<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, let mut discr = 0; while discr < 2 { if cases[1 - discr].is_zerolen(cx, t) { - let st = mk_struct(cx, cases[discr].tys.as_slice(), + let st = mk_struct(cx, cases[discr].tys[], false, t); match cases[discr].find_ptr(cx) { Some(ThinPointer(_)) if st.fields.len() == 1 => { @@ -260,17 +260,17 @@ fn represent_type_uncached<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, let fields : Vec<_> = cases.iter().map(|c| { let mut ftys = vec!(ty_of_inttype(ity)); - ftys.push_all(c.tys.as_slice()); + ftys.push_all(c.tys[]); if dtor { ftys.push(ty::mk_bool()); } - mk_struct(cx, ftys.as_slice(), false, t) + mk_struct(cx, ftys[], false, t) }).collect(); - ensure_enum_fits_in_address_space(cx, ity, fields.as_slice(), t); + ensure_enum_fits_in_address_space(cx, ity, fields[], t); General(ity, fields, dtor) } _ => cx.sess().bug(format!("adt::represent_type called on non-ADT type: {}", - ty_to_string(cx.tcx(), t)).as_slice()) + ty_to_string(cx.tcx(), t))[]) } } @@ -290,7 +290,7 @@ pub enum PointerField { impl<'tcx> Case<'tcx> { fn is_zerolen<'a>(&self, cx: &CrateContext<'a, 'tcx>, scapegoat: Ty<'tcx>) -> bool { - mk_struct(cx, self.tys.as_slice(), false, scapegoat).size == 0 + mk_struct(cx, self.tys[], false, scapegoat).size == 0 } fn find_ptr<'a>(&self, cx: &CrateContext<'a, 'tcx>) -> Option { @@ -352,9 +352,9 @@ fn mk_struct<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, .map(|&ty| type_of::sizing_type_of(cx, ty)).collect() }; - ensure_struct_fits_in_address_space(cx, lltys.as_slice(), packed, scapegoat); + ensure_struct_fits_in_address_space(cx, lltys[], packed, scapegoat); - let llty_rec = Type::struct_(cx, lltys.as_slice(), packed); + let llty_rec = Type::struct_(cx, lltys[], packed); Struct { size: machine::llsize_of_alloc(cx, llty_rec), align: machine::llalign_of_min(cx, llty_rec), @@ -403,7 +403,7 @@ fn range_to_inttype(cx: &CrateContext, hint: Hint, bounds: &IntBounds) -> IntTyp return ity; } attr::ReprExtern => { - attempts = match cx.sess().target.target.arch.as_slice() { + attempts = match cx.sess().target.target.arch[] { // WARNING: the ARM EABI has two variants; the one corresponding to `at_least_32` // appears to be used on Linux and NetBSD, but some systems may use the variant // corresponding to `choose_shortest`. However, we don't run on those yet...? @@ -530,7 +530,7 @@ pub fn finish_type_of<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, match *r { CEnum(..) | General(..) | RawNullablePointer { .. } => { } Univariant(ref st, _) | StructWrappedNullablePointer { nonnull: ref st, .. } => - llty.set_struct_body(struct_llfields(cx, st, false, false).as_slice(), + llty.set_struct_body(struct_llfields(cx, st, false, false)[], st.packed) } } @@ -546,7 +546,7 @@ fn generic_type_of<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, Univariant(ref st, _) | StructWrappedNullablePointer { nonnull: ref st, .. } => { match name { None => { - Type::struct_(cx, struct_llfields(cx, st, sizing, dst).as_slice(), + Type::struct_(cx, struct_llfields(cx, st, sizing, dst)[], st.packed) } Some(name) => { assert_eq!(sizing, false); Type::named_struct(cx, name) } @@ -565,7 +565,7 @@ fn generic_type_of<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, // of the size. // // FIXME #10604: this breaks when vector types are present. - let (size, align) = union_size_and_align(sts.as_slice()); + let (size, align) = union_size_and_align(sts[]); let align_s = align as u64; let discr_ty = ll_inttype(cx, ity); let discr_size = machine::llsize_of_alloc(cx, discr_ty); @@ -586,10 +586,10 @@ fn generic_type_of<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, Type::array(&discr_ty, align_s / discr_size - 1), pad_ty); match name { - None => Type::struct_(cx, fields.as_slice(), false), + None => Type::struct_(cx, fields[], false), Some(name) => { let mut llty = Type::named_struct(cx, name); - llty.set_struct_body(fields.as_slice(), false); + llty.set_struct_body(fields[], false); llty } } @@ -847,7 +847,7 @@ pub fn struct_field_ptr<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, st: &Struct<'tcx>, v let val = if needs_cast { let ccx = bcx.ccx(); let fields = st.fields.iter().map(|&ty| type_of::type_of(ccx, ty)).collect::>(); - let real_ty = Type::struct_(ccx, fields.as_slice(), st.packed); + let real_ty = Type::struct_(ccx, fields[], st.packed); PointerCast(bcx, val, real_ty.ptr_to()) } else { val @@ -879,14 +879,14 @@ pub fn fold_variants<'blk, 'tcx, F>(bcx: Block<'blk, 'tcx>, for (discr, case) in cases.iter().enumerate() { let mut variant_cx = fcx.new_temp_block( - format!("enum-variant-iter-{}", discr.to_string()).as_slice() + format!("enum-variant-iter-{}", discr.to_string())[] ); let rhs_val = C_integral(ll_inttype(ccx, ity), discr as u64, true); AddCase(llswitch, rhs_val, variant_cx.llbb); let fields = case.fields.iter().map(|&ty| type_of::type_of(bcx.ccx(), ty)).collect::>(); - let real_ty = Type::struct_(ccx, fields.as_slice(), case.packed); + let real_ty = Type::struct_(ccx, fields[], case.packed); let variant_value = PointerCast(variant_cx, value, real_ty.ptr_to()); variant_cx = f(variant_cx, case, variant_value); @@ -961,14 +961,14 @@ pub fn trans_const<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, r: &Repr<'tcx>, discr let lldiscr = C_integral(ll_inttype(ccx, ity), discr as u64, true); let mut f = vec![lldiscr]; f.push_all(vals); - let mut contents = build_const_struct(ccx, case, f.as_slice()); + let mut contents = build_const_struct(ccx, case, f[]); contents.push_all(&[padding(ccx, max_sz - case.size)]); - C_struct(ccx, contents.as_slice(), false) + C_struct(ccx, contents[], false) } Univariant(ref st, _dro) => { assert!(discr == 0); let contents = build_const_struct(ccx, st, vals); - C_struct(ccx, contents.as_slice(), st.packed) + C_struct(ccx, contents[], st.packed) } RawNullablePointer { nndiscr, nnty, .. } => { if discr == nndiscr { @@ -982,7 +982,7 @@ pub fn trans_const<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, r: &Repr<'tcx>, discr if discr == nndiscr { C_struct(ccx, build_const_struct(ccx, nonnull, - vals).as_slice(), + vals)[], false) } else { let vals = nonnull.fields.iter().map(|&ty| { @@ -992,7 +992,7 @@ pub fn trans_const<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, r: &Repr<'tcx>, discr }).collect::>(); C_struct(ccx, build_const_struct(ccx, nonnull, - vals.as_slice()).as_slice(), + vals[])[], false) } } diff --git a/src/librustc_trans/trans/asm.rs b/src/librustc_trans/trans/asm.rs index e3afe22897e3..b8bee1000824 100644 --- a/src/librustc_trans/trans/asm.rs +++ b/src/librustc_trans/trans/asm.rs @@ -72,7 +72,7 @@ pub fn trans_inline_asm<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, ia: &ast::InlineAsm) callee::DontAutorefArg) }) }).collect::>(); - inputs.push_all(ext_inputs.as_slice()); + inputs.push_all(ext_inputs[]); // no failure occurred preparing operands, no need to cleanup fcx.pop_custom_cleanup_scope(temp_scope); @@ -92,18 +92,18 @@ pub fn trans_inline_asm<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, ia: &ast::InlineAsm) if !clobbers.is_empty() { clobbers.push(','); } - clobbers.push_str(more_clobbers.as_slice()); + clobbers.push_str(more_clobbers[]); } // Add the clobbers to our constraints list if clobbers.len() != 0 && constraints.len() != 0 { constraints.push(','); - constraints.push_str(clobbers.as_slice()); + constraints.push_str(clobbers[]); } else { - constraints.push_str(clobbers.as_slice()); + constraints.push_str(clobbers[]); } - debug!("Asm Constraints: {}", constraints.as_slice()); + debug!("Asm Constraints: {}", constraints[]); let num_outputs = outputs.len(); @@ -113,7 +113,7 @@ pub fn trans_inline_asm<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, ia: &ast::InlineAsm) } else if num_outputs == 1 { output_types[0] } else { - Type::struct_(bcx.ccx(), output_types.as_slice(), false) + Type::struct_(bcx.ccx(), output_types[], false) }; let dialect = match ia.dialect { @@ -126,7 +126,7 @@ pub fn trans_inline_asm<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, ia: &ast::InlineAsm) InlineAsmCall(bcx, a, c, - inputs.as_slice(), + inputs[], output_type, ia.volatile, ia.alignstack, diff --git a/src/librustc_trans/trans/base.rs b/src/librustc_trans/trans/base.rs index ca1e0d7de721..a18d403bd954 100644 --- a/src/librustc_trans/trans/base.rs +++ b/src/librustc_trans/trans/base.rs @@ -249,7 +249,7 @@ fn get_extern_rust_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, fn_ty: Ty<'tcx>, let f = decl_rust_fn(ccx, fn_ty, name); csearch::get_item_attrs(&ccx.sess().cstore, did, |attrs| { - set_llvm_fn_attrs(ccx, attrs.as_slice(), f) + set_llvm_fn_attrs(ccx, attrs[], f) }); ccx.externs().borrow_mut().insert(name.to_string(), f); @@ -302,7 +302,7 @@ pub fn decl_rust_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, _ => panic!("expected closure or fn") }; - let llfty = type_of_rust_fn(ccx, env, inputs.as_slice(), output, abi); + let llfty = type_of_rust_fn(ccx, env, inputs[], output, abi); debug!("decl_rust_fn(input count={},type={})", inputs.len(), ccx.tn().type_to_string(llfty)); @@ -369,7 +369,7 @@ fn require_alloc_fn<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, Err(s) => { bcx.sess().fatal(format!("allocation of `{}` {}", bcx.ty_to_string(info_ty), - s).as_slice()); + s)[]); } } } @@ -510,7 +510,7 @@ pub fn unset_split_stack(f: ValueRef) { // silently mangles such symbols, breaking our linkage model. pub fn note_unique_llvm_symbol(ccx: &CrateContext, sym: String) { if ccx.all_llvm_symbols().borrow().contains(&sym) { - ccx.sess().bug(format!("duplicate LLVM symbol: {}", sym).as_slice()); + ccx.sess().bug(format!("duplicate LLVM symbol: {}", sym)[]); } ccx.all_llvm_symbols().borrow_mut().insert(sym); } @@ -546,7 +546,7 @@ pub fn get_res_dtor<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, ty::mk_nil(ccx.tcx())); get_extern_fn(ccx, &mut *ccx.externs().borrow_mut(), - name.as_slice(), + name[], llvm::CCallConv, llty, dtor_ty) @@ -796,8 +796,8 @@ fn iter_variant<'a, 'blk, 'tcx>(cx: Block<'blk, 'tcx>, let variant_cx = fcx.new_temp_block( format!("enum-iter-variant-{}", - variant.disr_val.to_string().as_slice()) - .as_slice()); + variant.disr_val.to_string()[]) + []); match adt::trans_case(cx, &*repr, variant.disr_val) { _match::SingleResult(r) => { AddCase(llswitch, r.val, variant_cx.llbb) @@ -822,7 +822,7 @@ fn iter_variant<'a, 'blk, 'tcx>(cx: Block<'blk, 'tcx>, } _ => { cx.sess().unimpl(format!("type in iter_structural_ty: {}", - ty_to_string(cx.tcx(), t)).as_slice()) + ty_to_string(cx.tcx(), t))[]) } } return cx; @@ -904,7 +904,7 @@ pub fn fail_if_zero_or_overflows<'blk, 'tcx>( } _ => { cx.sess().bug(format!("fail-if-zero on unexpected type: {}", - ty_to_string(cx.tcx(), rhs_t)).as_slice()); + ty_to_string(cx.tcx(), rhs_t))[]); } }; let bcx = with_cond(cx, is_zero, |bcx| { @@ -958,19 +958,19 @@ pub fn trans_external_path<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, ty::ty_bare_fn(ref fn_ty) => { match ccx.sess().target.target.adjust_abi(fn_ty.abi) { Rust | RustCall => { - get_extern_rust_fn(ccx, t, name.as_slice(), did) + get_extern_rust_fn(ccx, t, name[], did) } RustIntrinsic => { ccx.sess().bug("unexpected intrinsic in trans_external_path") } _ => { foreign::register_foreign_item_fn(ccx, fn_ty.abi, t, - name.as_slice()) + name[]) } } } ty::ty_closure(_) => { - get_extern_rust_fn(ccx, t, name.as_slice(), did) + get_extern_rust_fn(ccx, t, name[], did) } _ => { get_extern_const(ccx, did, t) @@ -1024,7 +1024,7 @@ pub fn invoke<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, let llresult = Invoke(bcx, llfn, - llargs.as_slice(), + llargs[], normal_bcx.llbb, landing_pad, Some(attributes)); @@ -1040,7 +1040,7 @@ pub fn invoke<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, None => debuginfo::clear_source_location(bcx.fcx) }; - let llresult = Call(bcx, llfn, llargs.as_slice(), Some(attributes)); + let llresult = Call(bcx, llfn, llargs[], Some(attributes)); return (llresult, bcx); } } @@ -1157,7 +1157,7 @@ pub fn call_lifetime_end(cx: Block, ptr: ValueRef) { pub fn call_memcpy(cx: Block, dst: ValueRef, src: ValueRef, n_bytes: ValueRef, align: u32) { let _icx = push_ctxt("call_memcpy"); let ccx = cx.ccx(); - let key = match ccx.sess().target.target.target_word_size.as_slice() { + let key = match ccx.sess().target.target.target_word_size[] { "32" => "llvm.memcpy.p0i8.p0i8.i32", "64" => "llvm.memcpy.p0i8.p0i8.i64", tws => panic!("Unsupported target word size for memcpy: {}", tws), @@ -1204,7 +1204,7 @@ fn memzero<'a, 'tcx>(b: &Builder<'a, 'tcx>, llptr: ValueRef, ty: Ty<'tcx>) { let llty = type_of::type_of(ccx, ty); - let intrinsic_key = match ccx.sess().target.target.target_word_size.as_slice() { + let intrinsic_key = match ccx.sess().target.target.target_word_size[] { "32" => "llvm.memset.p0i8.i32", "64" => "llvm.memset.p0i8.i64", tws => panic!("Unsupported target word size for memset: {}", tws), @@ -1691,7 +1691,7 @@ fn copy_unboxed_closure_args_to_allocas<'blk, 'tcx>( "argtuple", arg_scope_id)); let untupled_arg_types = match monomorphized_arg_types[0].sty { - ty::ty_tup(ref types) => types.as_slice(), + ty::ty_tup(ref types) => types[], _ => { bcx.tcx().sess.span_bug(args[0].pat.span, "first arg to `rust-call` ABI function \ @@ -1879,12 +1879,12 @@ pub fn trans_closure<'a, 'b, 'tcx>(ccx: &CrateContext<'a, 'tcx>, let arg_datums = if abi != RustCall { create_datums_for_fn_args(&fcx, - monomorphized_arg_types.as_slice()) + monomorphized_arg_types[]) } else { create_datums_for_fn_args_under_call_abi( bcx, arg_scope, - monomorphized_arg_types.as_slice()) + monomorphized_arg_types[]) }; bcx = match closure_env.kind { @@ -1892,16 +1892,16 @@ pub fn trans_closure<'a, 'b, 'tcx>(ccx: &CrateContext<'a, 'tcx>, copy_args_to_allocas(&fcx, arg_scope, bcx, - decl.inputs.as_slice(), + decl.inputs[], arg_datums) } closure::UnboxedClosure(..) => { copy_unboxed_closure_args_to_allocas( bcx, arg_scope, - decl.inputs.as_slice(), + decl.inputs[], arg_datums, - monomorphized_arg_types.as_slice()) + monomorphized_arg_types[]) } }; @@ -2018,7 +2018,7 @@ pub fn trans_named_tuple_constructor<'blk, 'tcx>(mut bcx: Block<'blk, 'tcx>, _ => ccx.sess().bug( format!("trans_enum_variant_constructor: \ unexpected ctor return type {}", - ctor_ty.repr(tcx)).as_slice()) + ctor_ty.repr(tcx))[]) }; // Get location to store the result. If the user does not care about @@ -2041,7 +2041,7 @@ pub fn trans_named_tuple_constructor<'blk, 'tcx>(mut bcx: Block<'blk, 'tcx>, bcx = expr::trans_adt(bcx, result_ty, disr, - fields.as_slice(), + fields[], None, expr::SaveIn(llresult), call_info); @@ -2090,7 +2090,7 @@ fn trans_enum_variant_or_tuple_like_struct<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx _ => ccx.sess().bug( format!("trans_enum_variant_or_tuple_like_struct: \ unexpected ctor return type {}", - ty_to_string(ccx.tcx(), ctor_ty)).as_slice()) + ty_to_string(ccx.tcx(), ctor_ty))[]) }; let arena = TypedArena::new(); @@ -2102,7 +2102,7 @@ fn trans_enum_variant_or_tuple_like_struct<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx let arg_tys = ty::ty_fn_args(ctor_ty); - let arg_datums = create_datums_for_fn_args(&fcx, arg_tys.as_slice()); + let arg_datums = create_datums_for_fn_args(&fcx, arg_tys[]); if !type_is_zero_size(fcx.ccx, result_ty.unwrap()) { let dest = fcx.get_ret_slot(bcx, result_ty, "eret_slot"); @@ -2166,7 +2166,7 @@ fn enum_variant_size_lint(ccx: &CrateContext, enum_def: &ast::EnumDef, sp: Span, lvlsrc, Some(sp), format!("enum variant is more than three times larger \ ({} bytes) than the next largest (ignoring padding)", - largest).as_slice()); + largest)[]); ccx.sess().span_note(enum_def.variants[largest_index].span, "this variant is the largest"); @@ -2284,7 +2284,7 @@ pub fn trans_item(ccx: &CrateContext, item: &ast::Item) { match item.node { ast::ItemFn(ref decl, _fn_style, abi, ref generics, ref body) => { if !generics.is_type_parameterized() { - let trans_everywhere = attr::requests_inline(item.attrs.as_slice()); + let trans_everywhere = attr::requests_inline(item.attrs[]); // Ignore `trans_everywhere` for cross-crate inlined items // (`from_external`). `trans_item` will be called once for each // compilation unit that references the item, so it will still get @@ -2295,7 +2295,7 @@ pub fn trans_item(ccx: &CrateContext, item: &ast::Item) { foreign::trans_rust_fn_with_foreign_abi(ccx, &**decl, &**body, - item.attrs.as_slice(), + item.attrs[], llfn, &Substs::trans_empty(), item.id, @@ -2307,7 +2307,7 @@ pub fn trans_item(ccx: &CrateContext, item: &ast::Item) { llfn, &Substs::trans_empty(), item.id, - item.attrs.as_slice()); + item.attrs[]); } update_linkage(ccx, llfn, @@ -2324,7 +2324,7 @@ pub fn trans_item(ccx: &CrateContext, item: &ast::Item) { ast::ItemImpl(_, ref generics, _, _, ref impl_items) => { meth::trans_impl(ccx, item.ident, - impl_items.as_slice(), + impl_items[], generics, item.id); } @@ -2350,7 +2350,7 @@ pub fn trans_item(ccx: &CrateContext, item: &ast::Item) { // Do static_assert checking. It can't really be done much earlier // because we need to get the value of the bool out of LLVM - if attr::contains_name(item.attrs.as_slice(), "static_assert") { + if attr::contains_name(item.attrs[], "static_assert") { if m == ast::MutMutable { ccx.sess().span_fatal(expr.span, "cannot have static_assert on a mutable \ @@ -2427,7 +2427,7 @@ fn register_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, _ => panic!("expected bare rust fn") }; - let llfn = decl_rust_fn(ccx, node_type, sym.as_slice()); + let llfn = decl_rust_fn(ccx, node_type, sym[]); finish_register_fn(ccx, sp, sym, node_id, llfn); llfn } @@ -2472,7 +2472,7 @@ pub fn get_fn_llvm_attributes<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, fn_ty: Ty< match fn_sig.0.inputs[1].sty { ty::ty_tup(ref t_in) => { - inputs.push_all(t_in.as_slice()); + inputs.push_all(t_in[]); inputs } _ => ccx.sess().bug("expected tuple'd inputs") @@ -2607,7 +2607,7 @@ pub fn register_fn_llvmty(ccx: &CrateContext, llfty: Type) -> ValueRef { debug!("register_fn_llvmty id={} sym={}", node_id, sym); - let llfn = decl_fn(ccx, sym.as_slice(), cc, llfty, ty::FnConverging(ty::mk_nil(ccx.tcx()))); + let llfn = decl_fn(ccx, sym[], cc, llfty, ty::FnConverging(ty::mk_nil(ccx.tcx()))); finish_register_fn(ccx, sp, sym, node_id, llfn); llfn } @@ -2659,7 +2659,7 @@ fn create_entry_fn(ccx: &CrateContext, let (start_fn, args) = if use_start_lang_item { let start_def_id = match ccx.tcx().lang_items.require(StartFnLangItem) { Ok(id) => id, - Err(s) => { ccx.sess().fatal(s.as_slice()); } + Err(s) => { ccx.sess().fatal(s[]); } }; let start_fn = if start_def_id.krate == ast::LOCAL_CRATE { get_item_val(ccx, start_def_id.node) @@ -2750,7 +2750,7 @@ pub fn get_item_val(ccx: &CrateContext, id: ast::NodeId) -> ValueRef { let val = match item { ast_map::NodeItem(i) => { let ty = ty::node_id_to_type(ccx.tcx(), i.id); - let sym = || exported_name(ccx, id, ty, i.attrs.as_slice()); + let sym = || exported_name(ccx, id, ty, i.attrs[]); let v = match i.node { ast::ItemStatic(_, _, ref expr) => { @@ -2773,16 +2773,16 @@ pub fn get_item_val(ccx: &CrateContext, id: ast::NodeId) -> ValueRef { } else { llvm::LLVMTypeOf(v) }; - if contains_null(sym.as_slice()) { + if contains_null(sym[]) { ccx.sess().fatal( format!("Illegal null byte in export_name \ - value: `{}`", sym).as_slice()); + value: `{}`", sym)[]); } let g = sym.with_c_str(|buf| { llvm::LLVMAddGlobal(ccx.llmod(), llty, buf) }); - if attr::contains_name(i.attrs.as_slice(), + if attr::contains_name(i.attrs[], "thread_local") { llvm::set_thread_local(g, true); } @@ -2807,19 +2807,19 @@ pub fn get_item_val(ccx: &CrateContext, id: ast::NodeId) -> ValueRef { sym, i.id) }; - set_llvm_fn_attrs(ccx, i.attrs.as_slice(), llfn); + set_llvm_fn_attrs(ccx, i.attrs[], llfn); llfn } _ => panic!("get_item_val: weird result in table") }; - match attr::first_attr_value_str_by_name(i.attrs.as_slice(), + match attr::first_attr_value_str_by_name(i.attrs[], "link_section") { Some(sect) => { if contains_null(sect.get()) { ccx.sess().fatal(format!("Illegal null byte in link_section value: `{}`", - sect.get()).as_slice()); + sect.get())[]); } unsafe { sect.get().with_c_str(|buf| { @@ -2863,7 +2863,7 @@ pub fn get_item_val(ccx: &CrateContext, id: ast::NodeId) -> ValueRef { let abi = ccx.tcx().map.get_foreign_abi(id); let ty = ty::node_id_to_type(ccx.tcx(), ni.id); let name = foreign::link_name(&*ni); - foreign::register_foreign_item_fn(ccx, abi, ty, name.get().as_slice()) + foreign::register_foreign_item_fn(ccx, abi, ty, name.get()[]) } ast::ForeignItemStatic(..) => { foreign::register_static(ccx, &*ni) @@ -2886,7 +2886,7 @@ pub fn get_item_val(ccx: &CrateContext, id: ast::NodeId) -> ValueRef { let sym = exported_name(ccx, id, ty, - enm.attrs.as_slice()); + enm.attrs[]); llfn = match enm.node { ast::ItemEnum(_, _) => { @@ -2914,7 +2914,7 @@ pub fn get_item_val(ccx: &CrateContext, id: ast::NodeId) -> ValueRef { id, ty, struct_item.attrs - .as_slice()); + []); let llfn = register_fn(ccx, struct_item.span, sym, ctor_id, ty); set_inline_hint(llfn); @@ -2923,7 +2923,7 @@ pub fn get_item_val(ccx: &CrateContext, id: ast::NodeId) -> ValueRef { ref variant => { ccx.sess().bug(format!("get_item_val(): unexpected variant: {}", - variant).as_slice()) + variant)[]) } }; @@ -2944,10 +2944,10 @@ fn register_method(ccx: &CrateContext, id: ast::NodeId, m: &ast::Method) -> ValueRef { let mty = ty::node_id_to_type(ccx.tcx(), id); - let sym = exported_name(ccx, id, mty, m.attrs.as_slice()); + let sym = exported_name(ccx, id, mty, m.attrs[]); let llfn = register_fn(ccx, m.span, sym, id, mty); - set_llvm_fn_attrs(ccx, m.attrs.as_slice(), llfn); + set_llvm_fn_attrs(ccx, m.attrs[], llfn); llfn } @@ -2986,7 +2986,7 @@ pub fn write_metadata(cx: &SharedCrateContext, krate: &ast::Crate) -> Vec { Some(compressed) => compressed, None => cx.sess().fatal("failed to compress metadata"), }.as_slice()); - let llmeta = C_bytes_in_context(cx.metadata_llcx(), compressed.as_slice()); + let llmeta = C_bytes_in_context(cx.metadata_llcx(), compressed[]); let llconst = C_struct_in_context(cx.metadata_llcx(), &[llmeta], false); let name = format!("rust_metadata_{}_{}", cx.link_meta().crate_name, @@ -3114,7 +3114,7 @@ pub fn trans_crate<'tcx>(analysis: ty::CrateAnalysis<'tcx>) let link_meta = link::build_link_meta(&tcx.sess, krate, name); let codegen_units = tcx.sess.opts.cg.codegen_units; - let shared_ccx = SharedCrateContext::new(link_meta.crate_name.as_slice(), + let shared_ccx = SharedCrateContext::new(link_meta.crate_name[], codegen_units, tcx, export_map, @@ -3216,7 +3216,7 @@ pub fn trans_crate<'tcx>(analysis: ty::CrateAnalysis<'tcx>) llmod: shared_ccx.metadata_llmod(), }; let formats = shared_ccx.tcx().dependency_formats.borrow().clone(); - let no_builtins = attr::contains_name(krate.attrs.as_slice(), "no_builtins"); + let no_builtins = attr::contains_name(krate.attrs[], "no_builtins"); let translation = CrateTranslation { modules: modules, diff --git a/src/librustc_trans/trans/builder.rs b/src/librustc_trans/trans/builder.rs index cf940b138467..1b9c9d221b90 100644 --- a/src/librustc_trans/trans/builder.rs +++ b/src/librustc_trans/trans/builder.rs @@ -555,7 +555,7 @@ pub fn gepi(&self, base: ValueRef, ixs: &[uint]) -> ValueRef { } else { let v = ixs.iter().map(|i| C_i32(self.ccx, *i as i32)).collect::>(); self.count_insn("gepi"); - self.inbounds_gep(base, v.as_slice()) + self.inbounds_gep(base, v[]) } } @@ -763,8 +763,8 @@ pub fn add_span_comment(&self, sp: Span, text: &str) { let s = format!("{} ({})", text, self.ccx.sess().codemap().span_to_string(sp)); - debug!("{}", s.as_slice()); - self.add_comment(s.as_slice()); + debug!("{}", s[]); + self.add_comment(s[]); } } @@ -801,7 +801,7 @@ pub fn inline_asm_call(&self, asm: *const c_char, cons: *const c_char, }).collect::>(); debug!("Asm Output Type: {}", self.ccx.tn().type_to_string(output)); - let fty = Type::func(argtys.as_slice(), &output); + let fty = Type::func(argtys[], &output); unsafe { let v = llvm::LLVMInlineAsm( fty.to_ref(), asm, cons, volatile, alignstack, dia as c_uint); diff --git a/src/librustc_trans/trans/cabi.rs b/src/librustc_trans/trans/cabi.rs index ad2a6db1222c..9ea158fbe210 100644 --- a/src/librustc_trans/trans/cabi.rs +++ b/src/librustc_trans/trans/cabi.rs @@ -107,7 +107,7 @@ pub fn compute_abi_info(ccx: &CrateContext, atys: &[Type], rty: Type, ret_def: bool) -> FnType { - match ccx.sess().target.target.arch.as_slice() { + match ccx.sess().target.target.arch[] { "x86" => cabi_x86::compute_abi_info(ccx, atys, rty, ret_def), "x86_64" => if ccx.sess().target.target.options.is_like_windows { cabi_x86_win64::compute_abi_info(ccx, atys, rty, ret_def) @@ -117,6 +117,6 @@ pub fn compute_abi_info(ccx: &CrateContext, "arm" => cabi_arm::compute_abi_info(ccx, atys, rty, ret_def), "mips" => cabi_mips::compute_abi_info(ccx, atys, rty, ret_def), a => ccx.sess().fatal((format!("unrecognized arch \"{}\" in target specification", a)) - .as_slice()), + []), } } diff --git a/src/librustc_trans/trans/callee.rs b/src/librustc_trans/trans/callee.rs index 1a753901f7ea..ec3a81afaa0e 100644 --- a/src/librustc_trans/trans/callee.rs +++ b/src/librustc_trans/trans/callee.rs @@ -122,7 +122,7 @@ fn datum_callee<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, expr: &ast::Expr) expr.span, format!("type of callee is neither bare-fn nor closure: \ {}", - bcx.ty_to_string(datum.ty)).as_slice()); + bcx.ty_to_string(datum.ty))[]); } } } @@ -208,7 +208,7 @@ fn trans_def<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, bcx.tcx().sess.span_bug( ref_expr.span, format!("cannot translate def {} \ - to a callable thing!", def).as_slice()); + to a callable thing!", def)[]); } } } @@ -288,7 +288,7 @@ pub fn trans_fn_pointer_shim<'a, 'tcx>( _ => { tcx.sess.bug(format!("trans_fn_pointer_shim invoked on invalid type: {}", - bare_fn_ty.repr(tcx)).as_slice()); + bare_fn_ty.repr(tcx))[]); } }; let tuple_input_ty = ty::mk_tup(tcx, input_tys.to_vec()); @@ -310,7 +310,7 @@ pub fn trans_fn_pointer_shim<'a, 'tcx>( let llfn = decl_internal_rust_fn(ccx, tuple_fn_ty, - function_name.as_slice()); + function_name[]); // let block_arena = TypedArena::new(); @@ -345,7 +345,7 @@ pub fn trans_fn_pointer_shim<'a, 'tcx>( None, bare_fn_ty, |bcx, _| Callee { bcx: bcx, data: Fn(llfnpointer) }, - ArgVals(llargs.as_slice()), + ArgVals(llargs[]), dest).bcx; finish_fn(&fcx, bcx, output_ty); @@ -813,7 +813,7 @@ pub fn trans_call_inner<'a, 'blk, 'tcx, F>(bcx: Block<'blk, 'tcx>, bcx = foreign::trans_native_call(bcx, callee_ty, llfn, opt_llretslot.unwrap(), - llargs.as_slice(), arg_tys); + llargs[], arg_tys); } fcx.pop_and_trans_custom_cleanup_scope(bcx, arg_cleanup_scope); diff --git a/src/librustc_trans/trans/cleanup.rs b/src/librustc_trans/trans/cleanup.rs index fb2c432ef5cf..c1bb21c496ad 100644 --- a/src/librustc_trans/trans/cleanup.rs +++ b/src/librustc_trans/trans/cleanup.rs @@ -404,7 +404,7 @@ fn schedule_clean_in_ast_scope(&self, self.ccx.sess().bug( format!("no cleanup scope {} found", - self.ccx.tcx().map.node_to_string(cleanup_scope)).as_slice()); + self.ccx.tcx().map.node_to_string(cleanup_scope))[]); } /// Schedules a cleanup to occur in the top-most scope, which must be a temporary scope. @@ -586,7 +586,7 @@ fn trans_cleanups_to_exit_scope(&'blk self, LoopExit(id, _) => { self.ccx.sess().bug(format!( "cannot exit from scope {}, \ - not in scope", id).as_slice()); + not in scope", id)[]); } } } @@ -655,7 +655,7 @@ fn trans_cleanups_to_exit_scope(&'blk self, let name = scope.block_name("clean"); debug!("generating cleanups for {}", name); let bcx_in = self.new_block(label.is_unwind(), - name.as_slice(), + name[], None); let mut bcx_out = bcx_in; for cleanup in scope.cleanups.iter().rev() { @@ -702,7 +702,7 @@ fn get_or_create_landing_pad(&'blk self) -> BasicBlockRef { Some(llbb) => { return llbb; } None => { let name = last_scope.block_name("unwind"); - pad_bcx = self.new_block(true, name.as_slice(), None); + pad_bcx = self.new_block(true, name[], None); last_scope.cached_landing_pad = Some(pad_bcx.llbb); } } @@ -1020,7 +1020,7 @@ pub fn temporary_scope(tcx: &ty::ctxt, } None => { tcx.sess.bug(format!("no temporary scope available for expr {}", - id).as_slice()) + id)[]) } } } diff --git a/src/librustc_trans/trans/closure.rs b/src/librustc_trans/trans/closure.rs index d5d954f5a907..8e56ef3c6f39 100644 --- a/src/librustc_trans/trans/closure.rs +++ b/src/librustc_trans/trans/closure.rs @@ -177,7 +177,7 @@ pub fn store_environment<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, let tcx = ccx.tcx(); // compute the type of the closure - let cdata_ty = mk_closure_tys(tcx, bound_values.as_slice()); + let cdata_ty = mk_closure_tys(tcx, bound_values[]); // cbox_ty has the form of a tuple: (a, b, c) we want a ptr to a // tuple. This could be a ptr in uniq or a box or on stack, @@ -206,7 +206,7 @@ pub fn store_environment<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, if ccx.sess().asm_comments() { add_comment(bcx, format!("Copy {} into closure", - bv.to_string(ccx)).as_slice()); + bv.to_string(ccx))[]); } let bound_data = GEPi(bcx, llbox, &[0u, abi::BOX_FIELD_BODY, i]); @@ -444,7 +444,7 @@ pub fn trans_expr_fn<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, let s = tcx.map.with_path(id, |path| { mangle_internal_name_by_path_and_seq(path, "closure") }); - let llfn = decl_internal_rust_fn(ccx, fty, s.as_slice()); + let llfn = decl_internal_rust_fn(ccx, fty, s[]); // set an inline hint for all closures set_inline_hint(llfn); @@ -468,7 +468,7 @@ pub fn trans_expr_fn<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, &[], ty::ty_fn_ret(fty), ty::ty_fn_abi(fty), - ClosureEnv::new(freevars.as_slice(), + ClosureEnv::new(freevars[], BoxedClosure(cdata_ty, store))); fill_fn_pair(bcx, dest_addr, llfn, llbox); bcx @@ -514,7 +514,7 @@ pub fn get_or_create_declaration_if_unboxed_closure<'blk, 'tcx>(bcx: Block<'blk, mangle_internal_name_by_path_and_seq(path, "unboxed_closure") }); - let llfn = decl_internal_rust_fn(ccx, function_type, symbol.as_slice()); + let llfn = decl_internal_rust_fn(ccx, function_type, symbol[]); // set an inline hint for all closures set_inline_hint(llfn); @@ -563,7 +563,7 @@ pub fn trans_unboxed_closure<'blk, 'tcx>( &[], ty::ty_fn_ret(function_type), ty::ty_fn_abi(function_type), - ClosureEnv::new(freevars.as_slice(), + ClosureEnv::new(freevars[], UnboxedClosure(freevar_mode))); // Don't hoist this to the top of the function. It's perfectly legitimate @@ -614,7 +614,7 @@ pub fn get_wrapper_for_bare_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, ccx.sess().bug(format!("get_wrapper_for_bare_fn: \ expected a statically resolved fn, got \ {}", - def).as_slice()); + def)[]); } }; @@ -632,7 +632,7 @@ pub fn get_wrapper_for_bare_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, _ => { ccx.sess().bug(format!("get_wrapper_for_bare_fn: \ expected a closure ty, got {}", - closure_ty.repr(tcx)).as_slice()); + closure_ty.repr(tcx))[]); } }; @@ -640,9 +640,9 @@ pub fn get_wrapper_for_bare_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, mangle_internal_name_by_path_and_seq(path, "as_closure") }); let llfn = if is_local { - decl_internal_rust_fn(ccx, closure_ty, name.as_slice()) + decl_internal_rust_fn(ccx, closure_ty, name[]) } else { - decl_rust_fn(ccx, closure_ty, name.as_slice()) + decl_rust_fn(ccx, closure_ty, name[]) }; ccx.closure_bare_wrapper_cache().borrow_mut().insert(fn_ptr, llfn); @@ -663,7 +663,7 @@ pub fn get_wrapper_for_bare_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, let args = create_datums_for_fn_args(&fcx, ty::ty_fn_args(closure_ty) - .as_slice()); + []); let mut llargs = Vec::new(); match fcx.llretslotptr.get() { Some(llretptr) => { diff --git a/src/librustc_trans/trans/common.rs b/src/librustc_trans/trans/common.rs index 61f27bcfa7ad..9a3e39ff10b3 100644 --- a/src/librustc_trans/trans/common.rs +++ b/src/librustc_trans/trans/common.rs @@ -117,7 +117,7 @@ pub fn gensym_name(name: &str) -> PathElem { let num = token::gensym(name).uint(); // use one colon which will get translated to a period by the mangler, and // we're guaranteed that `num` is globally unique for this crate. - PathName(token::gensym(format!("{}:{}", name, num).as_slice())) + PathName(token::gensym(format!("{}:{}", name, num)[])) } #[deriving(Copy)] @@ -436,7 +436,7 @@ pub fn def(&self, nid: ast::NodeId) -> def::Def { Some(v) => v.clone(), None => { self.tcx().sess.bug(format!( - "no def associated with node id {}", nid).as_slice()); + "no def associated with node id {}", nid)[]); } } } @@ -817,7 +817,7 @@ pub fn fulfill_obligation<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, span, format!("Encountered error `{}` selecting `{}` during trans", e.repr(tcx), - trait_ref.repr(tcx)).as_slice()) + trait_ref.repr(tcx))[]) } }; @@ -844,7 +844,7 @@ pub fn fulfill_obligation<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, span, format!("Encountered errors `{}` fulfilling `{}` during trans", errors.repr(tcx), - trait_ref.repr(tcx)).as_slice()); + trait_ref.repr(tcx))[]); } } } @@ -892,7 +892,7 @@ pub fn node_id_substs<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, format!("type parameters for node {} include inference types: \ {}", node, - substs.repr(bcx.tcx())).as_slice()); + substs.repr(bcx.tcx()))[]); } let substs = substs.erase_regions(); @@ -909,8 +909,8 @@ pub fn langcall(bcx: Block, Err(s) => { let msg = format!("{} {}", msg, s); match span { - Some(span) => bcx.tcx().sess.span_fatal(span, msg.as_slice()), - None => bcx.tcx().sess.fatal(msg.as_slice()), + Some(span) => bcx.tcx().sess.span_fatal(span, msg[]), + None => bcx.tcx().sess.fatal(msg[]), } } } diff --git a/src/librustc_trans/trans/consts.rs b/src/librustc_trans/trans/consts.rs index e4f0543b5e70..4f7d0f8fe754 100644 --- a/src/librustc_trans/trans/consts.rs +++ b/src/librustc_trans/trans/consts.rs @@ -54,7 +54,7 @@ pub fn const_lit(cx: &CrateContext, e: &ast::Expr, lit: &ast::Lit) _ => cx.sess().span_bug(lit.span, format!("integer literal has type {} (expected int \ or uint)", - ty_to_string(cx.tcx(), lit_int_ty)).as_slice()) + ty_to_string(cx.tcx(), lit_int_ty))[]) } } ast::LitFloat(ref fs, t) => { @@ -74,7 +74,7 @@ pub fn const_lit(cx: &CrateContext, e: &ast::Expr, lit: &ast::Lit) } ast::LitBool(b) => C_bool(cx, b), ast::LitStr(ref s, _) => C_str_slice(cx, (*s).clone()), - ast::LitBinary(ref data) => C_binary_slice(cx, data.as_slice()), + ast::LitBinary(ref data) => C_binary_slice(cx, data[]), } } @@ -95,9 +95,9 @@ fn const_vec(cx: &CrateContext, e: &ast::Expr, .collect::>(); // If the vector contains enums, an LLVM array won't work. let v = if vs.iter().any(|vi| val_ty(*vi) != llunitty) { - C_struct(cx, vs.as_slice(), false) + C_struct(cx, vs[], false) } else { - C_array(llunitty, vs.as_slice()) + C_array(llunitty, vs[]) }; (v, llunitty) } @@ -152,13 +152,13 @@ fn const_deref<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, v: ValueRef, } _ => { cx.sess().bug(format!("unexpected dereferenceable type {}", - ty_to_string(cx.tcx(), t)).as_slice()) + ty_to_string(cx.tcx(), t))[]) } } } None => { cx.sess().bug(format!("cannot dereference const of type {}", - ty_to_string(cx.tcx(), t)).as_slice()) + ty_to_string(cx.tcx(), t))[]) } } } @@ -203,7 +203,7 @@ pub fn const_expr<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, e: &ast::Expr) cx.sess() .span_bug(e.span, format!("unexpected static function: {}", - store).as_slice()) + store)[]) } ty::AdjustDerefRef(ref adj) => { let mut ty = ety; @@ -264,7 +264,7 @@ pub fn const_expr<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, e: &ast::Expr) } _ => cx.sess().span_bug(e.span, format!("unimplemented type in const unsize: {}", - ty_to_string(cx.tcx(), ty)).as_slice()) + ty_to_string(cx.tcx(), ty))[]) } } _ => { @@ -272,7 +272,7 @@ pub fn const_expr<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, e: &ast::Expr) .span_bug(e.span, format!("unimplemented const \ autoref {}", - autoref).as_slice()) + autoref)[]) } } } @@ -293,7 +293,7 @@ pub fn const_expr<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, e: &ast::Expr) } cx.sess().bug(format!("const {} of type {} has size {} instead of {}", e.repr(cx.tcx()), ty_to_string(cx.tcx(), ety), - csize, tsize).as_slice()); + csize, tsize)[]); } (llconst, ety_adjusted) } @@ -443,7 +443,7 @@ fn const_expr_unadjusted(cx: &CrateContext, e: &ast::Expr) -> ValueRef { _ => cx.sess().span_bug(base.span, format!("index-expr base must be a vector \ or string type, found {}", - ty_to_string(cx.tcx(), bt)).as_slice()) + ty_to_string(cx.tcx(), bt))[]) }, ty::ty_rptr(_, mt) => match mt.ty.sty { ty::ty_vec(_, Some(u)) => { @@ -452,12 +452,12 @@ fn const_expr_unadjusted(cx: &CrateContext, e: &ast::Expr) -> ValueRef { _ => cx.sess().span_bug(base.span, format!("index-expr base must be a vector \ or string type, found {}", - ty_to_string(cx.tcx(), bt)).as_slice()) + ty_to_string(cx.tcx(), bt))[]) }, _ => cx.sess().span_bug(base.span, format!("index-expr base must be a vector \ or string type, found {}", - ty_to_string(cx.tcx(), bt)).as_slice()) + ty_to_string(cx.tcx(), bt))[]) }; let len = llvm::LLVMConstIntGetZExtValue(len) as u64; @@ -558,8 +558,8 @@ fn const_expr_unadjusted(cx: &CrateContext, e: &ast::Expr) -> ValueRef { ast::ExprTup(ref es) => { let ety = ty::expr_ty(cx.tcx(), e); let repr = adt::represent_type(cx, ety); - let vals = map_list(es.as_slice()); - adt::trans_const(cx, &*repr, 0, vals.as_slice()) + let vals = map_list(es[]); + adt::trans_const(cx, &*repr, 0, vals[]) } ast::ExprStruct(_, ref fs, ref base_opt) => { let ety = ty::expr_ty(cx.tcx(), e); @@ -590,7 +590,7 @@ fn const_expr_unadjusted(cx: &CrateContext, e: &ast::Expr) -> ValueRef { } } }).collect::>(); - adt::trans_const(cx, &*repr, discr, cs.as_slice()) + adt::trans_const(cx, &*repr, discr, cs[]) }) } ast::ExprVec(ref es) => { @@ -607,9 +607,9 @@ fn const_expr_unadjusted(cx: &CrateContext, e: &ast::Expr) -> ValueRef { }; let vs = Vec::from_elem(n, const_expr(cx, &**elem).0); if vs.iter().any(|vi| val_ty(*vi) != llunitty) { - C_struct(cx, vs.as_slice(), false) + C_struct(cx, vs[], false) } else { - C_array(llunitty, vs.as_slice()) + C_array(llunitty, vs[]) } } ast::ExprPath(ref pth) => { @@ -655,8 +655,8 @@ fn const_expr_unadjusted(cx: &CrateContext, e: &ast::Expr) -> ValueRef { Some(def::DefStruct(_)) => { let ety = ty::expr_ty(cx.tcx(), e); let repr = adt::represent_type(cx, ety); - let arg_vals = map_list(args.as_slice()); - adt::trans_const(cx, &*repr, 0, arg_vals.as_slice()) + let arg_vals = map_list(args[]); + adt::trans_const(cx, &*repr, 0, arg_vals[]) } Some(def::DefVariant(enum_did, variant_did, _)) => { let ety = ty::expr_ty(cx.tcx(), e); @@ -664,11 +664,11 @@ fn const_expr_unadjusted(cx: &CrateContext, e: &ast::Expr) -> ValueRef { let vinfo = ty::enum_variant_with_id(cx.tcx(), enum_did, variant_did); - let arg_vals = map_list(args.as_slice()); + let arg_vals = map_list(args[]); adt::trans_const(cx, &*repr, vinfo.disr_val, - arg_vals.as_slice()) + arg_vals[]) } _ => cx.sess().span_bug(e.span, "expected a struct or variant def") } diff --git a/src/librustc_trans/trans/context.rs b/src/librustc_trans/trans/context.rs index 7b962a939906..2c71dd831fbc 100644 --- a/src/librustc_trans/trans/context.rs +++ b/src/librustc_trans/trans/context.rs @@ -284,7 +284,7 @@ pub fn new(crate_name: &str, // such as a function name in the module. // 1. http://llvm.org/bugs/show_bug.cgi?id=11479 let llmod_id = format!("{}.{}.rs", crate_name, i); - let local_ccx = LocalCrateContext::new(&shared_ccx, llmod_id.as_slice()); + let local_ccx = LocalCrateContext::new(&shared_ccx, llmod_id[]); shared_ccx.local_ccxs.push(local_ccx); } @@ -374,7 +374,7 @@ fn new(shared: &SharedCrateContext<'tcx>, .target .target .data_layout - .as_slice()); + []); let dbg_cx = if shared.tcx.sess.opts.debuginfo != NoDebugInfo { Some(debuginfo::CrateDebugContext::new(llmod)) @@ -726,7 +726,7 @@ pub fn obj_size_bound(&self) -> u64 { pub fn report_overbig_object(&self, obj: Ty<'tcx>) -> ! { self.sess().fatal( format!("the type `{}` is too big for the current architecture", - obj.repr(self.tcx())).as_slice()) + obj.repr(self.tcx()))[]) } } diff --git a/src/librustc_trans/trans/controlflow.rs b/src/librustc_trans/trans/controlflow.rs index 135e192a2fd4..3b24ded6717c 100644 --- a/src/librustc_trans/trans/controlflow.rs +++ b/src/librustc_trans/trans/controlflow.rs @@ -48,7 +48,7 @@ pub fn trans_stmt<'blk, 'tcx>(cx: Block<'blk, 'tcx>, debug!("trans_stmt({})", s.repr(cx.tcx())); if cx.sess().asm_comments() { - add_span_comment(cx, s.span, s.repr(cx.tcx()).as_slice()); + add_span_comment(cx, s.span, s.repr(cx.tcx())[]); } let mut bcx = cx; @@ -188,7 +188,7 @@ pub fn trans_if<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, } let name = format!("then-block-{}-", thn.id); - let then_bcx_in = bcx.fcx.new_id_block(name.as_slice(), thn.id); + let then_bcx_in = bcx.fcx.new_id_block(name[], thn.id); let then_bcx_out = trans_block(then_bcx_in, &*thn, dest); trans::debuginfo::clear_source_location(bcx.fcx); @@ -437,7 +437,7 @@ pub fn trans_break_cont<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, Some(&def::DefLabel(loop_id)) => loop_id, ref r => { bcx.tcx().sess.bug(format!("{} in def-map for label", - r).as_slice()) + r)[]) } } } @@ -501,7 +501,7 @@ pub fn trans_fail<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, let v_str = C_str_slice(ccx, fail_str); let loc = bcx.sess().codemap().lookup_char_pos(sp.lo); - let filename = token::intern_and_get_ident(loc.file.name.as_slice()); + let filename = token::intern_and_get_ident(loc.file.name[]); let filename = C_str_slice(ccx, filename); let line = C_uint(ccx, loc.line); let expr_file_line_const = C_struct(ccx, &[v_str, filename, line], false); @@ -510,7 +510,7 @@ pub fn trans_fail<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, let did = langcall(bcx, Some(sp), "", PanicFnLangItem); let bcx = callee::trans_lang_call(bcx, did, - args.as_slice(), + args[], Some(expr::Ignore)).bcx; Unreachable(bcx); return bcx; @@ -526,7 +526,7 @@ pub fn trans_fail_bounds_check<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, // Extract the file/line from the span let loc = bcx.sess().codemap().lookup_char_pos(sp.lo); - let filename = token::intern_and_get_ident(loc.file.name.as_slice()); + let filename = token::intern_and_get_ident(loc.file.name[]); // Invoke the lang item let filename = C_str_slice(ccx, filename); @@ -537,7 +537,7 @@ pub fn trans_fail_bounds_check<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, let did = langcall(bcx, Some(sp), "", PanicBoundsCheckFnLangItem); let bcx = callee::trans_lang_call(bcx, did, - args.as_slice(), + args[], Some(expr::Ignore)).bcx; Unreachable(bcx); return bcx; diff --git a/src/librustc_trans/trans/datum.rs b/src/librustc_trans/trans/datum.rs index 75473dc58bf4..9ab4e92b5113 100644 --- a/src/librustc_trans/trans/datum.rs +++ b/src/librustc_trans/trans/datum.rs @@ -463,7 +463,7 @@ pub fn get_element<'blk, F>(&self, bcx: Block<'blk, 'tcx>, ty: Ty<'tcx>, } _ => bcx.tcx().sess.bug( format!("Unexpected unsized type in get_element: {}", - bcx.ty_to_string(self.ty)).as_slice()) + bcx.ty_to_string(self.ty))[]) }; Datum { val: val, diff --git a/src/librustc_trans/trans/debuginfo.rs b/src/librustc_trans/trans/debuginfo.rs index 51e3a83f81f5..2545de34ed88 100644 --- a/src/librustc_trans/trans/debuginfo.rs +++ b/src/librustc_trans/trans/debuginfo.rs @@ -284,7 +284,7 @@ fn register_type_with_metadata<'a>(&mut self, metadata: DIType) { if self.type_to_metadata.insert(type_, metadata).is_some() { cx.sess().bug(format!("Type metadata for Ty '{}' is already in the TypeMap!", - ppaux::ty_to_string(cx.tcx(), type_)).as_slice()); + ppaux::ty_to_string(cx.tcx(), type_))[]); } } @@ -297,7 +297,7 @@ fn register_unique_id_with_metadata(&mut self, if self.unique_id_to_metadata.insert(unique_type_id, metadata).is_some() { let unique_type_id_str = self.get_unique_type_id_as_string(unique_type_id); cx.sess().bug(format!("Type metadata for unique id '{}' is already in the TypeMap!", - unique_type_id_str.as_slice()).as_slice()); + unique_type_id_str[])[]); } } @@ -378,14 +378,14 @@ fn get_unique_type_id_of_type<'a>(&mut self, cx: &CrateContext<'a, 'tcx>, self.get_unique_type_id_of_type(cx, component_type); let component_type_id = self.get_unique_type_id_as_string(component_type_id); - unique_type_id.push_str(component_type_id.as_slice()); + unique_type_id.push_str(component_type_id[]); } }, ty::ty_uniq(inner_type) => { unique_type_id.push('~'); let inner_type_id = self.get_unique_type_id_of_type(cx, inner_type); let inner_type_id = self.get_unique_type_id_as_string(inner_type_id); - unique_type_id.push_str(inner_type_id.as_slice()); + unique_type_id.push_str(inner_type_id[]); }, ty::ty_ptr(ty::mt { ty: inner_type, mutbl } ) => { unique_type_id.push('*'); @@ -395,7 +395,7 @@ fn get_unique_type_id_of_type<'a>(&mut self, cx: &CrateContext<'a, 'tcx>, let inner_type_id = self.get_unique_type_id_of_type(cx, inner_type); let inner_type_id = self.get_unique_type_id_as_string(inner_type_id); - unique_type_id.push_str(inner_type_id.as_slice()); + unique_type_id.push_str(inner_type_id[]); }, ty::ty_rptr(_, ty::mt { ty: inner_type, mutbl }) => { unique_type_id.push('&'); @@ -405,12 +405,12 @@ fn get_unique_type_id_of_type<'a>(&mut self, cx: &CrateContext<'a, 'tcx>, let inner_type_id = self.get_unique_type_id_of_type(cx, inner_type); let inner_type_id = self.get_unique_type_id_as_string(inner_type_id); - unique_type_id.push_str(inner_type_id.as_slice()); + unique_type_id.push_str(inner_type_id[]); }, ty::ty_vec(inner_type, optional_length) => { match optional_length { Some(len) => { - unique_type_id.push_str(format!("[{}]", len).as_slice()); + unique_type_id.push_str(format!("[{}]", len)[]); } None => { unique_type_id.push_str("[]"); @@ -419,7 +419,7 @@ fn get_unique_type_id_of_type<'a>(&mut self, cx: &CrateContext<'a, 'tcx>, let inner_type_id = self.get_unique_type_id_of_type(cx, inner_type); let inner_type_id = self.get_unique_type_id_as_string(inner_type_id); - unique_type_id.push_str(inner_type_id.as_slice()); + unique_type_id.push_str(inner_type_id[]); }, ty::ty_trait(ref trait_data) => { unique_type_id.push_str("trait "); @@ -444,7 +444,7 @@ fn get_unique_type_id_of_type<'a>(&mut self, cx: &CrateContext<'a, 'tcx>, self.get_unique_type_id_of_type(cx, parameter_type); let parameter_type_id = self.get_unique_type_id_as_string(parameter_type_id); - unique_type_id.push_str(parameter_type_id.as_slice()); + unique_type_id.push_str(parameter_type_id[]); unique_type_id.push(','); } @@ -457,7 +457,7 @@ fn get_unique_type_id_of_type<'a>(&mut self, cx: &CrateContext<'a, 'tcx>, ty::FnConverging(ret_ty) => { let return_type_id = self.get_unique_type_id_of_type(cx, ret_ty); let return_type_id = self.get_unique_type_id_as_string(return_type_id); - unique_type_id.push_str(return_type_id.as_slice()); + unique_type_id.push_str(return_type_id[]); } ty::FnDiverging => { unique_type_id.push_str("!"); @@ -478,8 +478,8 @@ fn get_unique_type_id_of_type<'a>(&mut self, cx: &CrateContext<'a, 'tcx>, }, _ => { cx.sess().bug(format!("get_unique_type_id_of_type() - unexpected type: {}, {}", - ppaux::ty_to_string(cx.tcx(), type_).as_slice(), - type_.sty).as_slice()) + ppaux::ty_to_string(cx.tcx(), type_)[], + type_.sty)[]) } }; @@ -522,7 +522,7 @@ fn from_def_id_and_substs<'a, 'tcx>(type_map: &mut TypeMap<'tcx>, output.push_str(crate_hash.as_str()); output.push_str("/"); - output.push_str(format!("{:x}", def_id.node).as_slice()); + output.push_str(format!("{:x}", def_id.node)[]); // Maybe check that there is no self type here. @@ -535,7 +535,7 @@ fn from_def_id_and_substs<'a, 'tcx>(type_map: &mut TypeMap<'tcx>, type_map.get_unique_type_id_of_type(cx, type_parameter); let param_type_id = type_map.get_unique_type_id_as_string(param_type_id); - output.push_str(param_type_id.as_slice()); + output.push_str(param_type_id[]); output.push(','); } @@ -577,7 +577,7 @@ fn get_unique_type_id_of_closure_type<'a>(&mut self, self.get_unique_type_id_of_type(cx, parameter_type); let parameter_type_id = self.get_unique_type_id_as_string(parameter_type_id); - unique_type_id.push_str(parameter_type_id.as_slice()); + unique_type_id.push_str(parameter_type_id[]); unique_type_id.push(','); } @@ -591,7 +591,7 @@ fn get_unique_type_id_of_closure_type<'a>(&mut self, ty::FnConverging(ret_ty) => { let return_type_id = self.get_unique_type_id_of_type(cx, ret_ty); let return_type_id = self.get_unique_type_id_as_string(return_type_id); - unique_type_id.push_str(return_type_id.as_slice()); + unique_type_id.push_str(return_type_id[]); } ty::FnDiverging => { unique_type_id.push_str("!"); @@ -622,7 +622,7 @@ fn get_unique_type_id_of_enum_variant<'a>(&mut self, let enum_type_id = self.get_unique_type_id_of_type(cx, enum_type); let enum_variant_type_id = format!("{}::{}", self.get_unique_type_id_as_string(enum_type_id) - .as_slice(), + [], variant_name); let interner_key = self.unique_id_interner.intern(Rc::new(enum_variant_type_id)); UniqueTypeId(interner_key) @@ -793,19 +793,19 @@ pub fn create_global_var_metadata(cx: &CrateContext, create_global_var_metadata() - Captured var-id refers to \ unexpected ast_item variant: {}", - var_item).as_slice()) + var_item)[]) } } }, _ => cx.sess().bug(format!("debuginfo::create_global_var_metadata() \ - Captured var-id refers to unexpected \ ast_map variant: {}", - var_item).as_slice()) + var_item)[]) }; let (file_metadata, line_number) = if span != codemap::DUMMY_SP { let loc = span_start(cx, span); - (file_metadata(cx, loc.file.name.as_slice()), loc.line as c_uint) + (file_metadata(cx, loc.file.name[]), loc.line as c_uint) } else { (UNKNOWN_FILE_METADATA, UNKNOWN_LINE_NUMBER) }; @@ -816,7 +816,7 @@ pub fn create_global_var_metadata(cx: &CrateContext, let namespace_node = namespace_for_item(cx, ast_util::local_def(node_id)); let var_name = token::get_ident(ident).get().to_string(); let linkage_name = - namespace_node.mangled_name_of_contained_item(var_name.as_slice()); + namespace_node.mangled_name_of_contained_item(var_name[]); let var_scope = namespace_node.scope; var_name.with_c_str(|var_name| { @@ -857,7 +857,7 @@ pub fn create_local_var_metadata(bcx: Block, local: &ast::Local) { None => { bcx.sess().span_bug(span, format!("no entry in lllocals table for {}", - node_id).as_slice()); + node_id)[]); } }; @@ -911,7 +911,7 @@ pub fn create_captured_var_metadata<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, "debuginfo::create_captured_var_metadata() - \ Captured var-id refers to unexpected \ ast_map variant: {}", - ast_item).as_slice()); + ast_item)[]); } } } @@ -921,7 +921,7 @@ pub fn create_captured_var_metadata<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, format!("debuginfo::create_captured_var_metadata() - \ Captured var-id refers to unexpected \ ast_map variant: {}", - ast_item).as_slice()); + ast_item)[]); } }; @@ -1028,7 +1028,7 @@ pub fn create_argument_metadata(bcx: Block, arg: &ast::Arg) { None => { bcx.sess().span_bug(span, format!("no entry in lllocals table for {}", - node_id).as_slice()); + node_id)[]); } }; @@ -1286,7 +1286,7 @@ pub fn create_function_debug_context<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, match expr.node { ast::ExprClosure(_, _, ref fn_decl, ref top_level_block) => { let name = format!("fn{}", token::gensym("fn")); - let name = token::str_to_ident(name.as_slice()); + let name = token::str_to_ident(name[]); (name, &**fn_decl, // This is not quite right. It should actually inherit // the generics of the enclosing function. @@ -1318,7 +1318,7 @@ pub fn create_function_debug_context<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, cx.sess() .bug(format!("create_function_debug_context: \ unexpected sort of node: {}", - fnitem).as_slice()) + fnitem)[]) } } } @@ -1329,7 +1329,7 @@ pub fn create_function_debug_context<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, } _ => cx.sess().bug(format!("create_function_debug_context: \ unexpected sort of node: {}", - fnitem).as_slice()) + fnitem)[]) }; // This can be the case for functions inlined from another crate @@ -1338,7 +1338,7 @@ pub fn create_function_debug_context<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, } let loc = span_start(cx, span); - let file_metadata = file_metadata(cx, loc.file.name.as_slice()); + let file_metadata = file_metadata(cx, loc.file.name[]); let function_type_metadata = unsafe { let fn_signature = get_function_signature(cx, @@ -1365,7 +1365,7 @@ pub fn create_function_debug_context<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, let (linkage_name, containing_scope) = if has_path { let namespace_node = namespace_for_item(cx, ast_util::local_def(fn_ast_id)); let linkage_name = namespace_node.mangled_name_of_contained_item( - function_name.as_slice()); + function_name[]); let containing_scope = namespace_node.scope; (linkage_name, containing_scope) } else { @@ -1451,7 +1451,7 @@ fn get_function_signature<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, signature.push(type_metadata(cx, arg_type, codemap::DUMMY_SP)); } - return create_DIArray(DIB(cx), signature.as_slice()); + return create_DIArray(DIB(cx), signature[]); } fn get_template_parameters<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, @@ -1484,7 +1484,7 @@ fn get_template_parameters<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, actual_self_type, true); - name_to_append_suffix_to.push_str(actual_self_type_name.as_slice()); + name_to_append_suffix_to.push_str(actual_self_type_name[]); if generics.is_type_parameterized() { name_to_append_suffix_to.push_str(","); @@ -1524,7 +1524,7 @@ fn get_template_parameters<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, let actual_type_name = compute_debuginfo_type_name(cx, actual_type, true); - name_to_append_suffix_to.push_str(actual_type_name.as_slice()); + name_to_append_suffix_to.push_str(actual_type_name[]); if index != generics.ty_params.len() - 1 { name_to_append_suffix_to.push_str(","); @@ -1552,7 +1552,7 @@ fn get_template_parameters<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, name_to_append_suffix_to.push('>'); - return create_DIArray(DIB(cx), template_params.as_slice()); + return create_DIArray(DIB(cx), template_params[]); } } @@ -1650,7 +1650,7 @@ fn declare_local<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, let cx: &CrateContext = bcx.ccx(); let filename = span_start(cx, span).file.name.clone(); - let file_metadata = file_metadata(cx, filename.as_slice()); + let file_metadata = file_metadata(cx, filename[]); let name = token::get_ident(variable_ident); let loc = span_start(cx, span); @@ -1737,7 +1737,7 @@ fn file_metadata(cx: &CrateContext, full_path: &str) -> DIFile { let work_dir = cx.sess().working_dir.as_str().unwrap(); let file_name = if full_path.starts_with(work_dir) { - full_path.slice(work_dir.len() + 1u, full_path.len()) + full_path[work_dir.len() + 1u..full_path.len()] } else { full_path }; @@ -1771,7 +1771,7 @@ fn scope_metadata(fcx: &FunctionContext, fcx.ccx.sess().span_bug(error_reporting_span, format!("debuginfo: Could not find scope info for node {}", - node).as_slice()); + node)[]); } } } @@ -1971,7 +1971,7 @@ fn finalize<'a>(&self, cx: &CrateContext<'a, 'tcx>) -> MetadataCreationResult { cx.sess().bug(format!("Forward declaration of potentially recursive type \ '{}' was not found in TypeMap!", ppaux::ty_to_string(cx.tcx(), unfinished_type)) - .as_slice()); + []); } } @@ -1983,7 +1983,7 @@ fn finalize<'a>(&self, cx: &CrateContext<'a, 'tcx>) -> MetadataCreationResult { set_members_of_composite_type(cx, metadata_stub, llvm_type, - member_descriptions.as_slice()); + member_descriptions[]); return MetadataCreationResult::new(metadata_stub, true); } } @@ -2055,7 +2055,7 @@ fn prepare_struct_metadata<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, let struct_metadata_stub = create_struct_stub(cx, struct_llvm_type, - struct_name.as_slice(), + struct_name[], unique_type_id, containing_scope); @@ -2116,7 +2116,7 @@ fn prepare_tuple_metadata<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, unique_type_id, create_struct_stub(cx, tuple_llvm_type, - tuple_name.as_slice(), + tuple_name[], unique_type_id, UNKNOWN_SCOPE_METADATA), tuple_llvm_type, @@ -2176,7 +2176,7 @@ fn create_member_descriptions<'a>(&self, cx: &CrateContext<'a, 'tcx>) set_members_of_composite_type(cx, variant_type_metadata, variant_llvm_type, - member_descriptions.as_slice()); + member_descriptions[]); MemberDescription { name: "".to_string(), llvm_type: variant_llvm_type, @@ -2209,7 +2209,7 @@ fn create_member_descriptions<'a>(&self, cx: &CrateContext<'a, 'tcx>) set_members_of_composite_type(cx, variant_type_metadata, variant_llvm_type, - member_descriptions.as_slice()); + member_descriptions[]); vec![ MemberDescription { name: "".to_string(), @@ -2309,7 +2309,7 @@ fn create_member_descriptions<'a>(&self, cx: &CrateContext<'a, 'tcx>) set_members_of_composite_type(cx, variant_type_metadata, variant_llvm_type, - variant_member_descriptions.as_slice()); + variant_member_descriptions[]); // Encode the information about the null variant in the union // member's name. @@ -2388,7 +2388,7 @@ fn describe_enum_variant<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, .iter() .map(|&t| type_of::type_of(cx, t)) .collect::>() - .as_slice(), + [], struct_def.packed); // Could do some consistency checks here: size, align, field count, discr type @@ -2412,7 +2412,7 @@ fn describe_enum_variant<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, Some(ref names) => { names.iter() .map(|ident| { - token::get_ident(*ident).get().to_string().into_string() + token::get_ident(*ident).get().to_string() }).collect() } None => variant_info.args.iter().map(|_| "".to_string()).collect() @@ -2455,7 +2455,7 @@ fn prepare_enum_metadata<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, let (containing_scope, definition_span) = get_namespace_and_span_for_item(cx, enum_def_id); let loc = span_start(cx, definition_span); - let file_metadata = file_metadata(cx, loc.file.name.as_slice()); + let file_metadata = file_metadata(cx, loc.file.name[]); let variants = ty::enum_variants(cx.tcx(), enum_def_id); @@ -2502,7 +2502,7 @@ fn prepare_enum_metadata<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, UNKNOWN_LINE_NUMBER, bytes_to_bits(discriminant_size), bytes_to_bits(discriminant_align), - create_DIArray(DIB(cx), enumerators_metadata.as_slice()), + create_DIArray(DIB(cx), enumerators_metadata[]), discriminant_base_type_metadata) } }); @@ -2644,7 +2644,7 @@ fn set_members_of_composite_type(cx: &CrateContext, Please use a rustc built with anewer \ version of LLVM.", llvm_version_major, - llvm_version_minor).as_slice()); + llvm_version_minor)[]); } else { cx.sess().bug("debuginfo::set_members_of_composite_type() - \ Already completed forward declaration re-encountered."); @@ -2683,7 +2683,7 @@ fn set_members_of_composite_type(cx: &CrateContext, .collect(); unsafe { - let type_array = create_DIArray(DIB(cx), member_metadata.as_slice()); + let type_array = create_DIArray(DIB(cx), member_metadata[]); llvm::LLVMDICompositeTypeSetTypeArray(composite_type_metadata, type_array); } } @@ -2784,7 +2784,7 @@ fn vec_slice_metadata<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, let member_llvm_types = slice_llvm_type.field_types(); assert!(slice_layout_is_correct(cx, - member_llvm_types.as_slice(), + member_llvm_types[], element_type)); let member_descriptions = [ MemberDescription { @@ -2806,11 +2806,11 @@ fn vec_slice_metadata<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, assert!(member_descriptions.len() == member_llvm_types.len()); let loc = span_start(cx, span); - let file_metadata = file_metadata(cx, loc.file.name.as_slice()); + let file_metadata = file_metadata(cx, loc.file.name[]); let metadata = composite_type_metadata(cx, slice_llvm_type, - slice_type_name.as_slice(), + slice_type_name[], unique_type_id, &member_descriptions, UNKNOWN_SCOPE_METADATA, @@ -2856,7 +2856,7 @@ fn subroutine_type_metadata<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, llvm::LLVMDIBuilderCreateSubroutineType( DIB(cx), UNKNOWN_FILE_METADATA, - create_DIArray(DIB(cx), signature_metadata.as_slice())) + create_DIArray(DIB(cx), signature_metadata[])) }, false); } @@ -2882,7 +2882,7 @@ fn trait_pointer_metadata<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, let pp_type_name = ppaux::ty_to_string(cx.tcx(), trait_type); cx.sess().bug(format!("debuginfo: Unexpected trait-object type in \ trait_pointer_metadata(): {}", - pp_type_name.as_slice()).as_slice()); + pp_type_name[])[]); } }; @@ -2896,7 +2896,7 @@ fn trait_pointer_metadata<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, composite_type_metadata(cx, trait_llvm_type, - trait_type_name.as_slice(), + trait_type_name[], unique_type_id, &[], containing_scope, @@ -3019,13 +3019,13 @@ fn type_metadata<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, ty::ty_tup(ref elements) => { prepare_tuple_metadata(cx, t, - elements.as_slice(), + elements[], unique_type_id, usage_site_span).finalize(cx) } _ => { cx.sess().bug(format!("debuginfo: unexpected type in type_metadata: {}", - sty).as_slice()) + sty)[]) } }; @@ -3043,9 +3043,9 @@ fn type_metadata<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, type id '{}' to already be in \ the debuginfo::TypeMap but it \ was not. (Ty = {})", - unique_type_id_str.as_slice(), + unique_type_id_str[], ppaux::ty_to_string(cx.tcx(), t)); - cx.sess().span_bug(usage_site_span, error_message.as_slice()); + cx.sess().span_bug(usage_site_span, error_message[]); } }; @@ -3058,9 +3058,9 @@ fn type_metadata<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, UniqueTypeId maps in \ debuginfo::TypeMap. \ UniqueTypeId={}, Ty={}", - unique_type_id_str.as_slice(), + unique_type_id_str[], ppaux::ty_to_string(cx.tcx(), t)); - cx.sess().span_bug(usage_site_span, error_message.as_slice()); + cx.sess().span_bug(usage_site_span, error_message[]); } } None => { @@ -3266,7 +3266,7 @@ fn with_new_scope(cx: &CrateContext, { // Create a new lexical scope and push it onto the stack let loc = cx.sess().codemap().lookup_char_pos(scope_span.lo); - let file_metadata = file_metadata(cx, loc.file.name.as_slice()); + let file_metadata = file_metadata(cx, loc.file.name[]); let parent_scope = scope_stack.last().unwrap().scope_metadata; let scope_metadata = unsafe { @@ -3391,7 +3391,7 @@ fn walk_pattern(cx: &CrateContext, let file_metadata = file_metadata(cx, loc.file .name - .as_slice()); + []); let parent_scope = scope_stack.last().unwrap().scope_metadata; let scope_metadata = unsafe { @@ -3925,7 +3925,7 @@ fn push_debuginfo_type_name<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, ty::ty_open(_) | ty::ty_param(_) => { cx.sess().bug(format!("debuginfo: Trying to create type name for \ - unexpected type: {}", ppaux::ty_to_string(cx.tcx(), t)).as_slice()); + unexpected type: {}", ppaux::ty_to_string(cx.tcx(), t))[]); } } @@ -4008,13 +4008,13 @@ fn fill_nested(node: &NamespaceTreeNode, output: &mut String) { None => {} } let string = token::get_name(node.name); - output.push_str(format!("{}", string.get().len()).as_slice()); + output.push_str(format!("{}", string.get().len())[]); output.push_str(string.get()); } let mut name = String::from_str("_ZN"); fill_nested(self, &mut name); - name.push_str(format!("{}", item_name.len()).as_slice()); + name.push_str(format!("{}", item_name.len())[]); name.push_str(item_name); name.push('E'); name @@ -4022,7 +4022,7 @@ fn fill_nested(node: &NamespaceTreeNode, output: &mut String) { } fn crate_root_namespace<'a>(cx: &'a CrateContext) -> &'a str { - cx.link_meta().crate_name.as_slice() + cx.link_meta().crate_name[] } fn namespace_for_item(cx: &CrateContext, def_id: ast::DefId) -> Rc { @@ -4099,7 +4099,7 @@ fn namespace_for_item(cx: &CrateContext, def_id: ast::DefId) -> Rc { cx.sess().bug(format!("debuginfo::namespace_for_item(): \ path too short for {}", - def_id).as_slice()); + def_id)[]); } } }) diff --git a/src/librustc_trans/trans/expr.rs b/src/librustc_trans/trans/expr.rs index 81892e5fa832..36f23f4a0cad 100644 --- a/src/librustc_trans/trans/expr.rs +++ b/src/librustc_trans/trans/expr.rs @@ -311,7 +311,7 @@ fn unsized_info<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, unsized_info(bcx, k, id, ty_substs[tp_index], |t| t) } _ => bcx.sess().bug(format!("UnsizeStruct with bad sty: {}", - bcx.ty_to_string(unadjusted_ty)).as_slice()) + bcx.ty_to_string(unadjusted_ty))[]) }, &ty::UnsizeVtable(ty::TyTrait { ref principal, .. }, _) => { let substs = principal.substs().with_self_ty(unadjusted_ty).erase_regions(); @@ -442,7 +442,7 @@ fn unsize_unique_expr<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, let unboxed_ty = match datum_ty.sty { ty::ty_uniq(t) => t, _ => bcx.sess().bug(format!("Expected ty_uniq, found {}", - bcx.ty_to_string(datum_ty)).as_slice()) + bcx.ty_to_string(datum_ty))[]) }; let result_ty = ty::mk_uniq(tcx, ty::unsize_ty(tcx, unboxed_ty, k, expr.span)); @@ -660,7 +660,7 @@ fn trans_datum_unadjusted<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, expr.span, format!("trans_rvalue_datum_unadjusted reached \ fall-through case: {}", - expr.node).as_slice()); + expr.node)[]); } } } @@ -1007,7 +1007,7 @@ fn trans_rvalue_stmt_unadjusted<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, expr.span, format!("trans_rvalue_stmt_unadjusted reached \ fall-through case: {}", - expr.node).as_slice()); + expr.node)[]); } } } @@ -1033,14 +1033,14 @@ fn trans_rvalue_dps_unadjusted<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, controlflow::trans_if(bcx, expr.id, &**cond, &**thn, els.as_ref().map(|e| &**e), dest) } ast::ExprMatch(ref discr, ref arms, _) => { - _match::trans_match(bcx, expr, &**discr, arms.as_slice(), dest) + _match::trans_match(bcx, expr, &**discr, arms[], dest) } ast::ExprBlock(ref blk) => { controlflow::trans_block(bcx, &**blk, dest) } ast::ExprStruct(_, ref fields, ref base) => { trans_struct(bcx, - fields.as_slice(), + fields[], base.as_ref().map(|e| &**e), expr.span, expr.id, @@ -1052,7 +1052,7 @@ fn trans_rvalue_dps_unadjusted<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, trans_adt(bcx, expr_ty(bcx, expr), 0, - numbered_fields.as_slice(), + numbered_fields[], None, dest, Some(NodeInfo { id: expr.id, span: expr.span })) @@ -1096,13 +1096,13 @@ fn trans_rvalue_dps_unadjusted<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, trans_overloaded_call(bcx, expr, &**f, - args.as_slice(), + args[], Some(dest)) } else { callee::trans_call(bcx, expr, &**f, - callee::ArgExprs(args.as_slice()), + callee::ArgExprs(args[]), dest) } } @@ -1110,7 +1110,7 @@ fn trans_rvalue_dps_unadjusted<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, callee::trans_method_call(bcx, expr, &*args[0], - callee::ArgExprs(args.as_slice()), + callee::ArgExprs(args[]), dest) } ast::ExprBinary(op, ref lhs, ref rhs) => { @@ -1159,7 +1159,7 @@ fn trans_rvalue_dps_unadjusted<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, expr.span, format!("trans_rvalue_dps_unadjusted reached fall-through \ case: {}", - expr.node).as_slice()); + expr.node)[]); } } } @@ -1207,7 +1207,7 @@ fn trans_def_dps_unadjusted<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, _ => { bcx.tcx().sess.span_bug(ref_expr.span, format!( "Non-DPS def {} referened by {}", - def, bcx.node_id_to_string(ref_expr.id)).as_slice()); + def, bcx.node_id_to_string(ref_expr.id))[]); } } } @@ -1234,7 +1234,7 @@ fn trans_def_fn_unadjusted<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, bcx.tcx().sess.span_bug(ref_expr.span, format!( "trans_def_fn_unadjusted invoked on: {} for {}", def, - ref_expr.repr(bcx.tcx())).as_slice()); + ref_expr.repr(bcx.tcx()))[]); } }; @@ -1257,7 +1257,7 @@ pub fn trans_local_var<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, None => { bcx.sess().bug(format!( "trans_local_var: no llval for upvar {} found", - nid).as_slice()); + nid)[]); } } } @@ -1267,7 +1267,7 @@ pub fn trans_local_var<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, None => { bcx.sess().bug(format!( "trans_local_var: no datum for local/arg {} found", - nid).as_slice()); + nid)[]); } }; debug!("take_local(nid={}, v={}, ty={})", @@ -1277,7 +1277,7 @@ pub fn trans_local_var<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, _ => { bcx.sess().unimpl(format!( "unsupported def type in trans_local_var: {}", - def).as_slice()); + def)[]); } } } @@ -1294,11 +1294,11 @@ pub fn with_field_tys<'tcx, R, F>(tcx: &ty::ctxt<'tcx>, { match ty.sty { ty::ty_struct(did, ref substs) => { - op(0, struct_fields(tcx, did, substs).as_slice()) + op(0, struct_fields(tcx, did, substs)[]) } ty::ty_tup(ref v) => { - op(0, tup_fields(v.as_slice()).as_slice()) + op(0, tup_fields(v[])[]) } ty::ty_enum(_, ref substs) => { @@ -1308,7 +1308,7 @@ pub fn with_field_tys<'tcx, R, F>(tcx: &ty::ctxt<'tcx>, tcx.sess.bug(format!( "cannot get field types from the enum type {} \ without a node ID", - ty.repr(tcx)).as_slice()); + ty.repr(tcx))[]); } Some(node_id) => { let def = tcx.def_map.borrow()[node_id].clone(); @@ -1319,7 +1319,7 @@ pub fn with_field_tys<'tcx, R, F>(tcx: &ty::ctxt<'tcx>, op(variant_info.disr_val, struct_fields(tcx, variant_id, - substs).as_slice()) + substs)[]) } _ => { tcx.sess.bug("resolve didn't map this expr to a \ @@ -1333,7 +1333,7 @@ pub fn with_field_tys<'tcx, R, F>(tcx: &ty::ctxt<'tcx>, _ => { tcx.sess.bug(format!( "cannot get field types from the type {}", - ty.repr(tcx)).as_slice()); + ty.repr(tcx))[]); } } } @@ -1388,7 +1388,7 @@ fn trans_struct<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, trans_adt(bcx, ty, discr, - numbered_fields.as_slice(), + numbered_fields[], optbase, dest, Some(NodeInfo { id: expr_id, span: expr_span })) @@ -2025,7 +2025,7 @@ fn trans_imm_cast<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, t_in.repr(bcx.tcx()), k_in, t_out.repr(bcx.tcx()), - k_out).as_slice()) + k_out)[]) } } } @@ -2034,7 +2034,7 @@ fn trans_imm_cast<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, t_in.repr(bcx.tcx()), k_in, t_out.repr(bcx.tcx()), - k_out).as_slice()) + k_out)[]) }; return immediate_rvalue_bcx(bcx, newval, t_out).to_expr_datumblock(); } @@ -2196,7 +2196,7 @@ fn deref_once<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, bcx.tcx().sess.span_bug( expr.span, format!("deref invoked on expr of illegal type {}", - datum.ty.repr(bcx.tcx())).as_slice()); + datum.ty.repr(bcx.tcx()))[]); } }; diff --git a/src/librustc_trans/trans/foreign.rs b/src/librustc_trans/trans/foreign.rs index d07203199305..d7e3476a470f 100644 --- a/src/librustc_trans/trans/foreign.rs +++ b/src/librustc_trans/trans/foreign.rs @@ -106,7 +106,7 @@ pub fn register_static(ccx: &CrateContext, let llty = type_of::type_of(ccx, ty); let ident = link_name(foreign_item); - match attr::first_attr_value_str_by_name(foreign_item.attrs.as_slice(), + match attr::first_attr_value_str_by_name(foreign_item.attrs[], "linkage") { // If this is a static with a linkage specified, then we need to handle // it a little specially. The typesystem prevents things like &T and @@ -231,13 +231,13 @@ pub fn trans_native_call<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, ty::ty_bare_fn(ref fn_ty) => (fn_ty.abi, fn_ty.sig.clone()), _ => ccx.sess().bug("trans_native_call called on non-function type") }; - let llsig = foreign_signature(ccx, &fn_sig, passed_arg_tys.as_slice()); + let llsig = foreign_signature(ccx, &fn_sig, passed_arg_tys[]); let fn_type = cabi::compute_abi_info(ccx, - llsig.llarg_tys.as_slice(), + llsig.llarg_tys[], llsig.llret_ty, llsig.ret_def); - let arg_tys: &[cabi::ArgType] = fn_type.arg_tys.as_slice(); + let arg_tys: &[cabi::ArgType] = fn_type.arg_tys[]; let mut llargs_foreign = Vec::new(); @@ -363,7 +363,7 @@ pub fn trans_native_call<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, let llforeign_retval = CallWithConv(bcx, llfn, - llargs_foreign.as_slice(), + llargs_foreign[], cc, Some(attrs)); @@ -433,7 +433,7 @@ pub fn trans_foreign_mod(ccx: &CrateContext, foreign_mod: &ast::ForeignMod) { abi => { let ty = ty::node_id_to_type(ccx.tcx(), foreign_item.id); register_foreign_item_fn(ccx, abi, ty, - lname.get().as_slice()); + lname.get()[]); // Unlike for other items, we shouldn't call // `base::update_linkage` here. Foreign items have // special linkage requirements, which are handled @@ -563,7 +563,7 @@ fn build_rust_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, ccx.sess().bug(format!("build_rust_fn: extern fn {} has ty {}, \ expected a bare fn ty", ccx.tcx().map.path_to_string(id), - t.repr(tcx)).as_slice()); + t.repr(tcx))[]); } }; @@ -571,7 +571,7 @@ fn build_rust_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, ccx.tcx().map.path_to_string(id), id, t.repr(tcx)); - let llfn = base::decl_internal_rust_fn(ccx, t, ps.as_slice()); + let llfn = base::decl_internal_rust_fn(ccx, t, ps[]); base::set_llvm_fn_attrs(ccx, attrs, llfn); base::trans_fn(ccx, decl, body, llfn, param_substs, id, &[]); llfn @@ -744,7 +744,7 @@ unsafe fn build_wrap_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, debug!("calling llrustfn = {}, t = {}", ccx.tn().val_to_string(llrustfn), t.repr(ccx.tcx())); let attributes = base::get_fn_llvm_attributes(ccx, t); - let llrust_ret_val = builder.call(llrustfn, llrust_args.as_slice(), Some(attributes)); + let llrust_ret_val = builder.call(llrustfn, llrust_args[], Some(attributes)); // Get the return value where the foreign fn expects it. let llforeign_ret_ty = match tys.fn_ty.ret_ty.cast { @@ -811,9 +811,9 @@ unsafe fn build_wrap_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, // the massive simplifications that have occurred. pub fn link_name(i: &ast::ForeignItem) -> InternedString { - match attr::first_attr_value_str_by_name(i.attrs.as_slice(), "link_name") { + match attr::first_attr_value_str_by_name(i.attrs[], "link_name") { Some(ln) => ln.clone(), - None => match weak_lang_items::link_name(i.attrs.as_slice()) { + None => match weak_lang_items::link_name(i.attrs[]) { Some(name) => name, None => token::get_ident(i.ident), } @@ -854,7 +854,7 @@ fn foreign_types_for_fn_ty<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, }; let llsig = foreign_signature(ccx, &fn_sig, fn_sig.0.inputs.as_slice()); let fn_ty = cabi::compute_abi_info(ccx, - llsig.llarg_tys.as_slice(), + llsig.llarg_tys[], llsig.llret_ty, llsig.ret_def); debug!("foreign_types_for_fn_ty(\ @@ -863,9 +863,9 @@ fn foreign_types_for_fn_ty<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, fn_ty={} -> {}, \ ret_def={}", ty.repr(ccx.tcx()), - ccx.tn().types_to_str(llsig.llarg_tys.as_slice()), + ccx.tn().types_to_str(llsig.llarg_tys[]), ccx.tn().type_to_string(llsig.llret_ty), - ccx.tn().types_to_str(fn_ty.arg_tys.iter().map(|t| t.ty).collect::>().as_slice()), + ccx.tn().types_to_str(fn_ty.arg_tys.iter().map(|t| t.ty).collect::>()[]), ccx.tn().type_to_string(fn_ty.ret_ty.ty), llsig.ret_def); @@ -915,7 +915,7 @@ fn lltype_for_fn_from_foreign_types(ccx: &CrateContext, tys: &ForeignTypes) -> T if tys.fn_sig.0.variadic { Type::variadic_func(llargument_tys.as_slice(), &llreturn_ty) } else { - Type::func(llargument_tys.as_slice(), &llreturn_ty) + Type::func(llargument_tys[], &llreturn_ty) } } diff --git a/src/librustc_trans/trans/glue.rs b/src/librustc_trans/trans/glue.rs index dea095ecaf59..c1089ea3ad10 100644 --- a/src/librustc_trans/trans/glue.rs +++ b/src/librustc_trans/trans/glue.rs @@ -160,7 +160,7 @@ pub fn get_drop_glue<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, t: Ty<'tcx>) -> Val let (glue, new_sym) = match ccx.available_drop_glues().borrow().get(&t) { Some(old_sym) => { - let glue = decl_cdecl_fn(ccx, old_sym.as_slice(), llfnty, ty::mk_nil(ccx.tcx())); + let glue = decl_cdecl_fn(ccx, old_sym[], llfnty, ty::mk_nil(ccx.tcx())); (glue, None) }, None => { @@ -231,7 +231,7 @@ fn trans_struct_drop<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, f.sig.0.inputs[0] } _ => bcx.sess().bug(format!("Expected function type, found {}", - bcx.ty_to_string(fty)).as_slice()) + bcx.ty_to_string(fty))[]) }; let (struct_data, info) = if ty::type_is_sized(bcx.tcx(), t) { @@ -350,7 +350,7 @@ fn size_and_align_of_dst<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, t: Ty<'tcx>, info: (Mul(bcx, info, C_uint(bcx.ccx(), unit_size)), C_uint(bcx.ccx(), 8u)) } _ => bcx.sess().bug(format!("Unexpected unsized type, found {}", - bcx.ty_to_string(t)).as_slice()) + bcx.ty_to_string(t))[]) } } @@ -422,7 +422,7 @@ fn make_drop_glue<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, v0: ValueRef, t: Ty<'tcx>) bcx.sess().warn(format!("Ignoring drop flag in destructor for {}\ because the struct is unsized. See issue\ #16758", - bcx.ty_to_string(t)).as_slice()); + bcx.ty_to_string(t))[]); trans_struct_drop(bcx, t, v0, dtor, did, substs) } } @@ -504,7 +504,7 @@ pub fn declare_tydesc<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, t: Ty<'tcx>) note_unique_llvm_symbol(ccx, name); let ty_name = token::intern_and_get_ident( - ppaux::ty_to_string(ccx.tcx(), t).as_slice()); + ppaux::ty_to_string(ccx.tcx(), t)[]); let ty_name = C_str_slice(ccx, ty_name); debug!("--- declare_tydesc {}", ppaux::ty_to_string(ccx.tcx(), t)); @@ -523,8 +523,8 @@ fn declare_generic_glue<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, t: Ty<'tcx>, let fn_nm = mangle_internal_name_by_type_and_seq( ccx, t, - format!("glue_{}", name).as_slice()); - let llfn = decl_cdecl_fn(ccx, fn_nm.as_slice(), llfnty, ty::mk_nil(ccx.tcx())); + format!("glue_{}", name)[]); + let llfn = decl_cdecl_fn(ccx, fn_nm[], llfnty, ty::mk_nil(ccx.tcx())); note_unique_llvm_symbol(ccx, fn_nm.clone()); return (fn_nm, llfn); } diff --git a/src/librustc_trans/trans/intrinsic.rs b/src/librustc_trans/trans/intrinsic.rs index a6f7c849f4d9..cc506e409c56 100644 --- a/src/librustc_trans/trans/intrinsic.rs +++ b/src/librustc_trans/trans/intrinsic.rs @@ -118,7 +118,7 @@ pub fn check_intrinsics(ccx: &CrateContext) { "" } else { "s" - }).as_slice()); + })[]); } if ty::type_is_fat_ptr(ccx.tcx(), transmute_restriction.to) || ty::type_is_fat_ptr(ccx.tcx(), transmute_restriction.from) { diff --git a/src/librustc_trans/trans/meth.rs b/src/librustc_trans/trans/meth.rs index 15f6d7bc3f42..25b8cefa68f2 100644 --- a/src/librustc_trans/trans/meth.rs +++ b/src/librustc_trans/trans/meth.rs @@ -77,7 +77,7 @@ pub fn trans_impl(ccx: &CrateContext, match *impl_item { ast::MethodImplItem(ref method) => { if method.pe_generics().ty_params.len() == 0u { - let trans_everywhere = attr::requests_inline(method.attrs.as_slice()); + let trans_everywhere = attr::requests_inline(method.attrs[]); for (ref ccx, is_origin) in ccx.maybe_iter(trans_everywhere) { let llfn = get_item_val(ccx, method.id); trans_fn(ccx, @@ -293,7 +293,7 @@ pub fn trans_static_method_callee(bcx: Block, _ => { bcx.tcx().sess.bug( format!("static call to invalid vtable: {}", - vtbl.repr(bcx.tcx())).as_slice()); + vtbl.repr(bcx.tcx()))[]); } } } @@ -375,7 +375,7 @@ fn trans_monomorphized_callee<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, traits::VtableParam(..) => { bcx.sess().bug( format!("resolved vtable bad vtable {} in trans", - vtable.repr(bcx.tcx())).as_slice()); + vtable.repr(bcx.tcx()))[]); } } } @@ -566,7 +566,7 @@ pub fn get_vtable<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, bcx.sess().bug( format!("resolved vtable for {} to bad vtable {} in trans", trait_ref.repr(bcx.tcx()), - vtable.repr(bcx.tcx())).as_slice()); + vtable.repr(bcx.tcx()))[]); } } }); @@ -598,7 +598,7 @@ pub fn make_vtable>(ccx: &CrateContext, let components: Vec<_> = head.into_iter().chain(ptrs).collect(); unsafe { - let tbl = C_struct(ccx, components.as_slice(), false); + let tbl = C_struct(ccx, components[], false); let sym = token::gensym("vtable"); let vt_gvar = format!("vtable{}", sym.uint()).with_c_str(|buf| { llvm::LLVMAddGlobal(ccx.llmod(), val_ty(tbl).to_ref(), buf) diff --git a/src/librustc_trans/trans/monomorphize.rs b/src/librustc_trans/trans/monomorphize.rs index cb3c56ad2778..2a6aff56513a 100644 --- a/src/librustc_trans/trans/monomorphize.rs +++ b/src/librustc_trans/trans/monomorphize.rs @@ -122,7 +122,7 @@ pub fn monomorphic_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, hash = format!("h{}", state.result()); ccx.tcx().map.with_path(fn_id.node, |path| { - exported_name(path, hash.as_slice()) + exported_name(path, hash[]) }) }; @@ -132,9 +132,9 @@ pub fn monomorphic_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, let mut hash_id = Some(hash_id); let mk_lldecl = |abi: abi::Abi| { let lldecl = if abi != abi::Rust { - foreign::decl_rust_fn_with_foreign_abi(ccx, mono_ty, s.as_slice()) + foreign::decl_rust_fn_with_foreign_abi(ccx, mono_ty, s[]) } else { - decl_internal_rust_fn(ccx, mono_ty, s.as_slice()) + decl_internal_rust_fn(ccx, mono_ty, s[]) }; ccx.monomorphized().borrow_mut().insert(hash_id.take().unwrap(), lldecl); @@ -168,12 +168,12 @@ pub fn monomorphic_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, .. } => { let d = mk_lldecl(abi); - let needs_body = setup_lldecl(d, i.attrs.as_slice()); + let needs_body = setup_lldecl(d, i.attrs[]); if needs_body { if abi != abi::Rust { foreign::trans_rust_fn_with_foreign_abi( ccx, &**decl, &**body, &[], d, psubsts, fn_id.node, - Some(hash.as_slice())); + Some(hash[])); } else { trans_fn(ccx, &**decl, &**body, d, psubsts, fn_id.node, &[]); } @@ -197,7 +197,7 @@ pub fn monomorphic_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, trans_enum_variant(ccx, parent, &*v, - args.as_slice(), + args[], this_tv.disr_val, psubsts, d); @@ -211,7 +211,7 @@ pub fn monomorphic_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, match *ii { ast::MethodImplItem(ref mth) => { let d = mk_lldecl(abi::Rust); - let needs_body = setup_lldecl(d, mth.attrs.as_slice()); + let needs_body = setup_lldecl(d, mth.attrs[]); if needs_body { trans_fn(ccx, mth.pe_fn_decl(), @@ -232,7 +232,7 @@ pub fn monomorphic_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, match *method { ast::ProvidedMethod(ref mth) => { let d = mk_lldecl(abi::Rust); - let needs_body = setup_lldecl(d, mth.attrs.as_slice()); + let needs_body = setup_lldecl(d, mth.attrs[]); if needs_body { trans_fn(ccx, mth.pe_fn_decl(), mth.pe_body(), d, psubsts, mth.id, &[]); @@ -241,7 +241,7 @@ pub fn monomorphic_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, } _ => { ccx.sess().bug(format!("can't monomorphize a {}", - map_node).as_slice()) + map_node)[]) } } } @@ -249,7 +249,7 @@ pub fn monomorphic_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, let d = mk_lldecl(abi::Rust); set_inline_hint(d); base::trans_tuple_struct(ccx, - struct_def.fields.as_slice(), + struct_def.fields[], struct_def.ctor_id.expect("ast-mapped tuple struct \ didn't have a ctor id"), psubsts, @@ -267,7 +267,7 @@ pub fn monomorphic_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, ast_map::NodePat(..) | ast_map::NodeLocal(..) => { ccx.sess().bug(format!("can't monomorphize a {}", - map_node).as_slice()) + map_node)[]) } }; diff --git a/src/librustc_trans/trans/type_.rs b/src/librustc_trans/trans/type_.rs index 51a0533a7bb6..45a2a343066c 100644 --- a/src/librustc_trans/trans/type_.rs +++ b/src/librustc_trans/trans/type_.rs @@ -102,7 +102,7 @@ pub fn i8p(ccx: &CrateContext) -> Type { } pub fn int(ccx: &CrateContext) -> Type { - match ccx.tcx().sess.target.target.target_word_size.as_slice() { + match ccx.tcx().sess.target.target.target_word_size[] { "32" => Type::i32(ccx), "64" => Type::i64(ccx), tws => panic!("Unsupported target word size for int: {}", tws), diff --git a/src/librustc_trans/trans/type_of.rs b/src/librustc_trans/trans/type_of.rs index 2801e0ccead6..2ef0006814a1 100644 --- a/src/librustc_trans/trans/type_of.rs +++ b/src/librustc_trans/trans/type_of.rs @@ -137,7 +137,7 @@ pub fn type_of_rust_fn<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, let input_tys = inputs.iter().map(|&arg_ty| type_of_explicit_arg(cx, arg_ty)); atys.extend(input_tys); - Type::func(atys.as_slice(), &lloutputtype) + Type::func(atys[], &lloutputtype) } // Given a function type and a count of ty params, construct an llvm type @@ -187,7 +187,7 @@ pub fn sizing_type_of<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, t: Ty<'tcx>) -> Typ let llsizingty = match t.sty { _ if !ty::lltype_is_sized(cx.tcx(), t) => { cx.sess().bug(format!("trying to take the sizing type of {}, an unsized type", - ppaux::ty_to_string(cx.tcx(), t)).as_slice()) + ppaux::ty_to_string(cx.tcx(), t))[]) } ty::ty_bool => Type::bool(cx), @@ -241,7 +241,7 @@ pub fn sizing_type_of<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, t: Ty<'tcx>) -> Typ ty::ty_infer(..) | ty::ty_param(..) | ty::ty_err(..) => { cx.sess().bug(format!("fictitious type {} in sizing_type_of()", - ppaux::ty_to_string(cx.tcx(), t)).as_slice()) + ppaux::ty_to_string(cx.tcx(), t))[]) } ty::ty_vec(_, None) | ty::ty_trait(..) | ty::ty_str => panic!("unreachable") }; @@ -318,7 +318,7 @@ fn type_of_unsize_info<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, t: Ty<'tcx>) -> Ty let repr = adt::represent_type(cx, t); let tps = substs.types.get_slice(subst::TypeSpace); let name = llvm_type_name(cx, an_enum, did, tps); - adt::incomplete_type_of(cx, &*repr, name.as_slice()) + adt::incomplete_type_of(cx, &*repr, name[]) } ty::ty_unboxed_closure(did, _, ref substs) => { // Only create the named struct, but don't fill it in. We @@ -329,7 +329,7 @@ fn type_of_unsize_info<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, t: Ty<'tcx>) -> Ty // contents of the VecPerParamSpace to to construct the llvm // name let name = llvm_type_name(cx, an_unboxed_closure, did, substs.types.as_slice()); - adt::incomplete_type_of(cx, &*repr, name.as_slice()) + adt::incomplete_type_of(cx, &*repr, name[]) } ty::ty_uniq(ty) | ty::ty_rptr(_, ty::mt{ty, ..}) | ty::ty_ptr(ty::mt{ty, ..}) => { @@ -389,7 +389,7 @@ fn type_of_unsize_info<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, t: Ty<'tcx>) -> Ty let repr = adt::represent_type(cx, t); let tps = substs.types.get_slice(subst::TypeSpace); let name = llvm_type_name(cx, a_struct, did, tps); - adt::incomplete_type_of(cx, &*repr, name.as_slice()) + adt::incomplete_type_of(cx, &*repr, name[]) } } @@ -408,7 +408,7 @@ fn type_of_unsize_info<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, t: Ty<'tcx>) -> Ty } ty::ty_trait(..) => Type::opaque_trait(cx), _ => cx.sess().bug(format!("ty_open with sized type: {}", - ppaux::ty_to_string(cx.tcx(), t)).as_slice()) + ppaux::ty_to_string(cx.tcx(), t))[]) }, ty::ty_infer(..) => cx.sess().bug("type_of with ty_infer"), diff --git a/src/librustc_typeck/astconv.rs b/src/librustc_typeck/astconv.rs index 175763c874ef..8e7452f30d38 100644 --- a/src/librustc_typeck/astconv.rs +++ b/src/librustc_typeck/astconv.rs @@ -168,7 +168,7 @@ pub fn opt_ast_region_to_region<'tcx, AC: AstConv<'tcx>, RS: RegionScope>( format!("`{}`", name) } else { format!("one of `{}`'s {} elided lifetimes", name, n) - }.as_slice()); + }[]); if len == 2 && i == 0 { m.push_str(" or "); @@ -323,7 +323,7 @@ fn create_substs_for_ast_path<'tcx,AC,RS>( format!("wrong number of type arguments: {} {}, found {}", expected, required_ty_param_count, - supplied_ty_param_count).as_slice()); + supplied_ty_param_count)[]); } else if supplied_ty_param_count > formal_ty_param_count { let expected = if required_ty_param_count < formal_ty_param_count { "expected at most" @@ -334,7 +334,7 @@ fn create_substs_for_ast_path<'tcx,AC,RS>( format!("wrong number of type arguments: {} {}, found {}", expected, formal_ty_param_count, - supplied_ty_param_count).as_slice()); + supplied_ty_param_count)[]); } if supplied_ty_param_count > required_ty_param_count @@ -723,7 +723,7 @@ pub fn ast_ty_to_builtin_ty<'tcx, AC: AstConv<'tcx>, RS: RegionScope>( .sess .span_bug(ast_ty.span, format!("unbound path {}", - path.repr(this.tcx())).as_slice()) + path.repr(this.tcx()))[]) } Some(&d) => d }; @@ -920,10 +920,10 @@ pub fn ast_ty_to_ty<'tcx, AC: AstConv<'tcx>, RS: RegionScope>( ty::mk_vec(tcx, ast_ty_to_ty(this, rscope, &**ty), None) } ast::TyObjectSum(ref ty, ref bounds) => { - match ast_ty_to_trait_ref(this, rscope, &**ty, bounds.as_slice()) { + match ast_ty_to_trait_ref(this, rscope, &**ty, bounds[]) { Ok(trait_ref) => { trait_ref_to_object_type(this, rscope, ast_ty.span, - trait_ref, bounds.as_slice()) + trait_ref, bounds[]) } Err(ErrorReported) => { ty::mk_err() @@ -977,7 +977,7 @@ pub fn ast_ty_to_ty<'tcx, AC: AstConv<'tcx>, RS: RegionScope>( ty::mk_closure(tcx, fn_decl) } ast::TyPolyTraitRef(ref bounds) => { - conv_ty_poly_trait_ref(this, rscope, ast_ty.span, bounds.as_slice()) + conv_ty_poly_trait_ref(this, rscope, ast_ty.span, bounds[]) } ast::TyPath(ref path, id) => { let a_def = match tcx.def_map.borrow().get(&id) { @@ -985,7 +985,7 @@ pub fn ast_ty_to_ty<'tcx, AC: AstConv<'tcx>, RS: RegionScope>( tcx.sess .span_bug(ast_ty.span, format!("unbound path {}", - path.repr(tcx)).as_slice()) + path.repr(tcx))[]) } Some(&d) => d }; @@ -1019,7 +1019,7 @@ pub fn ast_ty_to_ty<'tcx, AC: AstConv<'tcx>, RS: RegionScope>( def::DefMod(id) => { tcx.sess.span_fatal(ast_ty.span, format!("found module name used as a type: {}", - tcx.map.node_to_string(id.node)).as_slice()); + tcx.map.node_to_string(id.node))[]); } def::DefPrimTy(_) => { panic!("DefPrimTy arm missed in previous ast_ty_to_prim_ty call"); @@ -1038,7 +1038,7 @@ pub fn ast_ty_to_ty<'tcx, AC: AstConv<'tcx>, RS: RegionScope>( .last() .unwrap() .identifier) - .get()).as_slice()); + .get())[]); ty::mk_err() } def::DefAssociatedPath(typ, assoc_ident) => { @@ -1084,7 +1084,7 @@ pub fn ast_ty_to_ty<'tcx, AC: AstConv<'tcx>, RS: RegionScope>( tcx.sess.span_fatal(ast_ty.span, format!("found value name used \ as a type: {}", - a_def).as_slice()); + a_def)[]); } } } @@ -1112,7 +1112,7 @@ pub fn ast_ty_to_ty<'tcx, AC: AstConv<'tcx>, RS: RegionScope>( ast_ty.span, format!("expected constant expr for array \ length: {}", - *r).as_slice()); + *r)[]); } } } @@ -1235,7 +1235,7 @@ fn ty_of_method_or_bare_fn<'a, 'tcx, AC: AstConv<'tcx>>( let input_params = if self_ty.is_some() { decl.inputs.slice_from(1) } else { - decl.inputs.as_slice() + decl.inputs[] }; let input_tys = input_params.iter().map(|a| ty_of_arg(this, &rb, a, None)); let input_pats: Vec = input_params.iter() @@ -1502,7 +1502,7 @@ pub fn conv_existential_bounds_from_partitioned_bounds<'tcx, AC, RS>( this.tcx().sess.span_err( b.trait_ref.path.span, format!("only the builtin traits can be used \ - as closure or object bounds").as_slice()); + as closure or object bounds")[]); } let region_bound = compute_region_bound(this, @@ -1572,7 +1572,7 @@ fn compute_opt_region_bound<'tcx>(tcx: &ty::ctxt<'tcx>, tcx.sess.span_err( span, format!("ambiguous lifetime bound, \ - explicit lifetime bound required").as_slice()); + explicit lifetime bound required")[]); } return Some(r); } @@ -1598,7 +1598,7 @@ fn compute_region_bound<'tcx, AC: AstConv<'tcx>, RS:RegionScope>( None => { this.tcx().sess.span_err( span, - format!("explicit lifetime bound required").as_slice()); + format!("explicit lifetime bound required")[]); ty::ReStatic } } diff --git a/src/librustc_typeck/check/method/mod.rs b/src/librustc_typeck/check/method/mod.rs index 3b7eb22e56cc..74e690bf68f3 100644 --- a/src/librustc_typeck/check/method/mod.rs +++ b/src/librustc_typeck/check/method/mod.rs @@ -269,7 +269,7 @@ pub fn lookup_in_trait_adjusted<'a, 'tcx>(fcx: &'a FnCtxt<'a, 'tcx>, span, format!( "trait method is &self but first arg is: {}", - transformed_self_ty.repr(fcx.tcx())).as_slice()); + transformed_self_ty.repr(fcx.tcx()))[]); } } } @@ -279,7 +279,7 @@ pub fn lookup_in_trait_adjusted<'a, 'tcx>(fcx: &'a FnCtxt<'a, 'tcx>, span, format!( "unexpected explicit self type in operator method: {}", - method_ty.explicit_self).as_slice()); + method_ty.explicit_self)[]); } } } @@ -333,7 +333,7 @@ pub fn report_error<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>, if is_field { cx.sess.span_note(span, format!("use `(s.{0})(...)` if you meant to call the \ - function stored in the `{0}` field", method_ustring).as_slice()); + function stored in the `{0}` field", method_ustring)[]); } if static_sources.len() > 0 { diff --git a/src/librustc_typeck/check/method/probe.rs b/src/librustc_typeck/check/method/probe.rs index b5776f9aeb34..961b664e404c 100644 --- a/src/librustc_typeck/check/method/probe.rs +++ b/src/librustc_typeck/check/method/probe.rs @@ -557,7 +557,7 @@ fn assemble_unboxed_closure_candidates(&mut self, self.tcx().sess.span_bug( self.span, format!("No entry for unboxed closure: {}", - closure_def_id.repr(self.tcx())).as_slice()); + closure_def_id.repr(self.tcx()))[]); } }; diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index cd9a09efe082..6b7ca399ad21 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -518,7 +518,7 @@ fn check_fn<'a, 'tcx>(ccx: &'a CrateCtxt<'a, 'tcx>, // The free region references will be bound the node_id of the body block. let fn_sig = liberate_late_bound_regions(tcx, CodeExtent::from_node_id(body.id), fn_sig); - let arg_tys = fn_sig.inputs.as_slice(); + let arg_tys = fn_sig.inputs[]; let ret_ty = fn_sig.output; debug!("check_fn(arg_tys={}, ret_ty={}, fn_id={})", @@ -616,7 +616,7 @@ pub fn check_item(ccx: &CrateCtxt, it: &ast::Item) { ast::ItemEnum(ref enum_definition, _) => { check_enum_variants(ccx, it.span, - enum_definition.variants.as_slice(), + enum_definition.variants[], it.id); } ast::ItemFn(ref decl, _, _, _, ref body) => { @@ -915,7 +915,7 @@ fn compare_impl_method<'tcx>(tcx: &ty::ctxt<'tcx>, but not in the trait", token::get_name(trait_m.name), ppaux::explicit_self_category_to_str( - &impl_m.explicit_self)).as_slice()); + &impl_m.explicit_self))[]); return; } (_, &ty::StaticExplicitSelfCategory) => { @@ -925,7 +925,7 @@ fn compare_impl_method<'tcx>(tcx: &ty::ctxt<'tcx>, but not in the impl", token::get_name(trait_m.name), ppaux::explicit_self_category_to_str( - &trait_m.explicit_self)).as_slice()); + &trait_m.explicit_self))[]); return; } _ => { @@ -1229,7 +1229,7 @@ fn check_region_bounds_on_impl_method<'tcx>(tcx: &ty::ctxt<'tcx>, span, format!("lifetime parameters or bounds on method `{}` do \ not match the trait declaration", - token::get_name(impl_m.name)).as_slice()); + token::get_name(impl_m.name))[]); return false; } @@ -1281,7 +1281,7 @@ fn check_region_bounds_on_impl_method<'tcx>(tcx: &ty::ctxt<'tcx>, from its counterpart `{}` \ declared in the trait", impl_param.name.user_string(tcx), - trait_param.name.user_string(tcx)).as_slice()); + trait_param.name.user_string(tcx))[]); true } else { false @@ -1291,14 +1291,14 @@ fn check_region_bounds_on_impl_method<'tcx>(tcx: &ty::ctxt<'tcx>, tcx.sess.span_note( span, format!("the impl is missing the following bounds: `{}`", - missing.user_string(tcx)).as_slice()); + missing.user_string(tcx))[]); } if extra.len() != 0 { tcx.sess.span_note( span, format!("the impl has the following extra bounds: `{}`", - extra.user_string(tcx)).as_slice()); + extra.user_string(tcx))[]); } if err { @@ -1557,7 +1557,7 @@ pub fn local_ty(&self, span: Span, nid: ast::NodeId) -> Ty<'tcx> { self.tcx().sess.span_bug( span, format!("no type for local variable {}", - nid).as_slice()); + nid)[]); } } } @@ -1805,7 +1805,7 @@ pub fn expr_ty(&self, ex: &ast::Expr) -> Ty<'tcx> { Some(&t) => t, None => { self.tcx().sess.bug(format!("no type for expr in fcx {}", - self.tag()).as_slice()); + self.tag())[]); } } } @@ -1835,7 +1835,7 @@ pub fn node_ty(&self, id: ast::NodeId) -> Ty<'tcx> { self.tcx().sess.bug( format!("no type for node {}: {} in fcx {}", id, self.tcx().map.node_to_string(id), - self.tag()).as_slice()); + self.tag())[]); } } } @@ -2392,7 +2392,7 @@ fn lookup_method_for_for_loop<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>, Ok(trait_did) => trait_did, Err(ref err_string) => { fcx.tcx().sess.span_err(iterator_expr.span, - err_string.as_slice()); + err_string[]); return ty::mk_err() } }; @@ -2419,7 +2419,7 @@ fn lookup_method_for_for_loop<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>, format!("`for` loop expression has type `{}` which does \ not implement the `Iterator` trait; \ maybe try .iter()", - ty_string).as_slice()); + ty_string)[]); } ty::mk_err() } @@ -2457,7 +2457,7 @@ fn lookup_method_for_for_loop<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>, format!("`next` method of the `Iterator` \ trait has an unexpected type `{}`", fcx.infcx().ty_to_string(return_type)) - .as_slice()); + []); ty::mk_err() } } @@ -2484,7 +2484,7 @@ fn check_method_argument_types<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>, check_argument_types(fcx, sp, - err_inputs.as_slice(), + err_inputs[], callee_expr, args_no_rcvr, autoref_args, @@ -2941,7 +2941,7 @@ fn check_call(fcx: &FnCtxt, // Call the generic checker. check_argument_types(fcx, call_expr.span, - fn_sig.inputs.as_slice(), + fn_sig.inputs[], f, args, AutorefArgs::No, @@ -3306,7 +3306,7 @@ fn check_field(fcx: &FnCtxt, ty::ty_struct(base_id, ref substs) => { debug!("struct named {}", ppaux::ty_to_string(tcx, base_t)); let fields = ty::lookup_struct_fields(tcx, base_id); - lookup_field_ty(tcx, base_id, fields.as_slice(), + lookup_field_ty(tcx, base_id, fields[], field.node.name, &(*substs)) } _ => None @@ -3369,7 +3369,7 @@ fn check_tup_field(fcx: &FnCtxt, if tuple_like { debug!("tuple struct named {}", ppaux::ty_to_string(tcx, base_t)); let fields = ty::lookup_struct_fields(tcx, base_id); - lookup_tup_field_ty(tcx, base_id, fields.as_slice(), + lookup_tup_field_ty(tcx, base_id, fields[], idx.node, &(*substs)) } else { None @@ -3522,7 +3522,7 @@ fn check_struct_constructor(fcx: &FnCtxt, class_id, id, struct_substs, - class_fields.as_slice(), + class_fields[], fields, base_expr.is_none()); if ty::type_is_error(fcx.node_ty(id)) { @@ -3564,7 +3564,7 @@ fn check_struct_enum_variant(fcx: &FnCtxt, variant_id, id, substitutions, - variant_fields.as_slice(), + variant_fields[], fields, true); fcx.write_ty(id, enum_type); @@ -3936,8 +3936,8 @@ fn check_struct_fields_on_error(fcx: &FnCtxt, let f_ty = fcx.expr_ty(&**f); let args: Vec<_> = args.iter().map(|x| x).collect(); - if !try_overloaded_call(fcx, expr, &**f, f_ty, args.as_slice()) { - check_call(fcx, expr, &**f, args.as_slice()); + if !try_overloaded_call(fcx, expr, &**f, f_ty, args[]) { + check_call(fcx, expr, &**f, args[]); let args_err = args.iter().fold(false, |rest_err, a| { // is this not working? @@ -3949,7 +3949,7 @@ fn check_struct_fields_on_error(fcx: &FnCtxt, } } ast::ExprMethodCall(ident, ref tps, ref args) => { - check_method_call(fcx, expr, ident, args.as_slice(), tps.as_slice(), lvalue_pref); + check_method_call(fcx, expr, ident, args[], tps[], lvalue_pref); let arg_tys = args.iter().map(|a| fcx.expr_ty(&**a)); let args_err = arg_tys.fold(false, |rest_err, a| { @@ -4074,7 +4074,7 @@ fn check_struct_fields_on_error(fcx: &FnCtxt, let struct_id = match def { Some(def::DefVariant(enum_id, variant_id, true)) => { check_struct_enum_variant(fcx, id, expr.span, enum_id, - variant_id, fields.as_slice()); + variant_id, fields[]); enum_id } Some(def::DefTrait(def_id)) => { @@ -4083,7 +4083,7 @@ fn check_struct_fields_on_error(fcx: &FnCtxt, pprust::path_to_string(path)); check_struct_fields_on_error(fcx, id, - fields.as_slice(), + fields[], base_expr); def_id }, @@ -4096,7 +4096,7 @@ fn check_struct_fields_on_error(fcx: &FnCtxt, id, expr.span, struct_did, - fields.as_slice(), + fields[], base_expr.as_ref().map(|e| &**e)); } _ => { @@ -4105,7 +4105,7 @@ fn check_struct_fields_on_error(fcx: &FnCtxt, pprust::path_to_string(path)); check_struct_fields_on_error(fcx, id, - fields.as_slice(), + fields[], base_expr); } } @@ -4146,7 +4146,7 @@ fn check_struct_fields_on_error(fcx: &FnCtxt, fcx.infcx() .ty_to_string( actual_structure_type), - type_error_description).as_slice()); + type_error_description)[]); ty::note_and_explain_type_err(tcx, &type_error); } } @@ -4755,7 +4755,7 @@ fn do_check<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>, } let hint = *ty::lookup_repr_hints(ccx.tcx, ast::DefId { krate: ast::LOCAL_CRATE, node: id }) - .as_slice().get(0).unwrap_or(&attr::ReprAny); + [].get(0).unwrap_or(&attr::ReprAny); if hint != attr::ReprAny && vs.len() <= 1 { if vs.len() == 1 { @@ -5438,7 +5438,7 @@ fn param<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>, n: uint) -> Ty<'tcx> { "get_tydesc" => { let tydesc_ty = match ty::get_tydesc_ty(ccx.tcx) { Ok(t) => t, - Err(s) => { tcx.sess.span_fatal(it.span, s.as_slice()); } + Err(s) => { tcx.sess.span_fatal(it.span, s[]); } }; let td_ptr = ty::mk_ptr(ccx.tcx, ty::mt { ty: tydesc_ty, @@ -5454,7 +5454,7 @@ fn param<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>, n: uint) -> Ty<'tcx> { ty::mk_struct(ccx.tcx, did, subst::Substs::empty())), Err(msg) => { - tcx.sess.span_fatal(it.span, msg.as_slice()); + tcx.sess.span_fatal(it.span, msg[]); } } }, diff --git a/src/librustc_typeck/check/regionck.rs b/src/librustc_typeck/check/regionck.rs index 8e70b8ff0da5..22502c0dd1a1 100644 --- a/src/librustc_typeck/check/regionck.rs +++ b/src/librustc_typeck/check/regionck.rs @@ -251,7 +251,7 @@ fn region_of_def(fcx: &FnCtxt, def: def::Def) -> ty::Region { } _ => { tcx.sess.bug(format!("unexpected def in region_of_def: {}", - def).as_slice()) + def)[]) } } } @@ -345,13 +345,13 @@ fn visit_fn_body(&mut self, Some(f) => f, None => { self.tcx().sess.bug( - format!("No fn-sig entry for id={}", id).as_slice()); + format!("No fn-sig entry for id={}", id)[]); } }; let len = self.region_param_pairs.len(); - self.relate_free_regions(fn_sig.as_slice(), body.id); - link_fn_args(self, CodeExtent::from_node_id(body.id), fn_decl.inputs.as_slice()); + self.relate_free_regions(fn_sig[], body.id); + link_fn_args(self, CodeExtent::from_node_id(body.id), fn_decl.inputs[]); self.visit_block(body); self.visit_region_obligations(body.id); self.region_param_pairs.truncate(len); @@ -738,7 +738,7 @@ fn visit_expr(rcx: &mut Rcx, expr: &ast::Expr) { } ast::ExprMatch(ref discr, ref arms, _) => { - link_match(rcx, &**discr, arms.as_slice()); + link_match(rcx, &**discr, arms[]); visit::walk_expr(rcx, expr); } @@ -1186,7 +1186,7 @@ fn constrain_autoderefs<'a, 'tcx>(rcx: &mut Rcx<'a, 'tcx>, ty::ty_rptr(r, ref m) => (m.mutbl, r), _ => rcx.tcx().sess.span_bug(deref_expr.span, format!("bad overloaded deref type {}", - method.ty.repr(rcx.tcx())).as_slice()) + method.ty.repr(rcx.tcx()))[]) }; { let mc = mc::MemCategorizationContext::new(rcx); @@ -1560,7 +1560,7 @@ fn link_reborrowed_region<'a, 'tcx>(rcx: &Rcx<'a, 'tcx>, span, format!("Illegal upvar id: {}", upvar_id.repr( - rcx.tcx())).as_slice()); + rcx.tcx()))[]); } } } diff --git a/src/librustc_typeck/check/regionmanip.rs b/src/librustc_typeck/check/regionmanip.rs index 112ad1fb5b9b..eaf638e388e3 100644 --- a/src/librustc_typeck/check/regionmanip.rs +++ b/src/librustc_typeck/check/regionmanip.rs @@ -138,7 +138,7 @@ fn accumulate_from_ty(&mut self, ty: Ty<'tcx>) { ty::ty_open(_) => { self.tcx.sess.bug( format!("Unexpected type encountered while doing wf check: {}", - ty.repr(self.tcx)).as_slice()); + ty.repr(self.tcx))[]); } } } diff --git a/src/librustc_typeck/check/vtable.rs b/src/librustc_typeck/check/vtable.rs index 4db795a1fda5..e23bf46b564b 100644 --- a/src/librustc_typeck/check/vtable.rs +++ b/src/librustc_typeck/check/vtable.rs @@ -77,7 +77,7 @@ pub fn check_object_cast<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>, source_expr.span, format!("can only cast an boxed pointer \ to a boxed object, not a {}", - ty::ty_sort_string(fcx.tcx(), source_ty)).as_slice()); + ty::ty_sort_string(fcx.tcx(), source_ty))[]); } (_, &ty::ty_rptr(..)) => { @@ -85,7 +85,7 @@ pub fn check_object_cast<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>, source_expr.span, format!("can only cast a &-pointer \ to an &-object, not a {}", - ty::ty_sort_string(fcx.tcx(), source_ty)).as_slice()); + ty::ty_sort_string(fcx.tcx(), source_ty))[]); } _ => { @@ -164,7 +164,7 @@ fn check_object_safety_inner<'tcx>(tcx: &ty::ctxt<'tcx>, trait_name); for msg in errors { - tcx.sess.note(msg.as_slice()); + tcx.sess.note(msg[]); } } @@ -455,7 +455,7 @@ pub fn maybe_report_ambiguity<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>, format!( "unable to infer enough type information about `{}`; type annotations \ required", - self_ty.user_string(fcx.tcx())).as_slice()); + self_ty.user_string(fcx.tcx()))[]); } else { fcx.tcx().sess.span_err( obligation.cause.span, @@ -464,7 +464,7 @@ pub fn maybe_report_ambiguity<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>, locate the impl of the trait `{}` for \ the type `{}`; type annotations required", trait_ref.user_string(fcx.tcx()), - self_ty.user_string(fcx.tcx())).as_slice()); + self_ty.user_string(fcx.tcx()))[]); note_obligation_cause(fcx, obligation); } } @@ -477,7 +477,7 @@ pub fn maybe_report_ambiguity<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>, cannot locate the impl of the trait `{}` for \ the type `{}`", trait_ref.user_string(fcx.tcx()), - self_ty.user_string(fcx.tcx())).as_slice()); + self_ty.user_string(fcx.tcx()))[]); } } diff --git a/src/librustc_typeck/coherence/mod.rs b/src/librustc_typeck/coherence/mod.rs index 5d0bb6622c2e..c08eeb6e13ee 100644 --- a/src/librustc_typeck/coherence/mod.rs +++ b/src/librustc_typeck/coherence/mod.rs @@ -488,7 +488,7 @@ fn check_implementations_of_copy(&self) { format!("the trait `Copy` may not be \ implemented for this type; field \ `{}` does not implement `Copy`", - token::get_name(name)).as_slice()) + token::get_name(name))[]) } Err(ty::VariantDoesNotImplementCopy(name)) => { tcx.sess @@ -496,7 +496,7 @@ fn check_implementations_of_copy(&self) { format!("the trait `Copy` may not be \ implemented for this type; variant \ `{}` does not implement `Copy`", - token::get_name(name)).as_slice()) + token::get_name(name))[]) } Err(ty::TypeIsStructural) => { tcx.sess diff --git a/src/librustc_typeck/collect.rs b/src/librustc_typeck/collect.rs index 3f59b50337fa..22c9a2e7b329 100644 --- a/src/librustc_typeck/collect.rs +++ b/src/librustc_typeck/collect.rs @@ -171,7 +171,7 @@ fn get_item_ty(&self, id: ast::DefId) -> ty::Polytype<'tcx> { x => { self.tcx.sess.bug(format!("unexpected sort of node \ in get_item_ty(): {}", - x).as_slice()); + x)[]); } } } @@ -217,7 +217,7 @@ pub fn get_enum_variant_types<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>, ast::TupleVariantKind(ref args) if args.len() > 0 => { let rs = ExplicitRscope; let input_tys: Vec<_> = args.iter().map(|va| ccx.to_ty(&rs, &*va.ty)).collect(); - ty::mk_ctor_fn(tcx, input_tys.as_slice(), enum_ty) + ty::mk_ctor_fn(tcx, input_tys[], enum_ty) } ast::TupleVariantKind(_) => { @@ -270,7 +270,7 @@ fn collect_trait_methods<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>, ccx, trait_id, &trait_def.generics, - trait_items.as_slice(), + trait_items[], &m.id, &m.ident.name, &m.explicit_self, @@ -284,7 +284,7 @@ fn collect_trait_methods<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>, ccx, trait_id, &trait_def.generics, - trait_items.as_slice(), + trait_items[], &m.id, &m.pe_ident().name, m.pe_explicit_self(), @@ -379,7 +379,7 @@ fn ty_method_of_trait_method<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>, let tmcx = TraitMethodCtxt { ccx: ccx, trait_id: local_def(trait_id), - trait_items: trait_items.as_slice(), + trait_items: trait_items[], method_generics: &ty_generics, }; let trait_self_ty = ty::mk_self_type(tmcx.tcx(), @@ -1040,7 +1040,7 @@ pub fn convert(ccx: &CrateCtxt, it: &ast::Item) { write_ty_to_tcx(tcx, it.id, pty.ty); get_enum_variant_types(ccx, pty.ty, - enum_definition.variants.as_slice(), + enum_definition.variants[], generics); }, ast::ItemImpl(_, @@ -1086,7 +1086,7 @@ pub fn convert(ccx: &CrateCtxt, it: &ast::Item) { ast_trait_ref.ref_id).def_id()) } }, - impl_items: impl_items.as_slice(), + impl_items: impl_items[], impl_generics: &ty_generics, }; @@ -1184,7 +1184,7 @@ pub fn convert(ccx: &CrateCtxt, it: &ast::Item) { local_def(it.id)); let convert_method_context = TraitConvertMethodContext(local_def(it.id), - trait_methods.as_slice()); + trait_methods[]); convert_methods(ccx, convert_method_context, TraitContainer(local_def(it.id)), @@ -1279,7 +1279,7 @@ pub fn convert_struct<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>, |field| (*tcx.tcache.borrow())[ local_def(field.node.id)].ty).collect(); let ctor_fn_ty = ty::mk_ctor_fn(tcx, - inputs.as_slice(), + inputs[], selfty); write_ty_to_tcx(tcx, ctor_id, ctor_fn_ty); tcx.tcache.borrow_mut().insert(local_def(ctor_id), @@ -1320,7 +1320,7 @@ fn get_trait_def<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>, ast_map::NodeItem(item) => trait_def_of_item(ccx, &*item), _ => { ccx.tcx.sess.bug(format!("get_trait_def({}): not an item", - trait_id.node).as_slice()) + trait_id.node)[]) } } } @@ -1345,7 +1345,7 @@ pub fn trait_def_of_item<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>, ref s => { tcx.sess.span_bug( it.span, - format!("trait_def_of_item invoked on {}", s).as_slice()); + format!("trait_def_of_item invoked on {}", s)[]); } }; @@ -1585,8 +1585,8 @@ fn ty_generics_for_type_or_impl<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>, -> ty::Generics<'tcx> { ty_generics(ccx, subst::TypeSpace, - generics.lifetimes.as_slice(), - generics.ty_params.as_slice(), + generics.lifetimes[], + generics.ty_params[], ty::Generics::empty(), &generics.where_clause, create_type_parameters_for_associated_types) @@ -1602,8 +1602,8 @@ fn ty_generics_for_trait<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>, let mut generics = ty_generics(ccx, subst::TypeSpace, - ast_generics.lifetimes.as_slice(), - ast_generics.ty_params.as_slice(), + ast_generics.lifetimes[], + ast_generics.ty_params[], ty::Generics::empty(), &ast_generics.where_clause, DontCreateTypeParametersForAssociatedTypes); @@ -1672,8 +1672,8 @@ fn ty_generics_for_fn_or_method<'tcx,AC>( let early_lifetimes = resolve_lifetime::early_bound_lifetimes(generics); ty_generics(this, subst::FnSpace, - early_lifetimes.as_slice(), - generics.ty_params.as_slice(), + early_lifetimes[], + generics.ty_params[], base_generics, &generics.where_clause, create_type_parameters_for_associated_types) @@ -1701,7 +1701,7 @@ fn add_unsized_bound<'tcx,AC>(this: &AC, a default. \ Only `Sized?` is \ supported", - desc).as_slice()); + desc)[]); ty::try_add_builtin_trait(this.tcx(), kind_id, bounds); @@ -1973,7 +1973,7 @@ fn get_or_create_type_parameter_def<'tcx,AC>(this: &AC, let bounds = compute_bounds(this, param.ident.name, param_ty, - param.bounds.as_slice(), + param.bounds[], ¶m.unbound, param.span); let default = match param.default { @@ -2054,7 +2054,7 @@ fn check_bounds_compatible<'tcx>(tcx: &ty::ctxt<'tcx>, if !param_bounds.builtin_bounds.contains(&ty::BoundSized) { ty::each_bound_trait_and_supertraits( tcx, - param_bounds.trait_bounds.as_slice(), + param_bounds.trait_bounds[], |trait_ref| { let trait_def = ty::lookup_trait_def(tcx, trait_ref.def_id()); if trait_def.bounds.builtin_bounds.contains(&ty::BoundSized) { diff --git a/src/librustc_typeck/lib.rs b/src/librustc_typeck/lib.rs index 49c5f13fa739..5a8f58274cce 100644 --- a/src/librustc_typeck/lib.rs +++ b/src/librustc_typeck/lib.rs @@ -196,7 +196,7 @@ fn require_same_types<'a, 'tcx, M>(tcx: &ty::ctxt<'tcx>, format!("{}: {}", msg(), ty::type_err_to_str(tcx, - terr)).as_slice()); + terr))[]); ty::note_and_explain_type_err(tcx, terr); false } @@ -245,7 +245,7 @@ fn check_main_fn_ty(ccx: &CrateCtxt, format!("main has a non-function type: found \ `{}`", ppaux::ty_to_string(tcx, - main_t)).as_slice()); + main_t))[]); } } } @@ -296,8 +296,7 @@ fn check_start_fn_ty(ccx: &CrateCtxt, tcx.sess.span_bug(start_span, format!("start has a non-function type: found \ `{}`", - ppaux::ty_to_string(tcx, - start_t)).as_slice()); + ppaux::ty_to_string(tcx, start_t))[]); } } } diff --git a/src/librustc_typeck/variance.rs b/src/librustc_typeck/variance.rs index ef0d1bc3859f..754294a3b8e4 100644 --- a/src/librustc_typeck/variance.rs +++ b/src/librustc_typeck/variance.rs @@ -556,7 +556,7 @@ fn inferred_index(&self, param_id: ast::NodeId) -> InferredIndex { None => { self.tcx().sess.bug(format!( "no inferred index entry for {}", - self.tcx().map.node_to_string(param_id)).as_slice()); + self.tcx().map.node_to_string(param_id))[]); } } } @@ -834,7 +834,7 @@ fn add_constraints_from_ty(&mut self, self.tcx().sess.bug( format!("unexpected type encountered in \ variance inference: {}", - ty.repr(self.tcx())).as_slice()); + ty.repr(self.tcx()))[]); } } } @@ -911,7 +911,7 @@ fn add_constraints_from_region(&mut self, .sess .bug(format!("unexpected region encountered in variance \ inference: {}", - region.repr(self.tcx())).as_slice()); + region.repr(self.tcx()))[]); } } } @@ -1046,7 +1046,7 @@ fn write(&self) { // attribute and report an error with various results if found. if ty::has_attr(tcx, item_def_id, "rustc_variance") { let found = item_variances.repr(tcx); - tcx.sess.span_err(tcx.map.span(item_id), found.as_slice()); + tcx.sess.span_err(tcx.map.span(item_id), found[]); } let newly_added = tcx.item_variance_map.borrow_mut() diff --git a/src/librustdoc/externalfiles.rs b/src/librustdoc/externalfiles.rs index 08fb94a801c3..25a20e5998bd 100644 --- a/src/librustdoc/externalfiles.rs +++ b/src/librustdoc/externalfiles.rs @@ -36,7 +36,7 @@ pub fn load(in_header: &[String], before_content: &[String], after_content: &[St pub fn load_string(input: &Path) -> io::IoResult> { let mut f = try!(io::File::open(input)); let d = try!(f.read_to_end()); - Ok(str::from_utf8(d.as_slice()).map(|s| s.to_string())) + Ok(str::from_utf8(d.as_slice()).map(|s| s.to_string()).ok()) } macro_rules! load_or_return { diff --git a/src/librustdoc/html/format.rs b/src/librustdoc/html/format.rs index e01cbbc812b8..a2d5530692c1 100644 --- a/src/librustdoc/html/format.rs +++ b/src/librustdoc/html/format.rs @@ -16,7 +16,7 @@ //! them in the future to instead emit any format desired. use std::fmt; -use std::string::String; +use std::iter::repeat; use syntax::ast; use syntax::ast_util; @@ -198,12 +198,12 @@ fn resolved_path(w: &mut fmt::Formatter, did: ast::DefId, p: &clean::Path, path(w, p, print_all, |cache, loc| { if ast_util::is_local(did) || cache.inlined.contains(&did) { - Some(("../".repeat(loc.len())).to_string()) + Some(repeat("../").take(loc.len()).collect::()) } else { match cache.extern_locations[did.krate] { render::Remote(ref s) => Some(s.to_string()), render::Local => { - Some(("../".repeat(loc.len())).to_string()) + Some(repeat("../").take(loc.len()).collect::()) } render::Unknown => None, } @@ -324,7 +324,7 @@ fn primitive_link(f: &mut fmt::Formatter, let len = CURRENT_LOCATION_KEY.with(|s| s.borrow().len()); let len = if len == 0 {0} else {len - 1}; try!(write!(f, "", - "../".repeat(len), + repeat("../").take(len).collect::(), prim.to_url_str())); needs_termination = true; } @@ -337,7 +337,7 @@ fn primitive_link(f: &mut fmt::Formatter, render::Remote(ref s) => Some(s.to_string()), render::Local => { let len = CURRENT_LOCATION_KEY.with(|s| s.borrow().len()); - Some("../".repeat(len)) + Some(repeat("../").take(len).collect::()) } render::Unknown => None, }; diff --git a/src/librustdoc/html/highlight.rs b/src/librustdoc/html/highlight.rs index 111650f565cf..c936f6a0819d 100644 --- a/src/librustdoc/html/highlight.rs +++ b/src/librustdoc/html/highlight.rs @@ -34,7 +34,7 @@ pub fn highlight(src: &str, class: Option<&str>, id: Option<&str>) -> String { class, id, &mut out).unwrap(); - String::from_utf8_lossy(out[]).into_string() + String::from_utf8_lossy(out[]).into_owned() } /// Exhausts the `lexer` writing the output into `out`. diff --git a/src/librustdoc/html/render.rs b/src/librustdoc/html/render.rs index efec620bca75..dc31cfae99cb 100644 --- a/src/librustdoc/html/render.rs +++ b/src/librustdoc/html/render.rs @@ -42,8 +42,8 @@ use std::io::fs::PathExtensions; use std::io::{fs, File, BufferedWriter, BufferedReader}; use std::io; +use std::iter::repeat; use std::str; -use std::string::String; use std::sync::Arc; use externalfiles::ExternalHtml; @@ -1186,7 +1186,8 @@ fn render(w: io::File, cx: &Context, it: &clean::Item, &Sidebar{ cx: cx, item: it }, &Item{ cx: cx, item: it })); } else { - let mut url = "../".repeat(cx.current.len()); + let mut url = repeat("../").take(cx.current.len()) + .collect::(); match cache().paths.get(&it.def_id) { Some(&(ref names, _)) => { for name in names[..names.len() - 1].iter() { @@ -1382,7 +1383,8 @@ fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result { let amt = if self.ismodule() { cur.len() - 1 } else { cur.len() }; for (i, component) in cur.iter().enumerate().take(amt) { try!(write!(fmt, "{}::", - "../".repeat(cur.len() - i - 1), + repeat("../").take(cur.len() - i - 1) + .collect::(), component.as_slice())); } } diff --git a/src/librustdoc/passes.rs b/src/librustdoc/passes.rs index e368d7f93320..9a67b479106e 100644 --- a/src/librustdoc/passes.rs +++ b/src/librustdoc/passes.rs @@ -319,7 +319,7 @@ pub fn unindent(s: &str) -> String { let ignore_previous_indents = saw_first_line && !saw_second_line && - !line.is_whitespace(); + !line.chars().all(|c| c.is_whitespace()); let min_indent = if ignore_previous_indents { uint::MAX @@ -331,7 +331,7 @@ pub fn unindent(s: &str) -> String { saw_second_line = true; } - if line.is_whitespace() { + if line.chars().all(|c| c.is_whitespace()) { min_indent } else { saw_first_line = true; @@ -353,7 +353,7 @@ pub fn unindent(s: &str) -> String { if lines.len() >= 1 { let mut unindented = vec![ lines[0].trim().to_string() ]; unindented.push_all(lines.tail().iter().map(|&line| { - if line.is_whitespace() { + if line.chars().all(|c| c.is_whitespace()) { line.to_string() } else { assert!(line.len() >= min_indent); diff --git a/src/libserialize/json.rs b/src/libserialize/json.rs index 3181e28a1211..c4f071994dc9 100644 --- a/src/libserialize/json.rs +++ b/src/libserialize/json.rs @@ -202,10 +202,11 @@ use std::{char, f64, fmt, io, num, str}; use std::mem::{swap, transmute}; use std::num::{Float, FPNaN, FPInfinite, Int}; -use std::str::{FromStr, ScalarValue}; +use std::str::{FromStr}; use std::string; -use std::vec::Vec; use std::ops; +use unicode::str as unicode_str; +use unicode::str::Utf16Item; use Encodable; @@ -1001,7 +1002,7 @@ pub fn is_string<'a>(&'a self) -> bool { /// Returns None otherwise. pub fn as_string<'a>(&'a self) -> Option<&'a str> { match *self { - Json::String(ref s) => Some(s.as_slice()), + Json::String(ref s) => Some(s[]), _ => None } } @@ -1585,8 +1586,8 @@ fn parse_str(&mut self) -> Result { } let buf = [n1, try!(self.decode_hex_escape())]; - match str::utf16_items(buf.as_slice()).next() { - Some(ScalarValue(c)) => res.push(c), + match unicode_str::utf16_items(&buf).next() { + Some(Utf16Item::ScalarValue(c)) => res.push(c), _ => return self.error(LoneLeadingSurrogateInHexEscape), } } @@ -1934,7 +1935,7 @@ pub fn from_reader(rdr: &mut io::Reader) -> Result { Ok(c) => c, Err(e) => return Err(io_error_to_error(e)) }; - let s = match str::from_utf8(contents.as_slice()) { + let s = match str::from_utf8(contents.as_slice()).ok() { Some(s) => s, _ => return Err(SyntaxError(NotUtf8, 0, 0)) }; @@ -1970,7 +1971,7 @@ macro_rules! expect { ($e:expr, Null) => ({ match $e { Json::Null => Ok(()), - other => Err(ExpectedError("Null".into_string(), + other => Err(ExpectedError("Null".to_string(), format!("{}", other))) } }); @@ -1991,20 +1992,20 @@ fn $name(&mut self) -> DecodeResult<$ty> { match self.pop() { Json::I64(f) => match num::cast(f) { Some(f) => Ok(f), - None => Err(ExpectedError("Number".into_string(), format!("{}", f))), + None => Err(ExpectedError("Number".to_string(), format!("{}", f))), }, Json::U64(f) => match num::cast(f) { Some(f) => Ok(f), - None => Err(ExpectedError("Number".into_string(), format!("{}", f))), + None => Err(ExpectedError("Number".to_string(), format!("{}", f))), }, - Json::F64(f) => Err(ExpectedError("Integer".into_string(), format!("{}", f))), + Json::F64(f) => Err(ExpectedError("Integer".to_string(), format!("{}", f))), // re: #12967.. a type w/ numeric keys (ie HashMap etc) // is going to have a string here, as per JSON spec. Json::String(s) => match std::str::from_str(s.as_slice()) { Some(f) => Ok(f), - None => Err(ExpectedError("Number".into_string(), s)), + None => Err(ExpectedError("Number".to_string(), s)), }, - value => Err(ExpectedError("Number".into_string(), format!("{}", value))), + value => Err(ExpectedError("Number".to_string(), format!("{}", value))), } } } @@ -2036,13 +2037,13 @@ fn read_f64(&mut self) -> DecodeResult { Json::String(s) => { // re: #12967.. a type w/ numeric keys (ie HashMap etc) // is going to have a string here, as per JSON spec. - match std::str::from_str(s.as_slice()) { + match s.parse() { Some(f) => Ok(f), - None => Err(ExpectedError("Number".into_string(), s)), + None => Err(ExpectedError("Number".to_string(), s)), } }, Json::Null => Ok(f64::NAN), - value => Err(ExpectedError("Number".into_string(), format!("{}", value))) + value => Err(ExpectedError("Number".to_string(), format!("{}", value))) } } @@ -2060,7 +2061,7 @@ fn read_char(&mut self) -> DecodeResult { _ => () } } - Err(ExpectedError("single character string".into_string(), format!("{}", s))) + Err(ExpectedError("single character string".to_string(), format!("{}", s))) } fn read_str(&mut self) -> DecodeResult { @@ -2080,36 +2081,35 @@ fn read_enum_variant(&mut self, names: &[&str], let name = match self.pop() { Json::String(s) => s, Json::Object(mut o) => { - let n = match o.remove(&"variant".into_string()) { + let n = match o.remove(&"variant".to_string()) { Some(Json::String(s)) => s, Some(val) => { - return Err(ExpectedError("String".into_string(), format!("{}", val))) + return Err(ExpectedError("String".to_string(), format!("{}", val))) } None => { - return Err(MissingFieldError("variant".into_string())) + return Err(MissingFieldError("variant".to_string())) } }; - match o.remove(&"fields".into_string()) { + match o.remove(&"fields".to_string()) { Some(Json::Array(l)) => { for field in l.into_iter().rev() { self.stack.push(field); } }, Some(val) => { - return Err(ExpectedError("Array".into_string(), format!("{}", val))) + return Err(ExpectedError("Array".to_string(), format!("{}", val))) } None => { - return Err(MissingFieldError("fields".into_string())) + return Err(MissingFieldError("fields".to_string())) } } n } json => { - return Err(ExpectedError("String or Object".into_string(), format!("{}", json))) + return Err(ExpectedError("String or Object".to_string(), format!("{}", json))) } }; - let idx = match names.iter() - .position(|n| str::eq_slice(*n, name.as_slice())) { + let idx = match names.iter().position(|n| *n == name[]) { Some(idx) => idx, None => return Err(UnknownVariantError(name)) }; @@ -2319,7 +2319,7 @@ fn to_json(&self) -> Json { Json::Boolean(*self) } } impl ToJson for str { - fn to_json(&self) -> Json { Json::String(self.into_string()) } + fn to_json(&self) -> Json { Json::String(self.to_string()) } } impl ToJson for string::String { @@ -2450,9 +2450,9 @@ fn test_decode_option_some() { #[test] fn test_decode_option_malformed() { check_err::("{ \"opt\": [] }", - ExpectedError("Number".into_string(), "[]".into_string())); + ExpectedError("Number".to_string(), "[]".to_string())); check_err::("{ \"opt\": false }", - ExpectedError("Number".into_string(), "false".into_string())); + ExpectedError("Number".to_string(), "false".to_string())); } #[deriving(PartialEq, Encodable, Decodable, Show)] @@ -2538,11 +2538,11 @@ fn test_write_f64() { #[test] fn test_write_str() { - assert_eq!(String("".into_string()).to_string(), "\"\""); - assert_eq!(String("".into_string()).to_pretty_str(), "\"\""); + assert_eq!(String("".to_string()).to_string(), "\"\""); + assert_eq!(String("".to_string()).to_pretty_str(), "\"\""); - assert_eq!(String("homura".into_string()).to_string(), "\"homura\""); - assert_eq!(String("madoka".into_string()).to_pretty_str(), "\"madoka\""); + assert_eq!(String("homura".to_string()).to_string(), "\"homura\""); + assert_eq!(String("madoka".to_string()).to_pretty_str(), "\"madoka\""); } #[test] @@ -2571,7 +2571,7 @@ fn test_write_array() { let long_test_array = Array(vec![ Boolean(false), Null, - Array(vec![String("foo\nbar".into_string()), F64(3.5)])]); + Array(vec![String("foo\nbar".to_string()), F64(3.5)])]); assert_eq!(long_test_array.to_string(), "[false,null,[\"foo\\nbar\",3.5]]"); @@ -2596,12 +2596,12 @@ fn test_write_object() { assert_eq!( mk_object(&[ - ("a".into_string(), Boolean(true)) + ("a".to_string(), Boolean(true)) ]).to_string(), "{\"a\":true}" ); assert_eq!( - mk_object(&[("a".into_string(), Boolean(true))]).to_pretty_str(), + mk_object(&[("a".to_string(), Boolean(true))]).to_pretty_str(), "\ {\n \ \"a\": true\n\ @@ -2609,9 +2609,9 @@ fn test_write_object() { ); let complex_obj = mk_object(&[ - ("b".into_string(), Array(vec![ - mk_object(&[("c".into_string(), String("\x0c\r".into_string()))]), - mk_object(&[("d".into_string(), String("".into_string()))]) + ("b".to_string(), Array(vec![ + mk_object(&[("c".to_string(), String("\x0c\r".to_string()))]), + mk_object(&[("d".to_string(), String("".to_string()))]) ])) ]); @@ -2640,10 +2640,10 @@ fn test_write_object() { ); let a = mk_object(&[ - ("a".into_string(), Boolean(true)), - ("b".into_string(), Array(vec![ - mk_object(&[("c".into_string(), String("\x0c\r".into_string()))]), - mk_object(&[("d".into_string(), String("".into_string()))]) + ("a".to_string(), Boolean(true)), + ("b".to_string(), Array(vec![ + mk_object(&[("c".to_string(), String("\x0c\r".to_string()))]), + mk_object(&[("d".to_string(), String("".to_string()))]) ])) ]); @@ -2678,7 +2678,7 @@ fn test_write_enum() { "\"Dog\"" ); - let animal = Frog("Henry".into_string(), 349); + let animal = Frog("Henry".to_string(), 349); assert_eq!( with_str_writer(|writer| { let mut encoder = Encoder::new(writer); @@ -2731,7 +2731,7 @@ fn test_write_none() { fn test_write_char() { check_encoder_for_simple!('a', "\"a\""); check_encoder_for_simple!('\t', "\"\\t\""); - check_encoder_for_simple!('\u{00a0}', "\"\u{00a0}\""); + check_encoder_for_simple!('\u{a0}', "\"\u{a0}\""); check_encoder_for_simple!('\u{abcd}', "\"\u{abcd}\""); check_encoder_for_simple!('\u{10ffff}', "\"\u{10ffff}\""); } @@ -2839,7 +2839,7 @@ fn test_decode_numbers() { assert_eq!(v, i64::MAX); let res: DecodeResult = super::decode("765.25252"); - assert_eq!(res, Err(ExpectedError("Integer".into_string(), "765.25252".into_string()))); + assert_eq!(res, Err(ExpectedError("Integer".to_string(), "765.25252".to_string()))); } #[test] @@ -2847,16 +2847,16 @@ fn test_read_str() { assert_eq!(from_str("\""), Err(SyntaxError(EOFWhileParsingString, 1, 2))); assert_eq!(from_str("\"lol"), Err(SyntaxError(EOFWhileParsingString, 1, 5))); - assert_eq!(from_str("\"\""), Ok(String("".into_string()))); - assert_eq!(from_str("\"foo\""), Ok(String("foo".into_string()))); - assert_eq!(from_str("\"\\\"\""), Ok(String("\"".into_string()))); - assert_eq!(from_str("\"\\b\""), Ok(String("\x08".into_string()))); - assert_eq!(from_str("\"\\n\""), Ok(String("\n".into_string()))); - assert_eq!(from_str("\"\\r\""), Ok(String("\r".into_string()))); - assert_eq!(from_str("\"\\t\""), Ok(String("\t".into_string()))); - assert_eq!(from_str(" \"foo\" "), Ok(String("foo".into_string()))); - assert_eq!(from_str("\"\\u12ab\""), Ok(String("\u{12ab}".into_string()))); - assert_eq!(from_str("\"\\uAB12\""), Ok(String("\u{AB12}".into_string()))); + assert_eq!(from_str("\"\""), Ok(String("".to_string()))); + assert_eq!(from_str("\"foo\""), Ok(String("foo".to_string()))); + assert_eq!(from_str("\"\\\"\""), Ok(String("\"".to_string()))); + assert_eq!(from_str("\"\\b\""), Ok(String("\x08".to_string()))); + assert_eq!(from_str("\"\\n\""), Ok(String("\n".to_string()))); + assert_eq!(from_str("\"\\r\""), Ok(String("\r".to_string()))); + assert_eq!(from_str("\"\\t\""), Ok(String("\t".to_string()))); + assert_eq!(from_str(" \"foo\" "), Ok(String("foo".to_string()))); + assert_eq!(from_str("\"\\u12ab\""), Ok(String("\u{12ab}".to_string()))); + assert_eq!(from_str("\"\\uAB12\""), Ok(String("\u{AB12}".to_string()))); } #[test] @@ -2922,7 +2922,7 @@ fn test_decode_tuple() { assert_eq!(t, (1u, 2, 3)); let t: (uint, string::String) = super::decode("[1, \"two\"]").unwrap(); - assert_eq!(t, (1u, "two".into_string())); + assert_eq!(t, (1u, "two".to_string())); } #[test] @@ -2952,22 +2952,22 @@ fn test_read_object() { assert_eq!(from_str("{}").unwrap(), mk_object(&[])); assert_eq!(from_str("{\"a\": 3}").unwrap(), - mk_object(&[("a".into_string(), U64(3))])); + mk_object(&[("a".to_string(), U64(3))])); assert_eq!(from_str( "{ \"a\": null, \"b\" : true }").unwrap(), mk_object(&[ - ("a".into_string(), Null), - ("b".into_string(), Boolean(true))])); + ("a".to_string(), Null), + ("b".to_string(), Boolean(true))])); assert_eq!(from_str("\n{ \"a\": null, \"b\" : true }\n").unwrap(), mk_object(&[ - ("a".into_string(), Null), - ("b".into_string(), Boolean(true))])); + ("a".to_string(), Null), + ("b".to_string(), Boolean(true))])); assert_eq!(from_str( "{\"a\" : 1.0 ,\"b\": [ true ]}").unwrap(), mk_object(&[ - ("a".into_string(), F64(1.0)), - ("b".into_string(), Array(vec![Boolean(true)])) + ("a".to_string(), F64(1.0)), + ("b".to_string(), Array(vec![Boolean(true)])) ])); assert_eq!(from_str( "{\ @@ -2979,12 +2979,12 @@ fn test_read_object() { ]\ }").unwrap(), mk_object(&[ - ("a".into_string(), F64(1.0)), - ("b".into_string(), Array(vec![ + ("a".to_string(), F64(1.0)), + ("b".to_string(), Array(vec![ Boolean(true), - String("foo\nbar".into_string()), + String("foo\nbar".to_string()), mk_object(&[ - ("c".into_string(), mk_object(&[("d".into_string(), Null)])) + ("c".to_string(), mk_object(&[("d".to_string(), Null)])) ]) ])) ])); @@ -3003,7 +3003,7 @@ fn test_decode_struct() { v, Outer { inner: vec![ - Inner { a: (), b: 2, c: vec!["abc".into_string(), "xyz".into_string()] } + Inner { a: (), b: 2, c: vec!["abc".to_string(), "xyz".to_string()] } ] } ); @@ -3029,7 +3029,7 @@ fn test_decode_option() { assert_eq!(value, None); let value: Option = super::decode("\"jodhpurs\"").unwrap(); - assert_eq!(value, Some("jodhpurs".into_string())); + assert_eq!(value, Some("jodhpurs".to_string())); } #[test] @@ -3039,7 +3039,7 @@ fn test_decode_enum() { let s = "{\"variant\":\"Frog\",\"fields\":[\"Henry\",349]}"; let value: Animal = super::decode(s).unwrap(); - assert_eq!(value, Frog("Henry".into_string(), 349)); + assert_eq!(value, Frog("Henry".to_string(), 349)); } #[test] @@ -3048,8 +3048,8 @@ fn test_decode_map() { \"fields\":[\"Henry\", 349]}}"; let mut map: BTreeMap = super::decode(s).unwrap(); - assert_eq!(map.remove(&"a".into_string()), Some(Dog)); - assert_eq!(map.remove(&"b".into_string()), Some(Frog("Henry".into_string(), 349))); + assert_eq!(map.remove(&"a".to_string()), Some(Dog)); + assert_eq!(map.remove(&"b".to_string()), Some(Frog("Henry".to_string(), 349))); } #[test] @@ -3089,30 +3089,30 @@ fn check_err>(to_parse: &'static str, } #[test] fn test_decode_errors_struct() { - check_err::("[]", ExpectedError("Object".into_string(), "[]".into_string())); + check_err::("[]", ExpectedError("Object".to_string(), "[]".to_string())); check_err::("{\"x\": true, \"y\": true, \"z\": \"\", \"w\": []}", - ExpectedError("Number".into_string(), "true".into_string())); + ExpectedError("Number".to_string(), "true".to_string())); check_err::("{\"x\": 1, \"y\": [], \"z\": \"\", \"w\": []}", - ExpectedError("Boolean".into_string(), "[]".into_string())); + ExpectedError("Boolean".to_string(), "[]".to_string())); check_err::("{\"x\": 1, \"y\": true, \"z\": {}, \"w\": []}", - ExpectedError("String".into_string(), "{}".into_string())); + ExpectedError("String".to_string(), "{}".to_string())); check_err::("{\"x\": 1, \"y\": true, \"z\": \"\", \"w\": null}", - ExpectedError("Array".into_string(), "null".into_string())); + ExpectedError("Array".to_string(), "null".to_string())); check_err::("{\"x\": 1, \"y\": true, \"z\": \"\"}", - MissingFieldError("w".into_string())); + MissingFieldError("w".to_string())); } #[test] fn test_decode_errors_enum() { check_err::("{}", - MissingFieldError("variant".into_string())); + MissingFieldError("variant".to_string())); check_err::("{\"variant\": 1}", - ExpectedError("String".into_string(), "1".into_string())); + ExpectedError("String".to_string(), "1".to_string())); check_err::("{\"variant\": \"A\"}", - MissingFieldError("fields".into_string())); + MissingFieldError("fields".to_string())); check_err::("{\"variant\": \"A\", \"fields\": null}", - ExpectedError("Array".into_string(), "null".into_string())); + ExpectedError("Array".to_string(), "null".to_string())); check_err::("{\"variant\": \"C\", \"fields\": []}", - UnknownVariantError("C".into_string())); + UnknownVariantError("C".to_string())); } #[test] @@ -3325,15 +3325,15 @@ fn test_prettyencoder_indent_level_param() { let mut tree = BTreeMap::new(); - tree.insert("hello".into_string(), String("guten tag".into_string())); - tree.insert("goodbye".into_string(), String("sayonara".into_string())); + tree.insert("hello".to_string(), String("guten tag".to_string())); + tree.insert("goodbye".to_string(), String("sayonara".to_string())); let json = Array( // The following layout below should look a lot like // the pretty-printed JSON (indent * x) vec! ( // 0x - String("greetings".into_string()), // 1x + String("greetings".to_string()), // 1x Object(tree), // 1x + 2x + 2x + 1x ) // 0x // End JSON array (7 lines) @@ -3397,7 +3397,7 @@ fn test_hashmap_with_numeric_key_will_error_with_string_keys() { }; let mut decoder = Decoder::new(json_obj); let result: Result, DecoderError> = Decodable::decode(&mut decoder); - assert_eq!(result, Err(ExpectedError("Number".into_string(), "a".into_string()))); + assert_eq!(result, Err(ExpectedError("Number".to_string(), "a".to_string()))); } fn assert_stream_equal(src: &str, @@ -3424,7 +3424,7 @@ fn test_streaming_parser() { r#"{ "foo":"bar", "array" : [0, 1, 2, 3, 4, 5], "idents":[null,true,false]}"#, vec![ (ObjectStart, vec![]), - (StringValue("bar".into_string()), vec![Key("foo")]), + (StringValue("bar".to_string()), vec![Key("foo")]), (ArrayStart, vec![Key("array")]), (U64Value(0), vec![Key("array"), Index(0)]), (U64Value(1), vec![Key("array"), Index(1)]), @@ -3515,7 +3515,7 @@ fn test_read_object_streaming() { (F64Value(1.0), vec![Key("a")]), (ArrayStart, vec![Key("b")]), (BooleanValue(true), vec![Key("b"), Index(0)]), - (StringValue("foo\nbar".into_string()), vec![Key("b"), Index(1)]), + (StringValue("foo\nbar".to_string()), vec![Key("b"), Index(1)]), (ObjectStart, vec![Key("b"), Index(2)]), (ObjectStart, vec![Key("b"), Index(2), Key("c")]), (NullValue, vec![Key("b"), Index(2), Key("c"), Key("d")]), @@ -3648,7 +3648,7 @@ fn test_stack() { assert!(stack.last_is_index()); assert!(stack.get(0) == Index(1)); - stack.push_key("foo".into_string()); + stack.push_key("foo".to_string()); assert!(stack.len() == 2); assert!(stack.is_equal_to(&[Index(1), Key("foo")])); @@ -3660,7 +3660,7 @@ fn test_stack() { assert!(stack.get(0) == Index(1)); assert!(stack.get(1) == Key("foo")); - stack.push_key("bar".into_string()); + stack.push_key("bar".to_string()); assert!(stack.len() == 3); assert!(stack.is_equal_to(&[Index(1), Key("foo"), Key("bar")])); @@ -3721,8 +3721,8 @@ fn test_to_json() { assert_eq!(f64::NAN.to_json(), Null); assert_eq!(true.to_json(), Boolean(true)); assert_eq!(false.to_json(), Boolean(false)); - assert_eq!("abc".to_json(), String("abc".into_string())); - assert_eq!("abc".into_string().to_json(), String("abc".into_string())); + assert_eq!("abc".to_json(), String("abc".to_string())); + assert_eq!("abc".to_string().to_json(), String("abc".to_string())); assert_eq!((1u, 2u).to_json(), array2); assert_eq!((1u, 2u, 3u).to_json(), array3); assert_eq!([1u, 2].to_json(), array2); @@ -3734,8 +3734,8 @@ fn test_to_json() { tree_map.insert("b".into_string(), 2); assert_eq!(tree_map.to_json(), object); let mut hash_map = HashMap::new(); - hash_map.insert("a".into_string(), 1u); - hash_map.insert("b".into_string(), 2); + hash_map.insert("a".to_string(), 1u); + hash_map.insert("b".to_string(), 2); assert_eq!(hash_map.to_json(), object); assert_eq!(Some(15i).to_json(), I64(15)); assert_eq!(Some(15u).to_json(), U64(15)); @@ -3778,7 +3778,7 @@ fn bench_small(b: &mut Bencher) { } fn big_json() -> string::String { - let mut src = "[\n".into_string(); + let mut src = "[\n".to_string(); for _ in range(0i, 500) { src.push_str(r#"{ "a": true, "b": null, "c":3.1415, "d": "Hello world", "e": \ [1,2,3]},"#); diff --git a/src/libserialize/lib.rs b/src/libserialize/lib.rs index e700d102fefd..fdbc5051f728 100644 --- a/src/libserialize/lib.rs +++ b/src/libserialize/lib.rs @@ -32,6 +32,7 @@ #[phase(plugin, link)] extern crate log; +extern crate unicode; extern crate collections; diff --git a/src/libserialize/serialize.rs b/src/libserialize/serialize.rs index 00c5158309e9..558f9e603e15 100644 --- a/src/libserialize/serialize.rs +++ b/src/libserialize/serialize.rs @@ -308,13 +308,13 @@ fn encode(&self, s: &mut S) -> Result<(), E> { impl> Encodable for String { fn encode(&self, s: &mut S) -> Result<(), E> { - s.emit_str(self.as_slice()) + s.emit_str(self[]) } } impl> Decodable for String { fn decode(d: &mut D) -> Result { - Ok(String::from_str(try!(d.read_str()).as_slice())) + d.read_str() } } diff --git a/src/libstd/ascii.rs b/src/libstd/ascii.rs index 08b17f25e29d..2c49beca98de 100644 --- a/src/libstd/ascii.rs +++ b/src/libstd/ascii.rs @@ -23,7 +23,7 @@ use option::Option; use option::Option::{Some, None}; use slice::{SliceExt, AsSlice}; -use str::{Str, StrPrelude}; +use str::{Str, StrExt}; use string::{String, IntoString}; use vec::Vec; diff --git a/src/libstd/c_str.rs b/src/libstd/c_str.rs index f1c8e8950a22..fb44961017fc 100644 --- a/src/libstd/c_str.rs +++ b/src/libstd/c_str.rs @@ -228,7 +228,7 @@ pub fn as_bytes_no_nul<'a>(&'a self) -> &'a [u8] { #[inline] pub fn as_str<'a>(&'a self) -> Option<&'a str> { let buf = self.as_bytes_no_nul(); - str::from_utf8(buf) + str::from_utf8(buf).ok() } /// Return a CString iterator. diff --git a/src/libstd/dynamic_lib.rs b/src/libstd/dynamic_lib.rs index 4d8c7d67b8c2..368abe7cb124 100644 --- a/src/libstd/dynamic_lib.rs +++ b/src/libstd/dynamic_lib.rs @@ -280,7 +280,7 @@ pub mod dl { use result::Result; use result::Result::{Ok, Err}; use slice::SliceExt; - use str::StrPrelude; + use str::StrExt; use str; use string::String; use vec::Vec; diff --git a/src/libstd/failure.rs b/src/libstd/failure.rs index 8e1e3dc4af92..7010eae6dba0 100644 --- a/src/libstd/failure.rs +++ b/src/libstd/failure.rs @@ -41,7 +41,7 @@ pub fn on_fail(obj: &(Any+Send), file: &'static str, line: uint) { let msg = match obj.downcast_ref::<&'static str>() { Some(s) => *s, None => match obj.downcast_ref::() { - Some(s) => s.as_slice(), + Some(s) => s[], None => "Box", } }; diff --git a/src/libstd/io/mod.rs b/src/libstd/io/mod.rs index dbf61b132e08..233ad7810938 100644 --- a/src/libstd/io/mod.rs +++ b/src/libstd/io/mod.rs @@ -242,10 +242,11 @@ use result::Result::{Ok, Err}; use sys; use slice::SliceExt; -use str::StrPrelude; +use str::StrExt; use str; use string::String; use uint; +use unicode; use unicode::char::UnicodeChar; use vec::Vec; @@ -1505,7 +1506,7 @@ fn read_until(&mut self, byte: u8) -> IoResult> { /// valid utf-8 encoded codepoint as the next few bytes in the stream. fn read_char(&mut self) -> IoResult { let first_byte = try!(self.read_byte()); - let width = str::utf8_char_width(first_byte); + let width = unicode::str::utf8_char_width(first_byte); if width == 1 { return Ok(first_byte as char) } if width == 0 { return Err(standard_error(InvalidInput)) } // not utf8 let mut buf = [first_byte, 0, 0, 0]; @@ -1519,7 +1520,7 @@ fn read_char(&mut self) -> IoResult { } } } - match str::from_utf8(buf[..width]) { + match str::from_utf8(buf[..width]).ok() { Some(s) => Ok(s.char_at(0)), None => Err(standard_error(InvalidInput)) } diff --git a/src/libstd/io/net/ip.rs b/src/libstd/io/net/ip.rs index 71776b6c46af..89a649d55bdc 100644 --- a/src/libstd/io/net/ip.rs +++ b/src/libstd/io/net/ip.rs @@ -25,8 +25,8 @@ use option::Option; use option::Option::{None, Some}; use result::Result::{Ok, Err}; -use str::{FromStr, StrPrelude}; use slice::{CloneSliceExt, SliceExt}; +use str::{FromStr, StrExt}; use vec::Vec; pub type Port = u16; diff --git a/src/libstd/io/process.rs b/src/libstd/io/process.rs index 9da1117f2272..4a0a39364243 100644 --- a/src/libstd/io/process.rs +++ b/src/libstd/io/process.rs @@ -1082,7 +1082,7 @@ fn test_override_env() { let prog = env_cmd().env_set_all(new_env.as_slice()).spawn().unwrap(); let result = prog.wait_with_output().unwrap(); - let output = String::from_utf8_lossy(result.output.as_slice()).into_string(); + let output = String::from_utf8_lossy(result.output.as_slice()).to_string(); assert!(output.contains("RUN_TEST_NEW_ENV=123"), "didn't find RUN_TEST_NEW_ENV inside of:\n\n{}", output); @@ -1092,7 +1092,7 @@ fn test_override_env() { fn test_add_to_env() { let prog = env_cmd().env("RUN_TEST_NEW_ENV", "123").spawn().unwrap(); let result = prog.wait_with_output().unwrap(); - let output = String::from_utf8_lossy(result.output.as_slice()).into_string(); + let output = String::from_utf8_lossy(result.output.as_slice()).to_string(); assert!(output.contains("RUN_TEST_NEW_ENV=123"), "didn't find RUN_TEST_NEW_ENV inside of:\n\n{}", output); diff --git a/src/libstd/io/stdio.rs b/src/libstd/io/stdio.rs index 36dd54923569..1c5ceaf24506 100644 --- a/src/libstd/io/stdio.rs +++ b/src/libstd/io/stdio.rs @@ -43,7 +43,7 @@ use result::Result::{Ok, Err}; use rt; use slice::SliceExt; -use str::StrPrelude; +use str::StrExt; use string::String; use sys::{fs, tty}; use sync::{Arc, Mutex, MutexGuard, Once, ONCE_INIT}; diff --git a/src/libstd/num/strconv.rs b/src/libstd/num/strconv.rs index b3e4dd52f89c..d6331f3c718a 100644 --- a/src/libstd/num/strconv.rs +++ b/src/libstd/num/strconv.rs @@ -20,7 +20,7 @@ use num::{mod, Int, Float, FPNaN, FPInfinite, ToPrimitive}; use ops::FnMut; use slice::{SliceExt, CloneSliceExt}; -use str::StrPrelude; +use str::StrExt; use string::String; use vec::Vec; diff --git a/src/libstd/os.rs b/src/libstd/os.rs index a16ee982f5c0..ceb9a4102f63 100644 --- a/src/libstd/os.rs +++ b/src/libstd/os.rs @@ -52,7 +52,7 @@ use result::Result::{Err, Ok}; use slice::{AsSlice, SliceExt}; use slice::CloneSliceExt; -use str::{Str, StrPrelude, StrAllocating}; +use str::{Str, StrExt}; use string::{String, ToString}; use sync::atomic::{AtomicInt, INIT_ATOMIC_INT, SeqCst}; use vec::Vec; @@ -134,8 +134,8 @@ fn with_env_lock(f: F) -> T where /// ``` pub fn env() -> Vec<(String,String)> { env_as_bytes().into_iter().map(|(k,v)| { - let k = String::from_utf8_lossy(k.as_slice()).into_string(); - let v = String::from_utf8_lossy(v.as_slice()).into_string(); + let k = String::from_utf8_lossy(k.as_slice()).into_owned(); + let v = String::from_utf8_lossy(v.as_slice()).into_owned(); (k,v) }).collect() } @@ -185,7 +185,7 @@ fn env_convert(input: Vec>) -> Vec<(Vec, Vec)> { /// } /// ``` pub fn getenv(n: &str) -> Option { - getenv_as_bytes(n).map(|v| String::from_utf8_lossy(v.as_slice()).into_string()) + getenv_as_bytes(n).map(|v| String::from_utf8_lossy(v.as_slice()).into_owned()) } #[cfg(unix)] @@ -707,7 +707,7 @@ fn real_args_as_bytes() -> Vec> { fn real_args() -> Vec { real_args_as_bytes().into_iter() .map(|v| { - String::from_utf8_lossy(v.as_slice()).into_string() + String::from_utf8_lossy(v.as_slice()).into_owned() }).collect() } @@ -729,7 +729,7 @@ fn real_args() -> Vec { // Push it onto the list. let ptr = ptr as *const u16; let buf = slice::from_raw_buf(&ptr, len); - let opt_s = String::from_utf16(os_imp::truncate_utf16_at_nul(buf)); + let opt_s = String::from_utf16(sys::os::truncate_utf16_at_nul(buf)); opt_s.expect("CommandLineToArgvW returned invalid UTF-16") }); diff --git a/src/libstd/path/mod.rs b/src/libstd/path/mod.rs index ed4bb6ee0811..30f3f56bc1c1 100644 --- a/src/libstd/path/mod.rs +++ b/src/libstd/path/mod.rs @@ -69,7 +69,7 @@ use option::Option; use option::Option::{None, Some}; use str; -use str::{CowString, MaybeOwned, Str, StrPrelude}; +use str::{CowString, MaybeOwned, Str, StrExt}; use string::String; use slice::{AsSlice, CloneSliceExt}; use slice::{PartialEqSliceExt, SliceExt}; @@ -197,7 +197,7 @@ fn new_opt(path: T) -> Option { /// ``` #[inline] fn as_str<'a>(&'a self) -> Option<&'a str> { - str::from_utf8(self.as_vec()) + str::from_utf8(self.as_vec()).ok() } /// Returns the path as a byte vector @@ -293,7 +293,7 @@ fn filename_display<'a>(&'a self) -> Display<'a, Self> { /// ``` #[inline] fn dirname_str<'a>(&'a self) -> Option<&'a str> { - str::from_utf8(self.dirname()) + str::from_utf8(self.dirname()).ok() } /// Returns the file component of `self`, as a byte vector. @@ -327,7 +327,7 @@ fn dirname_str<'a>(&'a self) -> Option<&'a str> { /// ``` #[inline] fn filename_str<'a>(&'a self) -> Option<&'a str> { - self.filename().and_then(str::from_utf8) + self.filename().and_then(|s| str::from_utf8(s).ok()) } /// Returns the stem of the filename of `self`, as a byte vector. @@ -373,7 +373,7 @@ fn filestem<'a>(&'a self) -> Option<&'a [u8]> { /// ``` #[inline] fn filestem_str<'a>(&'a self) -> Option<&'a str> { - self.filestem().and_then(str::from_utf8) + self.filestem().and_then(|s| str::from_utf8(s).ok()) } /// Returns the extension of the filename of `self`, as an optional byte vector. @@ -420,7 +420,7 @@ fn extension<'a>(&'a self) -> Option<&'a [u8]> { /// ``` #[inline] fn extension_str<'a>(&'a self) -> Option<&'a str> { - self.extension().and_then(str::from_utf8) + self.extension().and_then(|s| str::from_utf8(s).ok()) } /// Replaces the filename portion of the path with the given byte vector or string. @@ -793,7 +793,7 @@ pub trait BytesContainer for Sized? { /// Returns the receiver interpreted as a utf-8 string, if possible #[inline] fn container_as_str<'a>(&'a self) -> Option<&'a str> { - str::from_utf8(self.container_as_bytes()) + str::from_utf8(self.container_as_bytes()).ok() } /// Returns whether .container_as_str() is guaranteed to not fail // FIXME (#8888): Remove unused arg once :: works @@ -870,7 +870,7 @@ fn container_as_bytes(&self) -> &[u8] { } #[inline] fn container_as_str(&self) -> Option<&str> { - Some(self.as_slice()) + Some(self[]) } #[inline] fn is_str(_: Option<&String>) -> bool { true } @@ -886,7 +886,7 @@ fn container_as_bytes(&self) -> &[u8] { impl BytesContainer for Vec { #[inline] fn container_as_bytes(&self) -> &[u8] { - self.as_slice() + self[] } } @@ -897,6 +897,7 @@ fn container_as_bytes<'a>(&'a self) -> &'a [u8] { } } +#[allow(deprecated)] impl<'a> BytesContainer for str::MaybeOwned<'a> { #[inline] fn container_as_bytes<'b>(&'b self) -> &'b [u8] { diff --git a/src/libstd/path/posix.rs b/src/libstd/path/posix.rs index 88907951673d..a514837492af 100644 --- a/src/libstd/path/posix.rs +++ b/src/libstd/path/posix.rs @@ -401,7 +401,10 @@ pub fn components<'a>(&'a self) -> Components<'a> { /// Returns an iterator that yields each component of the path as Option<&str>. /// See components() for details. pub fn str_components<'a>(&'a self) -> StrComponents<'a> { - self.components().map(str::from_utf8) + fn from_utf8(s: &[u8]) -> Option<&str> { + str::from_utf8(s).ok() + } + self.components().map(from_utf8) } } diff --git a/src/libstd/path/windows.rs b/src/libstd/path/windows.rs index c2c17103554c..277c675c22d6 100644 --- a/src/libstd/path/windows.rs +++ b/src/libstd/path/windows.rs @@ -25,9 +25,9 @@ use mem; use option::Option; use option::Option::{Some, None}; -use slice::{AsSlice, SliceExt}; -use str::{CharSplits, FromStr, Str, StrAllocating, StrVector, StrPrelude}; -use string::String; +use slice::SliceExt; +use str::{CharSplits, FromStr, StrVector, StrExt}; +use string::{String, ToString}; use unicode::char::UnicodeChar; use vec::Vec; @@ -187,30 +187,30 @@ unsafe fn set_filename_unchecked(&mut self, filename: T) { s.push_str(".."); s.push(SEP); s.push_str(filename); - self.update_normalized(s); + self.update_normalized(s[]); } None => { self.update_normalized(filename); } - Some((_,idxa,end)) if self.repr.slice(idxa,end) == ".." => { + Some((_,idxa,end)) if self.repr[idxa..end] == ".." => { let mut s = String::with_capacity(end + 1 + filename.len()); - s.push_str(self.repr.slice_to(end)); + s.push_str(self.repr[0..end]); s.push(SEP); s.push_str(filename); - self.update_normalized(s); + self.update_normalized(s[]); } Some((idxb,idxa,_)) if self.prefix == Some(DiskPrefix) && idxa == self.prefix_len() => { let mut s = String::with_capacity(idxb + filename.len()); - s.push_str(self.repr.slice_to(idxb)); + s.push_str(self.repr[0..idxb]); s.push_str(filename); - self.update_normalized(s); + self.update_normalized(s[]); } Some((idxb,_,_)) => { let mut s = String::with_capacity(idxb + 1 + filename.len()); - s.push_str(self.repr.slice_to(idxb)); + s.push_str(self.repr[0..idxb]); s.push(SEP); s.push_str(filename); - self.update_normalized(s); + self.update_normalized(s[]); } } } @@ -229,12 +229,12 @@ unsafe fn push_unchecked(&mut self, path: T) { let path = path.container_as_str().unwrap(); fn is_vol_abs(path: &str, prefix: Option) -> bool { // assume prefix is Some(DiskPrefix) - let rest = path.slice_from(prefix_len(prefix)); + let rest = path[prefix_len(prefix)..]; !rest.is_empty() && rest.as_bytes()[0].is_ascii() && is_sep(rest.as_bytes()[0] as char) } fn shares_volume(me: &Path, path: &str) -> bool { // path is assumed to have a prefix of Some(DiskPrefix) - let repr = me.repr.as_slice(); + let repr = me.repr[]; match me.prefix { Some(DiskPrefix) => { repr.as_bytes()[0] == path.as_bytes()[0].to_ascii().to_uppercase().as_byte() @@ -266,7 +266,7 @@ fn append_path(me: &mut Path, path: &str) { else { None }; let pathlen = path_.as_ref().map_or(path.len(), |p| p.len()); let mut s = String::with_capacity(me.repr.len() + 1 + pathlen); - s.push_str(me.repr.as_slice()); + s.push_str(me.repr[]); let plen = me.prefix_len(); // if me is "C:" we don't want to add a path separator match me.prefix { @@ -278,9 +278,9 @@ fn append_path(me: &mut Path, path: &str) { } match path_ { None => s.push_str(path), - Some(p) => s.push_str(p.as_slice()) + Some(p) => s.push_str(p[]), }; - me.update_normalized(s) + me.update_normalized(s[]) } if !path.is_empty() { @@ -288,7 +288,7 @@ fn append_path(me: &mut Path, path: &str) { match prefix { Some(DiskPrefix) if !is_vol_abs(path, prefix) && shares_volume(self, path) => { // cwd-relative path, self is on the same volume - append_path(self, path.slice_from(prefix_len(prefix))); + append_path(self, path[prefix_len(prefix)..]); } Some(_) => { // absolute path, or cwd-relative and self is not same volume @@ -334,7 +334,7 @@ fn new_opt(path: T) -> Option { /// Always returns a `Some` value. #[inline] fn as_str<'a>(&'a self) -> Option<&'a str> { - Some(self.repr.as_slice()) + Some(self.repr[]) } #[inline] @@ -356,21 +356,17 @@ fn dirname<'a>(&'a self) -> &'a [u8] { /// Always returns a `Some` value. fn dirname_str<'a>(&'a self) -> Option<&'a str> { Some(match self.sepidx_or_prefix_len() { - None if ".." == self.repr => self.repr.as_slice(), + None if ".." == self.repr => self.repr[], None => ".", - Some((_,idxa,end)) if self.repr.slice(idxa, end) == ".." => { - self.repr.as_slice() - } - Some((idxb,_,end)) if self.repr.slice(idxb, end) == "\\" => { - self.repr.as_slice() - } - Some((0,idxa,_)) => self.repr.slice_to(idxa), + Some((_,idxa,end)) if self.repr[idxa..end] == ".." => self.repr[], + Some((idxb,_,end)) if self.repr[idxb..end] == "\\" => self.repr[], + Some((0,idxa,_)) => self.repr[0..idxa], Some((idxb,idxa,_)) => { match self.prefix { Some(DiskPrefix) | Some(VerbatimDiskPrefix) if idxb == self.prefix_len() => { - self.repr.slice_to(idxa) + self.repr[0..idxa] } - _ => self.repr.slice_to(idxb) + _ => self.repr[0..idxb] } } }) @@ -384,13 +380,13 @@ fn filename<'a>(&'a self) -> Option<&'a [u8]> { /// See `GenericPath::filename_str` for info. /// Always returns a `Some` value if `filename` returns a `Some` value. fn filename_str<'a>(&'a self) -> Option<&'a str> { - let repr = self.repr.as_slice(); + let repr = self.repr[]; match self.sepidx_or_prefix_len() { None if "." == repr || ".." == repr => None, None => Some(repr), - Some((_,idxa,end)) if repr.slice(idxa, end) == ".." => None, + Some((_,idxa,end)) if repr[idxa..end] == ".." => None, Some((_,idxa,end)) if idxa == end => None, - Some((_,idxa,end)) => Some(repr.slice(idxa, end)) + Some((_,idxa,end)) => Some(repr[idxa..end]) } } @@ -422,7 +418,7 @@ fn pop(&mut self) -> bool { true } Some((idxb,idxa,end)) if idxb == idxa && idxb == end => false, - Some((idxb,_,end)) if self.repr.slice(idxb, end) == "\\" => false, + Some((idxb,_,end)) if self.repr[idxb..end] == "\\" => false, Some((idxb,idxa,_)) => { let trunc = match self.prefix { Some(DiskPrefix) | Some(VerbatimDiskPrefix) | None => { @@ -442,15 +438,15 @@ fn root_path(&self) -> Option { if self.prefix.is_some() { Some(Path::new(match self.prefix { Some(DiskPrefix) if self.is_absolute() => { - self.repr.slice_to(self.prefix_len()+1) + self.repr[0..self.prefix_len()+1] } Some(VerbatimDiskPrefix) => { - self.repr.slice_to(self.prefix_len()+1) + self.repr[0..self.prefix_len()+1] } - _ => self.repr.slice_to(self.prefix_len()) + _ => self.repr[0..self.prefix_len()] })) } else if is_vol_relative(self) { - Some(Path::new(self.repr.slice_to(1))) + Some(Path::new(self.repr[0..1])) } else { None } @@ -469,7 +465,7 @@ fn root_path(&self) -> Option { fn is_absolute(&self) -> bool { match self.prefix { Some(DiskPrefix) => { - let rest = self.repr.slice_from(self.prefix_len()); + let rest = self.repr[self.prefix_len()..]; rest.len() > 0 && rest.as_bytes()[0] == SEP_BYTE } Some(_) => true, @@ -644,15 +640,15 @@ pub fn new_opt(path: T) -> Option { /// Does not distinguish between absolute and cwd-relative paths, e.g. /// C:\foo and C:foo. pub fn str_components<'a>(&'a self) -> StrComponents<'a> { - let repr = self.repr.as_slice(); + let repr = self.repr[]; let s = match self.prefix { Some(_) => { let plen = self.prefix_len(); if repr.len() > plen && repr.as_bytes()[plen] == SEP_BYTE { - repr.slice_from(plen+1) - } else { repr.slice_from(plen) } + repr[plen+1..] + } else { repr[plen..] } } - None if repr.as_bytes()[0] == SEP_BYTE => repr.slice_from(1), + None if repr.as_bytes()[0] == SEP_BYTE => repr[1..], None => repr }; let ret = s.split_terminator(SEP).map(Some); @@ -670,8 +666,8 @@ fn convert<'a>(x: Option<&'a str>) -> &'a [u8] { } fn equiv_prefix(&self, other: &Path) -> bool { - let s_repr = self.repr.as_slice(); - let o_repr = other.repr.as_slice(); + let s_repr = self.repr[]; + let o_repr = other.repr[]; match (self.prefix, other.prefix) { (Some(DiskPrefix), Some(VerbatimDiskPrefix)) => { self.is_absolute() && @@ -688,28 +684,28 @@ fn equiv_prefix(&self, other: &Path) -> bool { o_repr.as_bytes()[4].to_ascii().to_lowercase() } (Some(UNCPrefix(_,_)), Some(VerbatimUNCPrefix(_,_))) => { - s_repr.slice(2, self.prefix_len()) == o_repr.slice(8, other.prefix_len()) + s_repr[2..self.prefix_len()] == o_repr[8..other.prefix_len()] } (Some(VerbatimUNCPrefix(_,_)), Some(UNCPrefix(_,_))) => { - s_repr.slice(8, self.prefix_len()) == o_repr.slice(2, other.prefix_len()) + s_repr[8..self.prefix_len()] == o_repr[2..other.prefix_len()] } (None, None) => true, (a, b) if a == b => { - s_repr.slice_to(self.prefix_len()) == o_repr.slice_to(other.prefix_len()) + s_repr[0..self.prefix_len()] == o_repr[0..other.prefix_len()] } _ => false } } - fn normalize_(s: S) -> (Option, String) { + fn normalize_(s: &str) -> (Option, String) { // make borrowck happy let (prefix, val) = { - let prefix = parse_prefix(s.as_slice()); - let path = Path::normalize__(s.as_slice(), prefix); + let prefix = parse_prefix(s); + let path = Path::normalize__(s, prefix); (prefix, path) }; (prefix, match val { - None => s.into_string(), + None => s.to_string(), Some(val) => val }) } @@ -749,7 +745,7 @@ fn normalize__(s: &str, prefix: Option) -> Option { match prefix.unwrap() { DiskPrefix => { let len = prefix_len(prefix) + is_abs as uint; - let mut s = String::from_str(s.slice_to(len)); + let mut s = String::from_str(s[0..len]); unsafe { let v = s.as_mut_vec(); v[0] = (*v)[0].to_ascii().to_uppercase().as_byte(); @@ -764,7 +760,7 @@ fn normalize__(s: &str, prefix: Option) -> Option { } VerbatimDiskPrefix => { let len = prefix_len(prefix) + is_abs as uint; - let mut s = String::from_str(s.slice_to(len)); + let mut s = String::from_str(s[0..len]); unsafe { let v = s.as_mut_vec(); v[4] = (*v)[4].to_ascii().to_uppercase().as_byte(); @@ -774,14 +770,14 @@ fn normalize__(s: &str, prefix: Option) -> Option { _ => { let plen = prefix_len(prefix); if s.len() > plen { - Some(String::from_str(s.slice_to(plen))) + Some(String::from_str(s[0..plen])) } else { None } } } } else if is_abs && comps.is_empty() { Some(String::from_char(1, SEP)) } else { - let prefix_ = s.slice_to(prefix_len(prefix)); + let prefix_ = s[0..prefix_len(prefix)]; let n = prefix_.len() + if is_abs { comps.len() } else { comps.len() - 1} + comps.iter().map(|v| v.len()).sum(); @@ -793,16 +789,16 @@ fn normalize__(s: &str, prefix: Option) -> Option { s.push(':'); } Some(VerbatimDiskPrefix) => { - s.push_str(prefix_.slice_to(4)); + s.push_str(prefix_[0..4]); s.push(prefix_.as_bytes()[4].to_ascii() .to_uppercase().as_char()); - s.push_str(prefix_.slice_from(5)); + s.push_str(prefix_[5..]); } Some(UNCPrefix(a,b)) => { s.push_str("\\\\"); - s.push_str(prefix_.slice(2, a+2)); + s.push_str(prefix_[2..a+2]); s.push(SEP); - s.push_str(prefix_.slice(3+a, 3+a+b)); + s.push_str(prefix_[3+a..3+a+b]); } Some(_) => s.push_str(prefix_), None => () @@ -827,8 +823,8 @@ fn normalize__(s: &str, prefix: Option) -> Option { fn update_sepidx(&mut self) { let s = if self.has_nonsemantic_trailing_slash() { - self.repr.slice_to(self.repr.len()-1) - } else { self.repr.as_slice() }; + self.repr[0..self.repr.len()-1] + } else { self.repr[] }; let idx = s.rfind(if !prefix_is_verbatim(self.prefix) { is_sep } else { is_sep_verbatim }); let prefixlen = self.prefix_len(); @@ -860,8 +856,8 @@ fn has_nonsemantic_trailing_slash(&self) -> bool { self.repr.as_bytes()[self.repr.len()-1] == SEP_BYTE } - fn update_normalized(&mut self, s: S) { - let (prefix, path) = Path::normalize_(s.as_slice()); + fn update_normalized(&mut self, s: &str) { + let (prefix, path) = Path::normalize_(s); self.repr = path; self.prefix = prefix; self.update_sepidx(); @@ -903,17 +899,17 @@ pub fn is_verbatim(path: &Path) -> bool { /// non-verbatim, the non-verbatim version is returned. /// Otherwise, None is returned. pub fn make_non_verbatim(path: &Path) -> Option { - let repr = path.repr.as_slice(); + let repr = path.repr[]; let new_path = match path.prefix { Some(VerbatimPrefix(_)) | Some(DeviceNSPrefix(_)) => return None, Some(UNCPrefix(_,_)) | Some(DiskPrefix) | None => return Some(path.clone()), Some(VerbatimDiskPrefix) => { // \\?\D:\ - Path::new(repr.slice_from(4)) + Path::new(repr[4..]) } Some(VerbatimUNCPrefix(_,_)) => { // \\?\UNC\server\share - Path::new(format!(r"\{}", repr.slice_from(7))) + Path::new(format!(r"\{}", repr[7..])) } }; if new_path.prefix.is_none() { @@ -922,8 +918,8 @@ pub fn make_non_verbatim(path: &Path) -> Option { return None; } // now ensure normalization didn't change anything - if repr.slice_from(path.prefix_len()) == - new_path.repr.slice_from(new_path.prefix_len()) { + if repr[path.prefix_len()..] == + new_path.repr[new_path.prefix_len()..] { Some(new_path) } else { None @@ -988,13 +984,13 @@ pub enum PathPrefix { fn parse_prefix<'a>(mut path: &'a str) -> Option { if path.starts_with("\\\\") { // \\ - path = path.slice_from(2); + path = path[2..]; if path.starts_with("?\\") { // \\?\ - path = path.slice_from(2); + path = path[2..]; if path.starts_with("UNC\\") { // \\?\UNC\server\share - path = path.slice_from(4); + path = path[4..]; let (idx_a, idx_b) = match parse_two_comps(path, is_sep_verbatim) { Some(x) => x, None => (path.len(), 0) @@ -1015,7 +1011,7 @@ fn parse_prefix<'a>(mut path: &'a str) -> Option { } } else if path.starts_with(".\\") { // \\.\path - path = path.slice_from(2); + path = path[2..]; let idx = path.find('\\').unwrap_or(path.len()); return Some(DeviceNSPrefix(idx)); } @@ -1040,7 +1036,7 @@ fn parse_two_comps(mut path: &str, f: fn(char) -> bool) -> Option<(uint, uint)> None => return None, Some(x) => x }; - path = path.slice_from(idx_a+1); + path = path[idx_a+1..]; let idx_b = path.find(f).unwrap_or(path.len()); Some((idx_a, idx_b)) } @@ -1050,8 +1046,8 @@ fn parse_two_comps(mut path: &str, f: fn(char) -> bool) -> Option<(uint, uint)> fn normalize_helper<'a>(s: &'a str, prefix: Option) -> (bool, Option>) { let f = if !prefix_is_verbatim(prefix) { is_sep } else { is_sep_verbatim }; let is_abs = s.len() > prefix_len(prefix) && f(s.char_at(prefix_len(prefix))); - let s_ = s.slice_from(prefix_len(prefix)); - let s_ = if is_abs { s_.slice_from(1) } else { s_ }; + let s_ = s[prefix_len(prefix)..]; + let s_ = if is_abs { s_[1..] } else { s_ }; if is_abs && s_.is_empty() { return (is_abs, match prefix { diff --git a/src/libstd/prelude.rs b/src/libstd/prelude.rs index f77627711a71..49b888d17f47 100644 --- a/src/libstd/prelude.rs +++ b/src/libstd/prelude.rs @@ -79,11 +79,11 @@ #[doc(no_inline)] pub use result::Result; #[doc(no_inline)] pub use result::Result::{Ok, Err}; #[doc(no_inline)] pub use io::{Buffer, Writer, Reader, Seek, BufferPrelude}; -#[doc(no_inline)] pub use str::{Str, StrVector, StrPrelude}; -#[doc(no_inline)] pub use str::{StrAllocating, UnicodeStrPrelude}; #[doc(no_inline)] pub use core::prelude::{Tuple1, Tuple2, Tuple3, Tuple4}; #[doc(no_inline)] pub use core::prelude::{Tuple5, Tuple6, Tuple7, Tuple8}; #[doc(no_inline)] pub use core::prelude::{Tuple9, Tuple10, Tuple11, Tuple12}; +#[doc(no_inline)] pub use str::{Str, StrVector}; +#[doc(no_inline)] pub use str::StrExt; #[doc(no_inline)] pub use slice::AsSlice; #[doc(no_inline)] pub use slice::{VectorVector, PartialEqSliceExt}; #[doc(no_inline)] pub use slice::{CloneSliceExt, OrdSliceExt, SliceExt}; diff --git a/src/libstd/rt/backtrace.rs b/src/libstd/rt/backtrace.rs index 4a692bccf9ee..775e9bb526f7 100644 --- a/src/libstd/rt/backtrace.rs +++ b/src/libstd/rt/backtrace.rs @@ -12,7 +12,8 @@ #![allow(non_camel_case_types)] -use option::Option::{Some, None}; +use prelude::*; + use os; use sync::atomic; diff --git a/src/libstd/rt/mod.rs b/src/libstd/rt/mod.rs index 8d9c1268e7e3..d64336569c6e 100644 --- a/src/libstd/rt/mod.rs +++ b/src/libstd/rt/mod.rs @@ -91,7 +91,7 @@ fn lang_start(main: *const u8, argc: int, argv: *const *const u8) -> int { // created. Note that this isn't necessary in general for new threads, // but we just do this to name the main thread and to give it correct // info about the stack bounds. - let thread: Thread = NewThread::new(Some("
".into_string())); + let thread: Thread = NewThread::new(Some("
".to_string())); thread_info::set((my_stack_bottom, my_stack_top), sys::thread::guard::main(), thread); diff --git a/src/libstd/rt/unwind.rs b/src/libstd/rt/unwind.rs index f572141642cb..eb15a7ba378e 100644 --- a/src/libstd/rt/unwind.rs +++ b/src/libstd/rt/unwind.rs @@ -512,7 +512,7 @@ fn write(&mut self, buf: &[u8]) -> fmt::Result { let mut v = Vec::new(); let _ = write!(&mut VecWriter { v: &mut v }, "{}", msg); - let msg = box String::from_utf8_lossy(v.as_slice()).into_string(); + let msg = box String::from_utf8_lossy(v.as_slice()).into_owned(); begin_unwind_inner(msg, file_line) } diff --git a/src/libstd/rt/util.rs b/src/libstd/rt/util.rs index 77500ca74d09..d8cd8455deb5 100644 --- a/src/libstd/rt/util.rs +++ b/src/libstd/rt/util.rs @@ -10,16 +10,16 @@ // // ignore-lexer-test FIXME #15677 -use core::prelude::*; +use prelude::*; -use core::cmp; -use core::fmt; -use core::intrinsics; -use core::slice; -use core::str; - -use libc::{mod, uintptr_t}; +use cmp; +use fmt; +use intrinsics; +use libc::uintptr_t; +use libc; use os; +use slice; +use str; use sync::atomic; /// Dynamically inquire about whether we're running under V. @@ -52,7 +52,7 @@ pub fn min_stack() -> uint { 0 => {} n => return n - 1, } - let amt = os::getenv("RUST_MIN_STACK").and_then(|s| from_str(s.as_slice())); + let amt = os::getenv("RUST_MIN_STACK").and_then(|s| s.parse()); let amt = amt.unwrap_or(2 * 1024 * 1024); // 0 is our sentinel value, so ensure that we'll never see 0 after // initialization has run @@ -65,7 +65,7 @@ pub fn min_stack() -> uint { pub fn default_sched_threads() -> uint { match os::getenv("RUST_THREADS") { Some(nstr) => { - let opt_n: Option = from_str(nstr.as_slice()); + let opt_n: Option = nstr.parse(); match opt_n { Some(n) if n > 0 => n, _ => panic!("`RUST_THREADS` is `{}`, should be a positive integer", nstr) @@ -113,9 +113,8 @@ fn write(&mut self, data: &[u8]) -> fmt::Result { } pub fn dumb_print(args: &fmt::Arguments) { - use fmt::FormatWriter; let mut w = Stderr; - let _ = w.write_fmt(args); + let _ = write!(&mut w, "{}", args); } pub fn abort(args: &fmt::Arguments) -> ! { diff --git a/src/libstd/sys/common/backtrace.rs b/src/libstd/sys/common/backtrace.rs index a39c8d6d8fed..1d646eb06b16 100644 --- a/src/libstd/sys/common/backtrace.rs +++ b/src/libstd/sys/common/backtrace.rs @@ -8,12 +8,9 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use io::{IoResult, Writer}; -use iter::{Iterator, IteratorExt}; -use option::Option::{Some, None}; -use result::Result::{Ok, Err}; -use str::{StrPrelude, from_str}; -use unicode::char::UnicodeChar; +use prelude::*; + +use io::IoResult; #[cfg(target_word_size = "64")] pub const HEX_WIDTH: uint = 18; #[cfg(target_word_size = "32")] pub const HEX_WIDTH: uint = 10; @@ -85,7 +82,7 @@ pub fn demangle(writer: &mut Writer, s: &str) -> IoResult<()> { while rest.char_at(0).is_numeric() { rest = rest.slice_from(1); } - let i: uint = from_str(inner.slice_to(inner.len() - rest.len())).unwrap(); + let i: uint = inner.slice_to(inner.len() - rest.len()).parse().unwrap(); inner = rest.slice_from(i); rest = rest.slice_to(i); while rest.len() > 0 { diff --git a/src/libstd/sys/windows/backtrace.rs b/src/libstd/sys/windows/backtrace.rs index f2f543dd9697..42c8f7705e1f 100644 --- a/src/libstd/sys/windows/backtrace.rs +++ b/src/libstd/sys/windows/backtrace.rs @@ -32,7 +32,7 @@ use result::Result::{Ok, Err}; use sync::{StaticMutex, MUTEX_INIT}; use slice::SliceExt; -use str::StrPrelude; +use str::StrExt; use dynamic_lib::DynamicLibrary; use sys_common::backtrace::*; diff --git a/src/libstd/sys/windows/fs.rs b/src/libstd/sys/windows/fs.rs index d5bf8c5b6291..15eddd569bee 100644 --- a/src/libstd/sys/windows/fs.rs +++ b/src/libstd/sys/windows/fs.rs @@ -23,6 +23,7 @@ use prelude::*; use sys; +use sys::os; use sys_common::{keep_going, eof, mkerr_libc}; use io::{FilePermission, Write, UnstableFileStat, Open, FileAccess, FileMode}; @@ -262,7 +263,7 @@ fn prune(root: &Path, dirs: Vec) -> Vec { let mut more_files = 1 as libc::BOOL; while more_files != 0 { { - let filename = str::truncate_utf16_at_nul(&wfd.cFileName); + let filename = os::truncate_utf16_at_nul(&wfd.cFileName); match String::from_utf16(filename) { Some(filename) => paths.push(Path::new(filename)), None => { diff --git a/src/libstd/sys/windows/os.rs b/src/libstd/sys/windows/os.rs index e1016048e58c..e007b46b261b 100644 --- a/src/libstd/sys/windows/os.rs +++ b/src/libstd/sys/windows/os.rs @@ -168,7 +168,7 @@ pub fn getcwd() -> IoResult { } } - match String::from_utf16(::str::truncate_utf16_at_nul(&buf)) { + match String::from_utf16(truncate_utf16_at_nul(&buf)) { Some(ref cwd) => Ok(Path::new(cwd)), None => Err(IoError { kind: OtherIoError, @@ -279,7 +279,7 @@ pub fn load_self() -> Option> { unsafe { fill_utf16_buf_and_decode(|buf, sz| { libc::GetModuleFileNameW(0u as libc::DWORD, buf, sz) - }).map(|s| s.into_string().into_bytes()) + }).map(|s| s.to_string().into_bytes()) } } diff --git a/src/libstd/sys/windows/process.rs b/src/libstd/sys/windows/process.rs index 8945c155e66c..0c2c76077dd5 100644 --- a/src/libstd/sys/windows/process.rs +++ b/src/libstd/sys/windows/process.rs @@ -122,7 +122,7 @@ pub fn spawn(cfg: &C, in_fd: Option

, use mem; use iter::{Iterator, IteratorExt}; - use str::StrPrelude; + use str::StrExt; if cfg.gid().is_some() || cfg.uid().is_some() { return Err(IoError { diff --git a/src/libstd/sys/windows/tty.rs b/src/libstd/sys/windows/tty.rs index 51679bb2003f..f793de5bb57e 100644 --- a/src/libstd/sys/windows/tty.rs +++ b/src/libstd/sys/windows/tty.rs @@ -111,7 +111,7 @@ pub fn read(&mut self, buf: &mut [u8]) -> IoResult { } pub fn write(&mut self, buf: &[u8]) -> IoResult<()> { - let utf16 = match from_utf8(buf) { + let utf16 = match from_utf8(buf).ok() { Some(utf8) => { utf8.utf16_units().collect::>() } diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index a294706ef2c1..3eea5b27f195 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -189,7 +189,7 @@ fn encode(&self, s: &mut S) -> Result<(), E> { impl, E> Decodable for Ident { fn decode(d: &mut D) -> Result { - Ok(str_to_ident(try!(d.read_str()).as_slice())) + Ok(str_to_ident(try!(d.read_str())[])) } } diff --git a/src/libsyntax/ast_map/mod.rs b/src/libsyntax/ast_map/mod.rs index a95c9e199060..e3eeb453c26b 100644 --- a/src/libsyntax/ast_map/mod.rs +++ b/src/libsyntax/ast_map/mod.rs @@ -95,7 +95,7 @@ pub fn path_to_string>(path: PI) -> String { if !s.is_empty() { s.push_str("::"); } - s.push_str(e.as_slice()); + s.push_str(e[]); s }).to_string() } @@ -472,20 +472,20 @@ pub fn with_attrs(&self, id: NodeId, f: F) -> T where F: FnOnce(Option<&[Attribute]>) -> T, { let attrs = match self.get(id) { - NodeItem(i) => Some(i.attrs.as_slice()), - NodeForeignItem(fi) => Some(fi.attrs.as_slice()), + NodeItem(i) => Some(i.attrs[]), + NodeForeignItem(fi) => Some(fi.attrs[]), NodeTraitItem(ref tm) => match **tm { - RequiredMethod(ref type_m) => Some(type_m.attrs.as_slice()), - ProvidedMethod(ref m) => Some(m.attrs.as_slice()), - TypeTraitItem(ref typ) => Some(typ.attrs.as_slice()), + RequiredMethod(ref type_m) => Some(type_m.attrs[]), + ProvidedMethod(ref m) => Some(m.attrs[]), + TypeTraitItem(ref typ) => Some(typ.attrs[]), }, NodeImplItem(ref ii) => { match **ii { - MethodImplItem(ref m) => Some(m.attrs.as_slice()), - TypeImplItem(ref t) => Some(t.attrs.as_slice()), + MethodImplItem(ref m) => Some(m.attrs[]), + TypeImplItem(ref t) => Some(t.attrs[]), } } - NodeVariant(ref v) => Some(v.node.attrs.as_slice()), + NodeVariant(ref v) => Some(v.node.attrs[]), // unit/tuple structs take the attributes straight from // the struct definition. // FIXME(eddyb) make this work again (requires access to the map). @@ -504,8 +504,8 @@ pub fn with_attrs(&self, id: NodeId, f: F) -> T where /// the iterator will produce node id's for items with paths /// such as `foo::bar::quux`, `bar::quux`, `other::bar::quux`, and /// any other such items it can find in the map. - pub fn nodes_matching_suffix<'a, S:Str>(&'a self, parts: &'a [S]) - -> NodesMatchingSuffix<'a, 'ast, S> { + pub fn nodes_matching_suffix<'a>(&'a self, parts: &'a [String]) + -> NodesMatchingSuffix<'a, 'ast> { NodesMatchingSuffix { map: self, item_name: parts.last().unwrap(), @@ -565,14 +565,14 @@ pub fn node_to_user_string(&self, id: NodeId) -> String { } } -pub struct NodesMatchingSuffix<'a, 'ast:'a, S:'a> { +pub struct NodesMatchingSuffix<'a, 'ast:'a> { map: &'a Map<'ast>, - item_name: &'a S, - in_which: &'a [S], + item_name: &'a String, + in_which: &'a [String], idx: NodeId, } -impl<'a, 'ast, S:Str> NodesMatchingSuffix<'a, 'ast, S> { +impl<'a, 'ast> NodesMatchingSuffix<'a, 'ast> { /// Returns true only if some suffix of the module path for parent /// matches `self.in_which`. /// @@ -586,7 +586,7 @@ fn suffix_matches(&self, parent: NodeId) -> bool { None => return false, Some((node_id, name)) => (node_id, name), }; - if part.as_slice() != mod_name.as_str() { + if part[] != mod_name.as_str() { return false; } cursor = self.map.get_parent(mod_id); @@ -624,12 +624,12 @@ fn item_is_mod(item: &Item) -> bool { // We are looking at some node `n` with a given name and parent // id; do their names match what I am seeking? fn matches_names(&self, parent_of_n: NodeId, name: Name) -> bool { - name.as_str() == self.item_name.as_slice() && + name.as_str() == self.item_name[] && self.suffix_matches(parent_of_n) } } -impl<'a, 'ast, S:Str> Iterator for NodesMatchingSuffix<'a, 'ast, S> { +impl<'a, 'ast> Iterator for NodesMatchingSuffix<'a, 'ast> { fn next(&mut self) -> Option { loop { let idx = self.idx; @@ -1037,7 +1037,7 @@ fn print_node(&mut self, node: &Node) -> IoResult<()> { fn node_id_to_string(map: &Map, id: NodeId, include_id: bool) -> String { let id_str = format!(" (id={})", id); - let id_str = if include_id { id_str.as_slice() } else { "" }; + let id_str = if include_id { id_str[] } else { "" }; match map.find(id) { Some(NodeItem(item)) => { diff --git a/src/libsyntax/ast_util.rs b/src/libsyntax/ast_util.rs index 02771809ae6a..5727866d6ec3 100644 --- a/src/libsyntax/ast_util.rs +++ b/src/libsyntax/ast_util.rs @@ -238,11 +238,11 @@ pub fn impl_pretty_name(trait_ref: &Option, ty: &Ty) -> Ident { match *trait_ref { Some(ref trait_ref) => { pretty.push('.'); - pretty.push_str(pprust::path_to_string(&trait_ref.path).as_slice()); + pretty.push_str(pprust::path_to_string(&trait_ref.path)[]); } None => {} } - token::gensym_ident(pretty.as_slice()) + token::gensym_ident(pretty[]) } pub fn trait_method_to_ty_method(method: &Method) -> TypeMethod { @@ -700,7 +700,7 @@ pub fn pat_is_ident(pat: P) -> bool { pub fn path_name_eq(a : &ast::Path, b : &ast::Path) -> bool { (a.span == b.span) && (a.global == b.global) - && (segments_name_eq(a.segments.as_slice(), b.segments.as_slice())) + && (segments_name_eq(a.segments[], b.segments[])) } // are two arrays of segments equal when compared unhygienically? @@ -788,13 +788,13 @@ fn ident_to_segment(id : &Ident) -> PathSegment { #[test] fn idents_name_eq_test() { assert!(segments_name_eq( [Ident{name:Name(3),ctxt:4}, Ident{name:Name(78),ctxt:82}] - .iter().map(ident_to_segment).collect::>().as_slice(), + .iter().map(ident_to_segment).collect::>()[], [Ident{name:Name(3),ctxt:104}, Ident{name:Name(78),ctxt:182}] - .iter().map(ident_to_segment).collect::>().as_slice())); + .iter().map(ident_to_segment).collect::>()[])); assert!(!segments_name_eq( [Ident{name:Name(3),ctxt:4}, Ident{name:Name(78),ctxt:82}] - .iter().map(ident_to_segment).collect::>().as_slice(), + .iter().map(ident_to_segment).collect::>()[], [Ident{name:Name(3),ctxt:104}, Ident{name:Name(77),ctxt:182}] - .iter().map(ident_to_segment).collect::>().as_slice())); + .iter().map(ident_to_segment).collect::>()[])); } } diff --git a/src/libsyntax/attr.rs b/src/libsyntax/attr.rs index 127cc5ed51d1..b1158917b72f 100644 --- a/src/libsyntax/attr.rs +++ b/src/libsyntax/attr.rs @@ -97,7 +97,7 @@ fn value_str(&self) -> Option { fn meta_item_list<'a>(&'a self) -> Option<&'a [P]> { match self.node { - MetaList(_, ref l) => Some(l.as_slice()), + MetaList(_, ref l) => Some(l[]), _ => None } } @@ -136,7 +136,7 @@ fn with_desugared_doc(&self, f: F) -> T where let meta = mk_name_value_item_str( InternedString::new("doc"), token::intern_and_get_ident(strip_doc_comment_decoration( - comment.get()).as_slice())); + comment.get())[])); if self.node.style == ast::AttrOuter { f(&mk_attr_outer(self.node.id, meta)) } else { @@ -296,9 +296,9 @@ pub fn find_inline_attr(attrs: &[Attribute]) -> InlineAttr { } MetaList(ref n, ref items) if *n == "inline" => { mark_used(attr); - if contains_name(items.as_slice(), "always") { + if contains_name(items[], "always") { InlineAlways - } else if contains_name(items.as_slice(), "never") { + } else if contains_name(items[], "never") { InlineNever } else { InlineHint @@ -332,7 +332,7 @@ pub fn cfg_matches(diagnostic: &SpanHandler, cfgs: &[P], cfg: &ast::Me !cfg_matches(diagnostic, cfgs, &*mis[0]) } ast::MetaList(ref pred, _) => { - diagnostic.span_err(cfg.span, format!("invalid predicate `{}`", pred).as_slice()); + diagnostic.span_err(cfg.span, format!("invalid predicate `{}`", pred)[]); false }, ast::MetaWord(_) | ast::MetaNameValue(..) => contains(cfgs, cfg), @@ -396,8 +396,7 @@ pub fn require_unique_names(diagnostic: &SpanHandler, metas: &[P]) { if !set.insert(name.clone()) { diagnostic.span_fatal(meta.span, - format!("duplicate meta item `{}`", - name).as_slice()); + format!("duplicate meta item `{}`", name)[]); } } } diff --git a/src/libsyntax/codemap.rs b/src/libsyntax/codemap.rs index c726e17a8fa6..060e1d3f9957 100644 --- a/src/libsyntax/codemap.rs +++ b/src/libsyntax/codemap.rs @@ -291,9 +291,9 @@ pub fn get_line(&self, line_number: uint) -> Option { lines.get(line_number).map(|&line| { let begin: BytePos = line - self.start_pos; let begin = begin.to_uint(); - let slice = self.src.slice_from(begin); + let slice = self.src[begin..]; match slice.find('\n') { - Some(e) => slice.slice_to(e), + Some(e) => slice[0..e], None => slice }.to_string() }) @@ -338,9 +338,9 @@ pub fn new_filemap(&self, filename: FileName, src: String) -> Rc { // FIXME #12884: no efficient/safe way to remove from the start of a string // and reuse the allocation. let mut src = if src.starts_with("\u{feff}") { - String::from_str(src.slice_from(3)) + String::from_str(src[3..]) } else { - String::from_str(src.as_slice()) + String::from_str(src[]) }; // Append '\n' in case it's not already there. @@ -427,8 +427,8 @@ pub fn span_to_snippet(&self, sp: Span) -> Option { if begin.fm.start_pos != end.fm.start_pos { None } else { - Some(begin.fm.src.slice(begin.pos.to_uint(), - end.pos.to_uint()).to_string()) + Some(begin.fm.src[begin.pos.to_uint().. + end.pos.to_uint()].to_string()) } } diff --git a/src/libsyntax/diagnostic.rs b/src/libsyntax/diagnostic.rs index 4d765f49acab..88dfdf6e2d8f 100644 --- a/src/libsyntax/diagnostic.rs +++ b/src/libsyntax/diagnostic.rs @@ -123,7 +123,7 @@ pub fn span_bug(&self, sp: Span, msg: &str) -> ! { panic!(ExplicitBug); } pub fn span_unimpl(&self, sp: Span, msg: &str) -> ! { - self.span_bug(sp, format!("unimplemented {}", msg).as_slice()); + self.span_bug(sp, format!("unimplemented {}", msg)[]); } pub fn handler<'a>(&'a self) -> &'a Handler { &self.handler @@ -166,7 +166,7 @@ pub fn abort_if_errors(&self) { self.err_count.get()); } } - self.fatal(s.as_slice()); + self.fatal(s[]); } pub fn warn(&self, msg: &str) { self.emit.borrow_mut().emit(None, msg, None, Warning); @@ -182,7 +182,7 @@ pub fn bug(&self, msg: &str) -> ! { panic!(ExplicitBug); } pub fn unimpl(&self, msg: &str) -> ! { - self.bug(format!("unimplemented {}", msg).as_slice()); + self.bug(format!("unimplemented {}", msg)[]); } pub fn emit(&self, cmsp: Option<(&codemap::CodeMap, Span)>, @@ -277,7 +277,7 @@ fn print_maybe_styled(w: &mut EmitterWriter, // to be miscolored. We assume this is rare enough that we don't // have to worry about it. if msg.ends_with("\n") { - try!(t.write_str(msg.slice_to(msg.len()-1))); + try!(t.write_str(msg[0..msg.len()-1])); try!(t.reset()); try!(t.write_str("\n")); } else { @@ -299,16 +299,16 @@ fn print_diagnostic(dst: &mut EmitterWriter, topic: &str, lvl: Level, } try!(print_maybe_styled(dst, - format!("{}: ", lvl.to_string()).as_slice(), + format!("{}: ", lvl.to_string())[], term::attr::ForegroundColor(lvl.color()))); try!(print_maybe_styled(dst, - format!("{}", msg).as_slice(), + format!("{}", msg)[], term::attr::Bold)); match code { Some(code) => { let style = term::attr::ForegroundColor(term::color::BRIGHT_MAGENTA); - try!(print_maybe_styled(dst, format!(" [{}]", code.clone()).as_slice(), style)); + try!(print_maybe_styled(dst, format!(" [{}]", code.clone())[], style)); } None => () } @@ -398,12 +398,12 @@ fn emit(dst: &mut EmitterWriter, cm: &codemap::CodeMap, rsp: RenderSpan, // the span) let span_end = Span { lo: sp.hi, hi: sp.hi, expn_id: sp.expn_id}; let ses = cm.span_to_string(span_end); - try!(print_diagnostic(dst, ses.as_slice(), lvl, msg, code)); + try!(print_diagnostic(dst, ses[], lvl, msg, code)); if rsp.is_full_span() { try!(custom_highlight_lines(dst, cm, sp, lvl, lines)); } } else { - try!(print_diagnostic(dst, ss.as_slice(), lvl, msg, code)); + try!(print_diagnostic(dst, ss[], lvl, msg, code)); if rsp.is_full_span() { try!(highlight_lines(dst, cm, sp, lvl, lines)); } @@ -413,9 +413,9 @@ fn emit(dst: &mut EmitterWriter, cm: &codemap::CodeMap, rsp: RenderSpan, Some(code) => match dst.registry.as_ref().and_then(|registry| registry.find_description(code)) { Some(_) => { - try!(print_diagnostic(dst, ss.as_slice(), Help, + try!(print_diagnostic(dst, ss[], Help, format!("pass `--explain {}` to see a detailed \ - explanation", code).as_slice(), None)); + explanation", code)[], None)); } None => () }, @@ -432,7 +432,7 @@ fn highlight_lines(err: &mut EmitterWriter, let fm = &*lines.file; let mut elided = false; - let mut display_lines = lines.lines.as_slice(); + let mut display_lines = lines.lines[]; if display_lines.len() > MAX_LINES { display_lines = display_lines[0u..MAX_LINES]; elided = true; @@ -494,7 +494,7 @@ fn highlight_lines(err: &mut EmitterWriter, } } try!(print_maybe_styled(err, - format!("{}\n", s).as_slice(), + format!("{}\n", s)[], term::attr::ForegroundColor(lvl.color()))); } Ok(()) @@ -514,7 +514,7 @@ fn custom_highlight_lines(w: &mut EmitterWriter, -> io::IoResult<()> { let fm = &*lines.file; - let lines = lines.lines.as_slice(); + let lines = lines.lines[]; if lines.len() > MAX_LINES { if let Some(line) = fm.get_line(lines[0]) { try!(write!(&mut w.dst, "{}:{} {}\n", fm.name, @@ -545,7 +545,7 @@ fn custom_highlight_lines(w: &mut EmitterWriter, s.push('^'); s.push('\n'); print_maybe_styled(w, - s.as_slice(), + s[], term::attr::ForegroundColor(lvl.color())) } @@ -560,12 +560,12 @@ fn print_macro_backtrace(w: &mut EmitterWriter, codemap::MacroAttribute => ("#[", "]"), codemap::MacroBang => ("", "!") }; - try!(print_diagnostic(w, ss.as_slice(), Note, + try!(print_diagnostic(w, ss[], Note, format!("in expansion of {}{}{}", pre, ei.callee.name, - post).as_slice(), None)); + post)[], None)); let ss = cm.span_to_string(ei.call_site); - try!(print_diagnostic(w, ss.as_slice(), Note, "expansion site", None)); + try!(print_diagnostic(w, ss[], Note, "expansion site", None)); Ok(Some(ei.call_site)) } None => Ok(None) @@ -578,6 +578,6 @@ pub fn expect(diag: &SpanHandler, opt: Option, msg: M) -> T where { match opt { Some(t) => t, - None => diag.handler().bug(msg().as_slice()), + None => diag.handler().bug(msg()[]), } } diff --git a/src/libsyntax/diagnostics/plugin.rs b/src/libsyntax/diagnostics/plugin.rs index bcce5538314b..90fc28014e64 100644 --- a/src/libsyntax/diagnostics/plugin.rs +++ b/src/libsyntax/diagnostics/plugin.rs @@ -58,7 +58,7 @@ pub fn expand_diagnostic_used<'cx>(ecx: &'cx mut ExtCtxt, Some(previous_span) => { ecx.span_warn(span, format!( "diagnostic code {} already used", token::get_ident(code).get() - ).as_slice()); + )[]); ecx.span_note(previous_span, "previous invocation"); }, None => () @@ -87,12 +87,12 @@ pub fn expand_register_diagnostic<'cx>(ecx: &'cx mut ExtCtxt, if diagnostics.insert(code.name, description).is_some() { ecx.span_err(span, format!( "diagnostic code {} already registered", token::get_ident(*code).get() - ).as_slice()); + )[]); } }); let sym = Ident::new(token::gensym(( "__register_diagnostic_".to_string() + token::get_ident(*code).get() - ).as_slice())); + )[])); MacItems::new(vec![quote_item!(ecx, mod $sym {}).unwrap()].into_iter()) } diff --git a/src/libsyntax/ext/asm.rs b/src/libsyntax/ext/asm.rs index b138811187ba..b77b822a6b2a 100644 --- a/src/libsyntax/ext/asm.rs +++ b/src/libsyntax/ext/asm.rs @@ -100,8 +100,7 @@ pub fn expand_asm<'cx>(cx: &'cx mut ExtCtxt, sp: Span, tts: &[ast::TokenTree]) Some(('=', _)) => None, Some(('+', operand)) => { Some(token::intern_and_get_ident(format!( - "={}", - operand).as_slice())) + "={}", operand)[])) } _ => { cx.span_err(span, "output operand constraint lacks '=' or '+'"); diff --git a/src/libsyntax/ext/base.rs b/src/libsyntax/ext/base.rs index aefbb2a1feab..62fe718b522b 100644 --- a/src/libsyntax/ext/base.rs +++ b/src/libsyntax/ext/base.rs @@ -549,7 +549,7 @@ pub fn original_span_in_file(&self) -> Span { pub fn mod_pop(&mut self) { self.mod_path.pop().unwrap(); } pub fn mod_path(&self) -> Vec { let mut v = Vec::new(); - v.push(token::str_to_ident(self.ecfg.crate_name.as_slice())); + v.push(token::str_to_ident(self.ecfg.crate_name[])); v.extend(self.mod_path.iter().map(|a| *a)); return v; } @@ -558,7 +558,7 @@ pub fn bt_push(&mut self, ei: ExpnInfo) { if self.recursion_count > self.ecfg.recursion_limit { self.span_fatal(ei.call_site, format!("recursion limit reached while expanding the macro `{}`", - ei.callee.name).as_slice()); + ei.callee.name)[]); } let mut call_site = ei.call_site; @@ -669,7 +669,7 @@ pub fn check_zero_tts(cx: &ExtCtxt, tts: &[ast::TokenTree], name: &str) { if tts.len() != 0 { - cx.span_err(sp, format!("{} takes no arguments", name).as_slice()); + cx.span_err(sp, format!("{} takes no arguments", name)[]); } } @@ -682,12 +682,12 @@ pub fn get_single_str_from_tts(cx: &mut ExtCtxt, -> Option { let mut p = cx.new_parser_from_tts(tts); if p.token == token::Eof { - cx.span_err(sp, format!("{} takes 1 argument", name).as_slice()); + cx.span_err(sp, format!("{} takes 1 argument", name)[]); return None } let ret = cx.expander().fold_expr(p.parse_expr()); if p.token != token::Eof { - cx.span_err(sp, format!("{} takes 1 argument", name).as_slice()); + cx.span_err(sp, format!("{} takes 1 argument", name)[]); } expr_to_string(cx, ret, "argument must be a string literal").map(|(s, _)| { s.get().to_string() diff --git a/src/libsyntax/ext/build.rs b/src/libsyntax/ext/build.rs index 9d4992f7453d..77165168746b 100644 --- a/src/libsyntax/ext/build.rs +++ b/src/libsyntax/ext/build.rs @@ -712,8 +712,7 @@ fn expr_fail(&self, span: Span, msg: InternedString) -> P { let loc = self.codemap().lookup_char_pos(span.lo); let expr_file = self.expr_str(span, token::intern_and_get_ident(loc.file - .name - .as_slice())); + .name[])); let expr_line = self.expr_uint(span, loc.line); let expr_file_line_tuple = self.expr_tuple(span, vec!(expr_file, expr_line)); let expr_file_line_ptr = self.expr_addr_of(span, expr_file_line_tuple); diff --git a/src/libsyntax/ext/concat.rs b/src/libsyntax/ext/concat.rs index e2867c2fbabf..03dd08fdf7fe 100644 --- a/src/libsyntax/ext/concat.rs +++ b/src/libsyntax/ext/concat.rs @@ -40,14 +40,14 @@ pub fn expand_syntax_ext(cx: &mut base::ExtCtxt, ast::LitInt(i, ast::UnsignedIntLit(_)) | ast::LitInt(i, ast::SignedIntLit(_, ast::Plus)) | ast::LitInt(i, ast::UnsuffixedIntLit(ast::Plus)) => { - accumulator.push_str(format!("{}", i).as_slice()); + accumulator.push_str(format!("{}", i)[]); } ast::LitInt(i, ast::SignedIntLit(_, ast::Minus)) | ast::LitInt(i, ast::UnsuffixedIntLit(ast::Minus)) => { - accumulator.push_str(format!("-{}", i).as_slice()); + accumulator.push_str(format!("-{}", i)[]); } ast::LitBool(b) => { - accumulator.push_str(format!("{}", b).as_slice()); + accumulator.push_str(format!("{}", b)[]); } ast::LitByte(..) | ast::LitBinary(..) => { @@ -62,5 +62,5 @@ pub fn expand_syntax_ext(cx: &mut base::ExtCtxt, } base::MacExpr::new(cx.expr_str( sp, - token::intern_and_get_ident(accumulator.as_slice()))) + token::intern_and_get_ident(accumulator[]))) } diff --git a/src/libsyntax/ext/concat_idents.rs b/src/libsyntax/ext/concat_idents.rs index aa18b1be31ac..2cf60d30a1b2 100644 --- a/src/libsyntax/ext/concat_idents.rs +++ b/src/libsyntax/ext/concat_idents.rs @@ -40,7 +40,7 @@ pub fn expand_syntax_ext<'cx>(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree] } } } - let res = str_to_ident(res_str.as_slice()); + let res = str_to_ident(res_str[]); let e = P(ast::Expr { id: ast::DUMMY_NODE_ID, diff --git a/src/libsyntax/ext/deriving/bounds.rs b/src/libsyntax/ext/deriving/bounds.rs index 3145b3bb1a4f..c27a27fce6a9 100644 --- a/src/libsyntax/ext/deriving/bounds.rs +++ b/src/libsyntax/ext/deriving/bounds.rs @@ -31,8 +31,7 @@ pub fn expand_deriving_bound(cx: &mut ExtCtxt, ref tname => { cx.span_bug(span, format!("expected built-in trait name but \ - found {}", - *tname).as_slice()) + found {}", *tname)[]) } } }, diff --git a/src/libsyntax/ext/deriving/clone.rs b/src/libsyntax/ext/deriving/clone.rs index a34764221b3b..eedec6f37c84 100644 --- a/src/libsyntax/ext/deriving/clone.rs +++ b/src/libsyntax/ext/deriving/clone.rs @@ -80,13 +80,11 @@ fn cs_clone( EnumNonMatchingCollapsed (..) => { cx.span_bug(trait_span, format!("non-matching enum variants in \ - `deriving({})`", - name).as_slice()) + `deriving({})`", name)[]) } StaticEnum(..) | StaticStruct(..) => { cx.span_bug(trait_span, - format!("static method in `deriving({})`", - name).as_slice()) + format!("static method in `deriving({})`", name)[]) } } @@ -103,8 +101,7 @@ fn cs_clone( None => { cx.span_bug(trait_span, format!("unnamed field in normal struct in \ - `deriving({})`", - name).as_slice()) + `deriving({})`", name)[]) } }; cx.field_imm(field.span, ident, subcall(field)) diff --git a/src/libsyntax/ext/deriving/decodable.rs b/src/libsyntax/ext/deriving/decodable.rs index 0a8d59da8967..a4c70ebbc8ef 100644 --- a/src/libsyntax/ext/deriving/decodable.rs +++ b/src/libsyntax/ext/deriving/decodable.rs @@ -174,7 +174,7 @@ fn decode_static_fields(cx: &mut ExtCtxt, let fields = fields.iter().enumerate().map(|(i, &span)| { getarg(cx, span, token::intern_and_get_ident(format!("_field{}", - i).as_slice()), + i)[]), i) }).collect(); diff --git a/src/libsyntax/ext/deriving/encodable.rs b/src/libsyntax/ext/deriving/encodable.rs index 30851ebeaaef..aac515ed81ac 100644 --- a/src/libsyntax/ext/deriving/encodable.rs +++ b/src/libsyntax/ext/deriving/encodable.rs @@ -162,8 +162,7 @@ fn encodable_substructure(cx: &mut ExtCtxt, trait_span: Span, let name = match name { Some(id) => token::get_ident(id), None => { - token::intern_and_get_ident(format!("_field{}", - i).as_slice()) + token::intern_and_get_ident(format!("_field{}", i)[]) } }; let enc = cx.expr_method_call(span, self_.clone(), diff --git a/src/libsyntax/ext/deriving/generic/mod.rs b/src/libsyntax/ext/deriving/generic/mod.rs index d8de3d2db979..a2b5c0b9e962 100644 --- a/src/libsyntax/ext/deriving/generic/mod.rs +++ b/src/libsyntax/ext/deriving/generic/mod.rs @@ -514,15 +514,15 @@ fn expand_struct_def(&self, self, struct_def, type_ident, - self_args.as_slice(), - nonself_args.as_slice()) + self_args[], + nonself_args[]) } else { method_def.expand_struct_method_body(cx, self, struct_def, type_ident, - self_args.as_slice(), - nonself_args.as_slice()) + self_args[], + nonself_args[]) }; method_def.create_method(cx, @@ -554,15 +554,15 @@ fn expand_enum_def(&self, self, enum_def, type_ident, - self_args.as_slice(), - nonself_args.as_slice()) + self_args[], + nonself_args[]) } else { method_def.expand_enum_method_body(cx, self, enum_def, type_ident, self_args, - nonself_args.as_slice()) + nonself_args[]) }; method_def.create_method(cx, @@ -649,7 +649,7 @@ fn split_self_nonself_args(&self, for (i, ty) in self.args.iter().enumerate() { let ast_ty = ty.to_ty(cx, trait_.span, type_ident, generics); - let ident = cx.ident_of(format!("__arg_{}", i).as_slice()); + let ident = cx.ident_of(format!("__arg_{}", i)[]); arg_tys.push((ident, ast_ty)); let arg_expr = cx.expr_ident(trait_.span, ident); @@ -756,7 +756,7 @@ fn expand_struct_method_body(&self, struct_path, struct_def, format!("__self_{}", - i).as_slice(), + i)[], ast::MutImmutable); patterns.push(pat); raw_fields.push(ident_expr); @@ -912,22 +912,22 @@ fn build_enum_match_tuple( .collect::>(); let self_arg_idents = self_arg_names.iter() - .map(|name|cx.ident_of(name.as_slice())) + .map(|name|cx.ident_of(name[])) .collect::>(); // The `vi_idents` will be bound, solely in the catch-all, to // a series of let statements mapping each self_arg to a uint // corresponding to its variant index. let vi_idents: Vec = self_arg_names.iter() - .map(|name| { let vi_suffix = format!("{}_vi", name.as_slice()); - cx.ident_of(vi_suffix.as_slice()) }) + .map(|name| { let vi_suffix = format!("{}_vi", name[]); + cx.ident_of(vi_suffix[]) }) .collect::>(); // Builds, via callback to call_substructure_method, the // delegated expression that handles the catch-all case, // using `__variants_tuple` to drive logic if necessary. let catch_all_substructure = EnumNonMatchingCollapsed( - self_arg_idents, variants.as_slice(), vi_idents.as_slice()); + self_arg_idents, variants[], vi_idents[]); // These arms are of the form: // (Variant1, Variant1, ...) => Body1 @@ -949,12 +949,12 @@ fn build_enum_match_tuple( let mut subpats = Vec::with_capacity(self_arg_names.len()); let mut self_pats_idents = Vec::with_capacity(self_arg_names.len() - 1); let first_self_pat_idents = { - let (p, idents) = mk_self_pat(cx, self_arg_names[0].as_slice()); + let (p, idents) = mk_self_pat(cx, self_arg_names[0][]); subpats.push(p); idents }; for self_arg_name in self_arg_names.tail().iter() { - let (p, idents) = mk_self_pat(cx, self_arg_name.as_slice()); + let (p, idents) = mk_self_pat(cx, self_arg_name[]); subpats.push(p); self_pats_idents.push(idents); } @@ -1010,7 +1010,7 @@ fn build_enum_match_tuple( &**variant, field_tuples); let arm_expr = self.call_substructure_method( - cx, trait_, type_ident, self_args.as_slice(), nonself_args, + cx, trait_, type_ident, self_args[], nonself_args, &substructure); cx.arm(sp, vec![single_pat], arm_expr) @@ -1063,7 +1063,7 @@ fn build_enum_match_tuple( } let arm_expr = self.call_substructure_method( - cx, trait_, type_ident, self_args.as_slice(), nonself_args, + cx, trait_, type_ident, self_args[], nonself_args, &catch_all_substructure); // Builds the expression: @@ -1267,7 +1267,7 @@ fn create_struct_pattern(&self, cx.span_bug(sp, "a struct with named and unnamed fields in `deriving`"); } }; - let ident = cx.ident_of(format!("{}_{}", prefix, i).as_slice()); + let ident = cx.ident_of(format!("{}_{}", prefix, i)[]); paths.push(codemap::Spanned{span: sp, node: ident}); let val = cx.expr( sp, ast::ExprParen(cx.expr_deref(sp, cx.expr_path(cx.path_ident(sp,ident))))); @@ -1313,7 +1313,7 @@ fn create_enum_variant_pattern(&self, let mut ident_expr = Vec::new(); for (i, va) in variant_args.iter().enumerate() { let sp = self.set_expn_info(cx, va.ty.span); - let ident = cx.ident_of(format!("{}_{}", prefix, i).as_slice()); + let ident = cx.ident_of(format!("{}_{}", prefix, i)[]); let path1 = codemap::Spanned{span: sp, node: ident}; paths.push(path1); let expr_path = cx.expr_path(cx.path_ident(sp, ident)); @@ -1356,7 +1356,7 @@ pub fn cs_fold(use_foldl: bool, field.span, old, field.self_.clone(), - field.other.as_slice()) + field.other[]) }) } else { all_fields.iter().rev().fold(base, |old, field| { @@ -1364,12 +1364,12 @@ pub fn cs_fold(use_foldl: bool, field.span, old, field.self_.clone(), - field.other.as_slice()) + field.other[]) }) } }, EnumNonMatchingCollapsed(ref all_args, _, tuple) => - enum_nonmatch_f(cx, trait_span, (all_args.as_slice(), tuple), + enum_nonmatch_f(cx, trait_span, (all_args[], tuple), substructure.nonself_args), StaticEnum(..) | StaticStruct(..) => { cx.span_bug(trait_span, "static function in `deriving`") @@ -1409,7 +1409,7 @@ pub fn cs_same_method(f: F, f(cx, trait_span, called) }, EnumNonMatchingCollapsed(ref all_self_args, _, tuple) => - enum_nonmatch_f(cx, trait_span, (all_self_args.as_slice(), tuple), + enum_nonmatch_f(cx, trait_span, (all_self_args[], tuple), substructure.nonself_args), StaticEnum(..) | StaticStruct(..) => { cx.span_bug(trait_span, "static function in `deriving`") diff --git a/src/libsyntax/ext/deriving/mod.rs b/src/libsyntax/ext/deriving/mod.rs index 839e99c81d1a..4a9076b07b5e 100644 --- a/src/libsyntax/ext/deriving/mod.rs +++ b/src/libsyntax/ext/deriving/mod.rs @@ -115,7 +115,7 @@ macro_rules! expand(($func:path) => ($func(cx, titem.span, cx.span_err(titem.span, format!("unknown `deriving` \ trait: `{}`", - *tname).as_slice()); + *tname)[]); } }; } diff --git a/src/libsyntax/ext/deriving/show.rs b/src/libsyntax/ext/deriving/show.rs index a68b521bbc9a..19b45a1e6100 100644 --- a/src/libsyntax/ext/deriving/show.rs +++ b/src/libsyntax/ext/deriving/show.rs @@ -127,7 +127,7 @@ fn show_substructure(cx: &mut ExtCtxt, span: Span, let formatter = substr.nonself_args[0].clone(); let meth = cx.ident_of("write_fmt"); - let s = token::intern_and_get_ident(format_string.as_slice()); + let s = token::intern_and_get_ident(format_string[]); let format_string = cx.expr_str(span, s); // phew, not our responsibility any more! diff --git a/src/libsyntax/ext/env.rs b/src/libsyntax/ext/env.rs index 8c17b31f458d..9fedc4a158e1 100644 --- a/src/libsyntax/ext/env.rs +++ b/src/libsyntax/ext/env.rs @@ -30,7 +30,7 @@ pub fn expand_option_env<'cx>(cx: &'cx mut ExtCtxt, sp: Span, tts: &[ast::TokenT Some(v) => v }; - let e = match os::getenv(var.as_slice()) { + let e = match os::getenv(var[]) { None => { cx.expr_path(cx.path_all(sp, true, @@ -56,7 +56,7 @@ pub fn expand_option_env<'cx>(cx: &'cx mut ExtCtxt, sp: Span, tts: &[ast::TokenT cx.ident_of("Some")), vec!(cx.expr_str(sp, token::intern_and_get_ident( - s.as_slice())))) + s[])))) } }; MacExpr::new(e) @@ -83,7 +83,7 @@ pub fn expand_env<'cx>(cx: &'cx mut ExtCtxt, sp: Span, tts: &[ast::TokenTree]) None => { token::intern_and_get_ident(format!("environment variable `{}` \ not defined", - var).as_slice()) + var)[]) } Some(second) => { match expr_to_string(cx, second, "expected string literal") { @@ -106,7 +106,7 @@ pub fn expand_env<'cx>(cx: &'cx mut ExtCtxt, sp: Span, tts: &[ast::TokenTree]) cx.span_err(sp, msg.get()); cx.expr_uint(sp, 0) } - Some(s) => cx.expr_str(sp, token::intern_and_get_ident(s.as_slice())) + Some(s) => cx.expr_str(sp, token::intern_and_get_ident(s[])) }; MacExpr::new(e) } diff --git a/src/libsyntax/ext/expand.rs b/src/libsyntax/ext/expand.rs index b10ae7a09db1..f2b6f6bfe16b 100644 --- a/src/libsyntax/ext/expand.rs +++ b/src/libsyntax/ext/expand.rs @@ -293,7 +293,7 @@ fn expand_mac_invoc(mac: ast::Mac, span: codemap::Span, fld.cx.span_err( pth.span, format!("macro undefined: '{}!'", - extnamestr.get()).as_slice()); + extnamestr.get())[]); // let compilation continue None @@ -309,7 +309,7 @@ fn expand_mac_invoc(mac: ast::Mac, span: codemap::Span, }, }); let fm = fresh_mark(); - let marked_before = mark_tts(tts.as_slice(), fm); + let marked_before = mark_tts(tts[], fm); // The span that we pass to the expanders we want to // be the root of the call stack. That's the most @@ -320,7 +320,7 @@ fn expand_mac_invoc(mac: ast::Mac, span: codemap::Span, let opt_parsed = { let expanded = expandfun.expand(fld.cx, mac_span, - marked_before.as_slice()); + marked_before[]); parse_thunk(expanded) }; let parsed = match opt_parsed { @@ -329,8 +329,8 @@ fn expand_mac_invoc(mac: ast::Mac, span: codemap::Span, fld.cx.span_err( pth.span, format!("non-expression macro in expression position: {}", - extnamestr.get().as_slice() - ).as_slice()); + extnamestr.get()[] + )[]); return None; } }; @@ -340,7 +340,7 @@ fn expand_mac_invoc(mac: ast::Mac, span: codemap::Span, fld.cx.span_err( pth.span, format!("'{}' is not a tt-style macro", - extnamestr.get()).as_slice()); + extnamestr.get())[]); None } } @@ -445,7 +445,7 @@ pub fn expand_item(it: P, fld: &mut MacroExpander) if valid_ident { fld.cx.mod_push(it.ident); } - let macro_escape = contains_macro_escape(new_attrs.as_slice()); + let macro_escape = contains_macro_escape(new_attrs[]); let result = with_exts_frame!(fld.cx.syntax_env, macro_escape, noop_fold_item(it, fld)); @@ -553,7 +553,7 @@ pub fn expand_item_mac(it: P, fld: &mut MacroExpander) None => { fld.cx.span_err(path_span, format!("macro undefined: '{}!'", - extnamestr).as_slice()); + extnamestr)[]); // let compilation continue return SmallVector::zero(); } @@ -566,7 +566,7 @@ pub fn expand_item_mac(it: P, fld: &mut MacroExpander) format!("macro {}! expects no ident argument, \ given '{}'", extnamestr, - token::get_ident(it.ident)).as_slice()); + token::get_ident(it.ident))[]); return SmallVector::zero(); } fld.cx.bt_push(ExpnInfo { @@ -578,14 +578,14 @@ pub fn expand_item_mac(it: P, fld: &mut MacroExpander) } }); // mark before expansion: - let marked_before = mark_tts(tts.as_slice(), fm); - expander.expand(fld.cx, it.span, marked_before.as_slice()) + let marked_before = mark_tts(tts[], fm); + expander.expand(fld.cx, it.span, marked_before[]) } IdentTT(ref expander, span) => { if it.ident.name == parse::token::special_idents::invalid.name { fld.cx.span_err(path_span, format!("macro {}! expects an ident argument", - extnamestr.get()).as_slice()); + extnamestr.get())[]); return SmallVector::zero(); } fld.cx.bt_push(ExpnInfo { @@ -597,14 +597,14 @@ pub fn expand_item_mac(it: P, fld: &mut MacroExpander) } }); // mark before expansion: - let marked_tts = mark_tts(tts.as_slice(), fm); + let marked_tts = mark_tts(tts[], fm); expander.expand(fld.cx, it.span, it.ident, marked_tts) } LetSyntaxTT(ref expander, span) => { if it.ident.name == parse::token::special_idents::invalid.name { fld.cx.span_err(path_span, format!("macro {}! expects an ident argument", - extnamestr.get()).as_slice()); + extnamestr.get())[]); return SmallVector::zero(); } fld.cx.bt_push(ExpnInfo { @@ -621,7 +621,7 @@ pub fn expand_item_mac(it: P, fld: &mut MacroExpander) _ => { fld.cx.span_err(it.span, format!("{}! is not legal in item position", - extnamestr.get()).as_slice()); + extnamestr.get())[]); return SmallVector::zero(); } } @@ -639,8 +639,8 @@ pub fn expand_item_mac(it: P, fld: &mut MacroExpander) // result of expanding a LetSyntaxTT, and thus doesn't // need to be marked. Not that it could be marked anyway. // create issue to recommend refactoring here? - fld.cx.syntax_env.insert(intern(name.as_slice()), ext); - if attr::contains_name(it.attrs.as_slice(), "macro_export") { + fld.cx.syntax_env.insert(intern(name[]), ext); + if attr::contains_name(it.attrs[], "macro_export") { fld.cx.exported_macros.push(it); } SmallVector::zero() @@ -654,7 +654,7 @@ pub fn expand_item_mac(it: P, fld: &mut MacroExpander) Right(None) => { fld.cx.span_err(path_span, format!("non-item macro in item position: {}", - extnamestr.get()).as_slice()); + extnamestr.get())[]); return SmallVector::zero(); } }; @@ -903,7 +903,7 @@ fn expand_pat(p: P, fld: &mut MacroExpander) -> P { None => { fld.cx.span_err(pth.span, format!("macro undefined: '{}!'", - extnamestr).as_slice()); + extnamestr)[]); // let compilation continue return DummyResult::raw_pat(span); } @@ -920,11 +920,11 @@ fn expand_pat(p: P, fld: &mut MacroExpander) -> P { }); let fm = fresh_mark(); - let marked_before = mark_tts(tts.as_slice(), fm); + let marked_before = mark_tts(tts[], fm); let mac_span = fld.cx.original_span(); let expanded = match expander.expand(fld.cx, mac_span, - marked_before.as_slice()).make_pat() { + marked_before[]).make_pat() { Some(e) => e, None => { fld.cx.span_err( @@ -932,7 +932,7 @@ fn expand_pat(p: P, fld: &mut MacroExpander) -> P { format!( "non-pattern macro in pattern position: {}", extnamestr.get() - ).as_slice() + )[] ); return DummyResult::raw_pat(span); } @@ -944,7 +944,7 @@ fn expand_pat(p: P, fld: &mut MacroExpander) -> P { _ => { fld.cx.span_err(span, format!("{}! is not legal in pattern position", - extnamestr.get()).as_slice()); + extnamestr.get())[]); return DummyResult::raw_pat(span); } } @@ -1192,8 +1192,7 @@ pub fn expand_crate(parse_sess: &parse::ParseSess, let mut expander = MacroExpander::new(&mut cx); for ExportedMacros { crate_name, macros } in imported_macros.into_iter() { - let name = format!("<{} macros>", token::get_ident(crate_name)) - .into_string(); + let name = format!("<{} macros>", token::get_ident(crate_name)); for source in macros.into_iter() { let item = parse::parse_item_from_source_str(name.clone(), @@ -1238,7 +1237,7 @@ fn fold_mac(&mut self, Spanned {node, span}: ast::Mac) -> ast::Mac { node: match node { MacInvocTT(path, tts, ctxt) => { MacInvocTT(self.fold_path(path), - self.fold_tts(tts.as_slice()), + self.fold_tts(tts[]), mtwt::apply_mark(self.mark, ctxt)) } }, @@ -1415,9 +1414,9 @@ fn test_ecfg() -> ExpansionConfig { let attr2 = make_dummy_attr ("bar"); let escape_attr = make_dummy_attr ("macro_escape"); let attrs1 = vec!(attr1.clone(), escape_attr, attr2.clone()); - assert_eq!(contains_macro_escape(attrs1.as_slice()),true); + assert_eq!(contains_macro_escape(attrs1[]),true); let attrs2 = vec!(attr1,attr2); - assert_eq!(contains_macro_escape(attrs2.as_slice()),false); + assert_eq!(contains_macro_escape(attrs2[]),false); } // make a MetaWord outer attribute with the given name @@ -1729,7 +1728,7 @@ fn run_renaming_test(t: &RenamingTest, test_idx: uint) { let string = ident.get(); "xx" == string }).collect(); - let cxbinds: &[&ast::Ident] = cxbinds.as_slice(); + let cxbinds: &[&ast::Ident] = cxbinds[]; let cxbind = match cxbinds { [b] => b, _ => panic!("expected just one binding for ext_cx") diff --git a/src/libsyntax/ext/format.rs b/src/libsyntax/ext/format.rs index 95c7fcc564af..aad4045f00a5 100644 --- a/src/libsyntax/ext/format.rs +++ b/src/libsyntax/ext/format.rs @@ -136,7 +136,7 @@ fn parse_args(ecx: &mut ExtCtxt, sp: Span, allow_method: bool, _ => { ecx.span_err(p.span, format!("expected ident for named argument, found `{}`", - p.this_token_to_string()).as_slice()); + p.this_token_to_string())[]); return (invocation, None); } }; @@ -149,7 +149,7 @@ fn parse_args(ecx: &mut ExtCtxt, sp: Span, allow_method: bool, Some(prev) => { ecx.span_err(e.span, format!("duplicate argument named `{}`", - name).as_slice()); + name)[]); ecx.parse_sess.span_diagnostic.span_note(prev.span, "previously here"); continue } @@ -240,7 +240,7 @@ fn verify_arg_type(&mut self, arg: Position, ty: ArgumentType) { let msg = format!("invalid reference to argument `{}` ({})", arg, self.describe_num_args()); - self.ecx.span_err(self.fmtsp, msg.as_slice()); + self.ecx.span_err(self.fmtsp, msg[]); return; } { @@ -260,7 +260,7 @@ fn verify_arg_type(&mut self, arg: Position, ty: ArgumentType) { Some(e) => e.span, None => { let msg = format!("there is no argument named `{}`", name); - self.ecx.span_err(self.fmtsp, msg.as_slice()); + self.ecx.span_err(self.fmtsp, msg[]); return; } }; @@ -303,19 +303,19 @@ fn verify_same(&self, format!("argument redeclared with type `{}` when \ it was previously `{}`", *ty, - *cur).as_slice()); + *cur)[]); } (&Known(ref cur), _) => { self.ecx.span_err(sp, format!("argument used to format with `{}` was \ attempted to not be used for formatting", - *cur).as_slice()); + *cur)[]); } (_, &Known(ref ty)) => { self.ecx.span_err(sp, format!("argument previously used as a format \ argument attempted to be used as `{}`", - *ty).as_slice()); + *ty)[]); } (_, _) => { self.ecx.span_err(sp, "argument declared with multiple formats"); @@ -380,7 +380,7 @@ fn trans_count(&self, c: parse::Count) -> P { /// Translate the accumulated string literals to a literal expression fn trans_literal_string(&mut self) -> P { let sp = self.fmtsp; - let s = token::intern_and_get_ident(self.literal.as_slice()); + let s = token::intern_and_get_ident(self.literal[]); self.literal.clear(); self.ecx.expr_str(sp, s) } @@ -552,7 +552,7 @@ fn to_expr(mut self, invocation: Invocation) -> P { None => continue // error already generated }; - let name = self.ecx.ident_of(format!("__arg{}", i).as_slice()); + let name = self.ecx.ident_of(format!("__arg{}", i)[]); pats.push(self.ecx.pat_ident(e.span, name)); locals.push(Context::format_arg(self.ecx, e.span, arg_ty, self.ecx.expr_ident(e.span, name))); @@ -569,7 +569,7 @@ fn to_expr(mut self, invocation: Invocation) -> P { }; let lname = self.ecx.ident_of(format!("__arg{}", - *name).as_slice()); + *name)[]); pats.push(self.ecx.pat_ident(e.span, lname)); names[self.name_positions[*name]] = Some(Context::format_arg(self.ecx, e.span, arg_ty, @@ -652,7 +652,7 @@ fn format_arg(ecx: &ExtCtxt, sp: Span, -> P { let trait_ = match *ty { Known(ref tyname) => { - match tyname.as_slice() { + match tyname[] { "" => "Show", "?" => "Show", "e" => "LowerExp", @@ -665,7 +665,7 @@ fn format_arg(ecx: &ExtCtxt, sp: Span, _ => { ecx.span_err(sp, format!("unknown format trait `{}`", - *tyname).as_slice()); + *tyname)[]); "Dummy" } } @@ -760,8 +760,7 @@ pub fn expand_preparsed_format_args(ecx: &mut ExtCtxt, sp: Span, match parser.errors.remove(0) { Some(error) => { cx.ecx.span_err(cx.fmtsp, - format!("invalid format string: {}", - error).as_slice()); + format!("invalid format string: {}", error)[]); return DummyResult::raw_expr(sp); } None => {} diff --git a/src/libsyntax/ext/quote.rs b/src/libsyntax/ext/quote.rs index c7cb41e2ece2..368d4fa84476 100644 --- a/src/libsyntax/ext/quote.rs +++ b/src/libsyntax/ext/quote.rs @@ -474,7 +474,7 @@ pub fn expand_quote_stmt(cx: &mut ExtCtxt, } fn ids_ext(strs: Vec ) -> Vec { - strs.iter().map(|str| str_to_ident((*str).as_slice())).collect() + strs.iter().map(|str| str_to_ident((*str)[])).collect() } fn id_ext(str: &str) -> ast::Ident { @@ -676,7 +676,7 @@ fn mk_tt(cx: &ExtCtxt, tt: &ast::TokenTree) -> Vec> { for i in range(0, tt.len()) { seq.push(tt.get_tt(i)); } - mk_tts(cx, seq.as_slice()) + mk_tts(cx, seq[]) } ast::TtToken(sp, ref tok) => { let e_sp = cx.expr_ident(sp, id_ext("_sp")); @@ -765,7 +765,7 @@ fn expand_tts(cx: &ExtCtxt, sp: Span, tts: &[ast::TokenTree]) let stmt_let_tt = cx.stmt_let(sp, true, id_ext("tt"), cx.expr_vec_ng(sp)); let mut vector = vec!(stmt_let_sp, stmt_let_tt); - vector.extend(mk_tts(cx, tts.as_slice()).into_iter()); + vector.extend(mk_tts(cx, tts[]).into_iter()); let block = cx.expr_block( cx.block_all(sp, Vec::new(), diff --git a/src/libsyntax/ext/source_util.rs b/src/libsyntax/ext/source_util.rs index 570231940aac..7c2c5c1530c9 100644 --- a/src/libsyntax/ext/source_util.rs +++ b/src/libsyntax/ext/source_util.rs @@ -57,7 +57,7 @@ pub fn expand_file(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree]) let topmost = cx.original_span_in_file(); let loc = cx.codemap().lookup_char_pos(topmost.lo); - let filename = token::intern_and_get_ident(loc.file.name.as_slice()); + let filename = token::intern_and_get_ident(loc.file.name[]); base::MacExpr::new(cx.expr_str(topmost, filename)) } @@ -65,7 +65,7 @@ pub fn expand_stringify(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree]) -> Box { let s = pprust::tts_to_string(tts); base::MacExpr::new(cx.expr_str(sp, - token::intern_and_get_ident(s.as_slice()))) + token::intern_and_get_ident(s[]))) } pub fn expand_mod(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree]) @@ -78,7 +78,7 @@ pub fn expand_mod(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree]) .connect("::"); base::MacExpr::new(cx.expr_str( sp, - token::intern_and_get_ident(string.as_slice()))) + token::intern_and_get_ident(string[]))) } /// include! : parse the given file as an expr @@ -137,7 +137,7 @@ pub fn expand_include_str(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree]) cx.span_err(sp, format!("couldn't read {}: {}", file.display(), - e).as_slice()); + e)[]); return DummyResult::expr(sp); } Ok(bytes) => bytes, @@ -147,7 +147,7 @@ pub fn expand_include_str(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree]) // Add this input file to the code map to make it available as // dependency information let filename = file.display().to_string(); - let interned = token::intern_and_get_ident(src.as_slice()); + let interned = token::intern_and_get_ident(src[]); cx.codemap().new_filemap(filename, src); base::MacExpr::new(cx.expr_str(sp, interned)) @@ -155,7 +155,7 @@ pub fn expand_include_str(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree]) Err(_) => { cx.span_err(sp, format!("{} wasn't a utf-8 file", - file.display()).as_slice()); + file.display())[]); return DummyResult::expr(sp); } } @@ -171,9 +171,7 @@ pub fn expand_include_bin(cx: &mut ExtCtxt, sp: Span, tts: &[ast::TokenTree]) match File::open(&file).read_to_end() { Err(e) => { cx.span_err(sp, - format!("couldn't read {}: {}", - file.display(), - e).as_slice()); + format!("couldn't read {}: {}", file.display(), e)[]); return DummyResult::expr(sp); } Ok(bytes) => { diff --git a/src/libsyntax/ext/tt/macro_parser.rs b/src/libsyntax/ext/tt/macro_parser.rs index bc639c32380f..73ef18b8449e 100644 --- a/src/libsyntax/ext/tt/macro_parser.rs +++ b/src/libsyntax/ext/tt/macro_parser.rs @@ -153,7 +153,7 @@ pub fn count_names(ms: &[TokenTree]) -> uint { seq.num_captures } &TtDelimited(_, ref delim) => { - count_names(delim.tts.as_slice()) + count_names(delim.tts[]) } &TtToken(_, MatchNt(..)) => { 1 @@ -165,7 +165,7 @@ pub fn count_names(ms: &[TokenTree]) -> uint { pub fn initial_matcher_pos(ms: Rc>, sep: Option, lo: BytePos) -> Box { - let match_idx_hi = count_names(ms.as_slice()); + let match_idx_hi = count_names(ms[]); let matches = Vec::from_fn(match_idx_hi, |_i| Vec::new()); box MatcherPos { stack: vec![], @@ -229,7 +229,7 @@ fn n_rec(p_s: &ParseSess, m: &TokenTree, res: &[Rc], p_s.span_diagnostic .span_fatal(sp, format!("duplicated bind name: {}", - string.get()).as_slice()) + string.get())[]) } } } @@ -254,13 +254,13 @@ pub fn parse_or_else(sess: &ParseSess, rdr: TtReader, ms: Vec ) -> HashMap> { - match parse(sess, cfg, rdr, ms.as_slice()) { + match parse(sess, cfg, rdr, ms[]) { Success(m) => m, Failure(sp, str) => { - sess.span_diagnostic.span_fatal(sp, str.as_slice()) + sess.span_diagnostic.span_fatal(sp, str[]) } Error(sp, str) => { - sess.span_diagnostic.span_fatal(sp, str.as_slice()) + sess.span_diagnostic.span_fatal(sp, str[]) } } } @@ -416,7 +416,7 @@ pub fn parse(sess: &ParseSess, } } TtToken(sp, SubstNt(..)) => { - return Error(sp, "Cannot transcribe in macro LHS".into_string()) + return Error(sp, "Cannot transcribe in macro LHS".to_string()) } seq @ TtDelimited(..) | seq @ TtToken(_, DocComment(..)) => { let lower_elts = mem::replace(&mut ei.top_elts, Tt(seq)); @@ -446,7 +446,7 @@ pub fn parse(sess: &ParseSess, for dv in eof_eis[0].matches.iter_mut() { v.push(dv.pop().unwrap()); } - return Success(nameize(sess, ms, v.as_slice())); + return Success(nameize(sess, ms, v[])); } else if eof_eis.len() > 1u { return Error(sp, "ambiguity: multiple successful parses".to_string()); } else { @@ -521,7 +521,7 @@ pub fn parse_nt(p: &mut Parser, name: &str) -> Nonterminal { _ => { let token_str = pprust::token_to_string(&p.token); p.fatal((format!("expected ident, found {}", - token_str.as_slice())).as_slice()) + token_str[]))[]) } }, "path" => { @@ -535,8 +535,7 @@ pub fn parse_nt(p: &mut Parser, name: &str) -> Nonterminal { res } _ => { - p.fatal(format!("unsupported builtin nonterminal parser: {}", - name).as_slice()) + p.fatal(format!("unsupported builtin nonterminal parser: {}", name)[]) } } } diff --git a/src/libsyntax/ext/tt/macro_rules.rs b/src/libsyntax/ext/tt/macro_rules.rs index 92c68b7a9c72..08014dc13383 100644 --- a/src/libsyntax/ext/tt/macro_rules.rs +++ b/src/libsyntax/ext/tt/macro_rules.rs @@ -52,7 +52,7 @@ fn ensure_complete_parse(&self, allow_semi: bool) { following", token_str); let span = parser.span; - parser.span_err(span, msg.as_slice()); + parser.span_err(span, msg[]); } } } @@ -124,8 +124,8 @@ fn expand<'cx>(&self, sp, self.name, arg, - self.lhses.as_slice(), - self.rhses.as_slice()) + self.lhses[], + self.rhses[]) } } @@ -160,7 +160,7 @@ fn generic_extension<'cx>(cx: &'cx ExtCtxt, match **lhs { MatchedNonterminal(NtTT(ref lhs_tt)) => { let lhs_tt = match **lhs_tt { - TtDelimited(_, ref delim) => delim.tts.as_slice(), + TtDelimited(_, ref delim) => delim.tts[], _ => cx.span_fatal(sp, "malformed macro lhs") }; // `None` is because we're not interpolating @@ -198,13 +198,13 @@ fn generic_extension<'cx>(cx: &'cx ExtCtxt, best_fail_spot = sp; best_fail_msg = (*msg).clone(); }, - Error(sp, ref msg) => cx.span_fatal(sp, msg.as_slice()) + Error(sp, ref msg) => cx.span_fatal(sp, msg[]) } } _ => cx.bug("non-matcher found in parsed lhses") } } - cx.span_fatal(best_fail_spot, best_fail_msg.as_slice()); + cx.span_fatal(best_fail_spot, best_fail_msg[]); } // Note that macro-by-example's input is also matched against a token tree: diff --git a/src/libsyntax/ext/tt/transcribe.rs b/src/libsyntax/ext/tt/transcribe.rs index 378dbba07fa6..deed0b78e87e 100644 --- a/src/libsyntax/ext/tt/transcribe.rs +++ b/src/libsyntax/ext/tt/transcribe.rs @@ -223,7 +223,7 @@ pub fn tt_next_token(r: &mut TtReader) -> TokenAndSpan { } LisContradiction(ref msg) => { // FIXME #2887 blame macro invoker instead - r.sp_diag.span_fatal(sp.clone(), msg.as_slice()); + r.sp_diag.span_fatal(sp.clone(), msg[]); } LisConstraint(len, _) => { if len == 0 { @@ -280,7 +280,7 @@ pub fn tt_next_token(r: &mut TtReader) -> TokenAndSpan { r.sp_diag.span_fatal( r.cur_span, /* blame the macro writer */ format!("variable '{}' is still repeating at this depth", - token::get_ident(ident)).as_slice()); + token::get_ident(ident))[]); } } } diff --git a/src/libsyntax/feature_gate.rs b/src/libsyntax/feature_gate.rs index 0e0a87c74f84..d53a4b0e8d1c 100644 --- a/src/libsyntax/feature_gate.rs +++ b/src/libsyntax/feature_gate.rs @@ -133,7 +133,7 @@ fn gate_feature(&self, feature: &str, span: Span, explain: &str) { self.span_handler.span_err(span, explain); self.span_handler.span_help(span, format!("add #![feature({})] to the \ crate attributes to enable", - feature).as_slice()); + feature)[]); } } @@ -187,7 +187,7 @@ fn visit_item(&mut self, i: &ast::Item) { } match i.node { ast::ItemForeignMod(ref foreign_module) => { - if attr::contains_name(i.attrs.as_slice(), "link_args") { + if attr::contains_name(i.attrs[], "link_args") { self.gate_feature("link_args", i.span, "the `link_args` attribute is not portable \ across platforms, it is recommended to \ @@ -201,14 +201,14 @@ fn visit_item(&mut self, i: &ast::Item) { } ast::ItemFn(..) => { - if attr::contains_name(i.attrs.as_slice(), "plugin_registrar") { + if attr::contains_name(i.attrs[], "plugin_registrar") { self.gate_feature("plugin_registrar", i.span, "compiler plugins are experimental and possibly buggy"); } } ast::ItemStruct(..) => { - if attr::contains_name(i.attrs.as_slice(), "simd") { + if attr::contains_name(i.attrs[], "simd") { self.gate_feature("simd", i.span, "SIMD types are experimental and possibly buggy"); } @@ -285,7 +285,7 @@ fn visit_mac(&mut self, macro: &ast::Mac) { } fn visit_foreign_item(&mut self, i: &ast::ForeignItem) { - if attr::contains_name(i.attrs.as_slice(), "linkage") { + if attr::contains_name(i.attrs[], "linkage") { self.gate_feature("linkage", i.span, "the `linkage` attribute is experimental \ and not portable across platforms") diff --git a/src/libsyntax/parse/attr.rs b/src/libsyntax/parse/attr.rs index 41fee1556abf..41693d9d47a5 100644 --- a/src/libsyntax/parse/attr.rs +++ b/src/libsyntax/parse/attr.rs @@ -92,8 +92,7 @@ fn parse_attribute(&mut self, permit_inner: bool) -> ast::Attribute { } _ => { let token_str = self.this_token_to_string(); - self.fatal(format!("expected `#`, found `{}`", - token_str).as_slice()); + self.fatal(format!("expected `#`, found `{}`", token_str)[]); } }; diff --git a/src/libsyntax/parse/lexer/comments.rs b/src/libsyntax/parse/lexer/comments.rs index 95bae63f58f6..b8da8365f7e2 100644 --- a/src/libsyntax/parse/lexer/comments.rs +++ b/src/libsyntax/parse/lexer/comments.rs @@ -82,7 +82,7 @@ fn vertical_trim(lines: Vec ) -> Vec { while j > i && lines[j - 1].trim().is_empty() { j -= 1; } - return lines.slice(i, j).iter().map(|x| (*x).clone()).collect(); + return lines[i..j].iter().map(|x| (*x).clone()).collect(); } /// remove a "[ \t]*\*" block from each line, if possible @@ -116,7 +116,7 @@ fn horizontal_trim(lines: Vec ) -> Vec { if can_trim { lines.iter().map(|line| { - line.slice(i + 1, line.len()).to_string() + line[i + 1..line.len()].to_string() }).collect() } else { lines @@ -127,12 +127,12 @@ fn horizontal_trim(lines: Vec ) -> Vec { static ONLINERS: &'static [&'static str] = &["///!", "///", "//!", "//"]; for prefix in ONLINERS.iter() { if comment.starts_with(*prefix) { - return comment.slice_from(prefix.len()).to_string(); + return comment[prefix.len()..].to_string(); } } if comment.starts_with("/*") { - let lines = comment.slice(3u, comment.len() - 2u) + let lines = comment[3u..comment.len() - 2u] .lines_any() .map(|s| s.to_string()) .collect:: >(); @@ -187,7 +187,7 @@ fn read_line_comments(rdr: &mut StringReader, code_to_the_left: bool, let line = rdr.read_one_line_comment(); debug!("{}", line); // Doc comments are not put in comments. - if is_doc_comment(line.as_slice()) { + if is_doc_comment(line[]) { break; } lines.push(line); @@ -224,10 +224,10 @@ fn all_whitespace(s: &str, col: CharPos) -> Option { fn trim_whitespace_prefix_and_push_line(lines: &mut Vec , s: String, col: CharPos) { let len = s.len(); - let s1 = match all_whitespace(s.as_slice(), col) { + let s1 = match all_whitespace(s[], col) { Some(col) => { if col < len { - s.slice(col, len).to_string() + s[col..len].to_string() } else { "".to_string() } @@ -261,7 +261,7 @@ fn read_block_comment(rdr: &mut StringReader, rdr.bump(); rdr.bump(); } - if is_block_doc_comment(curr_line.as_slice()) { + if is_block_doc_comment(curr_line[]) { return } assert!(!curr_line.contains_char('\n')); diff --git a/src/libsyntax/parse/lexer/mod.rs b/src/libsyntax/parse/lexer/mod.rs index da908f46ff61..13d020f6ae31 100644 --- a/src/libsyntax/parse/lexer/mod.rs +++ b/src/libsyntax/parse/lexer/mod.rs @@ -194,7 +194,7 @@ fn fatal_span_char(&self, from_pos: BytePos, to_pos: BytePos, m: &str, c: char) let mut m = m.to_string(); m.push_str(": "); for c in c.escape_default() { m.push(c) } - self.fatal_span_(from_pos, to_pos, m.as_slice()); + self.fatal_span_(from_pos, to_pos, m[]); } /// Report a lexical error spanning [`from_pos`, `to_pos`), appending an @@ -203,7 +203,7 @@ fn err_span_char(&self, from_pos: BytePos, to_pos: BytePos, m: &str, c: char) { let mut m = m.to_string(); m.push_str(": "); for c in c.escape_default() { m.push(c) } - self.err_span_(from_pos, to_pos, m.as_slice()); + self.err_span_(from_pos, to_pos, m[]); } /// Report a lexical error spanning [`from_pos`, `to_pos`), appending the @@ -212,8 +212,8 @@ fn fatal_span_verbose(&self, from_pos: BytePos, to_pos: BytePos, mut m: String) m.push_str(": "); let from = self.byte_offset(from_pos).to_uint(); let to = self.byte_offset(to_pos).to_uint(); - m.push_str(self.filemap.src.as_slice().slice(from, to)); - self.fatal_span_(from_pos, to_pos, m.as_slice()); + m.push_str(self.filemap.src[from..to]); + self.fatal_span_(from_pos, to_pos, m[]); } /// Advance peek_tok and peek_span to refer to the next token, and @@ -299,7 +299,7 @@ fn translate_crlf_(rdr: &StringReader, start: BytePos, while i < s.len() { let str::CharRange { ch, next } = s.char_range_at(i); if ch == '\r' { - if j < i { buf.push_str(s.slice(j, i)); } + if j < i { buf.push_str(s[j..i]); } j = next; if next >= s.len() || s.char_at(next) != '\n' { let pos = start + BytePos(i as u32); @@ -309,7 +309,7 @@ fn translate_crlf_(rdr: &StringReader, start: BytePos, } i = next; } - if j < s.len() { buf.push_str(s.slice_from(j)); } + if j < s.len() { buf.push_str(s[j..]); } buf } } @@ -358,7 +358,7 @@ pub fn nextch_is(&self, c: char) -> bool { pub fn nextnextch(&self) -> Option { let offset = self.byte_offset(self.pos).to_uint(); - let s = self.filemap.deref().src.as_slice(); + let s = self.filemap.deref().src[]; if offset >= s.len() { return None } let str::CharRange { next, .. } = s.char_range_at(offset); if next < s.len() { @@ -554,7 +554,7 @@ fn scan_block_comment(&mut self) -> Option { self.translate_crlf(start_bpos, string, "bare CR not allowed in block doc-comment") } else { string.into_cow() }; - token::DocComment(token::intern(string.as_slice())) + token::DocComment(token::intern(string[])) } else { token::Comment }; @@ -1108,7 +1108,7 @@ fn next_token_inner(&mut self) -> token::Token { // expansion purposes. See #12512 for the gory details of why // this is necessary. let ident = self.with_str_from(start, |lifetime_name| { - str_to_ident(format!("'{}", lifetime_name).as_slice()) + str_to_ident(format!("'{}", lifetime_name)[]) }); // Conjure up a "keyword checking ident" to make sure that diff --git a/src/libsyntax/parse/mod.rs b/src/libsyntax/parse/mod.rs index 3d0877dd4327..8cefb111fd1f 100644 --- a/src/libsyntax/parse/mod.rs +++ b/src/libsyntax/parse/mod.rs @@ -251,17 +251,17 @@ pub fn file_to_filemap(sess: &ParseSess, path: &Path, spanopt: Option) Err(e) => { err(format!("couldn't read {}: {}", path.display(), - e).as_slice()); + e)[]); unreachable!() } }; - match str::from_utf8(bytes.as_slice()) { + match str::from_utf8(bytes[]).ok() { Some(s) => { return string_to_filemap(sess, s.to_string(), path.as_str().unwrap().to_string()) } None => { - err(format!("{} is not UTF-8 encoded", path.display()).as_slice()) + err(format!("{} is not UTF-8 encoded", path.display())[]) } } unreachable!() @@ -391,10 +391,10 @@ pub fn char_lit(lit: &str) -> (char, int) { } let msg = format!("lexer should have rejected a bad character escape {}", lit); - let msg2 = msg.as_slice(); + let msg2 = msg[]; fn esc(len: uint, lit: &str) -> Option<(char, int)> { - num::from_str_radix(lit.slice(2, len), 16) + num::from_str_radix(lit[2..len], 16) .and_then(char::from_u32) .map(|x| (x, len as int)) } @@ -402,10 +402,10 @@ fn esc(len: uint, lit: &str) -> Option<(char, int)> { let unicode_escape: || -> Option<(char, int)> = || if lit.as_bytes()[2] == b'{' { let idx = lit.find('}').expect(msg2); - let subslice = lit.slice(3, idx); + let subslice = lit[3..idx]; num::from_str_radix(subslice, 16) .and_then(char::from_u32) - .map(|x| (x, subslice.char_len() as int + 4)) + .map(|x| (x, subslice.chars().count() as int + 4)) } else { esc(6, lit) }; @@ -429,7 +429,7 @@ pub fn str_lit(lit: &str) -> String { let error = |i| format!("lexer should have rejected {} at {}", lit, i); /// Eat everything up to a non-whitespace - fn eat<'a>(it: &mut iter::Peekable<(uint, char), str::CharOffsets<'a>>) { + fn eat<'a>(it: &mut iter::Peekable<(uint, char), str::CharIndices<'a>>) { loop { match it.peek().map(|x| x.1) { Some(' ') | Some('\n') | Some('\r') | Some('\t') => { @@ -464,7 +464,7 @@ fn eat<'a>(it: &mut iter::Peekable<(uint, char), str::CharOffsets<'a>>) { eat(&mut chars); } else { // otherwise, a normal escape - let (c, n) = char_lit(lit.slice_from(i)); + let (c, n) = char_lit(lit[i..]); for _ in range(0, n - 1) { // we don't need to move past the first \ chars.next(); } @@ -527,7 +527,7 @@ pub fn raw_str_lit(lit: &str) -> String { fn looks_like_width_suffix(first_chars: &[char], s: &str) -> bool { s.len() > 1 && first_chars.contains(&s.char_at(0)) && - s.slice_from(1).chars().all(|c| '0' <= c && c <= '9') + s[1..].chars().all(|c| '0' <= c && c <= '9') } fn filtered_float_lit(data: token::InternedString, suffix: Option<&str>, @@ -540,7 +540,7 @@ fn filtered_float_lit(data: token::InternedString, suffix: Option<&str>, if suf.len() >= 2 && looks_like_width_suffix(&['f'], suf) { // if it looks like a width, lets try to be helpful. sd.span_err(sp, &*format!("illegal width `{}` for float literal, \ - valid widths are 32 and 64", suf.slice_from(1))); + valid widths are 32 and 64", suf[1..])); } else { sd.span_err(sp, &*format!("illegal suffix `{}` for float literal, \ valid suffixes are `f32` and `f64`", suf)); @@ -576,7 +576,7 @@ pub fn byte_lit(lit: &str) -> (u8, uint) { b'\'' => b'\'', b'0' => b'\0', _ => { - match ::std::num::from_str_radix::(lit.slice(2, 4), 16) { + match ::std::num::from_str_radix::(lit[2..4], 16) { Some(c) => if c > 0xFF { panic!(err(2)) @@ -626,7 +626,7 @@ fn eat<'a, I: Iterator<(uint, u8)>>(it: &mut iter::Peekable<(uint, u8), I>) { } _ => { // otherwise, a normal escape - let (c, n) = byte_lit(lit.slice_from(i)); + let (c, n) = byte_lit(lit[i..]); // we don't need to move past the first \ for _ in range(0, n - 1) { chars.next(); @@ -655,7 +655,7 @@ pub fn integer_lit(s: &str, suffix: Option<&str>, sd: &SpanHandler, sp: Span) -> // s can only be ascii, byte indexing is fine let s2 = s.chars().filter(|&c| c != '_').collect::(); - let mut s = s2.as_slice(); + let mut s = s2[]; debug!("integer_lit: {}, {}", s, suffix); @@ -688,7 +688,7 @@ pub fn integer_lit(s: &str, suffix: Option<&str>, sd: &SpanHandler, sp: Span) -> } if base != 10 { - s = s.slice_from(2); + s = s[2..]; } if let Some(suf) = suffix { @@ -710,7 +710,7 @@ pub fn integer_lit(s: &str, suffix: Option<&str>, sd: &SpanHandler, sp: Span) -> if looks_like_width_suffix(&['i', 'u'], suf) { sd.span_err(sp, &*format!("illegal width `{}` for integer literal; \ valid widths are 8, 16, 32 and 64", - suf.slice_from(1))); + suf[1..])); } else { sd.span_err(sp, &*format!("illegal suffix `{}` for numeric literal", suf)); } @@ -808,7 +808,7 @@ fn sp(a: u32, b: u32) -> Span { #[test] fn string_to_tts_macro () { let tts = string_to_tts("macro_rules! zip (($a)=>($a))".to_string()); - let tts: &[ast::TokenTree] = tts.as_slice(); + let tts: &[ast::TokenTree] = tts[]; match tts { [ast::TtToken(_, token::Ident(name_macro_rules, token::Plain)), ast::TtToken(_, token::Not), @@ -816,19 +816,19 @@ fn string_to_tts_macro () { ast::TtDelimited(_, ref macro_delimed)] if name_macro_rules.as_str() == "macro_rules" && name_zip.as_str() == "zip" => { - match macro_delimed.tts.as_slice() { + match macro_delimed.tts[] { [ast::TtDelimited(_, ref first_delimed), ast::TtToken(_, token::FatArrow), ast::TtDelimited(_, ref second_delimed)] if macro_delimed.delim == token::Paren => { - match first_delimed.tts.as_slice() { + match first_delimed.tts[] { [ast::TtToken(_, token::Dollar), ast::TtToken(_, token::Ident(name, token::Plain))] if first_delimed.delim == token::Paren && name.as_str() == "a" => {}, _ => panic!("value 3: {}", **first_delimed), } - match second_delimed.tts.as_slice() { + match second_delimed.tts[] { [ast::TtToken(_, token::Dollar), ast::TtToken(_, token::Ident(name, token::Plain))] if second_delimed.delim == token::Paren @@ -1106,24 +1106,24 @@ fn parser_done(p: Parser){ let use_s = "use foo::bar::baz;"; let vitem = string_to_view_item(use_s.to_string()); let vitem_s = view_item_to_string(&vitem); - assert_eq!(vitem_s.as_slice(), use_s); + assert_eq!(vitem_s[], use_s); let use_s = "use foo::bar as baz;"; let vitem = string_to_view_item(use_s.to_string()); let vitem_s = view_item_to_string(&vitem); - assert_eq!(vitem_s.as_slice(), use_s); + assert_eq!(vitem_s[], use_s); } #[test] fn parse_extern_crate() { let ex_s = "extern crate foo;"; let vitem = string_to_view_item(ex_s.to_string()); let vitem_s = view_item_to_string(&vitem); - assert_eq!(vitem_s.as_slice(), ex_s); + assert_eq!(vitem_s[], ex_s); let ex_s = "extern crate \"foo\" as bar;"; let vitem = string_to_view_item(ex_s.to_string()); let vitem_s = view_item_to_string(&vitem); - assert_eq!(vitem_s.as_slice(), ex_s); + assert_eq!(vitem_s[], ex_s); } fn get_spans_of_pat_idents(src: &str) -> Vec { @@ -1161,9 +1161,9 @@ fn visit_pat(&mut self, p: &'v ast::Pat) { for &src in srcs.iter() { let spans = get_spans_of_pat_idents(src); let Span{lo:lo,hi:hi,..} = spans[0]; - assert!("self" == src.slice(lo.to_uint(), hi.to_uint()), + assert!("self" == src[lo.to_uint()..hi.to_uint()], "\"{}\" != \"self\". src=\"{}\"", - src.slice(lo.to_uint(), hi.to_uint()), src) + src[lo.to_uint()..hi.to_uint()], src) } } @@ -1202,7 +1202,7 @@ fn wb() -> c_int { O_WRONLY as c_int } let docs = item.attrs.iter().filter(|a| a.name().get() == "doc") .map(|a| a.value_str().unwrap().get().to_string()).collect::>(); let b: &[_] = &["/// doc comment".to_string(), "/// line 2".to_string()]; - assert_eq!(docs.as_slice(), b); + assert_eq!(docs[], b); let source = "/** doc comment\r\n * with CRLF */\r\nfn foo() {}".to_string(); let item = parse_item_from_source_str(name, source, Vec::new(), &sess).unwrap(); diff --git a/src/libsyntax/parse/obsolete.rs b/src/libsyntax/parse/obsolete.rs index a6ddcbf9ac41..e3c831c09bac 100644 --- a/src/libsyntax/parse/obsolete.rs +++ b/src/libsyntax/parse/obsolete.rs @@ -113,13 +113,13 @@ fn report(&mut self, kind_str: &str, desc: &str) { self.span_err(sp, - format!("obsolete syntax: {}", kind_str).as_slice()); + format!("obsolete syntax: {}", kind_str)[]); if !self.obsolete_set.contains(&kind) { self.sess .span_diagnostic .handler() - .note(format!("{}", desc).as_slice()); + .note(format!("{}", desc)[]); self.obsolete_set.insert(kind); } } diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 19af118b1905..7e53b28a09c0 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -319,7 +319,7 @@ impl TokenType { fn to_string(&self) -> String { match *self { TokenType::Token(ref t) => format!("`{}`", Parser::token_to_string(t)), - TokenType::Operator => "an operator".into_string(), + TokenType::Operator => "an operator".to_string(), } } } @@ -384,12 +384,12 @@ pub fn unexpected_last(&mut self, t: &token::Token) -> ! { let token_str = Parser::token_to_string(t); let last_span = self.last_span; self.span_fatal(last_span, format!("unexpected token: `{}`", - token_str).as_slice()); + token_str)[]); } pub fn unexpected(&mut self) -> ! { let this_token = self.this_token_to_string(); - self.fatal(format!("unexpected token: `{}`", this_token).as_slice()); + self.fatal(format!("unexpected token: `{}`", this_token)[]); } /// Expect and consume the token t. Signal an error if @@ -403,7 +403,7 @@ pub fn expect(&mut self, t: &token::Token) { let this_token_str = self.this_token_to_string(); self.fatal(format!("expected `{}`, found `{}`", token_str, - this_token_str).as_slice()) + this_token_str)[]) } } else { self.expect_one_of(slice::ref_slice(t), &[]); @@ -420,7 +420,7 @@ fn tokens_to_string(tokens: &[TokenType]) -> String { let mut i = tokens.iter(); // This might be a sign we need a connect method on Iterator. let b = i.next() - .map_or("".into_string(), |t| t.to_string()); + .map_or("".to_string(), |t| t.to_string()); i.enumerate().fold(b, |mut b, (i, ref a)| { if tokens.len() > 2 && i == tokens.len() - 2 { b.push_str(", or "); @@ -444,7 +444,7 @@ fn tokens_to_string(tokens: &[TokenType]) -> String { expected.push_all(&*self.expected_tokens); expected.sort_by(|a, b| a.to_string().cmp(&b.to_string())); expected.dedup(); - let expect = tokens_to_string(expected.as_slice()); + let expect = tokens_to_string(expected[]); let actual = self.this_token_to_string(); self.fatal( (if expected.len() != 1 { @@ -455,7 +455,7 @@ fn tokens_to_string(tokens: &[TokenType]) -> String { (format!("expected {}, found `{}`", expect, actual)) - }).as_slice() + })[] ) } } @@ -488,7 +488,7 @@ pub fn commit_expr(&mut self, e: &Expr, edible: &[token::Token], inedible: &[tok // might be unit-struct construction; check for recoverableinput error. let mut expected = edible.iter().map(|x| x.clone()).collect::>(); expected.push_all(inedible); - self.check_for_erroneous_unit_struct_expecting(expected.as_slice()); + self.check_for_erroneous_unit_struct_expecting(expected[]); } self.expect_one_of(edible, inedible) } @@ -505,9 +505,9 @@ pub fn commit_stmt(&mut self, edible: &[token::Token], inedible: &[token::Token] .as_ref() .map_or(false, |t| t.is_ident() || t.is_path()) { let mut expected = edible.iter().map(|x| x.clone()).collect::>(); - expected.push_all(inedible.as_slice()); + expected.push_all(inedible[]); self.check_for_erroneous_unit_struct_expecting( - expected.as_slice()); + expected[]); } self.expect_one_of(edible, inedible) } @@ -530,7 +530,7 @@ pub fn parse_ident(&mut self) -> ast::Ident { _ => { let token_str = self.this_token_to_string(); self.fatal((format!("expected ident, found `{}`", - token_str)).as_slice()) + token_str))[]) } } } @@ -584,7 +584,7 @@ pub fn expect_keyword(&mut self, kw: keywords::Keyword) { let id_interned_str = token::get_name(kw.to_name()); let token_str = self.this_token_to_string(); self.fatal(format!("expected `{}`, found `{}`", - id_interned_str, token_str).as_slice()) + id_interned_str, token_str)[]) } } @@ -595,7 +595,7 @@ pub fn check_strict_keywords(&mut self) { let span = self.span; self.span_err(span, format!("expected identifier, found keyword `{}`", - token_str).as_slice()); + token_str)[]); } } @@ -604,7 +604,7 @@ pub fn check_reserved_keywords(&mut self) { if self.token.is_reserved_keyword() { let token_str = self.this_token_to_string(); self.fatal(format!("`{}` is a reserved keyword", - token_str).as_slice()) + token_str)[]) } } @@ -624,7 +624,7 @@ fn expect_and(&mut self) { Parser::token_to_string(&token::BinOp(token::And)); self.fatal(format!("expected `{}`, found `{}`", found_token, - token_str).as_slice()) + token_str)[]) } } } @@ -645,7 +645,7 @@ fn expect_or(&mut self) { Parser::token_to_string(&token::BinOp(token::Or)); self.fatal(format!("expected `{}`, found `{}`", token_str, - found_token).as_slice()) + found_token)[]) } } } @@ -711,7 +711,7 @@ fn expect_lt(&mut self) { let token_str = Parser::token_to_string(&token::Lt); self.fatal(format!("expected `{}`, found `{}`", token_str, - found_token).as_slice()) + found_token)[]) } } @@ -763,7 +763,7 @@ pub fn expect_gt(&mut self) { let this_token_str = self.this_token_to_string(); self.fatal(format!("expected `{}`, found `{}`", gt_str, - this_token_str).as_slice()) + this_token_str)[]) } } } @@ -1392,7 +1392,7 @@ pub fn parse_trait_items(&mut self) -> Vec { let (inner_attrs, body) = p.parse_inner_attrs_and_block(); let mut attrs = attrs; - attrs.push_all(inner_attrs.as_slice()); + attrs.push_all(inner_attrs[]); ProvidedMethod(P(ast::Method { attrs: attrs, id: ast::DUMMY_NODE_ID, @@ -1411,7 +1411,7 @@ pub fn parse_trait_items(&mut self) -> Vec { _ => { let token_str = p.this_token_to_string(); p.fatal((format!("expected `;` or `{{`, found `{}`", - token_str)).as_slice()) + token_str))[]) } } } @@ -1606,7 +1606,7 @@ pub fn parse_ty(&mut self) -> P { } else { let this_token_str = self.this_token_to_string(); let msg = format!("expected type, found `{}`", this_token_str); - self.fatal(msg.as_slice()); + self.fatal(msg[]); }; let sp = mk_sp(lo, self.last_span.hi); @@ -1753,14 +1753,14 @@ pub fn lit_from_token(&mut self, tok: &token::Token) -> Lit_ { token::Str_(s) => { (true, - LitStr(token::intern_and_get_ident(parse::str_lit(s.as_str()).as_slice()), + LitStr(token::intern_and_get_ident(parse::str_lit(s.as_str())[]), ast::CookedStr)) } token::StrRaw(s, n) => { (true, LitStr( token::intern_and_get_ident( - parse::raw_str_lit(s.as_str()).as_slice()), + parse::raw_str_lit(s.as_str())[]), ast::RawStr(n))) } token::Binary(i) => @@ -2004,7 +2004,7 @@ pub fn parse_lifetime(&mut self) -> ast::Lifetime { }; } _ => { - self.fatal(format!("expected a lifetime name").as_slice()); + self.fatal(format!("expected a lifetime name")[]); } } } @@ -2042,7 +2042,7 @@ pub fn parse_lifetime_defs(&mut self) -> Vec { let msg = format!("expected `,` or `>` after lifetime \ name, found `{}`", this_token_str); - self.fatal(msg.as_slice()); + self.fatal(msg[]); } } } @@ -2517,7 +2517,7 @@ pub fn parse_dot_or_call_expr_with(&mut self, e0: P) -> P { hi = self.span.hi; self.bump(); - let index = from_str::(n.as_str()); + let index = n.as_str().parse::(); match index { Some(n) => { let id = spanned(dot, hi, n); @@ -2535,16 +2535,16 @@ pub fn parse_dot_or_call_expr_with(&mut self, e0: P) -> P { let last_span = self.last_span; let fstr = n.as_str(); self.span_err(last_span, - format!("unexpected token: `{}`", n.as_str()).as_slice()); + format!("unexpected token: `{}`", n.as_str())[]); if fstr.chars().all(|x| "0123456789.".contains_char(x)) { - let float = match from_str::(fstr) { + let float = match fstr.parse::() { Some(f) => f, None => continue, }; self.span_help(last_span, format!("try parenthesizing the first index; e.g., `(foo.{}){}`", float.trunc() as uint, - float.fract().to_string()[1..]).as_slice()); + float.fract().to_string()[1..])[]); } self.abort_if_errors(); @@ -2716,7 +2716,7 @@ fn parse_non_delim_tt_tok(p: &mut Parser) -> TokenTree { }; let token_str = p.this_token_to_string(); p.fatal(format!("incorrect close delimiter: `{}`", - token_str).as_slice()) + token_str)[]) }, /* we ought to allow different depths of unquotation */ token::Dollar if p.quote_depth > 0u => { @@ -2734,7 +2734,7 @@ fn parse_non_delim_tt_tok(p: &mut Parser) -> TokenTree { let seq = match seq { Spanned { node, .. } => node, }; - let name_num = macro_parser::count_names(seq.as_slice()); + let name_num = macro_parser::count_names(seq[]); TtSequence(mk_sp(sp.lo, p.span.hi), Rc::new(SequenceRepetition { tts: seq, @@ -2885,7 +2885,7 @@ pub fn parse_prefix_expr(&mut self) -> P { let this_token_to_string = self.this_token_to_string(); self.span_err(span, format!("expected expression, found `{}`", - this_token_to_string).as_slice()); + this_token_to_string)[]); let box_span = mk_sp(lo, self.last_span.hi); self.span_help(box_span, "perhaps you meant `box() (foo)` instead?"); @@ -3264,7 +3264,7 @@ fn parse_pat_fields(&mut self) -> (Vec> , bool) if self.token != token::CloseDelim(token::Brace) { let token_str = self.this_token_to_string(); self.fatal(format!("expected `{}`, found `{}`", "}", - token_str).as_slice()) + token_str)[]) } etc = true; break; @@ -3285,7 +3285,7 @@ fn parse_pat_fields(&mut self) -> (Vec> , bool) BindByRef(..) | BindByValue(MutMutable) => { let token_str = self.this_token_to_string(); self.fatal(format!("unexpected `{}`", - token_str).as_slice()) + token_str)[]) } _ => {} } @@ -3563,7 +3563,7 @@ fn parse_pat_ident(&mut self, let span = self.span; let tok_str = self.this_token_to_string(); self.span_fatal(span, - format!("expected identifier, found `{}`", tok_str).as_slice()); + format!("expected identifier, found `{}`", tok_str)[]); } let ident = self.parse_ident(); let last_span = self.last_span; @@ -3664,7 +3664,7 @@ fn check_expected_item(p: &mut Parser, attrs: &[Attribute]) { let lo = self.span.lo; if self.token.is_keyword(keywords::Let) { - check_expected_item(self, item_attrs.as_slice()); + check_expected_item(self, item_attrs[]); self.expect_keyword(keywords::Let); let decl = self.parse_let(); P(spanned(lo, decl.span.hi, StmtDecl(decl, ast::DUMMY_NODE_ID))) @@ -3673,7 +3673,7 @@ fn check_expected_item(p: &mut Parser, attrs: &[Attribute]) { && self.look_ahead(1, |t| *t == token::Not) { // it's a macro invocation: - check_expected_item(self, item_attrs.as_slice()); + check_expected_item(self, item_attrs[]); // Potential trouble: if we allow macros with paths instead of // idents, we'd need to look ahead past the whole path here... @@ -3701,7 +3701,7 @@ fn check_expected_item(p: &mut Parser, attrs: &[Attribute]) { let tok_str = self.this_token_to_string(); self.fatal(format!("expected {}`(` or `{{`, found `{}`", ident_str, - tok_str).as_slice()) + tok_str)[]) }, }; @@ -3749,7 +3749,7 @@ fn check_expected_item(p: &mut Parser, attrs: &[Attribute]) { } } else { let found_attrs = !item_attrs.is_empty(); - let item_err = Parser::expected_item_err(item_attrs.as_slice()); + let item_err = Parser::expected_item_err(item_attrs[]); match self.parse_item_or_view_item(item_attrs, false) { IoviItem(i) => { let hi = i.span.hi; @@ -3793,7 +3793,7 @@ pub fn parse_block(&mut self) -> P { let sp = self.span; let tok = self.this_token_to_string(); self.span_fatal_help(sp, - format!("expected `{{`, found `{}`", tok).as_slice(), + format!("expected `{{`, found `{}`", tok)[], "place this code inside a block"); } @@ -3847,13 +3847,13 @@ fn parse_block_tail_(&mut self, lo: BytePos, s: BlockCheckMode, while self.token != token::CloseDelim(token::Brace) { // parsing items even when they're not allowed lets us give // better error messages and recover more gracefully. - attributes_box.push_all(self.parse_outer_attributes().as_slice()); + attributes_box.push_all(self.parse_outer_attributes()[]); match self.token { token::Semi => { if !attributes_box.is_empty() { let last_span = self.last_span; self.span_err(last_span, - Parser::expected_item_err(attributes_box.as_slice())); + Parser::expected_item_err(attributes_box[])); attributes_box = Vec::new(); } self.bump(); // empty @@ -3944,7 +3944,7 @@ fn parse_block_tail_(&mut self, lo: BytePos, s: BlockCheckMode, if !attributes_box.is_empty() { let last_span = self.last_span; self.span_err(last_span, - Parser::expected_item_err(attributes_box.as_slice())); + Parser::expected_item_err(attributes_box[])); } let hi = self.span.hi; @@ -4362,7 +4362,7 @@ fn expect_self_ident(&mut self) -> ast::Ident { _ => { let token_str = self.this_token_to_string(); self.fatal(format!("expected `self`, found `{}`", - token_str).as_slice()) + token_str)[]) } } } @@ -4516,7 +4516,7 @@ macro_rules! parse_remaining_arguments { _ => { let token_str = self.this_token_to_string(); self.fatal(format!("expected `,` or `)`, found `{}`", - token_str).as_slice()) + token_str)[]) } } } @@ -4692,7 +4692,7 @@ pub fn parse_method(&mut self, let (inner_attrs, body) = self.parse_inner_attrs_and_block(); let body_span = body.span; let mut new_attrs = attrs; - new_attrs.push_all(inner_attrs.as_slice()); + new_attrs.push_all(inner_attrs[]); (ast::MethDecl(ident, generics, abi, @@ -4849,7 +4849,7 @@ fn parse_item_struct(&mut self) -> ItemInfo { if fields.len() == 0 { self.fatal(format!("unit-like struct definition should be \ written as `struct {};`", - token::get_ident(class_name)).as_slice()); + token::get_ident(class_name))[]); } self.bump(); } else if self.check(&token::OpenDelim(token::Paren)) { @@ -4873,7 +4873,7 @@ fn parse_item_struct(&mut self) -> ItemInfo { if fields.len() == 0 { self.fatal(format!("unit-like struct definition should be \ written as `struct {};`", - token::get_ident(class_name)).as_slice()); + token::get_ident(class_name))[]); } self.expect(&token::Semi); } else if self.eat(&token::Semi) { @@ -4884,7 +4884,7 @@ fn parse_item_struct(&mut self) -> ItemInfo { let token_str = self.this_token_to_string(); self.fatal(format!("expected `{}`, `(`, or `;` after struct \ name, found `{}`", "{", - token_str).as_slice()) + token_str)[]) } let _ = ast::DUMMY_NODE_ID; // FIXME: Workaround for crazy bug. @@ -4913,7 +4913,7 @@ pub fn parse_single_struct_field(&mut self, let token_str = self.this_token_to_string(); self.span_fatal_help(span, format!("expected `,`, or `}}`, found `{}`", - token_str).as_slice(), + token_str)[], "struct fields should be separated by commas") } } @@ -4983,7 +4983,7 @@ fn parse_mod_items(&mut self, let mut attrs = self.parse_outer_attributes(); if first { let mut tmp = attrs_remaining.clone(); - tmp.push_all(attrs.as_slice()); + tmp.push_all(attrs[]); attrs = tmp; first = false; } @@ -5000,7 +5000,7 @@ fn parse_mod_items(&mut self, _ => { let token_str = self.this_token_to_string(); self.fatal(format!("expected item, found `{}`", - token_str).as_slice()) + token_str)[]) } } } @@ -5009,7 +5009,7 @@ fn parse_mod_items(&mut self, // We parsed attributes for the first item but didn't find it let last_span = self.last_span; self.span_err(last_span, - Parser::expected_item_err(attrs_remaining.as_slice())); + Parser::expected_item_err(attrs_remaining[])); } ast::Mod { @@ -5079,7 +5079,7 @@ fn eval_src_mod(&mut self, -> (ast::Item_, Vec ) { let mut prefix = Path::new(self.sess.span_diagnostic.cm.span_to_filename(self.span)); prefix.pop(); - let mod_path = Path::new(".").join_many(self.mod_path_stack.as_slice()); + let mod_path = Path::new(".").join_many(self.mod_path_stack[]); let dir_path = prefix.join(&mod_path); let mod_string = token::get_ident(id); let (file_path, owns_directory) = match ::attr::first_attr_value_str_by_name( @@ -5089,8 +5089,8 @@ fn eval_src_mod(&mut self, let mod_name = mod_string.get().to_string(); let default_path_str = format!("{}.rs", mod_name); let secondary_path_str = format!("{}/mod.rs", mod_name); - let default_path = dir_path.join(default_path_str.as_slice()); - let secondary_path = dir_path.join(secondary_path_str.as_slice()); + let default_path = dir_path.join(default_path_str[]); + let secondary_path = dir_path.join(secondary_path_str[]); let default_exists = default_path.exists(); let secondary_exists = secondary_path.exists(); @@ -5105,13 +5105,13 @@ fn eval_src_mod(&mut self, format!("maybe move this module `{0}` \ to its own directory via \ `{0}/mod.rs`", - this_module).as_slice()); + this_module)[]); if default_exists || secondary_exists { self.span_note(id_sp, format!("... or maybe `use` the module \ `{}` instead of possibly \ redeclaring it", - mod_name).as_slice()); + mod_name)[]); } self.abort_if_errors(); } @@ -5122,12 +5122,12 @@ fn eval_src_mod(&mut self, (false, false) => { self.span_fatal_help(id_sp, format!("file not found for module `{}`", - mod_name).as_slice(), + mod_name)[], format!("name the file either {} or {} inside \ the directory {}", default_path_str, secondary_path_str, - dir_path.display()).as_slice()); + dir_path.display())[]); } (true, true) => { self.span_fatal_help( @@ -5136,7 +5136,7 @@ fn eval_src_mod(&mut self, and {}", mod_name, default_path_str, - secondary_path_str).as_slice(), + secondary_path_str)[], "delete or rename one of them to remove the ambiguity"); } } @@ -5158,11 +5158,11 @@ fn eval_src_mod_from_path(&mut self, let mut err = String::from_str("circular modules: "); let len = included_mod_stack.len(); for p in included_mod_stack.slice(i, len).iter() { - err.push_str(p.display().as_cow().as_slice()); + err.push_str(p.display().as_cow()[]); err.push_str(" -> "); } - err.push_str(path.display().as_cow().as_slice()); - self.span_fatal(id_sp, err.as_slice()); + err.push_str(path.display().as_cow()[]); + self.span_fatal(id_sp, err[]); } None => () } @@ -5243,7 +5243,7 @@ fn parse_foreign_mod_items(&mut self, if !attrs_remaining.is_empty() { let last_span = self.last_span; self.span_err(last_span, - Parser::expected_item_err(attrs_remaining.as_slice())); + Parser::expected_item_err(attrs_remaining[])); } assert!(self.token == token::CloseDelim(token::Brace)); ast::ForeignMod { @@ -5284,7 +5284,7 @@ fn parse_item_extern_crate(&mut self, self.span_help(span, format!("perhaps you meant to enclose the crate name `{}` in \ a string?", - the_ident.as_str()).as_slice()); + the_ident.as_str())[]); None } else { None @@ -5310,7 +5310,7 @@ fn parse_item_extern_crate(&mut self, self.span_fatal(span, format!("expected extern crate name but \ found `{}`", - token_str).as_slice()); + token_str)[]); } }; @@ -5408,7 +5408,7 @@ fn parse_enum_def(&mut self, _generics: &ast::Generics) -> EnumDef { self.span_err(start_span, format!("unit-like struct variant should be written \ without braces, as `{},`", - token::get_ident(ident)).as_slice()); + token::get_ident(ident))[]); } kind = StructVariantKind(struct_def); } else if self.check(&token::OpenDelim(token::Paren)) { @@ -5493,7 +5493,7 @@ fn parse_opt_abi(&mut self) -> Option { format!("illegal ABI: expected one of [{}], \ found `{}`", abi::all_names().connect(", "), - the_string).as_slice()); + the_string)[]); None } } @@ -5555,7 +5555,7 @@ fn parse_item_or_view_item(&mut self, format!("`extern mod` is obsolete, use \ `extern crate` instead \ to refer to external \ - crates.").as_slice()) + crates.")[]) } return self.parse_item_extern_crate(lo, visibility, attrs); } @@ -5583,7 +5583,7 @@ fn parse_item_or_view_item(&mut self, let token_str = self.this_token_to_string(); self.span_fatal(span, format!("expected `{}` or `fn`, found `{}`", "{", - token_str).as_slice()); + token_str)[]); } if self.eat_keyword(keywords::Virtual) { @@ -5696,7 +5696,7 @@ fn parse_item_or_view_item(&mut self, if self.eat_keyword(keywords::Mod) { // MODULE ITEM let (ident, item_, extra_attrs) = - self.parse_item_mod(attrs.as_slice()); + self.parse_item_mod(attrs[]); let last_span = self.last_span; let item = self.mk_item(lo, last_span.hi, @@ -6031,7 +6031,7 @@ fn parse_items_and_view_items(&mut self, macros_allowed: bool) -> ParsedItemsAndViewItems { let mut attrs = first_item_attrs; - attrs.push_all(self.parse_outer_attributes().as_slice()); + attrs.push_all(self.parse_outer_attributes()[]); // First, parse view items. let mut view_items : Vec = Vec::new(); let mut items = Vec::new(); @@ -6113,7 +6113,7 @@ fn parse_foreign_items(&mut self, first_item_attrs: Vec , macros_allowed: bool) -> ParsedItemsAndViewItems { let mut attrs = first_item_attrs; - attrs.push_all(self.parse_outer_attributes().as_slice()); + attrs.push_all(self.parse_outer_attributes()[]); let mut foreign_items = Vec::new(); loop { match self.parse_foreign_item(attrs, macros_allowed) { diff --git a/src/libsyntax/parse/token.rs b/src/libsyntax/parse/token.rs index dad369792d7a..9e61eaae3526 100644 --- a/src/libsyntax/parse/token.rs +++ b/src/libsyntax/parse/token.rs @@ -454,7 +454,7 @@ fn mk_fresh_ident_interner() -> IdentInterner { $(init_vec.push($si_str);)* $(init_vec.push($sk_str);)* $(init_vec.push($rk_str);)* - interner::StrInterner::prefill(init_vec.as_slice()) + interner::StrInterner::prefill(init_vec[]) } }} @@ -602,10 +602,14 @@ fn new_from_rc_str(string: RcStr) -> InternedString { #[inline] pub fn get<'a>(&'a self) -> &'a str { - self.string.as_slice() + self.string[] } } +impl Deref for InternedString { + fn deref(&self) -> &str { &*self.string } +} + impl BytesContainer for InternedString { fn container_as_bytes<'a>(&'a self) -> &'a [u8] { // FIXME #12938: This is a workaround for the incorrect signature @@ -620,49 +624,49 @@ fn container_as_bytes<'a>(&'a self) -> &'a [u8] { impl fmt::Show for InternedString { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "{}", self.string.as_slice()) + write!(f, "{}", self.string[]) } } #[allow(deprecated)] impl<'a> Equiv<&'a str> for InternedString { fn equiv(&self, other: & &'a str) -> bool { - (*other) == self.string.as_slice() + (*other) == self.string[] } } impl<'a> PartialEq<&'a str> for InternedString { #[inline(always)] fn eq(&self, other: & &'a str) -> bool { - PartialEq::eq(self.string.as_slice(), *other) + PartialEq::eq(self.string[], *other) } #[inline(always)] fn ne(&self, other: & &'a str) -> bool { - PartialEq::ne(self.string.as_slice(), *other) + PartialEq::ne(self.string[], *other) } } impl<'a> PartialEq for &'a str { #[inline(always)] fn eq(&self, other: &InternedString) -> bool { - PartialEq::eq(*self, other.string.as_slice()) + PartialEq::eq(*self, other.string[]) } #[inline(always)] fn ne(&self, other: &InternedString) -> bool { - PartialEq::ne(*self, other.string.as_slice()) + PartialEq::ne(*self, other.string[]) } } impl, E> Decodable for InternedString { fn decode(d: &mut D) -> Result { Ok(get_name(get_ident_interner().intern( - try!(d.read_str()).as_slice()))) + try!(d.read_str())[]))) } } impl, E> Encodable for InternedString { fn encode(&self, s: &mut S) -> Result<(), E> { - s.emit_str(self.string.as_slice()) + s.emit_str(self.string[]) } } diff --git a/src/libsyntax/print/pp.rs b/src/libsyntax/print/pp.rs index bfa47a46e746..ab0e0f9585c4 100644 --- a/src/libsyntax/print/pp.rs +++ b/src/libsyntax/print/pp.rs @@ -139,12 +139,12 @@ pub fn buf_str(toks: Vec, } s.push_str(format!("{}={}", szs[i], - tok_str(toks[i].clone())).as_slice()); + tok_str(toks[i].clone()))[]); i += 1u; i %= n; } s.push(']'); - return s.into_string(); + s } #[deriving(Copy)] @@ -601,7 +601,7 @@ pub fn print(&mut self, x: Token, l: int) -> io::IoResult<()> { assert_eq!(l, len); // assert!(l <= space); self.space -= len; - self.print_str(s.as_slice()) + self.print_str(s[]) } Eof => { // Eof should never get here. diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs index a9717a526ad9..0d79b7cf9257 100644 --- a/src/libsyntax/print/pprust.rs +++ b/src/libsyntax/print/pprust.rs @@ -30,6 +30,7 @@ use std::{ascii, mem}; use std::io::{mod, IoResult}; +use std::iter; pub enum AnnNode<'a> { NodeIdent(&'a ast::Ident), @@ -113,7 +114,7 @@ pub fn print_crate<'a>(cm: &'a CodeMap, out, ann, is_expanded); - try!(s.print_mod(&krate.module, krate.attrs.as_slice())); + try!(s.print_mod(&krate.module, krate.attrs[])); try!(s.print_remaining_comments()); eof(&mut s.s) } @@ -197,56 +198,56 @@ pub fn binop_to_string(op: BinOpToken) -> &'static str { pub fn token_to_string(tok: &Token) -> String { match *tok { - token::Eq => "=".into_string(), - token::Lt => "<".into_string(), - token::Le => "<=".into_string(), - token::EqEq => "==".into_string(), - token::Ne => "!=".into_string(), - token::Ge => ">=".into_string(), - token::Gt => ">".into_string(), - token::Not => "!".into_string(), - token::Tilde => "~".into_string(), - token::OrOr => "||".into_string(), - token::AndAnd => "&&".into_string(), - token::BinOp(op) => binop_to_string(op).into_string(), + token::Eq => "=".to_string(), + token::Lt => "<".to_string(), + token::Le => "<=".to_string(), + token::EqEq => "==".to_string(), + token::Ne => "!=".to_string(), + token::Ge => ">=".to_string(), + token::Gt => ">".to_string(), + token::Not => "!".to_string(), + token::Tilde => "~".to_string(), + token::OrOr => "||".to_string(), + token::AndAnd => "&&".to_string(), + token::BinOp(op) => binop_to_string(op).to_string(), token::BinOpEq(op) => format!("{}=", binop_to_string(op)), /* Structural symbols */ - token::At => "@".into_string(), - token::Dot => ".".into_string(), - token::DotDot => "..".into_string(), - token::DotDotDot => "...".into_string(), - token::Comma => ",".into_string(), - token::Semi => ";".into_string(), - token::Colon => ":".into_string(), - token::ModSep => "::".into_string(), - token::RArrow => "->".into_string(), - token::LArrow => "<-".into_string(), - token::FatArrow => "=>".into_string(), - token::OpenDelim(token::Paren) => "(".into_string(), - token::CloseDelim(token::Paren) => ")".into_string(), - token::OpenDelim(token::Bracket) => "[".into_string(), - token::CloseDelim(token::Bracket) => "]".into_string(), - token::OpenDelim(token::Brace) => "{".into_string(), - token::CloseDelim(token::Brace) => "}".into_string(), - token::Pound => "#".into_string(), - token::Dollar => "$".into_string(), - token::Question => "?".into_string(), + token::At => "@".to_string(), + token::Dot => ".".to_string(), + token::DotDot => "..".to_string(), + token::DotDotDot => "...".to_string(), + token::Comma => ",".to_string(), + token::Semi => ";".to_string(), + token::Colon => ":".to_string(), + token::ModSep => "::".to_string(), + token::RArrow => "->".to_string(), + token::LArrow => "<-".to_string(), + token::FatArrow => "=>".to_string(), + token::OpenDelim(token::Paren) => "(".to_string(), + token::CloseDelim(token::Paren) => ")".to_string(), + token::OpenDelim(token::Bracket) => "[".to_string(), + token::CloseDelim(token::Bracket) => "]".to_string(), + token::OpenDelim(token::Brace) => "{".to_string(), + token::CloseDelim(token::Brace) => "}".to_string(), + token::Pound => "#".to_string(), + token::Dollar => "$".to_string(), + token::Question => "?".to_string(), /* Literals */ token::Literal(lit, suf) => { let mut out = match lit { token::Byte(b) => format!("b'{}'", b.as_str()), token::Char(c) => format!("'{}'", c.as_str()), - token::Float(c) => c.as_str().into_string(), - token::Integer(c) => c.as_str().into_string(), + token::Float(c) => c.as_str().to_string(), + token::Integer(c) => c.as_str().to_string(), token::Str_(s) => format!("\"{}\"", s.as_str()), token::StrRaw(s, n) => format!("r{delim}\"{string}\"{delim}", - delim="#".repeat(n), + delim=repeat("#", n), string=s.as_str()), token::Binary(v) => format!("b\"{}\"", v.as_str()), token::BinaryRaw(s, n) => format!("br{delim}\"{string}\"{delim}", - delim="#".repeat(n), + delim=repeat("#", n), string=s.as_str()), }; @@ -258,17 +259,17 @@ pub fn token_to_string(tok: &Token) -> String { } /* Name components */ - token::Ident(s, _) => token::get_ident(s).get().into_string(), + token::Ident(s, _) => token::get_ident(s).get().to_string(), token::Lifetime(s) => format!("{}", token::get_ident(s)), - token::Underscore => "_".into_string(), + token::Underscore => "_".to_string(), /* Other */ - token::DocComment(s) => s.as_str().into_string(), + token::DocComment(s) => s.as_str().to_string(), token::SubstNt(s, _) => format!("${}", s), token::MatchNt(s, t, _, _) => format!("${}:{}", s, t), - token::Eof => "".into_string(), - token::Whitespace => " ".into_string(), - token::Comment => "/* */".into_string(), + token::Eof => "".to_string(), + token::Whitespace => " ".to_string(), + token::Comment => "/* */".to_string(), token::Shebang(s) => format!("/* shebang: {}*/", s.as_str()), token::Interpolated(ref nt) => match *nt { @@ -276,12 +277,12 @@ pub fn token_to_string(tok: &Token) -> String { token::NtMeta(ref e) => meta_item_to_string(&**e), token::NtTy(ref e) => ty_to_string(&**e), token::NtPath(ref e) => path_to_string(&**e), - token::NtItem(..) => "an interpolated item".into_string(), - token::NtBlock(..) => "an interpolated block".into_string(), - token::NtStmt(..) => "an interpolated statement".into_string(), - token::NtPat(..) => "an interpolated pattern".into_string(), - token::NtIdent(..) => "an interpolated identifier".into_string(), - token::NtTT(..) => "an interpolated tt".into_string(), + token::NtItem(..) => "an interpolated item".to_string(), + token::NtBlock(..) => "an interpolated block".to_string(), + token::NtStmt(..) => "an interpolated statement".to_string(), + token::NtPat(..) => "an interpolated pattern".to_string(), + token::NtIdent(..) => "an interpolated identifier".to_string(), + token::NtTT(..) => "an interpolated tt".to_string(), } } } @@ -577,7 +578,7 @@ pub fn break_offset_if_not_bol(&mut self, n: uint, pub fn synth_comment(&mut self, text: String) -> IoResult<()> { try!(word(&mut self.s, "/*")); try!(space(&mut self.s)); - try!(word(&mut self.s, text.as_slice())); + try!(word(&mut self.s, text[])); try!(space(&mut self.s)); word(&mut self.s, "*/") } @@ -682,7 +683,7 @@ pub fn print_type(&mut self, ty: &ast::Ty) -> IoResult<()> { } ast::TyTup(ref elts) => { try!(self.popen()); - try!(self.commasep(Inconsistent, elts.as_slice(), + try!(self.commasep(Inconsistent, elts[], |s, ty| s.print_type(&**ty))); if elts.len() == 1 { try!(word(&mut self.s, ",")); @@ -737,10 +738,10 @@ pub fn print_type(&mut self, ty: &ast::Ty) -> IoResult<()> { } ast::TyObjectSum(ref ty, ref bounds) => { try!(self.print_type(&**ty)); - try!(self.print_bounds("+", bounds.as_slice())); + try!(self.print_bounds("+", bounds[])); } ast::TyPolyTraitRef(ref bounds) => { - try!(self.print_bounds("", bounds.as_slice())); + try!(self.print_bounds("", bounds[])); } ast::TyQPath(ref qpath) => { try!(word(&mut self.s, "<")); @@ -775,7 +776,7 @@ pub fn print_foreign_item(&mut self, item: &ast::ForeignItem) -> IoResult<()> { try!(self.hardbreak_if_not_bol()); try!(self.maybe_print_comment(item.span.lo)); - try!(self.print_outer_attributes(item.attrs.as_slice())); + try!(self.print_outer_attributes(item.attrs[])); match item.node { ast::ForeignItemFn(ref decl, ref generics) => { try!(self.print_fn(&**decl, None, abi::Rust, item.ident, generics, @@ -786,7 +787,7 @@ pub fn print_foreign_item(&mut self, } ast::ForeignItemStatic(ref t, m) => { try!(self.head(visibility_qualified(item.vis, - "static").as_slice())); + "static")[])); if m { try!(self.word_space("mut")); } @@ -822,12 +823,12 @@ fn print_typedef(&mut self, typedef: &ast::Typedef) -> IoResult<()> { pub fn print_item(&mut self, item: &ast::Item) -> IoResult<()> { try!(self.hardbreak_if_not_bol()); try!(self.maybe_print_comment(item.span.lo)); - try!(self.print_outer_attributes(item.attrs.as_slice())); + try!(self.print_outer_attributes(item.attrs[])); try!(self.ann.pre(self, NodeItem(item))); match item.node { ast::ItemStatic(ref ty, m, ref expr) => { try!(self.head(visibility_qualified(item.vis, - "static").as_slice())); + "static")[])); if m == ast::MutMutable { try!(self.word_space("mut")); } @@ -844,7 +845,7 @@ pub fn print_item(&mut self, item: &ast::Item) -> IoResult<()> { } ast::ItemConst(ref ty, ref expr) => { try!(self.head(visibility_qualified(item.vis, - "const").as_slice())); + "const")[])); try!(self.print_ident(item.ident)); try!(self.word_space(":")); try!(self.print_type(&**ty)); @@ -867,29 +868,29 @@ pub fn print_item(&mut self, item: &ast::Item) -> IoResult<()> { item.vis )); try!(word(&mut self.s, " ")); - try!(self.print_block_with_attrs(&**body, item.attrs.as_slice())); + try!(self.print_block_with_attrs(&**body, item.attrs[])); } ast::ItemMod(ref _mod) => { try!(self.head(visibility_qualified(item.vis, - "mod").as_slice())); + "mod")[])); try!(self.print_ident(item.ident)); try!(self.nbsp()); try!(self.bopen()); - try!(self.print_mod(_mod, item.attrs.as_slice())); + try!(self.print_mod(_mod, item.attrs[])); try!(self.bclose(item.span)); } ast::ItemForeignMod(ref nmod) => { try!(self.head("extern")); - try!(self.word_nbsp(nmod.abi.to_string().as_slice())); + try!(self.word_nbsp(nmod.abi.to_string()[])); try!(self.bopen()); - try!(self.print_foreign_mod(nmod, item.attrs.as_slice())); + try!(self.print_foreign_mod(nmod, item.attrs[])); try!(self.bclose(item.span)); } ast::ItemTy(ref ty, ref params) => { try!(self.ibox(indent_unit)); try!(self.ibox(0u)); try!(self.word_nbsp(visibility_qualified(item.vis, - "type").as_slice())); + "type")[])); try!(self.print_ident(item.ident)); try!(self.print_generics(params)); try!(self.end()); // end the inner ibox @@ -911,7 +912,7 @@ pub fn print_item(&mut self, item: &ast::Item) -> IoResult<()> { )); } ast::ItemStruct(ref struct_def, ref generics) => { - try!(self.head(visibility_qualified(item.vis,"struct").as_slice())); + try!(self.head(visibility_qualified(item.vis,"struct")[])); try!(self.print_struct(&**struct_def, generics, item.ident, item.span)); } @@ -944,7 +945,7 @@ pub fn print_item(&mut self, item: &ast::Item) -> IoResult<()> { try!(space(&mut self.s)); try!(self.bopen()); - try!(self.print_inner_attributes(item.attrs.as_slice())); + try!(self.print_inner_attributes(item.attrs[])); for impl_item in impl_items.iter() { match *impl_item { ast::MethodImplItem(ref meth) => { @@ -970,7 +971,7 @@ pub fn print_item(&mut self, item: &ast::Item) -> IoResult<()> { try!(self.print_trait_ref(tref)); try!(word(&mut self.s, "?")); } - try!(self.print_bounds(":", bounds.as_slice())); + try!(self.print_bounds(":", bounds[])); try!(self.print_where_clause(generics)); try!(word(&mut self.s, " ")); try!(self.bopen()); @@ -988,7 +989,7 @@ pub fn print_item(&mut self, item: &ast::Item) -> IoResult<()> { try!(self.print_ident(item.ident)); try!(self.cbox(indent_unit)); try!(self.popen()); - try!(self.print_tts(tts.as_slice())); + try!(self.print_tts(tts[])); try!(self.pclose()); try!(word(&mut self.s, ";")); try!(self.end()); @@ -1022,12 +1023,12 @@ pub fn print_enum_def(&mut self, enum_definition: &ast::EnumDef, generics: &ast::Generics, ident: ast::Ident, span: codemap::Span, visibility: ast::Visibility) -> IoResult<()> { - try!(self.head(visibility_qualified(visibility, "enum").as_slice())); + try!(self.head(visibility_qualified(visibility, "enum")[])); try!(self.print_ident(ident)); try!(self.print_generics(generics)); try!(self.print_where_clause(generics)); try!(space(&mut self.s)); - self.print_variants(enum_definition.variants.as_slice(), span) + self.print_variants(enum_definition.variants[], span) } pub fn print_variants(&mut self, @@ -1037,7 +1038,7 @@ pub fn print_variants(&mut self, for v in variants.iter() { try!(self.space_if_not_bol()); try!(self.maybe_print_comment(v.span.lo)); - try!(self.print_outer_attributes(v.node.attrs.as_slice())); + try!(self.print_outer_attributes(v.node.attrs[])); try!(self.ibox(indent_unit)); try!(self.print_variant(&**v)); try!(word(&mut self.s, ",")); @@ -1066,7 +1067,7 @@ pub fn print_struct(&mut self, if !struct_def.fields.is_empty() { try!(self.popen()); try!(self.commasep( - Inconsistent, struct_def.fields.as_slice(), + Inconsistent, struct_def.fields[], |s, field| { match field.node.kind { ast::NamedField(..) => panic!("unexpected named field"), @@ -1094,7 +1095,7 @@ pub fn print_struct(&mut self, ast::NamedField(ident, visibility) => { try!(self.hardbreak_if_not_bol()); try!(self.maybe_print_comment(field.span.lo)); - try!(self.print_outer_attributes(field.node.attrs.as_slice())); + try!(self.print_outer_attributes(field.node.attrs[])); try!(self.print_visibility(visibility)); try!(self.print_ident(ident)); try!(self.word_nbsp(":")); @@ -1118,7 +1119,7 @@ pub fn print_struct(&mut self, pub fn print_tt(&mut self, tt: &ast::TokenTree) -> IoResult<()> { match *tt { ast::TtToken(_, ref tk) => { - try!(word(&mut self.s, token_to_string(tk).as_slice())); + try!(word(&mut self.s, token_to_string(tk)[])); match *tk { parse::token::DocComment(..) => { hardbreak(&mut self.s) @@ -1127,11 +1128,11 @@ pub fn print_tt(&mut self, tt: &ast::TokenTree) -> IoResult<()> { } } ast::TtDelimited(_, ref delimed) => { - try!(word(&mut self.s, token_to_string(&delimed.open_token()).as_slice())); + try!(word(&mut self.s, token_to_string(&delimed.open_token())[])); try!(space(&mut self.s)); - try!(self.print_tts(delimed.tts.as_slice())); + try!(self.print_tts(delimed.tts[])); try!(space(&mut self.s)); - word(&mut self.s, token_to_string(&delimed.close_token()).as_slice()) + word(&mut self.s, token_to_string(&delimed.close_token())[]) }, ast::TtSequence(_, ref seq) => { try!(word(&mut self.s, "$(")); @@ -1141,7 +1142,7 @@ pub fn print_tt(&mut self, tt: &ast::TokenTree) -> IoResult<()> { try!(word(&mut self.s, ")")); match seq.separator { Some(ref tk) => { - try!(word(&mut self.s, token_to_string(tk).as_slice())); + try!(word(&mut self.s, token_to_string(tk)[])); } None => {}, } @@ -1172,7 +1173,7 @@ pub fn print_variant(&mut self, v: &ast::Variant) -> IoResult<()> { if !args.is_empty() { try!(self.popen()); try!(self.commasep(Consistent, - args.as_slice(), + args[], |s, arg| s.print_type(&*arg.ty))); try!(self.pclose()); } @@ -1196,7 +1197,7 @@ pub fn print_variant(&mut self, v: &ast::Variant) -> IoResult<()> { pub fn print_ty_method(&mut self, m: &ast::TypeMethod) -> IoResult<()> { try!(self.hardbreak_if_not_bol()); try!(self.maybe_print_comment(m.span.lo)); - try!(self.print_outer_attributes(m.attrs.as_slice())); + try!(self.print_outer_attributes(m.attrs[])); try!(self.print_ty_fn(None, None, m.unsafety, @@ -1228,7 +1229,7 @@ pub fn print_impl_item(&mut self, ii: &ast::ImplItem) -> IoResult<()> { pub fn print_method(&mut self, meth: &ast::Method) -> IoResult<()> { try!(self.hardbreak_if_not_bol()); try!(self.maybe_print_comment(meth.span.lo)); - try!(self.print_outer_attributes(meth.attrs.as_slice())); + try!(self.print_outer_attributes(meth.attrs[])); match meth.node { ast::MethDecl(ident, ref generics, @@ -1246,7 +1247,7 @@ pub fn print_method(&mut self, meth: &ast::Method) -> IoResult<()> { Some(&explicit_self.node), vis)); try!(word(&mut self.s, " ")); - self.print_block_with_attrs(&**body, meth.attrs.as_slice()) + self.print_block_with_attrs(&**body, meth.attrs[]) }, ast::MethMac(codemap::Spanned { node: ast::MacInvocTT(ref pth, ref tts, _), ..}) => { @@ -1255,7 +1256,7 @@ pub fn print_method(&mut self, meth: &ast::Method) -> IoResult<()> { try!(word(&mut self.s, "! ")); try!(self.cbox(indent_unit)); try!(self.popen()); - try!(self.print_tts(tts.as_slice())); + try!(self.print_tts(tts[])); try!(self.pclose()); try!(word(&mut self.s, ";")); self.end() @@ -1522,7 +1523,7 @@ pub fn print_expr(&mut self, expr: &ast::Expr) -> IoResult<()> { ast::ExprVec(ref exprs) => { try!(self.ibox(indent_unit)); try!(word(&mut self.s, "[")); - try!(self.commasep_exprs(Inconsistent, exprs.as_slice())); + try!(self.commasep_exprs(Inconsistent, exprs[])); try!(word(&mut self.s, "]")); try!(self.end()); } @@ -1542,7 +1543,7 @@ pub fn print_expr(&mut self, expr: &ast::Expr) -> IoResult<()> { try!(word(&mut self.s, "{")); try!(self.commasep_cmnt( Consistent, - fields.as_slice(), + fields[], |s, field| { try!(s.ibox(indent_unit)); try!(s.print_ident(field.ident.node)); @@ -1568,7 +1569,7 @@ pub fn print_expr(&mut self, expr: &ast::Expr) -> IoResult<()> { } ast::ExprTup(ref exprs) => { try!(self.popen()); - try!(self.commasep_exprs(Inconsistent, exprs.as_slice())); + try!(self.commasep_exprs(Inconsistent, exprs[])); if exprs.len() == 1 { try!(word(&mut self.s, ",")); } @@ -1576,7 +1577,7 @@ pub fn print_expr(&mut self, expr: &ast::Expr) -> IoResult<()> { } ast::ExprCall(ref func, ref args) => { try!(self.print_expr_maybe_paren(&**func)); - try!(self.print_call_post(args.as_slice())); + try!(self.print_call_post(args[])); } ast::ExprMethodCall(ident, ref tys, ref args) => { let base_args = args.slice_from(1); @@ -1585,7 +1586,7 @@ pub fn print_expr(&mut self, expr: &ast::Expr) -> IoResult<()> { try!(self.print_ident(ident.node)); if tys.len() > 0u { try!(word(&mut self.s, "::<")); - try!(self.commasep(Inconsistent, tys.as_slice(), + try!(self.commasep(Inconsistent, tys[], |s, ty| s.print_type(&**ty))); try!(word(&mut self.s, ">")); } @@ -1795,11 +1796,11 @@ pub fn print_expr(&mut self, expr: &ast::Expr) -> IoResult<()> { try!(self.print_string(a.asm.get(), a.asm_str_style)); try!(self.word_space(":")); - try!(self.commasep(Inconsistent, a.outputs.as_slice(), + try!(self.commasep(Inconsistent, a.outputs[], |s, &(ref co, ref o, is_rw)| { match co.get().slice_shift_char() { Some(('=', operand)) if is_rw => { - try!(s.print_string(format!("+{}", operand).as_slice(), + try!(s.print_string(format!("+{}", operand)[], ast::CookedStr)) } _ => try!(s.print_string(co.get(), ast::CookedStr)) @@ -1812,7 +1813,7 @@ pub fn print_expr(&mut self, expr: &ast::Expr) -> IoResult<()> { try!(space(&mut self.s)); try!(self.word_space(":")); - try!(self.commasep(Inconsistent, a.inputs.as_slice(), + try!(self.commasep(Inconsistent, a.inputs[], |s, &(ref co, ref o)| { try!(s.print_string(co.get(), ast::CookedStr)); try!(s.popen()); @@ -1823,7 +1824,7 @@ pub fn print_expr(&mut self, expr: &ast::Expr) -> IoResult<()> { try!(space(&mut self.s)); try!(self.word_space(":")); - try!(self.commasep(Inconsistent, a.clobbers.as_slice(), + try!(self.commasep(Inconsistent, a.clobbers[], |s, co| { try!(s.print_string(co.get(), ast::CookedStr)); Ok(()) @@ -1877,7 +1878,7 @@ pub fn print_decl(&mut self, decl: &ast::Decl) -> IoResult<()> { pub fn print_ident(&mut self, ident: ast::Ident) -> IoResult<()> { if self.encode_idents_with_hygiene { let encoded = ident.encode_with_hygiene(); - try!(word(&mut self.s, encoded.as_slice())) + try!(word(&mut self.s, encoded[])) } else { try!(word(&mut self.s, token::get_ident(ident).get())) } @@ -1885,7 +1886,7 @@ pub fn print_ident(&mut self, ident: ast::Ident) -> IoResult<()> { } pub fn print_uint(&mut self, i: uint) -> IoResult<()> { - word(&mut self.s, i.to_string().as_slice()) + word(&mut self.s, i.to_string()[]) } pub fn print_name(&mut self, name: ast::Name) -> IoResult<()> { @@ -1959,7 +1960,7 @@ fn print_path_parameters(&mut self, } try!(self.commasep( Inconsistent, - data.types.as_slice(), + data.types[], |s, ty| s.print_type(&**ty))); comma = true; } @@ -1982,7 +1983,7 @@ fn print_path_parameters(&mut self, try!(word(&mut self.s, "(")); try!(self.commasep( Inconsistent, - data.inputs.as_slice(), + data.inputs[], |s, ty| s.print_type(&**ty))); try!(word(&mut self.s, ")")); @@ -2035,7 +2036,7 @@ pub fn print_pat(&mut self, pat: &ast::Pat) -> IoResult<()> { Some(ref args) => { if !args.is_empty() { try!(self.popen()); - try!(self.commasep(Inconsistent, args.as_slice(), + try!(self.commasep(Inconsistent, args[], |s, p| s.print_pat(&**p))); try!(self.pclose()); } @@ -2047,7 +2048,7 @@ pub fn print_pat(&mut self, pat: &ast::Pat) -> IoResult<()> { try!(self.nbsp()); try!(self.word_space("{")); try!(self.commasep_cmnt( - Consistent, fields.as_slice(), + Consistent, fields[], |s, f| { try!(s.cbox(indent_unit)); if !f.node.is_shorthand { @@ -2068,7 +2069,7 @@ pub fn print_pat(&mut self, pat: &ast::Pat) -> IoResult<()> { ast::PatTup(ref elts) => { try!(self.popen()); try!(self.commasep(Inconsistent, - elts.as_slice(), + elts[], |s, p| s.print_pat(&**p))); if elts.len() == 1 { try!(word(&mut self.s, ",")); @@ -2093,7 +2094,7 @@ pub fn print_pat(&mut self, pat: &ast::Pat) -> IoResult<()> { ast::PatVec(ref before, ref slice, ref after) => { try!(word(&mut self.s, "[")); try!(self.commasep(Inconsistent, - before.as_slice(), + before[], |s, p| s.print_pat(&**p))); for p in slice.iter() { if !before.is_empty() { try!(self.word_space(",")); } @@ -2107,7 +2108,7 @@ pub fn print_pat(&mut self, pat: &ast::Pat) -> IoResult<()> { if !after.is_empty() { try!(self.word_space(",")); } } try!(self.commasep(Inconsistent, - after.as_slice(), + after[], |s, p| s.print_pat(&**p))); try!(word(&mut self.s, "]")); } @@ -2124,7 +2125,7 @@ fn print_arm(&mut self, arm: &ast::Arm) -> IoResult<()> { } try!(self.cbox(indent_unit)); try!(self.ibox(0u)); - try!(self.print_outer_attributes(arm.attrs.as_slice())); + try!(self.print_outer_attributes(arm.attrs[])); let mut first = true; for p in arm.pats.iter() { if first { @@ -2224,7 +2225,7 @@ pub fn print_fn_args(&mut self, decl: &ast::FnDecl, // HACK(eddyb) ignore the separately printed self argument. let args = if first { - decl.inputs.as_slice() + decl.inputs[] } else { decl.inputs.slice_from(1) }; @@ -2386,7 +2387,7 @@ pub fn print_generics(&mut self, ints.push(i); } - try!(self.commasep(Inconsistent, ints.as_slice(), |s, &idx| { + try!(self.commasep(Inconsistent, ints[], |s, &idx| { if idx < generics.lifetimes.len() { let lifetime = &generics.lifetimes[idx]; s.print_lifetime_def(lifetime) @@ -2407,7 +2408,7 @@ pub fn print_ty_param(&mut self, param: &ast::TyParam) -> IoResult<()> { try!(self.word_space("?")); } try!(self.print_ident(param.ident)); - try!(self.print_bounds(":", param.bounds.as_slice())); + try!(self.print_bounds(":", param.bounds[])); match param.default { Some(ref default) => { try!(space(&mut self.s)); @@ -2483,7 +2484,7 @@ pub fn print_meta_item(&mut self, item: &ast::MetaItem) -> IoResult<()> { try!(word(&mut self.s, name.get())); try!(self.popen()); try!(self.commasep(Consistent, - items.as_slice(), + items[], |s, i| s.print_meta_item(&**i))); try!(self.pclose()); } @@ -2519,7 +2520,7 @@ pub fn print_view_path(&mut self, vp: &ast::ViewPath) -> IoResult<()> { try!(self.print_path(path, false)); try!(word(&mut self.s, "::{")); } - try!(self.commasep(Inconsistent, idents.as_slice(), |s, w| { + try!(self.commasep(Inconsistent, idents[], |s, w| { match w.node { ast::PathListIdent { name, .. } => { s.print_ident(name) @@ -2537,7 +2538,7 @@ pub fn print_view_path(&mut self, vp: &ast::ViewPath) -> IoResult<()> { pub fn print_view_item(&mut self, item: &ast::ViewItem) -> IoResult<()> { try!(self.hardbreak_if_not_bol()); try!(self.maybe_print_comment(item.span.lo)); - try!(self.print_outer_attributes(item.attrs.as_slice())); + try!(self.print_outer_attributes(item.attrs[])); try!(self.print_visibility(item.vis)); match item.node { ast::ViewItemExternCrate(id, ref optional_path, _) => { @@ -2679,7 +2680,7 @@ pub fn print_ty_fn(&mut self, try!(self.pclose()); } - try!(self.print_bounds(":", bounds.as_slice())); + try!(self.print_bounds(":", bounds[])); try!(self.print_fn_output(decl)); @@ -2738,7 +2739,7 @@ pub fn print_literal(&mut self, lit: &ast::Lit) -> IoResult<()> { try!(self.maybe_print_comment(lit.span.lo)); match self.next_lit(lit.span.lo) { Some(ref ltrl) => { - return word(&mut self.s, (*ltrl).lit.as_slice()); + return word(&mut self.s, (*ltrl).lit[]); } _ => () } @@ -2748,7 +2749,7 @@ pub fn print_literal(&mut self, lit: &ast::Lit) -> IoResult<()> { let mut res = String::from_str("b'"); ascii::escape_default(byte, |c| res.push(c as char)); res.push('\''); - word(&mut self.s, res.as_slice()) + word(&mut self.s, res[]) } ast::LitChar(ch) => { let mut res = String::from_str("'"); @@ -2756,27 +2757,27 @@ pub fn print_literal(&mut self, lit: &ast::Lit) -> IoResult<()> { res.push(c); } res.push('\''); - word(&mut self.s, res.as_slice()) + word(&mut self.s, res[]) } ast::LitInt(i, t) => { match t { ast::SignedIntLit(st, ast::Plus) => { word(&mut self.s, - ast_util::int_ty_to_string(st, Some(i as i64)).as_slice()) + ast_util::int_ty_to_string(st, Some(i as i64))[]) } ast::SignedIntLit(st, ast::Minus) => { let istr = ast_util::int_ty_to_string(st, Some(-(i as i64))); word(&mut self.s, - format!("-{}", istr).as_slice()) + format!("-{}", istr)[]) } ast::UnsignedIntLit(ut) => { - word(&mut self.s, ast_util::uint_ty_to_string(ut, Some(i)).as_slice()) + word(&mut self.s, ast_util::uint_ty_to_string(ut, Some(i))[]) } ast::UnsuffixedIntLit(ast::Plus) => { - word(&mut self.s, format!("{}", i).as_slice()) + word(&mut self.s, format!("{}", i)[]) } ast::UnsuffixedIntLit(ast::Minus) => { - word(&mut self.s, format!("-{}", i).as_slice()) + word(&mut self.s, format!("-{}", i)[]) } } } @@ -2785,7 +2786,7 @@ pub fn print_literal(&mut self, lit: &ast::Lit) -> IoResult<()> { format!( "{}{}", f.get(), - ast_util::float_ty_to_string(t).as_slice()).as_slice()) + ast_util::float_ty_to_string(t)[])[]) } ast::LitFloatUnsuffixed(ref f) => word(&mut self.s, f.get()), ast::LitBool(val) => { @@ -2797,7 +2798,7 @@ pub fn print_literal(&mut self, lit: &ast::Lit) -> IoResult<()> { ascii::escape_default(ch as u8, |ch| escaped.push(ch as char)); } - word(&mut self.s, format!("b\"{}\"", escaped).as_slice()) + word(&mut self.s, format!("b\"{}\"", escaped)[]) } } } @@ -2838,7 +2839,7 @@ pub fn print_comment(&mut self, comments::Mixed => { assert_eq!(cmnt.lines.len(), 1u); try!(zerobreak(&mut self.s)); - try!(word(&mut self.s, cmnt.lines[0].as_slice())); + try!(word(&mut self.s, cmnt.lines[0][])); zerobreak(&mut self.s) } comments::Isolated => { @@ -2847,7 +2848,7 @@ pub fn print_comment(&mut self, // Don't print empty lines because they will end up as trailing // whitespace if !line.is_empty() { - try!(word(&mut self.s, line.as_slice())); + try!(word(&mut self.s, line[])); } try!(hardbreak(&mut self.s)); } @@ -2856,13 +2857,13 @@ pub fn print_comment(&mut self, comments::Trailing => { try!(word(&mut self.s, " ")); if cmnt.lines.len() == 1u { - try!(word(&mut self.s, cmnt.lines[0].as_slice())); + try!(word(&mut self.s, cmnt.lines[0][])); hardbreak(&mut self.s) } else { try!(self.ibox(0u)); for line in cmnt.lines.iter() { if !line.is_empty() { - try!(word(&mut self.s, line.as_slice())); + try!(word(&mut self.s, line[])); } try!(hardbreak(&mut self.s)); } @@ -2891,11 +2892,11 @@ pub fn print_string(&mut self, st: &str, } ast::RawStr(n) => { (format!("r{delim}\"{string}\"{delim}", - delim="#".repeat(n), + delim=repeat("#", n), string=st)) } }; - word(&mut self.s, st.as_slice()) + word(&mut self.s, st[]) } pub fn next_comment(&mut self) -> Option { @@ -2926,7 +2927,7 @@ pub fn print_opt_abi_and_extern_if_nondefault(&mut self, Some(abi::Rust) => Ok(()), Some(abi) => { try!(self.word_nbsp("extern")); - self.word_nbsp(abi.to_string().as_slice()) + self.word_nbsp(abi.to_string()[]) } None => Ok(()) } @@ -2937,7 +2938,7 @@ pub fn print_extern_opt_abi(&mut self, match opt_abi { Some(abi) => { try!(self.word_nbsp("extern")); - self.word_nbsp(abi.to_string().as_slice()) + self.word_nbsp(abi.to_string()[]) } None => Ok(()) } @@ -2953,7 +2954,7 @@ pub fn print_fn_header_info(&mut self, if abi != abi::Rust { try!(self.word_nbsp("extern")); - try!(self.word_nbsp(abi.to_string().as_slice())); + try!(self.word_nbsp(abi.to_string()[])); } word(&mut self.s, "fn") @@ -2967,6 +2968,8 @@ pub fn print_unsafety(&mut self, s: ast::Unsafety) -> IoResult<()> { } } +fn repeat(s: &str, n: uint) -> String { iter::repeat(s).take(n).collect() } + #[cfg(test)] mod test { use super::*; diff --git a/src/libsyntax/std_inject.rs b/src/libsyntax/std_inject.rs index e98be046586e..e1c8ff5011b2 100644 --- a/src/libsyntax/std_inject.rs +++ b/src/libsyntax/std_inject.rs @@ -40,7 +40,7 @@ pub fn maybe_inject_prelude(krate: ast::Crate) -> ast::Crate { } fn use_std(krate: &ast::Crate) -> bool { - !attr::contains_name(krate.attrs.as_slice(), "no_std") + !attr::contains_name(krate.attrs[], "no_std") } fn no_prelude(attrs: &[ast::Attribute]) -> bool { @@ -56,7 +56,7 @@ fn fold_crate(&mut self, mut krate: ast::Crate) -> ast::Crate { // The name to use in `extern crate "name" as std;` let actual_crate_name = match self.alt_std_name { - Some(ref s) => token::intern_and_get_ident(s.as_slice()), + Some(ref s) => token::intern_and_get_ident(s[]), None => token::intern_and_get_ident("std"), }; @@ -118,7 +118,7 @@ fn fold_crate(&mut self, mut krate: ast::Crate) -> ast::Crate { attr::mark_used(&no_std_attr); krate.attrs.push(no_std_attr); - if !no_prelude(krate.attrs.as_slice()) { + if !no_prelude(krate.attrs[]) { // only add `use std::prelude::*;` if there wasn't a // `#![no_implicit_prelude]` at the crate level. // fold_mod() will insert glob path. @@ -138,7 +138,7 @@ fn fold_crate(&mut self, mut krate: ast::Crate) -> ast::Crate { } fn fold_item(&mut self, item: P) -> SmallVector> { - if !no_prelude(item.attrs.as_slice()) { + if !no_prelude(item.attrs[]) { // only recur if there wasn't `#![no_implicit_prelude]` // on this item, i.e. this means that the prelude is not // implicitly imported though the whole subtree diff --git a/src/libsyntax/test.rs b/src/libsyntax/test.rs index 155cabb153cf..bc7dda8c44ac 100644 --- a/src/libsyntax/test.rs +++ b/src/libsyntax/test.rs @@ -73,14 +73,14 @@ pub fn modify_for_testing(sess: &ParseSess, // We generate the test harness when building in the 'test' // configuration, either with the '--test' or '--cfg test' // command line options. - let should_test = attr::contains_name(krate.config.as_slice(), "test"); + let should_test = attr::contains_name(krate.config[], "test"); // Check for #[reexport_test_harness_main = "some_name"] which // creates a `use some_name = __test::main;`. This needs to be // unconditional, so that the attribute is still marked as used in // non-test builds. let reexport_test_harness_main = - attr::first_attr_value_str_by_name(krate.attrs.as_slice(), + attr::first_attr_value_str_by_name(krate.attrs[], "reexport_test_harness_main"); if should_test { @@ -119,7 +119,7 @@ fn fold_item(&mut self, i: P) -> SmallVector> { self.cx.path.push(ident); } debug!("current path: {}", - ast_util::path_name_i(self.cx.path.as_slice())); + ast_util::path_name_i(self.cx.path[])); if is_test_fn(&self.cx, &*i) || is_bench_fn(&self.cx, &*i) { match i.node { @@ -277,8 +277,8 @@ fn strip_test_functions(krate: ast::Crate) -> ast::Crate { // When not compiling with --test we should not compile the // #[test] functions config::strip_items(krate, |attrs| { - !attr::contains_name(attrs.as_slice(), "test") && - !attr::contains_name(attrs.as_slice(), "bench") + !attr::contains_name(attrs[], "test") && + !attr::contains_name(attrs[], "bench") }) } @@ -291,7 +291,7 @@ enum HasTestSignature { fn is_test_fn(cx: &TestCtxt, i: &ast::Item) -> bool { - let has_test_attr = attr::contains_name(i.attrs.as_slice(), "test"); + let has_test_attr = attr::contains_name(i.attrs[], "test"); fn has_test_signature(i: &ast::Item) -> HasTestSignature { match &i.node { @@ -329,7 +329,7 @@ fn has_test_signature(i: &ast::Item) -> HasTestSignature { } fn is_bench_fn(cx: &TestCtxt, i: &ast::Item) -> bool { - let has_bench_attr = attr::contains_name(i.attrs.as_slice(), "bench"); + let has_bench_attr = attr::contains_name(i.attrs[], "bench"); fn has_test_signature(i: &ast::Item) -> bool { match i.node { @@ -384,7 +384,7 @@ fn should_fail(i: &ast::Item) -> ShouldFail { mod __test { extern crate test (name = "test", vers = "..."); fn main() { - test::test_main_static(::os::args().as_slice(), tests) + test::test_main_static(::os::args()[], tests) } static tests : &'static [test::TestDescAndFn] = &[ @@ -510,8 +510,8 @@ fn mk_tests(cx: &TestCtxt) -> P { } fn is_test_crate(krate: &ast::Crate) -> bool { - match attr::find_crate_name(krate.attrs.as_slice()) { - Some(ref s) if "test" == s.get().as_slice() => true, + match attr::find_crate_name(krate.attrs[]) { + Some(ref s) if "test" == s.get()[] => true, _ => false } } @@ -551,11 +551,11 @@ fn mk_test_desc_and_fn_rec(cx: &TestCtxt, test: &Test) -> P { // creates $name: $expr let field = |name, expr| ecx.field_imm(span, ecx.ident_of(name), expr); - debug!("encoding {}", ast_util::path_name_i(path.as_slice())); + debug!("encoding {}", ast_util::path_name_i(path[])); // path to the #[test] function: "foo::bar::baz" - let path_string = ast_util::path_name_i(path.as_slice()); - let name_expr = ecx.expr_str(span, token::intern_and_get_ident(path_string.as_slice())); + let path_string = ast_util::path_name_i(path[]); + let name_expr = ecx.expr_str(span, token::intern_and_get_ident(path_string[])); // self::test::StaticTestName($name_expr) let name_expr = ecx.expr_call(span, diff --git a/src/libsyntax/util/interner.rs b/src/libsyntax/util/interner.rs index 590a04ce2210..97eb43165833 100644 --- a/src/libsyntax/util/interner.rs +++ b/src/libsyntax/util/interner.rs @@ -95,41 +95,37 @@ pub struct RcStr { string: Rc, } +impl RcStr { + pub fn new(string: &str) -> RcStr { + RcStr { + string: Rc::new(string.to_string()), + } + } +} + impl Eq for RcStr {} impl Ord for RcStr { fn cmp(&self, other: &RcStr) -> Ordering { - self.as_slice().cmp(other.as_slice()) - } -} - -impl Str for RcStr { - #[inline] - fn as_slice<'a>(&'a self) -> &'a str { - let s: &'a str = self.string.as_slice(); - s + self[].cmp(other[]) } } impl fmt::Show for RcStr { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { use std::fmt::Show; - self.as_slice().fmt(f) + self[].fmt(f) } } impl BorrowFrom for str { fn borrow_from(owned: &RcStr) -> &str { - owned.string.as_slice() + owned.string[] } } -impl RcStr { - pub fn new(string: &str) -> RcStr { - RcStr { - string: Rc::new(string.into_string()), - } - } +impl Deref for RcStr { + fn deref(&self) -> &str { self.string[] } } /// A StrInterner differs from Interner in that it accepts diff --git a/src/libterm/terminfo/mod.rs b/src/libterm/terminfo/mod.rs index 65f8415835a3..d944d0362fbe 100644 --- a/src/libterm/terminfo/mod.rs +++ b/src/libterm/terminfo/mod.rs @@ -180,7 +180,7 @@ pub fn new(out: T) -> Option+Send+'static>> { } }; - let entry = open(term.as_slice()); + let entry = open(term[]); if entry.is_err() { if os::getenv("MSYSCON").map_or(false, |s| { "mintty.exe" == s diff --git a/src/libterm/terminfo/searcher.rs b/src/libterm/terminfo/searcher.rs index 33bfd69f71bb..395fac52d8da 100644 --- a/src/libterm/terminfo/searcher.rs +++ b/src/libterm/terminfo/searcher.rs @@ -61,13 +61,13 @@ pub fn get_dbpath_for_term(term: &str) -> Option> { for p in dirs_to_search.iter() { if p.exists() { let f = first_char.to_string(); - let newp = p.join_many(&[f.as_slice(), term]); + let newp = p.join_many(&[f[], term]); if newp.exists() { return Some(box newp); } // on some installations the dir is named after the hex of the char (e.g. OS X) let f = format!("{:x}", first_char as uint); - let newp = p.join_many(&[f.as_slice(), term]); + let newp = p.join_many(&[f[], term]); if newp.exists() { return Some(box newp); } diff --git a/src/libtest/lib.rs b/src/libtest/lib.rs index 5b04a1fed896..1870f162eceb 100644 --- a/src/libtest/lib.rs +++ b/src/libtest/lib.rs @@ -65,6 +65,7 @@ use std::io::stdio::StdWriter; use std::io::{File, ChanReader, ChanWriter}; use std::io; +use std::iter::repeat; use std::num::{Float, FloatMath, Int}; use std::os; use std::str::FromStr; @@ -121,7 +122,7 @@ impl TestDesc { fn padded_name(&self, column_count: uint, align: NamePadding) -> String { let mut name = String::from_str(self.name.as_slice()); let fill = column_count.saturating_sub(name.len()); - let mut pad = " ".repeat(fill); + let mut pad = repeat(" ").take(fill).collect::(); match align { PadNone => name, PadOnLeft => { @@ -426,7 +427,7 @@ pub fn parse_opts(args: &[String]) -> Option { let ratchet_noise_percent = matches.opt_str("ratchet-noise-percent"); let ratchet_noise_percent = - ratchet_noise_percent.map(|s| from_str::(s.as_slice()).unwrap()); + ratchet_noise_percent.map(|s| s.as_slice().parse::().unwrap()); let save_metrics = matches.opt_str("save-metrics"); let save_metrics = save_metrics.map(|s| Path::new(s)); @@ -489,7 +490,8 @@ pub fn opt_shard(maybestr: Option) -> Option<(uint,uint)> { None => None, Some(s) => { let mut it = s.split('.'); - match (it.next().and_then(from_str::), it.next().and_then(from_str::), + match (it.next().and_then(|s| s.parse::()), + it.next().and_then(|s| s.parse::()), it.next()) { (Some(a), Some(b), None) => { if a <= 0 || a > b { diff --git a/src/libunicode/u_str.rs b/src/libunicode/u_str.rs index 5d7d29516282..7d59e3de7b1e 100644 --- a/src/libunicode/u_str.rs +++ b/src/libunicode/u_str.rs @@ -15,22 +15,16 @@ //! This module provides functionality to `str` that requires the Unicode methods provided by the //! UnicodeChar trait. +use self::GraphemeState::*; use core::prelude::*; use core::char; use core::cmp; -use core::iter::{DoubleEndedIterator, DoubleEndedIteratorExt}; -use core::iter::{Filter, AdditiveIterator, Iterator, IteratorExt}; use core::iter::{Filter, AdditiveIterator}; -use core::kinds::Sized; use core::mem; use core::num::Int; -use core::option::Option::{None, Some}; -use core::option::Option; -use core::slice::SliceExt; use core::slice; -use core::str::{CharSplits, StrPrelude}; -use core::str::{CharSplits}; +use core::str::CharSplits; use u_char::UnicodeChar; use tables::grapheme::GraphemeCat; @@ -39,106 +33,20 @@ /// FIXME: This should be opaque #[stable] pub struct Words<'a> { - inner: Filter<'a, &'a str, CharSplits<'a, |char|:'a -> bool>, - fn(&&str) -> bool>, + inner: Filter<&'a str, CharSplits<'a, fn(char) -> bool>, fn(&&str) -> bool>, } /// Methods for Unicode string slices +#[allow(missing_docs)] // docs in libcollections pub trait UnicodeStr for Sized? { - /// Returns an iterator over the - /// [grapheme clusters](http://www.unicode.org/reports/tr29/#Grapheme_Cluster_Boundaries) - /// of the string. - /// - /// If `is_extended` is true, the iterator is over the *extended grapheme clusters*; - /// otherwise, the iterator is over the *legacy grapheme clusters*. - /// [UAX#29](http://www.unicode.org/reports/tr29/#Grapheme_Cluster_Boundaries) - /// recommends extended grapheme cluster boundaries for general processing. - /// - /// # Example - /// - /// ```rust - /// let gr1 = "a\u{0310}e\u{0301}o\u{0308}\u{0332}".graphemes(true).collect::>(); - /// let b: &[_] = &["a\u{0310}", "e\u{0301}", "o\u{0308}\u{0332}"]; - /// assert_eq!(gr1.as_slice(), b); - /// let gr2 = "a\r\nb🇷🇺🇸🇹".graphemes(true).collect::>(); - /// let b: &[_] = &["a", "\r\n", "b", "🇷🇺🇸🇹"]; - /// assert_eq!(gr2.as_slice(), b); - /// ``` fn graphemes<'a>(&'a self, is_extended: bool) -> Graphemes<'a>; - - /// Returns an iterator over the grapheme clusters of self and their byte offsets. - /// See `graphemes()` method for more information. - /// - /// # Example - /// - /// ```rust - /// let gr_inds = "a̐éö̲\r\n".grapheme_indices(true).collect::>(); - /// let b: &[_] = &[(0u, "a̐"), (3, "é"), (6, "ö̲"), (11, "\r\n")]; - /// assert_eq!(gr_inds.as_slice(), b); - /// ``` fn grapheme_indices<'a>(&'a self, is_extended: bool) -> GraphemeIndices<'a>; - - /// An iterator over the words of a string (subsequences separated - /// by any sequence of whitespace). Sequences of whitespace are - /// collapsed, so empty "words" are not included. - /// - /// # Example - /// - /// ```rust - /// let some_words = " Mary had\ta little \n\t lamb"; - /// let v: Vec<&str> = some_words.words().collect(); - /// assert_eq!(v, vec!["Mary", "had", "a", "little", "lamb"]); - /// ``` - #[stable] fn words<'a>(&'a self) -> Words<'a>; - - /// Returns true if the string contains only whitespace. - /// - /// Whitespace characters are determined by `char::is_whitespace`. - /// - /// # Example - /// - /// ```rust - /// assert!(" \t\n".is_whitespace()); - /// assert!("".is_whitespace()); - /// - /// assert!( !"abc".is_whitespace()); - /// ``` fn is_whitespace(&self) -> bool; - - /// Returns true if the string contains only alphanumeric code - /// points. - /// - /// Alphanumeric characters are determined by `char::is_alphanumeric`. - /// - /// # Example - /// - /// ```rust - /// assert!("Löwe老虎Léopard123".is_alphanumeric()); - /// assert!("".is_alphanumeric()); - /// - /// assert!( !" &*~".is_alphanumeric()); - /// ``` fn is_alphanumeric(&self) -> bool; - - /// Returns a string's displayed width in columns, treating control - /// characters as zero-width. - /// - /// `is_cjk` determines behavior for characters in the Ambiguous category: - /// if `is_cjk` is `true`, these are 2 columns wide; otherwise, they are 1. - /// In CJK locales, `is_cjk` should be `true`, else it should be `false`. - /// [Unicode Standard Annex #11](http://www.unicode.org/reports/tr11/) - /// recommends that these characters be treated as 1 column (i.e., - /// `is_cjk` = `false`) if the locale is unknown. fn width(&self, is_cjk: bool) -> uint; - - /// Returns a string with leading and trailing whitespace removed. fn trim<'a>(&'a self) -> &'a str; - - /// Returns a string with leading whitespace removed. fn trim_left<'a>(&'a self) -> &'a str; - - /// Returns a string with trailing whitespace removed. fn trim_right<'a>(&'a self) -> &'a str; } @@ -471,10 +379,10 @@ pub fn utf8_char_width(b: u8) -> uint { /// Determines if a vector of `u16` contains valid UTF-16 pub fn is_utf16(v: &[u16]) -> bool { let mut it = v.iter(); - macro_rules! next ( ($ret:expr) => { + macro_rules! next { ($ret:expr) => { match it.next() { Some(u) => *u, None => return $ret } } - ) + } loop { let u = next!(true); @@ -513,7 +421,7 @@ impl Utf16Item { pub fn to_char_lossy(&self) -> char { match *self { Utf16Item::ScalarValue(c) => c, - Utf16Item::LoneSurrogate(_) => '\uFFFD' + Utf16Item::LoneSurrogate(_) => '\u{FFFD}' } } } @@ -568,15 +476,14 @@ fn size_hint(&self) -> (uint, Option) { /// # Example /// /// ```rust -/// use std::str; -/// use std::str::{ScalarValue, LoneSurrogate}; +/// use unicode::str::Utf16Item::{ScalarValue, LoneSurrogate}; /// /// // 𝄞music /// let v = [0xD834, 0xDD1E, 0x006d, 0x0075, /// 0x0073, 0xDD1E, 0x0069, 0x0063, /// 0xD834]; /// -/// assert_eq!(str::utf16_items(&v).collect::>(), +/// assert_eq!(unicode::str::utf16_items(&v).collect::>(), /// vec![ScalarValue('𝄞'), /// ScalarValue('m'), ScalarValue('u'), ScalarValue('s'), /// LoneSurrogate(0xDD1E), diff --git a/src/test/run-pass/issue-19340-1.rs b/src/test/run-pass/issue-19340-1.rs index b7a6391ee047..2f466d4ca8c4 100644 --- a/src/test/run-pass/issue-19340-1.rs +++ b/src/test/run-pass/issue-19340-1.rs @@ -15,7 +15,7 @@ use lib::Homura; fn main() { - let homura = Homura::Madoka { name: "Kaname".into_string() }; + let homura = Homura::Madoka { name: "Kaname".to_string() }; match homura { Homura::Madoka { name } => (), diff --git a/src/test/run-pass/issue-19340-2.rs b/src/test/run-pass/issue-19340-2.rs index 5179c1e2acb7..8300220edeaf 100644 --- a/src/test/run-pass/issue-19340-2.rs +++ b/src/test/run-pass/issue-19340-2.rs @@ -17,7 +17,7 @@ enum Homura { fn main() { let homura = Homura::Madoka { - name: "Akemi".into_string(), + name: "Akemi".to_string(), age: 14, }; diff --git a/src/test/run-pass/issue-19367.rs b/src/test/run-pass/issue-19367.rs index 3efc2ee50f35..7db84d518ff3 100644 --- a/src/test/run-pass/issue-19367.rs +++ b/src/test/run-pass/issue-19367.rs @@ -16,10 +16,10 @@ struct S { // on field of struct or tuple which we reassign in the match body. fn main() { - let mut a = (0i, Some("right".into_string())); + let mut a = (0i, Some("right".to_string())); let b = match a.1 { Some(v) => { - a.1 = Some("wrong".into_string()); + a.1 = Some("wrong".to_string()); v } None => String::new() @@ -28,10 +28,10 @@ fn main() { assert_eq!(b, "right"); - let mut s = S{ o: Some("right".into_string()) }; + let mut s = S{ o: Some("right".to_string()) }; let b = match s.o { Some(v) => { - s.o = Some("wrong".into_string()); + s.o = Some("wrong".to_string()); v } None => String::new(), From a76a80276852f05f30adaa4d2a8a2729b5fc0bfa Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Thu, 18 Dec 2014 22:52:48 -0800 Subject: [PATCH 14/44] serialize: Fully deprecate the library This commit completes the deprecation story for the in-tree serialization library. The compiler will now emit a warning whenever it encounters `deriving(Encodable)` or `deriving(Decodable)`, and the library itself is now marked `#[unstable]` for when feature staging is enabled. All users of serialization can migrate to the `rustc-serialize` crate on crates.io which provides the exact same interface as the libserialize library in-tree. The new deriving modes are named `RustcEncodable` and `RustcDecodable` and require `extern crate "rustc-serialize" as rustc_serialize` at the crate root in order to expand correctly. To migrate all crates, add the following to your `Cargo.toml`: [dependencies] rustc-serialize = "0.1.1" And then add the following to your crate root: extern crate "rustc-serialize" as rustc_serialize; Finally, rename `Encodable` and `Decodable` deriving modes to `RustcEncodable` and `RustcDecodable`. [breaking-change] --- src/librustc/lib.rs | 2 + src/librustc/middle/def.rs | 6 +- src/librustc/middle/region.rs | 3 +- src/librustc/middle/resolve_lifetime.rs | 2 +- src/librustc/middle/subst.rs | 6 +- src/librustc/middle/ty.rs | 35 ++-- src/librustdoc/clean/mod.rs | 96 +++++------ src/librustdoc/doctree.rs | 2 +- src/librustdoc/lib.rs | 2 + src/librustdoc/stability_summary.rs | 4 +- src/libserialize/json.rs | 26 +-- src/libserialize/lib.rs | 6 +- src/libsyntax/abi.rs | 2 +- src/libsyntax/ast.rs | 209 ++++++++++++------------ src/libsyntax/ast_util.rs | 2 +- src/libsyntax/attr.rs | 8 +- src/libsyntax/codemap.rs | 4 +- src/libsyntax/ext/deriving/decodable.rs | 32 +++- src/libsyntax/ext/deriving/encodable.rs | 25 ++- src/libsyntax/ext/deriving/mod.rs | 18 +- src/libsyntax/ext/mtwt.rs | 2 +- src/libsyntax/lib.rs | 2 + src/libsyntax/parse/token.rs | 12 +- src/libtest/lib.rs | 3 +- src/libtime/lib.rs | 5 +- 25 files changed, 288 insertions(+), 226 deletions(-) diff --git a/src/librustc/lib.rs b/src/librustc/lib.rs index 463dcddaf94f..203e63858d49 100644 --- a/src/librustc/lib.rs +++ b/src/librustc/lib.rs @@ -40,6 +40,8 @@ #[phase(plugin, link)] extern crate log; #[phase(plugin, link)] extern crate syntax; +extern crate "serialize" as rustc_serialize; // used by deriving + #[cfg(test)] extern crate test; diff --git a/src/librustc/middle/def.rs b/src/librustc/middle/def.rs index a582907612fd..a54bc4a945ae 100644 --- a/src/librustc/middle/def.rs +++ b/src/librustc/middle/def.rs @@ -20,7 +20,7 @@ use std::cell::RefCell; -#[deriving(Clone, Copy, PartialEq, Eq, Encodable, Decodable, Hash, Show)] +#[deriving(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show)] pub enum Def { DefFn(ast::DefId, bool /* is_ctor */), DefStaticMethod(/* method */ ast::DefId, MethodProvenance), @@ -73,13 +73,13 @@ pub struct Export { pub def_id: ast::DefId, // The definition of the target. } -#[deriving(Clone, Copy, PartialEq, Eq, Encodable, Decodable, Hash, Show)] +#[deriving(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show)] pub enum MethodProvenance { FromTrait(ast::DefId), FromImpl(ast::DefId), } -#[deriving(Clone, Copy, PartialEq, Eq, Encodable, Decodable, Hash, Show)] +#[deriving(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show)] pub enum TyParamProvenance { FromSelf(ast::DefId), FromParam(ast::DefId), diff --git a/src/librustc/middle/region.rs b/src/librustc/middle/region.rs index e0d5a3a50e61..8df78281cc22 100644 --- a/src/librustc/middle/region.rs +++ b/src/librustc/middle/region.rs @@ -36,7 +36,8 @@ /// placate the same deriving in `ty::FreeRegion`, but we may want to /// actually attach a more meaningful ordering to scopes than the one /// generated via deriving here. -#[deriving(Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Hash, Encodable, Decodable, Show)] +#[deriving(Clone, PartialEq, PartialOrd, Eq, Ord, Hash, RustcEncodable, + RustcDecodable, Show, Copy)] pub enum CodeExtent { Misc(ast::NodeId) } diff --git a/src/librustc/middle/resolve_lifetime.rs b/src/librustc/middle/resolve_lifetime.rs index be191801626a..b64170c2e129 100644 --- a/src/librustc/middle/resolve_lifetime.rs +++ b/src/librustc/middle/resolve_lifetime.rs @@ -33,7 +33,7 @@ use syntax::visit::Visitor; use util::nodemap::NodeMap; -#[deriving(Clone, Copy, PartialEq, Eq, Hash, Encodable, Decodable, Show)] +#[deriving(Clone, Copy, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable, Show)] pub enum DefRegion { DefStaticRegion, DefEarlyBoundRegion(/* space */ subst::ParamSpace, diff --git a/src/librustc/middle/subst.rs b/src/librustc/middle/subst.rs index 30a47ff91325..69249b9ed503 100644 --- a/src/librustc/middle/subst.rs +++ b/src/librustc/middle/subst.rs @@ -187,8 +187,8 @@ pub fn is_erased(&self) -> bool { /////////////////////////////////////////////////////////////////////////// // ParamSpace -#[deriving(Copy, PartialOrd, Ord, PartialEq, Eq, - Clone, Hash, Encodable, Decodable, Show)] +#[deriving(PartialOrd, Ord, PartialEq, Eq, Copy, + Clone, Hash, RustcEncodable, RustcDecodable, Show)] pub enum ParamSpace { TypeSpace, // Type parameters attached to a type definition, trait, or impl SelfSpace, // Self parameter on a trait @@ -224,7 +224,7 @@ pub fn from_uint(u: uint) -> ParamSpace { /// Vector of things sorted by param space. Used to keep /// the set of things declared on the type, self, or method /// distinct. -#[deriving(PartialEq, Eq, Clone, Hash, Encodable, Decodable)] +#[deriving(PartialEq, Eq, Clone, Hash, RustcEncodable, RustcDecodable)] pub struct VecPerParamSpace { // This was originally represented as a tuple with one Vec for // each variant of ParamSpace, and that remains the abstraction diff --git a/src/librustc/middle/ty.rs b/src/librustc/middle/ty.rs index 50a6fb9d0ca3..6b298cfd0d13 100644 --- a/src/librustc/middle/ty.rs +++ b/src/librustc/middle/ty.rs @@ -246,7 +246,7 @@ pub struct mt<'tcx> { pub mutbl: ast::Mutability, } -#[deriving(Clone, Copy, PartialEq, Eq, Hash, Encodable, Decodable, Show)] +#[deriving(Clone, Copy, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable, Show)] pub enum TraitStore { /// Box UniqTraitStore, @@ -277,13 +277,13 @@ pub enum ast_ty_to_ty_cache_entry<'tcx> { atttce_resolved(Ty<'tcx>) /* resolved to a type, irrespective of region */ } -#[deriving(Clone, PartialEq, Decodable, Encodable)] +#[deriving(Clone, PartialEq, RustcDecodable, RustcEncodable)] pub struct ItemVariances { pub types: VecPerParamSpace, pub regions: VecPerParamSpace, } -#[deriving(Clone, Copy, PartialEq, Decodable, Encodable, Show)] +#[deriving(Clone, PartialEq, RustcDecodable, RustcEncodable, Show, Copy)] pub enum Variance { Covariant, // T <: T iff A <: B -- e.g., function return type Invariant, // T <: T iff B == A -- e.g., type of mutable cell @@ -430,7 +430,7 @@ fn type_of_autoref<'tcx>(cx: &ctxt<'tcx>, autoref: &AutoRef<'tcx>) -> Option bool { } } -#[deriving(Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Hash, Encodable, Decodable, Show)] +#[deriving(Clone, PartialEq, PartialOrd, Eq, Ord, Hash, + RustcEncodable, RustcDecodable, Show, Copy)] /// A "free" region `fr` can be interpreted as "some region /// at least as big as the scope `fr.scope`". pub struct FreeRegion { @@ -1154,7 +1155,8 @@ pub struct FreeRegion { pub bound_region: BoundRegion } -#[deriving(Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Hash, Encodable, Decodable, Show)] +#[deriving(Clone, PartialEq, PartialOrd, Eq, Ord, Hash, + RustcEncodable, RustcDecodable, Show, Copy)] pub enum BoundRegion { /// An anonymous region parameter for a given fn (&T) BrAnon(uint), @@ -1412,7 +1414,8 @@ pub struct ExistentialBounds { pub type BuiltinBounds = EnumSet; -#[deriving(Copy, Clone, Encodable, PartialEq, Eq, Decodable, Hash, Show)] +#[deriving(Clone, RustcEncodable, PartialEq, Eq, RustcDecodable, Hash, + Show, Copy)] #[repr(uint)] pub enum BuiltinBound { BoundSend, @@ -1463,7 +1466,7 @@ pub struct FloatVid { pub index: uint } -#[deriving(Clone, Copy, PartialEq, Eq, Encodable, Decodable, Hash)] +#[deriving(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Copy)] pub struct RegionVid { pub index: uint } @@ -1485,7 +1488,7 @@ pub enum InferTy { FreshIntTy(uint), } -#[deriving(Clone, Copy, Encodable, Decodable, Eq, Hash, Show)] +#[deriving(Clone, RustcEncodable, RustcDecodable, Eq, Hash, Show, Copy)] pub enum InferRegion { ReVar(RegionVid), ReSkolemized(uint, BoundRegion) @@ -1571,7 +1574,7 @@ pub struct TypeParameterDef<'tcx> { pub default: Option>, } -#[deriving(Encodable, Decodable, Clone, Show)] +#[deriving(RustcEncodable, RustcDecodable, Clone, Show)] pub struct RegionParameterDef { pub name: ast::Name, pub def_id: ast::DefId, @@ -6223,7 +6226,7 @@ fn accum_substs(accumulator: &mut Vec, substs: &Substs) { } /// A free variable referred to in a function. -#[deriving(Copy, Encodable, Decodable)] +#[deriving(Copy, RustcEncodable, RustcDecodable)] pub struct Freevar { /// The variable being accessed free. pub def: def::Def, diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index ac688784f926..ed2b85a34a9f 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -113,7 +113,7 @@ fn clean(&self, cx: &DocContext) -> Vec { } } -#[deriving(Clone, Encodable, Decodable)] +#[deriving(Clone, RustcEncodable, RustcDecodable)] pub struct Crate { pub name: String, pub src: FsPath, @@ -195,7 +195,7 @@ fn clean(&self, cx: &DocContext) -> Crate { } } -#[deriving(Clone, Encodable, Decodable)] +#[deriving(Clone, RustcEncodable, RustcDecodable)] pub struct ExternalCrate { pub name: String, pub attrs: Vec, @@ -228,7 +228,7 @@ fn clean(&self, cx: &DocContext) -> ExternalCrate { /// Anything with a source location and set of attributes and, optionally, a /// name. That is, anything that can be documented. This doesn't correspond /// directly to the AST's concept of an item; it's a strict superset. -#[deriving(Clone, Encodable, Decodable)] +#[deriving(Clone, RustcEncodable, RustcDecodable)] pub struct Item { /// Stringified span pub source: Span, @@ -304,7 +304,7 @@ pub fn is_fn(&self) -> bool { } } -#[deriving(Clone, Encodable, Decodable)] +#[deriving(Clone, RustcEncodable, RustcDecodable)] pub enum ItemEnum { StructItem(Struct), EnumItem(Enum), @@ -333,7 +333,7 @@ pub enum ItemEnum { AssociatedTypeItem(TyParam), } -#[deriving(Clone, Encodable, Decodable)] +#[deriving(Clone, RustcEncodable, RustcDecodable)] pub struct Module { pub items: Vec, pub is_crate: bool, @@ -400,7 +400,7 @@ fn clean(&self, cx: &DocContext) -> Item { } } -#[deriving(Clone, Encodable, Decodable, PartialEq)] +#[deriving(Clone, RustcEncodable, RustcDecodable, PartialEq)] pub enum Attribute { Word(String), List(String, Vec ), @@ -453,7 +453,7 @@ fn value_str(&self) -> Option { (**self).value_str() } fn meta_item_list(&self) -> Option<&[P]> { None } } -#[deriving(Clone, Encodable, Decodable, PartialEq)] +#[deriving(Clone, RustcEncodable, RustcDecodable, PartialEq)] pub struct TyParam { pub name: String, pub did: ast::DefId, @@ -490,7 +490,7 @@ fn clean(&self, cx: &DocContext) -> TyParam { } } -#[deriving(Clone, Encodable, Decodable, PartialEq)] +#[deriving(Clone, RustcEncodable, RustcDecodable, PartialEq)] pub enum TyParamBound { RegionBound(Lifetime), TraitBound(Type) @@ -632,7 +632,7 @@ fn clean(&self, cx: &DocContext) -> Option> { } } -#[deriving(Clone, Encodable, Decodable, PartialEq)] +#[deriving(Clone, RustcEncodable, RustcDecodable, PartialEq)] pub struct Lifetime(String); impl Lifetime { @@ -682,7 +682,7 @@ fn clean(&self, cx: &DocContext) -> Option { } } -#[deriving(Clone, Encodable, Decodable, PartialEq)] +#[deriving(Clone, RustcEncodable, RustcDecodable, PartialEq)] pub struct WherePredicate { pub ty: Type, pub bounds: Vec @@ -706,7 +706,7 @@ fn clean(&self, cx: &DocContext) -> WherePredicate { } // maybe use a Generic enum and use ~[Generic]? -#[deriving(Clone, Encodable, Decodable, PartialEq)] +#[deriving(Clone, RustcEncodable, RustcDecodable, PartialEq)] pub struct Generics { pub lifetimes: Vec, pub type_params: Vec, @@ -734,7 +734,7 @@ fn clean(&self, cx: &DocContext) -> Generics { } } -#[deriving(Clone, Encodable, Decodable)] +#[deriving(Clone, RustcEncodable, RustcDecodable)] pub struct Method { pub generics: Generics, pub self_: SelfTy, @@ -773,7 +773,7 @@ fn clean(&self, cx: &DocContext) -> Item { } } -#[deriving(Clone, Encodable, Decodable)] +#[deriving(Clone, RustcEncodable, RustcDecodable)] pub struct TyMethod { pub unsafety: ast::Unsafety, pub decl: FnDecl, @@ -811,7 +811,7 @@ fn clean(&self, cx: &DocContext) -> Item { } } -#[deriving(Clone, Encodable, Decodable, PartialEq)] +#[deriving(Clone, RustcEncodable, RustcDecodable, PartialEq)] pub enum SelfTy { SelfStatic, SelfValue, @@ -832,7 +832,7 @@ fn clean(&self, cx: &DocContext) -> SelfTy { } } -#[deriving(Clone, Encodable, Decodable)] +#[deriving(Clone, RustcEncodable, RustcDecodable)] pub struct Function { pub decl: FnDecl, pub generics: Generics, @@ -857,7 +857,7 @@ fn clean(&self, cx: &DocContext) -> Item { } } -#[deriving(Clone, Encodable, Decodable, PartialEq)] +#[deriving(Clone, RustcEncodable, RustcDecodable, PartialEq)] pub struct ClosureDecl { pub lifetimes: Vec, pub decl: FnDecl, @@ -878,14 +878,14 @@ fn clean(&self, cx: &DocContext) -> ClosureDecl { } } -#[deriving(Clone, Encodable, Decodable, PartialEq)] +#[deriving(Clone, RustcEncodable, RustcDecodable, PartialEq)] pub struct FnDecl { pub inputs: Arguments, pub output: FunctionRetTy, pub attrs: Vec, } -#[deriving(Clone, Encodable, Decodable, PartialEq)] +#[deriving(Clone, RustcEncodable, RustcDecodable, PartialEq)] pub struct Arguments { pub values: Vec, } @@ -938,7 +938,7 @@ fn clean(&self, cx: &DocContext) -> FnDecl { } } -#[deriving(Clone, Encodable, Decodable, PartialEq)] +#[deriving(Clone, RustcEncodable, RustcDecodable, PartialEq)] pub struct Argument { pub type_: Type, pub name: String, @@ -955,7 +955,7 @@ fn clean(&self, cx: &DocContext) -> Argument { } } -#[deriving(Clone, Encodable, Decodable, PartialEq)] +#[deriving(Clone, RustcEncodable, RustcDecodable, PartialEq)] pub enum FunctionRetTy { Return(Type), NoReturn @@ -970,7 +970,7 @@ fn clean(&self, cx: &DocContext) -> FunctionRetTy { } } -#[deriving(Clone, Encodable, Decodable)] +#[deriving(Clone, RustcEncodable, RustcDecodable)] pub struct Trait { pub unsafety: ast::Unsafety, pub items: Vec, @@ -1014,7 +1014,7 @@ fn clean(&self, cx: &DocContext) -> Type { /// An item belonging to a trait, whether a method or associated. Could be named /// TraitItem except that's already taken by an exported enum variant. -#[deriving(Clone, Encodable, Decodable)] +#[deriving(Clone, RustcEncodable, RustcDecodable)] pub enum TraitMethod { RequiredMethod(Item), ProvidedMethod(Item), @@ -1059,7 +1059,7 @@ fn clean(&self, cx: &DocContext) -> TraitMethod { } } -#[deriving(Clone, Encodable, Decodable)] +#[deriving(Clone, RustcEncodable, RustcDecodable)] pub enum ImplMethod { MethodImplItem(Item), TypeImplItem(Item), @@ -1132,7 +1132,7 @@ fn clean(&self, cx: &DocContext) -> Item { /// A representation of a Type suitable for hyperlinking purposes. Ideally one can get the original /// type out of the AST/ty::ctxt given one of these, if more information is needed. Most importantly /// it does not preserve mutability or boxes. -#[deriving(Clone, Encodable, Decodable, PartialEq)] +#[deriving(Clone, RustcEncodable, RustcDecodable, PartialEq)] pub enum Type { /// structs/enums/traits (anything that'd be an ast::TyPath) ResolvedPath { @@ -1180,7 +1180,7 @@ pub enum Type { PolyTraitRef(Vec), } -#[deriving(Clone, Copy, Encodable, Decodable, PartialEq, Eq, Hash)] +#[deriving(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Hash, Copy)] pub enum PrimitiveType { Int, I8, I16, I32, I64, Uint, U8, U16, U32, U64, @@ -1192,7 +1192,7 @@ pub enum PrimitiveType { PrimitiveTuple, } -#[deriving(Clone, Copy, Encodable, Decodable)] +#[deriving(Clone, RustcEncodable, RustcDecodable, Copy)] pub enum TypeKind { TypeEnum, TypeFunction, @@ -1436,7 +1436,7 @@ fn clean(&self, cx: &DocContext) -> Type { } } -#[deriving(Clone, Encodable, Decodable)] +#[deriving(Clone, RustcEncodable, RustcDecodable)] pub enum StructField { HiddenStructField, // inserted later by strip passes TypedStructField(Type), @@ -1495,7 +1495,7 @@ fn clean(&self, _: &DocContext) -> Option { } } -#[deriving(Clone, Encodable, Decodable)] +#[deriving(Clone, RustcEncodable, RustcDecodable)] pub struct Struct { pub struct_type: doctree::StructType, pub generics: Generics, @@ -1525,7 +1525,7 @@ fn clean(&self, cx: &DocContext) -> Item { /// This is a more limited form of the standard Struct, different in that /// it lacks the things most items have (name, id, parameterization). Found /// only as a variant in an enum. -#[deriving(Clone, Encodable, Decodable)] +#[deriving(Clone, RustcEncodable, RustcDecodable)] pub struct VariantStruct { pub struct_type: doctree::StructType, pub fields: Vec, @@ -1542,7 +1542,7 @@ fn clean(&self, cx: &DocContext) -> VariantStruct { } } -#[deriving(Clone, Encodable, Decodable)] +#[deriving(Clone, RustcEncodable, RustcDecodable)] pub struct Enum { pub variants: Vec, pub generics: Generics, @@ -1567,7 +1567,7 @@ fn clean(&self, cx: &DocContext) -> Item { } } -#[deriving(Clone, Encodable, Decodable)] +#[deriving(Clone, RustcEncodable, RustcDecodable)] pub struct Variant { pub kind: VariantKind, } @@ -1635,7 +1635,7 @@ fn clean(&self, cx: &DocContext) -> Item { } } -#[deriving(Clone, Encodable, Decodable)] +#[deriving(Clone, RustcEncodable, RustcDecodable)] pub enum VariantKind { CLikeVariant, TupleVariant(Vec), @@ -1657,7 +1657,7 @@ fn clean(&self, cx: &DocContext) -> VariantKind { } } -#[deriving(Clone, Encodable, Decodable, Show)] +#[deriving(Clone, RustcEncodable, RustcDecodable, Show)] pub struct Span { pub filename: String, pub loline: uint, @@ -1692,7 +1692,7 @@ fn clean(&self, cx: &DocContext) -> Span { } } -#[deriving(Clone, Encodable, Decodable, PartialEq)] +#[deriving(Clone, RustcEncodable, RustcDecodable, PartialEq)] pub struct Path { pub global: bool, pub segments: Vec, @@ -1707,7 +1707,7 @@ fn clean(&self, cx: &DocContext) -> Path { } } -#[deriving(Clone, Encodable, Decodable, PartialEq)] +#[deriving(Clone, RustcEncodable, RustcDecodable, PartialEq)] pub struct PathSegment { pub name: String, pub lifetimes: Vec, @@ -1763,7 +1763,7 @@ fn clean(&self, _: &DocContext) -> String { } } -#[deriving(Clone, Encodable, Decodable)] +#[deriving(Clone, RustcEncodable, RustcDecodable)] pub struct Typedef { pub type_: Type, pub generics: Generics, @@ -1786,7 +1786,7 @@ fn clean(&self, cx: &DocContext) -> Item { } } -#[deriving(Clone, Encodable, Decodable, PartialEq)] +#[deriving(Clone, RustcEncodable, RustcDecodable, PartialEq)] pub struct BareFunctionDecl { pub unsafety: ast::Unsafety, pub generics: Generics, @@ -1809,7 +1809,7 @@ fn clean(&self, cx: &DocContext) -> BareFunctionDecl { } } -#[deriving(Clone, Encodable, Decodable, Show)] +#[deriving(Clone, RustcEncodable, RustcDecodable, Show)] pub struct Static { pub type_: Type, pub mutability: Mutability, @@ -1838,7 +1838,7 @@ fn clean(&self, cx: &DocContext) -> Item { } } -#[deriving(Clone, Encodable, Decodable, Show)] +#[deriving(Clone, RustcEncodable, RustcDecodable, Show)] pub struct Constant { pub type_: Type, pub expr: String, @@ -1861,7 +1861,7 @@ fn clean(&self, cx: &DocContext) -> Item { } } -#[deriving(Copy, Show, Clone, Encodable, Decodable, PartialEq)] +#[deriving(Show, Clone, RustcEncodable, RustcDecodable, PartialEq, Copy)] pub enum Mutability { Mutable, Immutable, @@ -1876,7 +1876,7 @@ fn clean(&self, _: &DocContext) -> Mutability { } } -#[deriving(Clone, Encodable, Decodable)] +#[deriving(Clone, RustcEncodable, RustcDecodable)] pub struct Impl { pub generics: Generics, pub trait_: Option, @@ -1914,7 +1914,7 @@ fn clean(&self, cx: &DocContext) -> Item { } } -#[deriving(Clone, Encodable, Decodable)] +#[deriving(Clone, RustcEncodable, RustcDecodable)] pub struct ViewItem { pub inner: ViewItemInner, } @@ -1980,7 +1980,7 @@ fn clean(&self, cx: &DocContext) -> Vec { } } -#[deriving(Clone, Encodable, Decodable)] +#[deriving(Clone, RustcEncodable, RustcDecodable)] pub enum ViewItemInner { ExternCrate(String, Option, ast::NodeId), Import(ViewPath) @@ -2003,7 +2003,7 @@ fn clean(&self, cx: &DocContext) -> ViewItemInner { } } -#[deriving(Clone, Encodable, Decodable)] +#[deriving(Clone, RustcEncodable, RustcDecodable)] pub enum ViewPath { // use source as str; SimpleImport(String, ImportSource), @@ -2013,7 +2013,7 @@ pub enum ViewPath { ImportList(ImportSource, Vec), } -#[deriving(Clone, Encodable, Decodable)] +#[deriving(Clone, RustcEncodable, RustcDecodable)] pub struct ImportSource { pub path: Path, pub did: Option, @@ -2034,7 +2034,7 @@ fn clean(&self, cx: &DocContext) -> ViewPath { } } -#[deriving(Clone, Encodable, Decodable)] +#[deriving(Clone, RustcEncodable, RustcDecodable)] pub struct ViewListIdent { pub name: String, pub source: Option, @@ -2247,7 +2247,7 @@ fn resolve_def(cx: &DocContext, id: ast::NodeId) -> Option { }) } -#[deriving(Clone, Encodable, Decodable)] +#[deriving(Clone, RustcEncodable, RustcDecodable)] pub struct Macro { pub source: String, } @@ -2268,7 +2268,7 @@ fn clean(&self, cx: &DocContext) -> Item { } } -#[deriving(Clone, Encodable, Decodable)] +#[deriving(Clone, RustcEncodable, RustcDecodable)] pub struct Stability { pub level: attr::StabilityLevel, pub text: String diff --git a/src/librustdoc/doctree.rs b/src/librustdoc/doctree.rs index 83552884d7ff..7f7c055062aa 100644 --- a/src/librustdoc/doctree.rs +++ b/src/librustdoc/doctree.rs @@ -70,7 +70,7 @@ pub fn new(name: Option) -> Module { } } -#[deriving(Copy, Show, Clone, Encodable, Decodable)] +#[deriving(Show, Clone, RustcEncodable, RustcDecodable, Copy)] pub enum StructType { /// A normal struct Plain, diff --git a/src/librustdoc/lib.rs b/src/librustdoc/lib.rs index 182c83d805c2..8dfb352d0288 100644 --- a/src/librustdoc/lib.rs +++ b/src/librustdoc/lib.rs @@ -32,6 +32,8 @@ extern crate "test" as testing; #[phase(plugin, link)] extern crate log; +extern crate "serialize" as rustc_serialize; // used by deriving + use std::cell::RefCell; use std::collections::HashMap; use std::collections::hash_map::Entry::{Occupied, Vacant}; diff --git a/src/librustdoc/stability_summary.rs b/src/librustdoc/stability_summary.rs index 2f3079f75b92..2e3adf8e7678 100644 --- a/src/librustdoc/stability_summary.rs +++ b/src/librustdoc/stability_summary.rs @@ -25,7 +25,7 @@ use html::render::cache; -#[deriving(Zero, Encodable, Decodable, PartialEq, Eq)] +#[deriving(Zero, RustcEncodable, RustcDecodable, PartialEq, Eq)] /// The counts for each stability level. #[deriving(Copy)] pub struct Counts { @@ -73,7 +73,7 @@ pub fn total(&self) -> uint { } } -#[deriving(Encodable, Decodable, PartialEq, Eq)] +#[deriving(RustcEncodable, RustcDecodable, PartialEq, Eq)] /// A summarized module, which includes total counts and summarized children /// modules. pub struct ModuleSummary { diff --git a/src/libserialize/json.rs b/src/libserialize/json.rs index 3181e28a1211..15e7de080163 100644 --- a/src/libserialize/json.rs +++ b/src/libserialize/json.rs @@ -57,17 +57,17 @@ //! //! Rust provides a mechanism for low boilerplate encoding & decoding of values to and from JSON via //! the serialization API. -//! To be able to encode a piece of data, it must implement the `serialize::Encodable` trait. -//! To be able to decode a piece of data, it must implement the `serialize::Decodable` trait. +//! To be able to encode a piece of data, it must implement the `serialize::RustcEncodable` trait. +//! To be able to decode a piece of data, it must implement the `serialize::RustcDecodable` trait. //! The Rust compiler provides an annotation to automatically generate the code for these traits: -//! `#[deriving(Decodable, Encodable)]` +//! `#[deriving(RustcDecodable, RustcEncodable)]` //! //! The JSON API provides an enum `json::Json` and a trait `ToJson` to encode objects. //! The `ToJson` trait provides a `to_json` method to convert an object into a `json::Json` value. //! A `json::Json` value can be encoded as a string or buffer using the functions described above. //! You can also use the `json::Encoder` object, which implements the `Encoder` trait. //! -//! When using `ToJson` the `Encodable` trait implementation is not mandatory. +//! When using `ToJson` the `RustcEncodable` trait implementation is not mandatory. //! //! # Examples of use //! @@ -127,7 +127,7 @@ //! } //! } //! -//! // Only generate `Encodable` trait implementation +//! // Only generate `RustcEncodable` trait implementation //! #[deriving(Encodable)] //! pub struct ComplexNumRecord { //! uid: u8, @@ -404,7 +404,7 @@ pub fn new(writer: &'a mut io::Writer) -> Encoder<'a> { } /// Encode the specified struct into a json [u8] - pub fn buffer_encode, io::IoError>>(object: &T) -> Vec { + pub fn buffer_encode, io::IoError>>(object: &T) -> Vec { //Serialize the object in a string using a writer let mut m = Vec::new(); // FIXME(14302) remove the transmute and unsafe block. @@ -2428,7 +2428,7 @@ mod tests { use std::num::Float; use std::string; - #[deriving(Decodable, Eq, PartialEq, Show)] + #[deriving(RustcDecodable, Eq, PartialEq, Show)] struct OptionData { opt: Option, } @@ -2455,20 +2455,20 @@ fn test_decode_option_malformed() { ExpectedError("Number".into_string(), "false".into_string())); } - #[deriving(PartialEq, Encodable, Decodable, Show)] + #[deriving(PartialEq, RustcEncodable, RustcDecodable, Show)] enum Animal { Dog, Frog(string::String, int) } - #[deriving(PartialEq, Encodable, Decodable, Show)] + #[deriving(PartialEq, RustcEncodable, RustcDecodable, Show)] struct Inner { a: (), b: uint, c: Vec, } - #[deriving(PartialEq, Encodable, Decodable, Show)] + #[deriving(PartialEq, RustcEncodable, RustcDecodable, Show)] struct Outer { inner: Vec, } @@ -3009,7 +3009,7 @@ fn test_decode_struct() { ); } - #[deriving(Decodable)] + #[deriving(RustcDecodable)] struct FloatStruct { f: f64, a: Vec @@ -3058,7 +3058,7 @@ fn test_multiline_errors() { Err(SyntaxError(EOFWhileParsingObject, 3u, 8u))); } - #[deriving(Decodable)] + #[deriving(RustcDecodable)] #[allow(dead_code)] struct DecodeStruct { x: f64, @@ -3066,7 +3066,7 @@ struct DecodeStruct { z: string::String, w: Vec } - #[deriving(Decodable)] + #[deriving(RustcDecodable)] enum DecodeEnum { A(f64), B(string::String) diff --git a/src/libserialize/lib.rs b/src/libserialize/lib.rs index e700d102fefd..7ed806fb3b66 100644 --- a/src/libserialize/lib.rs +++ b/src/libserialize/lib.rs @@ -15,7 +15,7 @@ */ #![crate_name = "serialize"] -#![experimental] +#![unstable = "deprecated in favor of rustc-serialize on crates.io"] #![crate_type = "rlib"] #![crate_type = "dylib"] #![doc(html_logo_url = "http://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png", @@ -44,3 +44,7 @@ pub mod base64; pub mod hex; pub mod json; + +mod rustc_serialize { + pub use serialize::*; +} diff --git a/src/libsyntax/abi.rs b/src/libsyntax/abi.rs index 70bad90aea1c..b1599cb807d0 100644 --- a/src/libsyntax/abi.rs +++ b/src/libsyntax/abi.rs @@ -26,7 +26,7 @@ pub enum Os { OsDragonfly, } -#[deriving(Copy, PartialEq, Eq, Hash, Encodable, Decodable, Clone)] +#[deriving(PartialEq, Eq, Hash, RustcEncodable, RustcDecodable, Clone, Copy)] pub enum Abi { // NB: This ordering MUST match the AbiDatas array below. // (This is ensured by the test indices_are_correct().) diff --git a/src/libsyntax/ast.rs b/src/libsyntax/ast.rs index a294706ef2c1..b37914ed429a 100644 --- a/src/libsyntax/ast.rs +++ b/src/libsyntax/ast.rs @@ -157,7 +157,8 @@ fn ne(&self, other: &Ident) -> bool { /// A name is a part of an identifier, representing a string or gensym. It's /// the result of interning. -#[deriving(Copy, Eq, Ord, PartialEq, PartialOrd, Hash, Encodable, Decodable, Clone)] +#[deriving(Eq, Ord, PartialEq, PartialOrd, Hash, + RustcEncodable, RustcDecodable, Clone, Copy)] pub struct Name(pub u32); impl Name { @@ -187,7 +188,7 @@ fn encode(&self, s: &mut S) -> Result<(), E> { } } -impl, E> Decodable for Ident { +impl, E> Decodable for Ident { fn decode(d: &mut D) -> Result { Ok(str_to_ident(try!(d.read_str()).as_slice())) } @@ -196,14 +197,15 @@ fn decode(d: &mut D) -> Result { /// Function name (not all functions have names) pub type FnIdent = Option; -#[deriving(Clone, Copy, PartialEq, Eq, Encodable, Decodable, Hash, Show)] +#[deriving(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, + Show, Copy)] pub struct Lifetime { pub id: NodeId, pub span: Span, pub name: Name } -#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)] +#[deriving(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show)] pub struct LifetimeDef { pub lifetime: Lifetime, pub bounds: Vec @@ -212,7 +214,7 @@ pub struct LifetimeDef { /// A "Path" is essentially Rust's notion of a name; for instance: /// std::cmp::PartialEq . It's represented as a sequence of identifiers, /// along with a bunch of supporting information. -#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)] +#[deriving(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show)] pub struct Path { pub span: Span, /// A `::foo` path, is relative to the crate root rather than current @@ -224,7 +226,7 @@ pub struct Path { /// A segment of a path: an identifier, an optional lifetime, and a set of /// types. -#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)] +#[deriving(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show)] pub struct PathSegment { /// The identifier portion of this path segment. pub identifier: Ident, @@ -237,7 +239,7 @@ pub struct PathSegment { pub parameters: PathParameters, } -#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)] +#[deriving(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show)] pub enum PathParameters { AngleBracketedParameters(AngleBracketedParameterData), ParenthesizedParameters(ParenthesizedParameterData), @@ -315,7 +317,7 @@ pub fn bindings(&self) -> Vec<&P> { } /// A path like `Foo<'a, T>` -#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)] +#[deriving(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show)] pub struct AngleBracketedParameterData { /// The lifetime parameters for this path segment. pub lifetimes: Vec, @@ -333,7 +335,7 @@ fn is_empty(&self) -> bool { } /// A path like `Foo(A,B) -> C` -#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)] +#[deriving(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show)] pub struct ParenthesizedParameterData { /// `(A,B)` pub inputs: Vec>, @@ -346,7 +348,8 @@ pub struct ParenthesizedParameterData { pub type NodeId = u32; -#[deriving(Clone, Copy, Eq, Ord, PartialOrd, PartialEq, Encodable, Decodable, Hash, Show)] +#[deriving(Clone, Eq, Ord, PartialOrd, PartialEq, RustcEncodable, + RustcDecodable, Hash, Show, Copy)] pub struct DefId { pub krate: CrateNum, pub node: NodeId, @@ -366,7 +369,7 @@ pub struct DefId { /// typeck::collect::compute_bounds matches these against /// the "special" built-in traits (see middle::lang_items) and /// detects Copy, Send and Sync. -#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)] +#[deriving(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show)] pub enum TyParamBound { TraitTyParamBound(PolyTraitRef), RegionTyParamBound(Lifetime) @@ -374,7 +377,7 @@ pub enum TyParamBound { pub type TyParamBounds = OwnedSlice; -#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)] +#[deriving(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show)] pub struct TyParam { pub ident: Ident, pub id: NodeId, @@ -386,7 +389,7 @@ pub struct TyParam { /// Represents lifetimes and type parameters attached to a declaration /// of a function, enum, trait, etc. -#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)] +#[deriving(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show)] pub struct Generics { pub lifetimes: Vec, pub ty_params: OwnedSlice, @@ -405,34 +408,34 @@ pub fn is_type_parameterized(&self) -> bool { } } -#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)] +#[deriving(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show)] pub struct WhereClause { pub id: NodeId, pub predicates: Vec, } -#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)] +#[deriving(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show)] pub enum WherePredicate { BoundPredicate(WhereBoundPredicate), RegionPredicate(WhereRegionPredicate), EqPredicate(WhereEqPredicate) } -#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)] +#[deriving(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show)] pub struct WhereBoundPredicate { pub span: Span, pub bounded_ty: P, pub bounds: OwnedSlice, } -#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)] +#[deriving(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show)] pub struct WhereRegionPredicate { pub span: Span, pub lifetime: Lifetime, pub bounds: Vec, } -#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)] +#[deriving(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show)] pub struct WhereEqPredicate { pub id: NodeId, pub span: Span, @@ -444,7 +447,7 @@ pub struct WhereEqPredicate { /// used to drive conditional compilation pub type CrateConfig = Vec> ; -#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)] +#[deriving(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show)] pub struct Crate { pub module: Mod, pub attrs: Vec, @@ -455,7 +458,7 @@ pub struct Crate { pub type MetaItem = Spanned; -#[deriving(Clone, Eq, Encodable, Decodable, Hash, Show)] +#[deriving(Clone, Eq, RustcEncodable, RustcDecodable, Hash, Show)] pub enum MetaItem_ { MetaWord(InternedString), MetaList(InternedString, Vec>), @@ -487,7 +490,7 @@ fn eq(&self, other: &MetaItem_) -> bool { } } -#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)] +#[deriving(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show)] pub struct Block { pub view_items: Vec, pub stmts: Vec>, @@ -497,27 +500,27 @@ pub struct Block { pub span: Span, } -#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)] +#[deriving(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show)] pub struct Pat { pub id: NodeId, pub node: Pat_, pub span: Span, } -#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)] +#[deriving(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show)] pub struct FieldPat { pub ident: Ident, pub pat: P, pub is_shorthand: bool, } -#[deriving(Clone, Copy, PartialEq, Eq, Encodable, Decodable, Hash, Show)] +#[deriving(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show, Copy)] pub enum BindingMode { BindByRef(Mutability), BindByValue(Mutability), } -#[deriving(Clone, Copy, PartialEq, Eq, Encodable, Decodable, Hash, Show)] +#[deriving(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show, Copy)] pub enum PatWildKind { /// Represents the wildcard pattern `_` PatWildSingle, @@ -526,7 +529,7 @@ pub enum PatWildKind { PatWildMulti, } -#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)] +#[deriving(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show)] pub enum Pat_ { /// Represents a wildcard pattern (either `_` or `..`) PatWild(PatWildKind), @@ -555,13 +558,13 @@ pub enum Pat_ { PatMac(Mac), } -#[deriving(Clone, Copy, PartialEq, Eq, Encodable, Decodable, Hash, Show)] +#[deriving(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show, Copy)] pub enum Mutability { MutMutable, MutImmutable, } -#[deriving(Clone, Copy, PartialEq, Eq, Encodable, Decodable, Hash, Show)] +#[deriving(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show, Copy)] pub enum BinOp { BiAdd, BiSub, @@ -583,7 +586,7 @@ pub enum BinOp { BiGt, } -#[deriving(Clone, Copy, PartialEq, Eq, Encodable, Decodable, Hash, Show)] +#[deriving(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show, Copy)] pub enum UnOp { UnUniq, UnDeref, @@ -593,7 +596,7 @@ pub enum UnOp { pub type Stmt = Spanned; -#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)] +#[deriving(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show)] pub enum Stmt_ { /// Could be an item or a local (let) binding: StmtDecl(P, NodeId), @@ -607,7 +610,7 @@ pub enum Stmt_ { StmtMac(Mac, MacStmtStyle), } -#[deriving(Clone, Copy, PartialEq, Eq, Encodable, Decodable, Hash, Show)] +#[deriving(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show)] pub enum MacStmtStyle { /// The macro statement had a trailing semicolon, e.g. `foo! { ... };` /// `foo!(...);`, `foo![...];` @@ -622,7 +625,7 @@ pub enum MacStmtStyle { /// Where a local declaration came from: either a true `let ... = /// ...;`, or one desugared from the pattern of a for loop. -#[deriving(Clone, Copy, PartialEq, Eq, Encodable, Decodable, Hash, Show)] +#[deriving(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show, Copy)] pub enum LocalSource { LocalLet, LocalFor, @@ -631,7 +634,7 @@ pub enum LocalSource { // FIXME (pending discussion of #1697, #2178...): local should really be // a refinement on pat. /// Local represents a `let` statement, e.g., `let : = ;` -#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)] +#[deriving(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show)] pub struct Local { pub ty: P, pub pat: P, @@ -643,7 +646,7 @@ pub struct Local { pub type Decl = Spanned; -#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)] +#[deriving(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show)] pub enum Decl_ { /// A local (let) binding: DeclLocal(P), @@ -652,7 +655,7 @@ pub enum Decl_ { } /// represents one arm of a 'match' -#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)] +#[deriving(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show)] pub struct Arm { pub attrs: Vec, pub pats: Vec>, @@ -660,7 +663,7 @@ pub struct Arm { pub body: P, } -#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)] +#[deriving(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show)] pub struct Field { pub ident: SpannedIdent, pub expr: P, @@ -669,26 +672,26 @@ pub struct Field { pub type SpannedIdent = Spanned; -#[deriving(Clone, Copy, PartialEq, Eq, Encodable, Decodable, Hash, Show)] +#[deriving(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show, Copy)] pub enum BlockCheckMode { DefaultBlock, UnsafeBlock(UnsafeSource), } -#[deriving(Clone, Copy, PartialEq, Eq, Encodable, Decodable, Hash, Show)] +#[deriving(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show, Copy)] pub enum UnsafeSource { CompilerGenerated, UserProvided, } -#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)] +#[deriving(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show)] pub struct Expr { pub id: NodeId, pub node: Expr_, pub span: Span, } -#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)] +#[deriving(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show)] pub enum Expr_ { /// First expr is the place; second expr is the value. ExprBox(Option>, P), @@ -750,28 +753,28 @@ pub enum Expr_ { /// as SomeTrait>::SomeAssociatedItem /// ^~~~~ ^~~~~~~~~ ^~~~~~~~~~~~~~~~~~ /// self_type trait_name item_name -#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)] +#[deriving(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show)] pub struct QPath { pub self_type: P, pub trait_ref: P, pub item_name: Ident, } -#[deriving(Clone, Copy, PartialEq, Eq, Encodable, Decodable, Hash, Show)] +#[deriving(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show, Copy)] pub enum MatchSource { Normal, IfLetDesugar { contains_else_clause: bool }, WhileLetDesugar, } -#[deriving(Clone, Copy, PartialEq, Eq, Encodable, Decodable, Hash, Show)] +#[deriving(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show, Copy)] pub enum CaptureClause { CaptureByValue, CaptureByRef, } /// A delimited sequence of token trees -#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)] +#[deriving(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show)] pub struct Delimited { /// The type of delimiter pub delim: token::DelimToken, @@ -806,7 +809,7 @@ pub fn close_tt(&self) -> TokenTree { } /// A sequence of token treesee -#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)] +#[deriving(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show)] pub struct SequenceRepetition { /// The sequence of token trees pub tts: Vec, @@ -820,7 +823,7 @@ pub struct SequenceRepetition { /// A Kleene-style [repetition operator](http://en.wikipedia.org/wiki/Kleene_star) /// for token sequences. -#[deriving(Clone, Copy, PartialEq, Eq, Encodable, Decodable, Hash, Show)] +#[deriving(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show, Copy)] pub enum KleeneOp { ZeroOrMore, OneOrMore, @@ -838,7 +841,7 @@ pub enum KleeneOp { /// /// The RHS of an MBE macro is the only place `SubstNt`s are substituted. /// Nothing special happens to misnamed or misplaced `SubstNt`s. -#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)] +#[deriving(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show)] #[doc="For macro invocations; parsing is delegated to the macro"] pub enum TokenTree { /// A single token @@ -928,14 +931,14 @@ pub fn get_span(&self) -> Span { /// is being invoked, and the vector of token-trees contains the source /// of the macro invocation. /// There's only one flavor, now, so this could presumably be simplified. -#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)] +#[deriving(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show)] pub enum Mac_ { // NB: the additional ident for a macro_rules-style macro is actually // stored in the enclosing item. Oog. MacInvocTT(Path, Vec , SyntaxContext), // new macro-invocation } -#[deriving(Clone, Copy, PartialEq, Eq, Encodable, Decodable, Hash, Show)] +#[deriving(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show, Copy)] pub enum StrStyle { CookedStr, RawStr(uint) @@ -943,7 +946,7 @@ pub enum StrStyle { pub type Lit = Spanned; -#[deriving(Clone, Copy, PartialEq, Eq, Encodable, Decodable, Hash, Show)] +#[deriving(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show, Copy)] pub enum Sign { Minus, Plus @@ -959,7 +962,7 @@ pub fn new(n: T) -> Sign { } } -#[deriving(Clone, Copy, PartialEq, Eq, Encodable, Decodable, Hash, Show)] +#[deriving(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show, Copy)] pub enum LitIntType { SignedIntLit(IntTy, Sign), UnsignedIntLit(UintTy), @@ -976,7 +979,7 @@ pub fn suffix_len(&self) -> uint { } } -#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)] +#[deriving(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show)] pub enum Lit_ { LitStr(InternedString, StrStyle), LitBinary(Rc >), @@ -990,13 +993,13 @@ pub enum Lit_ { // NB: If you change this, you'll probably want to change the corresponding // type structure in middle/ty.rs as well. -#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)] +#[deriving(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show)] pub struct MutTy { pub ty: P, pub mutbl: Mutability, } -#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)] +#[deriving(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show)] pub struct TypeField { pub ident: Ident, pub mt: MutTy, @@ -1005,7 +1008,7 @@ pub struct TypeField { /// Represents a required method in a trait declaration, /// one without a default implementation -#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)] +#[deriving(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show)] pub struct TypeMethod { pub ident: Ident, pub attrs: Vec, @@ -1023,26 +1026,26 @@ pub struct TypeMethod { /// a default implementation A trait method is either required (meaning it /// doesn't have an implementation, just a signature) or provided (meaning it /// has a default implementation). -#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)] +#[deriving(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show)] pub enum TraitItem { RequiredMethod(TypeMethod), ProvidedMethod(P), TypeTraitItem(P), } -#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)] +#[deriving(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show)] pub enum ImplItem { MethodImplItem(P), TypeImplItem(P), } -#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)] +#[deriving(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show)] pub struct AssociatedType { pub attrs: Vec, pub ty_param: TyParam, } -#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)] +#[deriving(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show)] pub struct Typedef { pub id: NodeId, pub span: Span, @@ -1052,7 +1055,7 @@ pub struct Typedef { pub typ: P, } -#[deriving(Clone, Copy, PartialEq, Eq, Encodable, Decodable, Hash)] +#[deriving(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Copy)] pub enum IntTy { TyI, TyI8, @@ -1077,7 +1080,7 @@ pub fn suffix_len(&self) -> uint { } } -#[deriving(Clone, Copy, PartialEq, Eq, Encodable, Decodable, Hash)] +#[deriving(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Copy)] pub enum UintTy { TyU, TyU8, @@ -1102,7 +1105,7 @@ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { } } -#[deriving(Clone, Copy, PartialEq, Eq, Encodable, Decodable, Hash)] +#[deriving(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Copy)] pub enum FloatTy { TyF32, TyF64, @@ -1123,7 +1126,7 @@ pub fn suffix_len(&self) -> uint { } // Bind a type to an associated type: `A=Foo`. -#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)] +#[deriving(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show)] pub struct TypeBinding { pub id: NodeId, pub ident: Ident, @@ -1133,7 +1136,7 @@ pub struct TypeBinding { // NB PartialEq method appears below. -#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)] +#[deriving(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show)] pub struct Ty { pub id: NodeId, pub node: Ty_, @@ -1141,7 +1144,7 @@ pub struct Ty { } /// Not represented directly in the AST, referred to by name through a ty_path. -#[deriving(Clone, Copy, PartialEq, Eq, Encodable, Decodable, Hash, Show)] +#[deriving(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show, Copy)] pub enum PrimTy { TyInt(IntTy), TyUint(UintTy), @@ -1151,7 +1154,7 @@ pub enum PrimTy { TyChar } -#[deriving(Clone, Copy, PartialEq, Eq, Encodable, Decodable, Hash)] +#[deriving(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Copy)] pub enum Onceness { Once, Many @@ -1167,7 +1170,7 @@ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { } /// Represents the type of a closure -#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)] +#[deriving(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show)] pub struct ClosureTy { pub lifetimes: Vec, pub unsafety: Unsafety, @@ -1176,7 +1179,7 @@ pub struct ClosureTy { pub bounds: TyParamBounds, } -#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)] +#[deriving(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show)] pub struct BareFnTy { pub unsafety: Unsafety, pub abi: Abi, @@ -1184,7 +1187,7 @@ pub struct BareFnTy { pub decl: P } -#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)] +#[deriving(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show)] /// The different kinds of types recognized by the compiler pub enum Ty_ { TyVec(P), @@ -1219,13 +1222,13 @@ pub enum Ty_ { TyInfer, } -#[deriving(Clone, Copy, PartialEq, Eq, Encodable, Decodable, Hash, Show)] +#[deriving(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show, Copy)] pub enum AsmDialect { AsmAtt, AsmIntel } -#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)] +#[deriving(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show)] pub struct InlineAsm { pub asm: InternedString, pub asm_str_style: StrStyle, @@ -1239,7 +1242,7 @@ pub struct InlineAsm { } /// represents an argument in a function header -#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)] +#[deriving(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show)] pub struct Arg { pub ty: P, pub pat: P, @@ -1267,14 +1270,14 @@ pub fn new_self(span: Span, mutability: Mutability, self_ident: Ident) -> Arg { } /// represents the header (not the body) of a function declaration -#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)] +#[deriving(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show)] pub struct FnDecl { pub inputs: Vec, pub output: FunctionRetTy, pub variadic: bool } -#[deriving(Copy, Clone, PartialEq, Eq, Encodable, Decodable, Hash)] +#[deriving(Copy, Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash)] pub enum Unsafety { Unsafe, Normal, @@ -1289,7 +1292,7 @@ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { } } -#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)] +#[deriving(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show)] pub enum FunctionRetTy { /// Functions with return type ! that always /// raise an error or exit (i.e. never return to the caller) @@ -1308,7 +1311,7 @@ pub fn span(&self) -> Span { } /// Represents the kind of 'self' associated with a method -#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)] +#[deriving(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show)] pub enum ExplicitSelf_ { /// No self SelfStatic, @@ -1322,7 +1325,7 @@ pub enum ExplicitSelf_ { pub type ExplicitSelf = Spanned; -#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)] +#[deriving(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show)] pub struct Method { pub attrs: Vec, pub id: NodeId, @@ -1330,7 +1333,7 @@ pub struct Method { pub node: Method_, } -#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)] +#[deriving(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show)] pub enum Method_ { /// Represents a method declaration MethDecl(Ident, @@ -1345,7 +1348,7 @@ pub enum Method_ { MethMac(Mac), } -#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)] +#[deriving(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show)] pub struct Mod { /// A span from the first token past `{` to the last token until `}`. /// For `mod foo;`, the inner span ranges from the first token @@ -1355,31 +1358,31 @@ pub struct Mod { pub items: Vec>, } -#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)] +#[deriving(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show)] pub struct ForeignMod { pub abi: Abi, pub view_items: Vec, pub items: Vec>, } -#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)] +#[deriving(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show)] pub struct VariantArg { pub ty: P, pub id: NodeId, } -#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)] +#[deriving(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show)] pub enum VariantKind { TupleVariantKind(Vec), StructVariantKind(P), } -#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)] +#[deriving(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show)] pub struct EnumDef { pub variants: Vec>, } -#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)] +#[deriving(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show)] pub struct Variant_ { pub name: Ident, pub attrs: Vec, @@ -1391,7 +1394,7 @@ pub struct Variant_ { pub type Variant = Spanned; -#[deriving(Clone, Copy, PartialEq, Eq, Encodable, Decodable, Hash, Show)] +#[deriving(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show, Copy)] pub enum PathListItem_ { PathListIdent { name: Ident, id: NodeId }, PathListMod { id: NodeId } @@ -1409,7 +1412,7 @@ pub fn id(&self) -> NodeId { pub type ViewPath = Spanned; -#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)] +#[deriving(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show)] pub enum ViewPath_ { /// `foo::bar::baz as quux` @@ -1426,7 +1429,7 @@ pub enum ViewPath_ { ViewPathList(Path, Vec , NodeId) } -#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)] +#[deriving(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show)] pub struct ViewItem { pub node: ViewItem_, pub attrs: Vec, @@ -1434,7 +1437,7 @@ pub struct ViewItem { pub span: Span, } -#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)] +#[deriving(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show)] pub enum ViewItem_ { /// Ident: name used to refer to this crate in the code /// optional (InternedString,StrStyle): if present, this is a location @@ -1450,17 +1453,17 @@ pub enum ViewItem_ { /// 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, Copy, PartialEq, Eq, Encodable, Decodable, Hash, Show)] +#[deriving(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show, Copy)] pub enum AttrStyle { AttrOuter, AttrInner, } -#[deriving(Clone, Copy, PartialEq, Eq, Encodable, Decodable, Hash, Show)] +#[deriving(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show, Copy)] pub struct AttrId(pub uint); /// Doc-comments are promoted to attributes that have is_sugared_doc = true -#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)] +#[deriving(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show)] pub struct Attribute_ { pub id: AttrId, pub style: AttrStyle, @@ -1473,13 +1476,13 @@ pub struct Attribute_ { /// that the ref_id is for. The impl_id maps to the "self type" of this impl. /// If this impl is an ItemImpl, the impl_id is redundant (it could be the /// same as the impl's node id). -#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)] +#[deriving(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show)] pub struct TraitRef { pub path: Path, pub ref_id: NodeId, } -#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)] +#[deriving(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show)] pub struct PolyTraitRef { /// The `'a` in `<'a> Foo<&'a T>` pub bound_lifetimes: Vec, @@ -1488,7 +1491,7 @@ pub struct PolyTraitRef { pub trait_ref: TraitRef } -#[deriving(Clone, Copy, PartialEq, Eq, Encodable, Decodable, Hash, Show)] +#[deriving(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show, Copy)] pub enum Visibility { Public, Inherited, @@ -1503,7 +1506,7 @@ pub fn inherit_from(&self, parent_visibility: Visibility) -> Visibility { } } -#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)] +#[deriving(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show)] pub struct StructField_ { pub kind: StructFieldKind, pub id: NodeId, @@ -1522,7 +1525,7 @@ pub fn ident(&self) -> Option { pub type StructField = Spanned; -#[deriving(Clone, Copy, PartialEq, Eq, Encodable, Decodable, Hash, Show)] +#[deriving(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show, Copy)] pub enum StructFieldKind { NamedField(Ident, Visibility), /// Element of a tuple-like struct @@ -1538,7 +1541,7 @@ pub fn is_unnamed(&self) -> bool { } } -#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)] +#[deriving(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show)] pub struct StructDef { /// Fields, not including ctor pub fields: Vec, @@ -1551,7 +1554,7 @@ pub struct StructDef { FIXME (#3300): Should allow items to be anonymous. Right now we just use dummy names for anon items. */ -#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)] +#[deriving(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show)] pub struct Item { pub ident: Ident, pub attrs: Vec, @@ -1561,7 +1564,7 @@ pub struct Item { pub span: Span, } -#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)] +#[deriving(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show)] pub enum Item_ { ItemStatic(P, Mutability, P), ItemConst(P, P), @@ -1605,7 +1608,7 @@ pub fn descriptive_variant(&self) -> &str { } } -#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)] +#[deriving(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show)] pub struct ForeignItem { pub ident: Ident, pub attrs: Vec, @@ -1615,7 +1618,7 @@ pub struct ForeignItem { pub vis: Visibility, } -#[deriving(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)] +#[deriving(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show)] pub enum ForeignItem_ { ForeignItemFn(P, Generics), ForeignItemStatic(P, /* is_mutbl */ bool), @@ -1630,7 +1633,7 @@ pub fn descriptive_variant(&self) -> &str { } } -#[deriving(Clone, Copy, PartialEq, Eq, Encodable, Decodable, Hash, Show)] +#[deriving(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show, Copy)] pub enum UnboxedClosureKind { FnUnboxedClosureKind, FnMutUnboxedClosureKind, @@ -1640,7 +1643,7 @@ pub enum UnboxedClosureKind { /// 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(Clone, PartialEq, Eq, Encodable, Decodable, Hash, Show)] +#[deriving(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show)] pub enum InlinedItem { IIItem(P), IITraitItem(DefId /* impl id */, TraitItem), diff --git a/src/libsyntax/ast_util.rs b/src/libsyntax/ast_util.rs index 02771809ae6a..48d39079c5bc 100644 --- a/src/libsyntax/ast_util.rs +++ b/src/libsyntax/ast_util.rs @@ -343,7 +343,7 @@ pub fn empty_generics() -> Generics { // ______________________________________________________________________ // Enumerating the IDs which appear in an AST -#[deriving(Copy, Encodable, Decodable, Show)] +#[deriving(RustcEncodable, RustcDecodable, Show, Copy)] pub struct IdRange { pub min: NodeId, pub max: NodeId, diff --git a/src/libsyntax/attr.rs b/src/libsyntax/attr.rs index 127cc5ed51d1..cddf1a9923a5 100644 --- a/src/libsyntax/attr.rs +++ b/src/libsyntax/attr.rs @@ -340,14 +340,14 @@ pub fn cfg_matches(diagnostic: &SpanHandler, cfgs: &[P], cfg: &ast::Me } /// Represents the #[deprecated="foo"] and friends attributes. -#[deriving(Encodable,Decodable,Clone,Show)] +#[deriving(RustcEncodable,RustcDecodable,Clone,Show)] pub struct Stability { pub level: StabilityLevel, pub text: Option } /// The available stability levels. -#[deriving(Copy,Encodable,Decodable,PartialEq,PartialOrd,Clone,Show)] +#[deriving(RustcEncodable,RustcDecodable,PartialEq,PartialOrd,Clone,Show,Copy)] pub enum StabilityLevel { Deprecated, Experimental, @@ -464,7 +464,7 @@ fn int_type_of_word(s: &str) -> Option { } } -#[deriving(Copy, PartialEq, Show, Encodable, Decodable)] +#[deriving(PartialEq, Show, RustcEncodable, RustcDecodable, Copy)] pub enum ReprAttr { ReprAny, ReprInt(Span, IntType), @@ -483,7 +483,7 @@ pub fn is_ffi_safe(&self) -> bool { } } -#[deriving(Copy, Eq, Hash, PartialEq, Show, Encodable, Decodable)] +#[deriving(Eq, Hash, PartialEq, Show, RustcEncodable, RustcDecodable, Copy)] pub enum IntType { SignedInt(ast::IntTy), UnsignedInt(ast::UintTy) diff --git a/src/libsyntax/codemap.rs b/src/libsyntax/codemap.rs index c726e17a8fa6..221c4fca5869 100644 --- a/src/libsyntax/codemap.rs +++ b/src/libsyntax/codemap.rs @@ -92,7 +92,7 @@ pub struct Span { pub const DUMMY_SP: Span = Span { lo: BytePos(0), hi: BytePos(0), expn_id: NO_EXPANSION }; -#[deriving(Clone, Copy, PartialEq, Eq, Encodable, Decodable, Hash, Show)] +#[deriving(Clone, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Show, Copy)] pub struct Spanned { pub node: T, pub span: Span, @@ -218,7 +218,7 @@ pub struct ExpnInfo { pub callee: NameAndSpan } -#[deriving(Copy, PartialEq, Eq, Clone, Show, Hash, Encodable, Decodable)] +#[deriving(PartialEq, Eq, Clone, Show, Hash, RustcEncodable, RustcDecodable, Copy)] pub struct ExpnId(u32); pub const NO_EXPANSION: ExpnId = ExpnId(-1); diff --git a/src/libsyntax/ext/deriving/decodable.rs b/src/libsyntax/ext/deriving/decodable.rs index 0a8d59da8967..4878a690bb9f 100644 --- a/src/libsyntax/ext/deriving/decodable.rs +++ b/src/libsyntax/ext/deriving/decodable.rs @@ -21,24 +21,45 @@ use parse::token; use ptr::P; +pub fn expand_deriving_rustc_decodable(cx: &mut ExtCtxt, + span: Span, + mitem: &MetaItem, + item: &Item, + push: F) where + F: FnOnce(P), +{ + expand_deriving_decodable_imp(cx, span, mitem, item, push, "rustc_serialize") +} + pub fn expand_deriving_decodable(cx: &mut ExtCtxt, span: Span, mitem: &MetaItem, item: &Item, push: F) where F: FnOnce(P), +{ + expand_deriving_decodable_imp(cx, span, mitem, item, push, "serialize") +} + +fn expand_deriving_decodable_imp(cx: &mut ExtCtxt, + span: Span, + mitem: &MetaItem, + item: &Item, + push: F, + krate: &'static str) where + F: FnOnce(P), { let trait_def = TraitDef { span: span, attributes: Vec::new(), - path: Path::new_(vec!("serialize", "Decodable"), None, + path: Path::new_(vec!(krate, "Decodable"), None, vec!(box Literal(Path::new_local("__D")), box Literal(Path::new_local("__E"))), true), additional_bounds: Vec::new(), generics: LifetimeBounds { lifetimes: Vec::new(), bounds: vec!(("__D", None, vec!(Path::new_( - vec!("serialize", "Decoder"), None, + vec!(krate, "Decoder"), None, vec!(box Literal(Path::new_local("__E"))), true))), ("__E", None, vec!())) }, @@ -54,7 +75,7 @@ pub fn expand_deriving_decodable(cx: &mut ExtCtxt, box Literal(Path::new_local("__E"))), true)), attributes: Vec::new(), combine_substructure: combine_substructure(|a, b, c| { - decodable_substructure(a, b, c) + decodable_substructure(a, b, c, krate) }), }) }; @@ -63,9 +84,10 @@ pub fn expand_deriving_decodable(cx: &mut ExtCtxt, } fn decodable_substructure(cx: &mut ExtCtxt, trait_span: Span, - substr: &Substructure) -> P { + substr: &Substructure, + krate: &str) -> P { let decoder = substr.nonself_args[0].clone(); - let recurse = vec!(cx.ident_of("serialize"), + let recurse = vec!(cx.ident_of(krate), cx.ident_of("Decodable"), cx.ident_of("decode")); // throw an underscore in front to suppress unused variable warnings diff --git a/src/libsyntax/ext/deriving/encodable.rs b/src/libsyntax/ext/deriving/encodable.rs index 30851ebeaaef..1b8d01f1cd03 100644 --- a/src/libsyntax/ext/deriving/encodable.rs +++ b/src/libsyntax/ext/deriving/encodable.rs @@ -97,24 +97,45 @@ use parse::token; use ptr::P; +pub fn expand_deriving_rustc_encodable(cx: &mut ExtCtxt, + span: Span, + mitem: &MetaItem, + item: &Item, + push: F) where + F: FnOnce(P), +{ + expand_deriving_encodable_imp(cx, span, mitem, item, push, "rustc_serialize") +} + pub fn expand_deriving_encodable(cx: &mut ExtCtxt, span: Span, mitem: &MetaItem, item: &Item, push: F) where F: FnOnce(P), +{ + expand_deriving_encodable_imp(cx, span, mitem, item, push, "serialize") +} + +fn expand_deriving_encodable_imp(cx: &mut ExtCtxt, + span: Span, + mitem: &MetaItem, + item: &Item, + push: F, + krate: &'static str) where + F: FnOnce(P), { let trait_def = TraitDef { span: span, attributes: Vec::new(), - path: Path::new_(vec!("serialize", "Encodable"), None, + path: Path::new_(vec!(krate, "Encodable"), None, vec!(box Literal(Path::new_local("__S")), box Literal(Path::new_local("__E"))), true), additional_bounds: Vec::new(), generics: LifetimeBounds { lifetimes: Vec::new(), bounds: vec!(("__S", None, vec!(Path::new_( - vec!("serialize", "Encoder"), None, + vec!(krate, "Encoder"), None, vec!(box Literal(Path::new_local("__E"))), true))), ("__E", None, vec!())) }, diff --git a/src/libsyntax/ext/deriving/mod.rs b/src/libsyntax/ext/deriving/mod.rs index 839e99c81d1a..b5957fd1b8a5 100644 --- a/src/libsyntax/ext/deriving/mod.rs +++ b/src/libsyntax/ext/deriving/mod.rs @@ -71,24 +71,22 @@ macro_rules! expand(($func:path) => ($func(cx, titem.span, "Hash" => expand!(hash::expand_deriving_hash), "RustcEncodable" => { - expand!(encodable::expand_deriving_encodable) + expand!(encodable::expand_deriving_rustc_encodable) } "RustcDecodable" => { - expand!(decodable::expand_deriving_decodable) + expand!(decodable::expand_deriving_rustc_decodable) } "Encodable" => { - // NOTE: uncomment after a stage0 snap - // cx.span_warn(titem.span, - // "deriving(Encodable) is deprecated \ - // in favor of deriving(RustcEncodable)"); + cx.span_warn(titem.span, + "deriving(Encodable) is deprecated \ + in favor of deriving(RustcEncodable)"); expand!(encodable::expand_deriving_encodable) } "Decodable" => { - // NOTE: uncomment after a stage0 snap - // cx.span_warn(titem.span, - // "deriving(Decodable) is deprecated \ - // in favor of deriving(RustcDecodable)"); + cx.span_warn(titem.span, + "deriving(Decodable) is deprecated \ + in favor of deriving(RustcDecodable)"); expand!(decodable::expand_deriving_decodable) } diff --git a/src/libsyntax/ext/mtwt.rs b/src/libsyntax/ext/mtwt.rs index f0392912878f..6a296333fdb6 100644 --- a/src/libsyntax/ext/mtwt.rs +++ b/src/libsyntax/ext/mtwt.rs @@ -39,7 +39,7 @@ pub struct SCTable { rename_memo: RefCell>, } -#[deriving(Copy, PartialEq, Encodable, Decodable, Hash, Show)] +#[deriving(PartialEq, RustcEncodable, RustcDecodable, Hash, Show, Copy)] pub enum SyntaxContext_ { EmptyCtxt, Mark (Mrk,SyntaxContext), diff --git a/src/libsyntax/lib.rs b/src/libsyntax/lib.rs index 5f62c74ef074..d5093c5055c7 100644 --- a/src/libsyntax/lib.rs +++ b/src/libsyntax/lib.rs @@ -34,6 +34,8 @@ extern crate term; extern crate libc; +extern crate "serialize" as rustc_serialize; // used by deriving + pub mod util { pub mod interner; #[cfg(test)] diff --git a/src/libsyntax/parse/token.rs b/src/libsyntax/parse/token.rs index dad369792d7a..87d65e258dea 100644 --- a/src/libsyntax/parse/token.rs +++ b/src/libsyntax/parse/token.rs @@ -28,7 +28,7 @@ use std::rc::Rc; #[allow(non_camel_case_types)] -#[deriving(Clone, Copy, Encodable, Decodable, PartialEq, Eq, Hash, Show)] +#[deriving(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Hash, Show, Copy)] pub enum BinOpToken { Plus, Minus, @@ -43,7 +43,7 @@ pub enum BinOpToken { } /// A delimeter token -#[deriving(Clone, Copy, Encodable, Decodable, PartialEq, Eq, Hash, Show)] +#[deriving(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Hash, Show, Copy)] pub enum DelimToken { /// A round parenthesis: `(` or `)` Paren, @@ -53,14 +53,14 @@ pub enum DelimToken { Brace, } -#[deriving(Clone, Copy, Encodable, Decodable, PartialEq, Eq, Hash, Show)] +#[deriving(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Hash, Show, Copy)] pub enum IdentStyle { /// `::` follows the identifier with no whitespace in-between. ModName, Plain, } -#[deriving(Clone, Copy, Encodable, Decodable, PartialEq, Eq, Hash, Show)] +#[deriving(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Hash, Show, Copy)] pub enum Lit { Byte(ast::Name), Char(ast::Name), @@ -86,7 +86,7 @@ pub fn short_name(&self) -> &'static str { } #[allow(non_camel_case_types)] -#[deriving(Clone, Encodable, Decodable, PartialEq, Eq, Hash, Show)] +#[deriving(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Hash, Show)] pub enum Token { /* Expression-operator symbols. */ Eq, @@ -334,7 +334,7 @@ pub fn mtwt_eq(&self, other : &Token) -> bool { } } -#[deriving(Clone, Encodable, Decodable, PartialEq, Eq, Hash)] +#[deriving(Clone, RustcEncodable, RustcDecodable, PartialEq, Eq, Hash)] /// For interpolation during macro expansion. pub enum Nonterminal { NtItem(P), diff --git a/src/libtest/lib.rs b/src/libtest/lib.rs index 5b04a1fed896..7ef6ec4fdcf0 100644 --- a/src/libtest/lib.rs +++ b/src/libtest/lib.rs @@ -37,6 +37,7 @@ extern crate getopts; extern crate regex; extern crate serialize; +extern crate "serialize" as rustc_serialize; extern crate term; pub use self::TestFn::*; @@ -213,7 +214,7 @@ pub struct TestDescAndFn { pub testfn: TestFn, } -#[deriving(Clone, Copy, Encodable, Decodable, PartialEq, Show)] +#[deriving(Clone, RustcEncodable, RustcDecodable, PartialEq, Show, Copy)] pub struct Metric { value: f64, noise: f64 diff --git a/src/libtime/lib.rs b/src/libtime/lib.rs index dfa9b9242740..e58a0229d696 100644 --- a/src/libtime/lib.rs +++ b/src/libtime/lib.rs @@ -24,7 +24,9 @@ #[cfg(test)] #[phase(plugin, link)] extern crate log; +#[cfg(stage0)] extern crate serialize; +extern crate "serialize" as rustc_serialize; extern crate libc; pub use self::ParseError::*; @@ -76,7 +78,8 @@ mod imp { } /// A record specifying a time value in seconds and nanoseconds. -#[deriving(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Encodable, Decodable, Show)] +#[deriving(Clone, PartialEq, Eq, PartialOrd, Ord, RustcEncodable, + RustcDecodable, Show, Copy)] pub struct Timespec { pub sec: i64, pub nsec: i32, From fc30518be9774e7661739eb29a9bca38f31505c5 Mon Sep 17 00:00:00 2001 From: Rolf Timmermans Date: Sun, 21 Dec 2014 00:04:39 +0100 Subject: [PATCH 15/44] Escape control characters in JSON output. --- src/libserialize/json.rs | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/src/libserialize/json.rs b/src/libserialize/json.rs index 3181e28a1211..aa628a71f4b0 100644 --- a/src/libserialize/json.rs +++ b/src/libserialize/json.rs @@ -331,13 +331,14 @@ pub fn escape_bytes(wr: &mut io::Writer, bytes: &[u8]) -> Result<(), io::IoError for (i, byte) in bytes.iter().enumerate() { let escaped = match *byte { - b'"' => "\\\"", - b'\\' => "\\\\", - b'\x08' => "\\b", - b'\x0c' => "\\f", - b'\n' => "\\n", - b'\r' => "\\r", - b'\t' => "\\t", + b'"' => "\\\"".into_cow(), + b'\\' => "\\\\".into_cow(), + b'\x08' => "\\b".into_cow(), + b'\x0c' => "\\f".into_cow(), + b'\n' => "\\n".into_cow(), + b'\r' => "\\r".into_cow(), + b'\t' => "\\t".into_cow(), + b'\x00'...b'\x1f' | b'\x7f' => format!("\\u00{:0>2x}", *byte).into_cow(), _ => { continue; } }; @@ -345,7 +346,7 @@ pub fn escape_bytes(wr: &mut io::Writer, bytes: &[u8]) -> Result<(), io::IoError try!(wr.write(bytes[start..i])); } - try!(wr.write_str(escaped)); + try!(wr.write_str(escaped.deref())); start = i + 1; } @@ -2731,6 +2732,9 @@ fn test_write_none() { fn test_write_char() { check_encoder_for_simple!('a', "\"a\""); check_encoder_for_simple!('\t', "\"\\t\""); + check_encoder_for_simple!('\u{0000}', "\"\\u0000\""); + check_encoder_for_simple!('\u{001b}', "\"\\u001b\""); + check_encoder_for_simple!('\u{007f}', "\"\\u007f\""); check_encoder_for_simple!('\u{00a0}', "\"\u{00a0}\""); check_encoder_for_simple!('\u{abcd}', "\"\u{abcd}\""); check_encoder_for_simple!('\u{10ffff}', "\"\u{10ffff}\""); From 903f5c43604e5cd37fb7f2b62e356259d3043db9 Mon Sep 17 00:00:00 2001 From: Rolf Timmermans Date: Sun, 21 Dec 2014 00:40:15 +0100 Subject: [PATCH 16/44] Avoid allocations. --- src/libserialize/json.rs | 43 ++++++++++++++++++++++++++++++++-------- 1 file changed, 35 insertions(+), 8 deletions(-) diff --git a/src/libserialize/json.rs b/src/libserialize/json.rs index aa628a71f4b0..9aea66483497 100644 --- a/src/libserialize/json.rs +++ b/src/libserialize/json.rs @@ -331,14 +331,41 @@ pub fn escape_bytes(wr: &mut io::Writer, bytes: &[u8]) -> Result<(), io::IoError for (i, byte) in bytes.iter().enumerate() { let escaped = match *byte { - b'"' => "\\\"".into_cow(), - b'\\' => "\\\\".into_cow(), - b'\x08' => "\\b".into_cow(), - b'\x0c' => "\\f".into_cow(), - b'\n' => "\\n".into_cow(), - b'\r' => "\\r".into_cow(), - b'\t' => "\\t".into_cow(), - b'\x00'...b'\x1f' | b'\x7f' => format!("\\u00{:0>2x}", *byte).into_cow(), + b'"' => "\\\"", + b'\\' => "\\\\", + b'\x00' => "\\u0000", + b'\x01' => "\\u0001", + b'\x02' => "\\u0002", + b'\x03' => "\\u0003", + b'\x04' => "\\u0004", + b'\x05' => "\\u0005", + b'\x06' => "\\u0006", + b'\x07' => "\\u0007", + b'\x08' => "\\b", + b'\t' => "\\t", + b'\n' => "\\n", + b'\x0b' => "\\u000b", + b'\x0c' => "\\f", + b'\r' => "\\r", + b'\x0e' => "\\u000e", + b'\x0f' => "\\u000f", + b'\x10' => "\\u0010", + b'\x11' => "\\u0011", + b'\x12' => "\\u0012", + b'\x13' => "\\u0013", + b'\x14' => "\\u0014", + b'\x15' => "\\u0015", + b'\x16' => "\\u0016", + b'\x17' => "\\u0017", + b'\x18' => "\\u0018", + b'\x19' => "\\u0019", + b'\x1a' => "\\u001a", + b'\x1b' => "\\u001b", + b'\x1c' => "\\u001c", + b'\x1d' => "\\u001d", + b'\x1e' => "\\u001e", + b'\x1f' => "\\u001f", + b'\x7f' => "\\u007f", _ => { continue; } }; From 82f411d8a45dca29680590199fa2e72a8553687d Mon Sep 17 00:00:00 2001 From: Rolf Timmermans Date: Sun, 21 Dec 2014 00:41:05 +0100 Subject: [PATCH 17/44] Remove unnecessary deref(). --- src/libserialize/json.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libserialize/json.rs b/src/libserialize/json.rs index 9aea66483497..67d1e501584e 100644 --- a/src/libserialize/json.rs +++ b/src/libserialize/json.rs @@ -373,7 +373,7 @@ pub fn escape_bytes(wr: &mut io::Writer, bytes: &[u8]) -> Result<(), io::IoError try!(wr.write(bytes[start..i])); } - try!(wr.write_str(escaped.deref())); + try!(wr.write_str(escaped)); start = i + 1; } From 0a7ef3f6cc7967feeb9c66a4f499ccbd515c67f6 Mon Sep 17 00:00:00 2001 From: Rolf Timmermans Date: Sun, 21 Dec 2014 11:51:28 +0100 Subject: [PATCH 18/44] Add myself to authors. --- AUTHORS.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/AUTHORS.txt b/AUTHORS.txt index 83f9bbff8aa6..9aa09ac17635 100644 --- a/AUTHORS.txt +++ b/AUTHORS.txt @@ -498,6 +498,7 @@ Robert Irelan Robert Knight Robert Millar Roland Tanglao +Rolf Timmermans Ron Dahlgren Roy Frostig Russell From f8cfd2480b69a1cc266fc91d0b60c825a9dc18a7 Mon Sep 17 00:00:00 2001 From: Florian Wilkens Date: Fri, 19 Dec 2014 21:52:10 +0100 Subject: [PATCH 19/44] Renaming of the Iter types as in RFC #344 libcore: slice::Items -> slice::Iter, slice::MutItems -> slice::IterMut libcollections: *::Items -> *::Iter, *::MoveItems -> *::IntoIter, *::MutItems -> *::IterMut This is of course a [breaking-change]. --- src/libcollections/binary_heap.rs | 29 ++++++----- src/libcollections/bit.rs | 2 +- src/libcollections/btree/node.rs | 12 ++--- src/libcollections/btree/set.rs | 40 +++++++-------- src/libcollections/dlist.rs | 46 ++++++++--------- src/libcollections/enum_set.rs | 14 +++--- src/libcollections/ring_buf.rs | 40 +++++++-------- src/libcollections/slice.rs | 12 ++--- src/libcollections/vec.rs | 18 +++---- src/libcollections/vec_map.rs | 16 +++--- src/libcore/fmt/mod.rs | 2 +- src/libcore/slice.rs | 50 +++++++++---------- src/libcore/str.rs | 9 ++-- src/libgraphviz/maybe_owned_vec.rs | 2 +- src/liblog/lib.rs | 2 +- src/libregex/re.rs | 4 +- src/librustc/middle/lang_items.rs | 2 +- src/librustc/middle/subst.rs | 4 +- src/librustc/middle/traits/mod.rs | 8 +-- src/libsyntax/ast_map/mod.rs | 4 +- src/libsyntax/ext/deriving/generic/mod.rs | 2 +- src/libsyntax/owned_slice.rs | 2 +- src/libsyntax/util/small_vector.rs | 18 +++---- src/test/bench/shootout-k-nucleotide.rs | 2 +- .../resolve-conflict-type-vs-import.rs | 6 +-- src/test/run-pass/issue-13167.rs | 2 +- 26 files changed, 173 insertions(+), 175 deletions(-) diff --git a/src/libcollections/binary_heap.rs b/src/libcollections/binary_heap.rs index 0840e8ec881c..c9d1d9d13fb2 100644 --- a/src/libcollections/binary_heap.rs +++ b/src/libcollections/binary_heap.rs @@ -239,9 +239,8 @@ pub fn from_vec(xs: Vec) -> BinaryHeap { /// } /// ``` #[unstable = "matches collection reform specification, waiting for dust to settle"] - pub fn iter(&self) -> Items { - Items { iter: self.data.iter() } - } + pub fn iter(&self) -> Iter { + Iter { iter: self.data.iter() } } /// Creates a consuming iterator, that is, one that moves each value out of /// the binary heap in arbitrary order. The binary heap cannot be used @@ -260,8 +259,8 @@ pub fn iter(&self) -> Items { /// } /// ``` #[unstable = "matches collection reform specification, waiting for dust to settle"] - pub fn into_iter(self) -> MoveItems { - MoveItems { iter: self.data.into_iter() } + pub fn into_iter(self) -> IntoIter { + IntoIter { iter: self.data.into_iter() } } /// Returns the greatest item in a queue, or `None` if it is empty. @@ -571,11 +570,11 @@ pub fn drain<'a>(&'a mut self) -> Drain<'a, T> { } /// `BinaryHeap` iterator. -pub struct Items<'a, T: 'a> { - iter: slice::Items<'a, T>, +pub struct Iter <'a, T: 'a> { + iter: slice::Iter<'a, T>, } -impl<'a, T> Iterator<&'a T> for Items<'a, T> { +impl<'a, T> Iterator<&'a T> for Iter<'a, T> { #[inline] fn next(&mut self) -> Option<&'a T> { self.iter.next() } @@ -583,19 +582,19 @@ fn next(&mut self) -> Option<&'a T> { self.iter.next() } fn size_hint(&self) -> (uint, Option) { self.iter.size_hint() } } -impl<'a, T> DoubleEndedIterator<&'a T> for Items<'a, T> { +impl<'a, T> DoubleEndedIterator<&'a T> for Iter<'a, T> { #[inline] fn next_back(&mut self) -> Option<&'a T> { self.iter.next_back() } } -impl<'a, T> ExactSizeIterator<&'a T> for Items<'a, T> {} +impl<'a, T> ExactSizeIterator<&'a T> for Iter<'a, T> {} /// An iterator that moves out of a `BinaryHeap`. -pub struct MoveItems { - iter: vec::MoveItems, +pub struct IntoIter { + iter: vec::IntoIter, } -impl Iterator for MoveItems { +impl Iterator for IntoIter { #[inline] fn next(&mut self) -> Option { self.iter.next() } @@ -603,12 +602,12 @@ fn next(&mut self) -> Option { self.iter.next() } fn size_hint(&self) -> (uint, Option) { self.iter.size_hint() } } -impl DoubleEndedIterator for MoveItems { +impl DoubleEndedIterator for IntoIter { #[inline] fn next_back(&mut self) -> Option { self.iter.next_back() } } -impl ExactSizeIterator for MoveItems {} +impl ExactSizeIterator for IntoIter {} /// An iterator that drains a `BinaryHeap`. pub struct Drain<'a, T: 'a> { diff --git a/src/libcollections/bit.rs b/src/libcollections/bit.rs index 17dbf8a2cae9..8ceeb99d5856 100644 --- a/src/libcollections/bit.rs +++ b/src/libcollections/bit.rs @@ -145,7 +145,7 @@ fn index<'a>(&'a self, i: &uint) -> &'a bool { } struct MaskWords<'a> { - iter: slice::Items<'a, u32>, + iter: slice::Iter<'a, u32>, next_word: Option<&'a u32>, last_word_mask: u32, offset: uint diff --git a/src/libcollections/btree/node.rs b/src/libcollections/btree/node.rs index 86c7def49b19..2c3c546fdb7f 100644 --- a/src/libcollections/btree/node.rs +++ b/src/libcollections/btree/node.rs @@ -1382,14 +1382,14 @@ pub enum TraversalItem { } /// A traversal over a node's entries and edges -pub type Traversal<'a, K, V> = AbsTraversal, - slice::Items<'a, V>>, - slice::Items<'a, Node>>>; +pub type Traversal<'a, K, V> = AbsTraversal, + slice::Iter<'a, V>>, + slice::Iter<'a, Node>>>; /// A mutable traversal over a node's entries and edges -pub type MutTraversal<'a, K, V> = AbsTraversal, - slice::MutItems<'a, V>>, - slice::MutItems<'a, Node>>>; +pub type MutTraversal<'a, K, V> = AbsTraversal, + slice::IterMut<'a, V>>, + slice::IterMut<'a, Node>>>; /// An owning traversal over a node's entries and edges pub type MoveTraversal = AbsTraversal>; diff --git a/src/libcollections/btree/set.rs b/src/libcollections/btree/set.rs index e4328a3cb202..26e82994f567 100644 --- a/src/libcollections/btree/set.rs +++ b/src/libcollections/btree/set.rs @@ -33,37 +33,37 @@ pub struct BTreeSet{ } /// An iterator over a BTreeSet's items. -pub struct Items<'a, T: 'a> { +pub struct Iter<'a, T: 'a> { iter: Keys<'a, T, ()> } /// An owning iterator over a BTreeSet's items. -pub struct MoveItems { +pub struct IntoIter { iter: Map<(T, ()), T, MoveEntries, fn((T, ())) -> T> } /// A lazy iterator producing elements in the set difference (in-order). pub struct DifferenceItems<'a, T:'a> { - a: Peekable<&'a T, Items<'a, T>>, - b: Peekable<&'a T, Items<'a, T>>, + a: Peekable<&'a T, Iter<'a, T>>, + b: Peekable<&'a T, Iter<'a, T>>, } /// A lazy iterator producing elements in the set symmetric difference (in-order). pub struct SymDifferenceItems<'a, T:'a> { - a: Peekable<&'a T, Items<'a, T>>, - b: Peekable<&'a T, Items<'a, T>>, + a: Peekable<&'a T, Iter<'a, T>>, + b: Peekable<&'a T, Iter<'a, T>>, } /// A lazy iterator producing elements in the set intersection (in-order). pub struct IntersectionItems<'a, T:'a> { - a: Peekable<&'a T, Items<'a, T>>, - b: Peekable<&'a T, Items<'a, T>>, + a: Peekable<&'a T, Iter<'a, T>>, + b: Peekable<&'a T, Iter<'a, T>>, } /// A lazy iterator producing elements in the set union (in-order). pub struct UnionItems<'a, T:'a> { - a: Peekable<&'a T, Items<'a, T>>, - b: Peekable<&'a T, Items<'a, T>>, + a: Peekable<&'a T, Iter<'a, T>>, + b: Peekable<&'a T, Iter<'a, T>>, } impl BTreeSet { @@ -107,8 +107,8 @@ impl BTreeSet { /// assert_eq!(v, vec![1u,2,3,4]); /// ``` #[unstable = "matches collection reform specification, waiting for dust to settle"] - pub fn iter<'a>(&'a self) -> Items<'a, T> { - Items { iter: self.map.keys() } + pub fn iter<'a>(&'a self) -> Iter<'a, T> { + Iter { iter: self.map.keys() } } /// Gets an iterator for moving out the BtreeSet's contents. @@ -124,10 +124,10 @@ pub fn iter<'a>(&'a self) -> Items<'a, T> { /// assert_eq!(v, vec![1u,2,3,4]); /// ``` #[unstable = "matches collection reform specification, waiting for dust to settle"] - pub fn into_iter(self) -> MoveItems { + pub fn into_iter(self) -> IntoIter { fn first((a, _): (A, B)) -> A { a } - MoveItems { iter: self.map.into_iter().map(first) } + IntoIter { iter: self.map.into_iter().map(first) } } } @@ -544,24 +544,24 @@ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { } } -impl<'a, T> Iterator<&'a T> for Items<'a, T> { +impl<'a, T> Iterator<&'a T> for Iter<'a, T> { fn next(&mut self) -> Option<&'a T> { self.iter.next() } fn size_hint(&self) -> (uint, Option) { self.iter.size_hint() } } -impl<'a, T> DoubleEndedIterator<&'a T> for Items<'a, T> { +impl<'a, T> DoubleEndedIterator<&'a T> for Iter<'a, T> { fn next_back(&mut self) -> Option<&'a T> { self.iter.next_back() } } -impl<'a, T> ExactSizeIterator<&'a T> for Items<'a, T> {} +impl<'a, T> ExactSizeIterator<&'a T> for Iter<'a, T> {} -impl Iterator for MoveItems { +impl Iterator for IntoIter { fn next(&mut self) -> Option { self.iter.next() } fn size_hint(&self) -> (uint, Option) { self.iter.size_hint() } } -impl DoubleEndedIterator for MoveItems { +impl DoubleEndedIterator for IntoIter { fn next_back(&mut self) -> Option { self.iter.next_back() } } -impl ExactSizeIterator for MoveItems {} +impl ExactSizeIterator for IntoIter {} /// Compare `x` and `y`, but return `short` if x is None and `long` if y is None fn cmp_opt(x: Option<&T>, y: Option<&T>, diff --git a/src/libcollections/dlist.rs b/src/libcollections/dlist.rs index d3c1a0f81a33..eb057b488531 100644 --- a/src/libcollections/dlist.rs +++ b/src/libcollections/dlist.rs @@ -51,21 +51,21 @@ struct Node { } /// An iterator over references to the items of a `DList`. -pub struct Items<'a, T:'a> { +pub struct Iter<'a, T:'a> { head: &'a Link, tail: Rawlink>, nelem: uint, } // FIXME #11820: the &'a Option<> of the Link stops clone working. -impl<'a, T> Clone for Items<'a, T> { - fn clone(&self) -> Items<'a, T> { *self } +impl<'a, T> Clone for Iter<'a, T> { + fn clone(&self) -> Iter<'a, T> { *self } } -impl<'a,T> Copy for Items<'a,T> {} +impl<'a,T> Copy for Iter<'a,T> {} /// An iterator over mutable references to the items of a `DList`. -pub struct MutItems<'a, T:'a> { +pub struct IterMut<'a, T:'a> { list: &'a mut DList, head: Rawlink>, tail: Rawlink>, @@ -74,7 +74,7 @@ pub struct MutItems<'a, T:'a> { /// An iterator over mutable references to the items of a `DList`. #[deriving(Clone)] -pub struct MoveItems { +pub struct IntoIter { list: DList } @@ -394,19 +394,19 @@ pub fn merge(&mut self, mut other: DList, mut f: F) where F: FnMut(&T, &T) /// Provides a forward iterator. #[inline] #[unstable = "matches collection reform specification, waiting for dust to settle"] - pub fn iter<'a>(&'a self) -> Items<'a, T> { - Items{nelem: self.len(), head: &self.list_head, tail: self.list_tail} + pub fn iter<'a>(&'a self) -> Iter<'a, T> { + Iter{nelem: self.len(), head: &self.list_head, tail: self.list_tail} } /// Provides a forward iterator with mutable references. #[inline] #[unstable = "matches collection reform specification, waiting for dust to settle"] - pub fn iter_mut<'a>(&'a mut self) -> MutItems<'a, T> { + pub fn iter_mut<'a>(&'a mut self) -> IterMut<'a, T> { let head_raw = match self.list_head { Some(ref mut h) => Rawlink::some(&mut **h), None => Rawlink::none(), }; - MutItems{ + IterMut{ nelem: self.len(), head: head_raw, tail: self.list_tail, @@ -417,8 +417,8 @@ pub fn iter_mut<'a>(&'a mut self) -> MutItems<'a, T> { /// Consumes the list into an iterator yielding elements by value. #[inline] #[unstable = "matches collection reform specification, waiting for dust to settle"] - pub fn into_iter(self) -> MoveItems { - MoveItems{list: self} + pub fn into_iter(self) -> IntoIter { + IntoIter{list: self} } /// Returns `true` if the `DList` is empty. @@ -579,7 +579,7 @@ fn drop(&mut self) { } -impl<'a, A> Iterator<&'a A> for Items<'a, A> { +impl<'a, A> Iterator<&'a A> for Iter<'a, A> { #[inline] fn next(&mut self) -> Option<&'a A> { if self.nelem == 0 { @@ -598,7 +598,7 @@ fn size_hint(&self) -> (uint, Option) { } } -impl<'a, A> DoubleEndedIterator<&'a A> for Items<'a, A> { +impl<'a, A> DoubleEndedIterator<&'a A> for Iter<'a, A> { #[inline] fn next_back(&mut self) -> Option<&'a A> { if self.nelem == 0 { @@ -612,9 +612,9 @@ fn next_back(&mut self) -> Option<&'a A> { } } -impl<'a, A> ExactSizeIterator<&'a A> for Items<'a, A> {} +impl<'a, A> ExactSizeIterator<&'a A> for Iter<'a, A> {} -impl<'a, A> Iterator<&'a mut A> for MutItems<'a, A> { +impl<'a, A> Iterator<&'a mut A> for IterMut<'a, A> { #[inline] fn next(&mut self) -> Option<&'a mut A> { if self.nelem == 0 { @@ -636,7 +636,7 @@ fn size_hint(&self) -> (uint, Option) { } } -impl<'a, A> DoubleEndedIterator<&'a mut A> for MutItems<'a, A> { +impl<'a, A> DoubleEndedIterator<&'a mut A> for IterMut<'a, A> { #[inline] fn next_back(&mut self) -> Option<&'a mut A> { if self.nelem == 0 { @@ -650,7 +650,7 @@ fn next_back(&mut self) -> Option<&'a mut A> { } } -impl<'a, A> ExactSizeIterator<&'a mut A> for MutItems<'a, A> {} +impl<'a, A> ExactSizeIterator<&'a mut A> for IterMut<'a, A> {} /// Allows mutating a `DList` while iterating. pub trait ListInsertion { @@ -664,8 +664,8 @@ pub trait ListInsertion { fn peek_next<'a>(&'a mut self) -> Option<&'a mut A>; } -// private methods for MutItems -impl<'a, A> MutItems<'a, A> { +// private methods for IterMut +impl<'a, A> IterMut<'a, A> { fn insert_next_node(&mut self, mut ins_node: Box>) { // Insert before `self.head` so that it is between the // previously yielded element and self.head. @@ -687,7 +687,7 @@ fn insert_next_node(&mut self, mut ins_node: Box>) { } } -impl<'a, A> ListInsertion for MutItems<'a, A> { +impl<'a, A> ListInsertion for IterMut<'a, A> { #[inline] fn insert_next(&mut self, elt: A) { self.insert_next_node(box Node::new(elt)) @@ -702,7 +702,7 @@ fn peek_next(&mut self) -> Option<&mut A> { } } -impl Iterator for MoveItems { +impl Iterator for IntoIter { #[inline] fn next(&mut self) -> Option { self.list.pop_front() } @@ -712,7 +712,7 @@ fn size_hint(&self) -> (uint, Option) { } } -impl DoubleEndedIterator for MoveItems { +impl DoubleEndedIterator for IntoIter { #[inline] fn next_back(&mut self) -> Option { self.list.pop_back() } } diff --git a/src/libcollections/enum_set.rs b/src/libcollections/enum_set.rs index bb762f4fb4e4..fd04ce94247b 100644 --- a/src/libcollections/enum_set.rs +++ b/src/libcollections/enum_set.rs @@ -178,8 +178,8 @@ pub fn contains(&self, e: &E) -> bool { /// Returns an iterator over an `EnumSet`. #[unstable = "matches collection reform specification, waiting for dust to settle"] - pub fn iter(&self) -> Items { - Items::new(self.bits) + pub fn iter(&self) -> Iter { + Iter::new(self.bits) } } @@ -208,18 +208,18 @@ fn bitxor(self, e: EnumSet) -> EnumSet { } /// An iterator over an EnumSet -pub struct Items { +pub struct Iter { index: uint, bits: uint, } -impl Items { - fn new(bits: uint) -> Items { - Items { index: 0, bits: bits } +impl Iter { + fn new(bits: uint) -> Iter { + Iter { index: 0, bits: bits } } } -impl Iterator for Items { +impl Iterator for Iter { fn next(&mut self) -> Option { if self.bits == 0 { return None; diff --git a/src/libcollections/ring_buf.rs b/src/libcollections/ring_buf.rs index aa0e33248fcc..37c088f74536 100644 --- a/src/libcollections/ring_buf.rs +++ b/src/libcollections/ring_buf.rs @@ -376,8 +376,8 @@ pub fn reserve(&mut self, additional: uint) { /// assert_eq!(buf.iter().collect::>().as_slice(), b); /// ``` #[unstable = "matches collection reform specification, waiting for dust to settle"] - pub fn iter(&self) -> Items { - Items { + pub fn iter(&self) -> Iter { + Iter { tail: self.tail, head: self.head, ring: unsafe { self.buffer_as_slice() } @@ -402,8 +402,8 @@ pub fn iter(&self) -> Items { /// assert_eq!(buf.iter_mut().collect::>()[], b); /// ``` #[unstable = "matches collection reform specification, waiting for dust to settle"] - pub fn iter_mut<'a>(&'a mut self) -> MutItems<'a, T> { - MutItems { + pub fn iter_mut<'a>(&'a mut self) -> IterMut<'a, T> { + IterMut { tail: self.tail, head: self.head, cap: self.cap, @@ -414,8 +414,8 @@ pub fn iter_mut<'a>(&'a mut self) -> MutItems<'a, T> { /// Consumes the list into an iterator yielding elements by value. #[unstable = "matches collection reform specification, waiting for dust to settle"] - pub fn into_iter(self) -> MoveItems { - MoveItems { + pub fn into_iter(self) -> IntoIter { + IntoIter { inner: self, } } @@ -1122,13 +1122,13 @@ fn count(tail: uint, head: uint, size: uint) -> uint { } /// `RingBuf` iterator. -pub struct Items<'a, T:'a> { +pub struct Iter<'a, T:'a> { ring: &'a [T], tail: uint, head: uint } -impl<'a, T> Iterator<&'a T> for Items<'a, T> { +impl<'a, T> Iterator<&'a T> for Iter<'a, T> { #[inline] fn next(&mut self) -> Option<&'a T> { if self.tail == self.head { @@ -1146,7 +1146,7 @@ fn size_hint(&self) -> (uint, Option) { } } -impl<'a, T> DoubleEndedIterator<&'a T> for Items<'a, T> { +impl<'a, T> DoubleEndedIterator<&'a T> for Iter<'a, T> { #[inline] fn next_back(&mut self) -> Option<&'a T> { if self.tail == self.head { @@ -1157,9 +1157,9 @@ fn next_back(&mut self) -> Option<&'a T> { } } -impl<'a, T> ExactSizeIterator<&'a T> for Items<'a, T> {} +impl<'a, T> ExactSizeIterator<&'a T> for Iter<'a, T> {} -impl<'a, T> RandomAccessIterator<&'a T> for Items<'a, T> { +impl<'a, T> RandomAccessIterator<&'a T> for Iter<'a, T> { #[inline] fn indexable(&self) -> uint { let (len, _) = self.size_hint(); @@ -1177,11 +1177,11 @@ fn idx(&mut self, j: uint) -> Option<&'a T> { } } -// FIXME This was implemented differently from Items because of a problem +// FIXME This was implemented differently from Iter because of a problem // with returning the mutable reference. I couldn't find a way to // make the lifetime checker happy so, but there should be a way. /// `RingBuf` mutable iterator. -pub struct MutItems<'a, T:'a> { +pub struct IterMut<'a, T:'a> { ptr: *mut T, tail: uint, head: uint, @@ -1189,7 +1189,7 @@ pub struct MutItems<'a, T:'a> { marker: marker::ContravariantLifetime<'a>, } -impl<'a, T> Iterator<&'a mut T> for MutItems<'a, T> { +impl<'a, T> Iterator<&'a mut T> for IterMut<'a, T> { #[inline] fn next(&mut self) -> Option<&'a mut T> { if self.tail == self.head { @@ -1210,7 +1210,7 @@ fn size_hint(&self) -> (uint, Option) { } } -impl<'a, T> DoubleEndedIterator<&'a mut T> for MutItems<'a, T> { +impl<'a, T> DoubleEndedIterator<&'a mut T> for IterMut<'a, T> { #[inline] fn next_back(&mut self) -> Option<&'a mut T> { if self.tail == self.head { @@ -1224,14 +1224,14 @@ fn next_back(&mut self) -> Option<&'a mut T> { } } -impl<'a, T> ExactSizeIterator<&'a mut T> for MutItems<'a, T> {} +impl<'a, T> ExactSizeIterator<&'a mut T> for IterMut<'a, T> {} // A by-value RingBuf iterator -pub struct MoveItems { +pub struct IntoIter { inner: RingBuf, } -impl Iterator for MoveItems { +impl Iterator for IntoIter { #[inline] fn next(&mut self) -> Option { self.inner.pop_front() @@ -1244,14 +1244,14 @@ fn size_hint(&self) -> (uint, Option) { } } -impl DoubleEndedIterator for MoveItems { +impl DoubleEndedIterator for IntoIter { #[inline] fn next_back(&mut self) -> Option { self.inner.pop_back() } } -impl ExactSizeIterator for MoveItems {} +impl ExactSizeIterator for IntoIter {} /// A draining RingBuf iterator pub struct Drain<'a, T: 'a> { diff --git a/src/libcollections/slice.rs b/src/libcollections/slice.rs index 16adf6fa224b..d6d94f57acf4 100644 --- a/src/libcollections/slice.rs +++ b/src/libcollections/slice.rs @@ -37,7 +37,7 @@ //! //! ## Structs //! -//! There are several structs that are useful for slices, such as `Items`, which +//! There are several structs that are useful for slices, such as `Iter`, which //! represents iteration over a slice. //! //! ## Traits @@ -104,7 +104,7 @@ use vec::Vec; pub use core::slice::{Chunks, AsSlice, SplitsN, Windows}; -pub use core::slice::{Items, MutItems, PartialEqSliceExt}; +pub use core::slice::{Iter, IterMut, PartialEqSliceExt}; pub use core::slice::{ImmutableIntSlice, MutableIntSlice}; pub use core::slice::{MutSplits, MutChunks, Splits}; pub use core::slice::{bytes, mut_ref_slice, ref_slice}; @@ -771,7 +771,7 @@ pub trait SliceExt for Sized? { /// Returns an iterator over the slice #[unstable = "iterator type may change"] - fn iter(&self) -> Items; + fn iter(&self) -> Iter; /// Returns an iterator over subslices separated by elements that match /// `pred`. The matched element is not contained in the subslices. @@ -970,7 +970,7 @@ fn is_empty(&self) -> bool { self.len() == 0 } /// Returns an iterator that allows modifying each value #[unstable = "waiting on iterator type name conventions"] - fn iter_mut(&mut self) -> MutItems; + fn iter_mut(&mut self) -> IterMut; /// Returns a mutable pointer to the first element of a slice, or `None` if it is empty #[unstable = "name may change"] @@ -1137,7 +1137,7 @@ fn split_at<'a>(&'a self, mid: uint) -> (&'a [T], &'a [T]) { } #[inline] - fn iter<'a>(&'a self) -> Items<'a, T> { + fn iter<'a>(&'a self) -> Iter<'a, T> { core_slice::SliceExt::iter(self) } @@ -1246,7 +1246,7 @@ fn slice_to_mut<'a>(&'a mut self, end: uint) -> &'a mut [T] { } #[inline] - fn iter_mut<'a>(&'a mut self) -> MutItems<'a, T> { + fn iter_mut<'a>(&'a mut self) -> IterMut<'a, T> { core_slice::SliceExt::iter_mut(self) } diff --git a/src/libcollections/vec.rs b/src/libcollections/vec.rs index b82c7e4cba27..fa0e4a2340e2 100644 --- a/src/libcollections/vec.rs +++ b/src/libcollections/vec.rs @@ -888,7 +888,7 @@ pub fn as_mut_slice<'a>(&'a mut self) -> &'a mut [T] { /// ``` #[inline] #[unstable = "matches collection reform specification, waiting for dust to settle"] - pub fn into_iter(self) -> MoveItems { + pub fn into_iter(self) -> IntoIter { unsafe { let ptr = self.ptr; let cap = self.cap; @@ -899,7 +899,7 @@ pub fn into_iter(self) -> MoveItems { ptr.offset(self.len() as int) as *const T }; mem::forget(self); - MoveItems { allocation: ptr, cap: cap, ptr: begin, end: end } + IntoIter { allocation: ptr, cap: cap, ptr: begin, end: end } } } @@ -1402,21 +1402,21 @@ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { } /// An iterator that moves out of a vector. -pub struct MoveItems { +pub struct IntoIter { allocation: *mut T, // the block of memory allocated for the vector cap: uint, // the capacity of the vector ptr: *const T, end: *const T } -impl MoveItems { +impl IntoIter { /// Drops all items that have not yet been moved and returns the empty vector. #[inline] #[unstable] pub fn into_inner(mut self) -> Vec { unsafe { for _x in self { } - let MoveItems { allocation, cap, ptr: _ptr, end: _end } = self; + let IntoIter { allocation, cap, ptr: _ptr, end: _end } = self; mem::forget(self); Vec { ptr: allocation, cap: cap, len: 0 } } @@ -1427,7 +1427,7 @@ pub fn into_inner(mut self) -> Vec { pub fn unwrap(self) -> Vec { self.into_inner() } } -impl Iterator for MoveItems { +impl Iterator for IntoIter { #[inline] fn next<'a>(&'a mut self) -> Option { unsafe { @@ -1461,7 +1461,7 @@ fn size_hint(&self) -> (uint, Option) { } } -impl DoubleEndedIterator for MoveItems { +impl DoubleEndedIterator for IntoIter { #[inline] fn next_back<'a>(&'a mut self) -> Option { unsafe { @@ -1484,10 +1484,10 @@ fn next_back<'a>(&'a mut self) -> Option { } } -impl ExactSizeIterator for MoveItems {} +impl ExactSizeIterator for IntoIter {} #[unsafe_destructor] -impl Drop for MoveItems { +impl Drop for IntoIter { fn drop(&mut self) { // destroy the remaining elements if self.cap != 0 { diff --git a/src/libcollections/vec_map.rs b/src/libcollections/vec_map.rs index 1babde6066d0..802be4271898 100644 --- a/src/libcollections/vec_map.rs +++ b/src/libcollections/vec_map.rs @@ -235,13 +235,13 @@ pub fn iter_mut<'r>(&'r mut self) -> MutEntries<'r, V> { /// assert_eq!(vec, vec![(1, "a"), (2, "b"), (3, "c")]); /// ``` #[unstable = "matches collection reform specification, waiting for dust to settle"] - pub fn into_iter(&mut self) -> MoveItems { + pub fn into_iter(&mut self) -> IntoIter { fn filter((i, v): (uint, Option)) -> Option<(uint, A)> { v.map(|v| (i, v)) } let values = replace(&mut self.v, vec!()); - MoveItems { iter: values.into_iter().enumerate().filter_map(filter) } + IntoIter { iter: values.into_iter().enumerate().filter_map(filter) } } /// Return the number of elements in the map. @@ -608,7 +608,7 @@ fn next_back(&mut self) -> Option<$elem> { pub struct Entries<'a, V:'a> { front: uint, back: uint, - iter: slice::Items<'a, Option> + iter: slice::Iter<'a, Option> } iterator! { impl Entries -> (uint, &'a V), as_ref } @@ -619,7 +619,7 @@ pub struct Entries<'a, V:'a> { pub struct MutEntries<'a, V:'a> { front: uint, back: uint, - iter: slice::MutItems<'a, Option> + iter: slice::IterMut<'a, Option> } iterator! { impl MutEntries -> (uint, &'a mut V), as_mut } @@ -636,11 +636,11 @@ pub struct Values<'a, V: 'a> { } /// A consuming iterator over the key-value pairs of a map. -pub struct MoveItems { +pub struct IntoIter { iter: FilterMap< (uint, Option), (uint, V), - Enumerate>>, + Enumerate>>, fn((uint, Option)) -> Option<(uint, V)>> } @@ -662,11 +662,11 @@ fn next_back(&mut self) -> Option<(&'a V)> { self.iter.next_back() } } -impl Iterator<(uint, V)> for MoveItems { +impl Iterator<(uint, V)> for IntoIter { fn next(&mut self) -> Option<(uint, V)> { self.iter.next() } fn size_hint(&self) -> (uint, Option) { self.iter.size_hint() } } -impl DoubleEndedIterator<(uint, V)> for MoveItems { +impl DoubleEndedIterator<(uint, V)> for IntoIter { fn next_back(&mut self) -> Option<(uint, V)> { self.iter.next_back() } } diff --git a/src/libcore/fmt/mod.rs b/src/libcore/fmt/mod.rs index 79fb11f38543..96f67ac4f7a1 100644 --- a/src/libcore/fmt/mod.rs +++ b/src/libcore/fmt/mod.rs @@ -89,7 +89,7 @@ pub struct Formatter<'a> { precision: Option, buf: &'a mut (FormatWriter+'a), - curarg: slice::Items<'a, Argument<'a>>, + curarg: slice::Iter<'a, Argument<'a>>, args: &'a [Argument<'a>], } diff --git a/src/libcore/slice.rs b/src/libcore/slice.rs index efc92429afdf..6092a45c97d9 100644 --- a/src/libcore/slice.rs +++ b/src/libcore/slice.rs @@ -67,7 +67,7 @@ pub trait SliceExt for Sized? { fn slice_from<'a>(&'a self, start: uint) -> &'a [T]; fn slice_to<'a>(&'a self, end: uint) -> &'a [T]; fn split_at<'a>(&'a self, mid: uint) -> (&'a [T], &'a [T]); - fn iter<'a>(&'a self) -> Items<'a, T>; + fn iter<'a>(&'a self) -> Iter<'a, T>; fn split<'a, P>(&'a self, pred: P) -> Splits<'a, T, P> where P: FnMut(&T) -> bool; fn splitn<'a, P>(&'a self, n: uint, pred: P) -> SplitsN> @@ -92,7 +92,7 @@ fn is_empty(&self) -> bool { self.len() == 0 } fn slice_mut<'a>(&'a mut self, start: uint, end: uint) -> &'a mut [T]; fn slice_from_mut<'a>(&'a mut self, start: uint) -> &'a mut [T]; fn slice_to_mut<'a>(&'a mut self, end: uint) -> &'a mut [T]; - fn iter_mut<'a>(&'a mut self) -> MutItems<'a, T>; + fn iter_mut<'a>(&'a mut self) -> IterMut<'a, T>; fn head_mut<'a>(&'a mut self) -> Option<&'a mut T>; fn tail_mut<'a>(&'a mut self) -> &'a mut [T]; fn init_mut<'a>(&'a mut self) -> &'a mut [T]; @@ -141,15 +141,15 @@ fn split_at(&self, mid: uint) -> (&[T], &[T]) { } #[inline] - fn iter<'a>(&'a self) -> Items<'a, T> { + fn iter<'a>(&'a self) -> Iter<'a, T> { unsafe { let p = self.as_ptr(); if mem::size_of::() == 0 { - Items{ptr: p, + Iter{ptr: p, end: (p as uint + self.len()) as *const T, marker: marker::ContravariantLifetime::<'a>} } else { - Items{ptr: p, + Iter{ptr: p, end: p.offset(self.len() as int), marker: marker::ContravariantLifetime::<'a>} } @@ -286,15 +286,15 @@ fn split_at_mut(&mut self, mid: uint) -> (&mut [T], &mut [T]) { } #[inline] - fn iter_mut<'a>(&'a mut self) -> MutItems<'a, T> { + fn iter_mut<'a>(&'a mut self) -> IterMut<'a, T> { unsafe { let p = self.as_mut_ptr(); if mem::size_of::() == 0 { - MutItems{ptr: p, + IterMut{ptr: p, end: (p as uint + self.len()) as *mut T, marker: marker::ContravariantLifetime::<'a>} } else { - MutItems{ptr: p, + IterMut{ptr: p, end: p.offset(self.len() as int), marker: marker::ContravariantLifetime::<'a>} } @@ -655,7 +655,7 @@ fn default() -> &'a [T] { &[] } // Iterators // -// The shared definition of the `Item` and `MutItems` iterators +// The shared definition of the `Item` and `IterMut` iterators macro_rules! iterator { (struct $name:ident -> $ptr:ty, $elem:ty) => { #[experimental = "needs review"] @@ -738,14 +738,14 @@ macro_rules! make_slice { /// Immutable slice iterator #[experimental = "needs review"] -pub struct Items<'a, T: 'a> { +pub struct Iter<'a, T: 'a> { ptr: *const T, end: *const T, marker: marker::ContravariantLifetime<'a> } #[experimental] -impl<'a, T> ops::Slice for Items<'a, T> { +impl<'a, T> ops::Slice for Iter<'a, T> { fn as_slice_(&self) -> &[T] { self.as_slice() } @@ -763,7 +763,7 @@ fn slice_or_fail<'b>(&'b self, from: &uint, to: &uint) -> &'b [T] { } } -impl<'a, T> Items<'a, T> { +impl<'a, T> Iter<'a, T> { /// View the underlying data as a subslice of the original data. /// /// This has the same lifetime as the original slice, and so the @@ -774,20 +774,20 @@ pub fn as_slice(&self) -> &'a [T] { } } -impl<'a,T> Copy for Items<'a,T> {} +impl<'a,T> Copy for Iter<'a,T> {} -iterator!{struct Items -> *const T, &'a T} +iterator!{struct Iter -> *const T, &'a T} #[experimental = "needs review"] -impl<'a, T> ExactSizeIterator<&'a T> for Items<'a, T> {} +impl<'a, T> ExactSizeIterator<&'a T> for Iter<'a, T> {} -#[stable] -impl<'a, T> Clone for Items<'a, T> { - fn clone(&self) -> Items<'a, T> { *self } + #[experimental = "needs review"] +impl<'a, T> Clone for Iter<'a, T> { + fn clone(&self) -> Iter<'a, T> { *self } } #[experimental = "needs review"] -impl<'a, T> RandomAccessIterator<&'a T> for Items<'a, T> { +impl<'a, T> RandomAccessIterator<&'a T> for Iter<'a, T> { #[inline] fn indexable(&self) -> uint { let (exact, _) = self.size_hint(); @@ -813,14 +813,14 @@ fn idx(&mut self, index: uint) -> Option<&'a T> { /// Mutable slice iterator. #[experimental = "needs review"] -pub struct MutItems<'a, T: 'a> { +pub struct IterMut<'a, T: 'a> { ptr: *mut T, end: *mut T, marker: marker::ContravariantLifetime<'a>, } #[experimental] -impl<'a, T> ops::Slice for MutItems<'a, T> { +impl<'a, T> ops::Slice for IterMut<'a, T> { fn as_slice_<'b>(&'b self) -> &'b [T] { make_slice!(T -> &'b [T]: self.ptr, self.end) } @@ -839,7 +839,7 @@ fn slice_or_fail<'b>(&'b self, from: &uint, to: &uint) -> &'b [T] { } #[experimental] -impl<'a, T> ops::SliceMut for MutItems<'a, T> { +impl<'a, T> ops::SliceMut for IterMut<'a, T> { fn as_mut_slice_<'b>(&'b mut self) -> &'b mut [T] { make_slice!(T -> &'b mut [T]: self.ptr, self.end) } @@ -857,7 +857,7 @@ fn slice_or_fail_mut<'b>(&'b mut self, from: &uint, to: &uint) -> &'b mut [T] { } } -impl<'a, T> MutItems<'a, T> { +impl<'a, T> IterMut<'a, T> { /// View the underlying data as a subslice of the original data. /// /// To avoid creating `&mut` references that alias, this is forced @@ -870,10 +870,10 @@ pub fn into_slice(self) -> &'a mut [T] { } } -iterator!{struct MutItems -> *mut T, &'a mut T} +iterator!{struct IterMut -> *mut T, &'a mut T} #[experimental = "needs review"] -impl<'a, T> ExactSizeIterator<&'a mut T> for MutItems<'a, T> {} +impl<'a, T> ExactSizeIterator<&'a mut T> for IterMut<'a, T> {} /// An abstraction over the splitting iterators, so that splitn, splitn_mut etc /// can be implemented once. diff --git a/src/libcore/str.rs b/src/libcore/str.rs index a89a7970ae9c..e147229bcbdd 100644 --- a/src/libcore/str.rs +++ b/src/libcore/str.rs @@ -167,7 +167,7 @@ fn only_ascii(&self) -> bool { /// Created with the method `.chars()`. #[deriving(Clone, Copy)] pub struct Chars<'a> { - iter: slice::Items<'a, u8> + iter: slice::Iter<'a, u8> } // Return the initial codepoint accumulator for the first byte. @@ -315,7 +315,7 @@ fn next_back(&mut self) -> Option<(uint, char)> { /// External iterator for a string's bytes. /// Use with the `std::iter` module. -pub type Bytes<'a> = Map<&'a u8, u8, slice::Items<'a, u8>, BytesFn>; +pub type Bytes<'a> = Map<&'a u8, u8, slice::Iter<'a, u8>, BytesFn>; /// A temporary new type wrapper that ensures that the `Bytes` iterator /// is cloneable. @@ -893,7 +893,7 @@ pub fn eq_slice(a: &str, b: &str) -> bool { /// `iter` reset such that it is pointing at the first byte in the /// invalid sequence. #[inline(always)] -fn run_utf8_validation_iterator(iter: &mut slice::Items) -> bool { +fn run_utf8_validation_iterator(iter: &mut slice::Iter) -> bool { loop { // save the current thing we're pointing at. let old = *iter; @@ -993,7 +993,7 @@ macro_rules! next ( ($ret:expr) => { /// of `u16`s. #[deriving(Clone)] pub struct Utf16Items<'a> { - iter: slice::Items<'a, u16> + iter: slice::Iter<'a, u16> } /// The possibilities for values decoded from a `u16` stream. #[deriving(Copy, PartialEq, Eq, Clone, Show)] @@ -2366,4 +2366,3 @@ impl<'a> Default for &'a str { #[stable] fn default() -> &'a str { "" } } - diff --git a/src/libgraphviz/maybe_owned_vec.rs b/src/libgraphviz/maybe_owned_vec.rs index be8761043c0c..88483b6c9354 100644 --- a/src/libgraphviz/maybe_owned_vec.rs +++ b/src/libgraphviz/maybe_owned_vec.rs @@ -63,7 +63,7 @@ fn into_maybe_owned(self) -> MaybeOwnedVector<'a,T> { Borrowed(self) } } impl<'a,T> MaybeOwnedVector<'a,T> { - pub fn iter(&'a self) -> slice::Items<'a,T> { + pub fn iter(&'a self) -> slice::Iter<'a,T> { match self { &Growable(ref v) => v.as_slice().iter(), &Borrowed(ref v) => v.iter(), diff --git a/src/liblog/lib.rs b/src/liblog/lib.rs index 2bf9af902718..bd11f38c1434 100644 --- a/src/liblog/lib.rs +++ b/src/liblog/lib.rs @@ -370,7 +370,7 @@ pub fn mod_enabled(level: u32, module: &str) -> bool { fn enabled(level: u32, module: &str, - iter: slice::Items) + iter: slice::Iter) -> bool { // Search for the longest match, the vector is assumed to be pre-sorted. for directive in iter.rev() { diff --git a/src/libregex/re.rs b/src/libregex/re.rs index 151587e423ab..5c84c0a55d69 100644 --- a/src/libregex/re.rs +++ b/src/libregex/re.rs @@ -540,8 +540,8 @@ fn names_len(&self) -> uint { } pub enum NamesIter<'a> { - NamesIterNative(::std::slice::Items<'a, Option<&'static str>>), - NamesIterDynamic(::std::slice::Items<'a, Option>) + NamesIterNative(::std::slice::Iter<'a, Option<&'static str>>), + NamesIterDynamic(::std::slice::Iter<'a, Option>) } impl<'a> Iterator> for NamesIter<'a> { diff --git a/src/librustc/middle/lang_items.rs b/src/librustc/middle/lang_items.rs index 2ffc5d8a510a..967e7f070c5a 100644 --- a/src/librustc/middle/lang_items.rs +++ b/src/librustc/middle/lang_items.rs @@ -65,7 +65,7 @@ fn foo(_: LangItem) -> Option { None } } } - pub fn items<'a>(&'a self) -> Enumerate>> { + pub fn items<'a>(&'a self) -> Enumerate>> { self.items.iter().enumerate() } diff --git a/src/librustc/middle/subst.rs b/src/librustc/middle/subst.rs index 30a47ff91325..6098e0065e04 100644 --- a/src/librustc/middle/subst.rs +++ b/src/librustc/middle/subst.rs @@ -18,7 +18,7 @@ use util::ppaux::Repr; use std::fmt; -use std::slice::Items; +use std::slice::Iter; use std::vec::Vec; use syntax::codemap::{Span, DUMMY_SP}; @@ -400,7 +400,7 @@ pub fn get<'a>(&'a self, space: ParamSpace, index: uint) -> &'a T { &self.get_slice(space)[index] } - pub fn iter<'a>(&'a self) -> Items<'a,T> { + pub fn iter<'a>(&'a self) -> Iter<'a,T> { self.content.iter() } diff --git a/src/librustc/middle/traits/mod.rs b/src/librustc/middle/traits/mod.rs index 3289acd0c2e5..8028971a4634 100644 --- a/src/librustc/middle/traits/mod.rs +++ b/src/librustc/middle/traits/mod.rs @@ -19,7 +19,7 @@ use middle::ty::{mod, Ty}; use middle::infer::InferCtxt; use std::rc::Rc; -use std::slice::Items; +use std::slice::Iter; use syntax::ast; use syntax::codemap::{Span, DUMMY_SP}; @@ -304,7 +304,7 @@ pub fn dummy() -> ObligationCause<'tcx> { } impl<'tcx, N> Vtable<'tcx, N> { - pub fn iter_nested(&self) -> Items { + pub fn iter_nested(&self) -> Iter { match *self { VtableImpl(ref i) => i.iter_nested(), VtableFnPointer(..) => (&[]).iter(), @@ -338,7 +338,7 @@ pub fn map_move_nested(self, op: F) -> Vtable<'tcx, M> where } impl<'tcx, N> VtableImplData<'tcx, N> { - pub fn iter_nested(&self) -> Items { + pub fn iter_nested(&self) -> Iter { self.nested.iter() } @@ -365,7 +365,7 @@ pub fn map_move_nested(self, op: F) -> VtableImplData<'tcx, M> where } impl VtableBuiltinData { - pub fn iter_nested(&self) -> Items { + pub fn iter_nested(&self) -> Iter { self.nested.iter() } diff --git a/src/libsyntax/ast_map/mod.rs b/src/libsyntax/ast_map/mod.rs index a95c9e199060..c8c7297f790a 100644 --- a/src/libsyntax/ast_map/mod.rs +++ b/src/libsyntax/ast_map/mod.rs @@ -73,9 +73,9 @@ fn next(&mut self) -> Option { } } -// HACK(eddyb) move this into libstd (value wrapper for slice::Items). +// HACK(eddyb) move this into libstd (value wrapper for slice::Iter). #[deriving(Clone)] -pub struct Values<'a, T:'a>(pub slice::Items<'a, T>); +pub struct Values<'a, T:'a>(pub slice::Iter<'a, T>); impl<'a, T: Copy> Iterator for Values<'a, T> { fn next(&mut self) -> Option { diff --git a/src/libsyntax/ext/deriving/generic/mod.rs b/src/libsyntax/ext/deriving/generic/mod.rs index d8de3d2db979..2f06271d8de3 100644 --- a/src/libsyntax/ext/deriving/generic/mod.rs +++ b/src/libsyntax/ext/deriving/generic/mod.rs @@ -766,7 +766,7 @@ fn expand_struct_method_body(&self, let fields = if raw_fields.len() > 0 { let mut raw_fields = raw_fields.into_iter().map(|v| v.into_iter()); let first_field = raw_fields.next().unwrap(); - let mut other_fields: Vec, P)>> + let mut other_fields: Vec, P)>> = raw_fields.collect(); first_field.map(|(span, opt_id, field)| { FieldInfo { diff --git a/src/libsyntax/owned_slice.rs b/src/libsyntax/owned_slice.rs index 8e418e46921f..3023c547fb05 100644 --- a/src/libsyntax/owned_slice.rs +++ b/src/libsyntax/owned_slice.rs @@ -45,7 +45,7 @@ pub fn as_slice<'a>(&'a self) -> &'a [T] { &*self.data } - pub fn move_iter(self) -> vec::MoveItems { + pub fn move_iter(self) -> vec::IntoIter { self.into_vec().into_iter() } diff --git a/src/libsyntax/util/small_vector.rs b/src/libsyntax/util/small_vector.rs index 8d050e34abf4..946181770c8e 100644 --- a/src/libsyntax/util/small_vector.rs +++ b/src/libsyntax/util/small_vector.rs @@ -8,7 +8,7 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. use self::SmallVectorRepr::*; -use self::MoveItemsRepr::*; +use self::IntoIterRepr::*; use std::mem; use std::slice; @@ -111,17 +111,17 @@ pub fn expect_one(self, err: &'static str) -> T { /// Deprecated: use `into_iter`. #[deprecated = "use into_iter"] - pub fn move_iter(self) -> MoveItems { + pub fn move_iter(self) -> IntoIter { self.into_iter() } - pub fn into_iter(self) -> MoveItems { + pub fn into_iter(self) -> IntoIter { let repr = match self.repr { Zero => ZeroIterator, One(v) => OneIterator(v), Many(vs) => ManyIterator(vs.into_iter()) }; - MoveItems { repr: repr } + IntoIter { repr: repr } } pub fn len(&self) -> uint { @@ -135,17 +135,17 @@ pub fn len(&self) -> uint { pub fn is_empty(&self) -> bool { self.len() == 0 } } -pub struct MoveItems { - repr: MoveItemsRepr, +pub struct IntoIter { + repr: IntoIterRepr, } -enum MoveItemsRepr { +enum IntoIterRepr { ZeroIterator, OneIterator(T), - ManyIterator(vec::MoveItems), + ManyIterator(vec::IntoIter), } -impl Iterator for MoveItems { +impl Iterator for IntoIter { fn next(&mut self) -> Option { match self.repr { ZeroIterator => None, diff --git a/src/test/bench/shootout-k-nucleotide.rs b/src/test/bench/shootout-k-nucleotide.rs index 8521e2216e93..6aa6b02857fb 100644 --- a/src/test/bench/shootout-k-nucleotide.rs +++ b/src/test/bench/shootout-k-nucleotide.rs @@ -130,7 +130,7 @@ struct Table { struct Items<'a> { cur: Option<&'a Entry>, - items: slice::Items<'a, Option>>, + items: slice::Iter<'a, Option>>, } impl Table { diff --git a/src/test/compile-fail/resolve-conflict-type-vs-import.rs b/src/test/compile-fail/resolve-conflict-type-vs-import.rs index fa072fa62ab7..de934286a7cb 100644 --- a/src/test/compile-fail/resolve-conflict-type-vs-import.rs +++ b/src/test/compile-fail/resolve-conflict-type-vs-import.rs @@ -8,10 +8,10 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use std::slice::Items; -//~^ ERROR import `Items` conflicts with type in this module +use std::slice::Iter; +//~^ ERROR import `Iter` conflicts with type in this module -struct Items; +struct Iter; fn main() { } diff --git a/src/test/run-pass/issue-13167.rs b/src/test/run-pass/issue-13167.rs index be3ee0e07831..1282077028ff 100644 --- a/src/test/run-pass/issue-13167.rs +++ b/src/test/run-pass/issue-13167.rs @@ -11,7 +11,7 @@ use std::slice; pub struct PhfMapEntries<'a, T: 'a> { - iter: slice::Items<'a, (&'static str, T)>, + iter: slice::Iter<'a, (&'static str, T)>, } impl<'a, T> Iterator<(&'static str, &'a T)> for PhfMapEntries<'a, T> { From e711e2d89b4ad588d1f7225288b714f0b751cf7d Mon Sep 17 00:00:00 2001 From: "Felix S. Klock II" Date: Wed, 17 Dec 2014 14:42:50 +0100 Subject: [PATCH 20/44] Add `-Z unstable-options` debugging flag, which can then be used to extend the `rustc` command line interface with options that we do not want to commit to making part of the long-term public interface for `rustc`. --- src/librustc/session/config.rs | 164 ++++++++++++++++++++++++--------- src/librustc_driver/lib.rs | 43 +++++++-- 2 files changed, 158 insertions(+), 49 deletions(-) diff --git a/src/librustc/session/config.rs b/src/librustc/session/config.rs index 0652645907bc..fd3819527538 100644 --- a/src/librustc/session/config.rs +++ b/src/librustc/session/config.rs @@ -34,7 +34,6 @@ use std::collections::HashMap; use std::collections::hash_map::Entry::{Occupied, Vacant}; -use getopts::{optopt, optmulti, optflag, optflagopt}; use getopts; use std::cell::{RefCell}; use std::fmt; @@ -278,7 +277,8 @@ macro_rules! debugging_opts { PRINT_REGION_GRAPH, PARSE_ONLY, NO_TRANS, - NO_ANALYSIS + NO_ANALYSIS, + UNSTABLE_OPTIONS ] 0 } @@ -330,7 +330,8 @@ pub fn debugging_opts_map() -> Vec<(&'static str, &'static str, u64)> { ("no-trans", "Run all passes except translation; no output", NO_TRANS), ("no-analysis", "Parse and expand the source, but run no analysis and", NO_TRANS), - ] + ("unstable-options", "Adds unstable command line options to rustc interface", + UNSTABLE_OPTIONS)] } #[deriving(Clone)] @@ -653,80 +654,160 @@ pub fn build_target_config(opts: &Options, sp: &SpanHandler) -> Config { } } +/// Returns the "short" subset of the stable rustc command line options. pub fn short_optgroups() -> Vec { + rustc_short_optgroups().into_iter() + .filter(|g|g.is_stable()) + .map(|g|g.opt_group) + .collect() +} + +/// Returns all of the stable rustc command line options. +pub fn optgroups() -> Vec { + rustc_optgroups().into_iter() + .filter(|g|g.is_stable()) + .map(|g|g.opt_group) + .collect() +} + +#[deriving(Copy, Clone, PartialEq, Eq, Show)] +pub enum OptionStability { Stable, Unstable } + +#[deriving(Clone, PartialEq, Eq)] +pub struct RustcOptGroup { + pub opt_group: getopts::OptGroup, + pub stability: OptionStability, +} + +impl RustcOptGroup { + pub fn is_stable(&self) -> bool { + self.stability == OptionStability::Stable + } + + fn stable(g: getopts::OptGroup) -> RustcOptGroup { + RustcOptGroup { opt_group: g, stability: OptionStability::Stable } + } + + fn unstable(g: getopts::OptGroup) -> RustcOptGroup { + RustcOptGroup { opt_group: g, stability: OptionStability::Unstable } + } +} + +// The `opt` local module holds wrappers around the `getopts` API that +// adds extra rustc-specific metadata to each option; such metadata +// is exposed by . The public +// functions below ending with `_u` are the functions that return +// *unstable* options, i.e. options that are only enabled when the +// user also passes the `-Z unstable-options` debugging flag. +mod opt { + // The `fn opt_u` etc below are written so that we can use them + // in the future; do not warn about them not being used right now. + #![allow(dead_code)] + + use getopts; + use super::RustcOptGroup; + + type R = RustcOptGroup; + type S<'a> = &'a str; + + fn stable(g: getopts::OptGroup) -> R { RustcOptGroup::stable(g) } + fn unstable(g: getopts::OptGroup) -> R { RustcOptGroup::unstable(g) } + + // FIXME (pnkfelix): We default to stable since the current set of + // options is defacto stable. However, it would be good to revise the + // code so that a stable option is the thing that takes extra effort + // to encode. + + pub fn opt(a: S, b: S, c: S, d: S) -> R { stable(getopts::optopt(a, b, c, d)) } + pub fn multi(a: S, b: S, c: S, d: S) -> R { stable(getopts::optmulti(a, b, c, d)) } + pub fn flag(a: S, b: S, c: S) -> R { stable(getopts::optflag(a, b, c)) } + pub fn flagopt(a: S, b: S, c: S, d: S) -> R { stable(getopts::optflagopt(a, b, c, d)) } + + pub fn opt_u(a: S, b: S, c: S, d: S) -> R { unstable(getopts::optopt(a, b, c, d)) } + pub fn multi_u(a: S, b: S, c: S, d: S) -> R { unstable(getopts::optmulti(a, b, c, d)) } + pub fn flag_u(a: S, b: S, c: S) -> R { unstable(getopts::optflag(a, b, c)) } + pub fn flagopt_u(a: S, b: S, c: S, d: S) -> R { unstable(getopts::optflagopt(a, b, c, d)) } +} + +/// Returns the "short" subset of the rustc command line options, +/// including metadata for each option, such as whether the option is +/// part of the stable long-term interface for rustc. +pub fn rustc_short_optgroups() -> Vec { vec![ - optflag("h", "help", "Display this message"), - optmulti("", "cfg", "Configure the compilation environment", "SPEC"), - optmulti("L", "", "Add a directory to the library search path", "PATH"), - optmulti("l", "", "Link the generated crate(s) to the specified native + opt::flag("h", "help", "Display this message"), + opt::multi("", "cfg", "Configure the compilation environment", "SPEC"), + opt::multi("L", "", "Add a directory to the library search path", "PATH"), + opt::multi("l", "", "Link the generated crate(s) to the specified native library NAME. The optional KIND can be one of, static, dylib, or framework. If omitted, dylib is assumed.", "NAME[:KIND]"), - optmulti("", "crate-type", "Comma separated list of types of crates + opt::multi("", "crate-type", "Comma separated list of types of crates for the compiler to emit", "[bin|lib|rlib|dylib|staticlib|dep-info]"), - optopt("", "crate-name", "Specify the name of the crate being built", + opt::opt("", "crate-name", "Specify the name of the crate being built", "NAME"), - optmulti("", "emit", "Comma separated list of types of output for \ + opt::multi("", "emit", "Comma separated list of types of output for \ the compiler to emit", "[asm|llvm-bc|llvm-ir|obj|link]"), - optmulti("", "print", "Comma separated list of compiler information to \ + opt::multi("", "print", "Comma separated list of compiler information to \ print on stdout", "[crate-name|output-file-names|sysroot]"), - optflag("g", "", "Equivalent to --debuginfo=2"), - optflag("O", "", "Equivalent to --opt-level=2"), - optopt("o", "", "Write output to ", "FILENAME"), - optopt("", "out-dir", "Write output to compiler-chosen filename \ + opt::flag("g", "", "Equivalent to --debuginfo=2"), + opt::flag("O", "", "Equivalent to --opt-level=2"), + opt::opt("o", "", "Write output to ", "FILENAME"), + opt::opt("", "out-dir", "Write output to compiler-chosen filename \ in

", "DIR"), - optopt("", "explain", "Provide a detailed explanation of an error \ + opt::opt("", "explain", "Provide a detailed explanation of an error \ message", "OPT"), - optflag("", "test", "Build a test harness"), - optopt("", "target", "Target triple cpu-manufacturer-kernel[-os] \ + opt::flag("", "test", "Build a test harness"), + opt::opt("", "target", "Target triple cpu-manufacturer-kernel[-os] \ to compile for (see chapter 3.4 of \ http://www.sourceware.org/autobook/ for details)", "TRIPLE"), - optmulti("W", "warn", "Set lint warnings", "OPT"), - optmulti("A", "allow", "Set lint allowed", "OPT"), - optmulti("D", "deny", "Set lint denied", "OPT"), - optmulti("F", "forbid", "Set lint forbidden", "OPT"), - optmulti("C", "codegen", "Set a codegen option", "OPT[=VALUE]"), - optflag("V", "version", "Print version info and exit"), - optflag("v", "verbose", "Use verbose output"), + opt::multi("W", "warn", "Set lint warnings", "OPT"), + opt::multi("A", "allow", "Set lint allowed", "OPT"), + opt::multi("D", "deny", "Set lint denied", "OPT"), + opt::multi("F", "forbid", "Set lint forbidden", "OPT"), + opt::multi("C", "codegen", "Set a codegen option", "OPT[=VALUE]"), + opt::flag("V", "version", "Print version info and exit"), + opt::flag("v", "verbose", "Use verbose output"), ] } -// rustc command line options -pub fn optgroups() -> Vec { - let mut opts = short_optgroups(); +/// Returns all rustc command line options, including metadata for +/// each option, such as whether the option is part of the stable +/// long-term interface for rustc. +pub fn rustc_optgroups() -> Vec { + let mut opts = rustc_short_optgroups(); opts.push_all(&[ - optmulti("", "extern", "Specify where an external rust library is \ + opt::multi("", "extern", "Specify where an external rust library is \ located", "NAME=PATH"), - optopt("", "opt-level", "Optimize with possible levels 0-3", "LEVEL"), - optopt("", "sysroot", "Override the system root", "PATH"), - optmulti("Z", "", "Set internal debugging options", "FLAG"), - optopt("", "color", "Configure coloring of output: + opt::opt("", "opt-level", "Optimize with possible levels 0-3", "LEVEL"), + opt::opt("", "sysroot", "Override the system root", "PATH"), + opt::multi("Z", "", "Set internal debugging options", "FLAG"), + opt::opt("", "color", "Configure coloring of output: auto = colorize, if output goes to a tty (default); always = always colorize output; never = never colorize output", "auto|always|never"), // DEPRECATED - optflag("", "print-crate-name", "Output the crate name and exit"), - optflag("", "print-file-name", "Output the file(s) that would be \ + opt::flag("", "print-crate-name", "Output the crate name and exit"), + opt::flag("", "print-file-name", "Output the file(s) that would be \ written if compilation \ continued and exit"), - optopt("", "debuginfo", "Emit DWARF debug info to the objects created: + opt::opt("", "debuginfo", "Emit DWARF debug info to the objects created: 0 = no debug info, 1 = line-tables only (for stacktraces and breakpoints), 2 = full debug info with variable and type information \ (same as -g)", "LEVEL"), - optflag("", "no-trans", "Run all passes except translation; no output"), - optflag("", "no-analysis", "Parse and expand the source, but run no \ + opt::flag("", "no-trans", "Run all passes except translation; no output"), + opt::flag("", "no-analysis", "Parse and expand the source, but run no \ analysis and produce no output"), - optflag("", "parse-only", "Parse only; do not compile, assemble, \ + opt::flag("", "parse-only", "Parse only; do not compile, assemble, \ or link"), - optflagopt("", "pretty", + opt::flagopt("", "pretty", "Pretty-print the input instead of compiling; valid types are: `normal` (un-annotated source), `expanded` (crates expanded), @@ -734,14 +815,13 @@ pub fn optgroups() -> Vec { `expanded,identified` (fully parenthesized, AST nodes with IDs), or `flowgraph=` (graphviz formatted flowgraph for node)", "TYPE"), - optflagopt("", "dep-info", + opt::flagopt("", "dep-info", "Output dependency info to after compiling, \ in a format suitable for use by Makefiles", "FILENAME"), ]); opts } - // Convert strings provided as --cfg [cfgspec] into a crate_cfg pub fn parse_cfgspecs(cfgspecs: Vec ) -> ast::CrateConfig { cfgspecs.into_iter().map(|s| { diff --git a/src/librustc_driver/lib.rs b/src/librustc_driver/lib.rs index 6944c733456f..7a0a8fd50d44 100644 --- a/src/librustc_driver/lib.rs +++ b/src/librustc_driver/lib.rs @@ -196,12 +196,16 @@ fn unw(x: Option<&str>) -> &str { x.unwrap_or("unknown") } } } -fn usage(verbose: bool) { +fn usage(verbose: bool, include_unstable_options: bool) { let groups = if verbose { - config::optgroups() + config::rustc_optgroups() } else { - config::short_optgroups() + config::rustc_short_optgroups() }; + let groups : Vec<_> = groups.into_iter() + .filter(|x| include_unstable_options || x.is_stable()) + .map(|x|x.opt_group) + .collect(); let message = format!("Usage: rustc [OPTIONS] INPUT"); let extra_help = if verbose { "" @@ -362,20 +366,45 @@ pub fn handle_options(mut args: Vec) -> Option { let _binary = args.remove(0).unwrap(); if args.is_empty() { - usage(false); + // user did not write `-v` nor `-Z unstable-options`, so do not + // include that extra information. + usage(false, false); return None; } let matches = match getopts::getopts(args.as_slice(), config::optgroups().as_slice()) { Ok(m) => m, - Err(f) => { - early_error(f.to_string().as_slice()); + Err(f_stable_attempt) => { + // redo option parsing, including unstable options this time, + // in anticipation that the mishandled option was one of the + // unstable ones. + let all_groups : Vec + = config::rustc_optgroups().into_iter().map(|x|x.opt_group).collect(); + match getopts::getopts(args.as_slice(), all_groups.as_slice()) { + Ok(m_unstable) => { + let r = m_unstable.opt_strs("Z"); + let include_unstable_options = r.iter().any(|x| *x == "unstable-options"); + if include_unstable_options { + m_unstable + } else { + early_error(f_stable_attempt.to_string().as_slice()); + } + } + Err(_) => { + // ignore the error from the unstable attempt; just + // pass the error we got from the first try. + early_error(f_stable_attempt.to_string().as_slice()); + } + } } }; + let r = matches.opt_strs("Z"); + let include_unstable_options = r.iter().any(|x| *x == "unstable-options"); + if matches.opt_present("h") || matches.opt_present("help") { - usage(matches.opt_present("verbose")); + usage(matches.opt_present("verbose"), include_unstable_options); return None; } From 41def27bda80eb5e1b3bfa75d34fd4f9e31e0988 Mon Sep 17 00:00:00 2001 From: "Felix S. Klock II" Date: Wed, 17 Dec 2014 13:37:26 +0100 Subject: [PATCH 21/44] Add `--pretty everybody_loops` option. This prints out a transformed version of the input source code where every function body is replaced with `loop { }`. All such bodies are (1.) trivial and (2.) guaranteed to pass the type-checker in a valid compiler; therefore they make very nice input to start with when narrowing down a bug exposed by a large test input (such as librustc itself). --- src/librustc_driver/pretty.rs | 75 ++++++++++++++++++++++++++++++++++- 1 file changed, 74 insertions(+), 1 deletion(-) diff --git a/src/librustc_driver/pretty.rs b/src/librustc_driver/pretty.rs index 2eb9d2c67a7c..266907e0bcdb 100644 --- a/src/librustc_driver/pretty.rs +++ b/src/librustc_driver/pretty.rs @@ -30,7 +30,10 @@ use syntax::ast; use syntax::ast_map::{mod, blocks, NodePrinter}; +use syntax::codemap; +use syntax::fold::{mod, Folder}; use syntax::print::{pp, pprust}; +use syntax::ptr::P; use graphviz as dot; @@ -42,6 +45,7 @@ #[deriving(Copy, PartialEq, Show)] pub enum PpSourceMode { PpmNormal, + PpmEveryBodyLoops, PpmExpanded, PpmTyped, PpmIdentified, @@ -61,6 +65,7 @@ pub fn parse_pretty(sess: &Session, name: &str) -> (PpMode, Option PpmSource(PpmNormal), + "everybody_loops" => PpmSource(PpmEveryBodyLoops), "expanded" => PpmSource(PpmExpanded), "typed" => PpmSource(PpmTyped), "expanded,identified" => PpmSource(PpmExpandedIdentified), @@ -105,7 +110,7 @@ fn call_with_pp_support<'tcx, A, B, F>(&self, F: FnOnce(&PrinterSupport, B) -> A, { match *self { - PpmNormal | PpmExpanded => { + PpmNormal | PpmEveryBodyLoops | PpmExpanded => { let annotation = NoAnn { sess: sess, ast_map: ast_map }; f(&annotation, payload) } @@ -384,6 +389,7 @@ fn to_one_node_id(self, user_option: &str, sess: &Session, map: &ast_map::Map) - fn needs_ast_map(ppm: &PpMode, opt_uii: &Option) -> bool { match *ppm { PpmSource(PpmNormal) | + PpmSource(PpmEveryBodyLoops) | PpmSource(PpmIdentified) => opt_uii.is_some(), PpmSource(PpmExpanded) | @@ -397,6 +403,7 @@ fn needs_ast_map(ppm: &PpMode, opt_uii: &Option) -> bool { fn needs_expansion(ppm: &PpMode) -> bool { match *ppm { PpmSource(PpmNormal) | + PpmSource(PpmEveryBodyLoops) | PpmSource(PpmIdentified) => false, PpmSource(PpmExpanded) | @@ -407,6 +414,64 @@ fn needs_expansion(ppm: &PpMode) -> bool { } } +struct ReplaceBodyWithLoop { + within_static_or_const: bool, +} + +impl ReplaceBodyWithLoop { + fn new() -> ReplaceBodyWithLoop { + ReplaceBodyWithLoop { within_static_or_const: false } + } +} + +impl fold::Folder for ReplaceBodyWithLoop { + fn fold_item_underscore(&mut self, i: ast::Item_) -> ast::Item_ { + match i { + ast::ItemStatic(..) | ast::ItemConst(..) => { + self.within_static_or_const = true; + let ret = fold::noop_fold_item_underscore(i, self); + self.within_static_or_const = false; + return ret; + } + _ => { + fold::noop_fold_item_underscore(i, self) + } + } + } + + + fn fold_block(&mut self, b: P) -> P { + fn expr_to_block(rules: ast::BlockCheckMode, + e: Option>) -> P { + P(ast::Block { + expr: e, + view_items: vec![], stmts: vec![], rules: rules, + id: ast::DUMMY_NODE_ID, span: codemap::DUMMY_SP, + }) + } + + if !self.within_static_or_const { + + let empty_block = expr_to_block(ast::DefaultBlock, None); + let loop_expr = P(ast::Expr { + node: ast::ExprLoop(empty_block, None), + id: ast::DUMMY_NODE_ID, span: codemap::DUMMY_SP + }); + + expr_to_block(b.rules, Some(loop_expr)) + + } else { + fold::noop_fold_block(b, self) + } + } + + // in general the pretty printer processes unexpanded code, so + // we override the default `fold_mac` method which panics. + fn fold_mac(&mut self, _macro: ast::Mac) -> ast::Mac { + fold::noop_fold_mac(_macro, self) + } +} + pub fn pretty_print_input(sess: Session, cfg: ast::CrateConfig, input: &Input, @@ -414,6 +479,14 @@ pub fn pretty_print_input(sess: Session, opt_uii: Option, ofile: Option) { let krate = driver::phase_1_parse_input(&sess, cfg, input); + + let krate = if let PpmSource(PpmEveryBodyLoops) = ppm { + let mut fold = ReplaceBodyWithLoop::new(); + fold.fold_crate(krate) + } else { + krate + }; + let id = link::find_crate_name(Some(&sess), krate.attrs.as_slice(), input); let is_expanded = needs_expansion(&ppm); From bf2f84bfe367d5a1f4b8f092fb22f535e6e95a40 Mon Sep 17 00:00:00 2001 From: "Felix S. Klock II" Date: Wed, 17 Dec 2014 15:13:38 +0100 Subject: [PATCH 22/44] Add an unstable `--xpretty _` option to `rustc`. Moved `flowgraph` and `everybody_loops` options to `--xpretty`. --- src/librustc/session/config.rs | 11 +++++++--- src/librustc_driver/lib.rs | 13 +++++++++++- src/librustc_driver/pretty.rs | 37 +++++++++++++++++++++------------- 3 files changed, 43 insertions(+), 18 deletions(-) diff --git a/src/librustc/session/config.rs b/src/librustc/session/config.rs index fd3819527538..e3bd2648588d 100644 --- a/src/librustc/session/config.rs +++ b/src/librustc/session/config.rs @@ -811,10 +811,15 @@ pub fn rustc_optgroups() -> Vec { "Pretty-print the input instead of compiling; valid types are: `normal` (un-annotated source), `expanded` (crates expanded), - `typed` (crates expanded, with type annotations), - `expanded,identified` (fully parenthesized, AST nodes with IDs), or - `flowgraph=` (graphviz formatted flowgraph for node)", + `typed` (crates expanded, with type annotations), or + `expanded,identified` (fully parenthesized, AST nodes with IDs).", "TYPE"), + opt::flagopt_u("", "xpretty", + "Pretty-print the input instead of compiling, unstable variants; + valid types are any of the types for `--pretty`, as well as: + `flowgraph=` (graphviz formatted flowgraph for node), or + `everybody_loops` (all function bodies replaced with `loop {}`).", + "TYPE"), opt::flagopt("", "dep-info", "Output dependency info to after compiling, \ in a format suitable for use by Makefiles", "FILENAME"), diff --git a/src/librustc_driver/lib.rs b/src/librustc_driver/lib.rs index 7a0a8fd50d44..64f4d8bdc783 100644 --- a/src/librustc_driver/lib.rs +++ b/src/librustc_driver/lib.rs @@ -138,8 +138,19 @@ fn run_compiler(args: &[String]) { } let pretty = matches.opt_default("pretty", "normal").map(|a| { - pretty::parse_pretty(&sess, a.as_slice()) + // stable pretty-print variants only + pretty::parse_pretty(&sess, a.as_slice(), false) }); + let pretty = if pretty.is_none() && + sess.debugging_opt(config::UNSTABLE_OPTIONS) { + matches.opt_str("xpretty").map(|a| { + // extended with unstable pretty-print variants + pretty::parse_pretty(&sess, a.as_slice(), true) + }) + } else { + pretty + }; + match pretty.into_iter().next() { Some((ppm, opt_uii)) => { pretty::pretty_print_input(sess, cfg, &input, ppm, opt_uii, ofile); diff --git a/src/librustc_driver/pretty.rs b/src/librustc_driver/pretty.rs index 266907e0bcdb..3b6ad75243e2 100644 --- a/src/librustc_driver/pretty.rs +++ b/src/librustc_driver/pretty.rs @@ -59,24 +59,33 @@ pub enum PpMode { PpmFlowGraph, } -pub fn parse_pretty(sess: &Session, name: &str) -> (PpMode, Option) { +pub fn parse_pretty(sess: &Session, + name: &str, + extended: bool) -> (PpMode, Option) { let mut split = name.splitn(1, '='); let first = split.next().unwrap(); let opt_second = split.next(); - let first = match first { - "normal" => PpmSource(PpmNormal), - "everybody_loops" => PpmSource(PpmEveryBodyLoops), - "expanded" => PpmSource(PpmExpanded), - "typed" => PpmSource(PpmTyped), - "expanded,identified" => PpmSource(PpmExpandedIdentified), - "expanded,hygiene" => PpmSource(PpmExpandedHygiene), - "identified" => PpmSource(PpmIdentified), - "flowgraph" => PpmFlowGraph, + let first = match (first, extended) { + ("normal", _) => PpmSource(PpmNormal), + ("everybody_loops", true) => PpmSource(PpmEveryBodyLoops), + ("expanded", _) => PpmSource(PpmExpanded), + ("typed", _) => PpmSource(PpmTyped), + ("expanded,identified", _) => PpmSource(PpmExpandedIdentified), + ("expanded,hygiene", _) => PpmSource(PpmExpandedHygiene), + ("identified", _) => PpmSource(PpmIdentified), + ("flowgraph", true) => PpmFlowGraph, _ => { - sess.fatal(format!( - "argument to `pretty` must be one of `normal`, \ - `expanded`, `flowgraph=`, `typed`, `identified`, \ - or `expanded,identified`; got {}", name).as_slice()); + if extended { + sess.fatal(format!( + "argument to `xpretty` must be one of `normal`, \ + `expanded`, `flowgraph=`, `typed`, `identified`, \ + `expanded,identified`, or `everybody_loops`; got {}", name).as_slice()); + } else { + sess.fatal(format!( + "argument to `pretty` must be one of `normal`, \ + `expanded`, `typed`, `identified`, \ + or `expanded,identified`; got {}", name).as_slice()); + } } }; let opt_second = opt_second.and_then::(from_str); From 8c8cb997a592f7348958e58c98117be03764d8b0 Mon Sep 17 00:00:00 2001 From: Niko Matsakis Date: Wed, 26 Nov 2014 06:36:52 -0500 Subject: [PATCH 23/44] Introduce a tcx() helper method to cleanup this mess. --- src/librustc/middle/infer/coercion.rs | 56 ++++++++++++++------------- 1 file changed, 29 insertions(+), 27 deletions(-) diff --git a/src/librustc/middle/infer/coercion.rs b/src/librustc/middle/infer/coercion.rs index 64bfd1388026..ca9ac10341c2 100644 --- a/src/librustc/middle/infer/coercion.rs +++ b/src/librustc/middle/infer/coercion.rs @@ -84,10 +84,14 @@ pub fn get_ref<'a>(&'a self) -> &'a CombineFields<'f, 'tcx> { let Coerce(ref v) = *self; v } + fn tcx(&self) -> &ty::ctxt<'tcx> { + self.get_ref().infcx.tcx + } + pub fn tys(&self, a: Ty<'tcx>, b: Ty<'tcx>) -> CoerceResult<'tcx> { debug!("Coerce.tys({} => {})", - a.repr(self.get_ref().infcx.tcx), - b.repr(self.get_ref().infcx.tcx)); + a.repr(self.tcx()), + b.repr(self.tcx())); // Consider coercing the subtype to a DST let unsize = self.unpack_actual_value(a, |a| { @@ -170,13 +174,11 @@ pub fn tys(&self, a: Ty<'tcx>, b: Ty<'tcx>) -> CoerceResult<'tcx> { self.unpack_actual_value(a, |a| { match a.sty { - ty::ty_bare_fn(ref a_f) => { - // Bare functions are coercible to any closure type. - // - // FIXME(#3320) this should go away and be - // replaced with proper inference, got a patch - // underway - ndm - self.coerce_from_bare_fn(a, a_f, b) + ty::ty_bare_fn(Some(a_def_id), ref a_f) => { + // Function items are coercible to any closure + // type; function pointers are not (that would + // require double indirection). + self.coerce_from_fn_item(a, a_def_id, a_f, b) } _ => { // Otherwise, just use subtyping rules. @@ -206,8 +208,8 @@ pub fn coerce_borrowed_pointer(&self, mutbl_b: ast::Mutability) -> CoerceResult<'tcx> { debug!("coerce_borrowed_pointer(a={}, b={})", - a.repr(self.get_ref().infcx.tcx), - b.repr(self.get_ref().infcx.tcx)); + a.repr(self.tcx()), + b.repr(self.tcx())); // If we have a parameter of type `&M T_a` and the value // provided is `expr`, we will be adding an implicit borrow, @@ -227,7 +229,7 @@ pub fn coerce_borrowed_pointer(&self, } }; - let a_borrowed = ty::mk_rptr(self.get_ref().infcx.tcx, + let a_borrowed = ty::mk_rptr(self.tcx(), r_borrow, mt {ty: inner_ty, mutbl: mutbl_b}); try!(sub.tys(a_borrowed, b)); @@ -247,8 +249,8 @@ fn coerce_unsized(&self, b: Ty<'tcx>) -> CoerceResult<'tcx> { debug!("coerce_unsized(a={}, b={})", - a.repr(self.get_ref().infcx.tcx), - b.repr(self.get_ref().infcx.tcx)); + a.repr(self.tcx()), + b.repr(self.tcx())); // Note, we want to avoid unnecessary unsizing. We don't want to coerce to // a DST unless we have to. This currently comes out in the wash since @@ -268,7 +270,7 @@ fn coerce_unsized(&self, let coercion = Coercion(self.get_ref().trace.clone()); let r_borrow = self.get_ref().infcx.next_region_var(coercion); - let ty = ty::mk_rptr(self.get_ref().infcx.tcx, + let ty = ty::mk_rptr(self.tcx(), r_borrow, ty::mt{ty: ty, mutbl: mt_b.mutbl}); try!(self.get_ref().infcx.try(|_| sub.tys(ty, b))); @@ -292,7 +294,7 @@ fn coerce_unsized(&self, return Err(ty::terr_mutability); } - let ty = ty::mk_ptr(self.get_ref().infcx.tcx, + let ty = ty::mk_ptr(self.tcx(), ty::mt{ty: ty, mutbl: mt_b.mutbl}); try!(self.get_ref().infcx.try(|_| sub.tys(ty, b))); debug!("Success, coerced with AutoDerefRef(1, \ @@ -311,7 +313,7 @@ fn coerce_unsized(&self, self.unpack_actual_value(t_a, |a| { match self.unsize_ty(t_a, a, t_b) { Some((ty, kind)) => { - let ty = ty::mk_uniq(self.get_ref().infcx.tcx, ty); + let ty = ty::mk_uniq(self.tcx(), ty); try!(self.get_ref().infcx.try(|_| sub.tys(ty, b))); debug!("Success, coerced with AutoDerefRef(1, \ AutoUnsizeUniq({}))", kind); @@ -336,9 +338,9 @@ fn unsize_ty(&self, a: Ty<'tcx>, ty_b: Ty<'tcx>) -> Option<(Ty<'tcx>, ty::UnsizeKind<'tcx>)> { - debug!("unsize_ty(a={}, ty_b={})", a, ty_b.repr(self.get_ref().infcx.tcx)); + debug!("unsize_ty(a={}, ty_b={})", a, ty_b.repr(self.tcx())); - let tcx = self.get_ref().infcx.tcx; + let tcx = self.tcx(); self.unpack_actual_value(ty_b, |b| match (&a.sty, &b.sty) { @@ -412,7 +414,7 @@ fn coerce_borrowed_object(&self, b: Ty<'tcx>, b_mutbl: ast::Mutability) -> CoerceResult<'tcx> { - let tcx = self.get_ref().infcx.tcx; + let tcx = self.tcx(); debug!("coerce_borrowed_object(a={}, b={}, b_mutbl={})", a.repr(tcx), @@ -431,7 +433,7 @@ fn coerce_unsafe_object(&self, b: Ty<'tcx>, b_mutbl: ast::Mutability) -> CoerceResult<'tcx> { - let tcx = self.get_ref().infcx.tcx; + let tcx = self.tcx(); debug!("coerce_unsafe_object(a={}, b={}, b_mutbl={})", a.repr(tcx), @@ -451,7 +453,7 @@ fn coerce_object(&self, F: FnOnce(Ty<'tcx>) -> Ty<'tcx>, G: FnOnce() -> ty::AutoRef<'tcx>, { - let tcx = self.get_ref().infcx.tcx; + let tcx = self.tcx(); match a.sty { ty::ty_rptr(_, ty::mt{ty, mutbl}) => match ty.sty { @@ -480,8 +482,8 @@ pub fn coerce_borrowed_fn(&self, b: Ty<'tcx>) -> CoerceResult<'tcx> { debug!("coerce_borrowed_fn(a={}, b={})", - a.repr(self.get_ref().infcx.tcx), - b.repr(self.get_ref().infcx.tcx)); + a.repr(self.tcx()), + b.repr(self.tcx())); match a.sty { ty::ty_bare_fn(ref f) => { @@ -528,8 +530,8 @@ pub fn coerce_unsafe_ptr(&self, mutbl_b: ast::Mutability) -> CoerceResult<'tcx> { debug!("coerce_unsafe_ptr(a={}, b={})", - a.repr(self.get_ref().infcx.tcx), - b.repr(self.get_ref().infcx.tcx)); + a.repr(self.tcx()), + b.repr(self.tcx())); let mt_a = match a.sty { ty::ty_rptr(_, mt) | ty::ty_ptr(mt) => mt, @@ -539,7 +541,7 @@ pub fn coerce_unsafe_ptr(&self, }; // Check that the types which they point at are compatible. - let a_unsafe = ty::mk_ptr(self.get_ref().infcx.tcx, ty::mt{ mutbl: mutbl_b, ty: mt_a.ty }); + let a_unsafe = ty::mk_ptr(self.tcx(), ty::mt{ mutbl: mutbl_b, ty: mt_a.ty }); try!(self.subtype(a_unsafe, b)); if !can_coerce_mutbls(mt_a.mutbl, mutbl_b) { return Err(ty::terr_mutability); From 22050e3ed44d9b4d79edced506b470a425e0d302 Mon Sep 17 00:00:00 2001 From: Florian Wilkens Date: Sat, 20 Dec 2014 15:28:20 +0100 Subject: [PATCH 24/44] Added missing renames: libcollections: AbsEntries -> AbsIter, Entries -> Iter, MoveEntries -> IntoIter, MutEntries -> IterMut DifferenceItems -> Difference, SymDifferenceItems -> SymmetricDifference, IntersectionItems -> Intersection, UnionItems -> Union libstd/hash/{table, map}: Entries -> Iter, MoveItems -> IntoIter, MutEntries -> IterMut Also a [breaking-change]. --- src/libcollections/binary_heap.rs | 3 +- src/libcollections/btree/map.rs | 58 ++++++++++++++-------------- src/libcollections/btree/set.rs | 36 ++++++++--------- src/libcollections/vec_map.rs | 24 ++++++------ src/libcore/slice.rs | 2 +- src/libstd/collections/hash/map.rs | 20 +++++----- src/libstd/collections/hash/set.rs | 4 +- src/libstd/collections/hash/table.rs | 16 ++++---- 8 files changed, 82 insertions(+), 81 deletions(-) diff --git a/src/libcollections/binary_heap.rs b/src/libcollections/binary_heap.rs index c9d1d9d13fb2..5e1025ebfb2f 100644 --- a/src/libcollections/binary_heap.rs +++ b/src/libcollections/binary_heap.rs @@ -240,7 +240,8 @@ pub fn from_vec(xs: Vec) -> BinaryHeap { /// ``` #[unstable = "matches collection reform specification, waiting for dust to settle"] pub fn iter(&self) -> Iter { - Iter { iter: self.data.iter() } } + Iter { iter: self.data.iter() } + } /// Creates a consuming iterator, that is, one that moves each value out of /// the binary heap in arbitrary order. The binary heap cannot be used diff --git a/src/libcollections/btree/map.rs b/src/libcollections/btree/map.rs index 01096c1fd4e9..24d991cda729 100644 --- a/src/libcollections/btree/map.rs +++ b/src/libcollections/btree/map.rs @@ -88,7 +88,7 @@ pub struct BTreeMap { } /// An abstract base over-which all other BTree iterators are built. -struct AbsEntries { +struct AbsIter { lca: T, left: RingBuf, right: RingBuf, @@ -96,28 +96,28 @@ struct AbsEntries { } /// An iterator over a BTreeMap's entries. -pub struct Entries<'a, K: 'a, V: 'a> { - inner: AbsEntries> +pub struct Iter<'a, K: 'a, V: 'a> { + inner: AbsIter> } /// A mutable iterator over a BTreeMap's entries. -pub struct MutEntries<'a, K: 'a, V: 'a> { - inner: AbsEntries> +pub struct IterMut<'a, K: 'a, V: 'a> { + inner: AbsIter> } /// An owning iterator over a BTreeMap's entries. -pub struct MoveEntries { - inner: AbsEntries> +pub struct IntoIter { + inner: AbsIter> } /// An iterator over a BTreeMap's keys. pub struct Keys<'a, K: 'a, V: 'a> { - inner: Map<(&'a K, &'a V), &'a K, Entries<'a, K, V>, fn((&'a K, &'a V)) -> &'a K> + inner: Map<(&'a K, &'a V), &'a K, Iter<'a, K, V>, fn((&'a K, &'a V)) -> &'a K> } /// An iterator over a BTreeMap's values. pub struct Values<'a, K: 'a, V: 'a> { - inner: Map<(&'a K, &'a V), &'a V, Entries<'a, K, V>, fn((&'a K, &'a V)) -> &'a V> + inner: Map<(&'a K, &'a V), &'a V, Iter<'a, K, V>, fn((&'a K, &'a V)) -> &'a V> } /// A view into a single entry in a map, which may either be vacant or occupied. @@ -929,7 +929,7 @@ enum StackOp { } impl + DoubleEndedIterator>> - Iterator<(K, V)> for AbsEntries { + Iterator<(K, V)> for AbsIter { // This function is pretty long, but only because there's a lot of cases to consider. // Our iterator represents two search paths, left and right, to the smallest and largest // elements we have yet to yield. lca represents the least common ancestor of these two paths, @@ -995,7 +995,7 @@ fn size_hint(&self) -> (uint, Option) { } impl + DoubleEndedIterator>> - DoubleEndedIterator<(K, V)> for AbsEntries { + DoubleEndedIterator<(K, V)> for AbsIter { // next_back is totally symmetric to next fn next_back(&mut self) -> Option<(K, V)> { loop { @@ -1032,34 +1032,34 @@ fn next_back(&mut self) -> Option<(K, V)> { } } -impl<'a, K, V> Iterator<(&'a K, &'a V)> for Entries<'a, K, V> { +impl<'a, K, V> Iterator<(&'a K, &'a V)> for Iter<'a, K, V> { fn next(&mut self) -> Option<(&'a K, &'a V)> { self.inner.next() } fn size_hint(&self) -> (uint, Option) { self.inner.size_hint() } } -impl<'a, K, V> DoubleEndedIterator<(&'a K, &'a V)> for Entries<'a, K, V> { +impl<'a, K, V> DoubleEndedIterator<(&'a K, &'a V)> for Iter<'a, K, V> { fn next_back(&mut self) -> Option<(&'a K, &'a V)> { self.inner.next_back() } } -impl<'a, K, V> ExactSizeIterator<(&'a K, &'a V)> for Entries<'a, K, V> {} +impl<'a, K, V> ExactSizeIterator<(&'a K, &'a V)> for Iter<'a, K, V> {} -impl<'a, K, V> Iterator<(&'a K, &'a mut V)> for MutEntries<'a, K, V> { +impl<'a, K, V> Iterator<(&'a K, &'a mut V)> for IterMut<'a, K, V> { fn next(&mut self) -> Option<(&'a K, &'a mut V)> { self.inner.next() } fn size_hint(&self) -> (uint, Option) { self.inner.size_hint() } } -impl<'a, K, V> DoubleEndedIterator<(&'a K, &'a mut V)> for MutEntries<'a, K, V> { +impl<'a, K, V> DoubleEndedIterator<(&'a K, &'a mut V)> for IterMut<'a, K, V> { fn next_back(&mut self) -> Option<(&'a K, &'a mut V)> { self.inner.next_back() } } -impl<'a, K, V> ExactSizeIterator<(&'a K, &'a mut V)> for MutEntries<'a, K, V> {} +impl<'a, K, V> ExactSizeIterator<(&'a K, &'a mut V)> for IterMut<'a, K, V> {} -impl Iterator<(K, V)> for MoveEntries { +impl Iterator<(K, V)> for IntoIter { fn next(&mut self) -> Option<(K, V)> { self.inner.next() } fn size_hint(&self) -> (uint, Option) { self.inner.size_hint() } } -impl DoubleEndedIterator<(K, V)> for MoveEntries { +impl DoubleEndedIterator<(K, V)> for IntoIter { fn next_back(&mut self) -> Option<(K, V)> { self.inner.next_back() } } -impl ExactSizeIterator<(K, V)> for MoveEntries {} +impl ExactSizeIterator<(K, V)> for IntoIter {} impl<'a, K, V> Iterator<&'a K> for Keys<'a, K, V> { @@ -1140,10 +1140,10 @@ impl BTreeMap { /// assert_eq!((*first_key, *first_value), (1u, "a")); /// ``` #[unstable = "matches collection reform specification, waiting for dust to settle"] - pub fn iter<'a>(&'a self) -> Entries<'a, K, V> { + pub fn iter<'a>(&'a self) -> Iter<'a, K, V> { let len = self.len(); - Entries { - inner: AbsEntries { + Iter { + inner: AbsIter { lca: Traverse::traverse(&self.root), left: RingBuf::new(), right: RingBuf::new(), @@ -1172,10 +1172,10 @@ pub fn iter<'a>(&'a self) -> Entries<'a, K, V> { /// } /// ``` #[unstable = "matches collection reform specification, waiting for dust to settle"] - pub fn iter_mut<'a>(&'a mut self) -> MutEntries<'a, K, V> { + pub fn iter_mut<'a>(&'a mut self) -> IterMut<'a, K, V> { let len = self.len(); - MutEntries { - inner: AbsEntries { + IterMut { + inner: AbsIter { lca: Traverse::traverse(&mut self.root), left: RingBuf::new(), right: RingBuf::new(), @@ -1201,10 +1201,10 @@ pub fn iter_mut<'a>(&'a mut self) -> MutEntries<'a, K, V> { /// } /// ``` #[unstable = "matches collection reform specification, waiting for dust to settle"] - pub fn into_iter(self) -> MoveEntries { + pub fn into_iter(self) -> IntoIter { let len = self.len(); - MoveEntries { - inner: AbsEntries { + IntoIter { + inner: AbsIter { lca: Traverse::traverse(self.root), left: RingBuf::new(), right: RingBuf::new(), diff --git a/src/libcollections/btree/set.rs b/src/libcollections/btree/set.rs index 26e82994f567..3b403d45d82e 100644 --- a/src/libcollections/btree/set.rs +++ b/src/libcollections/btree/set.rs @@ -13,7 +13,7 @@ use core::prelude::*; -use btree_map::{BTreeMap, Keys, MoveEntries}; +use btree_map::{BTreeMap, Keys}; use std::hash::Hash; use core::borrow::BorrowFrom; use core::default::Default; @@ -39,29 +39,29 @@ pub struct Iter<'a, T: 'a> { /// An owning iterator over a BTreeSet's items. pub struct IntoIter { - iter: Map<(T, ()), T, MoveEntries, fn((T, ())) -> T> + iter: Map<(T, ()), T, ::btree_map::IntoIter, fn((T, ())) -> T> } /// A lazy iterator producing elements in the set difference (in-order). -pub struct DifferenceItems<'a, T:'a> { +pub struct Difference<'a, T:'a> { a: Peekable<&'a T, Iter<'a, T>>, b: Peekable<&'a T, Iter<'a, T>>, } /// A lazy iterator producing elements in the set symmetric difference (in-order). -pub struct SymDifferenceItems<'a, T:'a> { +pub struct SymmetricDifference<'a, T:'a> { a: Peekable<&'a T, Iter<'a, T>>, b: Peekable<&'a T, Iter<'a, T>>, } /// A lazy iterator producing elements in the set intersection (in-order). -pub struct IntersectionItems<'a, T:'a> { +pub struct Intersection<'a, T:'a> { a: Peekable<&'a T, Iter<'a, T>>, b: Peekable<&'a T, Iter<'a, T>>, } /// A lazy iterator producing elements in the set union (in-order). -pub struct UnionItems<'a, T:'a> { +pub struct Union<'a, T:'a> { a: Peekable<&'a T, Iter<'a, T>>, b: Peekable<&'a T, Iter<'a, T>>, } @@ -151,8 +151,8 @@ impl BTreeSet { /// assert_eq!(diff, vec![1u]); /// ``` #[unstable = "matches collection reform specification, waiting for dust to settle"] - pub fn difference<'a>(&'a self, other: &'a BTreeSet) -> DifferenceItems<'a, T> { - DifferenceItems{a: self.iter().peekable(), b: other.iter().peekable()} + pub fn difference<'a>(&'a self, other: &'a BTreeSet) -> Difference<'a, T> { + Difference{a: self.iter().peekable(), b: other.iter().peekable()} } /// Visits the values representing the symmetric difference, in ascending order. @@ -175,8 +175,8 @@ pub fn difference<'a>(&'a self, other: &'a BTreeSet) -> DifferenceItems<'a, T /// ``` #[unstable = "matches collection reform specification, waiting for dust to settle"] pub fn symmetric_difference<'a>(&'a self, other: &'a BTreeSet) - -> SymDifferenceItems<'a, T> { - SymDifferenceItems{a: self.iter().peekable(), b: other.iter().peekable()} + -> SymmetricDifference<'a, T> { + SymmetricDifference{a: self.iter().peekable(), b: other.iter().peekable()} } /// Visits the values representing the intersection, in ascending order. @@ -199,8 +199,8 @@ pub fn symmetric_difference<'a>(&'a self, other: &'a BTreeSet) /// ``` #[unstable = "matches collection reform specification, waiting for dust to settle"] pub fn intersection<'a>(&'a self, other: &'a BTreeSet) - -> IntersectionItems<'a, T> { - IntersectionItems{a: self.iter().peekable(), b: other.iter().peekable()} + -> Intersection<'a, T> { + Intersection{a: self.iter().peekable(), b: other.iter().peekable()} } /// Visits the values representing the union, in ascending order. @@ -220,8 +220,8 @@ pub fn intersection<'a>(&'a self, other: &'a BTreeSet) /// assert_eq!(union, vec![1u,2]); /// ``` #[unstable = "matches collection reform specification, waiting for dust to settle"] - pub fn union<'a>(&'a self, other: &'a BTreeSet) -> UnionItems<'a, T> { - UnionItems{a: self.iter().peekable(), b: other.iter().peekable()} + pub fn union<'a>(&'a self, other: &'a BTreeSet) -> Union<'a, T> { + Union{a: self.iter().peekable(), b: other.iter().peekable()} } /// Return the number of elements in the set @@ -573,7 +573,7 @@ fn cmp_opt(x: Option<&T>, y: Option<&T>, } } -impl<'a, T: Ord> Iterator<&'a T> for DifferenceItems<'a, T> { +impl<'a, T: Ord> Iterator<&'a T> for Difference<'a, T> { fn next(&mut self) -> Option<&'a T> { loop { match cmp_opt(self.a.peek(), self.b.peek(), Less, Less) { @@ -585,7 +585,7 @@ fn next(&mut self) -> Option<&'a T> { } } -impl<'a, T: Ord> Iterator<&'a T> for SymDifferenceItems<'a, T> { +impl<'a, T: Ord> Iterator<&'a T> for SymmetricDifference<'a, T> { fn next(&mut self) -> Option<&'a T> { loop { match cmp_opt(self.a.peek(), self.b.peek(), Greater, Less) { @@ -597,7 +597,7 @@ fn next(&mut self) -> Option<&'a T> { } } -impl<'a, T: Ord> Iterator<&'a T> for IntersectionItems<'a, T> { +impl<'a, T: Ord> Iterator<&'a T> for Intersection<'a, T> { fn next(&mut self) -> Option<&'a T> { loop { let o_cmp = match (self.a.peek(), self.b.peek()) { @@ -615,7 +615,7 @@ fn next(&mut self) -> Option<&'a T> { } } -impl<'a, T: Ord> Iterator<&'a T> for UnionItems<'a, T> { +impl<'a, T: Ord> Iterator<&'a T> for Union<'a, T> { fn next(&mut self) -> Option<&'a T> { loop { match cmp_opt(self.a.peek(), self.b.peek(), Greater, Less) { diff --git a/src/libcollections/vec_map.rs b/src/libcollections/vec_map.rs index 802be4271898..999025840caa 100644 --- a/src/libcollections/vec_map.rs +++ b/src/libcollections/vec_map.rs @@ -176,8 +176,8 @@ fn second((_, b): (A, B)) -> B { b } /// } /// ``` #[unstable = "matches collection reform specification, waiting for dust to settle"] - pub fn iter<'r>(&'r self) -> Entries<'r, V> { - Entries { + pub fn iter<'r>(&'r self) -> Iter<'r, V> { + Iter { front: 0, back: self.v.len(), iter: self.v.iter() @@ -207,8 +207,8 @@ pub fn iter<'r>(&'r self) -> Entries<'r, V> { /// } /// ``` #[unstable = "matches collection reform specification, waiting for dust to settle"] - pub fn iter_mut<'r>(&'r mut self) -> MutEntries<'r, V> { - MutEntries { + pub fn iter_mut<'r>(&'r mut self) -> IterMut<'r, V> { + IterMut { front: 0, back: self.v.len(), iter: self.v.iter_mut() @@ -605,34 +605,34 @@ fn next_back(&mut self) -> Option<$elem> { } /// An iterator over the key-value pairs of a map. -pub struct Entries<'a, V:'a> { +pub struct Iter<'a, V:'a> { front: uint, back: uint, iter: slice::Iter<'a, Option> } -iterator! { impl Entries -> (uint, &'a V), as_ref } -double_ended_iterator! { impl Entries -> (uint, &'a V), as_ref } +iterator! { impl Iter -> (uint, &'a V), as_ref } +double_ended_iterator! { impl Iter -> (uint, &'a V), as_ref } /// An iterator over the key-value pairs of a map, with the /// values being mutable. -pub struct MutEntries<'a, V:'a> { +pub struct IterMut<'a, V:'a> { front: uint, back: uint, iter: slice::IterMut<'a, Option> } -iterator! { impl MutEntries -> (uint, &'a mut V), as_mut } -double_ended_iterator! { impl MutEntries -> (uint, &'a mut V), as_mut } +iterator! { impl IterMut -> (uint, &'a mut V), as_mut } +double_ended_iterator! { impl IterMut -> (uint, &'a mut V), as_mut } /// An iterator over the keys of a map. pub struct Keys<'a, V: 'a> { - iter: Map<(uint, &'a V), uint, Entries<'a, V>, fn((uint, &'a V)) -> uint> + iter: Map<(uint, &'a V), uint, Iter<'a, V>, fn((uint, &'a V)) -> uint> } /// An iterator over the values of a map. pub struct Values<'a, V: 'a> { - iter: Map<(uint, &'a V), &'a V, Entries<'a, V>, fn((uint, &'a V)) -> &'a V> + iter: Map<(uint, &'a V), &'a V, Iter<'a, V>, fn((uint, &'a V)) -> &'a V> } /// A consuming iterator over the key-value pairs of a map. diff --git a/src/libcore/slice.rs b/src/libcore/slice.rs index 6092a45c97d9..26684864c4c4 100644 --- a/src/libcore/slice.rs +++ b/src/libcore/slice.rs @@ -781,7 +781,7 @@ impl<'a,T> Copy for Iter<'a,T> {} #[experimental = "needs review"] impl<'a, T> ExactSizeIterator<&'a T> for Iter<'a, T> {} - #[experimental = "needs review"] +#[stable] impl<'a, T> Clone for Iter<'a, T> { fn clone(&self) -> Iter<'a, T> { *self } } diff --git a/src/libstd/collections/hash/map.rs b/src/libstd/collections/hash/map.rs index 8149864afd40..692f120737a5 100644 --- a/src/libstd/collections/hash/map.rs +++ b/src/libstd/collections/hash/map.rs @@ -914,8 +914,8 @@ pub fn iter(&self) -> Entries { /// } /// ``` #[unstable = "matches collection reform specification, waiting for dust to settle"] - pub fn iter_mut(&mut self) -> MutEntries { - MutEntries { inner: self.table.iter_mut() } + pub fn iter_mut(&mut self) -> IterMut { + IterMut { inner: self.table.iter_mut() } } /// Creates a consuming iterator, that is, one that moves each key-value @@ -936,10 +936,10 @@ pub fn iter_mut(&mut self) -> MutEntries { /// let vec: Vec<(&str, int)> = map.into_iter().collect(); /// ``` #[unstable = "matches collection reform specification, waiting for dust to settle"] - pub fn into_iter(self) -> MoveEntries { + pub fn into_iter(self) -> IntoIter { fn last_two((_, b, c): (A, B, C)) -> (B, C) { (b, c) } - MoveEntries { + IntoIter { inner: self.table.into_iter().map(last_two) } } @@ -1306,16 +1306,16 @@ pub struct Entries<'a, K: 'a, V: 'a> { } /// HashMap mutable values iterator -pub struct MutEntries<'a, K: 'a, V: 'a> { - inner: table::MutEntries<'a, K, V> +pub struct IterMut<'a, K: 'a, V: 'a> { + inner: table::IterMut<'a, K, V> } /// HashMap move iterator -pub struct MoveEntries { +pub struct IntoIter { inner: iter::Map< (SafeHash, K, V), (K, V), - table::MoveEntries, + table::IntoIter, fn((SafeHash, K, V)) -> (K, V), > } @@ -1374,12 +1374,12 @@ impl<'a, K, V> Iterator<(&'a K, &'a V)> for Entries<'a, K, V> { #[inline] fn size_hint(&self) -> (uint, Option) { self.inner.size_hint() } } -impl<'a, K, V> Iterator<(&'a K, &'a mut V)> for MutEntries<'a, K, V> { +impl<'a, K, V> Iterator<(&'a K, &'a mut V)> for IterMut<'a, K, V> { #[inline] fn next(&mut self) -> Option<(&'a K, &'a mut V)> { self.inner.next() } #[inline] fn size_hint(&self) -> (uint, Option) { self.inner.size_hint() } } -impl Iterator<(K, V)> for MoveEntries { +impl Iterator<(K, V)> for IntoIter { #[inline] fn next(&mut self) -> Option<(K, V)> { self.inner.next() } #[inline] fn size_hint(&self) -> (uint, Option) { self.inner.size_hint() } } diff --git a/src/libstd/collections/hash/set.rs b/src/libstd/collections/hash/set.rs index f587669d3dac..a2c31591d8df 100644 --- a/src/libstd/collections/hash/set.rs +++ b/src/libstd/collections/hash/set.rs @@ -21,7 +21,7 @@ use option::Option::{Some, None, mod}; use result::Result::{Ok, Err}; -use super::map::{mod, HashMap, MoveEntries, Keys, INITIAL_CAPACITY}; +use super::map::{mod, HashMap, Keys, INITIAL_CAPACITY}; // FIXME(conventions): implement BitOr, BitAnd, BitXor, and Sub @@ -625,7 +625,7 @@ pub struct Iter<'a, K: 'a> { /// HashSet move iterator pub struct IntoIter { - iter: Map<(K, ()), K, MoveEntries, fn((K, ())) -> K> + iter: Map<(K, ()), K, map::IntoIter, fn((K, ())) -> K> } /// HashSet drain iterator diff --git a/src/libstd/collections/hash/table.rs b/src/libstd/collections/hash/table.rs index ce7dbd8ea5ec..8f2152c5a9de 100644 --- a/src/libstd/collections/hash/table.rs +++ b/src/libstd/collections/hash/table.rs @@ -664,17 +664,17 @@ pub fn iter(&self) -> Entries { } } - pub fn iter_mut(&mut self) -> MutEntries { - MutEntries { + pub fn iter_mut(&mut self) -> IterMut { + IterMut { iter: self.raw_buckets(), elems_left: self.size(), } } - pub fn into_iter(self) -> MoveEntries { + pub fn into_iter(self) -> IntoIter { let RawBuckets { raw, hashes_end, .. } = self.raw_buckets(); // Replace the marker regardless of lifetime bounds on parameters. - MoveEntries { + IntoIter { iter: RawBuckets { raw: raw, hashes_end: hashes_end, @@ -776,13 +776,13 @@ pub struct Entries<'a, K: 'a, V: 'a> { } /// Iterator over mutable references to entries in a table. -pub struct MutEntries<'a, K: 'a, V: 'a> { +pub struct IterMut<'a, K: 'a, V: 'a> { iter: RawBuckets<'a, K, V>, elems_left: uint, } /// Iterator over the entries in a table, consuming the table. -pub struct MoveEntries { +pub struct IntoIter { table: RawTable, iter: RawBuckets<'static, K, V> } @@ -809,7 +809,7 @@ fn size_hint(&self) -> (uint, Option) { } } -impl<'a, K, V> Iterator<(&'a K, &'a mut V)> for MutEntries<'a, K, V> { +impl<'a, K, V> Iterator<(&'a K, &'a mut V)> for IterMut<'a, K, V> { fn next(&mut self) -> Option<(&'a K, &'a mut V)> { self.iter.next().map(|bucket| { self.elems_left -= 1; @@ -825,7 +825,7 @@ fn size_hint(&self) -> (uint, Option) { } } -impl Iterator<(SafeHash, K, V)> for MoveEntries { +impl Iterator<(SafeHash, K, V)> for IntoIter { fn next(&mut self) -> Option<(SafeHash, K, V)> { self.iter.next().map(|bucket| { self.table.size -= 1; From 98ed8825114d4413c3d739e734ca0668222eb2f6 Mon Sep 17 00:00:00 2001 From: Maya Nitu Date: Mon, 22 Dec 2014 18:20:43 +0200 Subject: [PATCH 25/44] Removed unused context-switching assembly code. --- mk/rt.mk | 5 +- src/rt/arch/arm/_context.S | 69 ------------ src/rt/arch/i386/_context.S | 65 ------------ src/rt/arch/mips/_context.S | 88 ---------------- src/rt/arch/mipsel/_context.S | 88 ---------------- src/rt/arch/x86_64/_context.S | 192 ---------------------------------- src/rt/arch/x86_64/regs.h | 65 ------------ 7 files changed, 2 insertions(+), 570 deletions(-) delete mode 100644 src/rt/arch/arm/_context.S delete mode 100644 src/rt/arch/i386/_context.S delete mode 100644 src/rt/arch/mips/_context.S delete mode 100644 src/rt/arch/mipsel/_context.S delete mode 100644 src/rt/arch/x86_64/_context.S delete mode 100644 src/rt/arch/x86_64/regs.h diff --git a/mk/rt.mk b/mk/rt.mk index 38aec8363160..a1d18aae1b45 100644 --- a/mk/rt.mk +++ b/mk/rt.mk @@ -35,7 +35,7 @@ # that's per-target so you're allowed to conditionally add files based on the # target. ################################################################################ -NATIVE_LIBS := rust_builtin hoedown morestack miniz context_switch \ +NATIVE_LIBS := rust_builtin hoedown morestack miniz \ rustrt_native rust_test_helpers # $(1) is the target triple @@ -58,8 +58,7 @@ NATIVE_DEPS_rustrt_native_$(1) := \ arch/$$(HOST_$(1))/record_sp.S NATIVE_DEPS_rust_test_helpers_$(1) := rust_test_helpers.c NATIVE_DEPS_morestack_$(1) := arch/$$(HOST_$(1))/morestack.S -NATIVE_DEPS_context_switch_$(1) := \ - arch/$$(HOST_$(1))/_context.S + ################################################################################ # You shouldn't find it that necessary to edit anything below this line. diff --git a/src/rt/arch/arm/_context.S b/src/rt/arch/arm/_context.S deleted file mode 100644 index 38fc4827f586..000000000000 --- a/src/rt/arch/arm/_context.S +++ /dev/null @@ -1,69 +0,0 @@ -// Mark stack as non-executable -#if defined(__linux__) && defined(__ELF__) -.section .note.GNU-stack, "", %progbits -#endif - -.text -.code 32 -.arm -#if defined(__APPLE__) -.align 2 -#else -.align -#endif - -#if defined(__APPLE__) - #define SWAP_REGISTERS _rust_swap_registers - #define BOOTSTRAP_TASK _rust_bootstrap_green_task -#else - #define SWAP_REGISTERS rust_swap_registers - #define BOOTSTRAP_TASK rust_bootstrap_green_task -#endif - -.globl SWAP_REGISTERS -SWAP_REGISTERS: - str r0, [r0, #0] - str r3, [r0, #12] - str r4, [r0, #16] - str r5, [r0, #20] - str r6, [r0, #24] - str r7, [r0, #28] - str r8, [r0, #32] - str r9, [r0, #36] - str r10, [r0, #40] - str r11, [r0, #44] - str r12, [r0, #48] - str sp, [r0, #52] - str lr, [r0, #56] - - mrs r2, cpsr - str r2, [r0, #64] - - - ldr r0, [r1, #0] - ldr r3, [r1, #12] - ldr r4, [r1, #16] - ldr r5, [r1, #20] - ldr r6, [r1, #24] - ldr r7, [r1, #28] - ldr r8, [r1, #32] - ldr r9, [r1, #36] - ldr r10, [r1, #40] - ldr r11, [r1, #44] - ldr r12, [r1, #48] - - ldr sp, [r1, #52] - ldr lr, [r1, #56] - - ldr r2, [r1, #64] - msr cpsr_cxsf, r2 - - mov pc, lr - -// For reasons of this existence, see the comments in x86_64/_context.S -.globl BOOTSTRAP_TASK -BOOTSTRAP_TASK: - mov r0, r0 - mov r1, r3 - mov r2, r4 - mov pc, r5 diff --git a/src/rt/arch/i386/_context.S b/src/rt/arch/i386/_context.S deleted file mode 100644 index 6b79a82e4aca..000000000000 --- a/src/rt/arch/i386/_context.S +++ /dev/null @@ -1,65 +0,0 @@ -// Mark stack as non-executable -#if defined(__linux__) && defined(__ELF__) -.section .note.GNU-stack, "", @progbits -#endif - - .text - -/* -Callee save registers: - ebp, ebx, esi, edi - -Caller save registers: - eax, ecx, edx -*/ - -/* -Saves a set of registers. This is used by our implementation of -getcontext. - -The registers_t variable is in (%esp) -*/ - -#if defined(__APPLE__) || defined(_WIN32) -#define SWAP_REGISTERS _rust_swap_registers -#else -#define SWAP_REGISTERS rust_swap_registers -#endif - -// swap_registers(registers_t *oregs, registers_t *regs) -.globl SWAP_REGISTERS -SWAP_REGISTERS: - // save the old context - movl 4(%esp), %eax - movl %ebx, 4(%eax) - movl %ebp, 16(%eax) - movl %esi, 20(%eax) - movl %edi, 24(%eax) - - // save the flags - pushf - popl %ecx - movl %ecx, 44(%eax) - - // save the return address as the instruction pointer - // and save the stack pointer of the caller - popl %ecx - movl %esp, 28(%eax) - movl %ecx, 48(%eax) - - // restore the new context - movl 4(%esp), %eax - - movl 4(%eax), %ebx - movl 16(%eax), %ebp - movl 20(%eax), %esi - movl 24(%eax), %edi - movl 28(%eax), %esp - - // restore the flags - movl 44(%eax), %ecx - push %ecx - popf - - // Return! - jmp *48(%eax) diff --git a/src/rt/arch/mips/_context.S b/src/rt/arch/mips/_context.S deleted file mode 100644 index cfe77cc30456..000000000000 --- a/src/rt/arch/mips/_context.S +++ /dev/null @@ -1,88 +0,0 @@ -// Mark stack as non-executable -#if defined(__linux__) && defined(__ELF__) -.section .note.GNU-stack, "", @progbits -#endif - -.text -.globl rust_swap_registers -.align 2 -.set nomips16 -.ent rust_swap_registers -rust_swap_registers: - .set noreorder - .set nomacro - .set noat - sw $1, 1 * 4($4) - sw $2, 2 * 4($4) - sw $3, 3 * 4($4) - sw $4, 4 * 4($4) - sw $5, 5 * 4($4) - sw $6, 6 * 4($4) - sw $7, 7 * 4($4) - - sw $8, 8 * 4($4) - sw $9, 9 * 4($4) - sw $10, 10 * 4($4) - sw $11, 11 * 4($4) - sw $12, 12 * 4($4) - sw $13, 13 * 4($4) - sw $14, 14 * 4($4) - sw $15, 15 * 4($4) - - sw $16, 16 * 4($4) - sw $17, 17 * 4($4) - sw $18, 18 * 4($4) - sw $19, 19 * 4($4) - sw $20, 20 * 4($4) - sw $21, 21 * 4($4) - sw $22, 22 * 4($4) - sw $23, 23 * 4($4) - - sw $24, 24 * 4($4) - sw $25, 25 * 4($4) - sw $26, 26 * 4($4) - sw $27, 27 * 4($4) - sw $28, 28 * 4($4) - sw $29, 29 * 4($4) - sw $30, 30 * 4($4) - sw $31, 31 * 4($4) - - lw $1, 1 * 4($5) - lw $2, 2 * 4($5) - lw $3, 3 * 4($5) - lw $4, 4 * 4($5) - lw $6, 6 * 4($5) - lw $7, 7 * 4($5) - - lw $8, 8 * 4($5) - lw $9, 9 * 4($5) - lw $10, 10 * 4($5) - lw $11, 11 * 4($5) - lw $12, 12 * 4($5) - lw $13, 13 * 4($5) - lw $14, 14 * 4($5) - lw $15, 15 * 4($5) - - lw $16, 16 * 4($5) - lw $17, 17 * 4($5) - lw $18, 18 * 4($5) - lw $19, 19 * 4($5) - lw $20, 20 * 4($5) - lw $21, 21 * 4($5) - lw $22, 22 * 4($5) - lw $23, 23 * 4($5) - - lw $24, 24 * 4($5) - lw $25, 25 * 4($5) - lw $26, 26 * 4($5) - lw $27, 27 * 4($5) - lw $28, 28 * 4($5) - lw $29, 29 * 4($5) - lw $30, 30 * 4($5) - lw $31, 31 * 4($5) - - lw $5, 5 * 4($5) - - jr $31 - nop -.end rust_swap_registers diff --git a/src/rt/arch/mipsel/_context.S b/src/rt/arch/mipsel/_context.S deleted file mode 100644 index cfe77cc30456..000000000000 --- a/src/rt/arch/mipsel/_context.S +++ /dev/null @@ -1,88 +0,0 @@ -// Mark stack as non-executable -#if defined(__linux__) && defined(__ELF__) -.section .note.GNU-stack, "", @progbits -#endif - -.text -.globl rust_swap_registers -.align 2 -.set nomips16 -.ent rust_swap_registers -rust_swap_registers: - .set noreorder - .set nomacro - .set noat - sw $1, 1 * 4($4) - sw $2, 2 * 4($4) - sw $3, 3 * 4($4) - sw $4, 4 * 4($4) - sw $5, 5 * 4($4) - sw $6, 6 * 4($4) - sw $7, 7 * 4($4) - - sw $8, 8 * 4($4) - sw $9, 9 * 4($4) - sw $10, 10 * 4($4) - sw $11, 11 * 4($4) - sw $12, 12 * 4($4) - sw $13, 13 * 4($4) - sw $14, 14 * 4($4) - sw $15, 15 * 4($4) - - sw $16, 16 * 4($4) - sw $17, 17 * 4($4) - sw $18, 18 * 4($4) - sw $19, 19 * 4($4) - sw $20, 20 * 4($4) - sw $21, 21 * 4($4) - sw $22, 22 * 4($4) - sw $23, 23 * 4($4) - - sw $24, 24 * 4($4) - sw $25, 25 * 4($4) - sw $26, 26 * 4($4) - sw $27, 27 * 4($4) - sw $28, 28 * 4($4) - sw $29, 29 * 4($4) - sw $30, 30 * 4($4) - sw $31, 31 * 4($4) - - lw $1, 1 * 4($5) - lw $2, 2 * 4($5) - lw $3, 3 * 4($5) - lw $4, 4 * 4($5) - lw $6, 6 * 4($5) - lw $7, 7 * 4($5) - - lw $8, 8 * 4($5) - lw $9, 9 * 4($5) - lw $10, 10 * 4($5) - lw $11, 11 * 4($5) - lw $12, 12 * 4($5) - lw $13, 13 * 4($5) - lw $14, 14 * 4($5) - lw $15, 15 * 4($5) - - lw $16, 16 * 4($5) - lw $17, 17 * 4($5) - lw $18, 18 * 4($5) - lw $19, 19 * 4($5) - lw $20, 20 * 4($5) - lw $21, 21 * 4($5) - lw $22, 22 * 4($5) - lw $23, 23 * 4($5) - - lw $24, 24 * 4($5) - lw $25, 25 * 4($5) - lw $26, 26 * 4($5) - lw $27, 27 * 4($5) - lw $28, 28 * 4($5) - lw $29, 29 * 4($5) - lw $30, 30 * 4($5) - lw $31, 31 * 4($5) - - lw $5, 5 * 4($5) - - jr $31 - nop -.end rust_swap_registers diff --git a/src/rt/arch/x86_64/_context.S b/src/rt/arch/x86_64/_context.S deleted file mode 100644 index 36caf7720c40..000000000000 --- a/src/rt/arch/x86_64/_context.S +++ /dev/null @@ -1,192 +0,0 @@ -// Mark stack as non-executable -#if defined(__linux__) && defined(__ELF__) -.section .note.GNU-stack, "", @progbits -#endif - -#include "regs.h" -#define ARG0 RUSTRT_ARG0_S -#define ARG1 RUSTRT_ARG1_S - - .text - -/* -According to ABI documentation found at -http://www.x86-64.org/documentation.html -and Microsoft discussion at -http://msdn.microsoft.com/en-US/library/9z1stfyw%28v=VS.80%29.aspx. - -BOTH CALLING CONVENTIONS - -Callee save registers: - R12--R15, RDI, RSI, RBX, RBP, RSP - XMM0--XMM5 - -Caller save registers: - RAX, RCX, RDX, R8--R11 - XMM6--XMM15 - Floating point stack - -MAC/AMD CALLING CONVENTIONS - -Integer arguments go in registers: - rdi, rsi, rdx, rcx, r8, r9 - -User flags have no specified role and are not preserved - across calls, with the exception of DF in %rFLAGS, - which must be clear (set to "forward" direction) - on function entry and return. - -MICROSOFT CALLING CONVENTIONS - -Return value: RAX - -First four arguments: - RCX, RDX, R8, R9 - XMM0, XMM1, XMM2, XMM3 -*/ - -/* - Stores current registers into arg0/RCX and restores - registers found in arg1/RDX. This is used by our - implementation of getcontext. Only saves/restores nonvolatile - registers and the register used for the first argument. - Volatile registers in general ought to be saved by the caller - anyhow. -*/ - -#if defined(__APPLE__) -#define SWAP_REGISTERS _rust_swap_registers -#else -#define SWAP_REGISTERS rust_swap_registers -#endif - -// swap_registers(registers_t *oregs, registers_t *regs) -.globl SWAP_REGISTERS -SWAP_REGISTERS: - // n.b. when we enter, the return address is at the top of - // the stack (i.e., 0(%RSP)) and the argument is in - // RUSTRT_ARG0_S. We - // simply save all NV registers into oregs. - // We then restore all NV registers from regs. This restores - // the old stack pointer, which should include the proper - // return address. We can therefore just return normally to - // jump back into the old code. - - // Save instruction pointer: - pop %rax - mov %rax, (RUSTRT_IP*8)(RUSTRT_ARG0_S) - - // Save non-volatile integer registers: - // (including RSP) - mov %rbx, (RUSTRT_RBX*8)(ARG0) - mov %rsp, (RUSTRT_RSP*8)(ARG0) - mov %rbp, (RUSTRT_RBP*8)(ARG0) - mov %r12, (RUSTRT_R12*8)(ARG0) - mov %r13, (RUSTRT_R13*8)(ARG0) - mov %r14, (RUSTRT_R14*8)(ARG0) - mov %r15, (RUSTRT_R15*8)(ARG0) - -#if defined(__MINGW32__) || defined(_WINDOWS) - mov %rdi, (RUSTRT_RDI*8)(ARG0) - mov %rsi, (RUSTRT_RSI*8)(ARG0) -#endif - - // Save 0th argument register: - mov ARG0, (RUSTRT_ARG0*8)(ARG0) - - // Save non-volatile XMM registers: -#if defined(__MINGW32__) || defined(_WINDOWS) - movapd %xmm6, (RUSTRT_XMM6*8)(ARG0) - movapd %xmm7, (RUSTRT_XMM7*8)(ARG0) - movapd %xmm8, (RUSTRT_XMM8*8)(ARG0) - movapd %xmm9, (RUSTRT_XMM9*8)(ARG0) - movapd %xmm10, (RUSTRT_XMM10*8)(ARG0) - movapd %xmm11, (RUSTRT_XMM11*8)(ARG0) - movapd %xmm12, (RUSTRT_XMM12*8)(ARG0) - movapd %xmm13, (RUSTRT_XMM13*8)(ARG0) - movapd %xmm14, (RUSTRT_XMM14*8)(ARG0) - movapd %xmm15, (RUSTRT_XMM15*8)(ARG0) -#else - movapd %xmm0, (RUSTRT_XMM0*8)(ARG0) - movapd %xmm1, (RUSTRT_XMM1*8)(ARG0) - movapd %xmm2, (RUSTRT_XMM2*8)(ARG0) - movapd %xmm3, (RUSTRT_XMM3*8)(ARG0) - movapd %xmm4, (RUSTRT_XMM4*8)(ARG0) - movapd %xmm5, (RUSTRT_XMM5*8)(ARG0) -#endif - - // Restore non-volatile integer registers: - // (including RSP) - mov (RUSTRT_RBX*8)(ARG1), %rbx - mov (RUSTRT_RSP*8)(ARG1), %rsp - mov (RUSTRT_RBP*8)(ARG1), %rbp - mov (RUSTRT_R12*8)(ARG1), %r12 - mov (RUSTRT_R13*8)(ARG1), %r13 - mov (RUSTRT_R14*8)(ARG1), %r14 - mov (RUSTRT_R15*8)(ARG1), %r15 - -#if defined(__MINGW32__) || defined(_WINDOWS) - mov (RUSTRT_RDI*8)(ARG1), %rdi - mov (RUSTRT_RSI*8)(ARG1), %rsi -#endif - - // Restore 0th argument register: - mov (RUSTRT_ARG0*8)(ARG1), ARG0 - - // Restore non-volatile XMM registers: -#if defined(__MINGW32__) || defined(_WINDOWS) - movapd (RUSTRT_XMM6*8)(ARG1), %xmm6 - movapd (RUSTRT_XMM7*8)(ARG1), %xmm7 - movapd (RUSTRT_XMM8*8)(ARG1), %xmm8 - movapd (RUSTRT_XMM9*8)(ARG1), %xmm9 - movapd (RUSTRT_XMM10*8)(ARG1), %xmm10 - movapd (RUSTRT_XMM11*8)(ARG1), %xmm11 - movapd (RUSTRT_XMM12*8)(ARG1), %xmm12 - movapd (RUSTRT_XMM13*8)(ARG1), %xmm13 - movapd (RUSTRT_XMM14*8)(ARG1), %xmm14 - movapd (RUSTRT_XMM15*8)(ARG1), %xmm15 -#else - movapd (RUSTRT_XMM0*8)(ARG1), %xmm0 - movapd (RUSTRT_XMM1*8)(ARG1), %xmm1 - movapd (RUSTRT_XMM2*8)(ARG1), %xmm2 - movapd (RUSTRT_XMM3*8)(ARG1), %xmm3 - movapd (RUSTRT_XMM4*8)(ARG1), %xmm4 - movapd (RUSTRT_XMM5*8)(ARG1), %xmm5 -#endif - - // Jump to the instruction pointer - // found in regs: - jmp *(RUSTRT_IP*8)(ARG1) - -// This function below, rust_bootstrap_green_task, is used to initialize a green -// task. This code is the very first code that is run whenever a green task -// starts. The only assumptions that this code makes is that it has a register -// context previously set up by Context::new() and some values are in some -// special registers. -// -// In theory the register context could be set up and then the context switching -// would plop us directly into some 'extern "C" fn', but not all platforms have -// the argument registers saved throughout a context switch (linux doesn't save -// rdi/rsi, the first two argument registers). Instead of modifying all context -// switches, instead the initial data for starting a green thread is shoved into -// unrelated registers (r12/13, etc) which always need to be saved on context -// switches anyway. -// -// With this strategy we get the benefit of being able to pass a fair bit of -// contextual data from the start of a green task to its init function, as well -// as not hindering any context switches. -// -// If you alter this code in any way, you likely need to update -// src/libgreen/context.rs as well. - -#if defined(__APPLE__) -#define BOOTSTRAP _rust_bootstrap_green_task -#else -#define BOOTSTRAP rust_bootstrap_green_task -#endif -.globl BOOTSTRAP -BOOTSTRAP: - mov %r12, RUSTRT_ARG0_S - mov %r13, RUSTRT_ARG1_S - mov %r14, RUSTRT_ARG2_S - jmpq *%r15 diff --git a/src/rt/arch/x86_64/regs.h b/src/rt/arch/x86_64/regs.h deleted file mode 100644 index 25160ca68a6b..000000000000 --- a/src/rt/arch/x86_64/regs.h +++ /dev/null @@ -1,65 +0,0 @@ -// Copyright 2012 The Rust Project Developers. See the COPYRIGHT -// file at the top-level directory of this distribution and at -// http://rust-lang.org/COPYRIGHT. -// -// Licensed under the Apache License, Version 2.0 or the MIT license -// , at your -// option. This file may not be copied, modified, or distributed -// except according to those terms. - -// This is loosely kept in sync with src/libstd/rt/context.rs - -#define RUSTRT_RBX 0 -#define RUSTRT_RSP 1 -#define RUSTRT_RBP 2 -// RCX on Windows, RDI elsewhere -#define RUSTRT_ARG0 3 -#define RUSTRT_R12 4 -#define RUSTRT_R13 5 -#define RUSTRT_R14 6 -#define RUSTRT_R15 7 -#define RUSTRT_IP 8 -#if defined(__MINGW32__) || defined(_WINDOWS) - #define RUSTRT_RDI 9 - #define RUSTRT_RSI 10 - #define RUSTRT_ST1 11 - #define RUSTRT_ST2 12 - #define RUSTRT_XMM6 14 - #define RUSTRT_XMM7 16 - #define RUSTRT_XMM8 18 - #define RUSTRT_XMM9 20 - #define RUSTRT_XMM10 22 - #define RUSTRT_XMM11 24 - #define RUSTRT_XMM12 26 - #define RUSTRT_XMM13 28 - #define RUSTRT_XMM14 30 - #define RUSTRT_XMM15 32 - #define RUSTRT_MAX 34 -#else - // Not used, just padding - #define RUSTRT_XXX 9 - #define RUSTRT_XMM0 10 - #define RUSTRT_XMM1 12 - #define RUSTRT_XMM2 14 - #define RUSTRT_XMM3 16 - #define RUSTRT_XMM4 18 - #define RUSTRT_XMM5 20 - #define RUSTRT_MAX 22 -#endif - -// ARG0 is the register in which the first argument goes. -// Naturally this depends on your operating system. -#if defined(__MINGW32__) || defined(_WINDOWS) -# define RUSTRT_ARG0_S %rcx -# define RUSTRT_ARG1_S %rdx -# define RUSTRT_ARG2_S %r8 -# define RUSTRT_ARG3_S %r9 -#else -# define RUSTRT_ARG0_S %rdi -# define RUSTRT_ARG1_S %rsi -# define RUSTRT_ARG2_S %rdx -# define RUSTRT_ARG3_S %rcx -# define RUSTRT_ARG4_S %r8 -# define RUSTRT_ARG5_S %r9 -#endif From f46099575a304ae9032f45c7921b496c4d220697 Mon Sep 17 00:00:00 2001 From: Niko Matsakis Date: Wed, 26 Nov 2014 06:36:09 -0500 Subject: [PATCH 26/44] Make ty_bare_fn carry an optional def-id indicating whether it is the type of a fn item or a fn pointer, which are now differentiated. Introduce coercion from fn item to fn pointer. --- src/librustc/middle/check_const.rs | 6 ++- src/librustc/middle/expr_use_visitor.rs | 10 ++-- src/librustc/middle/infer/coercion.rs | 60 +++++++++++++-------- src/librustc/middle/infer/combine.rs | 11 ++-- src/librustc/middle/mem_categorization.rs | 4 +- src/librustc/middle/ty.rs | 60 +++++++++++++++++---- src/librustc/middle/ty_fold.rs | 4 +- src/librustc_trans/trans/base.rs | 1 + src/librustc_trans/trans/callee.rs | 5 +- src/librustc_trans/trans/closure.rs | 22 ++------ src/librustc_trans/trans/consts.rs | 7 ++- src/librustc_trans/trans/expr.rs | 11 ++-- src/librustc_trans/trans/glue.rs | 1 + src/librustc_trans/trans/meth.rs | 3 +- src/librustc_typeck/astconv.rs | 2 +- src/librustc_typeck/check/method/confirm.rs | 2 +- src/librustc_typeck/check/method/mod.rs | 2 +- src/librustc_typeck/check/mod.rs | 16 ++++-- src/librustc_typeck/check/writeback.rs | 24 ++------- src/librustc_typeck/coherence/mod.rs | 2 +- src/librustc_typeck/collect.rs | 17 +++--- src/librustc_typeck/lib.rs | 5 +- 22 files changed, 161 insertions(+), 114 deletions(-) diff --git a/src/librustc/middle/check_const.rs b/src/librustc/middle/check_const.rs index a91ea8bfef8c..e08dd64d4d41 100644 --- a/src/librustc/middle/check_const.rs +++ b/src/librustc/middle/check_const.rs @@ -127,7 +127,11 @@ fn check_expr(v: &mut CheckCrateVisitor, e: &ast::Expr) -> bool { ast::ExprCast(ref from, _) => { let toty = ty::expr_ty(v.tcx, e); let fromty = ty::expr_ty(v.tcx, &**from); - if !ty::type_is_numeric(toty) && !ty::type_is_unsafe_ptr(toty) { + let is_legal_cast = + ty::type_is_numeric(toty) || + ty::type_is_unsafe_ptr(toty) || + (ty::type_is_bare_fn(toty) && ty::type_is_bare_fn_item(fromty)); + if !is_legal_cast { span_err!(v.tcx.sess, e.span, E0012, "can not cast to `{}` in a constant expression", ppaux::ty_to_string(v.tcx, toty)); diff --git a/src/librustc/middle/expr_use_visitor.rs b/src/librustc/middle/expr_use_visitor.rs index abc3c8d0d8fa..046a2894b5df 100644 --- a/src/librustc/middle/expr_use_visitor.rs +++ b/src/librustc/middle/expr_use_visitor.rs @@ -824,10 +824,12 @@ fn walk_adjustment(&mut self, expr: &ast::Expr) { None => { } Some(adjustment) => { match *adjustment { - ty::AdjustAddEnv(..) => { - // Creating a closure consumes the input and stores it - // into the resulting rvalue. - debug!("walk_adjustment(AutoAddEnv)"); + ty::AdjustAddEnv(..) | + ty::AdjustReifyFnPointer(..) => { + // Creating a closure/fn-pointer consumes the + // input and stores it into the resulting + // rvalue. + debug!("walk_adjustment(AutoAddEnv|AdjustReifyFnPointer)"); let cmt_unadjusted = return_if_err!(self.mc.cat_expr_unadjusted(expr)); self.delegate_consume(expr.id, expr.span, cmt_unadjusted); diff --git a/src/librustc/middle/infer/coercion.rs b/src/librustc/middle/infer/coercion.rs index ca9ac10341c2..08336da0d8a6 100644 --- a/src/librustc/middle/infer/coercion.rs +++ b/src/librustc/middle/infer/coercion.rs @@ -486,8 +486,8 @@ pub fn coerce_borrowed_fn(&self, b.repr(self.tcx())); match a.sty { - ty::ty_bare_fn(ref f) => { - self.coerce_from_bare_fn(a, f, b) + ty::ty_bare_fn(Some(a_def_id), ref f) => { + self.coerce_from_fn_item(a, a_def_id, f, b) } _ => { self.subtype(a, b) @@ -495,32 +495,46 @@ pub fn coerce_borrowed_fn(&self, } } - /// Attempts to coerce from a bare Rust function (`extern "Rust" fn`) into a closure or a - /// `proc`. - fn coerce_from_bare_fn(&self, a: Ty<'tcx>, fn_ty_a: &ty::BareFnTy<'tcx>, b: Ty<'tcx>) + fn coerce_from_fn_item(&self, + a: Ty<'tcx>, + fn_def_id_a: ast::DefId, + fn_ty_a: &ty::BareFnTy<'tcx>, + b: Ty<'tcx>) -> CoerceResult<'tcx> { + /*! + * Attempts to coerce from the type of a Rust function item + * into a closure or a `proc`. + */ + self.unpack_actual_value(b, |b| { - debug!("coerce_from_bare_fn(a={}, b={})", - a.repr(self.get_ref().infcx.tcx), b.repr(self.get_ref().infcx.tcx)); + a.repr(self.tcx()), b.repr(self.tcx())); - if fn_ty_a.abi != abi::Rust || fn_ty_a.unsafety != ast::Unsafety::Normal { - return self.subtype(a, b); + match b.sty { + ty::ty_closure(ref f) => { + if fn_ty_a.abi != abi::Rust || fn_ty_a.unsafety != ast::Unsafety::Normal { + return self.subtype(a, b); + } + + let fn_ty_b = (*f).clone(); + let adj = ty::AdjustAddEnv(fn_def_id_a, fn_ty_b.store); + let a_closure = ty::mk_closure(self.tcx(), + ty::ClosureTy { + sig: fn_ty_a.sig.clone(), + .. *fn_ty_b + }); + try!(self.subtype(a_closure, b)); + Ok(Some(adj)) + } + ty::ty_bare_fn(None, _) => { + let a_fn_pointer = ty::mk_bare_fn(self.tcx(), None, (*fn_ty_a).clone()); + try!(self.subtype(a_fn_pointer, b)); + Ok(Some(ty::AdjustReifyFnPointer(fn_def_id_a))) + } + _ => { + return self.subtype(a, b) + } } - - let fn_ty_b = match b.sty { - ty::ty_closure(ref f) => (*f).clone(), - _ => return self.subtype(a, b) - }; - - let adj = ty::AdjustAddEnv(fn_ty_b.store); - let a_closure = ty::mk_closure(self.get_ref().infcx.tcx, - ty::ClosureTy { - sig: fn_ty_a.sig.clone(), - .. *fn_ty_b - }); - try!(self.subtype(a_closure, b)); - Ok(Some(adj)) }) } diff --git a/src/librustc/middle/infer/combine.rs b/src/librustc/middle/infer/combine.rs index 82ddbcee5a72..fc240faac449 100644 --- a/src/librustc/middle/infer/combine.rs +++ b/src/librustc/middle/infer/combine.rs @@ -568,11 +568,12 @@ pub fn super_tys<'tcx, C: Combine<'tcx>>(this: &C, } } - (&ty::ty_bare_fn(ref a_fty), &ty::ty_bare_fn(ref b_fty)) => { - this.bare_fn_tys(a_fty, b_fty).and_then(|fty| { - Ok(ty::mk_bare_fn(tcx, fty)) - }) - } + (&ty::ty_bare_fn(a_opt_def_id, ref a_fty), &ty::ty_bare_fn(b_opt_def_id, ref b_fty)) + if a_opt_def_id == b_opt_def_id => + { + let fty = try!(this.bare_fn_tys(a_fty, b_fty)); + Ok(ty::mk_bare_fn(tcx, a_opt_def_id, fty)) + } (&ty::ty_closure(ref a_fty), &ty::ty_closure(ref b_fty)) => { this.closure_tys(&**a_fty, &**b_fty).and_then(|fty| { diff --git a/src/librustc/middle/mem_categorization.rs b/src/librustc/middle/mem_categorization.rs index dce75579ca0a..5aac988948f7 100644 --- a/src/librustc/middle/mem_categorization.rs +++ b/src/librustc/middle/mem_categorization.rs @@ -441,8 +441,8 @@ pub fn cat_expr(&self, expr: &ast::Expr) -> McResult> { Some(adjustment) => { match *adjustment { - ty::AdjustAddEnv(..) => { - debug!("cat_expr(AdjustAddEnv): {}", + ty::AdjustAddEnv(..) | ty::AdjustReifyFnPointer(..) => { + debug!("cat_expr(AdjustAddEnv|AdjustReifyFnPointer): {}", expr.repr(self.tcx())); // Convert a bare fn to a closure by adding NULL env. // Result is an rvalue. diff --git a/src/librustc/middle/ty.rs b/src/librustc/middle/ty.rs index 50a6fb9d0ca3..4130d07aed01 100644 --- a/src/librustc/middle/ty.rs +++ b/src/librustc/middle/ty.rs @@ -293,7 +293,8 @@ pub enum Variance { #[deriving(Clone, Show)] pub enum AutoAdjustment<'tcx> { - AdjustAddEnv(ty::TraitStore), + AdjustAddEnv(ast::DefId, ty::TraitStore), + AdjustReifyFnPointer(ast::DefId), // go from a fn-item type to a fn-pointer type AdjustDerefRef(AutoDerefRef<'tcx>) } @@ -1243,11 +1244,17 @@ pub enum sty<'tcx> { ty_vec(Ty<'tcx>, Option), // Second field is length. ty_ptr(mt<'tcx>), ty_rptr(Region, mt<'tcx>), - ty_bare_fn(BareFnTy<'tcx>), + + // If the def-id is Some(_), then this is the type of a specific + // fn item. Otherwise, if None(_), it a fn pointer type. + ty_bare_fn(Option, BareFnTy<'tcx>), + ty_closure(Box>), ty_trait(Box>), ty_struct(DefId, Substs<'tcx>), + ty_unboxed_closure(DefId, Region, Substs<'tcx>), + ty_tup(Vec>), ty_param(ParamTy), // type parameter @@ -2339,15 +2346,19 @@ pub fn mk_closure<'tcx>(cx: &ctxt<'tcx>, fty: ClosureTy<'tcx>) -> Ty<'tcx> { mk_t(cx, ty_closure(box fty)) } -pub fn mk_bare_fn<'tcx>(cx: &ctxt<'tcx>, fty: BareFnTy<'tcx>) -> Ty<'tcx> { - mk_t(cx, ty_bare_fn(fty)) +pub fn mk_bare_fn<'tcx>(cx: &ctxt<'tcx>, + opt_def_id: Option, + fty: BareFnTy<'tcx>) -> Ty<'tcx> { + mk_t(cx, ty_bare_fn(opt_def_id, fty)) } pub fn mk_ctor_fn<'tcx>(cx: &ctxt<'tcx>, + def_id: ast::DefId, input_tys: &[Ty<'tcx>], output: Ty<'tcx>) -> Ty<'tcx> { let input_args = input_tys.iter().map(|ty| *ty).collect(); mk_bare_fn(cx, + Some(def_id), BareFnTy { unsafety: ast::Unsafety::Normal, abi: abi::Rust, @@ -3560,6 +3571,13 @@ pub fn type_is_bare_fn(ty: Ty) -> bool { } } +pub fn type_is_bare_fn_item(ty: Ty) -> bool { + match ty.sty { + ty_bare_fn(Some(_), _) => true, + _ => false + } +} + pub fn type_is_fp(ty: Ty) -> bool { match ty.sty { ty_infer(FloatVar(_)) | ty_float(_) => true, @@ -3975,9 +3993,9 @@ pub fn adjust_ty<'tcx, F>(cx: &ctxt<'tcx>, return match adjustment { Some(adjustment) => { match *adjustment { - AdjustAddEnv(store) => { + AdjustAddEnv(_, store) => { match unadjusted_ty.sty { - ty::ty_bare_fn(ref b) => { + ty::ty_bare_fn(Some(_), ref b) => { let bounds = ty::ExistentialBounds { region_bound: ReStatic, builtin_bounds: all_builtin_bounds(), @@ -3994,7 +4012,21 @@ pub fn adjust_ty<'tcx, F>(cx: &ctxt<'tcx>, } ref b => { cx.sess.bug( - format!("add_env adjustment on non-bare-fn: \ + format!("add_env adjustment on non-fn-item: \ + {}", + b).as_slice()); + } + } + } + + AdjustReifyFnPointer(_) => { + match unadjusted_ty.sty { + ty::ty_bare_fn(Some(_), ref b) => { + ty::mk_bare_fn(cx, None, (*b).clone()) + } + ref b => { + cx.sess.bug( + format!("AdjustReifyFnPointer adjustment on non-fn-item: \ {}", b).as_slice()); } @@ -4353,7 +4385,8 @@ pub fn ty_sort_string<'tcx>(cx: &ctxt<'tcx>, ty: Ty<'tcx>) -> String { ty_vec(_, None) => "slice".to_string(), ty_ptr(_) => "*-ptr".to_string(), ty_rptr(_, _) => "&-ptr".to_string(), - ty_bare_fn(_) => "extern fn".to_string(), + ty_bare_fn(Some(_), _) => format!("fn item"), + ty_bare_fn(None, _) => "fn pointer".to_string(), ty_closure(_) => "fn".to_string(), ty_trait(ref inner) => { format!("trait {}", item_path_str(cx, inner.principal.def_id())) @@ -5884,8 +5917,9 @@ fn helper<'tcx>(tcx: &ctxt<'tcx>, ty: Ty<'tcx>, svh: &Svh, state: &mut sip::SipS region(state, r); mt(state, m); } - ty_bare_fn(ref b) => { + ty_bare_fn(opt_def_id, ref b) => { byte!(14); + hash!(opt_def_id); hash!(b.unsafety); hash!(b.abi); fn_sig(state, &b.sig); @@ -6252,6 +6286,7 @@ impl<'tcx> AutoAdjustment<'tcx> { pub fn is_identity(&self) -> bool { match *self { AdjustAddEnv(..) => false, + AdjustReifyFnPointer(..) => false, AdjustDerefRef(ref r) => r.is_identity(), } } @@ -6367,8 +6402,11 @@ pub fn shifted(&self, amount: uint) -> DebruijnIndex { impl<'tcx> Repr<'tcx> for AutoAdjustment<'tcx> { fn repr(&self, tcx: &ctxt<'tcx>) -> String { match *self { - AdjustAddEnv(ref trait_store) => { - format!("AdjustAddEnv({})", trait_store) + AdjustAddEnv(def_id, ref trait_store) => { + format!("AdjustAddEnv({},{})", def_id.repr(tcx), trait_store) + } + AdjustReifyFnPointer(def_id) => { + format!("AdjustAddEnv({})", def_id.repr(tcx)) } AdjustDerefRef(ref data) => { data.repr(tcx) diff --git a/src/librustc/middle/ty_fold.rs b/src/librustc/middle/ty_fold.rs index d69ae96d07ec..a35ea30b2179 100644 --- a/src/librustc/middle/ty_fold.rs +++ b/src/librustc/middle/ty_fold.rs @@ -538,8 +538,8 @@ pub fn super_fold_ty<'tcx, T: TypeFolder<'tcx>>(this: &mut T, ty::ty_tup(ref ts) => { ty::ty_tup(ts.fold_with(this)) } - ty::ty_bare_fn(ref f) => { - ty::ty_bare_fn(f.fold_with(this)) + ty::ty_bare_fn(opt_def_id, ref f) => { + ty::ty_bare_fn(opt_def_id, f.fold_with(this)) } ty::ty_closure(ref f) => { ty::ty_closure(box f.fold_with(this)) diff --git a/src/librustc_trans/trans/base.rs b/src/librustc_trans/trans/base.rs index ca1e0d7de721..8b2c7f3a78a2 100644 --- a/src/librustc_trans/trans/base.rs +++ b/src/librustc_trans/trans/base.rs @@ -542,6 +542,7 @@ pub fn get_res_dtor<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, let class_ty = ty::lookup_item_type(tcx, parent_id).ty.subst(tcx, substs); let llty = type_of_dtor(ccx, class_ty); let dtor_ty = ty::mk_ctor_fn(ccx.tcx(), + did, &[glue::get_drop_glue_type(ccx, t)], ty::mk_nil(ccx.tcx())); get_extern_fn(ccx, diff --git a/src/librustc_trans/trans/callee.rs b/src/librustc_trans/trans/callee.rs index 1a753901f7ea..7e7a6dfc48ad 100644 --- a/src/librustc_trans/trans/callee.rs +++ b/src/librustc_trans/trans/callee.rs @@ -18,10 +18,9 @@ pub use self::CalleeData::*; pub use self::CallArgs::*; -use arena::TypedArena; -use back::{abi,link}; +use back::abi; use session; -use llvm::{ValueRef, get_param}; +use llvm::{ValueRef}; use llvm; use metadata::csearch; use middle::def; diff --git a/src/librustc_trans/trans/closure.rs b/src/librustc_trans/trans/closure.rs index d5d954f5a907..bbf8cb1c0702 100644 --- a/src/librustc_trans/trans/closure.rs +++ b/src/librustc_trans/trans/closure.rs @@ -13,7 +13,6 @@ use back::abi; use back::link::mangle_internal_name_by_path_and_seq; use llvm::ValueRef; -use middle::def; use middle::mem_categorization::Typer; use trans::adt; use trans::base::*; @@ -603,21 +602,10 @@ pub fn trans_unboxed_closure<'blk, 'tcx>( pub fn get_wrapper_for_bare_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, closure_ty: Ty<'tcx>, - def: def::Def, + def_id: ast::DefId, fn_ptr: ValueRef, - is_local: bool) -> ValueRef { - - let def_id = match def { - def::DefFn(did, _) | def::DefStaticMethod(did, _) | - def::DefVariant(_, did, _) | def::DefStruct(did) => did, - _ => { - ccx.sess().bug(format!("get_wrapper_for_bare_fn: \ - expected a statically resolved fn, got \ - {}", - def).as_slice()); - } - }; - + is_local: bool) -> ValueRef +{ match ccx.closure_bare_wrapper_cache().borrow().get(&fn_ptr) { Some(&llval) => return llval, None => {} @@ -697,11 +685,11 @@ pub fn get_wrapper_for_bare_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, pub fn make_closure_from_bare_fn<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, closure_ty: Ty<'tcx>, - def: def::Def, + def_id: ast::DefId, fn_ptr: ValueRef) -> DatumBlock<'blk, 'tcx, Expr> { let scratch = rvalue_scratch_datum(bcx, closure_ty, "__adjust"); - let wrapper = get_wrapper_for_bare_fn(bcx.ccx(), closure_ty, def, fn_ptr, true); + let wrapper = get_wrapper_for_bare_fn(bcx.ccx(), closure_ty, def_id, fn_ptr, true); fill_fn_pair(bcx, scratch.val, wrapper, C_null(Type::i8p(bcx.ccx()))); DatumBlock::new(bcx, scratch.to_expr_datum()) diff --git a/src/librustc_trans/trans/consts.rs b/src/librustc_trans/trans/consts.rs index e4f0543b5e70..1125a2aa529e 100644 --- a/src/librustc_trans/trans/consts.rs +++ b/src/librustc_trans/trans/consts.rs @@ -190,16 +190,15 @@ pub fn const_expr<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, e: &ast::Expr) None => { } Some(adj) => { match adj { - ty::AdjustAddEnv(ty::RegionTraitStore(ty::ReStatic, _)) => { - let def = ty::resolve_expr(cx.tcx(), e); + ty::AdjustAddEnv(def_id, ty::RegionTraitStore(ty::ReStatic, _)) => { let wrapper = closure::get_wrapper_for_bare_fn(cx, ety_adjusted, - def, + def_id, llconst, true); llconst = C_struct(cx, &[wrapper, C_null(Type::i8p(cx))], false) } - ty::AdjustAddEnv(store) => { + ty::AdjustAddEnv(_, store) => { cx.sess() .span_bug(e.span, format!("unexpected static function: {}", diff --git a/src/librustc_trans/trans/expr.rs b/src/librustc_trans/trans/expr.rs index 81892e5fa832..3e3252a347da 100644 --- a/src/librustc_trans/trans/expr.rs +++ b/src/librustc_trans/trans/expr.rs @@ -54,7 +54,7 @@ use trans::tvec; use trans::type_of; use middle::ty::{struct_fields, tup_fields}; -use middle::ty::{AdjustDerefRef, AdjustAddEnv, AutoUnsafe}; +use middle::ty::{AdjustDerefRef, AdjustReifyFnPointer, AdjustAddEnv, AutoUnsafe}; use middle::ty::{AutoPtr}; use middle::ty::{mod, Ty}; use middle::ty::MethodCall; @@ -177,8 +177,9 @@ fn apply_adjustments<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, datum.to_string(bcx.ccx()), adjustment.repr(bcx.tcx())); match adjustment { - AdjustAddEnv(..) => { - datum = unpack_datum!(bcx, add_env(bcx, expr, datum)); + AdjustAddEnv(def_id, _) => { + datum = unpack_datum!(bcx, add_env(bcx, def_id, expr, datum)); + } } AdjustDerefRef(ref adj) => { let (autoderefs, use_autoref) = match adj.autoref { @@ -466,6 +467,7 @@ fn unsize_unique_expr<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, } fn add_env<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, + def_id: ast::DefId, expr: &ast::Expr, datum: Datum<'tcx, Expr>) -> DatumBlock<'blk, 'tcx, Expr> { @@ -477,8 +479,7 @@ fn add_env<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, let closure_ty = expr_ty_adjusted(bcx, expr); let fn_ptr = datum.to_llscalarish(bcx); - let def = ty::resolve_expr(bcx.tcx(), expr); - closure::make_closure_from_bare_fn(bcx, closure_ty, def, fn_ptr) + closure::make_closure_from_bare_fn(bcx, closure_ty, def_id, fn_ptr) } } diff --git a/src/librustc_trans/trans/glue.rs b/src/librustc_trans/trans/glue.rs index dea095ecaf59..e3c1e4b12bfd 100644 --- a/src/librustc_trans/trans/glue.rs +++ b/src/librustc_trans/trans/glue.rs @@ -289,6 +289,7 @@ fn trans_struct_drop<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, } let dtor_ty = ty::mk_ctor_fn(bcx.tcx(), + class_did, &[get_drop_glue_type(bcx.ccx(), t)], ty::mk_nil(bcx.tcx())); let (_, variant_cx) = invoke(variant_cx, dtor_addr, args[], dtor_ty, None, false); diff --git a/src/librustc_trans/trans/meth.rs b/src/librustc_trans/trans/meth.rs index 15f6d7bc3f42..de7adda6bfd7 100644 --- a/src/librustc_trans/trans/meth.rs +++ b/src/librustc_trans/trans/meth.rs @@ -639,7 +639,8 @@ fn emit_vtable_methods<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, m.repr(tcx), substs.repr(tcx)); if m.generics.has_type_params(subst::FnSpace) || - ty::type_has_self(ty::mk_bare_fn(tcx, m.fty.clone())) { + ty::type_has_self(ty::mk_bare_fn(tcx, None, m.fty.clone())) + { debug!("(making impl vtable) method has self or type \ params: {}", token::get_name(name)); diff --git a/src/librustc_typeck/astconv.rs b/src/librustc_typeck/astconv.rs index 175763c874ef..cd711deabed5 100644 --- a/src/librustc_typeck/astconv.rs +++ b/src/librustc_typeck/astconv.rs @@ -954,7 +954,7 @@ pub fn ast_ty_to_ty<'tcx, AC: AstConv<'tcx>, RS: RegionScope>( tcx.sess.span_err(ast_ty.span, "variadic function must have C calling convention"); } - ty::mk_bare_fn(tcx, ty_of_bare_fn(this, bf.unsafety, bf.abi, &*bf.decl)) + ty::mk_bare_fn(tcx, None, ty_of_bare_fn(this, bf.unsafety, bf.abi, &*bf.decl)) } ast::TyClosure(ref f) => { // Use corresponding trait store to figure out default bounds diff --git a/src/librustc_typeck/check/method/confirm.rs b/src/librustc_typeck/check/method/confirm.rs index 2c220f298262..10801563f61d 100644 --- a/src/librustc_typeck/check/method/confirm.rs +++ b/src/librustc_typeck/check/method/confirm.rs @@ -113,7 +113,7 @@ fn confirm(&mut self, self.add_obligations(&pick, &method_bounds_substs, &method_bounds); // Create the final `MethodCallee`. - let fty = ty::mk_bare_fn(self.tcx(), ty::BareFnTy { + let fty = ty::mk_bare_fn(self.tcx(), None, ty::BareFnTy { sig: ty::Binder(method_sig), unsafety: pick.method_ty.fty.unsafety, abi: pick.method_ty.fty.abi.clone(), diff --git a/src/librustc_typeck/check/method/mod.rs b/src/librustc_typeck/check/method/mod.rs index 3b7eb22e56cc..af5434e94fe9 100644 --- a/src/librustc_typeck/check/method/mod.rs +++ b/src/librustc_typeck/check/method/mod.rs @@ -199,7 +199,7 @@ pub fn lookup_in_trait_adjusted<'a, 'tcx>(fcx: &'a FnCtxt<'a, 'tcx>, infer::FnCall, &fn_sig).0; let transformed_self_ty = fn_sig.inputs[0]; - let fty = ty::mk_bare_fn(tcx, ty::BareFnTy { + let fty = ty::mk_bare_fn(tcx, None, ty::BareFnTy { sig: ty::Binder(fn_sig), unsafety: bare_fn_ty.unsafety, abi: bare_fn_ty.abi.clone(), diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index cd9a09efe082..7078f1e58c75 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -1132,9 +1132,9 @@ fn compare_impl_method<'tcx>(tcx: &ty::ctxt<'tcx>, } // Compute skolemized form of impl and trait method tys. - let impl_fty = ty::mk_bare_fn(tcx, impl_m.fty.clone()); + let impl_fty = ty::mk_bare_fn(tcx, None, impl_m.fty.clone()); let impl_fty = impl_fty.subst(tcx, &impl_to_skol_substs); - let trait_fty = ty::mk_bare_fn(tcx, trait_m.fty.clone()); + let trait_fty = ty::mk_bare_fn(tcx, None, trait_m.fty.clone()); let trait_fty = trait_fty.subst(tcx, &trait_to_skol_substs); // Check the impl method type IM is a subtype of the trait method @@ -1389,6 +1389,8 @@ fn check_cast(fcx: &FnCtxt, }, t_e, None); } + let t_e_is_bare_fn_item = ty::type_is_bare_fn_item(t_e); + let t_1_is_scalar = ty::type_is_scalar(t_1); let t_1_is_char = ty::type_is_char(t_1); let t_1_is_bare_fn = ty::type_is_bare_fn(t_1); @@ -1396,7 +1398,9 @@ fn check_cast(fcx: &FnCtxt, // casts to scalars other than `char` and `bare fn` are trivial let t_1_is_trivial = t_1_is_scalar && !t_1_is_char && !t_1_is_bare_fn; - if ty::type_is_c_like_enum(fcx.tcx(), t_e) && t_1_is_trivial { + if t_e_is_bare_fn_item && t_1_is_bare_fn { + demand::coerce(fcx, e.span, t_1, &*e); + } else if ty::type_is_c_like_enum(fcx.tcx(), t_e) && t_1_is_trivial { if t_1_is_float || ty::type_is_unsafe_ptr(t_1) { fcx.type_error_message(span, |actual| { format!("illegal cast; cast through an \ @@ -1634,7 +1638,9 @@ fn register_adjustment_obligations(&self, span: Span, adj: &ty::AutoAdjustment<'tcx>) { match *adj { - ty::AdjustAddEnv(..) => { } + ty::AdjustAddEnv(..) | + ty::AdjustReifyFnPointer(..) => { + } ty::AdjustDerefRef(ref d_r) => { match d_r.autoref { Some(ref a_r) => { @@ -5627,7 +5633,7 @@ fn param<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>, n: uint) -> Ty<'tcx> { }; (n_tps, inputs, ty::FnConverging(output)) }; - let fty = ty::mk_bare_fn(tcx, ty::BareFnTy { + let fty = ty::mk_bare_fn(tcx, None, ty::BareFnTy { unsafety: ast::Unsafety::Unsafe, abi: abi::RustIntrinsic, sig: ty::Binder(FnSig { diff --git a/src/librustc_typeck/check/writeback.rs b/src/librustc_typeck/check/writeback.rs index 700d12116060..b123d97d8970 100644 --- a/src/librustc_typeck/check/writeback.rs +++ b/src/librustc_typeck/check/writeback.rs @@ -15,7 +15,6 @@ use astconv::AstConv; use check::FnCtxt; -use middle::def; use middle::pat_util; use middle::ty::{mod, Ty, MethodCall, MethodCallee}; use middle::ty_fold::{TypeFolder,TypeFoldable}; @@ -267,25 +266,12 @@ fn visit_adjustments(&self, reason: ResolveReason, id: ast::NodeId) { Some(adjustment) => { let adj_object = ty::adjust_is_object(&adjustment); let resolved_adjustment = match adjustment { - ty::AdjustAddEnv(store) => { - // FIXME(eddyb) #2190 Allow only statically resolved - // bare functions to coerce to a closure to avoid - // constructing (slower) indirect call wrappers. - match self.tcx().def_map.borrow().get(&id) { - Some(&def::DefFn(..)) | - Some(&def::DefStaticMethod(..)) | - Some(&def::DefVariant(..)) | - Some(&def::DefStruct(_)) => { - } - _ => { - span_err!(self.tcx().sess, reason.span(self.tcx()), E0100, - "cannot coerce non-statically resolved bare fn to closure"); - span_help!(self.tcx().sess, reason.span(self.tcx()), - "consider embedding the function in a closure"); - } - } + ty::AdjustAddEnv(def_id, store) => { + ty::AdjustAddEnv(def_id, self.resolve(&store, reason)) + } - ty::AdjustAddEnv(self.resolve(&store, reason)) + ty::AdjustReifyFnPointer(def_id) => { + ty::AdjustReifyFnPointer(def_id) } ty::AdjustDerefRef(adj) => { diff --git a/src/librustc_typeck/coherence/mod.rs b/src/librustc_typeck/coherence/mod.rs index 5d0bb6622c2e..bba6af958459 100644 --- a/src/librustc_typeck/coherence/mod.rs +++ b/src/librustc_typeck/coherence/mod.rs @@ -235,7 +235,7 @@ fn instantiate_default_methods( // impl, plus its own. let new_polytype = ty::Polytype { generics: new_method_ty.generics.clone(), - ty: ty::mk_bare_fn(tcx, new_method_ty.fty.clone()) + ty: ty::mk_bare_fn(tcx, Some(new_did), new_method_ty.fty.clone()) }; debug!("new_polytype={}", new_polytype.repr(tcx)); diff --git a/src/librustc_typeck/collect.rs b/src/librustc_typeck/collect.rs index 3f59b50337fa..af996da5543d 100644 --- a/src/librustc_typeck/collect.rs +++ b/src/librustc_typeck/collect.rs @@ -211,13 +211,15 @@ pub fn get_enum_variant_types<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>, // Create a set of parameter types shared among all the variants. for variant in variants.iter() { + let variant_def_id = local_def(variant.node.id); + // Nullary enum constructors get turned into constants; n-ary enum // constructors get turned into functions. let result_ty = match variant.node.kind { ast::TupleVariantKind(ref args) if args.len() > 0 => { let rs = ExplicitRscope; let input_tys: Vec<_> = args.iter().map(|va| ccx.to_ty(&rs, &*va.ty)).collect(); - ty::mk_ctor_fn(tcx, input_tys.as_slice(), enum_ty) + ty::mk_ctor_fn(tcx, variant_def_id, input_tys.as_slice(), enum_ty) } ast::TupleVariantKind(_) => { @@ -246,7 +248,7 @@ pub fn get_enum_variant_types<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>, ty: result_ty }; - tcx.tcache.borrow_mut().insert(local_def(variant.node.id), pty); + tcx.tcache.borrow_mut().insert(variant_def_id, pty); write_ty_to_tcx(tcx, variant.node.id, result_ty); } @@ -353,7 +355,7 @@ fn make_method_ty<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>, m: &ty::Method<'tcx>) { m.def_id, Polytype { generics: m.generics.clone(), - ty: ty::mk_bare_fn(ccx.tcx, m.fty.clone()) }); + ty: ty::mk_bare_fn(ccx.tcx, Some(m.def_id), m.fty.clone()) }); } fn ty_method_of_trait_method<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>, @@ -519,6 +521,7 @@ fn convert_methods<'a,'tcx,'i,I>(ccx: &CrateCtxt<'a, 'tcx>, tcx.sess.span_err(m.span, "duplicate method in trait impl"); } + let m_def_id = local_def(m.id); let mty = Rc::new(ty_of_method(ccx, convert_method_context, container, @@ -526,13 +529,13 @@ fn convert_methods<'a,'tcx,'i,I>(ccx: &CrateCtxt<'a, 'tcx>, untransformed_rcvr_ty, rcvr_ty_generics, rcvr_visibility)); - let fty = ty::mk_bare_fn(tcx, mty.fty.clone()); + let fty = ty::mk_bare_fn(tcx, Some(m_def_id), mty.fty.clone()); debug!("method {} (id {}) has type {}", m.pe_ident().repr(tcx), m.id, fty.repr(tcx)); tcx.tcache.borrow_mut().insert( - local_def(m.id), + m_def_id, Polytype { generics: mty.generics.clone(), ty: fty @@ -1279,6 +1282,7 @@ pub fn convert_struct<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>, |field| (*tcx.tcache.borrow())[ local_def(field.node.id)].ty).collect(); let ctor_fn_ty = ty::mk_ctor_fn(tcx, + local_def(ctor_id), inputs.as_slice(), selfty); write_ty_to_tcx(tcx, ctor_id, ctor_fn_ty); @@ -1461,7 +1465,7 @@ pub fn ty_of_item<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>, it: &ast::Item) }; let pty = Polytype { generics: ty_generics, - ty: ty::mk_bare_fn(ccx.tcx, tofd) + ty: ty::mk_bare_fn(ccx.tcx, Some(local_def(it.id)), tofd) }; debug!("type of {} (id {}) is {}", token::get_ident(it.ident), @@ -2138,6 +2142,7 @@ pub fn ty_of_foreign_fn_decl<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>, let t_fn = ty::mk_bare_fn( ccx.tcx, + None, ty::BareFnTy { abi: abi, unsafety: ast::Unsafety::Unsafe, diff --git a/src/librustc_typeck/lib.rs b/src/librustc_typeck/lib.rs index 49c5f13fa739..c30293e4765c 100644 --- a/src/librustc_typeck/lib.rs +++ b/src/librustc_typeck/lib.rs @@ -102,6 +102,7 @@ use syntax::codemap::Span; use syntax::print::pprust::*; use syntax::{ast, ast_map, abi}; +use syntax::ast_util::local_def; #[cfg(stage0)] mod diagnostics; @@ -224,7 +225,7 @@ fn check_main_fn_ty(ccx: &CrateCtxt, } _ => () } - let se_ty = ty::mk_bare_fn(tcx, ty::BareFnTy { + let se_ty = ty::mk_bare_fn(tcx, Some(local_def(main_id)), ty::BareFnTy { unsafety: ast::Unsafety::Normal, abi: abi::Rust, sig: ty::Binder(ty::FnSig { @@ -272,7 +273,7 @@ fn check_start_fn_ty(ccx: &CrateCtxt, _ => () } - let se_ty = ty::mk_bare_fn(tcx, ty::BareFnTy { + let se_ty = ty::mk_bare_fn(tcx, Some(local_def(start_id)), ty::BareFnTy { unsafety: ast::Unsafety::Normal, abi: abi::Rust, sig: ty::Binder(ty::FnSig { From 2a43b352f71a55ded2b3b17680d59674ee9e4e79 Mon Sep 17 00:00:00 2001 From: Niko Matsakis Date: Wed, 26 Nov 2014 06:01:28 -0500 Subject: [PATCH 27/44] Rote changes that don't care to distinguish between a fn pointer and a fn item. --- src/librustc/metadata/decoder.rs | 2 +- src/librustc/middle/effect.rs | 2 +- src/librustc/middle/fast_reject.rs | 2 +- src/librustc/middle/intrinsicck.rs | 4 ++-- src/librustc/middle/traits/select.rs | 6 +++--- src/librustc/middle/ty.rs | 20 ++++++++++---------- src/librustc/util/ppaux.rs | 2 +- src/librustc_driver/test.rs | 13 ++++++++++++- src/librustc_trans/trans/base.rs | 14 +++++++------- src/librustc_trans/trans/callee.rs | 16 ++++++++++------ src/librustc_trans/trans/debuginfo.rs | 6 +++--- src/librustc_trans/trans/foreign.rs | 10 +++++----- src/librustc_trans/trans/glue.rs | 2 +- src/librustc_trans/trans/intrinsic.rs | 2 +- src/librustc_trans/trans/meth.rs | 2 +- src/librustc_trans/trans/type_of.rs | 2 +- src/librustc_typeck/check/method/confirm.rs | 2 +- src/librustc_typeck/check/mod.rs | 8 ++++---- src/librustc_typeck/check/wf.rs | 2 +- src/librustc_typeck/lib.rs | 2 +- src/librustc_typeck/variance.rs | 5 +++-- src/librustdoc/clean/inline.rs | 2 +- src/librustdoc/clean/mod.rs | 2 +- 23 files changed, 72 insertions(+), 56 deletions(-) diff --git a/src/librustc/metadata/decoder.rs b/src/librustc/metadata/decoder.rs index d8168814c6cd..9d9b8780bbfb 100644 --- a/src/librustc/metadata/decoder.rs +++ b/src/librustc/metadata/decoder.rs @@ -700,7 +700,7 @@ pub fn get_enum_variants<'tcx>(intr: Rc, cdata: Cmd, id: ast::Nod item, tcx, cdata); let name = item_name(&*intr, item); let (ctor_ty, arg_tys, arg_names) = match ctor_ty.sty { - ty::ty_bare_fn(ref f) => + ty::ty_bare_fn(_, ref f) => (Some(ctor_ty), f.sig.0.inputs.clone(), None), _ => { // Nullary or struct enum variant. let mut arg_names = Vec::new(); diff --git a/src/librustc/middle/effect.rs b/src/librustc/middle/effect.rs index 0c0cba6e53e0..52899aaba412 100644 --- a/src/librustc/middle/effect.rs +++ b/src/librustc/middle/effect.rs @@ -32,7 +32,7 @@ enum UnsafeContext { fn type_is_unsafe_function(ty: Ty) -> bool { match ty.sty { - ty::ty_bare_fn(ref f) => f.unsafety == ast::Unsafety::Unsafe, + ty::ty_bare_fn(_, ref f) => f.unsafety == ast::Unsafety::Unsafe, ty::ty_closure(ref f) => f.unsafety == ast::Unsafety::Unsafe, _ => false, } diff --git a/src/librustc/middle/fast_reject.rs b/src/librustc/middle/fast_reject.rs index 297d6bcb03cb..62cf47da6870 100644 --- a/src/librustc/middle/fast_reject.rs +++ b/src/librustc/middle/fast_reject.rs @@ -83,7 +83,7 @@ pub fn simplify_type(tcx: &ty::ctxt, ty::ty_closure(ref f) => { Some(FunctionSimplifiedType(f.sig.0.inputs.len())) } - ty::ty_bare_fn(ref f) => { + ty::ty_bare_fn(_, ref f) => { Some(FunctionSimplifiedType(f.sig.0.inputs.len())) } ty::ty_param(_) => { diff --git a/src/librustc/middle/intrinsicck.rs b/src/librustc/middle/intrinsicck.rs index ea19111ce3d6..6acbc98b4b27 100644 --- a/src/librustc/middle/intrinsicck.rs +++ b/src/librustc/middle/intrinsicck.rs @@ -74,7 +74,7 @@ struct IntrinsicCheckingVisitor<'a, 'tcx: 'a> { impl<'a, 'tcx> IntrinsicCheckingVisitor<'a, 'tcx> { fn def_id_is_transmute(&self, def_id: DefId) -> bool { let intrinsic = match ty::lookup_item_type(self.tcx, def_id).ty.sty { - ty::ty_bare_fn(ref bfty) => bfty.abi == RustIntrinsic, + ty::ty_bare_fn(_, ref bfty) => bfty.abi == RustIntrinsic, _ => return false }; if def_id.krate == ast::LOCAL_CRATE { @@ -123,7 +123,7 @@ fn visit_expr(&mut self, expr: &ast::Expr) { DefFn(did, _) if self.def_id_is_transmute(did) => { let typ = ty::node_id_to_type(self.tcx, expr.id); match typ.sty { - ty_bare_fn(ref bare_fn_ty) if bare_fn_ty.abi == RustIntrinsic => { + ty_bare_fn(_, ref bare_fn_ty) if bare_fn_ty.abi == RustIntrinsic => { if let ty::FnConverging(to) = bare_fn_ty.sig.0.output { let from = bare_fn_ty.sig.0.inputs[0]; self.check_transmute(expr.span, from, to, expr.id); diff --git a/src/librustc/middle/traits/select.rs b/src/librustc/middle/traits/select.rs index 8ba28b61006b..77d7aac4d09f 100644 --- a/src/librustc/middle/traits/select.rs +++ b/src/librustc/middle/traits/select.rs @@ -795,7 +795,7 @@ fn assemble_fn_pointer_candidates(&mut self, } // provide an impl, but only for suitable `fn` pointers - ty::ty_bare_fn(ty::BareFnTy { + ty::ty_bare_fn(_, ty::BareFnTy { unsafety: ast::Unsafety::Normal, abi: abi::Rust, sig: ty::Binder(ty::FnSig { @@ -984,7 +984,7 @@ fn builtin_bound(&mut self, ty::ty_int(_) | ty::ty_bool | ty::ty_float(_) | - ty::ty_bare_fn(_) | + ty::ty_bare_fn(..) | ty::ty_char => { // safe for everything Ok(If(Vec::new())) @@ -1543,7 +1543,7 @@ fn confirm_fn_pointer_candidate(&mut self, let self_ty = self.infcx.shallow_resolve(obligation.self_ty()); let sig = match self_ty.sty { - ty::ty_bare_fn(ty::BareFnTy { + ty::ty_bare_fn(_, ty::BareFnTy { unsafety: ast::Unsafety::Normal, abi: abi::Rust, ref sig diff --git a/src/librustc/middle/ty.rs b/src/librustc/middle/ty.rs index 4130d07aed01..f05404759c4e 100644 --- a/src/librustc/middle/ty.rs +++ b/src/librustc/middle/ty.rs @@ -2185,7 +2185,7 @@ fn add_sty(&mut self, st: &sty) { self.add_tys(ts[]); } - &ty_bare_fn(ref f) => { + &ty_bare_fn(_, ref f) => { self.add_fn_sig(&f.sig); } @@ -2457,7 +2457,7 @@ pub fn maybe_walk_ty<'tcx>(ty: Ty<'tcx>, f: |Ty<'tcx>| -> bool) { } } ty_tup(ref ts) => { for tt in ts.iter() { maybe_walk_ty(*tt, |x| f(x)); } } - ty_bare_fn(ref ft) => { + ty_bare_fn(_, ref ft) => { for a in ft.sig.0.inputs.iter() { maybe_walk_ty(*a, |x| f(x)); } if let ty::FnConverging(output) = ft.sig.0.output { maybe_walk_ty(output, f); @@ -2940,7 +2940,7 @@ fn tc_ty<'tcx>(cx: &ctxt<'tcx>, // Scalar and unique types are sendable, and durable ty_infer(ty::FreshIntTy(_)) | ty_bool | ty_int(_) | ty_uint(_) | ty_float(_) | - ty_bare_fn(_) | ty::ty_char => { + ty_bare_fn(..) | ty::ty_char => { TC::None } @@ -3275,7 +3275,7 @@ fn subtypes_require<'tcx>(cx: &ctxt<'tcx>, seen: &mut Vec, ty_uint(_) | ty_float(_) | ty_str | - ty_bare_fn(_) | + ty_bare_fn(..) | ty_closure(_) | ty_infer(_) | ty_err | @@ -3810,7 +3810,7 @@ pub fn node_id_item_substs<'tcx>(cx: &ctxt<'tcx>, id: ast::NodeId) -> ItemSubsts pub fn fn_is_variadic(fty: Ty) -> bool { match fty.sty { - ty_bare_fn(ref f) => f.sig.0.variadic, + ty_bare_fn(_, ref f) => f.sig.0.variadic, ty_closure(ref f) => f.sig.0.variadic, ref s => { panic!("fn_is_variadic() called on non-fn type: {}", s) @@ -3820,7 +3820,7 @@ pub fn fn_is_variadic(fty: Ty) -> bool { pub fn ty_fn_sig<'tcx>(fty: Ty<'tcx>) -> &'tcx PolyFnSig<'tcx> { match fty.sty { - ty_bare_fn(ref f) => &f.sig, + ty_bare_fn(_, ref f) => &f.sig, ty_closure(ref f) => &f.sig, ref s => { panic!("ty_fn_sig() called on non-fn type: {}", s) @@ -3831,7 +3831,7 @@ pub fn ty_fn_sig<'tcx>(fty: Ty<'tcx>) -> &'tcx PolyFnSig<'tcx> { /// Returns the ABI of the given function. pub fn ty_fn_abi(fty: Ty) -> abi::Abi { match fty.sty { - ty_bare_fn(ref f) => f.abi, + ty_bare_fn(_, ref f) => f.abi, ty_closure(ref f) => f.abi, _ => panic!("ty_fn_abi() called on non-fn type"), } @@ -3858,7 +3858,7 @@ pub fn ty_closure_store(fty: Ty) -> TraitStore { pub fn ty_fn_ret<'tcx>(fty: Ty<'tcx>) -> FnOutput<'tcx> { match fty.sty { - ty_bare_fn(ref f) => f.sig.0.output, + ty_bare_fn(_, ref f) => f.sig.0.output, ty_closure(ref f) => f.sig.0.output, ref s => { panic!("ty_fn_ret() called on non-fn type: {}", s) @@ -3868,7 +3868,7 @@ pub fn ty_fn_ret<'tcx>(fty: Ty<'tcx>) -> FnOutput<'tcx> { pub fn is_fn_ty(fty: Ty) -> bool { match fty.sty { - ty_bare_fn(_) => true, + ty_bare_fn(..) => true, ty_closure(_) => true, _ => false } @@ -6234,7 +6234,7 @@ pub fn accumulate_lifetimes_in_type(accumulator: &mut Vec, ty_str | ty_vec(_, _) | ty_ptr(_) | - ty_bare_fn(_) | + ty_bare_fn(..) | ty_tup(_) | ty_param(_) | ty_infer(_) | diff --git a/src/librustc/util/ppaux.rs b/src/librustc/util/ppaux.rs index 85a06125e23a..daa7c13e3b31 100644 --- a/src/librustc/util/ppaux.rs +++ b/src/librustc/util/ppaux.rs @@ -408,7 +408,7 @@ fn infer_ty_to_string(cx: &ctxt, ty: ty::InferTy) -> String { ty_closure(ref f) => { closure_to_string(cx, &**f) } - ty_bare_fn(ref f) => { + ty_bare_fn(_, ref f) => { bare_fn_to_string(cx, f.unsafety, f.abi, None, &f.sig) } ty_infer(infer_ty) => infer_ty_to_string(cx, infer_ty), diff --git a/src/librustc_driver/test.rs b/src/librustc_driver/test.rs index 090d6a7a3cae..526bbca8d70d 100644 --- a/src/librustc_driver/test.rs +++ b/src/librustc_driver/test.rs @@ -253,7 +253,18 @@ pub fn t_fn(&self, output_ty: Ty<'tcx>) -> Ty<'tcx> { - ty::mk_ctor_fn(self.infcx.tcx, input_tys, output_ty) + let input_args = input_tys.iter().map(|ty| *ty).collect(); + ty::mk_bare_fn(self.infcx.tcx, + None, + ty::BareFnTy { + unsafety: ast::Unsafety::Normal, + abi: abi::Rust, + sig: ty::Binder(ty::FnSig { + inputs: input_args, + output: ty::FnConverging(output_ty), + variadic: false + }) + }) } pub fn t_nil(&self) -> Ty<'tcx> { diff --git a/src/librustc_trans/trans/base.rs b/src/librustc_trans/trans/base.rs index 8b2c7f3a78a2..3be40aed57ac 100644 --- a/src/librustc_trans/trans/base.rs +++ b/src/librustc_trans/trans/base.rs @@ -282,7 +282,7 @@ pub fn kind_for_unboxed_closure(ccx: &CrateContext, closure_id: ast::DefId) pub fn decl_rust_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, fn_ty: Ty<'tcx>, name: &str) -> ValueRef { let (inputs, output, abi, env) = match fn_ty.sty { - ty::ty_bare_fn(ref f) => { + ty::ty_bare_fn(_, ref f) => { (f.sig.0.inputs.clone(), f.sig.0.output, f.abi, None) } ty::ty_closure(ref f) => { @@ -956,7 +956,7 @@ pub fn trans_external_path<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, did: ast::DefId, t: Ty<'tcx>) -> ValueRef { let name = csearch::get_symbol(&ccx.sess().cstore, did); match t.sty { - ty::ty_bare_fn(ref fn_ty) => { + ty::ty_bare_fn(_, ref fn_ty) => { match ccx.sess().target.target.adjust_abi(fn_ty.abi) { Rust | RustCall => { get_extern_rust_fn(ccx, t, name.as_slice(), did) @@ -2015,7 +2015,7 @@ pub fn trans_named_tuple_constructor<'blk, 'tcx>(mut bcx: Block<'blk, 'tcx>, let tcx = ccx.tcx(); let result_ty = match ctor_ty.sty { - ty::ty_bare_fn(ref bft) => bft.sig.0.output.unwrap(), + ty::ty_bare_fn(_, ref bft) => bft.sig.0.output.unwrap(), _ => ccx.sess().bug( format!("trans_enum_variant_constructor: \ unexpected ctor return type {}", @@ -2087,7 +2087,7 @@ fn trans_enum_variant_or_tuple_like_struct<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx let ctor_ty = ctor_ty.subst(ccx.tcx(), param_substs); let result_ty = match ctor_ty.sty { - ty::ty_bare_fn(ref bft) => bft.sig.0.output, + ty::ty_bare_fn(_, ref bft) => bft.sig.0.output, _ => ccx.sess().bug( format!("trans_enum_variant_or_tuple_like_struct: \ unexpected ctor return type {}", @@ -2422,7 +2422,7 @@ fn register_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, node_type: Ty<'tcx>) -> ValueRef { match node_type.sty { - ty::ty_bare_fn(ref f) => { + ty::ty_bare_fn(_, ref f) => { assert!(f.abi == Rust || f.abi == RustCall); } _ => panic!("expected bare rust fn") @@ -2439,7 +2439,7 @@ pub fn get_fn_llvm_attributes<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, fn_ty: Ty< let (fn_sig, abi, has_env) = match fn_ty.sty { ty::ty_closure(ref f) => (f.sig.clone(), f.abi, true), - ty::ty_bare_fn(ref f) => (f.sig.clone(), f.abi, false), + ty::ty_bare_fn(_, ref f) => (f.sig.clone(), f.abi, false), ty::ty_unboxed_closure(closure_did, _, ref substs) => { let unboxed_closures = ccx.tcx().unboxed_closures.borrow(); let ref function_type = (*unboxed_closures)[closure_did] @@ -2468,7 +2468,7 @@ pub fn get_fn_llvm_attributes<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, fn_ty: Ty< _ => ccx.sess().bug("expected tuple'd inputs") } }, - ty::ty_bare_fn(_) if abi == RustCall => { + ty::ty_bare_fn(..) if abi == RustCall => { let mut inputs = vec![fn_sig.0.inputs[0]]; match fn_sig.0.inputs[1].sty { diff --git a/src/librustc_trans/trans/callee.rs b/src/librustc_trans/trans/callee.rs index 7e7a6dfc48ad..b60c75d305ca 100644 --- a/src/librustc_trans/trans/callee.rs +++ b/src/librustc_trans/trans/callee.rs @@ -18,9 +18,11 @@ pub use self::CalleeData::*; pub use self::CallArgs::*; -use back::abi; +use arena::TypedArena; +use back::{abi,link}; use session; use llvm::{ValueRef}; +use llvm::get_param; use llvm; use metadata::csearch; use middle::def; @@ -157,7 +159,7 @@ fn trans_def<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, } } def::DefFn(did, _) if match expr_ty.sty { - ty::ty_bare_fn(ref f) => f.abi == synabi::RustIntrinsic, + ty::ty_bare_fn(_, ref f) => f.abi == synabi::RustIntrinsic, _ => false } => { let substs = node_id_substs(bcx, ExprId(ref_expr.id)); @@ -274,15 +276,16 @@ pub fn trans_fn_pointer_shim<'a, 'tcx>( // Construct the "tuply" version of `bare_fn_ty`. It takes two arguments: `self`, // which is the fn pointer, and `args`, which is the arguments tuple. - let (input_tys, output_ty) = + let (opt_def_id, input_tys, output_ty) = match bare_fn_ty.sty { - ty::ty_bare_fn(ty::BareFnTy { unsafety: ast::Unsafety::Normal, + ty::ty_bare_fn(opt_def_id, + ty::BareFnTy { unsafety: ast::Unsafety::Normal, abi: synabi::Rust, sig: ty::Binder(ty::FnSig { inputs: ref input_tys, output: output_ty, variadic: false })}) => { - (input_tys, output_ty) + (opt_def_id, input_tys, output_ty) } _ => { @@ -292,6 +295,7 @@ pub fn trans_fn_pointer_shim<'a, 'tcx>( }; let tuple_input_ty = ty::mk_tup(tcx, input_tys.to_vec()); let tuple_fn_ty = ty::mk_bare_fn(tcx, + opt_def_id, ty::BareFnTy { unsafety: ast::Unsafety::Normal, abi: synabi::RustCall, sig: ty::Binder(ty::FnSig { @@ -654,7 +658,7 @@ pub fn trans_call_inner<'a, 'blk, 'tcx, F>(bcx: Block<'blk, 'tcx>, let mut bcx = callee.bcx; let (abi, ret_ty) = match callee_ty.sty { - ty::ty_bare_fn(ref f) => (f.abi, f.sig.0.output), + ty::ty_bare_fn(_, ref f) => (f.abi, f.sig.0.output), ty::ty_closure(ref f) => (f.abi, f.sig.0.output), _ => panic!("expected bare rust fn or closure in trans_call_inner") }; diff --git a/src/librustc_trans/trans/debuginfo.rs b/src/librustc_trans/trans/debuginfo.rs index 51e3a83f81f5..b03934af016b 100644 --- a/src/librustc_trans/trans/debuginfo.rs +++ b/src/librustc_trans/trans/debuginfo.rs @@ -430,7 +430,7 @@ fn get_unique_type_id_of_type<'a>(&mut self, cx: &CrateContext<'a, 'tcx>, trait_data.principal.substs(), &mut unique_type_id); }, - ty::ty_bare_fn(ty::BareFnTy{ unsafety, abi, ref sig } ) => { + ty::ty_bare_fn(_, ty::BareFnTy{ unsafety, abi, ref sig } ) => { if unsafety == ast::Unsafety::Unsafe { unique_type_id.push_str("unsafe "); } @@ -2997,7 +2997,7 @@ fn type_metadata<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, } } } - ty::ty_bare_fn(ref barefnty) => { + ty::ty_bare_fn(_, ref barefnty) => { subroutine_type_metadata(cx, unique_type_id, &barefnty.sig, usage_site_span) } ty::ty_closure(ref closurety) => { @@ -3814,7 +3814,7 @@ fn push_debuginfo_type_name<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, push_item_name(cx, trait_data.principal.def_id(), false, output); push_type_params(cx, trait_data.principal.substs(), output); }, - ty::ty_bare_fn(ty::BareFnTy{ unsafety, abi, ref sig } ) => { + ty::ty_bare_fn(_, ty::BareFnTy{ unsafety, abi, ref sig } ) => { if unsafety == ast::Unsafety::Unsafe { output.push_str("unsafe "); } diff --git a/src/librustc_trans/trans/foreign.rs b/src/librustc_trans/trans/foreign.rs index d07203199305..e50d645afd8d 100644 --- a/src/librustc_trans/trans/foreign.rs +++ b/src/librustc_trans/trans/foreign.rs @@ -228,7 +228,7 @@ pub fn trans_native_call<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, ccx.tn().val_to_string(llretptr)); let (fn_abi, fn_sig) = match callee_ty.sty { - ty::ty_bare_fn(ref fn_ty) => (fn_ty.abi, fn_ty.sig.clone()), + ty::ty_bare_fn(_, ref fn_ty) => (fn_ty.abi, fn_ty.sig.clone()), _ => ccx.sess().bug("trans_native_call called on non-function type") }; let llsig = foreign_signature(ccx, &fn_sig, passed_arg_tys.as_slice()); @@ -479,7 +479,7 @@ pub fn decl_rust_fn_with_foreign_abi<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, let tys = foreign_types_for_fn_ty(ccx, t); let llfn_ty = lltype_for_fn_from_foreign_types(ccx, &tys); let cconv = match t.sty { - ty::ty_bare_fn(ref fn_ty) => { + ty::ty_bare_fn(_, ref fn_ty) => { llvm_calling_convention(ccx, fn_ty.abi) } _ => panic!("expected bare fn in decl_rust_fn_with_foreign_abi") @@ -502,7 +502,7 @@ pub fn register_rust_fn_with_foreign_abi(ccx: &CrateContext, let llfn_ty = lltype_for_fn_from_foreign_types(ccx, &tys); let t = ty::node_id_to_type(ccx.tcx(), node_id); let cconv = match t.sty { - ty::ty_bare_fn(ref fn_ty) => { + ty::ty_bare_fn(_, ref fn_ty) => { llvm_calling_convention(ccx, fn_ty.abi) } _ => panic!("expected bare fn in register_rust_fn_with_foreign_abi") @@ -556,7 +556,7 @@ fn build_rust_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, // Compute the type that the function would have if it were just a // normal Rust function. This will be the type of the wrappee fn. match t.sty { - ty::ty_bare_fn(ref f) => { + ty::ty_bare_fn(_, ref f) => { assert!(f.abi != Rust && f.abi != RustIntrinsic); } _ => { @@ -849,7 +849,7 @@ fn foreign_types_for_id<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, fn foreign_types_for_fn_ty<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, ty: Ty<'tcx>) -> ForeignTypes<'tcx> { let fn_sig = match ty.sty { - ty::ty_bare_fn(ref fn_ty) => fn_ty.sig.clone(), + ty::ty_bare_fn(_, ref fn_ty) => fn_ty.sig.clone(), _ => ccx.sess().bug("foreign_types_for_fn_ty called on non-function type") }; let llsig = foreign_signature(ccx, &fn_sig, fn_sig.0.inputs.as_slice()); diff --git a/src/librustc_trans/trans/glue.rs b/src/librustc_trans/trans/glue.rs index e3c1e4b12bfd..905ce3c61dce 100644 --- a/src/librustc_trans/trans/glue.rs +++ b/src/librustc_trans/trans/glue.rs @@ -226,7 +226,7 @@ fn trans_struct_drop<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, let fty = ty::lookup_item_type(bcx.tcx(), dtor_did).ty.subst(bcx.tcx(), substs); let self_ty = match fty.sty { - ty::ty_bare_fn(ref f) => { + ty::ty_bare_fn(_, ref f) => { assert!(f.sig.0.inputs.len() == 1); f.sig.0.inputs[0] } diff --git a/src/librustc_trans/trans/intrinsic.rs b/src/librustc_trans/trans/intrinsic.rs index a6f7c849f4d9..f676608bbcc7 100644 --- a/src/librustc_trans/trans/intrinsic.rs +++ b/src/librustc_trans/trans/intrinsic.rs @@ -150,7 +150,7 @@ pub fn trans_intrinsic_call<'a, 'blk, 'tcx>(mut bcx: Block<'blk, 'tcx>, let tcx = bcx.tcx(); let ret_ty = match callee_ty.sty { - ty::ty_bare_fn(ref f) => f.sig.0.output, + ty::ty_bare_fn(_, ref f) => f.sig.0.output, _ => panic!("expected bare_fn in trans_intrinsic_call") }; let foreign_item = tcx.map.expect_foreign_item(node); diff --git a/src/librustc_trans/trans/meth.rs b/src/librustc_trans/trans/meth.rs index de7adda6bfd7..85f819b915c8 100644 --- a/src/librustc_trans/trans/meth.rs +++ b/src/librustc_trans/trans/meth.rs @@ -477,7 +477,7 @@ pub fn trans_trait_callee_from_llval<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, debug!("(translating trait callee) loading method"); // Replace the self type (&Self or Box) with an opaque pointer. let llcallee_ty = match callee_ty.sty { - ty::ty_bare_fn(ref f) if f.abi == Rust || f.abi == RustCall => { + ty::ty_bare_fn(_, ref f) if f.abi == Rust || f.abi == RustCall => { type_of_rust_fn(ccx, Some(Type::i8p(ccx)), f.sig.0.inputs.slice_from(1), diff --git a/src/librustc_trans/trans/type_of.rs b/src/librustc_trans/trans/type_of.rs index 2801e0ccead6..89b728370ca4 100644 --- a/src/librustc_trans/trans/type_of.rs +++ b/src/librustc_trans/trans/type_of.rs @@ -364,7 +364,7 @@ fn type_of_unsize_info<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, t: Ty<'tcx>) -> Ty ty::ty_str => Type::i8(cx), - ty::ty_bare_fn(_) => { + ty::ty_bare_fn(..) => { type_of_fn_from_ty(cx, t).ptr_to() } ty::ty_closure(_) => { diff --git a/src/librustc_typeck/check/method/confirm.rs b/src/librustc_typeck/check/method/confirm.rs index 10801563f61d..d3b518ec2e3a 100644 --- a/src/librustc_typeck/check/method/confirm.rs +++ b/src/librustc_typeck/check/method/confirm.rs @@ -466,7 +466,7 @@ fn add_obligations(&mut self, fn fixup_derefs_on_method_receiver_if_necessary(&self, method_callee: &MethodCallee) { let sig = match method_callee.ty.sty { - ty::ty_bare_fn(ref f) => f.sig.clone(), + ty::ty_bare_fn(_, ref f) => f.sig.clone(), ty::ty_closure(ref f) => f.sig.clone(), _ => return, }; diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index 7078f1e58c75..8198e64a973d 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -399,7 +399,7 @@ fn check_bare_fn<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>, let fty = fty.subst(ccx.tcx, ¶m_env.free_substs); match fty.sty { - ty::ty_bare_fn(ref fn_ty) => { + ty::ty_bare_fn(_, ref fn_ty) => { let inh = Inherited::new(ccx.tcx, param_env); let fcx = check_fn(ccx, fn_ty.unsafety, id, &fn_ty.sig, decl, id, body, &inh); @@ -2049,7 +2049,7 @@ fn try_overloaded_call<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>, // Bail out if the callee is a bare function or a closure. We check those // manually. match structurally_resolved_type(fcx, callee.span, callee_type).sty { - ty::ty_bare_fn(_) | ty::ty_closure(_) => return false, + ty::ty_bare_fn(..) | ty::ty_closure(_) => return false, _ => {} } @@ -2499,7 +2499,7 @@ fn check_method_argument_types<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>, ty::FnConverging(ty::mk_err()) } else { match method_fn_ty.sty { - ty::ty_bare_fn(ref fty) => { + ty::ty_bare_fn(_, ref fty) => { // HACK(eddyb) ignore self in the definition (see above). check_argument_types(fcx, sp, @@ -2927,7 +2927,7 @@ fn check_call(fcx: &FnCtxt, }); let fn_sig = match fn_ty.sty { - ty::ty_bare_fn(ty::BareFnTy {ref sig, ..}) | + ty::ty_bare_fn(_, ty::BareFnTy {ref sig, ..}) | ty::ty_closure(box ty::ClosureTy {ref sig, ..}) => sig, _ => { fcx.type_error_message(call_expr.span, |actual| { diff --git a/src/librustc_typeck/check/wf.rs b/src/librustc_typeck/check/wf.rs index 24d7bf5031e4..a2fb44fff796 100644 --- a/src/librustc_typeck/check/wf.rs +++ b/src/librustc_typeck/check/wf.rs @@ -355,7 +355,7 @@ fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> { self.fold_substs(substs); } - ty::ty_bare_fn(ty::BareFnTy{sig: ref fn_sig, ..}) | + ty::ty_bare_fn(_, ty::BareFnTy{sig: ref fn_sig, ..}) | ty::ty_closure(box ty::ClosureTy{sig: ref fn_sig, ..}) => { self.binding_count += 1; diff --git a/src/librustc_typeck/lib.rs b/src/librustc_typeck/lib.rs index c30293e4765c..368658cb1c51 100644 --- a/src/librustc_typeck/lib.rs +++ b/src/librustc_typeck/lib.rs @@ -257,7 +257,7 @@ fn check_start_fn_ty(ccx: &CrateCtxt, let tcx = ccx.tcx; let start_t = ty::node_id_to_type(tcx, start_id); match start_t.sty { - ty::ty_bare_fn(_) => { + ty::ty_bare_fn(..) => { match tcx.map.find(start_id) { Some(ast_map::NodeItem(it)) => { match it.node { diff --git a/src/librustc_typeck/variance.rs b/src/librustc_typeck/variance.rs index ef0d1bc3859f..e241be3e55c9 100644 --- a/src/librustc_typeck/variance.rs +++ b/src/librustc_typeck/variance.rs @@ -814,12 +814,13 @@ fn add_constraints_from_ty(&mut self, } } - ty::ty_bare_fn(ty::BareFnTy { ref sig, .. }) | + ty::ty_bare_fn(_, ty::BareFnTy { ref sig, .. }) | ty::ty_closure(box ty::ClosureTy { ref sig, store: ty::UniqTraitStore, .. - }) => { + }) => + { self.add_constraints_from_sig(sig, variance); } diff --git a/src/librustdoc/clean/inline.rs b/src/librustdoc/clean/inline.rs index d0988af1cb47..2bc93ade7774 100644 --- a/src/librustdoc/clean/inline.rs +++ b/src/librustdoc/clean/inline.rs @@ -176,7 +176,7 @@ pub fn build_external_trait(cx: &DocContext, tcx: &ty::ctxt, fn build_external_function(cx: &DocContext, tcx: &ty::ctxt, did: ast::DefId) -> clean::Function { let t = ty::lookup_item_type(tcx, did); let (decl, style) = match t.ty.sty { - ty::ty_bare_fn(ref f) => ((did, &f.sig).clean(cx), f.unsafety), + ty::ty_bare_fn(_, ref f) => ((did, &f.sig).clean(cx), f.unsafety), _ => panic!("bad function"), }; clean::Function { diff --git a/src/librustdoc/clean/mod.rs b/src/librustdoc/clean/mod.rs index ac688784f926..6e7a750d8b3f 100644 --- a/src/librustdoc/clean/mod.rs +++ b/src/librustdoc/clean/mod.rs @@ -1360,7 +1360,7 @@ fn clean(&self, cx: &DocContext) -> Type { mutability: mt.mutbl.clean(cx), type_: box mt.ty.clean(cx), }, - ty::ty_bare_fn(ref fty) => BareFunction(box BareFunctionDecl { + ty::ty_bare_fn(_, ref fty) => BareFunction(box BareFunctionDecl { unsafety: fty.unsafety, generics: Generics { lifetimes: Vec::new(), From fad1423d1ef12d3036433432d838723ff783a53d Mon Sep 17 00:00:00 2001 From: Niko Matsakis Date: Wed, 26 Nov 2014 05:53:15 -0500 Subject: [PATCH 28/44] Adjust metadata for new fields and enum variants. Yawn. --- src/librustc/metadata/tydecode.rs | 6 +++++- src/librustc/metadata/tyencode.rs | 7 ++++++- src/librustc/middle/astencode.rs | 25 ++++++++++++++++++++----- 3 files changed, 31 insertions(+), 7 deletions(-) diff --git a/src/librustc/metadata/tydecode.rs b/src/librustc/metadata/tydecode.rs index 9d3a2c1d6677..5b78265a4151 100644 --- a/src/librustc/metadata/tydecode.rs +++ b/src/librustc/metadata/tydecode.rs @@ -453,7 +453,11 @@ fn parse_ty<'a, 'tcx>(st: &mut PState<'a, 'tcx>, conv: conv_did) -> Ty<'tcx> { return ty::mk_closure(st.tcx, parse_closure_ty(st, |x,y| conv(x,y))); } 'F' => { - return ty::mk_bare_fn(st.tcx, parse_bare_fn_ty(st, |x,y| conv(x,y))); + let def_id = parse_def(st, NominalType, |x,y| conv(x,y)); + return ty::mk_bare_fn(st.tcx, Some(def_id), parse_bare_fn_ty(st, |x,y| conv(x,y))); + } + 'G' => { + return ty::mk_bare_fn(st.tcx, None, parse_bare_fn_ty(st, |x,y| conv(x,y))); } '#' => { let pos = parse_hex(st); diff --git a/src/librustc/metadata/tyencode.rs b/src/librustc/metadata/tyencode.rs index ce63c467822d..7fa23620af4b 100644 --- a/src/librustc/metadata/tyencode.rs +++ b/src/librustc/metadata/tyencode.rs @@ -123,8 +123,13 @@ pub fn enc_ty<'a, 'tcx>(w: &mut SeekableMemWriter, cx: &ctxt<'a, 'tcx>, t: Ty<'t mywrite!(w, "f"); enc_closure_ty(w, cx, &**f); } - ty::ty_bare_fn(ref f) => { + ty::ty_bare_fn(Some(def_id), ref f) => { mywrite!(w, "F"); + mywrite!(w, "{}|", (cx.ds)(def_id)); + enc_bare_fn_ty(w, cx, f); + } + ty::ty_bare_fn(None, ref f) => { + mywrite!(w, "G"); enc_bare_fn_ty(w, cx, f); } ty::ty_infer(_) => { diff --git a/src/librustc/middle/astencode.rs b/src/librustc/middle/astencode.rs index 69fbd59fd924..592b509c0b4c 100644 --- a/src/librustc/middle/astencode.rs +++ b/src/librustc/middle/astencode.rs @@ -1007,14 +1007,21 @@ fn emit_auto_adjustment<'b>(&mut self, ecx: &e::EncodeContext<'b, 'tcx>, self.emit_enum("AutoAdjustment", |this| { match *adj { - ty::AdjustAddEnv(store) => { - this.emit_enum_variant("AutoAddEnv", 0, 1, |this| { - this.emit_enum_variant_arg(0, |this| store.encode(this)) + ty::AdjustAddEnv(def_id, store) => { + this.emit_enum_variant("AdjustAddEnv", 0, 2, |this| { + this.emit_enum_variant_arg(0, |this| def_id.encode(this)); + this.emit_enum_variant_arg(1, |this| store.encode(this)) + }) + } + + ty::AdjustReifyFnPointer(def_id) => { + this.emit_enum_variant("AdjustReifyFnPointer", 1, 2, |this| { + this.emit_enum_variant_arg(0, |this| def_id.encode(this)) }) } ty::AdjustDerefRef(ref auto_deref_ref) => { - this.emit_enum_variant("AutoDerefRef", 1, 1, |this| { + this.emit_enum_variant("AdjustDerefRef", 2, 2, |this| { this.emit_enum_variant_arg(0, |this| Ok(this.emit_auto_deref_ref(ecx, auto_deref_ref))) }) @@ -1648,12 +1655,20 @@ fn read_auto_adjustment<'b, 'c>(&mut self, dcx: &DecodeContext<'b, 'c, 'tcx>) this.read_enum_variant(&variants, |this, i| { Ok(match i { 0 => { + let def_id: ast::DefId = + this.read_def_id(dcx); let store: ty::TraitStore = this.read_enum_variant_arg(0, |this| Decodable::decode(this)).unwrap(); - ty::AdjustAddEnv(store.tr(dcx)) + ty::AdjustAddEnv(def_id, store.tr(dcx)) } 1 => { + let def_id: ast::DefId = + this.read_def_id(dcx); + + ty::AdjustReifyFnPointer(def_id) + } + 2 => { let auto_deref_ref: ty::AutoDerefRef = this.read_enum_variant_arg(0, |this| Ok(this.read_auto_deref_ref(dcx))).unwrap(); From 39be95c16ba3d8ed7aa5253f36c5009acb37404b Mon Sep 17 00:00:00 2001 From: Niko Matsakis Date: Wed, 26 Nov 2014 06:01:45 -0500 Subject: [PATCH 29/44] Insert FIXME links to issue #19925: fn item types should be zero-sized. --- src/librustc_trans/trans/consts.rs | 4 ++++ src/librustc_trans/trans/expr.rs | 3 +++ src/librustc_trans/trans/type_of.rs | 4 +++- 3 files changed, 10 insertions(+), 1 deletion(-) diff --git a/src/librustc_trans/trans/consts.rs b/src/librustc_trans/trans/consts.rs index 1125a2aa529e..eff735e4946b 100644 --- a/src/librustc_trans/trans/consts.rs +++ b/src/librustc_trans/trans/consts.rs @@ -204,6 +204,10 @@ pub fn const_expr<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, e: &ast::Expr) format!("unexpected static function: {}", store).as_slice()) } + ty::AdjustReifyFnPointer(_def_id) => { + // FIXME(#19925) once fn item types are + // zero-sized, we'll need to do something here + } ty::AdjustDerefRef(ref adj) => { let mut ty = ety; // Save the last autoderef in case we can avoid it. diff --git a/src/librustc_trans/trans/expr.rs b/src/librustc_trans/trans/expr.rs index 3e3252a347da..536ef3ffd98e 100644 --- a/src/librustc_trans/trans/expr.rs +++ b/src/librustc_trans/trans/expr.rs @@ -180,6 +180,9 @@ fn apply_adjustments<'blk, 'tcx>(bcx: Block<'blk, 'tcx>, AdjustAddEnv(def_id, _) => { datum = unpack_datum!(bcx, add_env(bcx, def_id, expr, datum)); } + AdjustReifyFnPointer(_def_id) => { + // FIXME(#19925) once fn item types are + // zero-sized, we'll need to do something here } AdjustDerefRef(ref adj) => { let (autoderefs, use_autoref) = match adj.autoref { diff --git a/src/librustc_trans/trans/type_of.rs b/src/librustc_trans/trans/type_of.rs index 89b728370ca4..ae0c0aa5598d 100644 --- a/src/librustc_trans/trans/type_of.rs +++ b/src/librustc_trans/trans/type_of.rs @@ -150,7 +150,9 @@ pub fn type_of_fn_from_ty<'a, 'tcx>(cx: &CrateContext<'a, 'tcx>, fty: Ty<'tcx>) f.sig.0.output, f.abi) } - ty::ty_bare_fn(ref f) => { + ty::ty_bare_fn(_, ref f) => { + // FIXME(#19925) once fn item types are + // zero-sized, we'll need to do something here if f.abi == abi::Rust || f.abi == abi::RustCall { type_of_rust_fn(cx, None, From 211782fef562cac25922080229fd8443fb352792 Mon Sep 17 00:00:00 2001 From: Niko Matsakis Date: Wed, 3 Dec 2014 13:51:07 -0500 Subject: [PATCH 30/44] Perform coercions on LHS assignments. --- src/librustc_typeck/check/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index 8198e64a973d..560d937f4a44 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -3867,7 +3867,7 @@ fn check_struct_fields_on_error(fcx: &FnCtxt, } let lhs_ty = fcx.expr_ty(&**lhs); - check_expr_has_type(fcx, &**rhs, lhs_ty); + check_expr_coercable_to_type(fcx, &**rhs, lhs_ty); let rhs_ty = fcx.expr_ty(&**rhs); fcx.require_expr_have_sized_type(&**lhs, traits::AssignmentLhsSized); From 7f6177e64627110e5b9776bd5304d65806081390 Mon Sep 17 00:00:00 2001 From: Niko Matsakis Date: Wed, 26 Nov 2014 05:52:16 -0500 Subject: [PATCH 31/44] Fix fallout from changes. In cases where stage0 compiler is needed, we cannot use an `as` expression to coerce, so I used a one-off function instead (this is a no-op in stage0, but in stage1+ it triggers coercion from the fn pointer to the fn item type). --- src/libcore/str.rs | 1 + src/librustdoc/html/markdown.rs | 20 ++++++----- src/libstd/path/windows.rs | 14 ++++++-- src/libstd/thread_local/mod.rs | 24 +++++++++++++- src/libsyntax/ext/base.rs | 28 +++++++++++----- .../compile-fail/borrowck-autoref-3261.rs | 2 +- src/test/compile-fail/cast-to-bare-fn.rs | 2 +- .../coerce-bare-fn-to-closure-and-proc.rs | 15 +++++++-- src/test/compile-fail/issue-10764.rs | 2 +- src/test/compile-fail/issue-9575.rs | 2 +- .../regions-lifetime-bounds-on-fns.rs | 2 +- src/test/compile-fail/regions-nested-fns.rs | 4 +-- .../compile-fail/static-reference-to-fn-1.rs | 2 +- .../compile-fail/static-reference-to-fn-2.rs | 12 ++++--- src/test/pretty/issue-4264.pp | 33 ++++++++++--------- src/test/run-pass/const-extern-function.rs | 2 +- .../extern-compare-with-return-type.rs | 12 ++++--- src/test/run-pass/issue-10767.rs | 2 +- src/test/run-pass/issue-15444.rs | 1 + 19 files changed, 120 insertions(+), 60 deletions(-) diff --git a/src/libcore/str.rs b/src/libcore/str.rs index a89a7970ae9c..1cd58aee0612 100644 --- a/src/libcore/str.rs +++ b/src/libcore/str.rs @@ -30,6 +30,7 @@ use kinds::Sized; use mem; use num::Int; +use ops::FnMut; use option::Option; use option::Option::{None, Some}; use ops::{Fn, FnMut}; diff --git a/src/librustdoc/html/markdown.rs b/src/librustdoc/html/markdown.rs index 8b2f644dfe33..009079bd2145 100644 --- a/src/librustdoc/html/markdown.rs +++ b/src/librustdoc/html/markdown.rs @@ -65,17 +65,21 @@ type hoedown_document = libc::c_void; // this is opaque to us +type blockcodefn = extern "C" fn(*mut hoedown_buffer, *const hoedown_buffer, + *const hoedown_buffer, *mut libc::c_void); + +type headerfn = extern "C" fn(*mut hoedown_buffer, *const hoedown_buffer, + libc::c_int, *mut libc::c_void); + #[repr(C)] struct hoedown_renderer { opaque: *mut hoedown_html_renderer_state, - blockcode: Option, + blockcode: Option, blockquote: Option, blockhtml: Option, - header: Option, + header: Option, other: [libc::size_t, ..28], } @@ -281,8 +285,8 @@ pub fn render(w: &mut fmt::Formatter, s: &str, print_toc: bool) -> fmt::Result { toc_builder: if print_toc {Some(TocBuilder::new())} else {None} }; (*(*renderer).opaque).opaque = &mut opaque as *mut _ as *mut libc::c_void; - (*renderer).blockcode = Some(block); - (*renderer).header = Some(header); + (*renderer).blockcode = Some(block as blockcodefn); + (*renderer).header = Some(header as headerfn); let document = hoedown_document_new(renderer, HOEDOWN_EXTENSIONS, 16); hoedown_document_render(document, ob, s.as_ptr(), @@ -354,8 +358,8 @@ pub fn find_testable_code(doc: &str, tests: &mut ::test::Collector) { unsafe { let ob = hoedown_buffer_new(DEF_OUNIT); let renderer = hoedown_html_renderer_new(0, 0); - (*renderer).blockcode = Some(block); - (*renderer).header = Some(header); + (*renderer).blockcode = Some(block as blockcodefn); + (*renderer).header = Some(header as headerfn); (*(*renderer).opaque).opaque = tests as *mut _ as *mut libc::c_void; let document = hoedown_document_new(renderer, HOEDOWN_EXTENSIONS, 16); diff --git a/src/libstd/path/windows.rs b/src/libstd/path/windows.rs index c2c17103554c..9e4a66e0e5e0 100644 --- a/src/libstd/path/windows.rs +++ b/src/libstd/path/windows.rs @@ -829,8 +829,12 @@ fn update_sepidx(&mut self) { let s = if self.has_nonsemantic_trailing_slash() { self.repr.slice_to(self.repr.len()-1) } else { self.repr.as_slice() }; - let idx = s.rfind(if !prefix_is_verbatim(self.prefix) { is_sep } - else { is_sep_verbatim }); + let sep_test: fn(char) -> bool = if !prefix_is_verbatim(self.prefix) { + is_sep + } else { + is_sep_verbatim + }; + let idx = s.rfind(sep_test); let prefixlen = self.prefix_len(); self.sepidx = idx.and_then(|x| if x < prefixlen { None } else { Some(x) }); } @@ -1048,7 +1052,11 @@ fn parse_two_comps(mut path: &str, f: fn(char) -> bool) -> Option<(uint, uint)> // None result means the string didn't need normalizing fn normalize_helper<'a>(s: &'a str, prefix: Option) -> (bool, Option>) { - let f = if !prefix_is_verbatim(prefix) { is_sep } else { is_sep_verbatim }; + let f: fn(char) -> bool = if !prefix_is_verbatim(prefix) { + is_sep + } else { + is_sep_verbatim + }; let is_abs = s.len() > prefix_len(prefix) && f(s.char_at(prefix_len(prefix))); let s_ = s.slice_from(prefix_len(prefix)); let s_ = if is_abs { s_.slice_from(1) } else { s_ }; diff --git a/src/libstd/thread_local/mod.rs b/src/libstd/thread_local/mod.rs index 4c33d1c418d9..04718dcc6ae3 100644 --- a/src/libstd/thread_local/mod.rs +++ b/src/libstd/thread_local/mod.rs @@ -189,11 +189,12 @@ macro_rules! __thread_local_inner { } }; - #[cfg(not(any(target_os = "macos", target_os = "linux")))] + #[cfg(all(stage0, not(any(target_os = "macos", target_os = "linux"))))] const INIT: ::std::thread_local::KeyInner<$t> = { unsafe extern fn __destroy(ptr: *mut u8) { ::std::thread_local::destroy_value::<$t>(ptr); } + ::std::thread_local::KeyInner { inner: ::std::cell::UnsafeCell { value: $init }, os: ::std::thread_local::OsStaticKey { @@ -203,6 +204,21 @@ macro_rules! __thread_local_inner { } }; + #[cfg(all(not(stage0), not(any(target_os = "macos", target_os = "linux"))))] + const INIT: ::std::thread_local::KeyInner<$t> = { + unsafe extern fn __destroy(ptr: *mut u8) { + ::std::thread_local::destroy_value::<$t>(ptr); + } + + ::std::thread_local::KeyInner { + inner: ::std::cell::UnsafeCell { value: $init }, + os: ::std::thread_local::OsStaticKey { + inner: ::std::thread_local::OS_INIT_INNER, + dtor: ::std::option::Option::Some(__destroy as unsafe extern fn(*mut u8)), + }, + } + }; + INIT }); } @@ -323,6 +339,12 @@ unsafe fn register_dtor(t: *mut u8, dtor: unsafe extern fn(*mut u8)) { // *should* be the case that this loop always terminates because we // provide the guarantee that a TLS key cannot be set after it is // flagged for destruction. + #[cfg(not(stage0))] + static DTORS: os::StaticKey = os::StaticKey { + inner: os::INIT_INNER, + dtor: Some(run_dtors as unsafe extern "C" fn(*mut u8)), + }; + #[cfg(stage0)] static DTORS: os::StaticKey = os::StaticKey { inner: os::INIT_INNER, dtor: Some(run_dtors), diff --git a/src/libsyntax/ext/base.rs b/src/libsyntax/ext/base.rs index aefbb2a1feab..f76c350902d7 100644 --- a/src/libsyntax/ext/base.rs +++ b/src/libsyntax/ext/base.rs @@ -50,14 +50,16 @@ fn expand(&self, push: |P|); } -impl ItemDecorator for fn(&mut ExtCtxt, Span, &ast::MetaItem, &ast::Item, |P|) { +impl ItemDecorator for F + where F : Fn(&mut ExtCtxt, Span, &ast::MetaItem, &ast::Item, |P|) +{ fn expand(&self, ecx: &mut ExtCtxt, sp: Span, meta_item: &ast::MetaItem, item: &ast::Item, push: |P|) { - self.clone()(ecx, sp, meta_item, item, push) + (*self)(ecx, sp, meta_item, item, push) } } @@ -70,14 +72,16 @@ fn expand(&self, -> P; } -impl ItemModifier for fn(&mut ExtCtxt, Span, &ast::MetaItem, P) -> P { +impl ItemModifier for F + where F : Fn(&mut ExtCtxt, Span, &ast::MetaItem, P) -> P +{ fn expand(&self, ecx: &mut ExtCtxt, span: Span, meta_item: &ast::MetaItem, item: P) -> P { - self.clone()(ecx, span, meta_item, item) + (*self)(ecx, span, meta_item, item) } } @@ -93,13 +97,15 @@ fn expand<'cx>(&self, pub type MacroExpanderFn = for<'cx> fn(&'cx mut ExtCtxt, Span, &[ast::TokenTree]) -> Box; -impl TTMacroExpander for MacroExpanderFn { +impl TTMacroExpander for F + where F : for<'cx> Fn(&'cx mut ExtCtxt, Span, &[ast::TokenTree]) -> Box +{ fn expand<'cx>(&self, ecx: &'cx mut ExtCtxt, span: Span, token_tree: &[ast::TokenTree]) -> Box { - self.clone()(ecx, span, token_tree) + (*self)(ecx, span, token_tree) } } @@ -115,14 +121,18 @@ fn expand<'cx>(&self, pub type IdentMacroExpanderFn = for<'cx> fn(&'cx mut ExtCtxt, Span, ast::Ident, Vec) -> Box; -impl IdentMacroExpander for IdentMacroExpanderFn { +impl IdentMacroExpander for F + where F : for<'cx> Fn(&'cx mut ExtCtxt, Span, ast::Ident, + Vec) -> Box +{ fn expand<'cx>(&self, cx: &'cx mut ExtCtxt, sp: Span, ident: ast::Ident, token_tree: Vec ) - -> Box { - self.clone()(cx, sp, ident, token_tree) + -> Box + { + (*self)(cx, sp, ident, token_tree) } } diff --git a/src/test/compile-fail/borrowck-autoref-3261.rs b/src/test/compile-fail/borrowck-autoref-3261.rs index 8c6e76e77461..1b4e5891f941 100644 --- a/src/test/compile-fail/borrowck-autoref-3261.rs +++ b/src/test/compile-fail/borrowck-autoref-3261.rs @@ -20,7 +20,7 @@ pub fn with(&self, blk: |x: &Either<(uint,uint), fn()>|) { } fn main() { - let mut x = X(Either::Right(main)); + let mut x = X(Either::Right(main as fn())); (&mut x).with( |opt| { //~ ERROR cannot borrow `x` as mutable more than once at a time match opt { diff --git a/src/test/compile-fail/cast-to-bare-fn.rs b/src/test/compile-fail/cast-to-bare-fn.rs index 10a829fd7945..1db813292b01 100644 --- a/src/test/compile-fail/cast-to-bare-fn.rs +++ b/src/test/compile-fail/cast-to-bare-fn.rs @@ -13,7 +13,7 @@ fn foo(_x: int) { } fn main() { let v: u64 = 5; let x = foo as extern "C" fn() -> int; - //~^ ERROR non-scalar cast + //~^ ERROR mismatched types let y = v as extern "Rust" fn(int) -> (int, int); //~^ ERROR non-scalar cast y(x()); diff --git a/src/test/compile-fail/coerce-bare-fn-to-closure-and-proc.rs b/src/test/compile-fail/coerce-bare-fn-to-closure-and-proc.rs index 27e339180a6c..52f4c4749e22 100644 --- a/src/test/compile-fail/coerce-bare-fn-to-closure-and-proc.rs +++ b/src/test/compile-fail/coerce-bare-fn-to-closure-and-proc.rs @@ -8,12 +8,21 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. +// Test that coercions from fn item types are ok, but not fn pointer +// types to closures/procs are not allowed. + fn foo() {} -fn main() { +fn fn_item_type() { let f = foo; let f_closure: || = f; - //~^ ERROR: cannot coerce non-statically resolved bare fn to closure - //~^^ HELP: consider embedding the function in a closure } + +fn fn_pointer_type() { + let f = foo as fn(); + let f_closure: || = f; + //~^ ERROR: mismatched types +} + +fn main() { } diff --git a/src/test/compile-fail/issue-10764.rs b/src/test/compile-fail/issue-10764.rs index 0733744b6521..cd4ec495556c 100644 --- a/src/test/compile-fail/issue-10764.rs +++ b/src/test/compile-fail/issue-10764.rs @@ -12,4 +12,4 @@ fn f(_: extern "Rust" fn()) {} extern fn bar() {} fn main() { f(bar) } -//~^ ERROR: expected `fn()`, found `extern "C" fn()` +//~^ ERROR mismatched types diff --git a/src/test/compile-fail/issue-9575.rs b/src/test/compile-fail/issue-9575.rs index aa3d9d9fef08..6e8f7ffb68da 100644 --- a/src/test/compile-fail/issue-9575.rs +++ b/src/test/compile-fail/issue-9575.rs @@ -10,6 +10,6 @@ #[start] fn start(argc: int, argv: *const *const u8, crate_map: *const u8) -> int { - //~^ ERROR start function expects type: `fn(int, *const *const u8) -> int` + //~^ ERROR incorrect number of function parameters 0 } diff --git a/src/test/compile-fail/regions-lifetime-bounds-on-fns.rs b/src/test/compile-fail/regions-lifetime-bounds-on-fns.rs index 773d6e2c7036..4a42728da6f5 100644 --- a/src/test/compile-fail/regions-lifetime-bounds-on-fns.rs +++ b/src/test/compile-fail/regions-lifetime-bounds-on-fns.rs @@ -15,7 +15,7 @@ fn a<'a, 'b:'a>(x: &mut &'a int, y: &mut &'b int) { fn b<'a, 'b>(x: &mut &'a int, y: &mut &'b int) { // Illegal now because there is no `'b:'a` declaration. - *x = *y; //~ ERROR mismatched types + *x = *y; //~ ERROR cannot infer } fn c<'a,'b>(x: &mut &'a int, y: &mut &'b int) { diff --git a/src/test/compile-fail/regions-nested-fns.rs b/src/test/compile-fail/regions-nested-fns.rs index cf0b615bb01a..f4654367970d 100644 --- a/src/test/compile-fail/regions-nested-fns.rs +++ b/src/test/compile-fail/regions-nested-fns.rs @@ -12,10 +12,10 @@ fn ignore(t: T) {} fn nested<'x>(x: &'x int) { let y = 3; - let mut ay = &y; //~ ERROR cannot infer + let mut ay = &y; ignore::< for<'z>|&'z int|>(|z| { - ay = x; + ay = x; //~ ERROR cannot infer ay = &y; ay = z; }); diff --git a/src/test/compile-fail/static-reference-to-fn-1.rs b/src/test/compile-fail/static-reference-to-fn-1.rs index c0d430908a13..bce397c47932 100644 --- a/src/test/compile-fail/static-reference-to-fn-1.rs +++ b/src/test/compile-fail/static-reference-to-fn-1.rs @@ -24,7 +24,7 @@ fn foo() -> Option { fn create() -> A<'static> { A { - func: &foo, //~ ERROR borrowed value does not live long enough + func: &foo, //~ ERROR mismatched types } } diff --git a/src/test/compile-fail/static-reference-to-fn-2.rs b/src/test/compile-fail/static-reference-to-fn-2.rs index 3a0f0a193cfe..d7255c3ba069 100644 --- a/src/test/compile-fail/static-reference-to-fn-2.rs +++ b/src/test/compile-fail/static-reference-to-fn-2.rs @@ -9,9 +9,11 @@ // except according to those terms. struct StateMachineIter<'a> { - statefn: &'a fn(&mut StateMachineIter<'a>) -> Option<&'static str> + statefn: &'a StateMachineFunc<'a> } +type StateMachineFunc<'a> = fn(&mut StateMachineIter<'a>) -> Option<&'static str>; + impl<'a> Iterator<&'static str> for StateMachineIter<'a> { fn next(&mut self) -> Option<&'static str> { return (*self.statefn)(self); @@ -19,19 +21,19 @@ fn next(&mut self) -> Option<&'static str> { } fn state1(self_: &mut StateMachineIter) -> Option<&'static str> { - self_.statefn = &state2; + self_.statefn = &(state2 as StateMachineFunc); //~^ ERROR borrowed value does not live long enough return Some("state1"); } fn state2(self_: &mut StateMachineIter) -> Option<(&'static str)> { - self_.statefn = &state3; + self_.statefn = &(state3 as StateMachineFunc); //~^ ERROR borrowed value does not live long enough return Some("state2"); } fn state3(self_: &mut StateMachineIter) -> Option<(&'static str)> { - self_.statefn = &finished; + self_.statefn = &(finished as StateMachineFunc); //~^ ERROR borrowed value does not live long enough return Some("state3"); } @@ -42,7 +44,7 @@ fn finished(_: &mut StateMachineIter) -> Option<(&'static str)> { fn state_iter() -> StateMachineIter<'static> { StateMachineIter { - statefn: &state1 //~ ERROR borrowed value does not live long enough + statefn: &(state1 as StateMachineFunc) //~ ERROR borrowed value does not live long enough } } diff --git a/src/test/pretty/issue-4264.pp b/src/test/pretty/issue-4264.pp index 974af1e6f3e1..e4389cd69dd6 100644 --- a/src/test/pretty/issue-4264.pp +++ b/src/test/pretty/issue-4264.pp @@ -50,20 +50,20 @@ pub fn bar() { ((::std::fmt::format as - fn(&core::fmt::Arguments<'_>) -> collections::string::String)((&((::std::fmt::Arguments::new - as - fn(&[&str], &[core::fmt::Argument<'_>]) -> core::fmt::Arguments<'_>)((__STATIC_FMTSTR - as - &'static [&'static str]), - (&([] - as - [core::fmt::Argument<'_>; 0]) - as - &[core::fmt::Argument<'_>; 0])) - as - core::fmt::Arguments<'_>) - as - &core::fmt::Arguments<'_>)) + fn(&core::fmt::Arguments<'_>) -> collections::string::String {std::fmt::format})((&((::std::fmt::Arguments::new + as + fn(&[&str], &[core::fmt::Argument<'_>]) -> core::fmt::Arguments<'_> {core::fmt::Arguments<'a>::new})((__STATIC_FMTSTR + as + &'static [&'static str]), + (&([] + as + [core::fmt::Argument<'_>; 0]) + as + &[core::fmt::Argument<'_>; 0])) + as + core::fmt::Arguments<'_>) + as + &core::fmt::Arguments<'_>)) as collections::string::String) } } as collections::string::String); @@ -78,7 +78,8 @@ pub fn id(x: T) -> T { (x as T) } pub fn use_id() { let _ = ((id::<[int; (3u as uint)]> as - fn([int; 3]) -> [int; 3])(([(1 as int), (2 as int), (3 as int)] - as [int; 3])) as [int; 3]); + fn([int; 3]) -> [int; 3] {id})(([(1 as int), (2 as int), + (3 as int)] as [int; 3])) as + [int; 3]); } fn main() { } diff --git a/src/test/run-pass/const-extern-function.rs b/src/test/run-pass/const-extern-function.rs index be7c47dafc01..069ca6ecf49d 100644 --- a/src/test/run-pass/const-extern-function.rs +++ b/src/test/run-pass/const-extern-function.rs @@ -18,6 +18,6 @@ struct S { } pub fn main() { - assert!(foopy == f); + assert!(foopy as extern "C" fn() == f); assert!(f == s.f); } diff --git a/src/test/run-pass/extern-compare-with-return-type.rs b/src/test/run-pass/extern-compare-with-return-type.rs index 057394b2624f..3febff18704d 100644 --- a/src/test/run-pass/extern-compare-with-return-type.rs +++ b/src/test/run-pass/extern-compare-with-return-type.rs @@ -18,15 +18,17 @@ extern fn uintvoidret(_x: uint) {} extern fn uintuintuintuintret(x: uint, y: uint, z: uint) -> uint { x+y+z } +type uintuintuintuintret = extern fn(uint,uint,uint) -> uint; pub fn main() { - assert!(voidret1 == voidret1); - assert!(voidret1 != voidret2); + assert!(voidret1 as extern fn() == voidret1 as extern fn()); + assert!(voidret1 as extern fn() != voidret2 as extern fn()); - assert!(uintret == uintret); + assert!(uintret as extern fn() -> uint == uintret as extern fn() -> uint); - assert!(uintvoidret == uintvoidret); + assert!(uintvoidret as extern fn(uint) == uintvoidret as extern fn(uint)); - assert!(uintuintuintuintret == uintuintuintuintret); + assert!(uintuintuintuintret as uintuintuintuintret == + uintuintuintuintret as uintuintuintuintret); } diff --git a/src/test/run-pass/issue-10767.rs b/src/test/run-pass/issue-10767.rs index a30eb8120eae..d28950241874 100644 --- a/src/test/run-pass/issue-10767.rs +++ b/src/test/run-pass/issue-10767.rs @@ -12,5 +12,5 @@ pub fn main() { fn f() { }; - let _: Box = box f; + let _: Box = box() (f as fn()); } diff --git a/src/test/run-pass/issue-15444.rs b/src/test/run-pass/issue-15444.rs index f5618c2c7a3c..0f4978d78dd8 100644 --- a/src/test/run-pass/issue-15444.rs +++ b/src/test/run-pass/issue-15444.rs @@ -25,5 +25,6 @@ fn thing(a: int, b: int) -> int { } fn main() { + let thing: fn(int, int) -> int = thing; // coerce to fn type bar(&thing); } From 8fe9e4dff6d9d0fdd940835ae377edcb3754f8c1 Mon Sep 17 00:00:00 2001 From: Niko Matsakis Date: Mon, 15 Dec 2014 15:41:30 -0500 Subject: [PATCH 32/44] Insert coercions to fn pointer types required for the new types post-unboxed-closure-conversion. This requires a fair amount of annoying coercions because all the `map` etc types are defined generically over the `F`, so the automatic coercions don't propagate; this is compounded by the need to use `let` and not `as` due to stage0. That said, this pattern is to a large extent temporary and unusual. --- src/libcollections/btree/map.rs | 2 ++ src/libcollections/btree/set.rs | 1 + src/libcollections/vec_map.rs | 3 +++ src/libcore/iter.rs | 3 +++ src/libcore/str.rs | 1 + src/librustc_trans/trans/basic_block.rs | 3 +++ src/libstd/collections/hash/map.rs | 8 ++++++-- src/libstd/collections/hash/set.rs | 3 +++ src/libstd/path/posix.rs | 4 +++- src/libstd/path/windows.rs | 4 +++- src/libunicode/u_str.rs | 3 +++ 11 files changed, 31 insertions(+), 4 deletions(-) diff --git a/src/libcollections/btree/map.rs b/src/libcollections/btree/map.rs index 01096c1fd4e9..778314f352c2 100644 --- a/src/libcollections/btree/map.rs +++ b/src/libcollections/btree/map.rs @@ -1230,6 +1230,7 @@ pub fn into_iter(self) -> MoveEntries { #[unstable = "matches collection reform specification, waiting for dust to settle"] pub fn keys<'a>(&'a self) -> Keys<'a, K, V> { fn first((a, _): (A, B)) -> A { a } + let first: fn((&'a K, &'a V)) -> &'a K = first; // coerce to fn pointer Keys { inner: self.iter().map(first) } } @@ -1251,6 +1252,7 @@ fn first((a, _): (A, B)) -> A { a } #[unstable = "matches collection reform specification, waiting for dust to settle"] pub fn values<'a>(&'a self) -> Values<'a, K, V> { fn second((_, b): (A, B)) -> B { b } + let second: fn((&'a K, &'a V)) -> &'a V = second; // coerce to fn pointer Values { inner: self.iter().map(second) } } diff --git a/src/libcollections/btree/set.rs b/src/libcollections/btree/set.rs index e4328a3cb202..4ab9c7a4fcdd 100644 --- a/src/libcollections/btree/set.rs +++ b/src/libcollections/btree/set.rs @@ -126,6 +126,7 @@ pub fn iter<'a>(&'a self) -> Items<'a, T> { #[unstable = "matches collection reform specification, waiting for dust to settle"] pub fn into_iter(self) -> MoveItems { fn first((a, _): (A, B)) -> A { a } + let first: fn((T, ())) -> T = first; // coerce to fn pointer MoveItems { iter: self.map.into_iter().map(first) } } diff --git a/src/libcollections/vec_map.rs b/src/libcollections/vec_map.rs index 1babde6066d0..553eae4d896d 100644 --- a/src/libcollections/vec_map.rs +++ b/src/libcollections/vec_map.rs @@ -144,6 +144,7 @@ pub fn capacity(&self) -> uint { #[unstable = "matches collection reform specification, waiting for dust to settle"] pub fn keys<'r>(&'r self) -> Keys<'r, V> { fn first((a, _): (A, B)) -> A { a } + let first: fn((uint, &'r V)) -> uint = first; // coerce to fn pointer Keys { iter: self.iter().map(first) } } @@ -153,6 +154,7 @@ fn first((a, _): (A, B)) -> A { a } #[unstable = "matches collection reform specification, waiting for dust to settle"] pub fn values<'r>(&'r self) -> Values<'r, V> { fn second((_, b): (A, B)) -> B { b } + let second: fn((uint, &'r V)) -> &'r V = second; // coerce to fn pointer Values { iter: self.iter().map(second) } } @@ -239,6 +241,7 @@ pub fn into_iter(&mut self) -> MoveItems { fn filter((i, v): (uint, Option)) -> Option<(uint, A)> { v.map(|v| (i, v)) } + let filter: fn((uint, Option)) -> Option<(uint, V)> = filter; // coerce to fn ptr let values = replace(&mut self.v, vec!()); MoveItems { iter: values.into_iter().enumerate().filter_map(filter) } diff --git a/src/libcore/iter.rs b/src/libcore/iter.rs index b592d1db274f..1cd4d7b89d6d 100644 --- a/src/libcore/iter.rs +++ b/src/libcore/iter.rs @@ -2612,6 +2612,9 @@ fn next(st: &mut IterateState) -> Option where val.clone() } + // coerce to a fn pointer + let next: fn(&mut IterateState) -> Option = next; + Unfold::new((f, Some(seed), true), next) } diff --git a/src/libcore/str.rs b/src/libcore/str.rs index 1cd58aee0612..1207c78fa37b 100644 --- a/src/libcore/str.rs +++ b/src/libcore/str.rs @@ -2101,6 +2101,7 @@ fn f(line: &str) -> &str { else { line } } + let f: fn(&str) -> &str = f; // coerce to fn pointer self.lines().map(f) } diff --git a/src/librustc_trans/trans/basic_block.rs b/src/librustc_trans/trans/basic_block.rs index 476f5e2d618f..ab25343ff5fe 100644 --- a/src/librustc_trans/trans/basic_block.rs +++ b/src/librustc_trans/trans/basic_block.rs @@ -37,7 +37,10 @@ pub fn as_value(self) -> Value { pub fn pred_iter(self) -> Preds { fn is_a_terminator_inst(user: &Value) -> bool { user.is_a_terminator_inst() } + let is_a_terminator_inst: fn(&Value) -> bool = is_a_terminator_inst; + fn get_parent(user: Value) -> BasicBlock { user.get_parent().unwrap() } + let get_parent: fn(Value) -> BasicBlock = get_parent; self.as_value().user_iter() .filter(is_a_terminator_inst) diff --git a/src/libstd/collections/hash/map.rs b/src/libstd/collections/hash/map.rs index 8149864afd40..dc9fd2893447 100644 --- a/src/libstd/collections/hash/map.rs +++ b/src/libstd/collections/hash/map.rs @@ -838,8 +838,9 @@ pub fn pop_equiv + Equiv>(&mut self, k: &Q) -> Option { /// } /// ``` #[unstable = "matches collection reform specification, waiting for dust to settle"] - pub fn keys(&self) -> Keys { + pub fn keys<'a>(&'a self) -> Keys<'a, K, V> { fn first((a, _): (A, B)) -> A { a } + let first: fn((&'a K,&'a V)) -> &'a K = first; // coerce to fn ptr Keys { inner: self.iter().map(first) } } @@ -862,8 +863,9 @@ fn first((a, _): (A, B)) -> A { a } /// } /// ``` #[unstable = "matches collection reform specification, waiting for dust to settle"] - pub fn values(&self) -> Values { + pub fn values<'a>(&'a self) -> Values<'a, K, V> { fn second((_, b): (A, B)) -> B { b } + let second: fn((&'a K,&'a V)) -> &'a V = second; // coerce to fn ptr Values { inner: self.iter().map(second) } } @@ -938,6 +940,7 @@ pub fn iter_mut(&mut self) -> MutEntries { #[unstable = "matches collection reform specification, waiting for dust to settle"] pub fn into_iter(self) -> MoveEntries { fn last_two((_, b, c): (A, B, C)) -> (B, C) { (b, c) } + let last_two: fn((SafeHash, K, V)) -> (K, V) = last_two; MoveEntries { inner: self.table.into_iter().map(last_two) @@ -1007,6 +1010,7 @@ pub fn is_empty(&self) -> bool { self.len() == 0 } #[unstable = "matches collection reform specification, waiting for dust to settle"] pub fn drain(&mut self) -> Drain { fn last_two((_, b, c): (A, B, C)) -> (B, C) { (b, c) } + let last_two: fn((SafeHash, K, V)) -> (K, V) = last_two; // coerce to fn pointer Drain { inner: self.table.drain().map(last_two), diff --git a/src/libstd/collections/hash/set.rs b/src/libstd/collections/hash/set.rs index f587669d3dac..15ac3394d417 100644 --- a/src/libstd/collections/hash/set.rs +++ b/src/libstd/collections/hash/set.rs @@ -277,6 +277,7 @@ pub fn iter<'a>(&'a self) -> Iter<'a, T> { #[unstable = "matches collection reform specification, waiting for dust to settle"] pub fn into_iter(self) -> IntoIter { fn first((a, _): (A, B)) -> A { a } + let first: fn((T, ())) -> T = first; IntoIter { iter: self.map.into_iter().map(first) } } @@ -419,6 +420,8 @@ pub fn is_empty(&self) -> bool { self.map.len() == 0 } #[unstable = "matches collection reform specification, waiting for dust to settle"] pub fn drain(&mut self) -> Drain { fn first((a, _): (A, B)) -> A { a } + let first: fn((T, ())) -> T = first; // coerce to fn pointer + Drain { iter: self.map.drain().map(first) } } diff --git a/src/libstd/path/posix.rs b/src/libstd/path/posix.rs index 88907951673d..0808b2a4f425 100644 --- a/src/libstd/path/posix.rs +++ b/src/libstd/path/posix.rs @@ -390,6 +390,7 @@ pub fn components<'a>(&'a self) -> Components<'a> { let v = if self.repr[0] == SEP_BYTE { self.repr[1..] } else { self.repr.as_slice() }; + let is_sep_byte: fn(&u8) -> bool = is_sep_byte; // coerce to fn ptr let mut ret = v.split(is_sep_byte); if v.is_empty() { // consume the empty "" component @@ -401,7 +402,8 @@ pub fn components<'a>(&'a self) -> Components<'a> { /// Returns an iterator that yields each component of the path as Option<&str>. /// See components() for details. pub fn str_components<'a>(&'a self) -> StrComponents<'a> { - self.components().map(str::from_utf8) + let from_utf8: fn(&[u8]) -> Option<&str> = str::from_utf8; // coerce to fn ptr + self.components().map(from_utf8) } } diff --git a/src/libstd/path/windows.rs b/src/libstd/path/windows.rs index 9e4a66e0e5e0..4b8fb7fad6a3 100644 --- a/src/libstd/path/windows.rs +++ b/src/libstd/path/windows.rs @@ -655,7 +655,8 @@ pub fn str_components<'a>(&'a self) -> StrComponents<'a> { None if repr.as_bytes()[0] == SEP_BYTE => repr.slice_from(1), None => repr }; - let ret = s.split_terminator(SEP).map(Some); + let some: fn(&'a str) -> Option<&'a str> = Some; // coerce to fn ptr + let ret = s.split_terminator(SEP).map(some); ret } @@ -666,6 +667,7 @@ fn convert<'a>(x: Option<&'a str>) -> &'a [u8] { #![inline] x.unwrap().as_bytes() } + let convert: for<'b> fn(Option<&'b str>) -> &'b [u8] = convert; // coerce to fn ptr self.str_components().map(convert) } diff --git a/src/libunicode/u_str.rs b/src/libunicode/u_str.rs index 5e98109c432a..88d87717ff3e 100644 --- a/src/libunicode/u_str.rs +++ b/src/libunicode/u_str.rs @@ -143,7 +143,10 @@ fn grapheme_indices(&self, is_extended: bool) -> GraphemeIndices { #[inline] fn words(&self) -> Words { fn is_not_empty(s: &&str) -> bool { !s.is_empty() } + let is_not_empty: fn(&&str) -> bool = is_not_empty; // coerce to fn pointer + fn is_whitespace(c: char) -> bool { c.is_whitespace() } + let is_whitespace: fn(char) -> bool = is_whitespace; // coerce to fn pointer self.split(is_whitespace).filter(is_not_empty) } From 41ef2d85a1c685002230e0c05165c115b502486b Mon Sep 17 00:00:00 2001 From: Niko Matsakis Date: Wed, 26 Nov 2014 06:47:14 -0500 Subject: [PATCH 33/44] Various simple tests for fn item type vs fn pointer type and for coercions between them. --- src/test/compile-fail/fn-item-type.rs | 25 +++++++++++++++++++++ src/test/run-pass/fn-item-type-cast.rs | 28 ++++++++++++++++++++++++ src/test/run-pass/fn-item-type-coerce.rs | 23 +++++++++++++++++++ 3 files changed, 76 insertions(+) create mode 100644 src/test/compile-fail/fn-item-type.rs create mode 100644 src/test/run-pass/fn-item-type-cast.rs create mode 100644 src/test/run-pass/fn-item-type-coerce.rs diff --git a/src/test/compile-fail/fn-item-type.rs b/src/test/compile-fail/fn-item-type.rs new file mode 100644 index 000000000000..dd4a24bfb2fd --- /dev/null +++ b/src/test/compile-fail/fn-item-type.rs @@ -0,0 +1,25 @@ +// Copyright 2014 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// Test that the types of distinct fn items are not compatible by +// default. See also `run-pass/fn-item-type-*.rs`. + +fn foo(x: int) -> int { x * 2 } +fn bar(x: int) -> int { x * 4 } + +fn eq(x: T, y: T) { } + +fn main() { + let f = if true { foo } else { bar }; + //~^ ERROR expected fn item, found a different fn item + + eq(foo, bar); + //~^ ERROR expected fn item, found a different fn item +} diff --git a/src/test/run-pass/fn-item-type-cast.rs b/src/test/run-pass/fn-item-type-cast.rs new file mode 100644 index 000000000000..bfd02f5e27b0 --- /dev/null +++ b/src/test/run-pass/fn-item-type-cast.rs @@ -0,0 +1,28 @@ +// Copyright 2014 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// Test explicit coercions from a fn item type to a fn pointer type. + +fn foo(x: int) -> int { x * 2 } +fn bar(x: int) -> int { x * 4 } +type IntMap = fn(int) -> int; + +fn eq(x: T, y: T) { } + +static TEST: Option = Some(foo as IntMap); + +fn main() { + let f = foo as IntMap; + + let f = if true { foo as IntMap } else { bar as IntMap }; + assert_eq!(f(4), 8); + + eq(foo as IntMap, bar as IntMap); +} diff --git a/src/test/run-pass/fn-item-type-coerce.rs b/src/test/run-pass/fn-item-type-coerce.rs new file mode 100644 index 000000000000..8427a0f44462 --- /dev/null +++ b/src/test/run-pass/fn-item-type-coerce.rs @@ -0,0 +1,23 @@ +// Copyright 2014 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +// Test implicit coercions from a fn item type to a fn pointer type. + +fn foo(x: int) -> int { x * 2 } +fn bar(x: int) -> int { x * 4 } +type IntMap = fn(int) -> int; + +fn eq(x: T, y: T) { } + +fn main() { + let f: IntMap = foo; + + eq::(foo, bar); +} From 69a1d0e610179e9fb7544764161e52b5d8f5faf3 Mon Sep 17 00:00:00 2001 From: Niko Matsakis Date: Wed, 3 Dec 2014 12:56:03 -0500 Subject: [PATCH 34/44] Change print out to include the path to the fn, a bit ad-hoc for now . --- src/librustc/util/ppaux.rs | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/librustc/util/ppaux.rs b/src/librustc/util/ppaux.rs index daa7c13e3b31..476d9949812b 100644 --- a/src/librustc/util/ppaux.rs +++ b/src/librustc/util/ppaux.rs @@ -254,12 +254,14 @@ pub fn vec_map_to_string(ts: &[T], f: F) -> String where pub fn ty_to_string<'tcx>(cx: &ctxt<'tcx>, typ: &ty::TyS<'tcx>) -> String { fn bare_fn_to_string<'tcx>(cx: &ctxt<'tcx>, + opt_def_id: Option, unsafety: ast::Unsafety, abi: abi::Abi, ident: Option, sig: &ty::PolyFnSig<'tcx>) -> String { let mut s = String::new(); + match unsafety { ast::Unsafety::Normal => {} ast::Unsafety::Unsafe => { @@ -284,6 +286,16 @@ fn bare_fn_to_string<'tcx>(cx: &ctxt<'tcx>, push_sig_to_string(cx, &mut s, '(', ')', sig, ""); + match opt_def_id { + Some(def_id) => { + s.push_str(" {"); + let path_str = ty::item_path_str(cx, def_id); + s.push_str(path_str[]); + s.push_str("}"); + } + None => { } + } + s } @@ -408,8 +420,8 @@ fn infer_ty_to_string(cx: &ctxt, ty: ty::InferTy) -> String { ty_closure(ref f) => { closure_to_string(cx, &**f) } - ty_bare_fn(_, ref f) => { - bare_fn_to_string(cx, f.unsafety, f.abi, None, &f.sig) + ty_bare_fn(opt_def_id, ref f) => { + bare_fn_to_string(cx, opt_def_id, f.unsafety, f.abi, None, &f.sig) } ty_infer(infer_ty) => infer_ty_to_string(cx, infer_ty), ty_err => "[type error]".to_string(), From 2e2906522d4a6160621725a6c58c751d3aab36cc Mon Sep 17 00:00:00 2001 From: Niko Matsakis Date: Tue, 16 Dec 2014 18:15:33 -0500 Subject: [PATCH 35/44] Address nit. --- src/librustc/middle/infer/coercion.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/librustc/middle/infer/coercion.rs b/src/librustc/middle/infer/coercion.rs index 08336da0d8a6..ec83b8fae9b7 100644 --- a/src/librustc/middle/infer/coercion.rs +++ b/src/librustc/middle/infer/coercion.rs @@ -507,7 +507,7 @@ fn coerce_from_fn_item(&self, */ self.unpack_actual_value(b, |b| { - debug!("coerce_from_bare_fn(a={}, b={})", + debug!("coerce_from_fn_item(a={}, b={})", a.repr(self.tcx()), b.repr(self.tcx())); match b.sty { From 2c76ced9540c1ae541d2f14e9d2c9fe702d4c3e0 Mon Sep 17 00:00:00 2001 From: Niko Matsakis Date: Fri, 19 Dec 2014 13:36:27 -0500 Subject: [PATCH 36/44] Correct merge failure around imports in `str.rs`. --- src/libcore/str.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/libcore/str.rs b/src/libcore/str.rs index 1207c78fa37b..8381deb2bf7d 100644 --- a/src/libcore/str.rs +++ b/src/libcore/str.rs @@ -30,10 +30,9 @@ use kinds::Sized; use mem; use num::Int; -use ops::FnMut; +use ops::{Fn, FnMut}; use option::Option; use option::Option::{None, Some}; -use ops::{Fn, FnMut}; use ptr::RawPtr; use raw::{Repr, Slice}; use slice::{mod, SliceExt}; From 763152b995bb506ac88c852a030c84a012bcf983 Mon Sep 17 00:00:00 2001 From: Niko Matsakis Date: Sun, 21 Dec 2014 14:20:15 -0500 Subject: [PATCH 37/44] Tweak region-related error messages that changed slightly due to coercion changes, and also stop printing semi-useless inference by-products. --- src/librustc/middle/ty.rs | 4 ++++ .../region-lifetime-bounds-on-fns-where-clause.rs | 2 +- .../region-multiple-lifetime-bounds-on-fns-where-clause.rs | 4 ++-- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/librustc/middle/ty.rs b/src/librustc/middle/ty.rs index f05404759c4e..aaa80f840fda 100644 --- a/src/librustc/middle/ty.rs +++ b/src/librustc/middle/ty.rs @@ -4577,6 +4577,10 @@ pub fn note_and_explain_type_err(cx: &ctxt, err: &type_err) { "concrete lifetime that was found is ", conc_region, ""); } + terr_regions_overly_polymorphic(_, ty::ReInfer(ty::ReVar(_))) => { + // don't bother to print out the message below for + // inference variables, it's not very illuminating. + } terr_regions_overly_polymorphic(_, conc_region) => { note_and_explain_region(cx, "expected concrete lifetime is ", diff --git a/src/test/compile-fail/region-lifetime-bounds-on-fns-where-clause.rs b/src/test/compile-fail/region-lifetime-bounds-on-fns-where-clause.rs index 381144f2599c..3e6a95b04f74 100644 --- a/src/test/compile-fail/region-lifetime-bounds-on-fns-where-clause.rs +++ b/src/test/compile-fail/region-lifetime-bounds-on-fns-where-clause.rs @@ -15,7 +15,7 @@ fn a<'a, 'b>(x: &mut &'a int, y: &mut &'b int) where 'b: 'a { fn b<'a, 'b>(x: &mut &'a int, y: &mut &'b int) { // Illegal now because there is no `'b:'a` declaration. - *x = *y; //~ ERROR mismatched types + *x = *y; //~ ERROR cannot infer } fn c<'a,'b>(x: &mut &'a int, y: &mut &'b int) { diff --git a/src/test/compile-fail/region-multiple-lifetime-bounds-on-fns-where-clause.rs b/src/test/compile-fail/region-multiple-lifetime-bounds-on-fns-where-clause.rs index a03911e1d0e1..2d635e9fc271 100644 --- a/src/test/compile-fail/region-multiple-lifetime-bounds-on-fns-where-clause.rs +++ b/src/test/compile-fail/region-multiple-lifetime-bounds-on-fns-where-clause.rs @@ -16,8 +16,8 @@ fn a<'a, 'b, 'c>(x: &mut &'a int, y: &mut &'b int, z: &mut &'c int) where 'b: 'a fn b<'a, 'b, 'c>(x: &mut &'a int, y: &mut &'b int, z: &mut &'c int) { // Illegal now because there is no `'b:'a` declaration. - *x = *y; //~ ERROR mismatched types - *z = *y; //~ ERROR mismatched types + *x = *y; //~ ERROR cannot infer + *z = *y; //~ ERROR cannot infer } fn c<'a,'b, 'c>(x: &mut &'a int, y: &mut &'b int, z: &mut &'c int) { From faa00949d9387321ec7cbb57282f0f73f0deb8dd Mon Sep 17 00:00:00 2001 From: Simonas Kazlauskas Date: Mon, 22 Dec 2014 12:32:29 +0200 Subject: [PATCH 38/44] Update man page with the new options This pull request updates the rustc manual page to represent post-#19900 state of rustc options better. A bit unrelatedly, --help output is changed to fix some issues too: * -g and -O descriptions were changed from deprected flags to the new codegen flags. * dep-info value was moved from crate-type to emit flag. Fixes #20111 Fixes #20131 --- man/rustc.1 | 131 ++++++++++++++++++++------------- src/librustc/session/config.rs | 8 +- 2 files changed, 83 insertions(+), 56 deletions(-) diff --git a/man/rustc.1 b/man/rustc.1 index 6d4f6aa84378..4457ac8cce7e 100644 --- a/man/rustc.1 +++ b/man/rustc.1 @@ -12,75 +12,54 @@ This program is a compiler for the Rust language, available at .SH OPTIONS .TP -\fB\-\-crate-name NAME\fR -Specify the name of the crate being built -.TP -\fB\-\-crate-type=[bin|lib|dylib|rlib|staticlib]\fR -Configure the flavor of rust crate that is generated (default `bin`) +\fB\-h\fR, \fB\-\-help\fR +Display the help message .TP \fB\-\-cfg\fR SPEC Configure the compilation environment .TP -\fB\-\-emit=[asm,ir,bc,obj,link]\fR -Configure the output that rustc will produce -.TP -\fB\-h\fR, \fB\-\-help\fR -Display this message -.TP \fB\-L\fR PATH Add a directory to the library search path .TP -\fB\-\-no\-trans\fR -Run all passes except translation; no output +\fB\-l\fR NAME[:KIND] +Link the generated crate(s) to the specified native library NAME. The optional +KIND can be one of, static, dylib, or framework. If omitted, dylib is assumed. .TP -\fB\-\-no\-analysis\fR -Parse and expand the source, but run no analysis and produce no output +\fB\-\-crate-type\fR [bin|lib|rlib|dylib|staticlib] +Comma separated list of types of crates for the compiler to emit +.TP +\fB\-\-crate-name NAME\fR +Specify the name of the crate being built +.TP +\fB\-\-emit\fR [asm|llvm-bc|llvm-ir|obj|link|dep-info] +Configure the output that rustc will produce +.TP +\fB\-\-print\fR [crate-name|output-file-names|sysroot] +Comma separated list of compiler information to print on stdout .TP \fB\-g\fR -Emit DWARF debug information into object files generated. -.TP -\fB\-\-debuginfo\fR LEVEL -Emit DWARF debug info to the objects created: 0 = no debug info, 1 = -line-tables only (for stacktraces and breakpoints), 2 = full debug -info with variable and type information (same as -g). +Equivalent to \fI\-C\fR debuginfo=2 .TP \fB\-O\fR -Equivalent to \fI\-\-opt\-level=2\fR +Equivalent to \fI\-C\fR opt-level=2 .TP \fB\-o\fR FILENAME -Write output to . Ignored if more than one --emit is specified. -.TP -\fB\-\-opt\-level\fR LEVEL -Optimize with possible levels 0-3 +Write output to . Ignored if multiple \fI\-\-emit\fR outputs are +specified. .TP \fB\-\-out\-dir\fR DIR -Write output to compiler-chosen filename in . Ignored if -o is specified. -(default the current directory) +Write output to compiler-chosen filename in . Ignored if \fI\-o\fR is +specified. Defaults to the current directory. .TP -\fB\-\-parse\-only\fR -Parse only; do not compile, assemble, or link -.TP -\fB\-\-pretty\fR [TYPE] -Pretty-print the input instead of compiling; valid types are: normal -(un-annotated source), expanded (crates expanded), typed (crates -expanded, with type annotations), identified (fully parenthesized, -AST nodes and blocks with IDs), or flowgraph= (graphviz -formatted flowgraph for node) -.TP -\fB\-\-dep-info\fR [FILENAME] -Output dependency info to after compiling, in a format suitable -for use by Makefiles. -.TP -\fB\-\-sysroot\fR PATH -Override the system root +\fB\-\-explain\fR OPT +Provide a detailed explanation of an error message .TP \fB\-\-test\fR Build a test harness .TP \fB\-\-target\fR TRIPLE -Target triple cpu-manufacturer-kernel[-os] to compile for (see -http://sources.redhat.com/autobook/autobook/autobook_17.html -for details) +Target triple cpu-manufacturer-kernel[-os] to compile for (see chapter 3.4 of +http://www.sourceware.org/autobook/ for details) .TP \fB\-W\fR help Print 'lint' options and default settings @@ -97,15 +76,30 @@ Set lint denied \fB\-F\fR OPT, \fB\-\-forbid\fR OPT Set lint forbidden .TP -\fB\-Z\fR FLAG -Set internal debugging options. Use "-Z help" to print available options. -.TP \fB\-C\fR FLAG[=VAL], \fB\-\-codegen\fR FLAG[=VAL] Set a codegen-related flag to the value specified. Use "-C help" to print available flags. See CODEGEN OPTIONS below .TP -\fB\-v\fR, \fB\-\-version\fR +\fB\-V\fR, \fB\-\-version\fR Print version info and exit +.TP +\fB\-v\fR, \fB\-\-verbose\fR +Use verbose output +.TP +\fB\-\-extern\fR NAME=PATH +Specify where an external rust library is located +.TP +\fB\-\-sysroot\fR PATH +Override the system root +.TP +\fB\-Z\fR FLAG +Set internal debugging options. Use "-Z help" to print available options. +.TP +\fB\-\-color\fR auto|always|never +Configure coloring of output: + auto = colorize, if output goes to a tty (default); + always = always colorize output; + never = never colorize output .SH CODEGEN OPTIONS @@ -121,6 +115,9 @@ objects. A space-separated list of extra arguments to pass to the linker when the linker is invoked. .TP +\fBlto\fR +Perform LLVM link-time optimizations. +.TP \fBtarget-cpu\fR=help Selects a target processor. If the value is 'help', then a list of available CPUs is printed. @@ -167,8 +164,38 @@ Prefers dynamic linking to static linking. \fBno-integrated-as\fR Force usage of an external assembler rather than LLVM's integrated one. .TP +\fBno-redzone\fR +Disable the use of the redzone. +.TP \fBrelocation-model\fR=[pic,static,dynamic-no-pic] -The relocation model to use. (default: pic) +The relocation model to use. (Default: pic) +.TP +\fBcode-model\fR=[small,kernel,medium,large] +Choose the code model to use. +.TP +\fBmetadata\fR=val +Metadata to mangle symbol names with. +.TP +\fBextra-filename\fR=val +Extra data to put in each output filename. +.TP +\fBcodegen-units\fR=val +Divide crate into N units to optimize in parallel. +.TP +\fBremark\fR=val +Print remarks for these optimization passes (space separated, or "all"). +.TP +\fBno-stack-check\fR +Disable checks for stack exhaustion (a memory-safety hazard!). +.TP +\fBdebuginfo\fR=val +Debug info emission level: + 0 = no debug info; + 1 = line-tables only (for stacktraces and breakpoints); + 2 = full debug info with variable and type information. +.TP +\fBopt-level\fR=val +Optimize with possible levels 0-3 .SH "EXAMPLES" To build an executable from a source file with a main function: diff --git a/src/librustc/session/config.rs b/src/librustc/session/config.rs index 0652645907bc..d961baa414ad 100644 --- a/src/librustc/session/config.rs +++ b/src/librustc/session/config.rs @@ -664,17 +664,17 @@ pub fn short_optgroups() -> Vec { assumed.", "NAME[:KIND]"), optmulti("", "crate-type", "Comma separated list of types of crates for the compiler to emit", - "[bin|lib|rlib|dylib|staticlib|dep-info]"), + "[bin|lib|rlib|dylib|staticlib]"), optopt("", "crate-name", "Specify the name of the crate being built", "NAME"), optmulti("", "emit", "Comma separated list of types of output for \ the compiler to emit", - "[asm|llvm-bc|llvm-ir|obj|link]"), + "[asm|llvm-bc|llvm-ir|obj|link|dep-info]"), optmulti("", "print", "Comma separated list of compiler information to \ print on stdout", "[crate-name|output-file-names|sysroot]"), - optflag("g", "", "Equivalent to --debuginfo=2"), - optflag("O", "", "Equivalent to --opt-level=2"), + optflag("g", "", "Equivalent to -C debuginfo=2"), + optflag("O", "", "Equivalent to -C opt-level=2"), optopt("o", "", "Write output to ", "FILENAME"), optopt("", "out-dir", "Write output to compiler-chosen filename \ in ", "DIR"), From e8fcbfb9599cb04808ad7241ad140ac7c961b280 Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Mon, 22 Dec 2014 11:29:31 -0600 Subject: [PATCH 39/44] Add a doctest for the btreemap's entry method. Make some updates to the entry doctest. --- src/libcollections/btree/map.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/libcollections/btree/map.rs b/src/libcollections/btree/map.rs index 01096c1fd4e9..5fb963b15ce5 100644 --- a/src/libcollections/btree/map.rs +++ b/src/libcollections/btree/map.rs @@ -1288,6 +1288,30 @@ pub fn is_empty(&self) -> bool { self.len() == 0 } impl BTreeMap { /// Gets the given key's corresponding entry in the map for in-place manipulation. + /// + /// # Examples + /// + /// ``` + /// use std::collections::BTreeMap; + /// use std::collections::btree_map::Entry; + /// + /// let mut count: BTreeMap<&str, uint> = BTreeMap::new(); + /// + /// // count the number of occurrences of letters in the vec + /// for x in vec!["a","b","a","c","a","b"].iter() { + /// match count.entry(*x) { + /// Entry::Vacant(view) => { + /// view.set(1); + /// }, + /// Entry::Occupied(mut view) => { + /// let v = view.get_mut(); + /// *v += 1; + /// }, + /// } + /// } + /// + /// assert_eq!(count["a"], 3u); + /// ``` pub fn entry<'a>(&'a mut self, mut key: K) -> Entry<'a, K, V> { // same basic logic of `swap` and `pop`, blended together let mut stack = stack::PartialSearchStack::new(self); From 34d43788e52763011b3a6559ac2dad6d6d350a06 Mon Sep 17 00:00:00 2001 From: "Felix S. Klock II" Date: Mon, 22 Dec 2014 19:41:19 +0100 Subject: [PATCH 40/44] fix run-make/ tests now flowgraph printing has moved to the unstable `--xpretty` option. --- src/test/run-make/graphviz-flowgraph/Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/run-make/graphviz-flowgraph/Makefile b/src/test/run-make/graphviz-flowgraph/Makefile index 09440949177d..0562e000e56e 100644 --- a/src/test/run-make/graphviz-flowgraph/Makefile +++ b/src/test/run-make/graphviz-flowgraph/Makefile @@ -28,7 +28,7 @@ $(TMPDIR)/%.pp: %.rs $(TMPDIR)/%.dot: %.rs $(eval $(call FIND_LAST_BLOCK,$<)) - $(RUSTC_LIB) --pretty flowgraph=$(LASTBLOCKNUM_$<) $< -o $@.tmp + $(RUSTC_LIB) -Z unstable-options --xpretty flowgraph=$(LASTBLOCKNUM_$<) $< -o $@.tmp cat $@.tmp | sed -e 's@ (id=[0-9]*)@@g' \ -e 's@\[label=""\]@@' \ -e 's@digraph [a-zA-Z0-9_]* @digraph block @' \ From 20d7a5fc3cc33ed39aff7e5879e6f73d64cb0474 Mon Sep 17 00:00:00 2001 From: Alexis Beingessner Date: Mon, 15 Dec 2014 11:54:35 -0500 Subject: [PATCH 41/44] Make bitv's APIs match RFC + fixup --- src/libcollections/bit.rs | 84 ++++++++++++--------------------------- 1 file changed, 26 insertions(+), 58 deletions(-) diff --git a/src/libcollections/bit.rs b/src/libcollections/bit.rs index dd3369ea1da7..f50e13c1b3c0 100644 --- a/src/libcollections/bit.rs +++ b/src/libcollections/bit.rs @@ -91,7 +91,7 @@ use core::slice::{Items, MutItems}; use core::{u8, u32, uint}; -use hash; +use core::hash; use Vec; type Blocks<'a> = Cloned>; @@ -922,7 +922,7 @@ pub fn from_bytes(bytes: &[u8]) -> Bitv { /// Deprecated: Now a static method on Bitv. #[deprecated = "Now a static method on Bitv"] -pub fn from_fn(len: uint, mut f: F) -> Bitv where F: FnMut(uint) -> bool { +pub fn from_fn(len: uint, f: F) -> Bitv where F: FnMut(uint) -> bool { Bitv::from_fn(len, f) } @@ -1226,12 +1226,12 @@ pub fn capacity(&self) -> uint { self.bitv.capacity() } - /// Reserves capacity for an element to be inserted at `index` in the given - /// `Bitv`. The collection may reserve more space to avoid frequent reallocations. + /// Reserves capacity for the given `BitvSet` to contain `len` distinct elements. In the case + /// of `BitvSet` this means reallocations will not occur as long as all inserted elements + /// are less than `len`. /// - /// # Panics + /// The collection may reserve more space to avoid frequent reallocations. /// - /// Panics if the new capacity overflows `uint`. /// /// # Examples /// @@ -1239,27 +1239,25 @@ pub fn capacity(&self) -> uint { /// use std::collections::BitvSet; /// /// let mut s = BitvSet::new(); - /// s.reserve_index(10); - /// assert!(s.capacity() >= 11); + /// s.reserve_len(10); + /// assert!(s.capacity() >= 10); /// ``` #[unstable = "matches collection reform specification, waiting for dust to settle"] - pub fn reserve_index(&mut self, index: uint) { - let len = self.bitv.len(); - if index >= len { - self.bitv.reserve(index - len + 1); + pub fn reserve_len(&mut self, len: uint) { + let cur_len = self.bitv.len(); + if len >= cur_len { + self.bitv.reserve(len - cur_len); } } - /// Reserves the minimum capacity for an element to be inserted at `index` - /// in the given `BitvSet`. Does nothing if the capacity is already sufficient. + /// Reserves the minimum capacity for the given `BitvSet` to contain `len` distinct elements. + /// In the case of `BitvSet` this means reallocations will not occur as long as all inserted + /// elements are less than `len`. /// /// Note that the allocator may give the collection more space than it requests. Therefore - /// capacity can not be relied upon to be precisely minimal. Prefer `reserve_index` if future + /// capacity can not be relied upon to be precisely minimal. Prefer `reserve_len` if future /// insertions are expected. /// - /// # Panics - /// - /// Panics if the new capacity overflows `uint`. /// /// # Examples /// @@ -1267,14 +1265,14 @@ pub fn reserve_index(&mut self, index: uint) { /// use std::collections::BitvSet; /// /// let mut s = BitvSet::new(); - /// s.reserve_index_exact(10); - /// assert!(s.capacity() >= 11); + /// s.reserve_len_exact(10); + /// assert!(s.capacity() >= 10); /// ``` #[unstable = "matches collection reform specification, waiting for dust to settle"] - pub fn reserve_index_exact(&mut self, index: uint) { - let len = self.bitv.len(); - if index >= len { - self.bitv.reserve_exact(index - len + 1); + pub fn reserve_len_exact(&mut self, len: uint) { + let cur_len = self.bitv.len(); + if len >= cur_len { + self.bitv.reserve_exact(len - cur_len); } } @@ -2233,35 +2231,6 @@ fn test_bitv_iterator() { assert_eq!(bitv.iter().collect::>(), long) } - #[test] - fn test_bitv_set_iterator() { - let bools = [true, false, true, true]; - let bitv: BitvSet = bools.iter().map(|n| *n).collect(); - - let idxs: Vec = bitv.iter().collect(); - assert_eq!(idxs, vec!(0, 2, 3)); - - let long: BitvSet = range(0u, 10000).map(|n| n % 2 == 0).collect(); - let real = range_step(0, 10000, 2).collect::>(); - - let idxs: Vec = long.iter().collect(); - assert_eq!(idxs, real); - } - - #[test] - fn test_bitv_set_frombitv_init() { - let bools = [true, false]; - let lengths = [10, 64, 100]; - for &b in bools.iter() { - for &l in lengths.iter() { - let bitset = BitvSet::from_bitv(Bitv::with_capacity(l, b)); - assert_eq!(bitset.contains(&1u), b); - assert_eq!(bitset.contains(&(l-1u)), b); - assert!(!bitset.contains(&l)) - } - } - } - #[test] fn test_small_difference() { let mut b1 = Bitv::from_elem(3, false); @@ -2587,11 +2556,10 @@ fn bench_bitv_big_iter(b: &mut Bencher) { #[cfg(test)] mod bitv_set_test { - use std::prelude::*; + use prelude::*; use std::iter::range_step; use super::{Bitv, BitvSet}; - use vec::Vec; #[test] fn test_bitv_set_show() { @@ -2636,9 +2604,9 @@ fn test_bitv_set_frombitv_init() { for &b in bools.iter() { for &l in lengths.iter() { let bitset = BitvSet::from_bitv(Bitv::from_elem(l, b)); - assert_eq!(bitset.contains(&1u), b) - assert_eq!(bitset.contains(&(l-1u)), b) - assert!(!bitset.contains(&l)) + assert_eq!(bitset.contains(&1u), b); + assert_eq!(bitset.contains(&(l-1u)), b); + assert!(!bitset.contains(&l)); } } } From 02feaf2a804e4e810d5a3adbedbcbe3905692286 Mon Sep 17 00:00:00 2001 From: Corey Farwell Date: Mon, 22 Dec 2014 11:09:55 -0800 Subject: [PATCH 42/44] Remove cmp::Ordering::* public reexport Part of #19253 I would have removed this public reexport in #19842, but #19812 hadn't merged (and snapshotted) at the time In #19407, I changed the codebase to stop utilizing this reexport [breaking-change] --- src/libcore/cmp.rs | 2 +- src/libcore/tuple.rs | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/libcore/cmp.rs b/src/libcore/cmp.rs index 6e793be67e25..ca523db214b0 100644 --- a/src/libcore/cmp.rs +++ b/src/libcore/cmp.rs @@ -41,7 +41,7 @@ #![stable] -pub use self::Ordering::*; +use self::Ordering::*; use kinds::Sized; use option::Option::{mod, Some, None}; diff --git a/src/libcore/tuple.rs b/src/libcore/tuple.rs index 89aed1487924..a92914c99e35 100644 --- a/src/libcore/tuple.rs +++ b/src/libcore/tuple.rs @@ -69,6 +69,7 @@ use clone::Clone; use cmp::*; +use cmp::Ordering::*; use default::Default; use option::Option; use option::Option::Some; From 7f0d2e8a2b0b69aef99ebb2f915b5f62922e6739 Mon Sep 17 00:00:00 2001 From: Chris Morgan Date: Sat, 29 Nov 2014 10:20:22 +1100 Subject: [PATCH 43/44] RFC 248? I think you meant RFC 438. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit There ain’t an RFC 248, while 438 looks to be what is being referred to: https://github.com/rust-lang/rfcs/blob/master/text/0438-precedence-of-plus.md --- src/librustc_typeck/astconv.rs | 6 +++--- src/libsyntax/parse/parser.rs | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/librustc_typeck/astconv.rs b/src/librustc_typeck/astconv.rs index 4f4bebabead2..befb4bf81e5d 100644 --- a/src/librustc_typeck/astconv.rs +++ b/src/librustc_typeck/astconv.rs @@ -798,14 +798,14 @@ fn ast_ty_to_trait_ref<'tcx,AC,RS>(this: &AC, match ty.node { ast::TyRptr(None, ref mut_ty) => { span_note!(this.tcx().sess, ty.span, - "perhaps you meant `&{}({} +{})`? (per RFC 248)", + "perhaps you meant `&{}({} +{})`? (per RFC 438)", ppaux::mutability_to_string(mut_ty.mutbl), pprust::ty_to_string(&*mut_ty.ty), pprust::bounds_to_string(bounds)); } ast::TyRptr(Some(ref lt), ref mut_ty) => { span_note!(this.tcx().sess, ty.span, - "perhaps you meant `&{} {}({} +{})`? (per RFC 248)", + "perhaps you meant `&{} {}({} +{})`? (per RFC 438)", pprust::lifetime_to_string(lt), ppaux::mutability_to_string(mut_ty.mutbl), pprust::ty_to_string(&*mut_ty.ty), @@ -814,7 +814,7 @@ fn ast_ty_to_trait_ref<'tcx,AC,RS>(this: &AC, _ => { span_note!(this.tcx().sess, ty.span, - "perhaps you forgot parentheses? (per RFC 248)"); + "perhaps you forgot parentheses? (per RFC 438)"); } } Err(ErrorReported) diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 3ad224b93ce9..db195c0f206c 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -1456,7 +1456,7 @@ pub fn parse_ret_ty(&mut self) -> FunctionRetTy { // clauses (i.e., not when parsing something like // `FnMut() -> T + Send`, where the `+` is legal). if self.token == token::BinOp(token::Plus) { - self.warn("deprecated syntax: `()` are required, see RFC 248 for details"); + self.warn("deprecated syntax: `()` are required, see RFC 438 for details"); } Return(t) From 3583d613b9c81855feb067aeeebb525cf8a4184c Mon Sep 17 00:00:00 2001 From: Alex Crichton Date: Mon, 22 Dec 2014 12:56:18 -0800 Subject: [PATCH 44/44] Test fixes and rebase conflicts --- src/libcollections/bit.rs | 6 +++--- src/libcore/str.rs | 2 +- src/librustc/session/config.rs | 3 +-- src/librustc_trans/trans/closure.rs | 11 ----------- src/librustc_typeck/collect.rs | 3 ++- src/libstd/path/windows.rs | 8 ++++++-- src/libstd/sys/unix/os.rs | 2 +- src/libunicode/u_str.rs | 2 +- 8 files changed, 15 insertions(+), 22 deletions(-) diff --git a/src/libcollections/bit.rs b/src/libcollections/bit.rs index 3b9b1eb1fb31..430d7210bf69 100644 --- a/src/libcollections/bit.rs +++ b/src/libcollections/bit.rs @@ -88,14 +88,14 @@ use core::iter::{Cloned, Chain, Enumerate, Repeat, Skip, Take}; use core::iter; use core::num::Int; -use core::slice::{Items, MutItems}; +use core::slice::{Iter, IterMut}; use core::{u8, u32, uint}; use core::hash; use Vec; -type Blocks<'a> = Cloned>; -type MutBlocks<'a> = MutItems<'a, u32>; +type Blocks<'a> = Cloned>; +type MutBlocks<'a> = IterMut<'a, u32>; type MatchWords<'a> = Chain>, Skip>>>>; fn reverse_bits(byte: u8) -> u8 { diff --git a/src/libcore/str.rs b/src/libcore/str.rs index de5b34ff0cad..204ffae6cbd5 100644 --- a/src/libcore/str.rs +++ b/src/libcore/str.rs @@ -336,7 +336,7 @@ fn next_back(&mut self) -> Option<(uint, char)> { #[stable] #[deriving(Clone)] pub struct Bytes<'a> { - inner: Map<&'a u8, u8, slice::Items<'a, u8>, BytesFn>, + inner: Map<&'a u8, u8, slice::Iter<'a, u8>, BytesFn>, } /// A temporary new type wrapper that ensures that the `Bytes` iterator diff --git a/src/librustc/session/config.rs b/src/librustc/session/config.rs index 86f0655a3ad6..6629f6620d48 100644 --- a/src/librustc/session/config.rs +++ b/src/librustc/session/config.rs @@ -743,13 +743,12 @@ pub fn rustc_short_optgroups() -> Vec { assumed.", "NAME[:KIND]"), opt::multi("", "crate-type", "Comma separated list of types of crates for the compiler to emit", - "[bin|lib|rlib|dylib|staticlib]"), + "[bin|lib|rlib|dylib|staticlib]"), opt::opt("", "crate-name", "Specify the name of the crate being built", "NAME"), opt::multi("", "emit", "Comma separated list of types of output for \ the compiler to emit", "[asm|llvm-bc|llvm-ir|obj|link|dep-info]"), - "[asm|llvm-bc|llvm-ir|obj|link]"), opt::multi("", "print", "Comma separated list of compiler information to \ print on stdout", "[crate-name|output-file-names|sysroot]"), diff --git a/src/librustc_trans/trans/closure.rs b/src/librustc_trans/trans/closure.rs index 28716f0a4819..0ae9de8c8918 100644 --- a/src/librustc_trans/trans/closure.rs +++ b/src/librustc_trans/trans/closure.rs @@ -606,17 +606,6 @@ pub fn get_wrapper_for_bare_fn<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, fn_ptr: ValueRef, is_local: bool) -> ValueRef { - let def_id = match def { - def::DefFn(did, _) | def::DefStaticMethod(did, _) | - def::DefVariant(_, did, _) | def::DefStruct(did) => did, - _ => { - ccx.sess().bug(format!("get_wrapper_for_bare_fn: \ - expected a statically resolved fn, got \ - {}", - def)[]); - } - }; - match ccx.closure_bare_wrapper_cache().borrow().get(&fn_ptr) { Some(&llval) => return llval, None => {} diff --git a/src/librustc_typeck/collect.rs b/src/librustc_typeck/collect.rs index c7c33db5746a..8380ed349cb3 100644 --- a/src/librustc_typeck/collect.rs +++ b/src/librustc_typeck/collect.rs @@ -219,7 +219,7 @@ pub fn get_enum_variant_types<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>, ast::TupleVariantKind(ref args) if args.len() > 0 => { let rs = ExplicitRscope; let input_tys: Vec<_> = args.iter().map(|va| ccx.to_ty(&rs, &*va.ty)).collect(); - ty::mk_ctor_fn(tcx, input_tys[], enum_ty) + ty::mk_ctor_fn(tcx, variant_def_id, input_tys[], enum_ty) } ast::TupleVariantKind(_) => { @@ -1282,6 +1282,7 @@ pub fn convert_struct<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>, |field| (*tcx.tcache.borrow())[ local_def(field.node.id)].ty).collect(); let ctor_fn_ty = ty::mk_ctor_fn(tcx, + local_def(ctor_id), inputs[], selfty); write_ty_to_tcx(tcx, ctor_id, ctor_fn_ty); diff --git a/src/libstd/path/windows.rs b/src/libstd/path/windows.rs index b24966241ff6..7d10188c437e 100644 --- a/src/libstd/path/windows.rs +++ b/src/libstd/path/windows.rs @@ -827,8 +827,12 @@ fn update_sepidx(&mut self) { let s = if self.has_nonsemantic_trailing_slash() { self.repr[0..self.repr.len()-1] } else { self.repr[] }; - let idx = s.rfind(if !prefix_is_verbatim(self.prefix) { is_sep } - else { is_sep_verbatim }); + let sep_test: fn(char) -> bool = if !prefix_is_verbatim(self.prefix) { + is_sep + } else { + is_sep_verbatim + }; + let idx = s.rfind(sep_test); let prefixlen = self.prefix_len(); self.sepidx = idx.and_then(|x| if x < prefixlen { None } else { Some(x) }); } diff --git a/src/libstd/sys/unix/os.rs b/src/libstd/sys/unix/os.rs index 6c909d7562d9..316d97064eec 100644 --- a/src/libstd/sys/unix/os.rs +++ b/src/libstd/sys/unix/os.rs @@ -189,7 +189,7 @@ pub fn load_self() -> Option> { if sz == 0 { return None; } let mut v: Vec = Vec::with_capacity(sz as uint); let err = sysctl(mib.as_mut_ptr(), mib.len() as ::libc::c_uint, - v.as_mut_ptr() as *mut c_void, &mut sz, + v.as_mut_ptr() as *mut libc::c_void, &mut sz, ptr::null_mut(), 0u as libc::size_t); if err != 0 { return None; } if sz == 0 { return None; } diff --git a/src/libunicode/u_str.rs b/src/libunicode/u_str.rs index 65b8ad997f6a..a3d4dd057d00 100644 --- a/src/libunicode/u_str.rs +++ b/src/libunicode/u_str.rs @@ -404,7 +404,7 @@ macro_rules! next { ($ret:expr) => { /// of `u16`s. #[deriving(Clone)] pub struct Utf16Items<'a> { - iter: slice::Items<'a, u16> + iter: slice::Iter<'a, u16> } /// The possibilities for values decoded from a `u16` stream. #[deriving(PartialEq, Eq, Clone, Show)]