Skip to content

Commit

Permalink
Don't use std:: paths in syntax extensions when compiling a #![no_std…
Browse files Browse the repository at this point in the history
…] crate

Fixes #16803.
Fixes #14342.
Fixes half of #21827 -- slice syntax is still broken.
  • Loading branch information
Keegan McAllister committed Feb 7, 2015
1 parent 74eef05 commit 67350bc
Show file tree
Hide file tree
Showing 41 changed files with 413 additions and 145 deletions.
4 changes: 0 additions & 4 deletions src/doc/trpl/unsafe.md
Expand Up @@ -576,10 +576,6 @@ extern fn panic_fmt(args: &core::fmt::Arguments,
#[lang = "eh_personality"] extern fn eh_personality() {}
# #[start] fn start(argc: isize, argv: *const *const u8) -> isize { 0 }
# fn main() {}
# mod std { // for-loops
# pub use core::iter;
# pub use core::option;
# }
```

Note that there is one extra lang item here which differs from the examples
Expand Down
3 changes: 2 additions & 1 deletion src/liballoc/lib.rs
Expand Up @@ -126,7 +126,8 @@ pub fn oom() -> ! {
#[doc(hidden)]
pub fn fixme_14344_be_sure_to_link_to_collections() {}

#[cfg(not(test))]
// NOTE: remove after next snapshot
#[cfg(all(stage0, not(test)))]
#[doc(hidden)]
mod std {
pub use core::fmt;
Expand Down
56 changes: 56 additions & 0 deletions src/libcollections/fmt.rs
@@ -0,0 +1,56 @@
// Copyright 2015 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 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

//! Formatting support for `String`.
//!
//! See `core::fmt` and `std::fmt` for full documentation on string
//! formatting.

#![stable(feature = "rust1", since = "1.0.0")]

use core::fmt;

use string;

/// 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.
///
/// # Example
///
/// ```rust
/// use std::fmt;
///
/// let s = fmt::format(format_args!("Hello, {}!", "world"));
/// assert_eq!(s, "Hello, world!".to_string());
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn format(args: fmt::Arguments) -> string::String {
// FIXME #21826
use core::fmt::Writer;
let mut output = string::String::new();
let _ = write!(&mut output, "{}", args);
output
}

#[cfg(test)]
mod tests {
use prelude::*;
use fmt;

#[test]
fn test_format() {
let s = fmt::format(format_args!("Hello, {}!", "world"));
assert_eq!(s.as_slice(), "Hello, world!");
}
}
18 changes: 10 additions & 8 deletions src/libcollections/lib.rs
Expand Up @@ -68,6 +68,7 @@ mod bit;
mod btree;
pub mod dlist;
pub mod enum_set;
pub mod fmt;
pub mod ring_buf;
pub mod slice;
pub mod str;
Expand Down Expand Up @@ -107,15 +108,16 @@ pub fn fixme_14344_be_sure_to_link_to_collections() {}

#[cfg(not(test))]
mod std {
pub use core::fmt; // necessary for panic!()
pub use core::option; // necessary for panic!()
pub use core::clone; // derive(Clone)
pub use core::cmp; // derive(Eq, Ord, etc.)
pub use core::marker; // derive(Copy)
pub use core::hash; // derive(Hash)
// NOTE: remove after next snapshot
#[cfg(stage0)] pub use core::clone; // derive(Clone)
#[cfg(stage0)] pub use core::cmp; // derive(Eq, Ord, etc.)
#[cfg(stage0)] pub use core::marker; // derive(Copy)
#[cfg(stage0)] pub use core::hash; // derive(Hash)
#[cfg(stage0)] pub use core::iter;
#[cfg(stage0)] pub use core::fmt; // necessary for panic!()
#[cfg(stage0)] pub use core::option; // necessary for panic!()

pub use core::ops; // RangeFull
// for-loops
pub use core::iter;
}

#[cfg(test)]
Expand Down
16 changes: 16 additions & 0 deletions src/libcollections/macros.rs
Expand Up @@ -22,3 +22,19 @@ macro_rules! vec {
);
($($x:expr,)*) => (vec![$($x),*])
}

/// Use the syntax described in `std::fmt` to create a value of type `String`.
/// See `std::fmt` for more information.
///
/// # Example
///
/// ```
/// format!("test");
/// format!("hello {}", "world!");
/// format!("x = {}, y = {y}", 10, y = 30);
/// ```
#[macro_export]
#[stable(feature = "rust1", since = "1.0.0")]
macro_rules! format {
($($arg:tt)*) => ($crate::fmt::format(format_args!($($arg)*)))
}
4 changes: 2 additions & 2 deletions src/libcollections/ring_buf.rs
Expand Up @@ -27,8 +27,8 @@ use core::ops::{Index, IndexMut};
use core::ptr;
use core::raw::Slice as RawSlice;

use std::hash::{Writer, Hash, Hasher};
use std::cmp;
use core::hash::{Writer, Hash, Hasher};
use core::cmp;

use alloc::heap;

Expand Down
2 changes: 1 addition & 1 deletion src/libcore/iter.rs
Expand Up @@ -67,7 +67,7 @@ use num::{ToPrimitive, Int};
use ops::{Add, Deref, FnMut};
use option::Option;
use option::Option::{Some, None};
use std::marker::Sized;
use marker::Sized;
use usize;

/// An interface for dealing with "external iterators". These types of iterators
Expand Down
24 changes: 16 additions & 8 deletions src/libcore/lib.rs
Expand Up @@ -148,17 +148,25 @@ mod array;
mod core {
pub use panicking;
pub use fmt;
#[cfg(not(stage0))] pub use clone;
#[cfg(not(stage0))] pub use cmp;
#[cfg(not(stage0))] pub use hash;
#[cfg(not(stage0))] pub use marker;
#[cfg(not(stage0))] pub use option;
#[cfg(not(stage0))] pub use iter;
}

#[doc(hidden)]
mod std {
pub use clone;
pub use cmp;
pub use fmt;
pub use hash;
pub use marker;
// NOTE: remove after next snapshot
#[cfg(stage0)] pub use clone;
#[cfg(stage0)] pub use cmp;
#[cfg(stage0)] pub use hash;
#[cfg(stage0)] pub use marker;
#[cfg(stage0)] pub use option;
#[cfg(stage0)] pub use fmt;
#[cfg(stage0)] pub use iter;

// range syntax
pub use ops;
pub use option;
// for-loops
pub use iter;
}
3 changes: 2 additions & 1 deletion src/liblibc/lib.rs
Expand Up @@ -5729,8 +5729,9 @@ pub fn issue_14344_workaround() {} // FIXME #14344 force linkage to happen corre

#[test] fn work_on_windows() { } // FIXME #10872 needed for a happy windows

// NOTE: remove after next snapshot
#[doc(hidden)]
#[cfg(not(test))]
#[cfg(all(stage0, not(test)))]
mod std {
pub use core::marker;
}
3 changes: 2 additions & 1 deletion src/librand/lib.rs
Expand Up @@ -496,7 +496,8 @@ pub struct Open01<F>(pub F);
/// ```
pub struct Closed01<F>(pub F);

#[cfg(not(test))]
// NOTE: remove after next snapshot
#[cfg(all(stage0, not(test)))]
mod std {
pub use core::{option, fmt}; // panic!()
pub use core::clone; // derive Clone
Expand Down
7 changes: 7 additions & 0 deletions src/librustc_bitflags/lib.rs
Expand Up @@ -281,6 +281,13 @@ macro_rules! bitflags {
};
}

// This is a no_std crate. So the test code's invocation of #[derive] etc, via
// bitflags!, will use names from the underlying crates.
#[cfg(test)]
mod core {
pub use std::{fmt, hash, clone, cmp, marker, option};
}

#[cfg(test)]
#[allow(non_upper_case_globals)]
mod tests {
Expand Down
24 changes: 1 addition & 23 deletions src/libstd/fmt.rs
Expand Up @@ -403,8 +403,6 @@

#![unstable(feature = "std_misc")]

use string;

pub use core::fmt::{Formatter, Result, Writer, rt};
pub use core::fmt::{Show, String, Octal, Binary};
pub use core::fmt::{Display, Debug};
Expand All @@ -413,24 +411,4 @@ pub use core::fmt::{LowerExp, UpperExp};
pub use core::fmt::Error;
pub use core::fmt::{ArgumentV1, Arguments, write, radix, Radix, RadixFmt};

/// 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.
///
/// # Example
///
/// ```rust
/// use std::fmt;
///
/// let s = fmt::format(format_args!("Hello, {}!", "world"));
/// assert_eq!(s, "Hello, world!".to_string());
/// ```
#[stable(feature = "rust1", since = "1.0.0")]
pub fn format(args: Arguments) -> string::String {
let mut output = string::String::new();
let _ = write!(&mut output, "{}", args);
output
}
pub use core_collections::fmt::format;
14 changes: 8 additions & 6 deletions src/libstd/lib.rs
Expand Up @@ -137,7 +137,7 @@
extern crate core;

#[macro_use]
#[macro_reexport(vec)]
#[macro_reexport(vec, format)]
extern crate "collections" as core_collections;

#[allow(deprecated)] extern crate "rand" as core_rand;
Expand Down Expand Up @@ -285,11 +285,12 @@ mod tuple;
// can be resolved within libstd.
#[doc(hidden)]
mod std {
// NOTE: remove after next snapshot
// mods used for deriving
pub use clone;
pub use cmp;
pub use hash;
pub use default;
#[cfg(stage0)] pub use clone;
#[cfg(stage0)] pub use cmp;
#[cfg(stage0)] pub use hash;
#[cfg(stage0)] pub use default;

pub use sync; // used for select!()
pub use error; // used for try!()
Expand All @@ -312,5 +313,6 @@ mod std {

pub use boxed; // used for vec![]
// for-loops
pub use iter;
// NOTE: remove after next snapshot
#[cfg(stage0)] pub use iter;
}
1 change: 1 addition & 0 deletions src/libstd/macros.rs
Expand Up @@ -70,6 +70,7 @@ macro_rules! panic {
/// format!("hello {}", "world!");
/// format!("x = {}, y = {y}", 10, y = 30);
/// ```
#[cfg(stage0)] // NOTE: remove after snapshot
#[macro_export]
#[stable(feature = "rust1", since = "1.0.0")]
macro_rules! format {
Expand Down
5 changes: 5 additions & 0 deletions src/libsyntax/ext/base.rs
Expand Up @@ -544,6 +544,7 @@ pub struct ExtCtxt<'a> {
pub cfg: ast::CrateConfig,
pub backtrace: ExpnId,
pub ecfg: expand::ExpansionConfig,
pub use_std: bool,

pub mod_path: Vec<ast::Ident> ,
pub trace_mac: bool,
Expand All @@ -563,6 +564,7 @@ impl<'a> ExtCtxt<'a> {
backtrace: NO_EXPANSION,
mod_path: Vec::new(),
ecfg: ecfg,
use_std: true,
trace_mac: false,
exported_macros: Vec::new(),
syntax_env: env,
Expand Down Expand Up @@ -737,6 +739,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.use_std { "std" } else { st })
}
pub fn name_of(&self, st: &str) -> ast::Name {
token::intern(st)
}
Expand Down

0 comments on commit 67350bc

Please sign in to comment.