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 symbol
::{Symbol, keywords}
;
14 use syntax_pos
::{DUMMY_SP, Span}
;
15 use codemap
::{self, ExpnInfo, NameAndSpan, MacroAttribute}
;
19 /// Craft a span that will be ignored by the stability lint's
20 /// call to codemap's is_internal check.
21 /// The expanded code uses the unstable `#[prelude_import]` attribute.
22 fn ignored_span(sess
: &ParseSess
, sp
: Span
) -> Span
{
26 format
: MacroAttribute(Symbol
::intern("std_inject")),
28 allow_internal_unstable
: true,
31 let expn_id
= sess
.codemap().record_expansion(info
);
37 pub fn injected_crate_name(krate
: &ast
::Crate
) -> Option
<&'
static str> {
38 if attr
::contains_name(&krate
.attrs
, "no_core") {
40 } else if attr
::contains_name(&krate
.attrs
, "no_std") {
47 pub fn maybe_inject_crates_ref(sess
: &ParseSess
,
48 mut krate
: ast
::Crate
,
49 alt_std_name
: Option
<String
>)
51 let name
= match injected_crate_name(&krate
) {
56 let crate_name
= Symbol
::intern(&alt_std_name
.unwrap_or(name
.to_string()));
58 krate
.module
.items
.insert(0, P(ast
::Item
{
59 attrs
: vec
![attr
::mk_attr_outer(attr
::mk_attr_id(),
60 attr
::mk_word_item(Symbol
::intern("macro_use")))],
61 vis
: ast
::Visibility
::Inherited
,
62 node
: ast
::ItemKind
::ExternCrate(Some(crate_name
)),
63 ident
: ast
::Ident
::from_str(name
),
64 id
: ast
::DUMMY_NODE_ID
,
68 let span
= ignored_span(sess
, DUMMY_SP
);
69 krate
.module
.items
.insert(0, P(ast
::Item
{
70 attrs
: vec
![ast
::Attribute
{
71 style
: ast
::AttrStyle
::Outer
,
72 value
: ast
::MetaItem
{
73 name
: Symbol
::intern("prelude_import"),
74 node
: ast
::MetaItemKind
::Word
,
77 id
: attr
::mk_attr_id(),
78 is_sugared_doc
: false,
81 vis
: ast
::Visibility
::Inherited
,
82 node
: ast
::ItemKind
::Use(P(codemap
::dummy_spanned(ast
::ViewPathGlob(ast
::Path
{
84 segments
: vec
![name
, "prelude", "v1"].into_iter().map(|name
| ast
::PathSegment
{
85 identifier
: ast
::Ident
::from_str(name
),
86 parameters
: ast
::PathParameters
::none(),
90 id
: ast
::DUMMY_NODE_ID
,
91 ident
: keywords
::Invalid
.ident(),