Skip to content

Commit ccf8fed

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 6cebe47 commit ccf8fed

39 files changed

+362
-100
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
@@ -124,7 +124,8 @@ pub fn oom() -> ! {
124124
#[doc(hidden)]
125125
pub fn fixme_14344_be_sure_to_link_to_collections() {}
126126

127-
#[cfg(not(test))]
127+
// NOTE: remove after next snapshot
128+
#[cfg(all(stage0, not(test)))]
128129
#[doc(hidden)]
129130
mod std {
130131
pub use core::fmt;

src/libcollections/lib.rs

+9-8
Original file line numberDiff line numberDiff line change
@@ -109,15 +109,16 @@ pub fn fixme_14344_be_sure_to_link_to_collections() {}
109109

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

123124
#[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::string::String::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/libcollections/string.rs

+29
Original file line numberDiff line numberDiff line change
@@ -671,6 +671,29 @@ impl String {
671671
pub fn clear(&mut self) {
672672
self.vec.clear()
673673
}
674+
675+
/// The format function takes a precompiled format string and a list of
676+
/// arguments, to return the resulting formatted string.
677+
///
678+
/// # Arguments
679+
///
680+
/// * args - a structure of arguments generated via the `format_args!` macro.
681+
///
682+
/// # Example
683+
///
684+
/// ```rust
685+
/// let s = String::format(format_args!("Hello, {}!", "world"));
686+
/// assert_eq!(s, "Hello, world!".to_string());
687+
/// ```
688+
#[unstable(feature = "collections",
689+
reason = "duplicates std::fmt::format, only needed when libstd is missing")]
690+
pub fn format(args: fmt::Arguments) -> String {
691+
// FIXME #21826
692+
use core::fmt::Writer;
693+
let mut output = String::new();
694+
let _ = write!(&mut output, "{}", args);
695+
output
696+
}
674697
}
675698

676699
impl FromUtf8Error {
@@ -1348,6 +1371,12 @@ mod tests {
13481371
assert_eq!(s, d);
13491372
}
13501373

1374+
#[test]
1375+
fn test_format() {
1376+
let s = format_args!(String::format, "Hello, {}!", "world");
1377+
assert_eq!(s.as_slice(), "Hello, world!");
1378+
}
1379+
13511380
#[bench]
13521381
fn bench_with_capacity(b: &mut Bencher) {
13531382
b.iter(|| {

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
@@ -149,17 +149,25 @@ mod array;
149149
mod core {
150150
pub use panicking;
151151
pub use fmt;
152+
#[cfg(not(stage0))] pub use clone;
153+
#[cfg(not(stage0))] pub use cmp;
154+
#[cfg(not(stage0))] pub use hash;
155+
#[cfg(not(stage0))] pub use marker;
156+
#[cfg(not(stage0))] pub use option;
157+
#[cfg(not(stage0))] pub use iter;
152158
}
153159

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

src/liblibc/lib.rs

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

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

5104+
// NOTE: remove after next snapshot
51045105
#[doc(hidden)]
5105-
#[cfg(not(test))]
5106+
#[cfg(all(stage0, not(test)))]
51065107
mod std {
51075108
pub use core::marker;
51085109
}

src/librand/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,8 @@ pub struct Open01<F>(pub F);
493493
/// ```
494494
pub struct Closed01<F>(pub F);
495495

496-
#[cfg(not(test))]
496+
// NOTE: remove after next snapshot
497+
#[cfg(all(stage0, not(test)))]
497498
mod std {
498499
pub use core::{option, fmt}; // panic!()
499500
pub use core::clone; // derive Clone

src/libstd/fmt.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,5 @@ pub use core::fmt::{argument, argumentuint};
434434
/// ```
435435
#[stable(feature = "rust1", since = "1.0.0")]
436436
pub fn format(args: Arguments) -> string::String {
437-
let mut output = string::String::new();
438-
let _ = write!(&mut output, "{}", args);
439-
output
437+
string::String::format(args)
440438
}

src/libstd/lib.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ extern crate log;
139139
extern crate core;
140140

141141
#[macro_use]
142-
#[macro_reexport(vec)]
142+
#[macro_reexport(vec, format)]
143143
extern crate "collections" as core_collections;
144144

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

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

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

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
}

src/libsyntax/ext/build.rs

+28-17
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
387387
self.path_all(DUMMY_SP,
388388
true,
389389
vec!(
390-
self.ident_of("std"),
390+
self.ident_of_std("core"),
391391
self.ident_of("option"),
392392
self.ident_of("Option")
393393
),
@@ -657,7 +657,7 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
657657
}
658658
fn expr_vec_ng(&self, sp: Span) -> P<ast::Expr> {
659659
self.expr_call_global(sp,
660-
vec!(self.ident_of("std"),
660+
vec!(self.ident_of_std("collections"),
661661
self.ident_of("vec"),
662662
self.ident_of("Vec"),
663663
self.ident_of("new")),
@@ -677,7 +677,7 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
677677

678678
fn expr_some(&self, sp: Span, expr: P<ast::Expr>) -> P<ast::Expr> {
679679
let some = vec!(
680-
self.ident_of("std"),
680+
self.ident_of_std("core"),
681681
self.ident_of("option"),
682682
self.ident_of("Option"),
683683
self.ident_of("Some"));
@@ -686,7 +686,7 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
686686

687687
fn expr_none(&self, sp: Span) -> P<ast::Expr> {
688688
let none = self.path_global(sp, vec!(
689-
self.ident_of("std"),
689+
self.ident_of_std("core"),
690690
self.ident_of("option"),
691691
self.ident_of("Option"),
692692
self.ident_of("None")));
@@ -713,7 +713,7 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
713713
self.expr_call_global(
714714
span,
715715
vec!(
716-
self.ident_of("std"),
716+
self.ident_of_std("core"),
717717
self.ident_of("rt"),
718718
self.ident_of("begin_unwind")),
719719
vec!(
@@ -729,7 +729,7 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
729729

730730
fn expr_ok(&self, sp: Span, expr: P<ast::Expr>) -> P<ast::Expr> {
731731
let ok = vec!(
732-
self.ident_of("std"),
732+
self.ident_of_std("core"),
733733
self.ident_of("result"),
734734
self.ident_of("Result"),
735735
self.ident_of("Ok"));
@@ -738,18 +738,28 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
738738

739739
fn expr_err(&self, sp: Span, expr: P<ast::Expr>) -> P<ast::Expr> {
740740
let err = vec!(
741-
self.ident_of("std"),
741+
self.ident_of_std("core"),
742742
self.ident_of("result"),
743743
self.ident_of("Result"),
744744
self.ident_of("Err"));
745745
self.expr_call_global(sp, err, vec!(expr))
746746
}
747747

748748
fn expr_try(&self, sp: Span, head: P<ast::Expr>) -> P<ast::Expr> {
749-
let ok = self.ident_of("Ok");
750-
let ok_path = self.path_ident(sp, ok);
751-
let err = self.ident_of("Err");
752-
let err_path = self.path_ident(sp, err);
749+
let ok = vec![
750+
self.ident_of_std("core"),
751+
self.ident_of("result"),
752+
self.ident_of("Result"),
753+
self.ident_of("Ok")
754+
];
755+
let ok_path = self.path_global(sp, ok);
756+
let err = vec![
757+
self.ident_of_std("core"),
758+
self.ident_of("result"),
759+
self.ident_of("Result"),
760+
self.ident_of("Err")
761+
];
762+
let err_path = self.path_global(sp, err);
753763

754764
let binding_variable = self.ident_of("__try_var");
755765
let binding_pat = self.pat_ident(sp, binding_variable);
@@ -759,8 +769,9 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
759769
let ok_pat = self.pat_enum(sp, ok_path, vec!(binding_pat.clone()));
760770

761771
// Err(__try_var) (pattern and expression resp.)
762-
let err_pat = self.pat_enum(sp, err_path, vec!(binding_pat));
763-
let err_inner_expr = self.expr_call_ident(sp, err, vec!(binding_expr.clone()));
772+
let err_pat = self.pat_enum(sp, err_path.clone(), vec!(binding_pat));
773+
let err_inner_expr = self.expr_call(sp, self.expr_path(err_path),
774+
vec!(binding_expr.clone()));
764775
// return Err(__try_var)
765776
let err_expr = self.expr(sp, ast::ExprRet(Some(err_inner_expr)));
766777

@@ -809,7 +820,7 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
809820

810821
fn pat_some(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat> {
811822
let some = vec!(
812-
self.ident_of("std"),
823+
self.ident_of_std("core"),
813824
self.ident_of("option"),
814825
self.ident_of("Option"),
815826
self.ident_of("Some"));
@@ -819,7 +830,7 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
819830

820831
fn pat_none(&self, span: Span) -> P<ast::Pat> {
821832
let some = vec!(
822-
self.ident_of("std"),
833+
self.ident_of_std("core"),
823834
self.ident_of("option"),
824835
self.ident_of("Option"),
825836
self.ident_of("None"));
@@ -829,7 +840,7 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
829840

830841
fn pat_ok(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat> {
831842
let some = vec!(
832-
self.ident_of("std"),
843+
self.ident_of_std("core"),
833844
self.ident_of("result"),
834845
self.ident_of("Result"),
835846
self.ident_of("Ok"));
@@ -839,7 +850,7 @@ impl<'a> AstBuilder for ExtCtxt<'a> {
839850

840851
fn pat_err(&self, span: Span, pat: P<ast::Pat>) -> P<ast::Pat> {
841852
let some = vec!(
842-
self.ident_of("std"),
853+
self.ident_of_std("core"),
843854
self.ident_of("result"),
844855
self.ident_of("Result"),
845856
self.ident_of("Err"));

0 commit comments

Comments
 (0)