Skip to content

Commit 78c99eb

Browse files
committed
Rename all ParseSess variables/fields/lifetimes as psess.
Existing names for values of this type are `sess`, `parse_sess`, `parse_session`, and `ps`. `sess` is particularly annoying because that's also used for `Session` values, which are often co-located, and it can be difficult to know which type a value named `sess` refers to. (That annoyance is the main motivation for this change.) `psess` is nice and short, which is good for a name used this much. The commit also renames some `parse_sess_created` values as `psess_created`.
1 parent 0b56261 commit 78c99eb

16 files changed

+126
-141
lines changed

src/comment.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1721,10 +1721,10 @@ pub(crate) fn recover_comment_removed(
17211721
// We missed some comments. Warn and keep the original text.
17221722
if context.config.error_on_unformatted() {
17231723
context.report.append(
1724-
context.parse_sess.span_to_filename(span),
1724+
context.psess.span_to_filename(span),
17251725
vec![FormattingError::from_span(
17261726
span,
1727-
context.parse_sess,
1727+
context.psess,
17281728
ErrorKind::LostComment,
17291729
)],
17301730
);

src/formatting.rs

+22-31
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ fn should_skip_module<T: FormatHandler>(
7979
// FIXME(calebcartwright) - we need to determine how we'll handle the
8080
// `format_generated_files` option with stdin based input.
8181
if !input_is_stdin && !config.format_generated_files() {
82-
let source_file = context.parse_session.span_to_file_contents(module.span);
82+
let source_file = context.psess.span_to_file_contents(module.span);
8383
let src = source_file.src.as_ref().expect("SourceFile without src");
8484

8585
if is_generated_file(src) {
@@ -109,16 +109,16 @@ fn format_project<T: FormatHandler>(
109109
let main_file = input.file_name();
110110
let input_is_stdin = main_file == FileName::Stdin;
111111

112-
let parse_session = ParseSess::new(config)?;
113-
if config.skip_children() && parse_session.ignore_file(&main_file) {
112+
let psess = ParseSess::new(config)?;
113+
if config.skip_children() && psess.ignore_file(&main_file) {
114114
return Ok(FormatReport::new());
115115
}
116116

117117
// Parse the crate.
118118
let mut report = FormatReport::new();
119119
let directory_ownership = input.to_directory_ownership();
120120

121-
let krate = match Parser::parse_crate(input, &parse_session) {
121+
let krate = match Parser::parse_crate(input, &psess) {
122122
Ok(krate) => krate,
123123
// Surface parse error via Session (errors are merged there from report)
124124
Err(e) => {
@@ -131,9 +131,9 @@ fn format_project<T: FormatHandler>(
131131
}
132132
};
133133

134-
let mut context = FormatContext::new(&krate, report, parse_session, config, handler);
134+
let mut context = FormatContext::new(&krate, report, psess, config, handler);
135135
let files = modules::ModResolver::new(
136-
&context.parse_session,
136+
&context.psess,
137137
directory_ownership.unwrap_or(DirectoryOwnership::UnownedViaBlock),
138138
!input_is_stdin && !config.skip_children(),
139139
)
@@ -148,16 +148,11 @@ fn format_project<T: FormatHandler>(
148148
timer = timer.done_parsing();
149149

150150
// Suppress error output if we have to do any further parsing.
151-
context.parse_session.set_silent_emitter();
151+
context.psess.set_silent_emitter();
152152

153153
for (path, module) in files {
154154
if input_is_stdin && contains_skip(module.attrs()) {
155-
return echo_back_stdin(
156-
context
157-
.parse_session
158-
.snippet_provider(module.span)
159-
.entire_snippet(),
160-
);
155+
return echo_back_stdin(context.psess.snippet_provider(module.span).entire_snippet());
161156
}
162157
should_emit_verbose(input_is_stdin, config, || println!("Formatting {}", path));
163158
context.format_file(path, &module, is_macro_def)?;
@@ -179,7 +174,7 @@ fn format_project<T: FormatHandler>(
179174
struct FormatContext<'a, T: FormatHandler> {
180175
krate: &'a ast::Crate,
181176
report: FormatReport,
182-
parse_session: ParseSess,
177+
psess: ParseSess,
183178
config: &'a Config,
184179
handler: &'a mut T,
185180
}
@@ -188,21 +183,21 @@ impl<'a, T: FormatHandler + 'a> FormatContext<'a, T> {
188183
fn new(
189184
krate: &'a ast::Crate,
190185
report: FormatReport,
191-
parse_session: ParseSess,
186+
psess: ParseSess,
192187
config: &'a Config,
193188
handler: &'a mut T,
194189
) -> Self {
195190
FormatContext {
196191
krate,
197192
report,
198-
parse_session,
193+
psess,
199194
config,
200195
handler,
201196
}
202197
}
203198

204199
fn ignore_file(&self, path: &FileName) -> bool {
205-
self.parse_session.ignore_file(path)
200+
self.psess.ignore_file(path)
206201
}
207202

208203
// Formats a single file/module.
@@ -212,9 +207,9 @@ impl<'a, T: FormatHandler + 'a> FormatContext<'a, T> {
212207
module: &Module<'_>,
213208
is_macro_def: bool,
214209
) -> Result<(), ErrorKind> {
215-
let snippet_provider = self.parse_session.snippet_provider(module.span);
216-
let mut visitor = FmtVisitor::from_parse_sess(
217-
&self.parse_session,
210+
let snippet_provider = self.psess.snippet_provider(module.span);
211+
let mut visitor = FmtVisitor::from_psess(
212+
&self.psess,
218213
self.config,
219214
&snippet_provider,
220215
self.report.clone(),
@@ -257,7 +252,7 @@ impl<'a, T: FormatHandler + 'a> FormatContext<'a, T> {
257252
.add_non_formatted_ranges(visitor.skipped_range.borrow().clone());
258253

259254
self.handler.handle_formatted_file(
260-
&self.parse_session,
255+
&self.psess,
261256
path,
262257
visitor.buffer.to_owned(),
263258
&mut self.report,
@@ -269,7 +264,7 @@ impl<'a, T: FormatHandler + 'a> FormatContext<'a, T> {
269264
trait FormatHandler {
270265
fn handle_formatted_file(
271266
&mut self,
272-
parse_session: &ParseSess,
267+
psess: &ParseSess,
273268
path: FileName,
274269
result: String,
275270
report: &mut FormatReport,
@@ -280,14 +275,14 @@ impl<'b, T: Write + 'b> FormatHandler for Session<'b, T> {
280275
// Called for each formatted file.
281276
fn handle_formatted_file(
282277
&mut self,
283-
parse_session: &ParseSess,
278+
psess: &ParseSess,
284279
path: FileName,
285280
result: String,
286281
report: &mut FormatReport,
287282
) -> Result<(), ErrorKind> {
288283
if let Some(ref mut out) = self.out {
289284
match source_file::write_file(
290-
Some(parse_session),
285+
Some(psess),
291286
&path,
292287
&result,
293288
out,
@@ -318,17 +313,13 @@ pub(crate) struct FormattingError {
318313
}
319314

320315
impl FormattingError {
321-
pub(crate) fn from_span(
322-
span: Span,
323-
parse_sess: &ParseSess,
324-
kind: ErrorKind,
325-
) -> FormattingError {
316+
pub(crate) fn from_span(span: Span, psess: &ParseSess, kind: ErrorKind) -> FormattingError {
326317
FormattingError {
327-
line: parse_sess.line_of_byte_pos(span.lo()),
318+
line: psess.line_of_byte_pos(span.lo()),
328319
is_comment: kind.is_comment(),
329320
kind,
330321
is_string: false,
331-
line_buffer: parse_sess.span_to_first_line_string(span),
322+
line_buffer: psess.span_to_first_line_string(span),
332323
}
333324
}
334325

src/macros.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,8 @@ fn return_macro_parse_failure_fallback(
136136
}
137137

138138
context.skipped_range.borrow_mut().push((
139-
context.parse_sess.line_of_byte_pos(span.lo()),
140-
context.parse_sess.line_of_byte_pos(span.hi()),
139+
context.psess.line_of_byte_pos(span.lo()),
140+
context.psess.line_of_byte_pos(span.hi()),
141141
));
142142

143143
// Return the snippet unmodified if the macro is not block-like

src/missed_spans.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ impl<'a> FmtVisitor<'a> {
9191
assert!(
9292
start < end,
9393
"Request to format inverted span: {}",
94-
self.parse_sess.span_to_debug_info(mk_sp(start, end)),
94+
self.psess.span_to_debug_info(mk_sp(start, end)),
9595
);
9696

9797
self.last_pos = end;
@@ -166,8 +166,8 @@ impl<'a> FmtVisitor<'a> {
166166
// Trim whitespace from the right hand side of each line.
167167
// Annoyingly, the library functions for splitting by lines etc. are not
168168
// quite right, so we must do it ourselves.
169-
let line = self.parse_sess.line_of_byte_pos(span.lo());
170-
let file_name = &self.parse_sess.span_to_filename(span);
169+
let line = self.psess.line_of_byte_pos(span.lo());
170+
let file_name = &self.psess.span_to_filename(span);
171171
let mut status = SnippetStatus::new(line);
172172

173173
let snippet = &*transform_missing_snippet(self.config, old_snippet);

src/modules.rs

+15-15
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ impl<'a> Module<'a> {
5757
}
5858

5959
/// Maps each module to the corresponding file.
60-
pub(crate) struct ModResolver<'ast, 'sess> {
61-
parse_sess: &'sess ParseSess,
60+
pub(crate) struct ModResolver<'ast, 'psess> {
61+
psess: &'psess ParseSess,
6262
directory: Directory,
6363
file_map: FileModMap<'ast>,
6464
recursive: bool,
@@ -99,10 +99,10 @@ enum SubModKind<'a, 'ast> {
9999
Internal(&'a ast::Item),
100100
}
101101

102-
impl<'ast, 'sess, 'c> ModResolver<'ast, 'sess> {
102+
impl<'ast, 'psess, 'c> ModResolver<'ast, 'psess> {
103103
/// Creates a new `ModResolver`.
104104
pub(crate) fn new(
105-
parse_sess: &'sess ParseSess,
105+
psess: &'psess ParseSess,
106106
directory_ownership: DirectoryOwnership,
107107
recursive: bool,
108108
) -> Self {
@@ -112,7 +112,7 @@ impl<'ast, 'sess, 'c> ModResolver<'ast, 'sess> {
112112
ownership: directory_ownership,
113113
},
114114
file_map: BTreeMap::new(),
115-
parse_sess,
115+
psess,
116116
recursive,
117117
}
118118
}
@@ -122,7 +122,7 @@ impl<'ast, 'sess, 'c> ModResolver<'ast, 'sess> {
122122
mut self,
123123
krate: &'ast ast::Crate,
124124
) -> Result<FileModMap<'ast>, ModuleResolutionError> {
125-
let root_filename = self.parse_sess.span_to_filename(krate.spans.inner_span);
125+
let root_filename = self.psess.span_to_filename(krate.spans.inner_span);
126126
self.directory.path = match root_filename {
127127
FileName::Real(ref p) => p.parent().unwrap_or(Path::new("")).to_path_buf(),
128128
_ => PathBuf::new(),
@@ -133,7 +133,7 @@ impl<'ast, 'sess, 'c> ModResolver<'ast, 'sess> {
133133
self.visit_mod_from_ast(&krate.items)?;
134134
}
135135

136-
let snippet_provider = self.parse_sess.snippet_provider(krate.spans.inner_span);
136+
let snippet_provider = self.psess.snippet_provider(krate.spans.inner_span);
137137

138138
self.file_map.insert(
139139
root_filename,
@@ -149,7 +149,7 @@ impl<'ast, 'sess, 'c> ModResolver<'ast, 'sess> {
149149

150150
/// Visit `cfg_if` macro and look for module declarations.
151151
fn visit_cfg_if(&mut self, item: Cow<'ast, ast::Item>) -> Result<(), ModuleResolutionError> {
152-
let mut visitor = visitor::CfgIfVisitor::new(self.parse_sess);
152+
let mut visitor = visitor::CfgIfVisitor::new(self.psess);
153153
visitor.visit_item(&item);
154154
for module_item in visitor.mods() {
155155
if let ast::ItemKind::Mod(_, ref sub_mod_kind) = module_item.item.kind {
@@ -338,10 +338,10 @@ impl<'ast, 'sess, 'c> ModResolver<'ast, 'sess> {
338338
DirectoryOwnership::UnownedViaBlock => None,
339339
};
340340
if let Some(path) = Parser::submod_path_from_attr(attrs, &self.directory.path) {
341-
if self.parse_sess.is_file_parsed(&path) {
341+
if self.psess.is_file_parsed(&path) {
342342
return Ok(None);
343343
}
344-
return match Parser::parse_file_as_module(self.parse_sess, &path, sub_mod.span) {
344+
return match Parser::parse_file_as_module(self.psess, &path, sub_mod.span) {
345345
Ok((ref attrs, _, _)) if contains_skip(attrs) => Ok(None),
346346
Ok((attrs, items, span)) => Ok(Some(SubModKind::External(
347347
path,
@@ -368,7 +368,7 @@ impl<'ast, 'sess, 'c> ModResolver<'ast, 'sess> {
368368
let mut mods_outside_ast = self.find_mods_outside_of_ast(attrs, sub_mod);
369369

370370
match self
371-
.parse_sess
371+
.psess
372372
.default_submod_path(mod_name, relative, &self.directory.path)
373373
{
374374
Ok(ModulePathSuccess {
@@ -380,7 +380,7 @@ impl<'ast, 'sess, 'c> ModResolver<'ast, 'sess> {
380380
let should_insert = !mods_outside_ast
381381
.iter()
382382
.any(|(outside_path, _, _)| outside_path == &file_path);
383-
if self.parse_sess.is_file_parsed(&file_path) {
383+
if self.psess.is_file_parsed(&file_path) {
384384
if outside_mods_empty {
385385
return Ok(None);
386386
} else {
@@ -390,7 +390,7 @@ impl<'ast, 'sess, 'c> ModResolver<'ast, 'sess> {
390390
return Ok(Some(SubModKind::MultiExternal(mods_outside_ast)));
391391
}
392392
}
393-
match Parser::parse_file_as_module(self.parse_sess, &file_path, sub_mod.span) {
393+
match Parser::parse_file_as_module(self.psess, &file_path, sub_mod.span) {
394394
Ok((ref attrs, _, _)) if contains_skip(attrs) => Ok(None),
395395
Ok((attrs, items, span)) if outside_mods_empty => {
396396
Ok(Some(SubModKind::External(
@@ -517,7 +517,7 @@ impl<'ast, 'sess, 'c> ModResolver<'ast, 'sess> {
517517
if !actual_path.exists() {
518518
continue;
519519
}
520-
if self.parse_sess.is_file_parsed(&actual_path) {
520+
if self.psess.is_file_parsed(&actual_path) {
521521
// If the specified file is already parsed, then we just use that.
522522
result.push((
523523
actual_path,
@@ -527,7 +527,7 @@ impl<'ast, 'sess, 'c> ModResolver<'ast, 'sess> {
527527
continue;
528528
}
529529
let (attrs, items, span) =
530-
match Parser::parse_file_as_module(self.parse_sess, &actual_path, sub_mod.span) {
530+
match Parser::parse_file_as_module(self.psess, &actual_path, sub_mod.span) {
531531
Ok((ref attrs, _, _)) if contains_skip(attrs) => continue,
532532
Ok(m) => m,
533533
Err(..) => continue,

src/modules/visitor.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@ pub(crate) struct ModItem {
1212

1313
/// Traverse `cfg_if!` macro and fetch modules.
1414
pub(crate) struct CfgIfVisitor<'a> {
15-
parse_sess: &'a ParseSess,
15+
psess: &'a ParseSess,
1616
mods: Vec<ModItem>,
1717
}
1818

1919
impl<'a> CfgIfVisitor<'a> {
20-
pub(crate) fn new(parse_sess: &'a ParseSess) -> CfgIfVisitor<'a> {
20+
pub(crate) fn new(psess: &'a ParseSess) -> CfgIfVisitor<'a> {
2121
CfgIfVisitor {
2222
mods: vec![],
23-
parse_sess,
23+
psess,
2424
}
2525
}
2626

@@ -62,7 +62,7 @@ impl<'a, 'ast: 'a> CfgIfVisitor<'a> {
6262
}
6363
};
6464

65-
let items = parse_cfg_if(self.parse_sess, mac)?;
65+
let items = parse_cfg_if(self.psess, mac)?;
6666
self.mods
6767
.append(&mut items.into_iter().map(|item| ModItem { item }).collect());
6868

src/parse/macros/cfg_if.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,22 @@ use crate::parse::macros::build_stream_parser;
99
use crate::parse::session::ParseSess;
1010

1111
pub(crate) fn parse_cfg_if<'a>(
12-
sess: &'a ParseSess,
12+
psess: &'a ParseSess,
1313
mac: &'a ast::MacCall,
1414
) -> Result<Vec<ast::Item>, &'static str> {
15-
match catch_unwind(AssertUnwindSafe(|| parse_cfg_if_inner(sess, mac))) {
15+
match catch_unwind(AssertUnwindSafe(|| parse_cfg_if_inner(psess, mac))) {
1616
Ok(Ok(items)) => Ok(items),
1717
Ok(err @ Err(_)) => err,
1818
Err(..) => Err("failed to parse cfg_if!"),
1919
}
2020
}
2121

2222
fn parse_cfg_if_inner<'a>(
23-
sess: &'a ParseSess,
23+
psess: &'a ParseSess,
2424
mac: &'a ast::MacCall,
2525
) -> Result<Vec<ast::Item>, &'static str> {
2626
let ts = mac.args.tokens.clone();
27-
let mut parser = build_stream_parser(sess.inner(), ts);
27+
let mut parser = build_stream_parser(psess.inner(), ts);
2828

2929
let mut items = vec![];
3030
let mut process_if_cfg = true;
@@ -67,7 +67,7 @@ fn parse_cfg_if_inner<'a>(
6767
Ok(None) => continue,
6868
Err(err) => {
6969
err.cancel();
70-
parser.sess.dcx.reset_err_count();
70+
parser.psess.dcx.reset_err_count();
7171
return Err(
7272
"Expected item inside cfg_if block, but failed to parse it as an item",
7373
);

0 commit comments

Comments
 (0)