about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMazdak Farrokhzad <twingoow@gmail.com>2020-01-11 10:33:18 +0100
committerMazdak Farrokhzad <twingoow@gmail.com>2020-02-01 18:54:55 +0100
commit64d0143c2c5f627e246822b4e2f501e563ec63cc (patch)
tree480be10e878405f57051db39ecc25b3e1cd9e68f
parente03d1064f0d98961b83885ce951351ae57cc7aad (diff)
downloadrust-64d0143c2c5f627e246822b4e2f501e563ec63cc.tar.gz
rust-64d0143c2c5f627e246822b4e2f501e563ec63cc.zip
pretty: remove ParseSess dependency
-rw-r--r--src/librustc_driver/pretty.rs18
-rw-r--r--src/librustc_hir/print.rs7
-rw-r--r--src/libsyntax/print/pprust.rs28
-rw-r--r--src/libsyntax/util/comments.rs5
4 files changed, 23 insertions, 35 deletions
diff --git a/src/librustc_driver/pretty.rs b/src/librustc_driver/pretty.rs
index 6ef6dcf87ed..5cd9e9a4a58 100644
--- a/src/librustc_driver/pretty.rs
+++ b/src/librustc_driver/pretty.rs
@@ -392,14 +392,16 @@ pub fn print_after_parsing(
         call_with_pp_support(&s, sess, None, move |annotation| {
             debug!("pretty printing source code {:?}", s);
             let sess = annotation.sess();
+            let parse = &sess.parse_sess;
             *out = pprust::print_crate(
                 sess.source_map(),
-                &sess.parse_sess,
                 krate,
                 src_name,
                 src,
                 annotation.pp_ann(),
                 false,
+                parse.edition,
+                &parse.injected_crate_name,
             )
         })
     } else {
@@ -432,14 +434,16 @@ pub fn print_after_hir_lowering<'tcx>(
             call_with_pp_support(&s, tcx.sess, Some(tcx), move |annotation| {
                 debug!("pretty printing source code {:?}", s);
                 let sess = annotation.sess();
+                let parse = &sess.parse_sess;
                 *out = pprust::print_crate(
                     sess.source_map(),
-                    &sess.parse_sess,
                     krate,
                     src_name,
                     src,
                     annotation.pp_ann(),
                     true,
+                    parse.edition,
+                    &parse.injected_crate_name,
                 )
             })
         }
@@ -449,14 +453,8 @@ pub fn print_after_hir_lowering<'tcx>(
             call_with_pp_support_hir(&s, tcx, move |annotation, krate| {
                 debug!("pretty printing source code {:?}", s);
                 let sess = annotation.sess();
-                *out = pprust_hir::print_crate(
-                    sess.source_map(),
-                    &sess.parse_sess,
-                    krate,
-                    src_name,
-                    src,
-                    annotation.pp_ann(),
-                )
+                let cm = sess.source_map();
+                *out = pprust_hir::print_crate(cm, krate, src_name, src, annotation.pp_ann())
             })
         }
 
diff --git a/src/librustc_hir/print.rs b/src/librustc_hir/print.rs
index b9598c93761..7beabacecc2 100644
--- a/src/librustc_hir/print.rs
+++ b/src/librustc_hir/print.rs
@@ -6,7 +6,6 @@ use syntax::ast;
 use syntax::print::pp::Breaks::{Consistent, Inconsistent};
 use syntax::print::pp::{self, Breaks};
 use syntax::print::pprust::{self, Comments, PrintState};
-use syntax::sess::ParseSess;
 use syntax::util::parser::{self, AssocOp, Fixity};
 
 use crate::hir;
@@ -142,13 +141,12 @@ pub const INDENT_UNIT: usize = 4;
 /// it can scan the input text for comments to copy forward.
 pub fn print_crate<'a>(
     cm: &'a SourceMap,
-    sess: &ParseSess,
     krate: &hir::Crate<'_>,
     filename: FileName,
     input: String,
     ann: &'a dyn PpAnn,
 ) -> String {
-    let mut s = State::new_from_input(cm, sess, filename, input, ann);
+    let mut s = State::new_from_input(cm, filename, input, ann);
 
     // When printing the AST, we sometimes need to inject `#[no_std]` here.
     // Since you can't compile the HIR, it's not necessary.
@@ -161,12 +159,11 @@ pub fn print_crate<'a>(
 impl<'a> State<'a> {
     pub fn new_from_input(
         cm: &'a SourceMap,
-        sess: &ParseSess,
         filename: FileName,
         input: String,
         ann: &'a dyn PpAnn,
     ) -> State<'a> {
-        State { s: pp::mk_printer(), comments: Some(Comments::new(cm, sess, filename, input)), ann }
+        State { s: pp::mk_printer(), comments: Some(Comments::new(cm, filename, input)), ann }
     }
 }
 
diff --git a/src/libsyntax/print/pprust.rs b/src/libsyntax/print/pprust.rs
index d6f18fda8b2..624d1c70c15 100644
--- a/src/libsyntax/print/pprust.rs
+++ b/src/libsyntax/print/pprust.rs
@@ -5,15 +5,16 @@ use crate::attr;
 use crate::print::pp::Breaks::{Consistent, Inconsistent};
 use crate::print::pp::{self, Breaks};
 use crate::ptr::P;
-use crate::sess::ParseSess;
 use crate::token::{self, BinOpToken, DelimToken, Nonterminal, Token, TokenKind};
 use crate::tokenstream::{self, TokenStream, TokenTree};
 use crate::util::classify;
 use crate::util::comments;
 use crate::util::parser::{self, AssocOp, Fixity};
 
+use rustc_data_structures::sync::Once;
+use rustc_span::edition::Edition;
 use rustc_span::source_map::{dummy_spanned, SourceMap, Spanned};
-use rustc_span::symbol::{kw, sym};
+use rustc_span::symbol::{kw, sym, Symbol};
 use rustc_span::{BytePos, FileName, Span};
 
 use std::borrow::Cow;
@@ -54,13 +55,8 @@ pub struct Comments<'a> {
 }
 
 impl<'a> Comments<'a> {
-    pub fn new(
-        cm: &'a SourceMap,
-        sess: &ParseSess,
-        filename: FileName,
-        input: String,
-    ) -> Comments<'a> {
-        let comments = comments::gather_comments(sess, filename, input);
+    pub fn new(cm: &'a SourceMap, filename: FileName, input: String) -> Comments<'a> {
+        let comments = comments::gather_comments(cm, filename, input);
         Comments { cm, comments, current: 0 }
     }
 
@@ -102,21 +98,22 @@ crate const INDENT_UNIT: usize = 4;
 /// it can scan the input text for comments to copy forward.
 pub fn print_crate<'a>(
     cm: &'a SourceMap,
-    sess: &ParseSess,
     krate: &ast::Crate,
     filename: FileName,
     input: String,
     ann: &'a dyn PpAnn,
     is_expanded: bool,
+    edition: Edition,
+    injected_crate_name: &Once<Symbol>,
 ) -> String {
     let mut s = State {
         s: pp::mk_printer(),
-        comments: Some(Comments::new(cm, sess, filename, input)),
+        comments: Some(Comments::new(cm, filename, input)),
         ann,
         is_expanded,
     };
 
-    if is_expanded && sess.injected_crate_name.try_get().is_some() {
+    if is_expanded && injected_crate_name.try_get().is_some() {
         // 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
@@ -130,7 +127,7 @@ pub fn print_crate<'a>(
 
         // Currently, in Rust 2018 we don't have `extern crate std;` at the crate
         // root, so this is not needed, and actually breaks things.
-        if sess.edition == rustc_span::edition::Edition::Edition2015 {
+        if edition == Edition::Edition2015 {
             // `#![no_std]`
             let no_std_meta = attr::mk_word_item(ast::Ident::with_dummy_span(sym::no_std));
             let fake_attr = attr::mk_attr_inner(no_std_meta);
@@ -144,10 +141,7 @@ pub fn print_crate<'a>(
     s.s.eof()
 }
 
-pub fn to_string<F>(f: F) -> String
-where
-    F: FnOnce(&mut State<'_>),
-{
+pub fn to_string(f: impl FnOnce(&mut State<'_>)) -> String {
     let mut printer =
         State { s: pp::mk_printer(), comments: None, ann: &NoAnn, is_expanded: false };
     f(&mut printer);
diff --git a/src/libsyntax/util/comments.rs b/src/libsyntax/util/comments.rs
index c385b498ced..de33189884c 100644
--- a/src/libsyntax/util/comments.rs
+++ b/src/libsyntax/util/comments.rs
@@ -1,7 +1,6 @@
 pub use CommentStyle::*;
 
 use crate::ast;
-use crate::sess::ParseSess;
 
 use rustc_span::source_map::SourceMap;
 use rustc_span::{BytePos, CharPos, FileName, Pos};
@@ -191,8 +190,8 @@ fn split_block_comment_into_lines(text: &str, col: CharPos) -> Vec<String> {
 
 // it appears this function is called only from pprust... that's
 // probably not a good thing.
-crate fn gather_comments(sess: &ParseSess, path: FileName, src: String) -> Vec<Comment> {
-    let cm = SourceMap::new(sess.source_map().path_mapping().clone());
+crate fn gather_comments(sm: &SourceMap, path: FileName, src: String) -> Vec<Comment> {
+    let cm = SourceMap::new(sm.path_mapping().clone());
     let source_file = cm.new_source_file(path, src);
     let text = (*source_file.src.as_ref().unwrap()).clone();