Skip to content

Commit a246b65

Browse files
author
Keegan McAllister
committed
Fake up #![no_std] on pretty-printing; keep it out of AST
1 parent d788588 commit a246b65

File tree

2 files changed

+22
-14
lines changed

2 files changed

+22
-14
lines changed

src/libsyntax/print/pprust.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,19 @@ use ast;
1515
use ast::{MethodImplItem, RegionTyParamBound, TraitTyParamBound, TraitBoundModifier};
1616
use ast::{RequiredMethod, ProvidedMethod, TypeImplItem, TypeTraitItem};
1717
use ast_util;
18+
use attr;
1819
use owned_slice::OwnedSlice;
1920
use attr::{AttrMetaMethods, AttributeMethods};
2021
use codemap::{self, CodeMap, BytePos};
2122
use diagnostic;
22-
use parse::token::{self, BinOpToken, Token};
23+
use parse::token::{self, BinOpToken, Token, InternedString};
2324
use parse::lexer::comments;
2425
use parse;
2526
use print::pp::{self, break_offset, word, space, zerobreak, hardbreak};
2627
use print::pp::{Breaks, eof};
2728
use print::pp::Breaks::{Consistent, Inconsistent};
2829
use ptr::P;
30+
use std_inject;
2931

3032
use std::{ascii, mem};
3133
use std::old_io::{self, IoResult};
@@ -113,6 +115,25 @@ pub fn print_crate<'a>(cm: &'a CodeMap,
113115
out,
114116
ann,
115117
is_expanded);
118+
if is_expanded && std_inject::use_std(krate) {
119+
// We need to print `#![no_std]` (and its feature gate) so that
120+
// compiling pretty-printed source won't inject libstd again.
121+
// However we don't want these attributes in the AST because
122+
// of the feature gate, so we fake them up here.
123+
124+
let no_std_meta = attr::mk_word_item(InternedString::new("no_std"));
125+
126+
// #![feature(no_std)]
127+
let fake_attr = attr::mk_attr_inner(attr::mk_attr_id(),
128+
attr::mk_list_item(InternedString::new("feature"),
129+
vec![no_std_meta.clone()]));
130+
try!(s.print_attribute(&fake_attr));
131+
132+
// #![no_std]
133+
let fake_attr = attr::mk_attr_inner(attr::mk_attr_id(), no_std_meta);
134+
try!(s.print_attribute(&fake_attr));
135+
}
136+
116137
try!(s.print_mod(&krate.module, &krate.attrs[]));
117138
try!(s.print_remaining_comments());
118139
eof(&mut s.s)

src/libsyntax/std_inject.rs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,6 @@ impl<'a> fold::Folder for StandardLibraryInjector<'a> {
6969
span: DUMMY_SP
7070
}));
7171

72-
// don't add #![no_std] here, that will block the prelude injection later.
73-
// Add it during the prelude injection instead.
74-
7572
krate
7673
}
7774
}
@@ -87,16 +84,6 @@ struct PreludeInjector<'a>;
8784

8885
impl<'a> fold::Folder for PreludeInjector<'a> {
8986
fn fold_crate(&mut self, mut krate: ast::Crate) -> ast::Crate {
90-
// Add #![no_std] here, so we don't re-inject when compiling pretty-printed source.
91-
// This must happen here and not in StandardLibraryInjector because this
92-
// fold happens second.
93-
94-
let no_std_attr = attr::mk_attr_inner(attr::mk_attr_id(),
95-
attr::mk_word_item(InternedString::new("no_std")));
96-
// std_inject runs after feature checking so manually mark this attr
97-
attr::mark_used(&no_std_attr);
98-
krate.attrs.push(no_std_attr);
99-
10087
// only add `use std::prelude::*;` if there wasn't a
10188
// `#![no_implicit_prelude]` at the crate level.
10289
// fold_mod() will insert glob path.

0 commit comments

Comments
 (0)