From 64d0143c2c5f627e246822b4e2f501e563ec63cc Mon Sep 17 00:00:00 2001 From: Mazdak Farrokhzad Date: Sat, 11 Jan 2020 10:33:18 +0100 Subject: [PATCH] pretty: remove ParseSess dependency --- src/librustc_driver/pretty.rs | 18 ++++++++---------- src/librustc_hir/print.rs | 7 ++----- src/libsyntax/print/pprust.rs | 28 +++++++++++----------------- src/libsyntax/util/comments.rs | 5 ++--- 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, ) -> 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) -> 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 { // 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 { - let cm = SourceMap::new(sess.source_map().path_mapping().clone()); +crate fn gather_comments(sm: &SourceMap, path: FileName, src: String) -> Vec { + 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();