Skip to content

Commit d213e2f

Browse files
authored
Rollup merge of rust-lang#62979 - Mark-Simulacrum:json-dumper-pretty, r=Xanewok
Cleanup save-analysis JsonDumper
2 parents 8654523 + 68c0ba2 commit d213e2f

File tree

3 files changed

+35
-78
lines changed

3 files changed

+35
-78
lines changed

src/librustc_save_analysis/dump_visitor.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
//!
1111
//! SpanUtils is used to manipulate spans. In particular, to extract sub-spans
1212
//! from spans (e.g., the span for `bar` from the above example path).
13-
//! DumpVisitor walks the AST and processes it, and JsonDumper is used for
13+
//! DumpVisitor walks the AST and processes it, and Dumper is used for
1414
//! recording the output.
1515
1616
use rustc::hir::def::{Res, DefKind as HirDefKind};
@@ -38,7 +38,7 @@ use syntax_pos::*;
3838

3939
use crate::{escape, generated_code, id_from_def_id, id_from_node_id, lower_attributes,
4040
PathCollector, SaveContext};
41-
use crate::json_dumper::{Access, DumpOutput, JsonDumper};
41+
use crate::dumper::{Access, Dumper};
4242
use crate::span_utils::SpanUtils;
4343
use crate::sig;
4444

@@ -75,10 +75,10 @@ macro_rules! access_from_vis {
7575
};
7676
}
7777

78-
pub struct DumpVisitor<'l, 'tcx, 'll, O: DumpOutput> {
78+
pub struct DumpVisitor<'l, 'tcx, 'll> {
7979
save_ctxt: SaveContext<'l, 'tcx>,
8080
tcx: TyCtxt<'tcx>,
81-
dumper: &'ll mut JsonDumper<O>,
81+
dumper: &'ll mut Dumper,
8282

8383
span: SpanUtils<'l>,
8484

@@ -92,11 +92,11 @@ pub struct DumpVisitor<'l, 'tcx, 'll, O: DumpOutput> {
9292
// macro_calls: FxHashSet<Span>,
9393
}
9494

95-
impl<'l, 'tcx, 'll, O: DumpOutput + 'll> DumpVisitor<'l, 'tcx, 'll, O> {
95+
impl<'l, 'tcx, 'll> DumpVisitor<'l, 'tcx, 'll> {
9696
pub fn new(
9797
save_ctxt: SaveContext<'l, 'tcx>,
98-
dumper: &'ll mut JsonDumper<O>,
99-
) -> DumpVisitor<'l, 'tcx, 'll, O> {
98+
dumper: &'ll mut Dumper,
99+
) -> DumpVisitor<'l, 'tcx, 'll> {
100100
let span_utils = SpanUtils::new(&save_ctxt.tcx.sess);
101101
DumpVisitor {
102102
tcx: save_ctxt.tcx,
@@ -111,7 +111,7 @@ impl<'l, 'tcx, 'll, O: DumpOutput + 'll> DumpVisitor<'l, 'tcx, 'll, O> {
111111

112112
fn nest_scope<F>(&mut self, scope_id: NodeId, f: F)
113113
where
114-
F: FnOnce(&mut DumpVisitor<'l, 'tcx, 'll, O>),
114+
F: FnOnce(&mut DumpVisitor<'l, 'tcx, 'll>),
115115
{
116116
let parent_scope = self.cur_scope;
117117
self.cur_scope = scope_id;
@@ -121,7 +121,7 @@ impl<'l, 'tcx, 'll, O: DumpOutput + 'll> DumpVisitor<'l, 'tcx, 'll, O> {
121121

122122
fn nest_tables<F>(&mut self, item_id: NodeId, f: F)
123123
where
124-
F: FnOnce(&mut DumpVisitor<'l, 'tcx, 'll, O>),
124+
F: FnOnce(&mut DumpVisitor<'l, 'tcx, 'll>),
125125
{
126126
let item_def_id = self.tcx.hir().local_def_id_from_node_id(item_id);
127127
if self.tcx.has_typeck_tables(item_def_id) {
@@ -1311,7 +1311,7 @@ impl<'l, 'tcx, 'll, O: DumpOutput + 'll> DumpVisitor<'l, 'tcx, 'll, O> {
13111311
}
13121312
}
13131313

1314-
impl<'l, 'tcx, 'll, O: DumpOutput + 'll> Visitor<'l> for DumpVisitor<'l, 'tcx, 'll, O> {
1314+
impl<'l, 'tcx, 'll> Visitor<'l> for DumpVisitor<'l, 'tcx, 'll> {
13151315
fn visit_mod(&mut self, m: &'l ast::Mod, span: Span, attrs: &[ast::Attribute], id: NodeId) {
13161316
// Since we handle explicit modules ourselves in visit_item, this should
13171317
// only get called for the root module of a crate.

src/librustc_save_analysis/json_dumper.rs renamed to src/librustc_save_analysis/dumper.rs

+7-54
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,33 @@
1-
use std::io::Write;
2-
31
use rls_data::config::Config;
42
use rls_data::{self, Analysis, CompilationOptions, CratePreludeData, Def, DefKind, Impl, Import,
53
MacroRef, Ref, RefKind, Relation};
64
use rls_span::{Column, Row};
75

8-
use log::error;
9-
106
#[derive(Debug)]
117
pub struct Access {
128
pub reachable: bool,
139
pub public: bool,
1410
}
1511

16-
pub struct JsonDumper<O: DumpOutput> {
12+
pub struct Dumper {
1713
result: Analysis,
1814
config: Config,
19-
output: O,
20-
}
21-
22-
pub trait DumpOutput {
23-
fn dump(&mut self, result: &Analysis);
24-
}
25-
26-
pub struct WriteOutput<'b, W: Write> {
27-
output: &'b mut W,
28-
}
29-
30-
impl<'b, W: Write> DumpOutput for WriteOutput<'b, W> {
31-
fn dump(&mut self, result: &Analysis) {
32-
if let Err(e) = serde_json::to_writer(self.output.by_ref(), result) {
33-
error!("Can't serialize save-analysis: {:?}", e);
34-
}
35-
}
36-
}
37-
38-
pub struct CallbackOutput<'b> {
39-
callback: &'b mut dyn FnMut(&Analysis),
40-
}
41-
42-
impl<'b> DumpOutput for CallbackOutput<'b> {
43-
fn dump(&mut self, result: &Analysis) {
44-
(self.callback)(result)
45-
}
4615
}
4716

48-
impl<'b, W: Write> JsonDumper<WriteOutput<'b, W>> {
49-
pub fn new(writer: &'b mut W, config: Config) -> JsonDumper<WriteOutput<'b, W>> {
50-
JsonDumper {
51-
output: WriteOutput { output: writer },
17+
impl Dumper {
18+
pub fn new(config: Config) -> Dumper {
19+
Dumper {
5220
config: config.clone(),
5321
result: Analysis::new(config),
5422
}
5523
}
56-
}
57-
58-
impl<'b> JsonDumper<CallbackOutput<'b>> {
59-
pub fn with_callback(
60-
callback: &'b mut dyn FnMut(&Analysis),
61-
config: Config,
62-
) -> JsonDumper<CallbackOutput<'b>> {
63-
JsonDumper {
64-
output: CallbackOutput { callback },
65-
config: config.clone(),
66-
result: Analysis::new(config),
67-
}
68-
}
69-
}
7024

71-
impl<O: DumpOutput> Drop for JsonDumper<O> {
72-
fn drop(&mut self) {
73-
self.output.dump(&self.result);
25+
pub fn to_output(self, f: impl FnOnce(&Analysis)) {
26+
f(&self.result)
7427
}
7528
}
7629

77-
impl<'b, O: DumpOutput + 'b> JsonDumper<O> {
30+
impl Dumper {
7831
pub fn crate_prelude(&mut self, data: CratePreludeData) {
7932
self.result.prelude = Some(data)
8033
}

src/librustc_save_analysis/lib.rs

+18-14
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
#![recursion_limit="256"]
88

99

10-
mod json_dumper;
10+
mod dumper;
1111
mod dump_visitor;
1212
#[macro_use]
1313
mod span_utils;
@@ -39,7 +39,7 @@ use syntax::visit::{self, Visitor};
3939
use syntax::print::pprust::{arg_to_string, ty_to_string};
4040
use syntax_pos::*;
4141

42-
use json_dumper::JsonDumper;
42+
use dumper::Dumper;
4343
use dump_visitor::DumpVisitor;
4444
use span_utils::SpanUtils;
4545

@@ -1075,17 +1075,19 @@ impl<'a> SaveHandler for DumpHandler<'a> {
10751075
input: &'l Input,
10761076
) {
10771077
let sess = &save_ctxt.tcx.sess;
1078-
let file_name = {
1079-
let (mut output, file_name) = self.output_file(&save_ctxt);
1080-
let mut dumper = JsonDumper::new(&mut output, save_ctxt.config.clone());
1081-
let mut visitor = DumpVisitor::new(save_ctxt, &mut dumper);
1078+
let (output, file_name) = self.output_file(&save_ctxt);
1079+
let mut dumper = Dumper::new(save_ctxt.config.clone());
1080+
let mut visitor = DumpVisitor::new(save_ctxt, &mut dumper);
10821081

1083-
visitor.dump_crate_info(cratename, krate);
1084-
visitor.dump_compilation_options(input, cratename);
1085-
visit::walk_crate(&mut visitor, krate);
1082+
visitor.dump_crate_info(cratename, krate);
1083+
visitor.dump_compilation_options(input, cratename);
1084+
visit::walk_crate(&mut visitor, krate);
10861085

1087-
file_name
1088-
};
1086+
dumper.to_output(|analysis| {
1087+
if let Err(e) = serde_json::to_writer(output, analysis) {
1088+
error!("Can't serialize save-analysis: {:?}", e);
1089+
}
1090+
});
10891091

10901092
if sess.opts.debugging_opts.emit_artifact_notifications {
10911093
sess.parse_sess.span_diagnostic
@@ -1107,17 +1109,19 @@ impl<'b> SaveHandler for CallbackHandler<'b> {
11071109
cratename: &str,
11081110
input: &'l Input,
11091111
) {
1110-
// We're using the JsonDumper here because it has the format of the
1112+
// We're using the Dumper here because it has the format of the
11111113
// save-analysis results that we will pass to the callback. IOW, we are
1112-
// using the JsonDumper to collect the save-analysis results, but not
1114+
// using the Dumper to collect the save-analysis results, but not
11131115
// actually to dump them to a file. This is all a bit convoluted and
11141116
// there is certainly a simpler design here trying to get out (FIXME).
1115-
let mut dumper = JsonDumper::with_callback(self.callback, save_ctxt.config.clone());
1117+
let mut dumper = Dumper::new(save_ctxt.config.clone());
11161118
let mut visitor = DumpVisitor::new(save_ctxt, &mut dumper);
11171119

11181120
visitor.dump_crate_info(cratename, krate);
11191121
visitor.dump_compilation_options(input, cratename);
11201122
visit::walk_crate(&mut visitor, krate);
1123+
1124+
dumper.to_output(|a| (self.callback)(a))
11211125
}
11221126
}
11231127

0 commit comments

Comments
 (0)