Querify glob map usage (last use of CrateAnalysis)

This commit is contained in:
Igor Matuszewski 2019-01-07 11:46:44 +01:00
parent 6c8fdf971c
commit 6f1d06d219
12 changed files with 30 additions and 62 deletions

View File

@ -630,6 +630,7 @@ define_dep_nodes!( <'tcx>
[input] Freevars(DefId),
[input] MaybeUnusedTraitImport(DefId),
[input] MaybeUnusedExternCrates,
[input] NamesImportedByGlobUse(DefId),
[eval_always] StabilityIndex,
[eval_always] AllTraits,
[input] AllCrateNums,

View File

@ -983,6 +983,9 @@ pub struct GlobalCtxt<'tcx> {
maybe_unused_trait_imports: FxHashSet<DefId>,
maybe_unused_extern_crates: Vec<(DefId, Span)>,
/// A map of glob use to a set of names it actually imports. Currently only
/// used in save-analysis.
glob_map: FxHashMap<DefId, FxHashSet<ast::Name>>,
/// Extern prelude entries. The value is `true` if the entry was introduced
/// via `extern crate` item and not `--extern` option or compiler built-in.
pub extern_prelude: FxHashMap<ast::Name, bool>,
@ -1232,6 +1235,9 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
.into_iter()
.map(|(id, sp)| (hir.local_def_id(id), sp))
.collect(),
glob_map: resolutions.glob_map.into_iter().map(|(id, names)| {
(hir.local_def_id(id), names)
}).collect(),
extern_prelude: resolutions.extern_prelude,
hir_map: hir,
def_path_hash_to_def_id,
@ -2972,6 +2978,10 @@ pub fn provide(providers: &mut ty::query::Providers<'_>) {
assert_eq!(cnum, LOCAL_CRATE);
Lrc::new(tcx.maybe_unused_extern_crates.clone())
};
providers.names_imported_by_glob_use = |tcx, id| {
assert_eq!(id.krate, LOCAL_CRATE);
Lrc::new(tcx.glob_map.get(&id).cloned().unwrap_or_default())
};
providers.stability_index = |tcx, cnum| {
assert_eq!(cnum, LOCAL_CRATE);

View File

@ -4,7 +4,7 @@ pub use self::BorrowKind::*;
pub use self::IntVarValue::*;
pub use self::fold::TypeFoldable;
use hir::{map as hir_map, FreevarMap, TraitMap};
use hir::{map as hir_map, FreevarMap, GlobMap, TraitMap};
use hir::Node;
use hir::def::{Def, CtorKind, ExportMap};
use hir::def_id::{CrateNum, DefId, LocalDefId, CRATE_DEF_INDEX, LOCAL_CRATE};
@ -115,16 +115,6 @@ mod sty;
// Data types
/// The complete set of all analyses described in this module. This is
/// produced by the driver and fed to codegen and later passes.
///
/// N.B., these contents are being migrated into queries using the
/// *on-demand* infrastructure.
#[derive(Clone)]
pub struct CrateAnalysis {
pub glob_map: hir::GlobMap,
}
#[derive(Clone)]
pub struct Resolutions {
pub freevars: FreevarMap,
@ -132,6 +122,7 @@ pub struct Resolutions {
pub maybe_unused_trait_imports: NodeSet,
pub maybe_unused_extern_crates: Vec<(NodeId, Span)>,
pub export_map: ExportMap,
pub glob_map: GlobMap,
/// Extern prelude entries. The value is `true` if the entry was introduced
/// via `extern crate` item and not `--extern` option or compiler built-in.
pub extern_prelude: FxHashMap<Name, bool>,

View File

@ -541,6 +541,8 @@ define_queries! { <'tcx>
[] fn maybe_unused_trait_import: MaybeUnusedTraitImport(DefId) -> bool,
[] fn maybe_unused_extern_crates: maybe_unused_extern_crates_node(CrateNum)
-> Lrc<Vec<(DefId, Span)>>,
[] fn names_imported_by_glob_use: NamesImportedByGlobUse(DefId)
-> Lrc<FxHashSet<ast::Name>>,
[] fn stability_index: stability_index_node(CrateNum) -> Lrc<stability::Index<'tcx>>,
[] fn all_crate_nums: all_crate_nums_node(CrateNum) -> Lrc<Vec<CrateNum>>,

View File

@ -1413,6 +1413,7 @@ pub fn force_from_dep_node<'a, 'gcx, 'lcx>(tcx: TyCtxt<'a, 'gcx, 'lcx>,
DepKind::MaybeUnusedTraitImport => {
force!(maybe_unused_trait_import, def_id!());
}
DepKind::NamesImportedByGlobUse => { force!(names_imported_by_glob_use, def_id!()); }
DepKind::MaybeUnusedExternCrates => { force!(maybe_unused_extern_crates, LOCAL_CRATE); }
DepKind::StabilityIndex => { force!(stability_index, LOCAL_CRATE); }
DepKind::AllTraits => { force!(all_traits, LOCAL_CRATE); }

View File

@ -168,7 +168,6 @@ pub fn compile_input(
let ExpansionResult {
expanded_crate,
defs,
analysis,
resolutions,
mut hir_forest,
} = {
@ -251,7 +250,6 @@ pub fn compile_input(
output,
&cstore,
&hir_map,
&analysis,
&resolutions,
&expanded_crate,
&hir_map.krate(),
@ -277,12 +275,11 @@ pub fn compile_input(
sess,
cstore,
hir_map,
analysis,
resolutions,
&mut arenas,
&crate_name,
&outputs,
|tcx, analysis, rx, result| {
|tcx, rx, result| {
{
// Eventually, we will want to track plugins.
tcx.dep_graph.with_ignore(|| {
@ -293,7 +290,6 @@ pub fn compile_input(
output,
opt_crate,
tcx.hir().krate(),
&analysis,
tcx,
&crate_name,
);
@ -527,7 +523,6 @@ pub struct CompileState<'a, 'tcx: 'a> {
pub hir_crate: Option<&'a hir::Crate>,
pub hir_map: Option<&'a hir_map::Map<'tcx>>,
pub resolutions: Option<&'a Resolutions>,
pub analysis: Option<&'a ty::CrateAnalysis>,
pub tcx: Option<TyCtxt<'a, 'tcx, 'tcx>>,
}
@ -547,7 +542,6 @@ impl<'a, 'tcx> CompileState<'a, 'tcx> {
hir_crate: None,
hir_map: None,
resolutions: None,
analysis: None,
tcx: None,
}
}
@ -595,7 +589,6 @@ impl<'a, 'tcx> CompileState<'a, 'tcx> {
out_file: &'a Option<PathBuf>,
cstore: &'tcx CStore,
hir_map: &'a hir_map::Map<'tcx>,
analysis: &'a ty::CrateAnalysis,
resolutions: &'a Resolutions,
krate: &'a ast::Crate,
hir_crate: &'a hir::Crate,
@ -606,7 +599,6 @@ impl<'a, 'tcx> CompileState<'a, 'tcx> {
crate_name: Some(crate_name),
cstore: Some(cstore),
hir_map: Some(hir_map),
analysis: Some(analysis),
resolutions: Some(resolutions),
expanded_crate: Some(krate),
hir_crate: Some(hir_crate),
@ -623,12 +615,10 @@ impl<'a, 'tcx> CompileState<'a, 'tcx> {
out_file: &'a Option<PathBuf>,
krate: Option<&'a ast::Crate>,
hir_crate: &'a hir::Crate,
analysis: &'a ty::CrateAnalysis,
tcx: TyCtxt<'a, 'tcx, 'tcx>,
crate_name: &'a str,
) -> Self {
CompileState {
analysis: Some(analysis),
tcx: Some(tcx),
expanded_crate: krate,
hir_crate: Some(hir_crate),
@ -711,7 +701,6 @@ fn count_nodes(krate: &ast::Crate) -> usize {
pub struct ExpansionResult {
pub expanded_crate: ast::Crate,
pub defs: hir_map::Definitions,
pub analysis: ty::CrateAnalysis,
pub resolutions: Resolutions,
pub hir_forest: hir_map::Forest,
}
@ -772,16 +761,13 @@ where
freevars: resolver.freevars,
export_map: resolver.export_map,
trait_map: resolver.trait_map,
glob_map: resolver.glob_map,
maybe_unused_trait_imports: resolver.maybe_unused_trait_imports,
maybe_unused_extern_crates: resolver.maybe_unused_extern_crates,
extern_prelude: resolver.extern_prelude.iter().map(|(ident, entry)| {
(ident.name, entry.introduced_by_item)
}).collect(),
},
analysis: ty::CrateAnalysis {
glob_map: resolver.glob_map
},
}),
Err(x) => Err(x),
}
@ -1180,7 +1166,6 @@ pub fn phase_3_run_analysis_passes<'tcx, F, R>(
sess: &'tcx Session,
cstore: &'tcx CStore,
hir_map: hir_map::Map<'tcx>,
analysis: ty::CrateAnalysis,
resolutions: Resolutions,
arenas: &'tcx mut AllArenas<'tcx>,
name: &str,
@ -1190,7 +1175,6 @@ pub fn phase_3_run_analysis_passes<'tcx, F, R>(
where
F: for<'a> FnOnce(
TyCtxt<'a, 'tcx, 'tcx>,
ty::CrateAnalysis,
mpsc::Receiver<Box<dyn Any + Send>>,
CompileResult,
) -> R,
@ -1254,7 +1238,7 @@ where
match typeck::check_crate(tcx) {
Ok(x) => x,
Err(x) => {
f(tcx, analysis, rx, Err(x));
f(tcx, rx, Err(x));
return Err(x);
}
}
@ -1307,7 +1291,7 @@ where
// lint warnings and so on -- kindck used to do this abort, but
// kindck is gone now). -nmatsakis
if sess.err_count() > 0 {
return Ok(f(tcx, analysis, rx, sess.compile_status()));
return Ok(f(tcx, rx, sess.compile_status()));
}
time(sess, "death checking", || middle::dead::check_crate(tcx));
@ -1318,7 +1302,7 @@ where
time(sess, "lint checking", || lint::check_crate(tcx));
return Ok(f(tcx, analysis, rx, tcx.sess.compile_status()));
return Ok(f(tcx, rx, tcx.sess.compile_status()));
},
)
}

View File

@ -879,7 +879,6 @@ impl<'a> CompilerCalls<'a> for RustcDefaultCalls {
pretty::print_after_hir_lowering(state.session,
state.cstore.unwrap(),
state.hir_map.unwrap(),
state.analysis.unwrap(),
state.resolutions.unwrap(),
state.input,
&state.expanded_crate.take().unwrap(),
@ -940,7 +939,6 @@ pub fn enable_save_analysis(control: &mut CompileController) {
time(state.session, "save analysis", || {
save::process_crate(state.tcx.unwrap(),
state.expanded_crate.unwrap(),
state.analysis.unwrap(),
state.crate_name.unwrap(),
state.input,
None,

View File

@ -190,7 +190,6 @@ impl PpSourceMode {
sess: &'tcx Session,
cstore: &'tcx CStore,
hir_map: &hir_map::Map<'tcx>,
analysis: &ty::CrateAnalysis,
resolutions: &Resolutions,
output_filenames: &OutputFilenames,
id: &str,
@ -223,12 +222,11 @@ impl PpSourceMode {
sess,
cstore,
hir_map.clone(),
analysis.clone(),
resolutions.clone(),
&mut arenas,
id,
output_filenames,
|tcx, _, _, _| {
|tcx, _, _| {
let empty_tables = ty::TypeckTables::empty(None);
let annotation = TypedAnnotation {
tcx,
@ -959,7 +957,6 @@ pub fn print_after_parsing(sess: &Session,
pub fn print_after_hir_lowering<'tcx, 'a: 'tcx>(sess: &'a Session,
cstore: &'tcx CStore,
hir_map: &hir_map::Map<'tcx>,
analysis: &ty::CrateAnalysis,
resolutions: &Resolutions,
input: &Input,
krate: &ast::Crate,
@ -972,7 +969,6 @@ pub fn print_after_hir_lowering<'tcx, 'a: 'tcx>(sess: &'a Session,
print_with_analysis(sess,
cstore,
hir_map,
analysis,
resolutions,
crate_name,
output_filenames,
@ -1010,7 +1006,6 @@ pub fn print_after_hir_lowering<'tcx, 'a: 'tcx>(sess: &'a Session,
s.call_with_pp_support_hir(sess,
cstore,
hir_map,
analysis,
resolutions,
output_filenames,
crate_name,
@ -1033,7 +1028,6 @@ pub fn print_after_hir_lowering<'tcx, 'a: 'tcx>(sess: &'a Session,
s.call_with_pp_support_hir(sess,
cstore,
hir_map,
analysis,
resolutions,
output_filenames,
crate_name,
@ -1048,7 +1042,6 @@ pub fn print_after_hir_lowering<'tcx, 'a: 'tcx>(sess: &'a Session,
s.call_with_pp_support_hir(sess,
cstore,
hir_map,
analysis,
resolutions,
output_filenames,
crate_name,
@ -1081,7 +1074,6 @@ pub fn print_after_hir_lowering<'tcx, 'a: 'tcx>(sess: &'a Session,
s.call_with_pp_support_hir(sess,
cstore,
hir_map,
analysis,
resolutions,
output_filenames,
crate_name,
@ -1103,13 +1095,12 @@ pub fn print_after_hir_lowering<'tcx, 'a: 'tcx>(sess: &'a Session,
}
// In an ideal world, this would be a public function called by the driver after
// analsysis is performed. However, we want to call `phase_3_run_analysis_passes`
// analysis is performed. However, we want to call `phase_3_run_analysis_passes`
// with a different callback than the standard driver, so that isn't easy.
// Instead, we call that function ourselves.
fn print_with_analysis<'tcx, 'a: 'tcx>(sess: &'a Session,
cstore: &'a CStore,
hir_map: &hir_map::Map<'tcx>,
analysis: &ty::CrateAnalysis,
resolutions: &Resolutions,
crate_name: &str,
output_filenames: &OutputFilenames,
@ -1134,12 +1125,11 @@ fn print_with_analysis<'tcx, 'a: 'tcx>(sess: &'a Session,
sess,
cstore,
hir_map.clone(),
analysis.clone(),
resolutions.clone(),
&mut arenas,
crate_name,
output_filenames,
|tcx, _, _, _| {
|tcx, _, _| {
match ppm {
PpmMir | PpmMirCFG => {
if let Some(nodeid) = nodeid {

View File

@ -1238,12 +1238,9 @@ impl<'l, 'tcx: 'l, 'll, O: DumpOutput + 'll> DumpVisitor<'l, 'tcx, 'll, O> {
};
// Make a comma-separated list of names of imported modules.
let glob_map = &self.save_ctxt.analysis.glob_map;
let names = if glob_map.contains_key(&id) {
glob_map.get(&id).unwrap().iter().map(|n| n.to_string()).collect()
} else {
Vec::new()
};
let def_id = self.tcx.hir().local_def_id(id);
let names = self.tcx.names_imported_by_glob_use(def_id);
let names: Vec<_> = names.iter().map(|n| n.to_string()).collect();
// Otherwise it's a span with wrong macro expansion info, which
// we don't want to track anyway, since it's probably macro-internal `use`

View File

@ -71,7 +71,6 @@ pub struct SaveContext<'l, 'tcx: 'l> {
tcx: TyCtxt<'l, 'tcx, 'tcx>,
tables: &'l ty::TypeckTables<'tcx>,
access_levels: &'l AccessLevels,
analysis: &'l ty::CrateAnalysis,
span_utils: SpanUtils<'tcx>,
config: Config,
impl_counter: Cell<u32>,
@ -1120,7 +1119,6 @@ impl<'b> SaveHandler for CallbackHandler<'b> {
pub fn process_crate<'l, 'tcx, H: SaveHandler>(
tcx: TyCtxt<'l, 'tcx, 'tcx>,
krate: &ast::Crate,
analysis: &'l ty::CrateAnalysis,
cratename: &str,
input: &'l Input,
config: Option<Config>,
@ -1139,7 +1137,6 @@ pub fn process_crate<'l, 'tcx, H: SaveHandler>(
let save_ctxt = SaveContext {
tcx,
tables: &ty::TypeckTables::empty(None),
analysis,
access_levels: &access_levels,
span_utils: SpanUtils::new(&tcx.sess),
config: find_config(config),

View File

@ -465,15 +465,13 @@ pub fn run_core(options: RustdocOptions) -> (clean::Crate, RenderInfo, RenderOpt
freevars: resolver.freevars.clone(),
export_map: resolver.export_map.clone(),
trait_map: resolver.trait_map.clone(),
glob_map: resolver.glob_map.clone(),
maybe_unused_trait_imports: resolver.maybe_unused_trait_imports.clone(),
maybe_unused_extern_crates: resolver.maybe_unused_extern_crates.clone(),
extern_prelude: resolver.extern_prelude.iter().map(|(ident, entry)| {
(ident.name, entry.introduced_by_item)
}).collect(),
};
let analysis = ty::CrateAnalysis {
glob_map: resolver.glob_map.clone(),
};
let mut arenas = AllArenas::new();
let hir_map = hir_map::map_crate(&sess, &*cstore, &mut hir_forest, &defs);
@ -489,12 +487,11 @@ pub fn run_core(options: RustdocOptions) -> (clean::Crate, RenderInfo, RenderOpt
&sess,
&*cstore,
hir_map,
analysis,
resolutions,
&mut arenas,
&name,
&output_filenames,
|tcx, _, _, result| {
|tcx, _, result| {
if result.is_err() {
sess.fatal("Compilation failed, aborting rustdoc");
}

View File

@ -309,7 +309,7 @@ pub struct Cache {
// Note that external items for which `doc(hidden)` applies to are shown as
// non-reachable while local items aren't. This is because we're reusing
// the access levels from crateanalysis.
// the access levels from the privacy check pass.
pub access_levels: AccessLevels<DefId>,
/// The version of the crate being documented, if given from the `--crate-version` flag.