Skip to content

Commit 4649f73

Browse files
committed
rustc_typeck: lift CrateCtxt to TyCtxt.
1 parent 374ea14 commit 4649f73

File tree

24 files changed

+651
-757
lines changed

24 files changed

+651
-757
lines changed

src/librustc/ty/context.rs

+17
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,20 @@ pub struct GlobalCtxt<'tcx> {
525525
stability_interner: RefCell<FxHashSet<&'tcx attr::Stability>>,
526526

527527
layout_interner: RefCell<FxHashSet<&'tcx Layout>>,
528+
529+
/// A vector of every trait accessible in the whole crate
530+
/// (i.e. including those from subcrates). This is used only for
531+
/// error reporting, and so is lazily initialised and generally
532+
/// shouldn't taint the common path (hence the RefCell).
533+
pub all_traits: RefCell<Option<Vec<DefId>>>,
534+
535+
/// Obligations which will have to be checked at the end of
536+
/// type-checking, after all functions have been inferred.
537+
/// The key is the NodeId of the item the obligations were from.
538+
pub deferred_obligations: RefCell<NodeMap<Vec<traits::DeferredObligation<'tcx>>>>,
539+
540+
/// HIR Ty -> Ty lowering cache.
541+
pub ast_ty_to_ty_cache: RefCell<NodeMap<Ty<'tcx>>>,
528542
}
529543

530544
impl<'tcx> GlobalCtxt<'tcx> {
@@ -720,6 +734,9 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
720734
layout_depth: Cell::new(0),
721735
derive_macros: RefCell::new(NodeMap()),
722736
stability_interner: RefCell::new(FxHashSet()),
737+
all_traits: RefCell::new(None),
738+
deferred_obligations: RefCell::new(NodeMap()),
739+
ast_ty_to_ty_cache: RefCell::new(NodeMap()),
723740
}, f)
724741
}
725742
}

src/librustc/ty/maps.rs

+13
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ macro_rules! define_maps {
3737
pub $name:ident: $node:ident($K:ty) -> $V:ty),*) => {
3838
pub struct Maps<$tcx> {
3939
providers: IndexVec<CrateNum, Providers<$tcx>>,
40+
pub query_stack: RefCell<Vec<Query>>,
4041
$($(#[$attr])* pub $name: RefCell<DepTrackingMap<queries::$name<$tcx>>>),*
4142
}
4243

@@ -46,11 +47,18 @@ macro_rules! define_maps {
4647
-> Self {
4748
Maps {
4849
providers,
50+
query_stack: RefCell::new(vec![]),
4951
$($name: RefCell::new(DepTrackingMap::new(dep_graph.clone()))),*
5052
}
5153
}
5254
}
5355

56+
#[allow(bad_style)]
57+
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
58+
pub enum Query {
59+
$($(#[$attr])* $name($K)),*
60+
}
61+
5462
pub mod queries {
5563
use std::marker::PhantomData;
5664

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

130+
/// To avoid cycles within the predicates of a single item we compute
131+
/// per-type-parameter predicates for resolving `T::AssocTy`.
132+
pub type_param_predicates: ItemSignature(DefId)
133+
-> ty::GenericPredicates<'tcx>,
134+
122135
pub trait_def: ItemSignature(DefId) -> &'tcx ty::TraitDef,
123136
pub adt_def: ItemSignature(DefId) -> &'tcx ty::AdtDef,
124137
pub adt_sized_constraint: SizedConstraint(DefId) -> Ty<'tcx>,

src/librustc/ty/mod.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use ty::subst::{Subst, Substs};
3131
use ty::util::IntTypeExt;
3232
use ty::walk::TypeWalker;
3333
use util::common::MemoizationMap;
34-
use util::nodemap::{NodeSet, NodeMap, FxHashMap};
34+
use util::nodemap::{NodeSet, FxHashMap};
3535

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

116115
#[derive(Clone)]
@@ -1383,7 +1382,7 @@ pub struct ReprOptions {
13831382
}
13841383

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

14021401
impl<'a, 'gcx, 'tcx> AdtDef {
1403-
fn new(tcx: TyCtxt<'a, 'gcx, 'tcx>,
1402+
fn new(tcx: TyCtxt,
14041403
did: DefId,
14051404
kind: AdtKind,
14061405
variants: Vec<VariantDef>,

src/librustc_driver/driver.rs

+8-10
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use rustc::middle::{self, dependency_format, stability, reachable};
2121
use rustc::middle::privacy::AccessLevels;
2222
use rustc::ty::{self, TyCtxt, Resolutions, GlobalArenas};
2323
use rustc::util::common::time;
24-
use rustc::util::nodemap::{NodeSet, NodeMap};
24+
use rustc::util::nodemap::NodeSet;
2525
use rustc::util::fs::rename_or_copy_remove;
2626
use rustc_borrowck as borrowck;
2727
use rustc_incremental::{self, IncrementalHashesMap};
@@ -343,7 +343,7 @@ pub struct CompileState<'a, 'tcx: 'a> {
343343
pub hir_crate: Option<&'a hir::Crate>,
344344
pub hir_map: Option<&'a hir_map::Map<'tcx>>,
345345
pub resolutions: Option<&'a Resolutions>,
346-
pub analysis: Option<&'a ty::CrateAnalysis<'tcx>>,
346+
pub analysis: Option<&'a ty::CrateAnalysis>,
347347
pub tcx: Option<TyCtxt<'a, 'tcx, 'tcx>>,
348348
pub trans: Option<&'a trans::CrateTranslation>,
349349
}
@@ -417,7 +417,7 @@ impl<'a, 'tcx> CompileState<'a, 'tcx> {
417417
arenas: &'tcx GlobalArenas<'tcx>,
418418
cstore: &'a CStore,
419419
hir_map: &'a hir_map::Map<'tcx>,
420-
analysis: &'a ty::CrateAnalysis<'static>,
420+
analysis: &'a ty::CrateAnalysis,
421421
resolutions: &'a Resolutions,
422422
krate: &'a ast::Crate,
423423
hir_crate: &'a hir::Crate,
@@ -444,7 +444,7 @@ impl<'a, 'tcx> CompileState<'a, 'tcx> {
444444
out_file: &'a Option<PathBuf>,
445445
krate: Option<&'a ast::Crate>,
446446
hir_crate: &'a hir::Crate,
447-
analysis: &'a ty::CrateAnalysis<'tcx>,
447+
analysis: &'a ty::CrateAnalysis,
448448
tcx: TyCtxt<'a, 'tcx, 'tcx>,
449449
crate_name: &'a str)
450450
-> Self {
@@ -534,7 +534,7 @@ fn count_nodes(krate: &ast::Crate) -> usize {
534534
pub struct ExpansionResult {
535535
pub expanded_crate: ast::Crate,
536536
pub defs: hir_map::Definitions,
537-
pub analysis: ty::CrateAnalysis<'static>,
537+
pub analysis: ty::CrateAnalysis,
538538
pub resolutions: Resolutions,
539539
pub hir_forest: hir_map::Forest,
540540
}
@@ -797,7 +797,6 @@ pub fn phase_2_configure_and_expand<F>(sess: &Session,
797797
reachable: NodeSet(),
798798
name: crate_name.to_string(),
799799
glob_map: if resolver.make_glob_map { Some(resolver.glob_map) } else { None },
800-
hir_ty_to_ty: NodeMap(),
801800
},
802801
resolutions: Resolutions {
803802
freevars: resolver.freevars,
@@ -813,15 +812,15 @@ pub fn phase_2_configure_and_expand<F>(sess: &Session,
813812
/// structures carrying the results of the analysis.
814813
pub fn phase_3_run_analysis_passes<'tcx, F, R>(sess: &'tcx Session,
815814
hir_map: hir_map::Map<'tcx>,
816-
mut analysis: ty::CrateAnalysis<'tcx>,
815+
mut analysis: ty::CrateAnalysis,
817816
resolutions: Resolutions,
818817
arena: &'tcx DroplessArena,
819818
arenas: &'tcx GlobalArenas<'tcx>,
820819
name: &str,
821820
f: F)
822821
-> Result<R, usize>
823822
where F: for<'a> FnOnce(TyCtxt<'a, 'tcx, 'tcx>,
824-
ty::CrateAnalysis<'tcx>,
823+
ty::CrateAnalysis,
825824
IncrementalHashesMap,
826825
CompileResult) -> R
827826
{
@@ -908,8 +907,7 @@ pub fn phase_3_run_analysis_passes<'tcx, F, R>(sess: &'tcx Session,
908907
|| stability::check_unstable_api_usage(tcx));
909908

910909
// passes are timed inside typeck
911-
analysis.hir_ty_to_ty =
912-
try_with_f!(typeck::check_crate(tcx), (tcx, analysis, incremental_hashes_map));
910+
try_with_f!(typeck::check_crate(tcx), (tcx, analysis, incremental_hashes_map));
913911

914912
time(time_passes,
915913
"const checking",

src/librustc_driver/pretty.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ impl PpSourceMode {
201201
fn call_with_pp_support_hir<'tcx, A, B, F>(&self,
202202
sess: &'tcx Session,
203203
hir_map: &hir_map::Map<'tcx>,
204-
analysis: &ty::CrateAnalysis<'tcx>,
204+
analysis: &ty::CrateAnalysis,
205205
resolutions: &Resolutions,
206206
arena: &'tcx DroplessArena,
207207
arenas: &'tcx GlobalArenas<'tcx>,
@@ -838,7 +838,7 @@ pub fn print_after_parsing(sess: &Session,
838838

839839
pub fn print_after_hir_lowering<'tcx, 'a: 'tcx>(sess: &'a Session,
840840
hir_map: &hir_map::Map<'tcx>,
841-
analysis: &ty::CrateAnalysis<'tcx>,
841+
analysis: &ty::CrateAnalysis,
842842
resolutions: &Resolutions,
843843
input: &Input,
844844
krate: &ast::Crate,
@@ -958,7 +958,7 @@ pub fn print_after_hir_lowering<'tcx, 'a: 'tcx>(sess: &'a Session,
958958
// Instead, we call that function ourselves.
959959
fn print_with_analysis<'tcx, 'a: 'tcx>(sess: &'a Session,
960960
hir_map: &hir_map::Map<'tcx>,
961-
analysis: &ty::CrateAnalysis<'tcx>,
961+
analysis: &ty::CrateAnalysis,
962962
resolutions: &Resolutions,
963963
crate_name: &str,
964964
arena: &'tcx DroplessArena,

src/librustc_save_analysis/lib.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ pub mod recorder {
8585
pub struct SaveContext<'l, 'tcx: 'l> {
8686
tcx: TyCtxt<'l, 'tcx, 'tcx>,
8787
tables: &'l ty::TypeckTables<'tcx>,
88-
analysis: &'l ty::CrateAnalysis<'tcx>,
88+
analysis: &'l ty::CrateAnalysis,
8989
span_utils: SpanUtils<'tcx>,
9090
}
9191

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

855855
pub fn process_crate<'l, 'tcx>(tcx: TyCtxt<'l, 'tcx, 'tcx>,
856856
krate: &ast::Crate,
857-
analysis: &'l ty::CrateAnalysis<'tcx>,
857+
analysis: &'l ty::CrateAnalysis,
858858
cratename: &str,
859859
odir: Option<&Path>,
860860
format: Format) {

src/librustc_typeck/astconv.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -897,7 +897,8 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
897897
// FIXME: Self type is not always computed when we are here because type parameter
898898
// bounds may affect Self type and have to be converted before it.
899899
let trait_ref = if impl_def_id.is_local() {
900-
tcx.impl_trait_refs.borrow().get(&impl_def_id).cloned().and_then(|x| x)
900+
tcx.maps.impl_trait_ref.borrow().get(&impl_def_id)
901+
.cloned().and_then(|x| x)
901902
} else {
902903
tcx.impl_trait_ref(impl_def_id)
903904
};

src/librustc_typeck/check/callee.rs

+4-8
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,10 @@
1010

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

13-
use CrateCtxt;
1413
use hir::def::Def;
1514
use hir::def_id::{DefId, LOCAL_CRATE};
1615
use rustc::{infer, traits};
17-
use rustc::ty::{self, LvaluePreference, Ty};
16+
use rustc::ty::{self, TyCtxt, LvaluePreference, Ty};
1817
use syntax::symbol::Symbol;
1918
use syntax_pos::Span;
2019

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

0 commit comments

Comments
 (0)