Move unused trait functions to inherent functions

This commit is contained in:
Mark Rousskov 2018-07-31 17:23:29 -06:00
parent 5aec365cb9
commit 6fdd6f65ca
9 changed files with 81 additions and 101 deletions

View file

@ -2366,6 +2366,7 @@ name = "rustc_metadata_utils"
version = "0.0.0" version = "0.0.0"
dependencies = [ dependencies = [
"rustc 0.0.0", "rustc 0.0.0",
"syntax 0.0.0",
"syntax_pos 0.0.0", "syntax_pos 0.0.0",
] ]

View file

@ -22,7 +22,6 @@
//! are *mostly* used as a part of that interface, but these should //! are *mostly* used as a part of that interface, but these should
//! probably get a better home if someone can find one. //! probably get a better home if someone can find one.
use hir::def;
use hir::def_id::{CrateNum, DefId, LOCAL_CRATE}; use hir::def_id::{CrateNum, DefId, LOCAL_CRATE};
use hir::map as hir_map; use hir::map as hir_map;
use hir::map::definitions::{DefKey, DefPathTable}; use hir::map::definitions::{DefKey, DefPathTable};
@ -34,8 +33,6 @@ use session::search_paths::PathKind;
use std::any::Any; use std::any::Any;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use syntax::ast; use syntax::ast;
use syntax::edition::Edition;
use syntax::ext::base::SyntaxExtension;
use syntax::symbol::Symbol; use syntax::symbol::Symbol;
use syntax_pos::Span; use syntax_pos::Span;
use rustc_target::spec::Target; use rustc_target::spec::Target;
@ -140,11 +137,6 @@ pub struct ForeignModule {
pub def_id: DefId, pub def_id: DefId,
} }
pub enum LoadedMacro {
MacroDef(ast::Item),
ProcMacro(Lrc<SyntaxExtension>),
}
#[derive(Copy, Clone, Debug)] #[derive(Copy, Clone, Debug)]
pub struct ExternCrate { pub struct ExternCrate {
pub src: ExternCrateSource, pub src: ExternCrateSource,
@ -221,9 +213,6 @@ pub trait MetadataLoader {
pub trait CrateStore { pub trait CrateStore {
fn crate_data_as_rc_any(&self, krate: CrateNum) -> Lrc<dyn Any>; fn crate_data_as_rc_any(&self, krate: CrateNum) -> Lrc<dyn Any>;
// access to the metadata loader
fn metadata_loader(&self) -> &dyn MetadataLoader;
// resolve // resolve
fn def_key(&self, def: DefId) -> DefKey; fn def_key(&self, def: DefId) -> DefKey;
fn def_path(&self, def: DefId) -> hir_map::DefPath; fn def_path(&self, def: DefId) -> hir_map::DefPath;
@ -231,18 +220,11 @@ pub trait CrateStore {
fn def_path_table(&self, cnum: CrateNum) -> Lrc<DefPathTable>; fn def_path_table(&self, cnum: CrateNum) -> Lrc<DefPathTable>;
// "queries" used in resolve that aren't tracked for incremental compilation // "queries" used in resolve that aren't tracked for incremental compilation
fn export_macros_untracked(&self, cnum: CrateNum);
fn dep_kind_untracked(&self, cnum: CrateNum) -> DepKind;
fn crate_name_untracked(&self, cnum: CrateNum) -> Symbol; fn crate_name_untracked(&self, cnum: CrateNum) -> Symbol;
fn crate_disambiguator_untracked(&self, cnum: CrateNum) -> CrateDisambiguator; fn crate_disambiguator_untracked(&self, cnum: CrateNum) -> CrateDisambiguator;
fn crate_hash_untracked(&self, cnum: CrateNum) -> Svh; fn crate_hash_untracked(&self, cnum: CrateNum) -> Svh;
fn crate_edition_untracked(&self, cnum: CrateNum) -> Edition;
fn struct_field_names_untracked(&self, def: DefId) -> Vec<ast::Name>;
fn item_children_untracked(&self, did: DefId, sess: &Session) -> Vec<def::Export>;
fn load_macro_untracked(&self, did: DefId, sess: &Session) -> LoadedMacro;
fn extern_mod_stmt_cnum_untracked(&self, emod_id: ast::NodeId) -> Option<CrateNum>; fn extern_mod_stmt_cnum_untracked(&self, emod_id: ast::NodeId) -> Option<CrateNum>;
fn item_generics_cloned_untracked(&self, def: DefId, sess: &Session) -> ty::Generics; fn item_generics_cloned_untracked(&self, def: DefId, sess: &Session) -> ty::Generics;
fn associated_item_cloned_untracked(&self, def: DefId) -> ty::AssociatedItem;
fn postorder_cnums_untracked(&self) -> Vec<CrateNum>; fn postorder_cnums_untracked(&self) -> Vec<CrateNum>;
// This is basically a 1-based range of ints, which is a little // This is basically a 1-based range of ints, which is a little

View file

@ -78,7 +78,6 @@ use rustc::session::filesearch;
use rustc::session::{early_error, early_warn}; use rustc::session::{early_error, early_warn};
use rustc::lint::Lint; use rustc::lint::Lint;
use rustc::lint; use rustc::lint;
use rustc::middle::cstore::CrateStore;
use rustc_metadata::locator; use rustc_metadata::locator;
use rustc_metadata::cstore::CStore; use rustc_metadata::cstore::CStore;
use rustc_metadata::dynamic_lib::DynamicLibrary; use rustc_metadata::dynamic_lib::DynamicLibrary;
@ -1002,7 +1001,7 @@ impl RustcDefaultCalls {
let mut v = Vec::new(); let mut v = Vec::new();
locator::list_file_metadata(&sess.target.target, locator::list_file_metadata(&sess.target.target,
path, path,
cstore.metadata_loader(), &*cstore.metadata_loader,
&mut v) &mut v)
.unwrap(); .unwrap();
println!("{}", String::from_utf8(v).unwrap()); println!("{}", String::from_utf8(v).unwrap());

View file

@ -93,6 +93,11 @@ pub struct CStore {
pub metadata_loader: Box<dyn MetadataLoader + Sync>, pub metadata_loader: Box<dyn MetadataLoader + Sync>,
} }
pub enum LoadedMacro {
MacroDef(ast::Item),
ProcMacro(Lrc<SyntaxExtension>),
}
impl CStore { impl CStore {
pub fn new(metadata_loader: Box<dyn MetadataLoader + Sync>) -> CStore { pub fn new(metadata_loader: Box<dyn MetadataLoader + Sync>) -> CStore {
CStore { CStore {

View file

@ -8,7 +8,7 @@
// option. This file may not be copied, modified, or distributed // option. This file may not be copied, modified, or distributed
// except according to those terms. // except according to those terms.
use cstore; use cstore::{self, LoadedMacro};
use encoder; use encoder;
use link_args; use link_args;
use native_libs; use native_libs;
@ -17,8 +17,8 @@ use schema;
use rustc::ty::query::QueryConfig; use rustc::ty::query::QueryConfig;
use rustc::middle::cstore::{CrateStore, DepKind, use rustc::middle::cstore::{CrateStore, DepKind,
MetadataLoader, LinkMeta, LinkMeta,
LoadedMacro, EncodedMetadata, NativeLibraryKind}; EncodedMetadata, NativeLibraryKind};
use rustc::middle::exported_symbols::ExportedSymbol; use rustc::middle::exported_symbols::ExportedSymbol;
use rustc::middle::stability::DeprecationEntry; use rustc::middle::stability::DeprecationEntry;
use rustc::hir::def; use rustc::hir::def;
@ -411,32 +411,8 @@ pub fn provide<'tcx>(providers: &mut Providers<'tcx>) {
}; };
} }
impl CrateStore for cstore::CStore { impl cstore::CStore {
fn crate_data_as_rc_any(&self, krate: CrateNum) -> Lrc<dyn Any> { pub fn export_macros_untracked(&self, cnum: CrateNum) {
self.get_crate_data(krate)
}
fn metadata_loader(&self) -> &dyn MetadataLoader {
&*self.metadata_loader
}
fn item_generics_cloned_untracked(&self, def: DefId, sess: &Session) -> ty::Generics {
self.get_crate_data(def.krate).get_generics(def.index, sess)
}
fn associated_item_cloned_untracked(&self, def: DefId) -> ty::AssociatedItem
{
self.get_crate_data(def.krate).get_associated_item(def.index)
}
fn dep_kind_untracked(&self, cnum: CrateNum) -> DepKind
{
let data = self.get_crate_data(cnum);
let r = *data.dep_kind.lock();
r
}
fn export_macros_untracked(&self, cnum: CrateNum) {
let data = self.get_crate_data(cnum); let data = self.get_crate_data(cnum);
let mut dep_kind = data.dep_kind.lock(); let mut dep_kind = data.dep_kind.lock();
if *dep_kind == DepKind::UnexportedMacrosOnly { if *dep_kind == DepKind::UnexportedMacrosOnly {
@ -444,69 +420,28 @@ impl CrateStore for cstore::CStore {
} }
} }
fn crate_name_untracked(&self, cnum: CrateNum) -> Symbol pub fn dep_kind_untracked(&self, cnum: CrateNum) -> DepKind {
{ let data = self.get_crate_data(cnum);
self.get_crate_data(cnum).name let r = *data.dep_kind.lock();
r
} }
fn crate_disambiguator_untracked(&self, cnum: CrateNum) -> CrateDisambiguator pub fn crate_edition_untracked(&self, cnum: CrateNum) -> Edition {
{
self.get_crate_data(cnum).root.disambiguator
}
fn crate_hash_untracked(&self, cnum: CrateNum) -> hir::svh::Svh
{
self.get_crate_data(cnum).root.hash
}
fn crate_edition_untracked(&self, cnum: CrateNum) -> Edition
{
self.get_crate_data(cnum).root.edition self.get_crate_data(cnum).root.edition
} }
/// Returns the `DefKey` for a given `DefId`. This indicates the pub fn struct_field_names_untracked(&self, def: DefId) -> Vec<ast::Name> {
/// parent `DefId` as well as some idea of what kind of data the
/// `DefId` refers to.
fn def_key(&self, def: DefId) -> DefKey {
// Note: loading the def-key (or def-path) for a def-id is not
// a *read* of its metadata. This is because the def-id is
// really just an interned shorthand for a def-path, which is the
// canonical name for an item.
//
// self.dep_graph.read(DepNode::MetaData(def));
self.get_crate_data(def.krate).def_key(def.index)
}
fn def_path(&self, def: DefId) -> DefPath {
// See `Note` above in `def_key()` for why this read is
// commented out:
//
// self.dep_graph.read(DepNode::MetaData(def));
self.get_crate_data(def.krate).def_path(def.index)
}
fn def_path_hash(&self, def: DefId) -> DefPathHash {
self.get_crate_data(def.krate).def_path_hash(def.index)
}
fn def_path_table(&self, cnum: CrateNum) -> Lrc<DefPathTable> {
self.get_crate_data(cnum).def_path_table.clone()
}
fn struct_field_names_untracked(&self, def: DefId) -> Vec<ast::Name>
{
self.get_crate_data(def.krate).get_struct_field_names(def.index) self.get_crate_data(def.krate).get_struct_field_names(def.index)
} }
fn item_children_untracked(&self, def_id: DefId, sess: &Session) -> Vec<def::Export> pub fn item_children_untracked(&self, def_id: DefId, sess: &Session) -> Vec<def::Export> {
{
let mut result = vec![]; let mut result = vec![];
self.get_crate_data(def_id.krate) self.get_crate_data(def_id.krate)
.each_child_of_item(def_id.index, |child| result.push(child), sess); .each_child_of_item(def_id.index, |child| result.push(child), sess);
result result
} }
fn load_macro_untracked(&self, id: DefId, sess: &Session) -> LoadedMacro { pub fn load_macro_untracked(&self, id: DefId, sess: &Session) -> LoadedMacro {
let data = self.get_crate_data(id.krate); let data = self.get_crate_data(id.krate);
if let Some(ref proc_macros) = data.proc_macros { if let Some(ref proc_macros) = data.proc_macros {
return LoadedMacro::ProcMacro(proc_macros[id.index.to_proc_macro_index()].1.clone()); return LoadedMacro::ProcMacro(proc_macros[id.index.to_proc_macro_index()].1.clone());
@ -555,6 +490,64 @@ impl CrateStore for cstore::CStore {
}) })
} }
pub fn associated_item_cloned_untracked(&self, def: DefId) -> ty::AssociatedItem {
self.get_crate_data(def.krate).get_associated_item(def.index)
}
}
impl CrateStore for cstore::CStore {
fn crate_data_as_rc_any(&self, krate: CrateNum) -> Lrc<dyn Any> {
self.get_crate_data(krate)
}
fn item_generics_cloned_untracked(&self, def: DefId, sess: &Session) -> ty::Generics {
self.get_crate_data(def.krate).get_generics(def.index, sess)
}
fn crate_name_untracked(&self, cnum: CrateNum) -> Symbol
{
self.get_crate_data(cnum).name
}
fn crate_disambiguator_untracked(&self, cnum: CrateNum) -> CrateDisambiguator
{
self.get_crate_data(cnum).root.disambiguator
}
fn crate_hash_untracked(&self, cnum: CrateNum) -> hir::svh::Svh
{
self.get_crate_data(cnum).root.hash
}
/// Returns the `DefKey` for a given `DefId`. This indicates the
/// parent `DefId` as well as some idea of what kind of data the
/// `DefId` refers to.
fn def_key(&self, def: DefId) -> DefKey {
// Note: loading the def-key (or def-path) for a def-id is not
// a *read* of its metadata. This is because the def-id is
// really just an interned shorthand for a def-path, which is the
// canonical name for an item.
//
// self.dep_graph.read(DepNode::MetaData(def));
self.get_crate_data(def.krate).def_key(def.index)
}
fn def_path(&self, def: DefId) -> DefPath {
// See `Note` above in `def_key()` for why this read is
// commented out:
//
// self.dep_graph.read(DepNode::MetaData(def));
self.get_crate_data(def.krate).def_path(def.index)
}
fn def_path_hash(&self, def: DefId) -> DefPathHash {
self.get_crate_data(def.krate).def_path_hash(def.index)
}
fn def_path_table(&self, cnum: CrateNum) -> Lrc<DefPathTable> {
self.get_crate_data(cnum).def_path_table.clone()
}
fn crates_untracked(&self) -> Vec<CrateNum> fn crates_untracked(&self) -> Vec<CrateNum>
{ {
let mut result = vec![]; let mut result = vec![];

View file

@ -10,4 +10,5 @@ crate-type = ["dylib"]
[dependencies] [dependencies]
rustc = { path = "../librustc" } rustc = { path = "../librustc" }
syntax = { path = "../libsyntax" }
syntax_pos = { path = "../libsyntax_pos" } syntax_pos = { path = "../libsyntax_pos" }

View file

@ -21,11 +21,11 @@ use {PerNS, Resolver, ResolverArenas};
use Namespace::{self, TypeNS, ValueNS, MacroNS}; use Namespace::{self, TypeNS, ValueNS, MacroNS};
use {resolve_error, resolve_struct_error, ResolutionError}; use {resolve_error, resolve_struct_error, ResolutionError};
use rustc::middle::cstore::LoadedMacro;
use rustc::hir::def::*; use rustc::hir::def::*;
use rustc::hir::def_id::{BUILTIN_MACROS_CRATE, CRATE_DEF_INDEX, LOCAL_CRATE, DefId}; use rustc::hir::def_id::{BUILTIN_MACROS_CRATE, CRATE_DEF_INDEX, LOCAL_CRATE, DefId};
use rustc::ty; use rustc::ty;
use rustc::middle::cstore::CrateStore; use rustc::middle::cstore::CrateStore;
use rustc_metadata::cstore::LoadedMacro;
use std::cell::Cell; use std::cell::Cell;
use rustc_data_structures::sync::Lrc; use rustc_data_structures::sync::Lrc;

View file

@ -25,7 +25,6 @@ use rustc::hir::def_id::{CRATE_DEF_INDEX, DefId};
use rustc::hir::def::*; use rustc::hir::def::*;
use rustc::session::DiagnosticMessageId; use rustc::session::DiagnosticMessageId;
use rustc::util::nodemap::{FxHashMap, FxHashSet}; use rustc::util::nodemap::{FxHashMap, FxHashSet};
use rustc::middle::cstore::CrateStore;
use syntax::ast::{Ident, Name, NodeId, CRATE_NODE_ID}; use syntax::ast::{Ident, Name, NodeId, CRATE_NODE_ID};
use syntax::ext::base::Determinacy::{self, Determined, Undetermined}; use syntax::ext::base::Determinacy::{self, Determined, Undetermined};

View file

@ -19,7 +19,7 @@ use syntax_pos::Span;
use rustc::hir; use rustc::hir;
use rustc::hir::def::{Def, CtorKind}; use rustc::hir::def::{Def, CtorKind};
use rustc::hir::def_id::DefId; use rustc::hir::def_id::DefId;
use rustc::middle::cstore::{CrateStore, LoadedMacro}; use rustc_metadata::cstore::LoadedMacro;
use rustc::ty; use rustc::ty;
use rustc::util::nodemap::FxHashSet; use rustc::util::nodemap::FxHashSet;