1 // Copyright 2012 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
13 use ext
::hygiene
::{Mark, SyntaxContext}
;
14 use symbol
::{Symbol, keywords}
;
15 use syntax_pos
::{DUMMY_SP, Span}
;
16 use codemap
::{self, ExpnInfo, NameAndSpan, MacroAttribute}
;
18 use tokenstream
::TokenStream
;
20 /// Craft a span that will be ignored by the stability lint's
21 /// call to codemap's `is_internal` check.
22 /// The expanded code uses the unstable `#[prelude_import]` attribute.
23 fn ignored_span(sp
: Span
) -> Span
{
24 let mark
= Mark
::fresh(Mark
::root());
25 mark
.set_expn_info(ExpnInfo
{
28 format
: MacroAttribute(Symbol
::intern("std_inject")),
30 allow_internal_unstable
: true,
33 Span { ctxt: SyntaxContext::empty().apply_mark(mark), ..sp }
36 pub fn injected_crate_name(krate
: &ast
::Crate
) -> Option
<&'
static str> {
37 if attr
::contains_name(&krate
.attrs
, "no_core") {
39 } else if attr
::contains_name(&krate
.attrs
, "no_std") {
46 pub fn maybe_inject_crates_ref(mut krate
: ast
::Crate
, alt_std_name
: Option
<String
>) -> ast
::Crate
{
47 let name
= match injected_crate_name(&krate
) {
52 let crate_name
= Symbol
::intern(&alt_std_name
.unwrap_or_else(|| name
.to_string()));
54 krate
.module
.items
.insert(0, P(ast
::Item
{
55 attrs
: vec
![attr
::mk_attr_outer(DUMMY_SP
,
57 attr
::mk_word_item(Symbol
::intern("macro_use")))],
58 vis
: ast
::Visibility
::Inherited
,
59 node
: ast
::ItemKind
::ExternCrate(Some(crate_name
)),
60 ident
: ast
::Ident
::from_str(name
),
61 id
: ast
::DUMMY_NODE_ID
,
65 let span
= ignored_span(DUMMY_SP
);
66 krate
.module
.items
.insert(0, P(ast
::Item
{
67 attrs
: vec
![ast
::Attribute
{
68 style
: ast
::AttrStyle
::Outer
,
69 path
: ast
::Path
::from_ident(span
, ast
::Ident
::from_str("prelude_import")),
70 tokens
: TokenStream
::empty(),
71 id
: attr
::mk_attr_id(),
72 is_sugared_doc
: false,
75 vis
: ast
::Visibility
::Inherited
,
76 node
: ast
::ItemKind
::Use(P(codemap
::dummy_spanned(ast
::ViewPathGlob(ast
::Path
{
77 segments
: ["{{root}}", name
, "prelude", "v1"].into_iter().map(|name
| {
78 ast
::PathSegment
::from_ident(ast
::Ident
::from_str(name
), DUMMY_SP
)
82 id
: ast
::DUMMY_NODE_ID
,
83 ident
: keywords
::Invalid
.ident(),