Skip to content

Commit c0ad942

Browse files
authored
Rollup merge of rust-lang#57476 - Xanewok:bye-crate-analysis, r=Zoxc
Move glob map use to query and get rid of CrateAnalysis ~Also includes commits from ~rust-lang#57392 and ~rust-lang#57436 With glob map calculated unconditionally in rust-lang#57392, this PR moves the calculated glob map to `GlobalCtxt` and exposes a relevant query (as we do with other queries which copy precomputed data over from the `Resolver`). This allows us to get rid of the `CrateAnalysis` struct in an attempt to simplify the compiler interface. cc @Zoxc r? @nikomatsakis @Zoxc @petrochenkov
2 parents 1dc54b7 + 6f1d06d commit c0ad942

File tree

12 files changed

+30
-65
lines changed

12 files changed

+30
-65
lines changed

Diff for: src/librustc/dep_graph/dep_node.rs

+1
Original file line numberDiff line numberDiff line change
@@ -630,6 +630,7 @@ define_dep_nodes!( <'tcx>
630630
[input] Freevars(DefId),
631631
[input] MaybeUnusedTraitImport(DefId),
632632
[input] MaybeUnusedExternCrates,
633+
[input] NamesImportedByGlobUse(DefId),
633634
[eval_always] StabilityIndex,
634635
[eval_always] AllTraits,
635636
[input] AllCrateNums,

Diff for: src/librustc/ty/context.rs

+10
Original file line numberDiff line numberDiff line change
@@ -983,6 +983,9 @@ pub struct GlobalCtxt<'tcx> {
983983

984984
maybe_unused_trait_imports: FxHashSet<DefId>,
985985
maybe_unused_extern_crates: Vec<(DefId, Span)>,
986+
/// A map of glob use to a set of names it actually imports. Currently only
987+
/// used in save-analysis.
988+
glob_map: FxHashMap<DefId, FxHashSet<ast::Name>>,
986989
/// Extern prelude entries. The value is `true` if the entry was introduced
987990
/// via `extern crate` item and not `--extern` option or compiler built-in.
988991
pub extern_prelude: FxHashMap<ast::Name, bool>,
@@ -1232,6 +1235,9 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
12321235
.into_iter()
12331236
.map(|(id, sp)| (hir.local_def_id(id), sp))
12341237
.collect(),
1238+
glob_map: resolutions.glob_map.into_iter().map(|(id, names)| {
1239+
(hir.local_def_id(id), names)
1240+
}).collect(),
12351241
extern_prelude: resolutions.extern_prelude,
12361242
hir_map: hir,
12371243
def_path_hash_to_def_id,
@@ -2972,6 +2978,10 @@ pub fn provide(providers: &mut ty::query::Providers<'_>) {
29722978
assert_eq!(cnum, LOCAL_CRATE);
29732979
Lrc::new(tcx.maybe_unused_extern_crates.clone())
29742980
};
2981+
providers.names_imported_by_glob_use = |tcx, id| {
2982+
assert_eq!(id.krate, LOCAL_CRATE);
2983+
Lrc::new(tcx.glob_map.get(&id).cloned().unwrap_or_default())
2984+
};
29752985

29762986
providers.stability_index = |tcx, cnum| {
29772987
assert_eq!(cnum, LOCAL_CRATE);

Diff for: src/librustc/ty/mod.rs

+2-11
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ pub use self::BorrowKind::*;
44
pub use self::IntVarValue::*;
55
pub use self::fold::TypeFoldable;
66

7-
use hir::{map as hir_map, FreevarMap, TraitMap};
7+
use hir::{map as hir_map, FreevarMap, GlobMap, TraitMap};
88
use hir::Node;
99
use hir::def::{Def, CtorKind, ExportMap};
1010
use hir::def_id::{CrateNum, DefId, LocalDefId, CRATE_DEF_INDEX, LOCAL_CRATE};
@@ -115,23 +115,14 @@ mod sty;
115115

116116
// Data types
117117

118-
/// The complete set of all analyses described in this module. This is
119-
/// produced by the driver and fed to codegen and later passes.
120-
///
121-
/// N.B., these contents are being migrated into queries using the
122-
/// *on-demand* infrastructure.
123-
#[derive(Clone)]
124-
pub struct CrateAnalysis {
125-
pub glob_map: hir::GlobMap,
126-
}
127-
128118
#[derive(Clone)]
129119
pub struct Resolutions {
130120
pub freevars: FreevarMap,
131121
pub trait_map: TraitMap,
132122
pub maybe_unused_trait_imports: NodeSet,
133123
pub maybe_unused_extern_crates: Vec<(NodeId, Span)>,
134124
pub export_map: ExportMap,
125+
pub glob_map: GlobMap,
135126
/// Extern prelude entries. The value is `true` if the entry was introduced
136127
/// via `extern crate` item and not `--extern` option or compiler built-in.
137128
pub extern_prelude: FxHashMap<Name, bool>,

Diff for: src/librustc/ty/query/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,8 @@ define_queries! { <'tcx>
541541
[] fn maybe_unused_trait_import: MaybeUnusedTraitImport(DefId) -> bool,
542542
[] fn maybe_unused_extern_crates: maybe_unused_extern_crates_node(CrateNum)
543543
-> Lrc<Vec<(DefId, Span)>>,
544+
[] fn names_imported_by_glob_use: NamesImportedByGlobUse(DefId)
545+
-> Lrc<FxHashSet<ast::Name>>,
544546

545547
[] fn stability_index: stability_index_node(CrateNum) -> Lrc<stability::Index<'tcx>>,
546548
[] fn all_crate_nums: all_crate_nums_node(CrateNum) -> Lrc<Vec<CrateNum>>,

Diff for: src/librustc/ty/query/plumbing.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1380,6 +1380,7 @@ pub fn force_from_dep_node<'a, 'gcx, 'lcx>(tcx: TyCtxt<'a, 'gcx, 'lcx>,
13801380
DepKind::MaybeUnusedTraitImport => {
13811381
force!(maybe_unused_trait_import, def_id!());
13821382
}
1383+
DepKind::NamesImportedByGlobUse => { force!(names_imported_by_glob_use, def_id!()); }
13831384
DepKind::MaybeUnusedExternCrates => { force!(maybe_unused_extern_crates, LOCAL_CRATE); }
13841385
DepKind::StabilityIndex => { force!(stability_index, LOCAL_CRATE); }
13851386
DepKind::AllTraits => { force!(all_traits, LOCAL_CRATE); }

Diff for: src/librustc_driver/driver.rs

+5-21
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,6 @@ pub fn compile_input(
168168
let ExpansionResult {
169169
expanded_crate,
170170
defs,
171-
analysis,
172171
resolutions,
173172
mut hir_forest,
174173
} = {
@@ -251,7 +250,6 @@ pub fn compile_input(
251250
output,
252251
&cstore,
253252
&hir_map,
254-
&analysis,
255253
&resolutions,
256254
&expanded_crate,
257255
&hir_map.krate(),
@@ -277,12 +275,11 @@ pub fn compile_input(
277275
sess,
278276
cstore,
279277
hir_map,
280-
analysis,
281278
resolutions,
282279
&mut arenas,
283280
&crate_name,
284281
&outputs,
285-
|tcx, analysis, rx, result| {
282+
|tcx, rx, result| {
286283
{
287284
// Eventually, we will want to track plugins.
288285
tcx.dep_graph.with_ignore(|| {
@@ -293,7 +290,6 @@ pub fn compile_input(
293290
output,
294291
opt_crate,
295292
tcx.hir().krate(),
296-
&analysis,
297293
tcx,
298294
&crate_name,
299295
);
@@ -527,7 +523,6 @@ pub struct CompileState<'a, 'tcx: 'a> {
527523
pub hir_crate: Option<&'a hir::Crate>,
528524
pub hir_map: Option<&'a hir_map::Map<'tcx>>,
529525
pub resolutions: Option<&'a Resolutions>,
530-
pub analysis: Option<&'a ty::CrateAnalysis>,
531526
pub tcx: Option<TyCtxt<'a, 'tcx, 'tcx>>,
532527
}
533528

@@ -547,7 +542,6 @@ impl<'a, 'tcx> CompileState<'a, 'tcx> {
547542
hir_crate: None,
548543
hir_map: None,
549544
resolutions: None,
550-
analysis: None,
551545
tcx: None,
552546
}
553547
}
@@ -595,7 +589,6 @@ impl<'a, 'tcx> CompileState<'a, 'tcx> {
595589
out_file: &'a Option<PathBuf>,
596590
cstore: &'tcx CStore,
597591
hir_map: &'a hir_map::Map<'tcx>,
598-
analysis: &'a ty::CrateAnalysis,
599592
resolutions: &'a Resolutions,
600593
krate: &'a ast::Crate,
601594
hir_crate: &'a hir::Crate,
@@ -606,7 +599,6 @@ impl<'a, 'tcx> CompileState<'a, 'tcx> {
606599
crate_name: Some(crate_name),
607600
cstore: Some(cstore),
608601
hir_map: Some(hir_map),
609-
analysis: Some(analysis),
610602
resolutions: Some(resolutions),
611603
expanded_crate: Some(krate),
612604
hir_crate: Some(hir_crate),
@@ -623,12 +615,10 @@ impl<'a, 'tcx> CompileState<'a, 'tcx> {
623615
out_file: &'a Option<PathBuf>,
624616
krate: Option<&'a ast::Crate>,
625617
hir_crate: &'a hir::Crate,
626-
analysis: &'a ty::CrateAnalysis,
627618
tcx: TyCtxt<'a, 'tcx, 'tcx>,
628619
crate_name: &'a str,
629620
) -> Self {
630621
CompileState {
631-
analysis: Some(analysis),
632622
tcx: Some(tcx),
633623
expanded_crate: krate,
634624
hir_crate: Some(hir_crate),
@@ -711,7 +701,6 @@ fn count_nodes(krate: &ast::Crate) -> usize {
711701
pub struct ExpansionResult {
712702
pub expanded_crate: ast::Crate,
713703
pub defs: hir_map::Definitions,
714-
pub analysis: ty::CrateAnalysis,
715704
pub resolutions: Resolutions,
716705
pub hir_forest: hir_map::Forest,
717706
}
@@ -772,16 +761,13 @@ where
772761
freevars: resolver.freevars,
773762
export_map: resolver.export_map,
774763
trait_map: resolver.trait_map,
764+
glob_map: resolver.glob_map,
775765
maybe_unused_trait_imports: resolver.maybe_unused_trait_imports,
776766
maybe_unused_extern_crates: resolver.maybe_unused_extern_crates,
777767
extern_prelude: resolver.extern_prelude.iter().map(|(ident, entry)| {
778768
(ident.name, entry.introduced_by_item)
779769
}).collect(),
780770
},
781-
782-
analysis: ty::CrateAnalysis {
783-
glob_map: resolver.glob_map
784-
},
785771
}),
786772
Err(x) => Err(x),
787773
}
@@ -1180,7 +1166,6 @@ pub fn phase_3_run_analysis_passes<'tcx, F, R>(
11801166
sess: &'tcx Session,
11811167
cstore: &'tcx CStore,
11821168
hir_map: hir_map::Map<'tcx>,
1183-
analysis: ty::CrateAnalysis,
11841169
resolutions: Resolutions,
11851170
arenas: &'tcx mut AllArenas<'tcx>,
11861171
name: &str,
@@ -1190,7 +1175,6 @@ pub fn phase_3_run_analysis_passes<'tcx, F, R>(
11901175
where
11911176
F: for<'a> FnOnce(
11921177
TyCtxt<'a, 'tcx, 'tcx>,
1193-
ty::CrateAnalysis,
11941178
mpsc::Receiver<Box<dyn Any + Send>>,
11951179
CompileResult,
11961180
) -> R,
@@ -1254,7 +1238,7 @@ where
12541238
match typeck::check_crate(tcx) {
12551239
Ok(x) => x,
12561240
Err(x) => {
1257-
f(tcx, analysis, rx, Err(x));
1241+
f(tcx, rx, Err(x));
12581242
return Err(x);
12591243
}
12601244
}
@@ -1307,7 +1291,7 @@ where
13071291
// lint warnings and so on -- kindck used to do this abort, but
13081292
// kindck is gone now). -nmatsakis
13091293
if sess.err_count() > 0 {
1310-
return Ok(f(tcx, analysis, rx, sess.compile_status()));
1294+
return Ok(f(tcx, rx, sess.compile_status()));
13111295
}
13121296

13131297
time(sess, "death checking", || middle::dead::check_crate(tcx));
@@ -1318,7 +1302,7 @@ where
13181302

13191303
time(sess, "lint checking", || lint::check_crate(tcx));
13201304

1321-
return Ok(f(tcx, analysis, rx, tcx.sess.compile_status()));
1305+
return Ok(f(tcx, rx, tcx.sess.compile_status()));
13221306
},
13231307
)
13241308
}

Diff for: src/librustc_driver/lib.rs

-2
Original file line numberDiff line numberDiff line change
@@ -879,7 +879,6 @@ impl<'a> CompilerCalls<'a> for RustcDefaultCalls {
879879
pretty::print_after_hir_lowering(state.session,
880880
state.cstore.unwrap(),
881881
state.hir_map.unwrap(),
882-
state.analysis.unwrap(),
883882
state.resolutions.unwrap(),
884883
state.input,
885884
&state.expanded_crate.take().unwrap(),
@@ -940,7 +939,6 @@ pub fn enable_save_analysis(control: &mut CompileController) {
940939
time(state.session, "save analysis", || {
941940
save::process_crate(state.tcx.unwrap(),
942941
state.expanded_crate.unwrap(),
943-
state.analysis.unwrap(),
944942
state.crate_name.unwrap(),
945943
state.input,
946944
None,

Diff for: src/librustc_driver/pretty.rs

+3-13
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,6 @@ impl PpSourceMode {
190190
sess: &'tcx Session,
191191
cstore: &'tcx CStore,
192192
hir_map: &hir_map::Map<'tcx>,
193-
analysis: &ty::CrateAnalysis,
194193
resolutions: &Resolutions,
195194
output_filenames: &OutputFilenames,
196195
id: &str,
@@ -223,12 +222,11 @@ impl PpSourceMode {
223222
sess,
224223
cstore,
225224
hir_map.clone(),
226-
analysis.clone(),
227225
resolutions.clone(),
228226
&mut arenas,
229227
id,
230228
output_filenames,
231-
|tcx, _, _, _| {
229+
|tcx, _, _| {
232230
let empty_tables = ty::TypeckTables::empty(None);
233231
let annotation = TypedAnnotation {
234232
tcx,
@@ -959,7 +957,6 @@ pub fn print_after_parsing(sess: &Session,
959957
pub fn print_after_hir_lowering<'tcx, 'a: 'tcx>(sess: &'a Session,
960958
cstore: &'tcx CStore,
961959
hir_map: &hir_map::Map<'tcx>,
962-
analysis: &ty::CrateAnalysis,
963960
resolutions: &Resolutions,
964961
input: &Input,
965962
krate: &ast::Crate,
@@ -972,7 +969,6 @@ pub fn print_after_hir_lowering<'tcx, 'a: 'tcx>(sess: &'a Session,
972969
print_with_analysis(sess,
973970
cstore,
974971
hir_map,
975-
analysis,
976972
resolutions,
977973
crate_name,
978974
output_filenames,
@@ -1010,7 +1006,6 @@ pub fn print_after_hir_lowering<'tcx, 'a: 'tcx>(sess: &'a Session,
10101006
s.call_with_pp_support_hir(sess,
10111007
cstore,
10121008
hir_map,
1013-
analysis,
10141009
resolutions,
10151010
output_filenames,
10161011
crate_name,
@@ -1033,7 +1028,6 @@ pub fn print_after_hir_lowering<'tcx, 'a: 'tcx>(sess: &'a Session,
10331028
s.call_with_pp_support_hir(sess,
10341029
cstore,
10351030
hir_map,
1036-
analysis,
10371031
resolutions,
10381032
output_filenames,
10391033
crate_name,
@@ -1048,7 +1042,6 @@ pub fn print_after_hir_lowering<'tcx, 'a: 'tcx>(sess: &'a Session,
10481042
s.call_with_pp_support_hir(sess,
10491043
cstore,
10501044
hir_map,
1051-
analysis,
10521045
resolutions,
10531046
output_filenames,
10541047
crate_name,
@@ -1081,7 +1074,6 @@ pub fn print_after_hir_lowering<'tcx, 'a: 'tcx>(sess: &'a Session,
10811074
s.call_with_pp_support_hir(sess,
10821075
cstore,
10831076
hir_map,
1084-
analysis,
10851077
resolutions,
10861078
output_filenames,
10871079
crate_name,
@@ -1103,13 +1095,12 @@ pub fn print_after_hir_lowering<'tcx, 'a: 'tcx>(sess: &'a Session,
11031095
}
11041096

11051097
// In an ideal world, this would be a public function called by the driver after
1106-
// analsysis is performed. However, we want to call `phase_3_run_analysis_passes`
1098+
// analysis is performed. However, we want to call `phase_3_run_analysis_passes`
11071099
// with a different callback than the standard driver, so that isn't easy.
11081100
// Instead, we call that function ourselves.
11091101
fn print_with_analysis<'tcx, 'a: 'tcx>(sess: &'a Session,
11101102
cstore: &'a CStore,
11111103
hir_map: &hir_map::Map<'tcx>,
1112-
analysis: &ty::CrateAnalysis,
11131104
resolutions: &Resolutions,
11141105
crate_name: &str,
11151106
output_filenames: &OutputFilenames,
@@ -1134,12 +1125,11 @@ fn print_with_analysis<'tcx, 'a: 'tcx>(sess: &'a Session,
11341125
sess,
11351126
cstore,
11361127
hir_map.clone(),
1137-
analysis.clone(),
11381128
resolutions.clone(),
11391129
&mut arenas,
11401130
crate_name,
11411131
output_filenames,
1142-
|tcx, _, _, _| {
1132+
|tcx, _, _| {
11431133
match ppm {
11441134
PpmMir | PpmMirCFG => {
11451135
if let Some(nodeid) = nodeid {

Diff for: src/librustc_save_analysis/dump_visitor.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -1238,12 +1238,9 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> DumpVisitor<'l, 'tcx, 'll, O> {
12381238
};
12391239

12401240
// Make a comma-separated list of names of imported modules.
1241-
let glob_map = &self.save_ctxt.analysis.glob_map;
1242-
let names = if glob_map.contains_key(&id) {
1243-
glob_map.get(&id).unwrap().iter().map(|n| n.to_string()).collect()
1244-
} else {
1245-
Vec::new()
1246-
};
1241+
let def_id = self.tcx.hir().local_def_id(id);
1242+
let names = self.tcx.names_imported_by_glob_use(def_id);
1243+
let names: Vec<_> = names.iter().map(|n| n.to_string()).collect();
12471244

12481245
// Otherwise it's a span with wrong macro expansion info, which
12491246
// we don't want to track anyway, since it's probably macro-internal `use`

Diff for: src/librustc_save_analysis/lib.rs

-3
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ pub struct SaveContext<'l, 'tcx: 'l> {
7171
tcx: TyCtxt<'l, 'tcx, 'tcx>,
7272
tables: &'l ty::TypeckTables<'tcx>,
7373
access_levels: &'l AccessLevels,
74-
analysis: &'l ty::CrateAnalysis,
7574
span_utils: SpanUtils<'tcx>,
7675
config: Config,
7776
impl_counter: Cell<u32>,
@@ -1120,7 +1119,6 @@ impl<'b> SaveHandler for CallbackHandler<'b> {
11201119
pub fn process_crate<'l, 'tcx, H: SaveHandler>(
11211120
tcx: TyCtxt<'l, 'tcx, 'tcx>,
11221121
krate: &ast::Crate,
1123-
analysis: &'l ty::CrateAnalysis,
11241122
cratename: &str,
11251123
input: &'l Input,
11261124
config: Option<Config>,
@@ -1139,7 +1137,6 @@ pub fn process_crate<'l, 'tcx, H: SaveHandler>(
11391137
let save_ctxt = SaveContext {
11401138
tcx,
11411139
tables: &ty::TypeckTables::empty(None),
1142-
analysis,
11431140
access_levels: &access_levels,
11441141
span_utils: SpanUtils::new(&tcx.sess),
11451142
config: find_config(config),

0 commit comments

Comments
 (0)