Skip to content

Commit 6a8d131

Browse files
committed
rustc: make all read access to tcx.tables go through a method.
1 parent ea4b94d commit 6a8d131

File tree

35 files changed

+219
-230
lines changed

35 files changed

+219
-230
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/reachable.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for ReachableContext<'a, 'tcx> {
116116
}
117117
hir::ExprMethodCall(..) => {
118118
let method_call = ty::MethodCall::expr(expr.id);
119-
let def_id = self.tcx.tables.borrow().method_map[&method_call].def_id;
119+
let def_id = self.tcx.tables().method_map[&method_call].def_id;
120120

121121
// Mark the trait item (and, possibly, its default impl) as reachable
122122
// Or mark inherent impl item as reachable

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
@@ -41,7 +41,7 @@ use rustc_data_structures::accumulate_vec::AccumulateVec;
4141

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

260319
impl<'tcx> CommonTypes<'tcx> {
@@ -599,14 +658,6 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
599658
self.ty_param_defs.borrow().get(&node_id).unwrap().clone()
600659
}
601660

602-
pub fn node_types(self) -> Ref<'a, NodeMap<Ty<'tcx>>> {
603-
fn projection<'a, 'tcx>(tables: &'a Tables<'tcx>) -> &'a NodeMap<Ty<'tcx>> {
604-
&tables.node_types
605-
}
606-
607-
Ref::map(self.tables.borrow(), projection)
608-
}
609-
610661
pub fn node_type_insert(self, id: NodeId, ty: Ty<'gcx>) {
611662
self.tables.borrow_mut().node_types.insert(id, ty);
612663
}

src/librustc/ty/mod.rs

+8-65
Original file line numberDiff line numberDiff line change
@@ -2120,52 +2120,8 @@ impl BorrowKind {
21202120
}
21212121

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

21712127
/// Returns the type of `expr`, considering any `AutoAdjustment`
@@ -2178,21 +2134,21 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
21782134
/// unless it was to fix it properly, which seemed a distraction from the
21792135
/// thread at hand! -nmatsakis
21802136
pub fn expr_ty_adjusted(self, expr: &hir::Expr) -> Ty<'gcx> {
2181-
self.expr_ty(expr)
2137+
self.tables().expr_ty(expr)
21822138
.adjust(self.global_tcx(), expr.span, expr.id,
2183-
self.tables.borrow().adjustments.get(&expr.id),
2139+
self.tables().adjustments.get(&expr.id),
21842140
|method_call| {
2185-
self.tables.borrow().method_map.get(&method_call).map(|method| method.ty)
2141+
self.tables().method_map.get(&method_call).map(|method| method.ty)
21862142
})
21872143
}
21882144

21892145
pub fn expr_ty_adjusted_opt(self, expr: &hir::Expr) -> Option<Ty<'gcx>> {
2190-
self.expr_ty_opt(expr).map(|t| t.adjust(self.global_tcx(),
2146+
self.tables().expr_ty_opt(expr).map(|t| t.adjust(self.global_tcx(),
21912147
expr.span,
21922148
expr.id,
2193-
self.tables.borrow().adjustments.get(&expr.id),
2149+
self.tables().adjustments.get(&expr.id),
21942150
|method_call| {
2195-
self.tables.borrow().method_map.get(&method_call).map(|method| method.ty)
2151+
self.tables().method_map.get(&method_call).map(|method| method.ty)
21962152
}))
21972153
}
21982154

@@ -2908,19 +2864,6 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
29082864
self.mk_region(ty::ReScope(self.region_maps.node_extent(id)))
29092865
}
29102866

2911-
pub fn is_method_call(self, expr_id: NodeId) -> bool {
2912-
self.tables.borrow().method_map.contains_key(&MethodCall::expr(expr_id))
2913-
}
2914-
2915-
pub fn is_overloaded_autoderef(self, expr_id: NodeId, autoderefs: u32) -> bool {
2916-
self.tables.borrow().method_map.contains_key(&MethodCall::autoderef(expr_id,
2917-
autoderefs))
2918-
}
2919-
2920-
pub fn upvar_capture(self, upvar_id: ty::UpvarId) -> Option<ty::UpvarCapture<'tcx>> {
2921-
Some(self.tables.borrow().upvar_capture_map.get(&upvar_id).unwrap().clone())
2922-
}
2923-
29242867
pub fn visit_all_items_in_krate<V,F>(self,
29252868
dep_node_fn: F,
29262869
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)