pretty: remove ParseSess dependency

This commit is contained in:
Mazdak Farrokhzad 2020-01-11 10:33:18 +01:00
parent e03d1064f0
commit 64d0143c2c
4 changed files with 23 additions and 35 deletions

View File

@ -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())
})
}

View File

@ -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 }
}
}

View File

@ -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);

View File

@ -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();