Skip to content

Commit 1f874de

Browse files
committed
rustc: do not depend on infcx.tables in MemCategorizationContext.
1 parent 5175bc1 commit 1f874de

File tree

13 files changed

+146
-240
lines changed

13 files changed

+146
-240
lines changed

src/librustc/infer/mod.rs

Lines changed: 15 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,8 @@ pub use self::freshen::TypeFreshener;
1919
pub use self::region_inference::{GenericKind, VerifyBound};
2020

2121
use hir::def_id::DefId;
22-
use hir;
2322
use middle::free_region::{FreeRegionMap, RegionRelations};
2423
use middle::region::RegionMaps;
25-
use middle::mem_categorization as mc;
26-
use middle::mem_categorization::McResult;
2724
use middle::lang_items;
2825
use mir::tcx::LvalueTy;
2926
use ty::subst::{Kind, Subst, Substs};
@@ -36,7 +33,6 @@ use traits::{self, ObligationCause, PredicateObligations, Reveal};
3633
use rustc_data_structures::unify::{self, UnificationTable};
3734
use std::cell::{Cell, RefCell, Ref, RefMut};
3835
use std::fmt;
39-
use std::ops::Deref;
4036
use syntax::ast;
4137
use errors::DiagnosticBuilder;
4238
use syntax_pos::{self, Span, DUMMY_SP};
@@ -80,55 +76,23 @@ pub type FixupResult<T> = Result<T, FixupError>; // "fixup result"
8076
/// `InProgress` (during typeck) or `Interned` (result of typeck).
8177
/// Only the `InProgress` version supports `borrow_mut`.
8278
#[derive(Copy, Clone)]
83-
pub enum InferTables<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
84-
Interned(&'a ty::TypeckTables<'gcx>),
79+
pub enum InferTables<'a, 'tcx: 'a> {
8580
InProgress(&'a RefCell<ty::TypeckTables<'tcx>>),
8681
Missing
8782
}
8883

89-
pub enum InferTablesRef<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
90-
Interned(&'a ty::TypeckTables<'gcx>),
91-
InProgress(Ref<'a, ty::TypeckTables<'tcx>>)
92-
}
93-
94-
impl<'a, 'gcx, 'tcx> Deref for InferTablesRef<'a, 'gcx, 'tcx> {
95-
type Target = ty::TypeckTables<'tcx>;
96-
fn deref(&self) -> &Self::Target {
97-
match *self {
98-
InferTablesRef::Interned(tables) => tables,
99-
InferTablesRef::InProgress(ref tables) => tables
100-
}
101-
}
102-
}
103-
104-
impl<'a, 'gcx, 'tcx> InferTables<'a, 'gcx, 'tcx> {
105-
pub fn borrow(self) -> InferTablesRef<'a, 'gcx, 'tcx> {
84+
impl<'a, 'tcx> InferTables<'a, 'tcx> {
85+
pub fn borrow(self) -> Ref<'a, ty::TypeckTables<'tcx>> {
10686
match self {
107-
InferTables::Interned(tables) => InferTablesRef::Interned(tables),
108-
InferTables::InProgress(tables) => InferTablesRef::InProgress(tables.borrow()),
87+
InferTables::InProgress(tables) => tables.borrow(),
10988
InferTables::Missing => {
11089
bug!("InferTables: infcx.tables.borrow() with no tables")
11190
}
11291
}
11392
}
11493

115-
pub fn expect_interned(self) -> &'a ty::TypeckTables<'gcx> {
116-
match self {
117-
InferTables::Interned(tables) => tables,
118-
InferTables::InProgress(_) => {
119-
bug!("InferTables: infcx.tables.expect_interned() during type-checking");
120-
}
121-
InferTables::Missing => {
122-
bug!("InferTables: infcx.tables.expect_interned() with no tables")
123-
}
124-
}
125-
}
126-
12794
pub fn borrow_mut(self) -> RefMut<'a, ty::TypeckTables<'tcx>> {
12895
match self {
129-
InferTables::Interned(_) => {
130-
bug!("InferTables: infcx.tables.borrow_mut() outside of type-checking");
131-
}
13296
InferTables::InProgress(tables) => tables.borrow_mut(),
13397
InferTables::Missing => {
13498
bug!("InferTables: infcx.tables.borrow_mut() with no tables")
@@ -140,7 +104,7 @@ impl<'a, 'gcx, 'tcx> InferTables<'a, 'gcx, 'tcx> {
140104
pub struct InferCtxt<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
141105
pub tcx: TyCtxt<'a, 'gcx, 'tcx>,
142106

143-
pub tables: InferTables<'a, 'gcx, 'tcx>,
107+
pub tables: InferTables<'a, 'tcx>,
144108

145109
// Cache for projections. This cache is snapshotted along with the
146110
// infcx.
@@ -397,41 +361,18 @@ impl fmt::Display for FixupError {
397361
}
398362

399363
pub trait InferEnv<'a, 'tcx> {
400-
fn to_parts(self, tcx: TyCtxt<'a, 'tcx, 'tcx>)
401-
-> (Option<&'a ty::TypeckTables<'tcx>>,
402-
Option<ty::TypeckTables<'tcx>>);
364+
fn fresh_tables(self) -> Option<ty::TypeckTables<'tcx>>;
403365
}
404366

405367
impl<'a, 'tcx> InferEnv<'a, 'tcx> for () {
406-
fn to_parts(self, _: TyCtxt<'a, 'tcx, 'tcx>)
407-
-> (Option<&'a ty::TypeckTables<'tcx>>,
408-
Option<ty::TypeckTables<'tcx>>) {
409-
(None, None)
410-
}
411-
}
412-
413-
impl<'a, 'tcx> InferEnv<'a, 'tcx> for &'a ty::TypeckTables<'tcx> {
414-
fn to_parts(self, _: TyCtxt<'a, 'tcx, 'tcx>)
415-
-> (Option<&'a ty::TypeckTables<'tcx>>,
416-
Option<ty::TypeckTables<'tcx>>) {
417-
(Some(self), None)
368+
fn fresh_tables(self) -> Option<ty::TypeckTables<'tcx>> {
369+
None
418370
}
419371
}
420372

421373
impl<'a, 'tcx> InferEnv<'a, 'tcx> for ty::TypeckTables<'tcx> {
422-
fn to_parts(self, _: TyCtxt<'a, 'tcx, 'tcx>)
423-
-> (Option<&'a ty::TypeckTables<'tcx>>,
424-
Option<ty::TypeckTables<'tcx>>) {
425-
(None, Some(self))
426-
}
427-
}
428-
429-
impl<'a, 'tcx> InferEnv<'a, 'tcx> for hir::BodyId {
430-
fn to_parts(self, tcx: TyCtxt<'a, 'tcx, 'tcx>)
431-
-> (Option<&'a ty::TypeckTables<'tcx>>,
432-
Option<ty::TypeckTables<'tcx>>) {
433-
let def_id = tcx.hir.body_owner_def_id(self);
434-
(Some(tcx.typeck_tables_of(def_id)), None)
374+
fn fresh_tables(self) -> Option<ty::TypeckTables<'tcx>> {
375+
Some(self)
435376
}
436377
}
437378

@@ -442,29 +383,24 @@ pub struct InferCtxtBuilder<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
442383
global_tcx: TyCtxt<'a, 'gcx, 'gcx>,
443384
arena: DroplessArena,
444385
fresh_tables: Option<RefCell<ty::TypeckTables<'tcx>>>,
445-
tables: Option<&'a ty::TypeckTables<'gcx>>,
446386
}
447387

448388
impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'gcx> {
449389
pub fn infer_ctxt<E: InferEnv<'a, 'gcx>>(self, env: E) -> InferCtxtBuilder<'a, 'gcx, 'tcx> {
450-
let (tables, fresh_tables) = env.to_parts(self);
451390
InferCtxtBuilder {
452391
global_tcx: self,
453392
arena: DroplessArena::new(),
454-
fresh_tables: fresh_tables.map(RefCell::new),
455-
tables: tables,
393+
fresh_tables: env.fresh_tables().map(RefCell::new),
456394
}
457395
}
458396

459397
/// Fake InferCtxt with the global tcx. Used by pre-MIR borrowck
460398
/// for MemCategorizationContext/ExprUseVisitor.
461399
/// If any inference functionality is used, ICEs will occur.
462-
pub fn borrowck_fake_infer_ctxt(self, body: hir::BodyId)
463-
-> InferCtxt<'a, 'gcx, 'gcx> {
464-
let (tables, _) = body.to_parts(self);
400+
pub fn borrowck_fake_infer_ctxt(self) -> InferCtxt<'a, 'gcx, 'gcx> {
465401
InferCtxt {
466402
tcx: self,
467-
tables: InferTables::Interned(tables.unwrap()),
403+
tables: InferTables::Missing,
468404
type_variables: RefCell::new(type_variable::TypeVariableTable::new()),
469405
int_unification_table: RefCell::new(UnificationTable::new()),
470406
float_unification_table: RefCell::new(UnificationTable::new()),
@@ -488,11 +424,9 @@ impl<'a, 'gcx, 'tcx> InferCtxtBuilder<'a, 'gcx, 'tcx> {
488424
global_tcx,
489425
ref arena,
490426
ref fresh_tables,
491-
tables,
492427
} = *self;
493-
let tables = tables.map(InferTables::Interned).unwrap_or_else(|| {
494-
fresh_tables.as_ref().map_or(InferTables::Missing, InferTables::InProgress)
495-
});
428+
let tables = fresh_tables.as_ref()
429+
.map_or(InferTables::Missing, InferTables::InProgress);
496430
global_tcx.enter_local(arena, |tcx| f(InferCtxt {
497431
tcx: tcx,
498432
tables: tables,
@@ -1190,28 +1124,6 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
11901124
self.tainted_by_errors_flag.set(true)
11911125
}
11921126

1193-
pub fn node_type(&self, id: ast::NodeId) -> Ty<'tcx> {
1194-
match self.tables.borrow().node_types.get(&id) {
1195-
Some(&t) => t,
1196-
// FIXME
1197-
None if self.is_tainted_by_errors() =>
1198-
self.tcx.types.err,
1199-
None => {
1200-
bug!("no type for node {}: {} in fcx",
1201-
id, self.tcx.hir.node_to_string(id));
1202-
}
1203-
}
1204-
}
1205-
1206-
pub fn expr_ty(&self, ex: &hir::Expr) -> Ty<'tcx> {
1207-
match self.tables.borrow().node_types.get(&ex.id) {
1208-
Some(&t) => t,
1209-
None => {
1210-
bug!("no type for expr in fcx");
1211-
}
1212-
}
1213-
}
1214-
12151127
pub fn resolve_regions_and_report_errors(&self,
12161128
region_context: DefId,
12171129
region_map: &RegionMaps,
@@ -1310,21 +1222,6 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
13101222
value.fold_with(&mut r)
13111223
}
13121224

1313-
/// Resolves all type variables in `t` and then, if any were left
1314-
/// unresolved, substitutes an error type. This is used after the
1315-
/// main checking when doing a second pass before writeback. The
1316-
/// justification is that writeback will produce an error for
1317-
/// these unconstrained type variables.
1318-
fn resolve_type_vars_or_error(&self, t: &Ty<'tcx>) -> mc::McResult<Ty<'tcx>> {
1319-
let ty = self.resolve_type_vars_if_possible(t);
1320-
if ty.references_error() || ty.is_ty_var() {
1321-
debug!("resolve_type_vars_or_error: error from {:?}", ty);
1322-
Err(())
1323-
} else {
1324-
Ok(ty)
1325-
}
1326-
}
1327-
13281225
pub fn fully_resolve<T:TypeFoldable<'tcx>>(&self, value: &T) -> FixupResult<T> {
13291226
/*!
13301227
* Attempts to resolve all type/region variables in
@@ -1484,16 +1381,6 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
14841381
self.region_vars.verify_generic_bound(origin, kind, a, bound);
14851382
}
14861383

1487-
pub fn node_ty(&self, id: ast::NodeId) -> McResult<Ty<'tcx>> {
1488-
let ty = self.node_type(id);
1489-
self.resolve_type_vars_or_error(&ty)
1490-
}
1491-
1492-
pub fn expr_ty_adjusted(&self, expr: &hir::Expr) -> McResult<Ty<'tcx>> {
1493-
let ty = self.tables.borrow().expr_ty_adjusted(expr);
1494-
self.resolve_type_vars_or_error(&ty)
1495-
}
1496-
14971384
pub fn type_moves_by_default(&self,
14981385
param_env: ty::ParamEnv<'tcx>,
14991386
ty: Ty<'tcx>,

0 commit comments

Comments
 (0)