Skip to content

Commit

Permalink
rustc_typeck: lift CrateCtxt to TyCtxt.
Browse files Browse the repository at this point in the history
  • Loading branch information
eddyb committed Feb 25, 2017
1 parent 374ea14 commit 4649f73
Show file tree
Hide file tree
Showing 24 changed files with 651 additions and 757 deletions.
17 changes: 17 additions & 0 deletions src/librustc/ty/context.rs
Expand Up @@ -525,6 +525,20 @@ pub struct GlobalCtxt<'tcx> {
stability_interner: RefCell<FxHashSet<&'tcx attr::Stability>>,

layout_interner: RefCell<FxHashSet<&'tcx Layout>>,

/// A vector of every trait accessible in the whole crate
/// (i.e. including those from subcrates). This is used only for
/// error reporting, and so is lazily initialised and generally
/// shouldn't taint the common path (hence the RefCell).
pub all_traits: RefCell<Option<Vec<DefId>>>,

/// Obligations which will have to be checked at the end of
/// type-checking, after all functions have been inferred.
/// The key is the NodeId of the item the obligations were from.
pub deferred_obligations: RefCell<NodeMap<Vec<traits::DeferredObligation<'tcx>>>>,

/// HIR Ty -> Ty lowering cache.
pub ast_ty_to_ty_cache: RefCell<NodeMap<Ty<'tcx>>>,
}

impl<'tcx> GlobalCtxt<'tcx> {
Expand Down Expand Up @@ -720,6 +734,9 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
layout_depth: Cell::new(0),
derive_macros: RefCell::new(NodeMap()),
stability_interner: RefCell::new(FxHashSet()),
all_traits: RefCell::new(None),
deferred_obligations: RefCell::new(NodeMap()),
ast_ty_to_ty_cache: RefCell::new(NodeMap()),
}, f)
}
}
Expand Down
13 changes: 13 additions & 0 deletions src/librustc/ty/maps.rs
Expand Up @@ -37,6 +37,7 @@ macro_rules! define_maps {
pub $name:ident: $node:ident($K:ty) -> $V:ty),*) => {
pub struct Maps<$tcx> {
providers: IndexVec<CrateNum, Providers<$tcx>>,
pub query_stack: RefCell<Vec<Query>>,
$($(#[$attr])* pub $name: RefCell<DepTrackingMap<queries::$name<$tcx>>>),*
}

Expand All @@ -46,11 +47,18 @@ macro_rules! define_maps {
-> Self {
Maps {
providers,
query_stack: RefCell::new(vec![]),
$($name: RefCell::new(DepTrackingMap::new(dep_graph.clone()))),*
}
}
}

#[allow(bad_style)]
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum Query {
$($(#[$attr])* $name($K)),*
}

pub mod queries {
use std::marker::PhantomData;

Expand Down Expand Up @@ -119,6 +127,11 @@ define_maps! { <'tcx>
/// additional acyclicity requirements).
pub super_predicates: ItemSignature(DefId) -> ty::GenericPredicates<'tcx>,

/// To avoid cycles within the predicates of a single item we compute
/// per-type-parameter predicates for resolving `T::AssocTy`.
pub type_param_predicates: ItemSignature(DefId)
-> ty::GenericPredicates<'tcx>,

pub trait_def: ItemSignature(DefId) -> &'tcx ty::TraitDef,
pub adt_def: ItemSignature(DefId) -> &'tcx ty::AdtDef,
pub adt_sized_constraint: SizedConstraint(DefId) -> Ty<'tcx>,
Expand Down
9 changes: 4 additions & 5 deletions src/librustc/ty/mod.rs
Expand Up @@ -31,7 +31,7 @@ use ty::subst::{Subst, Substs};
use ty::util::IntTypeExt;
use ty::walk::TypeWalker;
use util::common::MemoizationMap;
use util::nodemap::{NodeSet, NodeMap, FxHashMap};
use util::nodemap::{NodeSet, FxHashMap};

use serialize::{self, Encodable, Encoder};
use std::borrow::Cow;
Expand Down Expand Up @@ -104,13 +104,12 @@ mod sty;
/// The complete set of all analyses described in this module. This is
/// produced by the driver and fed to trans and later passes.
#[derive(Clone)]
pub struct CrateAnalysis<'tcx> {
pub struct CrateAnalysis {
pub export_map: ExportMap,
pub access_levels: middle::privacy::AccessLevels,
pub reachable: NodeSet,
pub name: String,
pub glob_map: Option<hir::GlobMap>,
pub hir_ty_to_ty: NodeMap<Ty<'tcx>>,
}

#[derive(Clone)]
Expand Down Expand Up @@ -1383,7 +1382,7 @@ pub struct ReprOptions {
}

impl ReprOptions {
pub fn new<'a, 'gcx, 'tcx>(tcx: &TyCtxt<'a, 'gcx, 'tcx>, did: DefId) -> ReprOptions {
pub fn new(tcx: TyCtxt, did: DefId) -> ReprOptions {
let mut ret = ReprOptions::default();
let attrs = tcx.lookup_repr_hints(did);
for r in attrs.iter() {
Expand All @@ -1400,7 +1399,7 @@ impl ReprOptions {
}

impl<'a, 'gcx, 'tcx> AdtDef {
fn new(tcx: TyCtxt<'a, 'gcx, 'tcx>,
fn new(tcx: TyCtxt,
did: DefId,
kind: AdtKind,
variants: Vec<VariantDef>,
Expand Down
18 changes: 8 additions & 10 deletions src/librustc_driver/driver.rs
Expand Up @@ -21,7 +21,7 @@ use rustc::middle::{self, dependency_format, stability, reachable};
use rustc::middle::privacy::AccessLevels;
use rustc::ty::{self, TyCtxt, Resolutions, GlobalArenas};
use rustc::util::common::time;
use rustc::util::nodemap::{NodeSet, NodeMap};
use rustc::util::nodemap::NodeSet;
use rustc::util::fs::rename_or_copy_remove;
use rustc_borrowck as borrowck;
use rustc_incremental::{self, IncrementalHashesMap};
Expand Down Expand Up @@ -343,7 +343,7 @@ 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<'tcx>>,
pub analysis: Option<&'a ty::CrateAnalysis>,
pub tcx: Option<TyCtxt<'a, 'tcx, 'tcx>>,
pub trans: Option<&'a trans::CrateTranslation>,
}
Expand Down Expand Up @@ -417,7 +417,7 @@ impl<'a, 'tcx> CompileState<'a, 'tcx> {
arenas: &'tcx GlobalArenas<'tcx>,
cstore: &'a CStore,
hir_map: &'a hir_map::Map<'tcx>,
analysis: &'a ty::CrateAnalysis<'static>,
analysis: &'a ty::CrateAnalysis,
resolutions: &'a Resolutions,
krate: &'a ast::Crate,
hir_crate: &'a hir::Crate,
Expand All @@ -444,7 +444,7 @@ 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>,
analysis: &'a ty::CrateAnalysis,
tcx: TyCtxt<'a, 'tcx, 'tcx>,
crate_name: &'a str)
-> Self {
Expand Down Expand Up @@ -534,7 +534,7 @@ fn count_nodes(krate: &ast::Crate) -> usize {
pub struct ExpansionResult {
pub expanded_crate: ast::Crate,
pub defs: hir_map::Definitions,
pub analysis: ty::CrateAnalysis<'static>,
pub analysis: ty::CrateAnalysis,
pub resolutions: Resolutions,
pub hir_forest: hir_map::Forest,
}
Expand Down Expand Up @@ -797,7 +797,6 @@ pub fn phase_2_configure_and_expand<F>(sess: &Session,
reachable: NodeSet(),
name: crate_name.to_string(),
glob_map: if resolver.make_glob_map { Some(resolver.glob_map) } else { None },
hir_ty_to_ty: NodeMap(),
},
resolutions: Resolutions {
freevars: resolver.freevars,
Expand All @@ -813,15 +812,15 @@ pub fn phase_2_configure_and_expand<F>(sess: &Session,
/// structures carrying the results of the analysis.
pub fn phase_3_run_analysis_passes<'tcx, F, R>(sess: &'tcx Session,
hir_map: hir_map::Map<'tcx>,
mut analysis: ty::CrateAnalysis<'tcx>,
mut analysis: ty::CrateAnalysis,
resolutions: Resolutions,
arena: &'tcx DroplessArena,
arenas: &'tcx GlobalArenas<'tcx>,
name: &str,
f: F)
-> Result<R, usize>
where F: for<'a> FnOnce(TyCtxt<'a, 'tcx, 'tcx>,
ty::CrateAnalysis<'tcx>,
ty::CrateAnalysis,
IncrementalHashesMap,
CompileResult) -> R
{
Expand Down Expand Up @@ -908,8 +907,7 @@ pub fn phase_3_run_analysis_passes<'tcx, F, R>(sess: &'tcx Session,
|| stability::check_unstable_api_usage(tcx));

// passes are timed inside typeck
analysis.hir_ty_to_ty =
try_with_f!(typeck::check_crate(tcx), (tcx, analysis, incremental_hashes_map));
try_with_f!(typeck::check_crate(tcx), (tcx, analysis, incremental_hashes_map));

time(time_passes,
"const checking",
Expand Down
6 changes: 3 additions & 3 deletions src/librustc_driver/pretty.rs
Expand Up @@ -201,7 +201,7 @@ impl PpSourceMode {
fn call_with_pp_support_hir<'tcx, A, B, F>(&self,
sess: &'tcx Session,
hir_map: &hir_map::Map<'tcx>,
analysis: &ty::CrateAnalysis<'tcx>,
analysis: &ty::CrateAnalysis,
resolutions: &Resolutions,
arena: &'tcx DroplessArena,
arenas: &'tcx GlobalArenas<'tcx>,
Expand Down Expand Up @@ -838,7 +838,7 @@ pub fn print_after_parsing(sess: &Session,

pub fn print_after_hir_lowering<'tcx, 'a: 'tcx>(sess: &'a Session,
hir_map: &hir_map::Map<'tcx>,
analysis: &ty::CrateAnalysis<'tcx>,
analysis: &ty::CrateAnalysis,
resolutions: &Resolutions,
input: &Input,
krate: &ast::Crate,
Expand Down Expand Up @@ -958,7 +958,7 @@ pub fn print_after_hir_lowering<'tcx, 'a: 'tcx>(sess: &'a Session,
// Instead, we call that function ourselves.
fn print_with_analysis<'tcx, 'a: 'tcx>(sess: &'a Session,
hir_map: &hir_map::Map<'tcx>,
analysis: &ty::CrateAnalysis<'tcx>,
analysis: &ty::CrateAnalysis,
resolutions: &Resolutions,
crate_name: &str,
arena: &'tcx DroplessArena,
Expand Down
6 changes: 3 additions & 3 deletions src/librustc_save_analysis/lib.rs
Expand Up @@ -85,7 +85,7 @@ pub mod recorder {
pub struct SaveContext<'l, 'tcx: 'l> {
tcx: TyCtxt<'l, 'tcx, 'tcx>,
tables: &'l ty::TypeckTables<'tcx>,
analysis: &'l ty::CrateAnalysis<'tcx>,
analysis: &'l ty::CrateAnalysis,
span_utils: SpanUtils<'tcx>,
}

Expand Down Expand Up @@ -550,7 +550,7 @@ impl<'l, 'tcx: 'l> SaveContext<'l, 'tcx> {
match *qpath {
hir::QPath::Resolved(_, ref path) => path.def,
hir::QPath::TypeRelative(..) => {
if let Some(ty) = self.analysis.hir_ty_to_ty.get(&id) {
if let Some(ty) = self.tcx.ast_ty_to_ty_cache.borrow().get(&id) {
if let ty::TyProjection(proj) = ty.sty {
for item in self.tcx.associated_items(proj.trait_ref.def_id) {
if item.kind == ty::AssociatedKind::Type {
Expand Down Expand Up @@ -854,7 +854,7 @@ impl Format {

pub fn process_crate<'l, 'tcx>(tcx: TyCtxt<'l, 'tcx, 'tcx>,
krate: &ast::Crate,
analysis: &'l ty::CrateAnalysis<'tcx>,
analysis: &'l ty::CrateAnalysis,
cratename: &str,
odir: Option<&Path>,
format: Format) {
Expand Down
3 changes: 2 additions & 1 deletion src/librustc_typeck/astconv.rs
Expand Up @@ -897,7 +897,8 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
// FIXME: Self type is not always computed when we are here because type parameter
// bounds may affect Self type and have to be converted before it.
let trait_ref = if impl_def_id.is_local() {
tcx.impl_trait_refs.borrow().get(&impl_def_id).cloned().and_then(|x| x)
tcx.maps.impl_trait_ref.borrow().get(&impl_def_id)
.cloned().and_then(|x| x)
} else {
tcx.impl_trait_ref(impl_def_id)
};
Expand Down
12 changes: 4 additions & 8 deletions src/librustc_typeck/check/callee.rs
Expand Up @@ -10,11 +10,10 @@

use super::{DeferredCallResolution, Expectation, FnCtxt, TupleArgumentsFlag};

use CrateCtxt;
use hir::def::Def;
use hir::def_id::{DefId, LOCAL_CRATE};
use rustc::{infer, traits};
use rustc::ty::{self, LvaluePreference, Ty};
use rustc::ty::{self, TyCtxt, LvaluePreference, Ty};
use syntax::symbol::Symbol;
use syntax_pos::Span;

Expand All @@ -23,12 +22,9 @@ use rustc::hir;
/// Check that it is legal to call methods of the trait corresponding
/// to `trait_id` (this only cares about the trait, not the specific
/// method that is called)
pub fn check_legal_trait_for_method_call(ccx: &CrateCtxt, span: Span, trait_id: DefId) {
if ccx.tcx.lang_items.drop_trait() == Some(trait_id) {
struct_span_err!(ccx.tcx.sess,
span,
E0040,
"explicit use of destructor method")
pub fn check_legal_trait_for_method_call(tcx: TyCtxt, span: Span, trait_id: DefId) {
if tcx.lang_items.drop_trait() == Some(trait_id) {
struct_span_err!(tcx.sess, span, E0040, "explicit use of destructor method")
.span_label(span, &format!("explicit destructor calls not allowed"))
.emit();
}
Expand Down

0 comments on commit 4649f73

Please sign in to comment.