Skip to content

Commit b52a95d

Browse files
authored
Rollup merge of #63055 - Mark-Simulacrum:save-analysis-clean-2, r=Xanewok
Various cleanups to save analysis
2 parents 778b631 + 5ff0856 commit b52a95d

File tree

3 files changed

+41
-72
lines changed

3 files changed

+41
-72
lines changed

src/librustc_save_analysis/dump_visitor.rs

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use rustc_data_structures::fx::FxHashSet;
2323
use std::path::Path;
2424
use std::env;
2525

26-
use syntax::ast::{self, Attribute, NodeId, PatKind, CRATE_NODE_ID};
26+
use syntax::ast::{self, Attribute, NodeId, PatKind};
2727
use syntax::parse::token;
2828
use syntax::visit::{self, Visitor};
2929
use syntax::print::pprust::{
@@ -75,15 +75,13 @@ macro_rules! access_from_vis {
7575
};
7676
}
7777

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

8383
span: SpanUtils<'l>,
8484

85-
cur_scope: NodeId,
86-
8785
// Set of macro definition (callee) spans, and the set
8886
// of macro use (callsite) spans. We store these to ensure
8987
// we only write one macro def per unique macro definition, and
@@ -92,36 +90,29 @@ pub struct DumpVisitor<'l, 'tcx, 'll> {
9290
// macro_calls: FxHashSet<Span>,
9391
}
9492

95-
impl<'l, 'tcx, 'll> DumpVisitor<'l, 'tcx, 'll> {
93+
impl<'l, 'tcx> DumpVisitor<'l, 'tcx> {
9694
pub fn new(
9795
save_ctxt: SaveContext<'l, 'tcx>,
98-
dumper: &'ll mut Dumper,
99-
) -> DumpVisitor<'l, 'tcx, 'll> {
96+
) -> DumpVisitor<'l, 'tcx> {
10097
let span_utils = SpanUtils::new(&save_ctxt.tcx.sess);
98+
let dumper = Dumper::new(save_ctxt.config.clone());
10199
DumpVisitor {
102100
tcx: save_ctxt.tcx,
103101
save_ctxt,
104102
dumper,
105103
span: span_utils,
106-
cur_scope: CRATE_NODE_ID,
107104
// mac_defs: FxHashSet::default(),
108105
// macro_calls: FxHashSet::default(),
109106
}
110107
}
111108

112-
fn nest_scope<F>(&mut self, scope_id: NodeId, f: F)
113-
where
114-
F: FnOnce(&mut DumpVisitor<'l, 'tcx, 'll>),
115-
{
116-
let parent_scope = self.cur_scope;
117-
self.cur_scope = scope_id;
118-
f(self);
119-
self.cur_scope = parent_scope;
109+
pub fn analysis(&self) -> &rls_data::Analysis {
110+
self.dumper.analysis()
120111
}
121112

122113
fn nest_tables<F>(&mut self, item_id: NodeId, f: F)
123114
where
124-
F: FnOnce(&mut DumpVisitor<'l, 'tcx, 'll>),
115+
F: FnOnce(&mut Self),
125116
{
126117
let item_def_id = self.tcx.hir().local_def_id_from_node_id(item_id);
127118
if self.tcx.has_typeck_tables(item_def_id) {
@@ -320,7 +311,7 @@ impl<'l, 'tcx, 'll> DumpVisitor<'l, 'tcx, 'll> {
320311

321312
// walk the fn body
322313
if let Some(body) = body {
323-
self.nest_tables(id, |v| v.nest_scope(id, |v| v.visit_block(body)));
314+
self.nest_tables(id, |v| v.visit_block(body));
324315
}
325316
}
326317

@@ -405,7 +396,7 @@ impl<'l, 'tcx, 'll> DumpVisitor<'l, 'tcx, 'll> {
405396
self.visit_ty(&ret_ty);
406397
}
407398

408-
self.nest_tables(item.id, |v| v.nest_scope(item.id, |v| v.visit_block(&body)));
399+
self.nest_tables(item.id, |v| v.visit_block(&body));
409400
}
410401

411402
fn process_static_or_const_item(
@@ -1311,7 +1302,7 @@ impl<'l, 'tcx, 'll> DumpVisitor<'l, 'tcx, 'll> {
13111302
}
13121303
}
13131304

1314-
impl<'l, 'tcx, 'll> Visitor<'l> for DumpVisitor<'l, 'tcx, 'll> {
1305+
impl<'l, 'tcx> Visitor<'l> for DumpVisitor<'l, 'tcx> {
13151306
fn visit_mod(&mut self, m: &'l ast::Mod, span: Span, attrs: &[ast::Attribute], id: NodeId) {
13161307
// Since we handle explicit modules ourselves in visit_item, this should
13171308
// only get called for the root module of a crate.
@@ -1349,7 +1340,7 @@ impl<'l, 'tcx, 'll> Visitor<'l> for DumpVisitor<'l, 'tcx, 'll> {
13491340
attributes: lower_attributes(attrs.to_owned(), &self.save_ctxt),
13501341
},
13511342
);
1352-
self.nest_scope(id, |v| visit::walk_mod(v, m));
1343+
visit::walk_mod(self, m);
13531344
}
13541345

13551346
fn visit_item(&mut self, item: &'l ast::Item) {
@@ -1404,7 +1395,7 @@ impl<'l, 'tcx, 'll> Visitor<'l> for DumpVisitor<'l, 'tcx, 'll> {
14041395
}
14051396
Mod(ref m) => {
14061397
self.process_mod(item);
1407-
self.nest_scope(item.id, |v| visit::walk_mod(v, m));
1398+
visit::walk_mod(self, m);
14081399
}
14091400
Ty(ref ty, ref ty_params) => {
14101401
let qualname = format!("::{}",
@@ -1570,7 +1561,7 @@ impl<'l, 'tcx, 'll> Visitor<'l> for DumpVisitor<'l, 'tcx, 'll> {
15701561
// walk the body
15711562
self.nest_tables(ex.id, |v| {
15721563
v.process_formals(&decl.inputs, &id);
1573-
v.nest_scope(ex.id, |v| v.visit_expr(body))
1564+
v.visit_expr(body)
15741565
});
15751566
}
15761567
ast::ExprKind::ForLoop(ref pattern, ref subexpression, ref block, _) => {

src/librustc_save_analysis/dumper.rs

Lines changed: 2 additions & 2 deletions
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 analysis(&self) -> &Analysis {
26+
&self.result
2727
}
2828
}
2929

src/librustc_save_analysis/lib.rs

Lines changed: 23 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,11 @@ use syntax::visit::{self, Visitor};
3535
use syntax::print::pprust::{arg_to_string, ty_to_string};
3636
use syntax_pos::*;
3737

38-
use dumper::Dumper;
3938
use dump_visitor::DumpVisitor;
4039
use span_utils::SpanUtils;
4140

4241
use rls_data::{Def, DefKind, ExternalCrateData, GlobalCrateId, MacroRef, Ref, RefKind, Relation,
43-
RelationKind, SpanData, Impl, ImplKind};
42+
RelationKind, SpanData, Impl, ImplKind, Analysis};
4443
use rls_data::config::Config;
4544

4645
use log::{debug, error, info};
@@ -997,12 +996,10 @@ impl<'l> Visitor<'l> for PathCollector<'l> {
997996

998997
/// Defines what to do with the results of saving the analysis.
999998
pub trait SaveHandler {
1000-
fn save<'l, 'tcx>(
999+
fn save(
10011000
&mut self,
1002-
save_ctxt: SaveContext<'l, 'tcx>,
1003-
krate: &ast::Crate,
1004-
cratename: &str,
1005-
input: &'l Input,
1001+
save_ctxt: &SaveContext<'_, '_>,
1002+
analysis: &Analysis,
10061003
);
10071004
}
10081005

@@ -1062,28 +1059,17 @@ impl<'a> DumpHandler<'a> {
10621059
}
10631060
}
10641061

1065-
impl<'a> SaveHandler for DumpHandler<'a> {
1066-
fn save<'l, 'tcx>(
1062+
impl SaveHandler for DumpHandler<'_> {
1063+
fn save(
10671064
&mut self,
1068-
save_ctxt: SaveContext<'l, 'tcx>,
1069-
krate: &ast::Crate,
1070-
cratename: &str,
1071-
input: &'l Input,
1065+
save_ctxt: &SaveContext<'_, '_>,
1066+
analysis: &Analysis,
10721067
) {
10731068
let sess = &save_ctxt.tcx.sess;
10741069
let (output, file_name) = self.output_file(&save_ctxt);
1075-
let mut dumper = Dumper::new(save_ctxt.config.clone());
1076-
let mut visitor = DumpVisitor::new(save_ctxt, &mut dumper);
1077-
1078-
visitor.dump_crate_info(cratename, krate);
1079-
visitor.dump_compilation_options(input, cratename);
1080-
visit::walk_crate(&mut visitor, krate);
1081-
1082-
dumper.to_output(|analysis| {
1083-
if let Err(e) = serde_json::to_writer(output, analysis) {
1084-
error!("Can't serialize save-analysis: {:?}", e);
1085-
}
1086-
});
1070+
if let Err(e) = serde_json::to_writer(output, &analysis) {
1071+
error!("Can't serialize save-analysis: {:?}", e);
1072+
}
10871073

10881074
if sess.opts.debugging_opts.emit_artifact_notifications {
10891075
sess.parse_sess.span_diagnostic
@@ -1097,27 +1083,13 @@ pub struct CallbackHandler<'b> {
10971083
pub callback: &'b mut dyn FnMut(&rls_data::Analysis),
10981084
}
10991085

1100-
impl<'b> SaveHandler for CallbackHandler<'b> {
1101-
fn save<'l, 'tcx>(
1086+
impl SaveHandler for CallbackHandler<'_> {
1087+
fn save(
11021088
&mut self,
1103-
save_ctxt: SaveContext<'l, 'tcx>,
1104-
krate: &ast::Crate,
1105-
cratename: &str,
1106-
input: &'l Input,
1089+
_: &SaveContext<'_, '_>,
1090+
analysis: &Analysis,
11071091
) {
1108-
// We're using the Dumper here because it has the format of the
1109-
// save-analysis results that we will pass to the callback. IOW, we are
1110-
// using the Dumper to collect the save-analysis results, but not
1111-
// actually to dump them to a file. This is all a bit convoluted and
1112-
// there is certainly a simpler design here trying to get out (FIXME).
1113-
let mut dumper = Dumper::new(save_ctxt.config.clone());
1114-
let mut visitor = DumpVisitor::new(save_ctxt, &mut dumper);
1115-
1116-
visitor.dump_crate_info(cratename, krate);
1117-
visitor.dump_compilation_options(input, cratename);
1118-
visit::walk_crate(&mut visitor, krate);
1119-
1120-
dumper.to_output(|a| (self.callback)(a))
1092+
(self.callback)(analysis)
11211093
}
11221094
}
11231095

@@ -1148,7 +1120,13 @@ pub fn process_crate<'l, 'tcx, H: SaveHandler>(
11481120
impl_counter: Cell::new(0),
11491121
};
11501122

1151-
handler.save(save_ctxt, krate, cratename, input)
1123+
let mut visitor = DumpVisitor::new(save_ctxt);
1124+
1125+
visitor.dump_crate_info(cratename, krate);
1126+
visitor.dump_compilation_options(input, cratename);
1127+
visit::walk_crate(&mut visitor, krate);
1128+
1129+
handler.save(&visitor.save_ctxt, &visitor.analysis())
11521130
})
11531131
}
11541132

0 commit comments

Comments
 (0)