Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix syntax extensions for #![no_std] #21912

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions src/doc/trpl/unsafe.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,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 @@ -108,15 +109,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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
Expand Up @@ -5734,8 +5734,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
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Loading