about summary refs log tree commit diff
path: root/src/libsyntax/print/pprust.rs
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2017-12-13 11:09:55 +0000
committerbors <bors@rust-lang.org>2017-12-13 11:09:55 +0000
commit3dfbc88a626625be01e112da11ec367e2fc71bb3 (patch)
tree3123349d2f450ac5317944d5f5803b20c3eca65b /src/libsyntax/print/pprust.rs
parent61100840e5c978a99b0489e8eaa922da06c05f65 (diff)
parent85d19b33357897c51d80727a4208f46b19c5c5a6 (diff)
downloadrust-3dfbc88a626625be01e112da11ec367e2fc71bb3.tar.gz
rust-3dfbc88a626625be01e112da11ec367e2fc71bb3.zip
Auto merge of #46550 - jseyfried:cleanup_builtin_hygiene, r=nrc
macros: hygienize use of `core`/`std` in builtin macros

Today, if a builtin macro wants to access an item from `core` or `std` (depending `#![no_std]`), it generates `::core::path::to::item` or `::std::path::to::item` respectively (c.f. `fn std_path()` in `libsyntax/ext/base.rs`).

This PR refactors the builtin macros to instead always emit `$crate::path::to::item` here. That is, the def site of builtin macros is taken to be in `extern crate core;` or `extern crate std;`. Since builtin macros are macros 1.0 (i.e. mostly unhygienic), changing the def site can only effect the resolution of `$crate`.

r? @nrc
Diffstat (limited to 'src/libsyntax/print/pprust.rs')
-rw-r--r--src/libsyntax/print/pprust.rs20
1 files changed, 19 insertions, 1 deletions
diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs
index a2d3ed4deb6..17f37d0f2c0 100644
--- a/src/libsyntax/print/pprust.rs
+++ b/src/libsyntax/print/pprust.rs
@@ -18,6 +18,7 @@ use util::parser::{self, AssocOp, Fixity};
 use attr;
 use codemap::{self, CodeMap};
 use syntax_pos::{self, BytePos};
+use syntax_pos::hygiene::{Mark, MarkKind, SyntaxContext};
 use parse::token::{self, BinOpToken, Token};
 use parse::lexer::comments;
 use parse::{self, ParseSess};
@@ -93,7 +94,7 @@ pub fn print_crate<'a>(cm: &'a CodeMap,
                        is_expanded: bool) -> io::Result<()> {
     let mut s = State::new_from_input(cm, sess, filename, input, out, ann, is_expanded);
 
-    if is_expanded && !std_inject::injected_crate_name(krate).is_none() {
+    if is_expanded && !std_inject::injected_crate_name().is_none() {
         // 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
@@ -734,6 +735,8 @@ pub trait PrintState<'a> {
                     if segment.identifier.name != keywords::CrateRoot.name() &&
                        segment.identifier.name != keywords::DollarCrate.name() {
                         self.writer().word(&segment.identifier.name.as_str())?;
+                    } else if segment.identifier.name == keywords::DollarCrate.name() {
+                        self.print_dollar_crate(segment.identifier.ctxt)?;
                     }
                 }
                 self.writer().space()?;
@@ -822,6 +825,19 @@ pub trait PrintState<'a> {
     }
 
     fn nbsp(&mut self) -> io::Result<()> { self.writer().word(" ") }
+
+    fn print_dollar_crate(&mut self, mut ctxt: SyntaxContext) -> io::Result<()> {
+        if let Some(mark) = ctxt.adjust(Mark::root()) {
+            // Make a best effort to print something that complies
+            if mark.kind() == MarkKind::Builtin {
+                if let Some(name) = std_inject::injected_crate_name() {
+                    self.writer().word("::")?;
+                    self.writer().word(name)?;
+                }
+            }
+        }
+        Ok(())
+    }
 }
 
 impl<'a> PrintState<'a> for State<'a> {
@@ -2411,6 +2427,8 @@ impl<'a> State<'a> {
             if let Some(ref parameters) = segment.parameters {
                 self.print_path_parameters(parameters, colons_before_params)?;
             }
+        } else if segment.identifier.name == keywords::DollarCrate.name() {
+            self.print_dollar_crate(segment.identifier.ctxt)?;
         }
         Ok(())
     }