Skip to content

Commit

Permalink
Fake up #![no_std] on pretty-printing; keep it out of AST
Browse files Browse the repository at this point in the history
  • Loading branch information
Keegan McAllister committed Feb 7, 2015
1 parent d788588 commit a246b65
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 14 deletions.
23 changes: 22 additions & 1 deletion src/libsyntax/print/pprust.rs
Expand Up @@ -15,17 +15,19 @@ use ast;
use ast::{MethodImplItem, RegionTyParamBound, TraitTyParamBound, TraitBoundModifier};
use ast::{RequiredMethod, ProvidedMethod, TypeImplItem, TypeTraitItem};
use ast_util;
use attr;
use owned_slice::OwnedSlice;
use attr::{AttrMetaMethods, AttributeMethods};
use codemap::{self, CodeMap, BytePos};
use diagnostic;
use parse::token::{self, BinOpToken, Token};
use parse::token::{self, BinOpToken, Token, InternedString};
use parse::lexer::comments;
use parse;
use print::pp::{self, break_offset, word, space, zerobreak, hardbreak};
use print::pp::{Breaks, eof};
use print::pp::Breaks::{Consistent, Inconsistent};
use ptr::P;
use std_inject;

use std::{ascii, mem};
use std::old_io::{self, IoResult};
Expand Down Expand Up @@ -113,6 +115,25 @@ pub fn print_crate<'a>(cm: &'a CodeMap,
out,
ann,
is_expanded);
if is_expanded && std_inject::use_std(krate) {
// We need to print `#![no_std]` (and its feature gate) so that
// compiling pretty-printed source won't inject libstd again.
// However we don't want these attributes in the AST because
// of the feature gate, so we fake them up here.

let no_std_meta = attr::mk_word_item(InternedString::new("no_std"));

// #![feature(no_std)]
let fake_attr = attr::mk_attr_inner(attr::mk_attr_id(),
attr::mk_list_item(InternedString::new("feature"),
vec![no_std_meta.clone()]));
try!(s.print_attribute(&fake_attr));

// #![no_std]
let fake_attr = attr::mk_attr_inner(attr::mk_attr_id(), no_std_meta);
try!(s.print_attribute(&fake_attr));
}

try!(s.print_mod(&krate.module, &krate.attrs[]));
try!(s.print_remaining_comments());
eof(&mut s.s)
Expand Down
13 changes: 0 additions & 13 deletions src/libsyntax/std_inject.rs
Expand Up @@ -69,9 +69,6 @@ impl<'a> fold::Folder for StandardLibraryInjector<'a> {
span: DUMMY_SP
}));

// don't add #![no_std] here, that will block the prelude injection later.
// Add it during the prelude injection instead.

krate
}
}
Expand All @@ -87,16 +84,6 @@ struct PreludeInjector<'a>;

impl<'a> fold::Folder for PreludeInjector<'a> {
fn fold_crate(&mut self, mut krate: ast::Crate) -> ast::Crate {
// Add #![no_std] here, so we don't re-inject when compiling pretty-printed source.
// This must happen here and not in StandardLibraryInjector because this
// fold happens second.

let no_std_attr = attr::mk_attr_inner(attr::mk_attr_id(),
attr::mk_word_item(InternedString::new("no_std")));
// std_inject runs after feature checking so manually mark this attr
attr::mark_used(&no_std_attr);
krate.attrs.push(no_std_attr);

// only add `use std::prelude::*;` if there wasn't a
// `#![no_implicit_prelude]` at the crate level.
// fold_mod() will insert glob path.
Expand Down

0 comments on commit a246b65

Please sign in to comment.