Skip to content

Commit 5b82289

Browse files
Store dumper directly in Visitor
1 parent a6d6eea commit 5b82289

File tree

3 files changed

+19
-25
lines changed

3 files changed

+19
-25
lines changed

src/librustc_save_analysis/dump_visitor.rs

+11-7
Original file line numberDiff line numberDiff line change
@@ -75,10 +75,10 @@ macro_rules! access_from_vis {
7575
};
7676
}
7777

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

8383
span: SpanUtils<'l>,
8484

@@ -90,12 +90,12 @@ pub struct DumpVisitor<'l, 'tcx, 'll> {
9090
// macro_calls: FxHashSet<Span>,
9191
}
9292

93-
impl<'l, 'tcx, 'll> DumpVisitor<'l, 'tcx, 'll> {
93+
impl<'l, 'tcx> DumpVisitor<'l, 'tcx> {
9494
pub fn new(
9595
save_ctxt: SaveContext<'l, 'tcx>,
96-
dumper: &'ll mut Dumper,
97-
) -> DumpVisitor<'l, 'tcx, 'll> {
96+
) -> DumpVisitor<'l, 'tcx> {
9897
let span_utils = SpanUtils::new(&save_ctxt.tcx.sess);
98+
let dumper = Dumper::new(save_ctxt.config.clone());
9999
DumpVisitor {
100100
tcx: save_ctxt.tcx,
101101
save_ctxt,
@@ -106,9 +106,13 @@ impl<'l, 'tcx, 'll> DumpVisitor<'l, 'tcx, 'll> {
106106
}
107107
}
108108

109+
pub fn into_analysis(self) -> rls_data::Analysis {
110+
self.dumper.into_analysis()
111+
}
112+
109113
fn nest_tables<F>(&mut self, item_id: NodeId, f: F)
110114
where
111-
F: FnOnce(&mut DumpVisitor<'l, 'tcx, 'll>),
115+
F: FnOnce(&mut Self),
112116
{
113117
let item_def_id = self.tcx.hir().local_def_id_from_node_id(item_id);
114118
if self.tcx.has_typeck_tables(item_def_id) {
@@ -1298,7 +1302,7 @@ impl<'l, 'tcx, 'll> DumpVisitor<'l, 'tcx, 'll> {
12981302
}
12991303
}
13001304

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

src/librustc_save_analysis/dumper.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ impl Dumper {
2222
}
2323
}
2424

25-
pub fn to_output(self, f: impl FnOnce(&Analysis)) {
26-
f(&self.result)
25+
pub fn into_analysis(self) -> Analysis {
26+
self.result
2727
}
2828
}
2929

src/librustc_save_analysis/lib.rs

+6-16
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ use syntax::visit::{self, Visitor};
3939
use syntax::print::pprust::{arg_to_string, ty_to_string};
4040
use syntax_pos::*;
4141

42-
use dumper::Dumper;
4342
use dump_visitor::DumpVisitor;
4443
use span_utils::SpanUtils;
4544

@@ -1076,18 +1075,15 @@ impl<'a> SaveHandler for DumpHandler<'a> {
10761075
) {
10771076
let sess = &save_ctxt.tcx.sess;
10781077
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);
1078+
let mut visitor = DumpVisitor::new(save_ctxt);
10811079

10821080
visitor.dump_crate_info(cratename, krate);
10831081
visitor.dump_compilation_options(input, cratename);
10841082
visit::walk_crate(&mut visitor, krate);
10851083

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-
});
1084+
if let Err(e) = serde_json::to_writer(output, &visitor.into_analysis()) {
1085+
error!("Can't serialize save-analysis: {:?}", e);
1086+
}
10911087

10921088
if sess.opts.debugging_opts.emit_artifact_notifications {
10931089
sess.parse_sess.span_diagnostic
@@ -1109,19 +1105,13 @@ impl<'b> SaveHandler for CallbackHandler<'b> {
11091105
cratename: &str,
11101106
input: &'l Input,
11111107
) {
1112-
// We're using the Dumper here because it has the format of the
1113-
// save-analysis results that we will pass to the callback. IOW, we are
1114-
// using the Dumper to collect the save-analysis results, but not
1115-
// actually to dump them to a file. This is all a bit convoluted and
1116-
// there is certainly a simpler design here trying to get out (FIXME).
1117-
let mut dumper = Dumper::new(save_ctxt.config.clone());
1118-
let mut visitor = DumpVisitor::new(save_ctxt, &mut dumper);
1108+
let mut visitor = DumpVisitor::new(save_ctxt);
11191109

11201110
visitor.dump_crate_info(cratename, krate);
11211111
visitor.dump_compilation_options(input, cratename);
11221112
visit::walk_crate(&mut visitor, krate);
11231113

1124-
dumper.to_output(|a| (self.callback)(a))
1114+
(self.callback)(&visitor.into_analysis())
11251115
}
11261116
}
11271117

0 commit comments

Comments
 (0)