Fake up #![no_std] on pretty-printing; keep it out of AST
This commit is contained in:
parent
d788588dce
commit
a246b6542a
2 changed files with 22 additions and 14 deletions
|
@ -15,17 +15,19 @@ use ast;
|
||||||
use ast::{MethodImplItem, RegionTyParamBound, TraitTyParamBound, TraitBoundModifier};
|
use ast::{MethodImplItem, RegionTyParamBound, TraitTyParamBound, TraitBoundModifier};
|
||||||
use ast::{RequiredMethod, ProvidedMethod, TypeImplItem, TypeTraitItem};
|
use ast::{RequiredMethod, ProvidedMethod, TypeImplItem, TypeTraitItem};
|
||||||
use ast_util;
|
use ast_util;
|
||||||
|
use attr;
|
||||||
use owned_slice::OwnedSlice;
|
use owned_slice::OwnedSlice;
|
||||||
use attr::{AttrMetaMethods, AttributeMethods};
|
use attr::{AttrMetaMethods, AttributeMethods};
|
||||||
use codemap::{self, CodeMap, BytePos};
|
use codemap::{self, CodeMap, BytePos};
|
||||||
use diagnostic;
|
use diagnostic;
|
||||||
use parse::token::{self, BinOpToken, Token};
|
use parse::token::{self, BinOpToken, Token, InternedString};
|
||||||
use parse::lexer::comments;
|
use parse::lexer::comments;
|
||||||
use parse;
|
use parse;
|
||||||
use print::pp::{self, break_offset, word, space, zerobreak, hardbreak};
|
use print::pp::{self, break_offset, word, space, zerobreak, hardbreak};
|
||||||
use print::pp::{Breaks, eof};
|
use print::pp::{Breaks, eof};
|
||||||
use print::pp::Breaks::{Consistent, Inconsistent};
|
use print::pp::Breaks::{Consistent, Inconsistent};
|
||||||
use ptr::P;
|
use ptr::P;
|
||||||
|
use std_inject;
|
||||||
|
|
||||||
use std::{ascii, mem};
|
use std::{ascii, mem};
|
||||||
use std::old_io::{self, IoResult};
|
use std::old_io::{self, IoResult};
|
||||||
|
@ -113,6 +115,25 @@ pub fn print_crate<'a>(cm: &'a CodeMap,
|
||||||
out,
|
out,
|
||||||
ann,
|
ann,
|
||||||
is_expanded);
|
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_mod(&krate.module, &krate.attrs[]));
|
||||||
try!(s.print_remaining_comments());
|
try!(s.print_remaining_comments());
|
||||||
eof(&mut s.s)
|
eof(&mut s.s)
|
||||||
|
|
|
@ -69,9 +69,6 @@ impl<'a> fold::Folder for StandardLibraryInjector<'a> {
|
||||||
span: DUMMY_SP
|
span: DUMMY_SP
|
||||||
}));
|
}));
|
||||||
|
|
||||||
// don't add #![no_std] here, that will block the prelude injection later.
|
|
||||||
// Add it during the prelude injection instead.
|
|
||||||
|
|
||||||
krate
|
krate
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -87,16 +84,6 @@ struct PreludeInjector<'a>;
|
||||||
|
|
||||||
impl<'a> fold::Folder for PreludeInjector<'a> {
|
impl<'a> fold::Folder for PreludeInjector<'a> {
|
||||||
fn fold_crate(&mut self, mut krate: ast::Crate) -> ast::Crate {
|
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
|
// only add `use std::prelude::*;` if there wasn't a
|
||||||
// `#![no_implicit_prelude]` at the crate level.
|
// `#![no_implicit_prelude]` at the crate level.
|
||||||
// fold_mod() will insert glob path.
|
// fold_mod() will insert glob path.
|
||||||
|
|
Loading…
Reference in a new issue