Skip to content

Commit c124e32

Browse files
committed
pretty: remove ParseSess dependency
1 parent dc457c6 commit c124e32

File tree

4 files changed

+23
-35
lines changed

4 files changed

+23
-35
lines changed

src/librustc_driver/pretty.rs

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -392,14 +392,16 @@ pub fn print_after_parsing(
392392
call_with_pp_support(&s, sess, None, move |annotation| {
393393
debug!("pretty printing source code {:?}", s);
394394
let sess = annotation.sess();
395+
let parse = &sess.parse_sess;
395396
*out = pprust::print_crate(
396397
sess.source_map(),
397-
&sess.parse_sess,
398398
krate,
399399
src_name,
400400
src,
401401
annotation.pp_ann(),
402402
false,
403+
parse.edition,
404+
&parse.injected_crate_name,
403405
)
404406
})
405407
} else {
@@ -432,14 +434,16 @@ pub fn print_after_hir_lowering<'tcx>(
432434
call_with_pp_support(&s, tcx.sess, Some(tcx), move |annotation| {
433435
debug!("pretty printing source code {:?}", s);
434436
let sess = annotation.sess();
437+
let parse = &sess.parse_sess;
435438
*out = pprust::print_crate(
436439
sess.source_map(),
437-
&sess.parse_sess,
438440
krate,
439441
src_name,
440442
src,
441443
annotation.pp_ann(),
442444
true,
445+
parse.edition,
446+
&parse.injected_crate_name,
443447
)
444448
})
445449
}
@@ -449,14 +453,8 @@ pub fn print_after_hir_lowering<'tcx>(
449453
call_with_pp_support_hir(&s, tcx, move |annotation, krate| {
450454
debug!("pretty printing source code {:?}", s);
451455
let sess = annotation.sess();
452-
*out = pprust_hir::print_crate(
453-
sess.source_map(),
454-
&sess.parse_sess,
455-
krate,
456-
src_name,
457-
src,
458-
annotation.pp_ann(),
459-
)
456+
let cm = sess.source_map();
457+
*out = pprust_hir::print_crate(cm, krate, src_name, src, annotation.pp_ann())
460458
})
461459
}
462460

src/librustc_hir/print.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use syntax::ast;
66
use syntax::print::pp::Breaks::{Consistent, Inconsistent};
77
use syntax::print::pp::{self, Breaks};
88
use syntax::print::pprust::{self, Comments, PrintState};
9-
use syntax::sess::ParseSess;
109
use syntax::util::parser::{self, AssocOp, Fixity};
1110

1211
use crate::hir;
@@ -142,13 +141,12 @@ pub const INDENT_UNIT: usize = 4;
142141
/// it can scan the input text for comments to copy forward.
143142
pub fn print_crate<'a>(
144143
cm: &'a SourceMap,
145-
sess: &ParseSess,
146144
krate: &hir::Crate<'_>,
147145
filename: FileName,
148146
input: String,
149147
ann: &'a dyn PpAnn,
150148
) -> String {
151-
let mut s = State::new_from_input(cm, sess, filename, input, ann);
149+
let mut s = State::new_from_input(cm, filename, input, ann);
152150

153151
// When printing the AST, we sometimes need to inject `#[no_std]` here.
154152
// Since you can't compile the HIR, it's not necessary.
@@ -161,12 +159,11 @@ pub fn print_crate<'a>(
161159
impl<'a> State<'a> {
162160
pub fn new_from_input(
163161
cm: &'a SourceMap,
164-
sess: &ParseSess,
165162
filename: FileName,
166163
input: String,
167164
ann: &'a dyn PpAnn,
168165
) -> State<'a> {
169-
State { s: pp::mk_printer(), comments: Some(Comments::new(cm, sess, filename, input)), ann }
166+
State { s: pp::mk_printer(), comments: Some(Comments::new(cm, filename, input)), ann }
170167
}
171168
}
172169

src/libsyntax/print/pprust.rs

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,16 @@ use crate::attr;
55
use crate::print::pp::Breaks::{Consistent, Inconsistent};
66
use crate::print::pp::{self, Breaks};
77
use crate::ptr::P;
8-
use crate::sess::ParseSess;
98
use crate::token::{self, BinOpToken, DelimToken, Nonterminal, Token, TokenKind};
109
use crate::tokenstream::{self, TokenStream, TokenTree};
1110
use crate::util::classify;
1211
use crate::util::comments;
1312
use crate::util::parser::{self, AssocOp, Fixity};
1413

14+
use rustc_data_structures::sync::Once;
15+
use rustc_span::edition::Edition;
1516
use rustc_span::source_map::{dummy_spanned, SourceMap, Spanned};
16-
use rustc_span::symbol::{kw, sym};
17+
use rustc_span::symbol::{kw, sym, Symbol};
1718
use rustc_span::{BytePos, FileName, Span};
1819

1920
use std::borrow::Cow;
@@ -54,13 +55,8 @@ pub struct Comments<'a> {
5455
}
5556

5657
impl<'a> Comments<'a> {
57-
pub fn new(
58-
cm: &'a SourceMap,
59-
sess: &ParseSess,
60-
filename: FileName,
61-
input: String,
62-
) -> Comments<'a> {
63-
let comments = comments::gather_comments(sess, filename, input);
58+
pub fn new(cm: &'a SourceMap, filename: FileName, input: String) -> Comments<'a> {
59+
let comments = comments::gather_comments(cm, filename, input);
6460
Comments { cm, comments, current: 0 }
6561
}
6662

@@ -102,21 +98,22 @@ crate const INDENT_UNIT: usize = 4;
10298
/// it can scan the input text for comments to copy forward.
10399
pub fn print_crate<'a>(
104100
cm: &'a SourceMap,
105-
sess: &ParseSess,
106101
krate: &ast::Crate,
107102
filename: FileName,
108103
input: String,
109104
ann: &'a dyn PpAnn,
110105
is_expanded: bool,
106+
edition: Edition,
107+
injected_crate_name: &Once<Symbol>,
111108
) -> String {
112109
let mut s = State {
113110
s: pp::mk_printer(),
114-
comments: Some(Comments::new(cm, sess, filename, input)),
111+
comments: Some(Comments::new(cm, filename, input)),
115112
ann,
116113
is_expanded,
117114
};
118115

119-
if is_expanded && sess.injected_crate_name.try_get().is_some() {
116+
if is_expanded && injected_crate_name.try_get().is_some() {
120117
// We need to print `#![no_std]` (and its feature gate) so that
121118
// compiling pretty-printed source won't inject libstd again.
122119
// However, we don't want these attributes in the AST because
@@ -130,7 +127,7 @@ pub fn print_crate<'a>(
130127

131128
// Currently, in Rust 2018 we don't have `extern crate std;` at the crate
132129
// root, so this is not needed, and actually breaks things.
133-
if sess.edition == rustc_span::edition::Edition::Edition2015 {
130+
if edition == Edition::Edition2015 {
134131
// `#![no_std]`
135132
let no_std_meta = attr::mk_word_item(ast::Ident::with_dummy_span(sym::no_std));
136133
let fake_attr = attr::mk_attr_inner(no_std_meta);
@@ -144,10 +141,7 @@ pub fn print_crate<'a>(
144141
s.s.eof()
145142
}
146143

147-
pub fn to_string<F>(f: F) -> String
148-
where
149-
F: FnOnce(&mut State<'_>),
150-
{
144+
pub fn to_string(f: impl FnOnce(&mut State<'_>)) -> String {
151145
let mut printer =
152146
State { s: pp::mk_printer(), comments: None, ann: &NoAnn, is_expanded: false };
153147
f(&mut printer);

src/libsyntax/util/comments.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
pub use CommentStyle::*;
22

33
use crate::ast;
4-
use crate::sess::ParseSess;
54

65
use rustc_span::source_map::SourceMap;
76
use rustc_span::{BytePos, CharPos, FileName, Pos};
@@ -191,8 +190,8 @@ fn split_block_comment_into_lines(text: &str, col: CharPos) -> Vec<String> {
191190

192191
// it appears this function is called only from pprust... that's
193192
// probably not a good thing.
194-
crate fn gather_comments(sess: &ParseSess, path: FileName, src: String) -> Vec<Comment> {
195-
let cm = SourceMap::new(sess.source_map().path_mapping().clone());
193+
crate fn gather_comments(sm: &SourceMap, path: FileName, src: String) -> Vec<Comment> {
194+
let cm = SourceMap::new(sm.path_mapping().clone());
196195
let source_file = cm.new_source_file(path, src);
197196
let text = (*source_file.src.as_ref().unwrap()).clone();
198197

0 commit comments

Comments
 (0)