Fake up #![no_std] on pretty-printing; keep it out of AST

This commit is contained in:
Keegan McAllister 2015-02-05 17:04:11 -08:00
parent d788588dce
commit a246b6542a
2 changed files with 22 additions and 14 deletions

View file

@ -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};
@ -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)

View file

@ -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
}
}
@ -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.