Skip to content

Commit 0f611b6

Browse files
committed
rustc: make all read access to tcx.tables go through a method.
1 parent a5b6a9f commit 0f611b6

File tree

32 files changed

+218
-225
lines changed

32 files changed

+218
-225
lines changed

src/librustc/cfg/construct.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -311,11 +311,11 @@ impl<'a, 'tcx> CFGBuilder<'a, 'tcx> {
311311
}
312312

313313
hir::ExprIndex(ref l, ref r) |
314-
hir::ExprBinary(_, ref l, ref r) if self.tcx.is_method_call(expr.id) => {
314+
hir::ExprBinary(_, ref l, ref r) if self.tcx.tables().is_method_call(expr.id) => {
315315
self.call(expr, pred, &l, Some(&**r).into_iter())
316316
}
317317

318-
hir::ExprUnary(_, ref e) if self.tcx.is_method_call(expr.id) => {
318+
hir::ExprUnary(_, ref e) if self.tcx.tables().is_method_call(expr.id) => {
319319
self.call(expr, pred, &e, None::<hir::Expr>.iter())
320320
}
321321

@@ -372,7 +372,7 @@ impl<'a, 'tcx> CFGBuilder<'a, 'tcx> {
372372
func_or_rcvr: &hir::Expr,
373373
args: I) -> CFGIndex {
374374
let method_call = ty::MethodCall::expr(call_expr.id);
375-
let fn_ty = match self.tcx.tables.borrow().method_map.get(&method_call) {
375+
let fn_ty = match self.tcx.tables().method_map.get(&method_call) {
376376
Some(method) => method.ty,
377377
None => self.tcx.expr_ty_adjusted(func_or_rcvr)
378378
};

src/librustc/middle/dead.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ impl<'a, 'tcx> MarkSymbolVisitor<'a, 'tcx> {
9292
match def {
9393
Def::AssociatedTy(..) | Def::Method(_) | Def::AssociatedConst(_)
9494
if self.tcx.trait_of_item(def.def_id()).is_some() => {
95-
if let Some(substs) = self.tcx.tables.borrow().item_substs.get(&id) {
95+
if let Some(substs) = self.tcx.tables().item_substs.get(&id) {
9696
if let ty::TyAdt(tyid, _) = substs.substs.type_at(0).sty {
9797
self.check_def_id(tyid.did);
9898
}
@@ -123,7 +123,7 @@ impl<'a, 'tcx> MarkSymbolVisitor<'a, 'tcx> {
123123

124124
fn lookup_and_handle_method(&mut self, id: ast::NodeId) {
125125
let method_call = ty::MethodCall::expr(id);
126-
let method = self.tcx.tables.borrow().method_map[&method_call];
126+
let method = self.tcx.tables().method_map[&method_call];
127127
self.check_def_id(method.def_id);
128128
}
129129

@@ -148,7 +148,7 @@ impl<'a, 'tcx> MarkSymbolVisitor<'a, 'tcx> {
148148

149149
fn handle_field_pattern_match(&mut self, lhs: &hir::Pat,
150150
pats: &[codemap::Spanned<hir::FieldPat>]) {
151-
let variant = match self.tcx.node_id_to_type(lhs.id).sty {
151+
let variant = match self.tcx.tables().node_id_to_type(lhs.id).sty {
152152
ty::TyAdt(adt, _) => {
153153
adt.variant_of_def(self.tcx.expect_def(lhs.id))
154154
}
@@ -433,7 +433,7 @@ impl<'a, 'tcx> DeadVisitor<'a, 'tcx> {
433433
}
434434

435435
fn should_warn_about_field(&mut self, field: &hir::StructField) -> bool {
436-
let field_type = self.tcx.node_id_to_type(field.id);
436+
let field_type = self.tcx.tables().node_id_to_type(field.id);
437437
let is_marker_field = match field_type.ty_to_def_id() {
438438
Some(def_id) => self.tcx.lang_items.items().iter().any(|item| *item == Some(def_id)),
439439
_ => false

src/librustc/middle/effect.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for EffectCheckVisitor<'a, 'tcx> {
159159
match expr.node {
160160
hir::ExprMethodCall(..) => {
161161
let method_call = MethodCall::expr(expr.id);
162-
let base_type = self.tcx.tables.borrow().method_map[&method_call].ty;
162+
let base_type = self.tcx.tables().method_map[&method_call].ty;
163163
debug!("effect: method call case, base type is {:?}",
164164
base_type);
165165
if type_is_unsafe_function(base_type) {
@@ -214,7 +214,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for EffectCheckVisitor<'a, 'tcx> {
214214

215215
fn visit_pat(&mut self, pat: &hir::Pat) {
216216
if let PatKind::Struct(_, ref fields, _) = pat.node {
217-
if let ty::TyAdt(adt, ..) = self.tcx.pat_ty(pat).sty {
217+
if let ty::TyAdt(adt, ..) = self.tcx.tables().pat_ty(pat).sty {
218218
if adt.is_union() {
219219
for field in fields {
220220
self.require_unsafe(field.span, "matching on union field");

src/librustc/middle/intrinsicck.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ impl<'a, 'gcx, 'tcx, 'v> Visitor<'v> for ExprVisitor<'a, 'gcx, 'tcx> {
163163
if let hir::ExprPath(..) = expr.node {
164164
match self.infcx.tcx.expect_def(expr.id) {
165165
Def::Fn(did) if self.def_id_is_transmute(did) => {
166-
let typ = self.infcx.tcx.node_id_to_type(expr.id);
166+
let typ = self.infcx.tcx.tables().node_id_to_type(expr.id);
167167
match typ.sty {
168168
ty::TyFnDef(.., ref bare_fn_ty) if bare_fn_ty.abi == RustIntrinsic => {
169169
let from = bare_fn_ty.sig.0.inputs[0];

src/librustc/middle/liveness.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -1081,7 +1081,7 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
10811081

10821082
hir::ExprAssignOp(_, ref l, ref r) => {
10831083
// an overloaded assign op is like a method call
1084-
if self.ir.tcx.is_method_call(expr.id) {
1084+
if self.ir.tcx.tables().is_method_call(expr.id) {
10851085
let succ = self.propagate_through_expr(&l, succ);
10861086
self.propagate_through_expr(&r, succ)
10871087
} else {
@@ -1113,7 +1113,7 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
11131113

11141114
hir::ExprCall(ref f, ref args) => {
11151115
// FIXME(canndrew): This is_never should really be an is_uninhabited
1116-
let diverges = !self.ir.tcx.is_method_call(expr.id) &&
1116+
let diverges = !self.ir.tcx.tables().is_method_call(expr.id) &&
11171117
self.ir.tcx.expr_ty_adjusted(&f).fn_ret().0.is_never();
11181118
let succ = if diverges {
11191119
self.s.exit_ln
@@ -1126,7 +1126,7 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
11261126

11271127
hir::ExprMethodCall(.., ref args) => {
11281128
let method_call = ty::MethodCall::expr(expr.id);
1129-
let method_ty = self.ir.tcx.tables.borrow().method_map[&method_call].ty;
1129+
let method_ty = self.ir.tcx.tables().method_map[&method_call].ty;
11301130
// FIXME(canndrew): This is_never should really be an is_uninhabited
11311131
let succ = if method_ty.fn_ret().0.is_never() {
11321132
self.s.exit_ln
@@ -1409,7 +1409,7 @@ fn check_expr(this: &mut Liveness, expr: &Expr) {
14091409
}
14101410

14111411
hir::ExprAssignOp(_, ref l, _) => {
1412-
if !this.ir.tcx.is_method_call(expr.id) {
1412+
if !this.ir.tcx.tables().is_method_call(expr.id) {
14131413
this.check_lvalue(&l);
14141414
}
14151415

@@ -1459,7 +1459,7 @@ fn check_fn(_v: &Liveness,
14591459

14601460
impl<'a, 'tcx> Liveness<'a, 'tcx> {
14611461
fn fn_ret(&self, id: NodeId) -> ty::Binder<Ty<'tcx>> {
1462-
let fn_ty = self.ir.tcx.node_id_to_type(id);
1462+
let fn_ty = self.ir.tcx.tables().node_id_to_type(id);
14631463
match fn_ty.sty {
14641464
ty::TyClosure(closure_def_id, substs) =>
14651465
self.ir.tcx.closure_type(closure_def_id, substs).sig.output(),
@@ -1502,7 +1502,7 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
15021502
None if !body.stmts.is_empty() =>
15031503
match body.stmts.last().unwrap().node {
15041504
hir::StmtSemi(ref e, _) => {
1505-
self.ir.tcx.expr_ty(&e) == fn_ret
1505+
self.ir.tcx.tables().expr_ty(&e) == fn_ret
15061506
},
15071507
_ => false
15081508
},

src/librustc/middle/stability.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -555,7 +555,7 @@ pub fn check_expr<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, e: &hir::Expr,
555555
hir::ExprMethodCall(i, ..) => {
556556
span = i.span;
557557
let method_call = ty::MethodCall::expr(e.id);
558-
tcx.tables.borrow().method_map[&method_call].def_id
558+
tcx.tables().method_map[&method_call].def_id
559559
}
560560
hir::ExprField(ref base_e, ref field) => {
561561
span = field.span;
@@ -580,7 +580,7 @@ pub fn check_expr<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, e: &hir::Expr,
580580
}
581581
}
582582
hir::ExprStruct(_, ref expr_fields, _) => {
583-
match tcx.expr_ty(e).sty {
583+
match tcx.tables().expr_ty(e).sty {
584584
ty::TyAdt(adt, ..) => match adt.adt_kind() {
585585
AdtKind::Struct | AdtKind::Union => {
586586
// check the stability of each field that appears
@@ -637,7 +637,7 @@ pub fn check_pat<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, pat: &hir::Pat,
637637
debug!("check_pat(pat = {:?})", pat);
638638
if is_internal(tcx, pat.span) { return; }
639639

640-
let v = match tcx.pat_ty_opt(pat).map(|ty| &ty.sty) {
640+
let v = match tcx.tables().pat_ty_opt(pat).map(|ty| &ty.sty) {
641641
Some(&ty::TyAdt(adt, _)) if !adt.is_enum() => adt.struct_variant(),
642642
_ => return,
643643
};

src/librustc/ty/context.rs

+60-9
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ use rustc_data_structures::accumulate_vec::AccumulateVec;
4040

4141
use arena::TypedArena;
4242
use std::borrow::Borrow;
43-
use std::cell::{Cell, RefCell, Ref};
43+
use std::cell::{Cell, RefCell};
4444
use std::hash::{Hash, Hasher};
4545
use std::mem;
4646
use std::ops::Deref;
@@ -252,6 +252,65 @@ impl<'a, 'gcx, 'tcx> Tables<'tcx> {
252252
fru_field_types: NodeMap()
253253
}
254254
}
255+
256+
pub fn node_id_to_type(&self, id: NodeId) -> Ty<'tcx> {
257+
match self.node_id_to_type_opt(id) {
258+
Some(ty) => ty,
259+
None => {
260+
bug!("node_id_to_type: no type for node `{}`",
261+
tls::with(|tcx| tcx.map.node_to_string(id)))
262+
}
263+
}
264+
}
265+
266+
pub fn node_id_to_type_opt(&self, id: NodeId) -> Option<Ty<'tcx>> {
267+
self.node_types.get(&id).cloned()
268+
}
269+
270+
pub fn node_id_item_substs(&self, id: NodeId) -> Option<&'tcx Substs<'tcx>> {
271+
self.item_substs.get(&id).map(|ts| ts.substs)
272+
}
273+
274+
// Returns the type of a pattern as a monotype. Like @expr_ty, this function
275+
// doesn't provide type parameter substitutions.
276+
pub fn pat_ty(&self, pat: &hir::Pat) -> Ty<'tcx> {
277+
self.node_id_to_type(pat.id)
278+
}
279+
280+
pub fn pat_ty_opt(&self, pat: &hir::Pat) -> Option<Ty<'tcx>> {
281+
self.node_id_to_type_opt(pat.id)
282+
}
283+
284+
// Returns the type of an expression as a monotype.
285+
//
286+
// NB (1): This is the PRE-ADJUSTMENT TYPE for the expression. That is, in
287+
// some cases, we insert `AutoAdjustment` annotations such as auto-deref or
288+
// auto-ref. The type returned by this function does not consider such
289+
// adjustments. See `expr_ty_adjusted()` instead.
290+
//
291+
// NB (2): This type doesn't provide type parameter substitutions; e.g. if you
292+
// ask for the type of "id" in "id(3)", it will return "fn(&isize) -> isize"
293+
// instead of "fn(ty) -> T with T = isize".
294+
pub fn expr_ty(&self, expr: &hir::Expr) -> Ty<'tcx> {
295+
self.node_id_to_type(expr.id)
296+
}
297+
298+
pub fn expr_ty_opt(&self, expr: &hir::Expr) -> Option<Ty<'tcx>> {
299+
self.node_id_to_type_opt(expr.id)
300+
}
301+
302+
303+
pub fn is_method_call(&self, expr_id: NodeId) -> bool {
304+
self.method_map.contains_key(&ty::MethodCall::expr(expr_id))
305+
}
306+
307+
pub fn is_overloaded_autoderef(&self, expr_id: NodeId, autoderefs: u32) -> bool {
308+
self.method_map.contains_key(&ty::MethodCall::autoderef(expr_id, autoderefs))
309+
}
310+
311+
pub fn upvar_capture(&self, upvar_id: ty::UpvarId) -> Option<ty::UpvarCapture<'tcx>> {
312+
Some(self.upvar_capture_map.get(&upvar_id).unwrap().clone())
313+
}
255314
}
256315

257316
impl<'tcx> CommonTypes<'tcx> {
@@ -587,14 +646,6 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
587646
self.ty_param_defs.borrow().get(&node_id).unwrap().clone()
588647
}
589648

590-
pub fn node_types(self) -> Ref<'a, NodeMap<Ty<'tcx>>> {
591-
fn projection<'a, 'tcx>(tables: &'a Tables<'tcx>) -> &'a NodeMap<Ty<'tcx>> {
592-
&tables.node_types
593-
}
594-
595-
Ref::map(self.tables.borrow(), projection)
596-
}
597-
598649
pub fn node_type_insert(self, id: NodeId, ty: Ty<'gcx>) {
599650
self.tables.borrow_mut().node_types.insert(id, ty);
600651
}

src/librustc/ty/mod.rs

+9-66
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ use util::nodemap::FnvHashMap;
3434

3535
use serialize::{self, Encodable, Encoder};
3636
use std::borrow::Cow;
37-
use std::cell::{Cell, RefCell};
37+
use std::cell::{Cell, RefCell, Ref};
3838
use std::hash::{Hash, Hasher};
3939
use std::iter;
4040
use std::ops::Deref;
@@ -2119,52 +2119,8 @@ impl BorrowKind {
21192119
}
21202120

21212121
impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
2122-
pub fn node_id_to_type(self, id: NodeId) -> Ty<'gcx> {
2123-
match self.node_id_to_type_opt(id) {
2124-
Some(ty) => ty,
2125-
None => bug!("node_id_to_type: no type for node `{}`",
2126-
self.map.node_to_string(id))
2127-
}
2128-
}
2129-
2130-
pub fn node_id_to_type_opt(self, id: NodeId) -> Option<Ty<'gcx>> {
2131-
self.tables.borrow().node_types.get(&id).cloned()
2132-
}
2133-
2134-
pub fn node_id_item_substs(self, id: NodeId) -> ItemSubsts<'gcx> {
2135-
match self.tables.borrow().item_substs.get(&id) {
2136-
None => ItemSubsts {
2137-
substs: self.global_tcx().intern_substs(&[])
2138-
},
2139-
Some(ts) => ts.clone(),
2140-
}
2141-
}
2142-
2143-
// Returns the type of a pattern as a monotype. Like @expr_ty, this function
2144-
// doesn't provide type parameter substitutions.
2145-
pub fn pat_ty(self, pat: &hir::Pat) -> Ty<'gcx> {
2146-
self.node_id_to_type(pat.id)
2147-
}
2148-
pub fn pat_ty_opt(self, pat: &hir::Pat) -> Option<Ty<'gcx>> {
2149-
self.node_id_to_type_opt(pat.id)
2150-
}
2151-
2152-
// Returns the type of an expression as a monotype.
2153-
//
2154-
// NB (1): This is the PRE-ADJUSTMENT TYPE for the expression. That is, in
2155-
// some cases, we insert `AutoAdjustment` annotations such as auto-deref or
2156-
// auto-ref. The type returned by this function does not consider such
2157-
// adjustments. See `expr_ty_adjusted()` instead.
2158-
//
2159-
// NB (2): This type doesn't provide type parameter substitutions; e.g. if you
2160-
// ask for the type of "id" in "id(3)", it will return "fn(&isize) -> isize"
2161-
// instead of "fn(ty) -> T with T = isize".
2162-
pub fn expr_ty(self, expr: &hir::Expr) -> Ty<'gcx> {
2163-
self.node_id_to_type(expr.id)
2164-
}
2165-
2166-
pub fn expr_ty_opt(self, expr: &hir::Expr) -> Option<Ty<'gcx>> {
2167-
self.node_id_to_type_opt(expr.id)
2122+
pub fn tables(self) -> Ref<'a, Tables<'gcx>> {
2123+
self.tables.borrow()
21682124
}
21692125

21702126
/// Returns the type of `expr`, considering any `AutoAdjustment`
@@ -2177,21 +2133,21 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
21772133
/// unless it was to fix it properly, which seemed a distraction from the
21782134
/// thread at hand! -nmatsakis
21792135
pub fn expr_ty_adjusted(self, expr: &hir::Expr) -> Ty<'gcx> {
2180-
self.expr_ty(expr)
2136+
self.tables().expr_ty(expr)
21812137
.adjust(self.global_tcx(), expr.span, expr.id,
2182-
self.tables.borrow().adjustments.get(&expr.id),
2138+
self.tables().adjustments.get(&expr.id),
21832139
|method_call| {
2184-
self.tables.borrow().method_map.get(&method_call).map(|method| method.ty)
2140+
self.tables().method_map.get(&method_call).map(|method| method.ty)
21852141
})
21862142
}
21872143

21882144
pub fn expr_ty_adjusted_opt(self, expr: &hir::Expr) -> Option<Ty<'gcx>> {
2189-
self.expr_ty_opt(expr).map(|t| t.adjust(self.global_tcx(),
2145+
self.tables().expr_ty_opt(expr).map(|t| t.adjust(self.global_tcx(),
21902146
expr.span,
21912147
expr.id,
2192-
self.tables.borrow().adjustments.get(&expr.id),
2148+
self.tables().adjustments.get(&expr.id),
21932149
|method_call| {
2194-
self.tables.borrow().method_map.get(&method_call).map(|method| method.ty)
2150+
self.tables().method_map.get(&method_call).map(|method| method.ty)
21952151
}))
21962152
}
21972153

@@ -2894,19 +2850,6 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
28942850
self.mk_region(ty::ReScope(self.region_maps.node_extent(id)))
28952851
}
28962852

2897-
pub fn is_method_call(self, expr_id: NodeId) -> bool {
2898-
self.tables.borrow().method_map.contains_key(&MethodCall::expr(expr_id))
2899-
}
2900-
2901-
pub fn is_overloaded_autoderef(self, expr_id: NodeId, autoderefs: u32) -> bool {
2902-
self.tables.borrow().method_map.contains_key(&MethodCall::autoderef(expr_id,
2903-
autoderefs))
2904-
}
2905-
2906-
pub fn upvar_capture(self, upvar_id: ty::UpvarId) -> Option<ty::UpvarCapture<'tcx>> {
2907-
Some(self.tables.borrow().upvar_capture_map.get(&upvar_id).unwrap().clone())
2908-
}
2909-
29102853
pub fn visit_all_items_in_krate<V,F>(self,
29112854
dep_node_fn: F,
29122855
visitor: &mut V)

src/librustc_borrowck/borrowck/gather_loans/gather_moves.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ pub fn gather_decl<'a, 'tcx>(bccx: &BorrowckCtxt<'a, 'tcx>,
3737
decl_id: ast::NodeId,
3838
_decl_span: Span,
3939
var_id: ast::NodeId) {
40-
let ty = bccx.tcx.node_id_to_type(var_id);
40+
let ty = bccx.tcx.tables().node_id_to_type(var_id);
4141
let loan_path = Rc::new(LoanPath::new(LpVar(var_id), ty));
4242
move_data.add_move(bccx.tcx, loan_path, decl_id, Declared);
4343
}

0 commit comments

Comments
 (0)