Skip to content

Commit 67350bc

Browse files
author
Keegan McAllister
committed
Don't use std:: paths in syntax extensions when compiling a #![no_std] crate
Fixes #16803. Fixes #14342. Fixes half of #21827 -- slice syntax is still broken.
1 parent 74eef05 commit 67350bc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

41 files changed

+413
-145
lines changed

src/doc/trpl/unsafe.md

-4
Original file line numberDiff line numberDiff line change
@@ -576,10 +576,6 @@ extern fn panic_fmt(args: &core::fmt::Arguments,
576576
#[lang = "eh_personality"] extern fn eh_personality() {}
577577
# #[start] fn start(argc: isize, argv: *const *const u8) -> isize { 0 }
578578
# fn main() {}
579-
# mod std { // for-loops
580-
# pub use core::iter;
581-
# pub use core::option;
582-
# }
583579
```
584580

585581
Note that there is one extra lang item here which differs from the examples

src/liballoc/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,8 @@ pub fn oom() -> ! {
126126
#[doc(hidden)]
127127
pub fn fixme_14344_be_sure_to_link_to_collections() {}
128128

129-
#[cfg(not(test))]
129+
// NOTE: remove after next snapshot
130+
#[cfg(all(stage0, not(test)))]
130131
#[doc(hidden)]
131132
mod std {
132133
pub use core::fmt;

src/libcollections/fmt.rs

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
//! Formatting support for `String`.
12+
//!
13+
//! See `core::fmt` and `std::fmt` for full documentation on string
14+
//! formatting.
15+
16+
#![stable(feature = "rust1", since = "1.0.0")]
17+
18+
use core::fmt;
19+
20+
use string;
21+
22+
/// The format function takes a precompiled format string and a list of
23+
/// arguments, to return the resulting formatted string.
24+
///
25+
/// # Arguments
26+
///
27+
/// * args - a structure of arguments generated via the `format_args!` macro.
28+
///
29+
/// # Example
30+
///
31+
/// ```rust
32+
/// use std::fmt;
33+
///
34+
/// let s = fmt::format(format_args!("Hello, {}!", "world"));
35+
/// assert_eq!(s, "Hello, world!".to_string());
36+
/// ```
37+
#[stable(feature = "rust1", since = "1.0.0")]
38+
pub fn format(args: fmt::Arguments) -> string::String {
39+
// FIXME #21826
40+
use core::fmt::Writer;
41+
let mut output = string::String::new();
42+
let _ = write!(&mut output, "{}", args);
43+
output
44+
}
45+
46+
#[cfg(test)]
47+
mod tests {
48+
use prelude::*;
49+
use fmt;
50+
51+
#[test]
52+
fn test_format() {
53+
let s = fmt::format(format_args!("Hello, {}!", "world"));
54+
assert_eq!(s.as_slice(), "Hello, world!");
55+
}
56+
}

src/libcollections/lib.rs

+10-8
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ mod bit;
6868
mod btree;
6969
pub mod dlist;
7070
pub mod enum_set;
71+
pub mod fmt;
7172
pub mod ring_buf;
7273
pub mod slice;
7374
pub mod str;
@@ -107,15 +108,16 @@ pub fn fixme_14344_be_sure_to_link_to_collections() {}
107108

108109
#[cfg(not(test))]
109110
mod std {
110-
pub use core::fmt; // necessary for panic!()
111-
pub use core::option; // necessary for panic!()
112-
pub use core::clone; // derive(Clone)
113-
pub use core::cmp; // derive(Eq, Ord, etc.)
114-
pub use core::marker; // derive(Copy)
115-
pub use core::hash; // derive(Hash)
111+
// NOTE: remove after next snapshot
112+
#[cfg(stage0)] pub use core::clone; // derive(Clone)
113+
#[cfg(stage0)] pub use core::cmp; // derive(Eq, Ord, etc.)
114+
#[cfg(stage0)] pub use core::marker; // derive(Copy)
115+
#[cfg(stage0)] pub use core::hash; // derive(Hash)
116+
#[cfg(stage0)] pub use core::iter;
117+
#[cfg(stage0)] pub use core::fmt; // necessary for panic!()
118+
#[cfg(stage0)] pub use core::option; // necessary for panic!()
119+
116120
pub use core::ops; // RangeFull
117-
// for-loops
118-
pub use core::iter;
119121
}
120122

121123
#[cfg(test)]

src/libcollections/macros.rs

+16
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,19 @@ macro_rules! vec {
2222
);
2323
($($x:expr,)*) => (vec![$($x),*])
2424
}
25+
26+
/// Use the syntax described in `std::fmt` to create a value of type `String`.
27+
/// See `std::fmt` for more information.
28+
///
29+
/// # Example
30+
///
31+
/// ```
32+
/// format!("test");
33+
/// format!("hello {}", "world!");
34+
/// format!("x = {}, y = {y}", 10, y = 30);
35+
/// ```
36+
#[macro_export]
37+
#[stable(feature = "rust1", since = "1.0.0")]
38+
macro_rules! format {
39+
($($arg:tt)*) => ($crate::fmt::format(format_args!($($arg)*)))
40+
}

src/libcollections/ring_buf.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ use core::ops::{Index, IndexMut};
2727
use core::ptr;
2828
use core::raw::Slice as RawSlice;
2929

30-
use std::hash::{Writer, Hash, Hasher};
31-
use std::cmp;
30+
use core::hash::{Writer, Hash, Hasher};
31+
use core::cmp;
3232

3333
use alloc::heap;
3434

src/libcore/iter.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ use num::{ToPrimitive, Int};
6767
use ops::{Add, Deref, FnMut};
6868
use option::Option;
6969
use option::Option::{Some, None};
70-
use std::marker::Sized;
70+
use marker::Sized;
7171
use usize;
7272

7373
/// An interface for dealing with "external iterators". These types of iterators

src/libcore/lib.rs

+16-8
Original file line numberDiff line numberDiff line change
@@ -148,17 +148,25 @@ mod array;
148148
mod core {
149149
pub use panicking;
150150
pub use fmt;
151+
#[cfg(not(stage0))] pub use clone;
152+
#[cfg(not(stage0))] pub use cmp;
153+
#[cfg(not(stage0))] pub use hash;
154+
#[cfg(not(stage0))] pub use marker;
155+
#[cfg(not(stage0))] pub use option;
156+
#[cfg(not(stage0))] pub use iter;
151157
}
152158

153159
#[doc(hidden)]
154160
mod std {
155-
pub use clone;
156-
pub use cmp;
157-
pub use fmt;
158-
pub use hash;
159-
pub use marker;
161+
// NOTE: remove after next snapshot
162+
#[cfg(stage0)] pub use clone;
163+
#[cfg(stage0)] pub use cmp;
164+
#[cfg(stage0)] pub use hash;
165+
#[cfg(stage0)] pub use marker;
166+
#[cfg(stage0)] pub use option;
167+
#[cfg(stage0)] pub use fmt;
168+
#[cfg(stage0)] pub use iter;
169+
170+
// range syntax
160171
pub use ops;
161-
pub use option;
162-
// for-loops
163-
pub use iter;
164172
}

src/liblibc/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -5729,8 +5729,9 @@ pub fn issue_14344_workaround() {} // FIXME #14344 force linkage to happen corre
57295729

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

5732+
// NOTE: remove after next snapshot
57325733
#[doc(hidden)]
5733-
#[cfg(not(test))]
5734+
#[cfg(all(stage0, not(test)))]
57345735
mod std {
57355736
pub use core::marker;
57365737
}

src/librand/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,8 @@ pub struct Open01<F>(pub F);
496496
/// ```
497497
pub struct Closed01<F>(pub F);
498498

499-
#[cfg(not(test))]
499+
// NOTE: remove after next snapshot
500+
#[cfg(all(stage0, not(test)))]
500501
mod std {
501502
pub use core::{option, fmt}; // panic!()
502503
pub use core::clone; // derive Clone

src/librustc_bitflags/lib.rs

+7
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,13 @@ macro_rules! bitflags {
281281
};
282282
}
283283

284+
// This is a no_std crate. So the test code's invocation of #[derive] etc, via
285+
// bitflags!, will use names from the underlying crates.
286+
#[cfg(test)]
287+
mod core {
288+
pub use std::{fmt, hash, clone, cmp, marker, option};
289+
}
290+
284291
#[cfg(test)]
285292
#[allow(non_upper_case_globals)]
286293
mod tests {

src/libstd/fmt.rs

+1-23
Original file line numberDiff line numberDiff line change
@@ -403,8 +403,6 @@
403403
404404
#![unstable(feature = "std_misc")]
405405

406-
use string;
407-
408406
pub use core::fmt::{Formatter, Result, Writer, rt};
409407
pub use core::fmt::{Show, String, Octal, Binary};
410408
pub use core::fmt::{Display, Debug};
@@ -413,24 +411,4 @@ pub use core::fmt::{LowerExp, UpperExp};
413411
pub use core::fmt::Error;
414412
pub use core::fmt::{ArgumentV1, Arguments, write, radix, Radix, RadixFmt};
415413

416-
/// The format function takes a precompiled format string and a list of
417-
/// arguments, to return the resulting formatted string.
418-
///
419-
/// # Arguments
420-
///
421-
/// * args - a structure of arguments generated via the `format_args!` macro.
422-
///
423-
/// # Example
424-
///
425-
/// ```rust
426-
/// use std::fmt;
427-
///
428-
/// let s = fmt::format(format_args!("Hello, {}!", "world"));
429-
/// assert_eq!(s, "Hello, world!".to_string());
430-
/// ```
431-
#[stable(feature = "rust1", since = "1.0.0")]
432-
pub fn format(args: Arguments) -> string::String {
433-
let mut output = string::String::new();
434-
let _ = write!(&mut output, "{}", args);
435-
output
436-
}
414+
pub use core_collections::fmt::format;

src/libstd/lib.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@
137137
extern crate core;
138138

139139
#[macro_use]
140-
#[macro_reexport(vec)]
140+
#[macro_reexport(vec, format)]
141141
extern crate "collections" as core_collections;
142142

143143
#[allow(deprecated)] extern crate "rand" as core_rand;
@@ -285,11 +285,12 @@ mod tuple;
285285
// can be resolved within libstd.
286286
#[doc(hidden)]
287287
mod std {
288+
// NOTE: remove after next snapshot
288289
// mods used for deriving
289-
pub use clone;
290-
pub use cmp;
291-
pub use hash;
292-
pub use default;
290+
#[cfg(stage0)] pub use clone;
291+
#[cfg(stage0)] pub use cmp;
292+
#[cfg(stage0)] pub use hash;
293+
#[cfg(stage0)] pub use default;
293294

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

313314
pub use boxed; // used for vec![]
314315
// for-loops
315-
pub use iter;
316+
// NOTE: remove after next snapshot
317+
#[cfg(stage0)] pub use iter;
316318
}

src/libstd/macros.rs

+1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ macro_rules! panic {
7070
/// format!("hello {}", "world!");
7171
/// format!("x = {}, y = {y}", 10, y = 30);
7272
/// ```
73+
#[cfg(stage0)] // NOTE: remove after snapshot
7374
#[macro_export]
7475
#[stable(feature = "rust1", since = "1.0.0")]
7576
macro_rules! format {

src/libsyntax/ext/base.rs

+5
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,7 @@ pub struct ExtCtxt<'a> {
544544
pub cfg: ast::CrateConfig,
545545
pub backtrace: ExpnId,
546546
pub ecfg: expand::ExpansionConfig,
547+
pub use_std: bool,
547548

548549
pub mod_path: Vec<ast::Ident> ,
549550
pub trace_mac: bool,
@@ -563,6 +564,7 @@ impl<'a> ExtCtxt<'a> {
563564
backtrace: NO_EXPANSION,
564565
mod_path: Vec::new(),
565566
ecfg: ecfg,
567+
use_std: true,
566568
trace_mac: false,
567569
exported_macros: Vec::new(),
568570
syntax_env: env,
@@ -737,6 +739,9 @@ impl<'a> ExtCtxt<'a> {
737739
pub fn ident_of(&self, st: &str) -> ast::Ident {
738740
str_to_ident(st)
739741
}
742+
pub fn ident_of_std(&self, st: &str) -> ast::Ident {
743+
self.ident_of(if self.use_std { "std" } else { st })
744+
}
740745
pub fn name_of(&self, st: &str) -> ast::Name {
741746
token::intern(st)
742747
}

0 commit comments

Comments
 (0)