diff --git a/src/liballoc/lib.rs b/src/liballoc/lib.rs index 503c484e469d7..cc0a783b6f76c 100644 --- a/src/liballoc/lib.rs +++ b/src/liballoc/lib.rs @@ -124,7 +124,8 @@ fn oom() -> ! { #[doc(hidden)] pub fn fixme_14344_be_sure_to_link_to_collections() {} -#[cfg(not(test))] +// NOTE: Remove after next snapshot +#[cfg(stage0, not(test))] #[doc(hidden)] mod std { pub use core::fmt; diff --git a/src/libcollections/bitv.rs b/src/libcollections/bitv.rs index 0a658c51dbd42..2d9dcced14700 100644 --- a/src/libcollections/bitv.rs +++ b/src/libcollections/bitv.rs @@ -70,10 +70,10 @@ use core::iter::{Chain, Enumerate, Repeat, Skip, Take}; use core::iter; use core::slice; use core::uint; -use std::hash; use {Mutable, Set, MutableSet, MutableSeq}; use vec::Vec; +use hash; type MatchWords<'a> = Chain, Skip>>>>; // Take two BitV's, and return iterators of their words, where the shorter one diff --git a/src/libcollections/dlist.rs b/src/libcollections/dlist.rs index 0ed946aa947db..e852bddd4b87a 100644 --- a/src/libcollections/dlist.rs +++ b/src/libcollections/dlist.rs @@ -29,9 +29,9 @@ use core::fmt; use core::iter; use core::mem; use core::ptr; -use std::hash::{Writer, Hash}; use {Mutable, Deque, MutableSeq}; +use hash::{Writer, Hash}; /// A doubly-linked list. pub struct DList { @@ -742,13 +742,13 @@ impl> Hash for DList { mod tests { use std::prelude::*; use std::rand; - use std::hash; use test::Bencher; use test; use {Deque, MutableSeq}; use super::{DList, Node, ListInsertion}; use vec::Vec; + use hash; pub fn check_links(list: &DList) { let mut len = 0u; diff --git a/src/libcollections/lib.rs b/src/libcollections/lib.rs index 834c95497336d..917f0ad9f01e6 100644 --- a/src/libcollections/lib.rs +++ b/src/libcollections/lib.rs @@ -30,8 +30,8 @@ extern crate alloc; #[cfg(test)] extern crate native; #[cfg(test)] extern crate test; #[cfg(test)] extern crate debug; +#[cfg(test)] extern crate std; -#[cfg(test)] #[phase(plugin, link)] extern crate std; #[cfg(test)] #[phase(plugin, link)] extern crate log; use core::prelude::Option; @@ -556,10 +556,12 @@ pub trait Deque : MutableSeq { #[doc(hidden)] pub fn fixme_14344_be_sure_to_link_to_collections() {} +// NOTE: Remove after next snapshot +#[cfg(stage0)] #[cfg(not(test))] mod std { - pub use core::fmt; // necessary for fail!() pub use core::option; // necessary for fail!() + pub use core::fmt; // necessary for fail!() pub use core::clone; // deriving(Clone) pub use core::cmp; // deriving(Eq, Ord, etc.) pub use hash; // deriving(Hash) @@ -568,3 +570,9 @@ mod std { pub use MutableSeq; } } + +mod collections { + pub use hash; // deriving(Hsah) + pub use string; // format!() + pub use vec; // vec!() +} diff --git a/src/libcollections/macros.rs b/src/libcollections/macros.rs index ba8b3b8c7d3e4..89b1e1def07d5 100644 --- a/src/libcollections/macros.rs +++ b/src/libcollections/macros.rs @@ -11,12 +11,30 @@ #![macro_escape] /// Creates a `std::vec::Vec` containing the arguments. +#[macro_export] macro_rules! vec( ($($e:expr),*) => ({ // leading _ to allow empty construction without a warning. - let mut _temp = ::vec::Vec::new(); + let mut _temp = ::collections::vec::Vec::new(); $(_temp.push($e);)* _temp }); ($($e:expr),+,) => (vec!($($e),+)) ) + +/// Use the syntax described in `core::fmt` to create a value of type `String`. +/// See `core::fmt` for more information. +/// +/// # Example +/// +/// ``` +/// format!("test"); +/// format!("hello {}", "world!"); +/// format!("x = {}, y = {y}", 10i, y = 30i); +/// ``` +#[macro_export] +macro_rules! format( + ($($arg:tt)*) => ( + format_args!(::collections::string::String::format, $($arg)*) + ) +) diff --git a/src/libcollections/ringbuf.rs b/src/libcollections/ringbuf.rs index 3665535e72015..6fdbe3116ed4e 100644 --- a/src/libcollections/ringbuf.rs +++ b/src/libcollections/ringbuf.rs @@ -19,10 +19,10 @@ use core::cmp; use core::default::Default; use core::fmt; use core::iter; -use std::hash::{Writer, Hash}; use {Deque, Mutable, MutableSeq}; use vec::Vec; +use hash::{Writer, Hash}; static INITIAL_CAPACITY: uint = 8u; // 2^3 static MINIMUM_CAPACITY: uint = 2u; @@ -527,13 +527,13 @@ mod tests { use std::fmt::Show; use std::prelude::*; use std::gc::{GC, Gc}; - use std::hash; use test::Bencher; use test; use {Deque, Mutable, MutableSeq}; use super::RingBuf; use vec::Vec; + use hash; #[test] fn test_simple() { diff --git a/src/libcollections/slice.rs b/src/libcollections/slice.rs index 36edd913de2f8..e8b61270f982b 100644 --- a/src/libcollections/slice.rs +++ b/src/libcollections/slice.rs @@ -742,10 +742,12 @@ pub mod raw { #[cfg(test)] mod tests { + use core::prelude::*; + use alloc::boxed::Box; + use collections::string::String; use std::cell::Cell; use std::default::Default; use std::mem; - use std::prelude::*; use std::rand::{Rng, task_rng}; use std::rc::Rc; use std::rt; @@ -1183,7 +1185,7 @@ mod tests { assert_eq!(it.next(), None); } { - let v = ["Hello".to_string()]; + let v = [String::from_str("Hello")]; let mut it = v.permutations(); let (min_size, max_opt) = it.size_hint(); assert_eq!(min_size, 1); @@ -1925,20 +1927,20 @@ mod tests { }) ) let empty: Vec = vec![]; - test_show_vec!(empty, "[]".to_string()); - test_show_vec!(vec![1i], "[1]".to_string()); - test_show_vec!(vec![1i, 2, 3], "[1, 2, 3]".to_string()); + test_show_vec!(empty, String::from_str("[]")); + test_show_vec!(vec![1i], String::from_str("[1]")); + test_show_vec!(vec![1i, 2, 3], String::from_str("[1, 2, 3]")); test_show_vec!(vec![vec![], vec![1u], vec![1u, 1u]], - "[[], [1], [1, 1]]".to_string()); + String::from_str("[[], [1], [1, 1]]")); let empty_mut: &mut [int] = &mut[]; - test_show_vec!(empty_mut, "[]".to_string()); + test_show_vec!(empty_mut, String::from_str("[]")); let v: &mut[int] = &mut[1]; - test_show_vec!(v, "[1]".to_string()); + test_show_vec!(v, String::from_str("[1]")); let v: &mut[int] = &mut[1, 2, 3]; - test_show_vec!(v, "[1, 2, 3]".to_string()); + test_show_vec!(v, String::from_str("[1, 2, 3]")); let v: &mut [&mut[uint]] = &mut[&mut[], &mut[1u], &mut[1u, 1u]]; - test_show_vec!(v, "[[], [1], [1, 1]]".to_string()); + test_show_vec!(v, String::from_str("[[], [1], [1, 1]]")); } #[test] diff --git a/src/libcollections/smallintmap.rs b/src/libcollections/smallintmap.rs index dd3a639aeac78..83692f142a857 100644 --- a/src/libcollections/smallintmap.rs +++ b/src/libcollections/smallintmap.rs @@ -520,12 +520,13 @@ pub type Values<'a, T> = #[cfg(test)] mod test_map { - use std::prelude::*; + use core::prelude::*; use vec::Vec; use hash; use {Map, MutableMap, Mutable, MutableSeq}; use super::SmallIntMap; + use string::String; #[test] fn test_find_mut() { @@ -764,6 +765,8 @@ mod test_map { #[test] fn test_show() { + use std::to_string::ToString; + let mut map = SmallIntMap::new(); let empty = SmallIntMap::::new(); @@ -773,7 +776,7 @@ mod test_map { let map_str = map.to_string(); let map_str = map_str.as_slice(); assert!(map_str == "{1: 2, 3: 4}" || map_str == "{3: 4, 1: 2}"); - assert_eq!(format!("{}", empty), "{}".to_string()); + assert_eq!(format!("{}", empty), String::from_str("{}")); } #[test] diff --git a/src/libcollections/string.rs b/src/libcollections/string.rs index 05d91a7504150..225e95e36b715 100644 --- a/src/libcollections/string.rs +++ b/src/libcollections/string.rs @@ -16,6 +16,7 @@ use core::prelude::*; use core::default::Default; use core::fmt; +use core::fmt::FormatWriter; use core::mem; use core::ptr; // FIXME: ICE's abound if you import the `Slice` type while importing `Slice` trait @@ -726,6 +727,28 @@ impl String { pub unsafe fn as_mut_vec<'a>(&'a mut self) -> &'a mut Vec { &mut self.vec } + + /// The format function takes a precompiled format string and a list of + /// arguments, to return the resulting formatted string. + /// + /// # Arguments + /// + /// * args - a structure of arguments generated via the `format_args!` macro. + /// Because this structure can only be safely generated at + /// compile-time, this function is safe. + /// + /// # Example + /// + /// ```rust + /// let s = format_args!(String::format, "Hello, {}!", "world"); + /// assert_eq!(s, "Hello, world!".to_string()); + /// ``` + pub fn format(args: &fmt::Arguments) -> String { + let mut output = vec!(); + let _ = write!(&mut output, "{}", args); + String::from_utf8(output) + .ok().expect("non-utf8 result from write!()") + } } impl Collection for String { @@ -867,7 +890,7 @@ pub mod raw { #[cfg(test)] mod tests { - use std::prelude::*; + use core::prelude::*; use test::Bencher; use {Mutable, MutableSeq}; @@ -878,7 +901,7 @@ mod tests { #[test] fn test_from_str() { - let owned: Option<::std::string::String> = from_str("string"); + let owned: Option<::string::String> = Some(String::from_str("string")); assert_eq!(owned.as_ref().map(|s| s.as_slice()), Some("string")); } @@ -1142,6 +1165,12 @@ mod tests { assert_eq!(b.as_slice(), "1234522"); } + #[test] + fn test_format() { + let s = format_args!(String::format, "Hello, {}!", "world"); + assert_eq!(s.as_slice(), "Hello, world!"); + } + #[bench] fn bench_with_capacity(b: &mut Bencher) { b.iter(|| { diff --git a/src/libcollections/treemap.rs b/src/libcollections/treemap.rs index 80a7c6d4bad1d..9e52ca09a9c96 100644 --- a/src/libcollections/treemap.rs +++ b/src/libcollections/treemap.rs @@ -38,10 +38,10 @@ use core::iter::Peekable; use core::iter; use core::mem::{replace, swap}; use core::ptr; -use std::hash::{Writer, Hash}; use {Mutable, Set, MutableSet, MutableMap, Map, MutableSeq}; use vec::Vec; +use hash::{Writer, Hash}; /// This is implemented as an AA tree, which is a simplified variation of /// a red-black tree where red (horizontal) nodes can only be added @@ -1669,12 +1669,15 @@ impl> Hash for TreeSet { #[cfg(test)] mod test_treemap { - use std::prelude::*; + use core::prelude::*; + use alloc::boxed::Box; use std::rand::Rng; use std::rand; use {Map, MutableMap, Mutable, MutableSeq}; use super::{TreeMap, TreeNode}; + use vec::Vec; + use string::String; #[test] fn find_empty() { @@ -2099,8 +2102,8 @@ mod test_treemap { let map_str = format!("{}", map); - assert!(map_str == "{1: 2, 3: 4}".to_string()); - assert_eq!(format!("{}", empty), "{}".to_string()); + assert!(map_str == String::from_str("{1: 2, 3: 4}")); + assert_eq!(format!("{}", empty), String::from_str("{}")); } #[test] @@ -2247,11 +2250,12 @@ mod bench { #[cfg(test)] mod test_set { - use std::prelude::*; - use std::hash; + use core::prelude::*; use {Set, MutableSet, Mutable, MutableMap, MutableSeq}; use super::{TreeMap, TreeSet}; + use hash; + use string::String; #[test] fn test_clear() { @@ -2550,7 +2554,7 @@ mod test_set { let set_str = format!("{}", set); - assert!(set_str == "{1, 2}".to_string()); - assert_eq!(format!("{}", empty), "{}".to_string()); + assert!(set_str == String::from_str("{1, 2}")); + assert_eq!(format!("{}", empty), String::from_str("{}")); } } diff --git a/src/libcollections/trie.rs b/src/libcollections/trie.rs index fa8bcf94de1cd..dd701074ee855 100644 --- a/src/libcollections/trie.rs +++ b/src/libcollections/trie.rs @@ -21,11 +21,11 @@ use core::mem::zeroed; use core::mem; use core::uint; use core::iter; -use std::hash::{Writer, Hash}; use {Mutable, Map, MutableMap, Set, MutableSet}; use slice::{Items, MutItems}; use slice; +use hash::{Writer, Hash}; // FIXME: #5244: need to manually update the TrieNode constructor static SHIFT: uint = 4; @@ -1016,13 +1016,15 @@ impl<'a> Iterator for SetItems<'a> { #[cfg(test)] mod test_map { - use std::prelude::*; + use core::prelude::*; use std::iter::range_step; use std::uint; - use std::hash; use {MutableMap, Map, MutableSeq}; use super::{TrieMap, TrieNode, Internal, External, Nothing}; + use hash; + use vec::Vec; + use string::String; fn check_integrity(trie: &TrieNode) { assert!(trie.count != 0); @@ -1403,8 +1405,8 @@ mod test_map { let map_str = format!("{}", map); - assert!(map_str == "{1: a, 2: b}".to_string()); - assert_eq!(format!("{}", empty), "{}".to_string()); + assert!(map_str == String::from_str("{1: a, 2: b}")); + assert_eq!(format!("{}", empty), String::from_str("{}")); } #[test] @@ -1543,7 +1545,8 @@ mod bench_map { #[cfg(test)] mod test_set { - use std::prelude::*; + use core::prelude::*; + use collections::string::String; use std::uint; use {MutableSet, Set, MutableSeq}; @@ -1589,8 +1592,8 @@ mod test_set { let set_str = format!("{}", set); - assert!(set_str == "{1, 2}".to_string()); - assert_eq!(format!("{}", empty), "{}".to_string()); + assert!(set_str == String::from_str("{1, 2}")); + assert_eq!(format!("{}", empty), String::from_str("{}")); } #[test] diff --git a/src/libcollections/vec.rs b/src/libcollections/vec.rs index a7005cf454db5..7caf66be17fb7 100644 --- a/src/libcollections/vec.rs +++ b/src/libcollections/vec.rs @@ -1611,6 +1611,13 @@ impl MutableSeq for Vec { } +impl fmt::FormatWriter for Vec { + fn write(&mut self, bytes: &[u8]) -> fmt::Result { + self.push_all(bytes); + Ok(()) + } +} + /// An iterator that moves out of a vector. pub struct MoveItems { allocation: *mut T, // the block of memory allocated for the vector @@ -2041,6 +2048,14 @@ mod tests { assert_eq!(vec.len(), 0); } + #[test] + fn test_format_writer() { + use core::fmt::FormatWriter; + let mut vec: Vec = vec!(b'x', b'y'); + vec.write("abc".as_bytes()); + assert_eq!(vec.as_slice(), b"xyabc"); + } + #[bench] fn bench_new(b: &mut Bencher) { b.iter(|| { diff --git a/src/libcore/atomic.rs b/src/libcore/atomic.rs index e248b934b69c5..9ae28bce8a1de 100644 --- a/src/libcore/atomic.rs +++ b/src/libcore/atomic.rs @@ -13,7 +13,7 @@ #![stable] use intrinsics; -use std::kinds::marker; +use kinds::marker; use cell::UnsafeCell; /// An atomic boolean type. diff --git a/src/libcore/lib.rs b/src/libcore/lib.rs index 7e2ea492d4ccf..f848b4230a62f 100644 --- a/src/libcore/lib.rs +++ b/src/libcore/lib.rs @@ -133,13 +133,20 @@ pub mod tuple; pub mod unit; pub mod fmt; +// NOTE: Remove after next snapshot #[doc(hidden)] -mod core { - pub use failure; +#[cfg(stage0)] +mod std { + pub use clone; + pub use cmp; + pub use kinds; + pub use option; + pub use fmt; } #[doc(hidden)] -mod std { +mod core { + pub use failure; pub use clone; pub use cmp; pub use kinds; diff --git a/src/libcore/macros.rs b/src/libcore/macros.rs index e846f8dbeb404..c9ec948705eea 100644 --- a/src/libcore/macros.rs +++ b/src/libcore/macros.rs @@ -38,7 +38,7 @@ macro_rules! fail( // insufficient, since the user may have // `#[forbid(dead_code)]` and which cannot be overridden. #[inline(always)] - fn _run_fmt(fmt: &::std::fmt::Arguments) -> ! { + fn _run_fmt(fmt: &::core::fmt::Arguments) -> ! { static _FILE_LINE: (&'static str, uint) = (file!(), line!()); ::core::failure::begin_unwind(fmt, &_FILE_LINE) } @@ -74,11 +74,16 @@ macro_rules! debug_assert( /// Runtime assertion for equality, for details see std::macros #[macro_export] macro_rules! assert_eq( - ($cond1:expr, $cond2:expr) => ({ - let c1 = $cond1; - let c2 = $cond2; - if c1 != c2 || c2 != c1 { - fail!("expressions not equal, left: {}, right: {}", c1, c2); + ($given:expr , $expected:expr) => ({ + match (&($given), &($expected)) { + (given_val, expected_val) => { + // check both directions of equality.... + if !((*given_val == *expected_val) && + (*expected_val == *given_val)) { + fail!("assertion failed: `(left == right) && (right == left)` \ + (left: `{}`, right: `{}`)", *given_val, *expected_val) + } + } } }) ) diff --git a/src/libcore/result.rs b/src/libcore/result.rs index bf351ecc89b1f..619954d626c37 100644 --- a/src/libcore/result.rs +++ b/src/libcore/result.rs @@ -278,7 +278,7 @@ use clone::Clone; use cmp::PartialEq; -use std::fmt::Show; +use fmt::Show; use slice; use slice::Slice; use iter::{Iterator, DoubleEndedIterator, FromIterator, ExactSize}; diff --git a/src/librand/lib.rs b/src/librand/lib.rs index 52112380c600a..b50a9bbae03e1 100644 --- a/src/librand/lib.rs +++ b/src/librand/lib.rs @@ -449,6 +449,8 @@ pub struct Open01(pub F); /// ``` pub struct Closed01(pub F); +// NOTE: Remove after next snapshot +#[cfg(stage0)] #[cfg(not(test))] mod std { pub use core::{option, fmt}; // fail!() diff --git a/src/librustc/driver/driver.rs b/src/librustc/driver/driver.rs index b80d53922f8a5..2b1953c5457c7 100644 --- a/src/librustc/driver/driver.rs +++ b/src/librustc/driver/driver.rs @@ -273,6 +273,7 @@ pub fn phase_2_configure_and_expand(sess: &Session, let cfg = syntax::ext::expand::ExpansionConfig { deriving_hash_type_parameter: sess.features.default_type_params.get(), crate_name: crate_name.to_string(), + use_std: front::std_inject::use_std(&krate), }; let ret = syntax::ext::expand::expand_crate(&sess.parse_sess, cfg, diff --git a/src/librustc/front/std_inject.rs b/src/librustc/front/std_inject.rs index 748641ba70c2b..a550eebb621cf 100644 --- a/src/librustc/front/std_inject.rs +++ b/src/librustc/front/std_inject.rs @@ -43,7 +43,7 @@ pub fn maybe_inject_prelude(sess: &Session, krate: ast::Crate) -> ast::Crate { } } -fn use_std(krate: &ast::Crate) -> bool { +pub fn use_std(krate: &ast::Crate) -> bool { !attr::contains_name(krate.attrs.as_slice(), "no_std") } diff --git a/src/librustc/front/test.rs b/src/librustc/front/test.rs index a8c9c50009588..c9182a07156bc 100644 --- a/src/librustc/front/test.rs +++ b/src/librustc/front/test.rs @@ -14,7 +14,7 @@ #![allow(unused_imports)] use driver::session::Session; -use front::config; +use front::{config, std_inject}; use std::slice; use std::mem; @@ -232,6 +232,7 @@ fn generate_test_harness(sess: &Session, ExpansionConfig { deriving_hash_type_parameter: false, crate_name: "test".to_string(), + use_std: std_inject::use_std(&krate), }), path: Vec::new(), testfns: Vec::new(), diff --git a/src/librustrt/lib.rs b/src/librustrt/lib.rs index b4ad1f23154b4..6ab20751a4762 100644 --- a/src/librustrt/lib.rs +++ b/src/librustrt/lib.rs @@ -164,6 +164,8 @@ pub mod shouldnt_be_public { pub use super::local_ptr::compiled::RT_TLS_PTR; } +// NOTE: Remove after next snapshot +#[cfg(stage0)] #[cfg(not(test))] mod std { pub use core::{fmt, option, cmp}; diff --git a/src/libstd/collections/mod.rs b/src/libstd/collections/mod.rs index d98d490a84b27..a450b8b2202d1 100644 --- a/src/libstd/collections/mod.rs +++ b/src/libstd/collections/mod.rs @@ -19,7 +19,7 @@ pub use core_collections::{Set, MutableSet, Deque, MutableSeq}; pub use core_collections::{Bitv, BitvSet, BTree, DList, EnumSet}; pub use core_collections::{PriorityQueue, RingBuf, SmallIntMap}; pub use core_collections::{TreeMap, TreeSet, TrieMap, TrieSet}; -pub use core_collections::{bitv, btree, dlist, enum_set}; +pub use core_collections::{bitv, btree, dlist, enum_set, hash}; pub use core_collections::{priority_queue, ringbuf, smallintmap, treemap, trie}; pub use self::hashmap::{HashMap, HashSet}; diff --git a/src/libstd/fmt.rs b/src/libstd/fmt.rs index f69f94c4db6ce..8225f07e24f3e 100644 --- a/src/libstd/fmt.rs +++ b/src/libstd/fmt.rs @@ -459,10 +459,8 @@ pub use core::fmt::{secret_pointer}; /// let s = format_args!(fmt::format, "Hello, {}!", "world"); /// assert_eq!(s, "Hello, world!".to_string()); /// ``` -pub fn format(args: &Arguments) -> string::String{ - let mut output = io::MemWriter::new(); - let _ = write!(&mut output, "{}", args); - string::String::from_utf8(output.unwrap()).unwrap() +pub fn format(args: &Arguments) -> string::String { + string::String::format(args) } impl<'a> Writer for Formatter<'a> { diff --git a/src/libstd/lib.rs b/src/libstd/lib.rs index 08255ba0cb2f4..95e5b448d3345 100644 --- a/src/libstd/lib.rs +++ b/src/libstd/lib.rs @@ -273,12 +273,13 @@ mod failure; #[doc(hidden)] mod std { // mods used for deriving - pub use clone; - pub use cmp; - pub use hash; + // NOTE: Remove after next snapshot + #[cfg(stage0)] pub use clone; + #[cfg(stage0)] pub use cmp; + #[cfg(stage0)] pub use hash; pub use comm; // used for select!() - pub use fmt; // used for any formatting strings + pub use fmt; // used for fail!() pub use io; // used for println!() pub use local_data; // used for local_data_key!() pub use option; // used for bitflags!{} diff --git a/src/libstd/num/strconv.rs b/src/libstd/num/strconv.rs index 407c8ea61d914..bf61fd36ce7fb 100644 --- a/src/libstd/num/strconv.rs +++ b/src/libstd/num/strconv.rs @@ -21,7 +21,7 @@ use num; use ops::{Add, Sub, Mul, Div, Rem, Neg}; use option::{None, Option, Some}; use slice::{ImmutableSlice, MutableSlice}; -use std::cmp::{PartialOrd, PartialEq}; +use cmp::{PartialOrd, PartialEq}; use str::StrSlice; use string::String; use vec::Vec; diff --git a/src/libsync/lib.rs b/src/libsync/lib.rs index e0acce1cd945a..f2e3dd00ad18f 100644 --- a/src/libsync/lib.rs +++ b/src/libsync/lib.rs @@ -33,8 +33,8 @@ #![no_std] #[phase(plugin, link)] extern crate core; +#[phase(plugin, link)] extern crate collections; extern crate alloc; -extern crate collections; extern crate rustrt; #[cfg(test)] extern crate test; @@ -74,7 +74,8 @@ pub mod comm; mod lock; -#[cfg(not(test))] +// NOTE: Remove after next snapshot +#[cfg(stage0, not(test))] mod std { pub use core::{fmt, option, cmp, clone}; } diff --git a/src/libsyntax/ext/base.rs b/src/libsyntax/ext/base.rs index 6e25b6b73ade6..8426f00b70743 100644 --- a/src/libsyntax/ext/base.rs +++ b/src/libsyntax/ext/base.rs @@ -588,6 +588,9 @@ impl<'a> ExtCtxt<'a> { pub fn ident_of(&self, st: &str) -> ast::Ident { str_to_ident(st) } + pub fn ident_of_std(&self, st: &str) -> ast::Ident { + self.ident_of(if self.ecfg.use_std { "std" } else { st }) + } pub fn name_of(&self, st: &str) -> ast::Name { token::intern(st) } diff --git a/src/libsyntax/ext/build.rs b/src/libsyntax/ext/build.rs index eda373c4fb806..c195b1c222bf2 100644 --- a/src/libsyntax/ext/build.rs +++ b/src/libsyntax/ext/build.rs @@ -376,7 +376,7 @@ impl<'a> AstBuilder for ExtCtxt<'a> { self.path_all(DUMMY_SP, true, vec!( - self.ident_of("std"), + self.ident_of_std("core"), self.ident_of("option"), self.ident_of("Option") ), @@ -651,7 +651,7 @@ impl<'a> AstBuilder for ExtCtxt<'a> { } fn expr_vec_ng(&self, sp: Span) -> P { self.expr_call_global(sp, - vec!(self.ident_of("std"), + vec!(self.ident_of_std("collections"), self.ident_of("vec"), self.ident_of("Vec"), self.ident_of("new")), @@ -671,7 +671,7 @@ impl<'a> AstBuilder for ExtCtxt<'a> { fn expr_some(&self, sp: Span, expr: P) -> P { let some = vec!( - self.ident_of("std"), + self.ident_of_std("core"), self.ident_of("option"), self.ident_of("Some")); self.expr_call_global(sp, some, vec!(expr)) @@ -679,7 +679,7 @@ impl<'a> AstBuilder for ExtCtxt<'a> { fn expr_none(&self, sp: Span) -> P { let none = self.path_global(sp, vec!( - self.ident_of("std"), + self.ident_of_std("core"), self.ident_of("option"), self.ident_of("None"))); self.expr_path(none) @@ -700,10 +700,16 @@ impl<'a> AstBuilder for ExtCtxt<'a> { let expr_file_line_ptr = self.expr_addr_of(span, expr_file_line_tuple); self.expr_call_global( span, - vec!( - self.ident_of("std"), - self.ident_of("rt"), - self.ident_of("begin_unwind")), + if self.ecfg.use_std { + vec!( + self.ident_of("std"), + self.ident_of("rt"), + self.ident_of("begin_unwind")) + } else { + vec!( + self.ident_of("rustrt"), + self.ident_of("begin_unwind")) + }, vec!( self.expr_str(span, msg), expr_file_line_ptr)) @@ -717,7 +723,7 @@ impl<'a> AstBuilder for ExtCtxt<'a> { fn expr_ok(&self, sp: Span, expr: P) -> P { let ok = vec!( - self.ident_of("std"), + self.ident_of_std("core"), self.ident_of("result"), self.ident_of("Ok")); self.expr_call_global(sp, ok, vec!(expr)) @@ -725,17 +731,24 @@ impl<'a> AstBuilder for ExtCtxt<'a> { fn expr_err(&self, sp: Span, expr: P) -> P { let err = vec!( - self.ident_of("std"), + self.ident_of_std("core"), self.ident_of("result"), self.ident_of("Err")); self.expr_call_global(sp, err, vec!(expr)) } fn expr_try(&self, sp: Span, head: P) -> P { - let ok = self.ident_of("Ok"); - let ok_path = self.path_ident(sp, ok); - let err = self.ident_of("Err"); - let err_path = self.path_ident(sp, err); + let ok = vec!( + self.ident_of_std("core"), + self.ident_of("result"), + self.ident_of("Ok")); + let ok_path = self.path_global(sp, ok); + + let err = vec!( + self.ident_of_std("core"), + self.ident_of("result"), + self.ident_of("Err")); + let err_path = self.path_global(sp, err); let binding_variable = self.ident_of("__try_var"); let binding_pat = self.pat_ident(sp, binding_variable); @@ -745,8 +758,9 @@ impl<'a> AstBuilder for ExtCtxt<'a> { let ok_pat = self.pat_enum(sp, ok_path, vec!(binding_pat.clone())); // Err(__try_var) (pattern and expression resp.) - let err_pat = self.pat_enum(sp, err_path, vec!(binding_pat)); - let err_inner_expr = self.expr_call_ident(sp, err, vec!(binding_expr.clone())); + let err_pat = self.pat_enum(sp, err_path.clone(), vec!(binding_pat)); + let err_inner_expr = self.expr_call(sp, self.expr_path(err_path), + vec!(binding_expr.clone())); // return Err(__try_var) let err_expr = self.expr(sp, ast::ExprRet(Some(err_inner_expr))); @@ -796,7 +810,7 @@ impl<'a> AstBuilder for ExtCtxt<'a> { fn pat_some(&self, span: Span, pat: P) -> P { let some = vec!( - self.ident_of("std"), + self.ident_of_std("core"), self.ident_of("option"), self.ident_of("Some")); let path = self.path_global(span, some); @@ -805,7 +819,7 @@ impl<'a> AstBuilder for ExtCtxt<'a> { fn pat_none(&self, span: Span) -> P { let some = vec!( - self.ident_of("std"), + self.ident_of_std("core"), self.ident_of("option"), self.ident_of("None")); let path = self.path_global(span, some); @@ -814,7 +828,7 @@ impl<'a> AstBuilder for ExtCtxt<'a> { fn pat_ok(&self, span: Span, pat: P) -> P { let some = vec!( - self.ident_of("std"), + self.ident_of_std("core"), self.ident_of("result"), self.ident_of("Ok")); let path = self.path_global(span, some); @@ -823,7 +837,7 @@ impl<'a> AstBuilder for ExtCtxt<'a> { fn pat_err(&self, span: Span, pat: P) -> P { let some = vec!( - self.ident_of("std"), + self.ident_of_std("core"), self.ident_of("result"), self.ident_of("Err")); let path = self.path_global(span, some); diff --git a/src/libsyntax/ext/deriving/bounds.rs b/src/libsyntax/ext/deriving/bounds.rs index 0595b0bc7f440..ee2cf8d4676d6 100644 --- a/src/libsyntax/ext/deriving/bounds.rs +++ b/src/libsyntax/ext/deriving/bounds.rs @@ -41,10 +41,12 @@ pub fn expand_deriving_bound(cx: &mut ExtCtxt, } }; + let krate = if cx.ecfg.use_std { "std" } else { "core" }; + let trait_def = TraitDef { span: span, attributes: Vec::new(), - path: Path::new(vec!("std", "kinds", name)), + path: Path::new(vec!(krate, "kinds", name)), additional_bounds: Vec::new(), generics: LifetimeBounds::empty(), methods: vec!() diff --git a/src/libsyntax/ext/deriving/clone.rs b/src/libsyntax/ext/deriving/clone.rs index 64607ffd5d4c9..8b9fcbcc9027c 100644 --- a/src/libsyntax/ext/deriving/clone.rs +++ b/src/libsyntax/ext/deriving/clone.rs @@ -27,7 +27,7 @@ pub fn expand_deriving_clone(cx: &mut ExtCtxt, let trait_def = TraitDef { span: span, attributes: Vec::new(), - path: Path::new(vec!("std", "clone", "Clone")), + path: quote_path_std!(cx, core::clone::Clone), additional_bounds: Vec::new(), generics: LifetimeBounds::empty(), methods: vec!( diff --git a/src/libsyntax/ext/deriving/cmp/eq.rs b/src/libsyntax/ext/deriving/cmp/eq.rs index a27016fde6156..6c224780f6775 100644 --- a/src/libsyntax/ext/deriving/cmp/eq.rs +++ b/src/libsyntax/ext/deriving/cmp/eq.rs @@ -42,7 +42,7 @@ pub fn expand_deriving_eq(cx: &mut ExtCtxt, generics: LifetimeBounds::empty(), explicit_self: borrowed_explicit_self(), args: vec!(borrowed_self()), - ret_ty: Literal(Path::new(vec!("bool"))), + ret_ty: Literal(quote_path!(bool)), attributes: attrs, combine_substructure: combine_substructure(|a, b, c| { $f(a, b, c) @@ -54,7 +54,7 @@ pub fn expand_deriving_eq(cx: &mut ExtCtxt, let trait_def = TraitDef { span: span, attributes: Vec::new(), - path: Path::new(vec!("std", "cmp", "PartialEq")), + path: quote_path_std!(cx, core::cmp::PartialEq), additional_bounds: Vec::new(), generics: LifetimeBounds::empty(), methods: vec!( diff --git a/src/libsyntax/ext/deriving/cmp/ord.rs b/src/libsyntax/ext/deriving/cmp/ord.rs index 7cb61d295c0d4..f85228436f273 100644 --- a/src/libsyntax/ext/deriving/cmp/ord.rs +++ b/src/libsyntax/ext/deriving/cmp/ord.rs @@ -32,7 +32,7 @@ pub fn expand_deriving_ord(cx: &mut ExtCtxt, generics: LifetimeBounds::empty(), explicit_self: borrowed_explicit_self(), args: vec!(borrowed_self()), - ret_ty: Literal(Path::new(vec!("bool"))), + ret_ty: Literal(quote_path!(bool)), attributes: attrs, combine_substructure: combine_substructure(|cx, span, substr| { cs_op($op, $equal, cx, span, substr) @@ -41,8 +41,8 @@ pub fn expand_deriving_ord(cx: &mut ExtCtxt, } } ); - let ordering_ty = Literal(Path::new(vec!["std", "cmp", "Ordering"])); - let ret_ty = Literal(Path::new_(vec!["std", "option", "Option"], + let ordering_ty = Literal(quote_path_std!(cx, core::cmp::Ordering)); + let ret_ty = Literal(Path::new_(quote_path_vec_std!(cx, core::option::Option), None, vec![box ordering_ty], true)); @@ -65,7 +65,7 @@ pub fn expand_deriving_ord(cx: &mut ExtCtxt, let trait_def = TraitDef { span: span, attributes: vec![], - path: Path::new(vec!["std", "cmp", "PartialOrd"]), + path: quote_path_std!(cx, core::cmp::PartialOrd), additional_bounds: vec![], generics: LifetimeBounds::empty(), methods: vec![ @@ -101,7 +101,7 @@ pub fn cs_partial_cmp(cx: &mut ExtCtxt, span: Span, substr: &Substructure) -> P { let test_id = cx.ident_of("__test"); let ordering = cx.path_global(span, - vec!(cx.ident_of("std"), + vec!(cx.ident_of_std("core"), cx.ident_of("cmp"), cx.ident_of("Equal"))); let ordering = cx.expr_path(ordering); diff --git a/src/libsyntax/ext/deriving/cmp/totaleq.rs b/src/libsyntax/ext/deriving/cmp/totaleq.rs index 98c8885f7fa01..4306090908378 100644 --- a/src/libsyntax/ext/deriving/cmp/totaleq.rs +++ b/src/libsyntax/ext/deriving/cmp/totaleq.rs @@ -44,7 +44,7 @@ pub fn expand_deriving_totaleq(cx: &mut ExtCtxt, let trait_def = TraitDef { span: span, attributes: Vec::new(), - path: Path::new(vec!("std", "cmp", "Eq")), + path: quote_path_std!(cx, core::cmp::Eq), additional_bounds: Vec::new(), generics: LifetimeBounds::empty(), methods: vec!( diff --git a/src/libsyntax/ext/deriving/cmp/totalord.rs b/src/libsyntax/ext/deriving/cmp/totalord.rs index 9ef463f9c630e..7cd7504143fa1 100644 --- a/src/libsyntax/ext/deriving/cmp/totalord.rs +++ b/src/libsyntax/ext/deriving/cmp/totalord.rs @@ -28,7 +28,7 @@ pub fn expand_deriving_totalord(cx: &mut ExtCtxt, let trait_def = TraitDef { span: span, attributes: Vec::new(), - path: Path::new(vec!("std", "cmp", "Ord")), + path: quote_path_std!(cx, core::cmp::Ord), additional_bounds: Vec::new(), generics: LifetimeBounds::empty(), methods: vec!( @@ -37,7 +37,7 @@ pub fn expand_deriving_totalord(cx: &mut ExtCtxt, generics: LifetimeBounds::empty(), explicit_self: borrowed_explicit_self(), args: vec!(borrowed_self()), - ret_ty: Literal(Path::new(vec!("std", "cmp", "Ordering"))), + ret_ty: Literal(quote_path_std!(cx, core::cmp::Ordering)), attributes: attrs, combine_substructure: combine_substructure(|a, b, c| { cs_cmp(a, b, c) @@ -62,7 +62,7 @@ pub fn cs_cmp(cx: &mut ExtCtxt, span: Span, substr: &Substructure) -> P { let test_id = cx.ident_of("__test"); let equals_path = cx.path_global(span, - vec!(cx.ident_of("std"), + vec!(cx.ident_of_std("core"), cx.ident_of("cmp"), cx.ident_of("Equal"))); diff --git a/src/libsyntax/ext/deriving/decodable.rs b/src/libsyntax/ext/deriving/decodable.rs index fd24f5e35a446..a062ed12027f1 100644 --- a/src/libsyntax/ext/deriving/decodable.rs +++ b/src/libsyntax/ext/deriving/decodable.rs @@ -31,14 +31,14 @@ pub fn expand_deriving_decodable(cx: &mut ExtCtxt, let trait_def = TraitDef { span: span, attributes: Vec::new(), - path: Path::new_(vec!("serialize", "Decodable"), None, + path: Path::new_(quote_path_vec!(serialize::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, + quote_path_vec!(serialize::Decoder), None, vec!(box Literal(Path::new_local("__E"))), true))), ("__E", None, vec!())) }, @@ -49,7 +49,7 @@ pub fn expand_deriving_decodable(cx: &mut ExtCtxt, explicit_self: None, args: vec!(Ptr(box Literal(Path::new_local("__D")), Borrowed(None, MutMutable))), - ret_ty: Literal(Path::new_(vec!("std", "result", "Result"), None, + ret_ty: Literal(Path::new_(quote_path_vec_std!(cx, core::result::Result), None, vec!(box Self, box Literal(Path::new_local("__E"))), true)), attributes: Vec::new(), diff --git a/src/libsyntax/ext/deriving/default.rs b/src/libsyntax/ext/deriving/default.rs index f4a66414d89bd..200df0f9fc1ce 100644 --- a/src/libsyntax/ext/deriving/default.rs +++ b/src/libsyntax/ext/deriving/default.rs @@ -27,7 +27,7 @@ pub fn expand_deriving_default(cx: &mut ExtCtxt, let trait_def = TraitDef { span: span, attributes: Vec::new(), - path: Path::new(vec!("std", "default", "Default")), + path: quote_path_std!(cx, core::default::Default), additional_bounds: Vec::new(), generics: LifetimeBounds::empty(), methods: vec!( @@ -48,7 +48,7 @@ pub fn expand_deriving_default(cx: &mut ExtCtxt, fn default_substructure(cx: &mut ExtCtxt, trait_span: Span, substr: &Substructure) -> P { let default_ident = vec!( - cx.ident_of("std"), + cx.ident_of_std("core"), cx.ident_of("default"), cx.ident_of("Default"), cx.ident_of("default") diff --git a/src/libsyntax/ext/deriving/encodable.rs b/src/libsyntax/ext/deriving/encodable.rs index 103253560df65..e66e95e2ae212 100644 --- a/src/libsyntax/ext/deriving/encodable.rs +++ b/src/libsyntax/ext/deriving/encodable.rs @@ -96,14 +96,14 @@ pub fn expand_deriving_encodable(cx: &mut ExtCtxt, let trait_def = TraitDef { span: span, attributes: Vec::new(), - path: Path::new_(vec!("serialize", "Encodable"), None, + path: Path::new_(quote_path_vec!(serialize::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, + quote_path_vec!(serialize::Encoder), None, vec!(box Literal(Path::new_local("__E"))), true))), ("__E", None, vec!())) }, @@ -114,7 +114,7 @@ pub fn expand_deriving_encodable(cx: &mut ExtCtxt, explicit_self: borrowed_explicit_self(), args: vec!(Ptr(box Literal(Path::new_local("__S")), Borrowed(None, MutMutable))), - ret_ty: Literal(Path::new_(vec!("std", "result", "Result"), + ret_ty: Literal(Path::new_(quote_path_vec_std!(cx, core::result::Result), None, vec!(box Tuple(Vec::new()), box Literal(Path::new_local("__E"))), diff --git a/src/libsyntax/ext/deriving/hash.rs b/src/libsyntax/ext/deriving/hash.rs index b7f11c2582548..55b4e2d88dfa1 100644 --- a/src/libsyntax/ext/deriving/hash.rs +++ b/src/libsyntax/ext/deriving/hash.rs @@ -24,18 +24,18 @@ pub fn expand_deriving_hash(cx: &mut ExtCtxt, push: |P|) { let (path, generics, args) = if cx.ecfg.deriving_hash_type_parameter { - (Path::new_(vec!("std", "hash", "Hash"), None, + (Path::new_(quote_path_vec_std!(cx, collections::hash::Hash), None, vec!(box Literal(Path::new_local("__S"))), true), LifetimeBounds { lifetimes: Vec::new(), bounds: vec!(("__S", None, - vec!(Path::new(vec!("std", "hash", "Writer"))))), + vec!(quote_path_std!(cx, collections::hash::Writer)))), }, Path::new_local("__S")) } else { - (Path::new(vec!("std", "hash", "Hash")), + (quote_path_std!(cx, collections::hash::Hash), LifetimeBounds::empty(), - Path::new(vec!("std", "hash", "sip", "SipState"))) + quote_path_std!(cx, collections::hash::sip::SipState)) }; let inline = cx.meta_word(span, InternedString::new("inline")); let attrs = vec!(cx.attribute(span, inline)); diff --git a/src/libsyntax/ext/deriving/mod.rs b/src/libsyntax/ext/deriving/mod.rs index b8cebd8ea201c..a307dc88fa40b 100644 --- a/src/libsyntax/ext/deriving/mod.rs +++ b/src/libsyntax/ext/deriving/mod.rs @@ -13,8 +13,6 @@ The compiler code necessary to implement the `#[deriving]` extensions. FIXME (#2810): hygiene. Search for "__" strings (in other files too). -We also assume "extra" is the standard library, and "std" is the core -library. */ @@ -23,6 +21,34 @@ use ext::base::ExtCtxt; use codemap::Span; use ptr::P; +macro_rules! quote_path_vec ( + ($($x:ident)::+) => ( + vec![ $( stringify!($x) ),+ ] + ) +) + +macro_rules! quote_path ( + ($($x:tt)*) => ( + ::ext::deriving::generic::ty::Path::new( quote_path_vec!( $($x)* ) ) + ) +) + +macro_rules! quote_path_vec_std ( + ($cx:expr, $first:ident :: $($rest:ident)::+) => ( + if $cx.ecfg.use_std { + quote_path_vec!(std :: $($rest)::+) + } else { + quote_path_vec!($first :: $($rest)::+) + } + ) +) + +macro_rules! quote_path_std ( + ($($x:tt)*) => ( + ::ext::deriving::generic::ty::Path::new( quote_path_vec_std!( $($x)* ) ) + ) +) + pub mod bounds; pub mod clone; pub mod encodable; diff --git a/src/libsyntax/ext/deriving/primitive.rs b/src/libsyntax/ext/deriving/primitive.rs index 044a2812c0003..dd4d2535df837 100644 --- a/src/libsyntax/ext/deriving/primitive.rs +++ b/src/libsyntax/ext/deriving/primitive.rs @@ -28,7 +28,7 @@ pub fn expand_deriving_from_primitive(cx: &mut ExtCtxt, let trait_def = TraitDef { span: span, attributes: Vec::new(), - path: Path::new(vec!("std", "num", "FromPrimitive")), + path: quote_path_std!(cx, core::num::FromPrimitive), additional_bounds: Vec::new(), generics: LifetimeBounds::empty(), methods: vec!( @@ -36,9 +36,8 @@ pub fn expand_deriving_from_primitive(cx: &mut ExtCtxt, name: "from_i64", generics: LifetimeBounds::empty(), explicit_self: None, - args: vec!( - Literal(Path::new(vec!("i64")))), - ret_ty: Literal(Path::new_(vec!("std", "option", "Option"), + args: vec!(Literal(quote_path!(i64))), + ret_ty: Literal(Path::new_(quote_path_vec_std!(cx, core::option::Option), None, vec!(box Self), true)), @@ -52,9 +51,8 @@ pub fn expand_deriving_from_primitive(cx: &mut ExtCtxt, name: "from_u64", generics: LifetimeBounds::empty(), explicit_self: None, - args: vec!( - Literal(Path::new(vec!("u64")))), - ret_ty: Literal(Path::new_(vec!("std", "option", "Option"), + args: vec!(Literal(quote_path!(u64))), + ret_ty: Literal(Path::new_(quote_path_vec_std!(cx, core::option::Option), None, vec!(box Self), true)), diff --git a/src/libsyntax/ext/deriving/rand.rs b/src/libsyntax/ext/deriving/rand.rs index 584645bb30639..cfffab2520b2e 100644 --- a/src/libsyntax/ext/deriving/rand.rs +++ b/src/libsyntax/ext/deriving/rand.rs @@ -22,10 +22,19 @@ pub fn expand_deriving_rand(cx: &mut ExtCtxt, mitem: &MetaItem, item: &Item, push: |P|) { + let rng = if cx.ecfg.use_std { + quote_path!(std::rand::Rng) + } else { + quote_path!(rand::Rng) + }; let trait_def = TraitDef { span: span, attributes: Vec::new(), - path: Path::new(vec!("std", "rand", "Rand")), + path: if cx.ecfg.use_std { + quote_path!(std::rand::Rand) + } else { + quote_path!(rand::Rand) + }, additional_bounds: Vec::new(), generics: LifetimeBounds::empty(), methods: vec!( @@ -33,9 +42,7 @@ pub fn expand_deriving_rand(cx: &mut ExtCtxt, name: "rand", generics: LifetimeBounds { lifetimes: Vec::new(), - bounds: vec!(("R", - None, - vec!( Path::new(vec!("std", "rand", "Rng")) ))) + bounds: vec!(("R", None, vec!(rng))), }, explicit_self: None, args: vec!( @@ -58,12 +65,18 @@ fn rand_substructure(cx: &mut ExtCtxt, trait_span: Span, substr: &Substructure) [ref rng] => rng, _ => cx.bug("Incorrect number of arguments to `rand` in `deriving(Rand)`") }; - let rand_ident = vec!( - cx.ident_of("std"), - cx.ident_of("rand"), - cx.ident_of("Rand"), - cx.ident_of("rand") - ); + let rand_ident = if cx.ecfg.use_std { + vec!( + cx.ident_of("std"), + cx.ident_of("rand"), + cx.ident_of("Rand"), + cx.ident_of("rand")) + } else { + vec!( + cx.ident_of("rand"), + cx.ident_of("Rand"), + cx.ident_of("rand")) + }; let rand_call = |cx: &mut ExtCtxt, span| { cx.expr_call_global(span, rand_ident.clone(), diff --git a/src/libsyntax/ext/deriving/show.rs b/src/libsyntax/ext/deriving/show.rs index 16ce264fe712d..65fd19a97aa40 100644 --- a/src/libsyntax/ext/deriving/show.rs +++ b/src/libsyntax/ext/deriving/show.rs @@ -27,13 +27,13 @@ pub fn expand_deriving_show(cx: &mut ExtCtxt, item: &Item, push: |P|) { // &mut ::std::fmt::Formatter - let fmtr = Ptr(box Literal(Path::new(vec!("std", "fmt", "Formatter"))), + let fmtr = Ptr(box Literal(quote_path_std!(cx, core::fmt::Formatter)), Borrowed(None, ast::MutMutable)); let trait_def = TraitDef { span: span, attributes: Vec::new(), - path: Path::new(vec!("std", "fmt", "Show")), + path: quote_path_std!(cx, core::fmt::Show), additional_bounds: Vec::new(), generics: LifetimeBounds::empty(), methods: vec!( @@ -42,7 +42,7 @@ pub fn expand_deriving_show(cx: &mut ExtCtxt, generics: LifetimeBounds::empty(), explicit_self: borrowed_explicit_self(), args: vec!(fmtr), - ret_ty: Literal(Path::new(vec!("std", "fmt", "Result"))), + ret_ty: Literal(quote_path_std!(cx, core::fmt::Result)), attributes: Vec::new(), combine_substructure: combine_substructure(|a, b, c| { show_substructure(a, b, c) diff --git a/src/libsyntax/ext/deriving/zero.rs b/src/libsyntax/ext/deriving/zero.rs index 7f265b529ffea..ef4e42c75d885 100644 --- a/src/libsyntax/ext/deriving/zero.rs +++ b/src/libsyntax/ext/deriving/zero.rs @@ -27,7 +27,7 @@ pub fn expand_deriving_zero(cx: &mut ExtCtxt, let trait_def = TraitDef { span: span, attributes: Vec::new(), - path: Path::new(vec!("std", "num", "Zero")), + path: quote_path_std!(cx, core::num::Zero), additional_bounds: Vec::new(), generics: LifetimeBounds::empty(), methods: vec!( @@ -47,7 +47,7 @@ pub fn expand_deriving_zero(cx: &mut ExtCtxt, generics: LifetimeBounds::empty(), explicit_self: borrowed_explicit_self(), args: Vec::new(), - ret_ty: Literal(Path::new(vec!("bool"))), + ret_ty: Literal(quote_path!(bool)), attributes: attrs, combine_substructure: combine_substructure(|cx, span, substr| { cs_and(|cx, span, _, _| cx.span_bug(span, @@ -64,7 +64,7 @@ pub fn expand_deriving_zero(cx: &mut ExtCtxt, fn zero_substructure(cx: &mut ExtCtxt, trait_span: Span, substr: &Substructure) -> P { let zero_ident = vec!( - cx.ident_of("std"), + cx.ident_of_std("core"), cx.ident_of("num"), cx.ident_of("Zero"), cx.ident_of("zero") diff --git a/src/libsyntax/ext/env.rs b/src/libsyntax/ext/env.rs index 69574ee669678..b5e31d980d7cc 100644 --- a/src/libsyntax/ext/env.rs +++ b/src/libsyntax/ext/env.rs @@ -34,7 +34,7 @@ pub fn expand_option_env<'cx>(cx: &'cx mut ExtCtxt, sp: Span, tts: &[ast::TokenT None => { cx.expr_path(cx.path_all(sp, true, - vec!(cx.ident_of("std"), + vec!(cx.ident_of_std("core"), cx.ident_of("option"), cx.ident_of("None")), Vec::new(), @@ -48,7 +48,7 @@ pub fn expand_option_env<'cx>(cx: &'cx mut ExtCtxt, sp: Span, tts: &[ast::TokenT } Some(s) => { cx.expr_call_global(sp, - vec!(cx.ident_of("std"), + vec!(cx.ident_of_std("core"), cx.ident_of("option"), cx.ident_of("Some")), vec!(cx.expr_str(sp, diff --git a/src/libsyntax/ext/expand.rs b/src/libsyntax/ext/expand.rs index 310f7c4f3036f..0e374abf67de2 100644 --- a/src/libsyntax/ext/expand.rs +++ b/src/libsyntax/ext/expand.rs @@ -976,6 +976,9 @@ fn new_span(cx: &ExtCtxt, sp: Span) -> Span { pub struct ExpansionConfig { pub deriving_hash_type_parameter: bool, pub crate_name: String, + + // Do we refer to things through their std re-exports? + pub use_std: bool, } pub struct ExportedMacros { @@ -1120,7 +1123,7 @@ impl<'a, 'v> Visitor<'v> for MacroExterminator<'a> { #[cfg(test)] mod test { use super::{pattern_bindings, expand_crate, contains_macro_escape}; - use super::{PatIdentFinder, IdentRenamer, PatIdentRenamer}; + use super::{PatIdentFinder, IdentRenamer, PatIdentRenamer, ExpansionConfig}; use ast; use ast::{Attribute_, AttrOuter, MetaWord, Name}; use attr; @@ -1185,6 +1188,14 @@ mod test { // these following tests are quite fragile, in that they don't test what // *kind* of failure occurs. + fn test_ecfg() -> ExpansionConfig { + ExpansionConfig { + deriving_hash_type_parameter: false, + crate_name: "test".to_string(), + use_std: true, + } + } + // make sure that macros can't escape fns #[should_fail] #[test] fn macros_cant_escape_fns_test () { @@ -1196,11 +1207,7 @@ mod test { src, Vec::new(), &sess); // should fail: - let cfg = ::syntax::ext::expand::ExpansionConfig { - deriving_hash_type_parameter: false, - crate_name: "test".to_string(), - }; - expand_crate(&sess,cfg,vec!(),vec!(),crate_ast); + expand_crate(&sess,test_ecfg(),vec!(),vec!(),crate_ast); } // make sure that macros can't escape modules @@ -1213,11 +1220,7 @@ mod test { "".to_string(), src, Vec::new(), &sess); - let cfg = ::syntax::ext::expand::ExpansionConfig { - deriving_hash_type_parameter: false, - crate_name: "test".to_string(), - }; - expand_crate(&sess,cfg,vec!(),vec!(),crate_ast); + expand_crate(&sess,test_ecfg(),vec!(),vec!(),crate_ast); } // macro_escape modules should allow macros to escape @@ -1229,11 +1232,7 @@ mod test { "".to_string(), src, Vec::new(), &sess); - let cfg = ::syntax::ext::expand::ExpansionConfig { - deriving_hash_type_parameter: false, - crate_name: "test".to_string(), - }; - expand_crate(&sess, cfg, vec!(), vec!(), crate_ast); + expand_crate(&sess, test_ecfg(), vec!(), vec!(), crate_ast); } #[test] fn test_contains_flatten (){ @@ -1266,11 +1265,7 @@ mod test { let ps = parse::new_parse_sess(); let crate_ast = string_to_parser(&ps, crate_str).parse_crate_mod(); // the cfg argument actually does matter, here... - let cfg = ::syntax::ext::expand::ExpansionConfig { - deriving_hash_type_parameter: false, - crate_name: "test".to_string(), - }; - expand_crate(&ps,cfg,vec!(),vec!(),crate_ast) + expand_crate(&ps,test_ecfg(),vec!(),vec!(),crate_ast) } // find the pat_ident paths in a crate diff --git a/src/libsyntax/ext/format.rs b/src/libsyntax/ext/format.rs index 271a5137bbf36..c8244d8afb238 100644 --- a/src/libsyntax/ext/format.rs +++ b/src/libsyntax/ext/format.rs @@ -338,7 +338,7 @@ impl<'a, 'b> Context<'a, 'b> { } fn rtpath(ecx: &ExtCtxt, s: &str) -> Vec { - vec![ecx.ident_of("std"), ecx.ident_of("fmt"), ecx.ident_of("rt"), ecx.ident_of(s)] + vec![ecx.ident_of_std("core"), ecx.ident_of("fmt"), ecx.ident_of("rt"), ecx.ident_of(s)] } fn trans_count(&self, c: parse::Count) -> P { @@ -595,7 +595,7 @@ impl<'a, 'b> Context<'a, 'b> { }; let result = self.ecx.expr_call_global(self.fmtsp, vec!( - self.ecx.ident_of("std"), + self.ecx.ident_of_std("core"), self.ecx.ident_of("fmt"), self.ecx.ident_of("Arguments"), self.ecx.ident_of(fn_name)), fn_args); @@ -664,52 +664,57 @@ impl<'a, 'b> Context<'a, 'b> { fn format_arg(ecx: &ExtCtxt, sp: Span, ty: &ArgumentType, arg: P) -> P { - let (krate, fmt_fn) = match *ty { + let mut krate = ecx.ident_of_std("core"); + let fmt_fn = match *ty { Known(ref tyname) => { match tyname.as_slice() { - "" => ("std", "secret_show"), - "?" => ("debug", "secret_poly"), - "b" => ("std", "secret_bool"), - "c" => ("std", "secret_char"), - "d" | "i" => ("std", "secret_signed"), - "e" => ("std", "secret_lower_exp"), - "E" => ("std", "secret_upper_exp"), - "f" => ("std", "secret_float"), - "o" => ("std", "secret_octal"), - "p" => ("std", "secret_pointer"), - "s" => ("std", "secret_string"), - "t" => ("std", "secret_binary"), - "u" => ("std", "secret_unsigned"), - "x" => ("std", "secret_lower_hex"), - "X" => ("std", "secret_upper_hex"), + "?" => { + krate = ecx.ident_of("debug"); + "secret_poly" + } + + "" => "secret_show", + "b" => "secret_bool", + "c" => "secret_char", + "d" | "i" => "secret_signed", + "e" => "secret_lower_exp", + "E" => "secret_upper_exp", + "f" => "secret_float", + "o" => "secret_octal", + "p" => "secret_pointer", + "s" => "secret_string", + "t" => "secret_binary", + "u" => "secret_unsigned", + "x" => "secret_lower_hex", + "X" => "secret_upper_hex", _ => { ecx.span_err(sp, format!("unknown format trait `{}`", *tyname).as_slice()); - ("std", "dummy") + "dummy" } } } String => { return ecx.expr_call_global(sp, vec![ - ecx.ident_of("std"), + ecx.ident_of_std("core"), ecx.ident_of("fmt"), ecx.ident_of("argumentstr")], vec![arg]) } Unsigned => { return ecx.expr_call_global(sp, vec![ - ecx.ident_of("std"), + ecx.ident_of_std("core"), ecx.ident_of("fmt"), ecx.ident_of("argumentuint")], vec![arg]) } }; let format_fn = ecx.path_global(sp, vec![ - ecx.ident_of(krate), + krate, ecx.ident_of("fmt"), ecx.ident_of(fmt_fn)]); ecx.expr_call_global(sp, vec![ - ecx.ident_of("std"), + ecx.ident_of_std("core"), ecx.ident_of("fmt"), ecx.ident_of("argument")], vec![ecx.expr_path(format_fn), arg]) } diff --git a/src/libunicode/lib.rs b/src/libunicode/lib.rs index 239966cfd475d..68cfa66a16bed 100644 --- a/src/libunicode/lib.rs +++ b/src/libunicode/lib.rs @@ -76,6 +76,8 @@ pub mod str { } // this lets us use #[deriving(Clone)] +// NOTE: Remove after next snapshot +#[cfg(stage0)] mod std { pub use core::clone; pub use core::cmp; diff --git a/src/test/run-pass/deriving-no-std.rs b/src/test/run-pass/deriving-no-std.rs new file mode 100644 index 0000000000000..44688e0f1d0fc --- /dev/null +++ b/src/test/run-pass/deriving-no-std.rs @@ -0,0 +1,36 @@ +// 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. + +#![no_std] + +extern crate core; +extern crate rand; +extern crate native; +extern crate serialize; +extern crate collections; + +// Issue #16803 + +#[deriving(Clone, Hash, Encodable, Decodable, PartialEq, Eq, PartialOrd, Ord, + Rand, Show, Zero, Default)] +struct Foo { + x: uint, +} + +// needed for Zero +impl core::ops::Add for Foo { + fn add(&self, rhs: &Foo) -> Foo { + Foo { x: self.x + rhs.x } + } +} + +fn main() { + Foo { x: 0 }; +} diff --git a/src/test/run-pass/format-no-std.rs b/src/test/run-pass/format-no-std.rs new file mode 100644 index 0000000000000..2d86f3f21128a --- /dev/null +++ b/src/test/run-pass/format-no-std.rs @@ -0,0 +1,37 @@ +// 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(phase)] +#![deny(warnings)] +#![no_std] + +#[phase(plugin, link)] +extern crate core; + +#[phase(plugin, link)] +extern crate collections; + +extern crate native; + +use core::str::Str; + +pub fn main() { + let s = format!("{}", 1i); + assert_eq!(s.as_slice(), "1"); + + let s = format!("test"); + assert_eq!(s.as_slice(), "test"); + + let s = format!("{test}", test=3i); + assert_eq!(s.as_slice(), "3"); + + let s = format!("hello {}", "world"); + assert_eq!(s.as_slice(), "hello world"); +} diff --git a/src/test/run-pass/vec-macro-no-std.rs b/src/test/run-pass/vec-macro-no-std.rs new file mode 100644 index 0000000000000..f27b8aa945d5b --- /dev/null +++ b/src/test/run-pass/vec-macro-no-std.rs @@ -0,0 +1,34 @@ +// 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(phase)] +#![no_std] + +#[phase(plugin, link)] +extern crate core; + +#[phase(plugin, link)] +extern crate collections; + +extern crate native; + +use core::option::Some; +use collections::vec::Vec; +use collections::MutableSeq; + +// Issue #16806 + +fn main() { + let x: Vec = vec!(0, 1, 2); + match x.last() { + Some(&2) => (), + _ => fail!(), + } +}