Skip to content

Commit ef2f5f6

Browse files
committed
typeck: Avoid passing &TyCtxt around where possible.
1 parent e387e6c commit ef2f5f6

File tree

16 files changed

+288
-359
lines changed

16 files changed

+288
-359
lines changed

src/librustc/ty/util.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ use hir;
3333

3434
pub trait IntTypeExt {
3535
fn to_ty<'tcx>(&self, tcx: &TyCtxt<'tcx>) -> Ty<'tcx>;
36-
fn disr_incr(&self, val: Disr) -> Option<Disr>;
36+
fn disr_incr(&self, tcx: &TyCtxt, val: Option<Disr>) -> Option<Disr>;
3737
fn assert_ty_matches(&self, val: Disr);
3838
fn initial_discriminant(&self, tcx: &TyCtxt) -> Disr;
3939
}
@@ -93,9 +93,13 @@ impl IntTypeExt for attr::IntType {
9393
}
9494
}
9595

96-
fn disr_incr(&self, val: Disr) -> Option<Disr> {
97-
self.assert_ty_matches(val);
98-
(val + ConstInt::Infer(1)).ok()
96+
fn disr_incr(&self, tcx: &TyCtxt, val: Option<Disr>) -> Option<Disr> {
97+
if let Some(val) = val {
98+
self.assert_ty_matches(val);
99+
(val + ConstInt::Infer(1)).ok()
100+
} else {
101+
Some(self.initial_discriminant(tcx))
102+
}
99103
}
100104
}
101105

src/librustc_typeck/astconv.rs

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ pub fn ast_path_substs_for_ty<'tcx>(
338338
}
339339
};
340340

341-
prohibit_projections(this.tcx(), &assoc_bindings);
341+
assoc_bindings.first().map(|b| prohibit_projection(this.tcx(), b.span));
342342

343343
create_substs_for_ast_path(this,
344344
span,
@@ -580,10 +580,11 @@ fn convert_angle_bracketed_parameters<'tcx>(this: &AstConv<'tcx>,
580580
/// Returns the appropriate lifetime to use for any output lifetimes
581581
/// (if one exists) and a vector of the (pattern, number of lifetimes)
582582
/// corresponding to each input type/pattern.
583-
fn find_implied_output_region<'tcx>(tcx: &TyCtxt<'tcx>,
583+
fn find_implied_output_region<'tcx>(this: &AstConv<'tcx>,
584584
input_tys: &[Ty<'tcx>],
585585
input_pats: Vec<String>) -> ElidedLifetime
586586
{
587+
let tcx = this.tcx();
587588
let mut lifetimes_for_params = Vec::new();
588589
let mut possible_implied_output_region = None;
589590

@@ -655,7 +656,7 @@ fn convert_parenthesized_parameters<'tcx>(this: &AstConv<'tcx>,
655656
.collect::<Vec<Ty<'tcx>>>();
656657

657658
let input_params = vec![String::new(); inputs.len()];
658-
let implied_output_region = find_implied_output_region(this.tcx(), &inputs, input_params);
659+
let implied_output_region = find_implied_output_region(this, &inputs, input_params);
659660

660661
let input_ty = this.tcx().mk_tup(inputs);
661662

@@ -824,7 +825,7 @@ fn ast_path_to_mono_trait_ref<'a,'tcx>(this: &AstConv<'tcx>,
824825
trait_def_id,
825826
self_ty,
826827
trait_segment);
827-
prohibit_projections(this.tcx(), &assoc_bindings);
828+
assoc_bindings.first().map(|b| prohibit_projection(this.tcx(), b.span));
828829
ty::TraitRef::new(trait_def_id, substs)
829830
}
830831

@@ -960,7 +961,7 @@ fn ast_type_binding_to_poly_projection_predicate<'tcx>(
960961
}
961962
}
962963

963-
let candidate = one_bound_for_assoc_type(tcx,
964+
let candidate = one_bound_for_assoc_type(this,
964965
candidates,
965966
&trait_ref.to_string(),
966967
&binding.item_name.as_str(),
@@ -1219,7 +1220,7 @@ fn find_bound_for_assoc_item<'tcx>(this: &AstConv<'tcx>,
12191220
.filter(|b| this.trait_defines_associated_type_named(b.def_id(), assoc_name))
12201221
.collect();
12211222

1222-
one_bound_for_assoc_type(tcx,
1223+
one_bound_for_assoc_type(this,
12231224
suitable_bounds,
12241225
&ty_param_name.as_str(),
12251226
&assoc_name.as_str(),
@@ -1229,23 +1230,23 @@ fn find_bound_for_assoc_item<'tcx>(this: &AstConv<'tcx>,
12291230

12301231
// Checks that bounds contains exactly one element and reports appropriate
12311232
// errors otherwise.
1232-
fn one_bound_for_assoc_type<'tcx>(tcx: &TyCtxt<'tcx>,
1233+
fn one_bound_for_assoc_type<'tcx>(this: &AstConv<'tcx>,
12331234
bounds: Vec<ty::PolyTraitRef<'tcx>>,
12341235
ty_param_name: &str,
12351236
assoc_name: &str,
12361237
span: Span)
12371238
-> Result<ty::PolyTraitRef<'tcx>, ErrorReported>
12381239
{
12391240
if bounds.is_empty() {
1240-
span_err!(tcx.sess, span, E0220,
1241+
span_err!(this.tcx().sess, span, E0220,
12411242
"associated type `{}` not found for `{}`",
12421243
assoc_name,
12431244
ty_param_name);
12441245
return Err(ErrorReported);
12451246
}
12461247

12471248
if bounds.len() > 1 {
1248-
let mut err = struct_span_err!(tcx.sess, span, E0221,
1249+
let mut err = struct_span_err!(this.tcx().sess, span, E0221,
12491250
"ambiguous associated type `{}` in bounds of `{}`",
12501251
assoc_name,
12511252
ty_param_name);
@@ -1305,7 +1306,7 @@ fn associated_path_def_to_ty<'tcx>(this: &AstConv<'tcx>,
13051306
assoc_name))
13061307
.collect();
13071308

1308-
match one_bound_for_assoc_type(tcx,
1309+
match one_bound_for_assoc_type(this,
13091310
candidates,
13101311
"Self",
13111312
&assoc_name.as_str(),
@@ -1814,7 +1815,7 @@ fn ty_of_method_or_bare_fn<'a, 'tcx>(this: &AstConv<'tcx>,
18141815
// have that lifetime.
18151816
let implied_output_region = match explicit_self_category {
18161817
Some(ty::ExplicitSelfCategory::ByReference(region, _)) => Ok(region),
1817-
_ => find_implied_output_region(this.tcx(), &arg_tys, arg_pats)
1818+
_ => find_implied_output_region(this, &arg_tys, arg_pats)
18181819
};
18191820

18201821
let output_ty = match decl.output {
@@ -2208,14 +2209,6 @@ pub fn partition_bounds<'a>(tcx: &TyCtxt,
22082209
}
22092210
}
22102211

2211-
fn prohibit_projections<'tcx>(tcx: &TyCtxt<'tcx>,
2212-
bindings: &[ConvertedBinding<'tcx>])
2213-
{
2214-
for binding in bindings.iter().take(1) {
2215-
prohibit_projection(tcx, binding.span);
2216-
}
2217-
}
2218-
22192212
fn check_type_argument_count(tcx: &TyCtxt, span: Span, supplied: usize,
22202213
required: usize, accepted: usize) {
22212214
if supplied < required {

src/librustc_typeck/check/_match.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ pub fn check_pat<'a, 'tcx>(pcx: &pat_ctxt<'a, 'tcx>,
116116

117117
// Check that the types of the end-points can be unified.
118118
let types_unify = require_same_types(
119-
tcx, Some(fcx.infcx()), false, pat.span, rhs_ty, lhs_ty,
119+
fcx.ccx, Some(fcx.infcx()), pat.span, rhs_ty, lhs_ty,
120120
"mismatched types in range",
121121
);
122122

src/librustc_typeck/check/callee.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,7 @@ fn confirm_builtin_call<'a,'tcx>(fcx: &FnCtxt<'a,'tcx>,
251251
// In that case, we check each argument against "error" in order to
252252
// set up all the node type bindings.
253253
error_fn_sig = ty::Binder(ty::FnSig {
254-
inputs: err_args(fcx.tcx(), arg_exprs.len()),
254+
inputs: err_args(fcx, arg_exprs.len()),
255255
output: ty::FnConverging(fcx.tcx().types.err),
256256
variadic: false
257257
});

src/librustc_typeck/check/compare_method.rs

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@
1010

1111
use middle::free_region::FreeRegionMap;
1212
use rustc::infer::{self, InferCtxt, InferOk, TypeOrigin};
13-
use rustc::ty::{self, TyCtxt};
13+
use rustc::ty;
1414
use rustc::traits::{self, ProjectionMode};
1515
use rustc::ty::subst::{self, Subst, Substs, VecPerParamSpace};
1616

1717
use syntax::ast;
1818
use syntax::codemap::Span;
1919

20+
use CrateCtxt;
2021
use super::assoc;
2122

2223
/// Checks that a method from an impl conforms to the signature of
@@ -30,18 +31,19 @@ use super::assoc;
3031
/// - trait_m: the method in the trait
3132
/// - impl_trait_ref: the TraitRef corresponding to the trait implementation
3233
33-
pub fn compare_impl_method<'tcx>(tcx: &TyCtxt<'tcx>,
34-
impl_m: &ty::Method<'tcx>,
35-
impl_m_span: Span,
36-
impl_m_body_id: ast::NodeId,
37-
trait_m: &ty::Method<'tcx>,
38-
impl_trait_ref: &ty::TraitRef<'tcx>) {
34+
pub fn compare_impl_method<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>,
35+
impl_m: &ty::Method<'tcx>,
36+
impl_m_span: Span,
37+
impl_m_body_id: ast::NodeId,
38+
trait_m: &ty::Method<'tcx>,
39+
impl_trait_ref: &ty::TraitRef<'tcx>) {
3940
debug!("compare_impl_method(impl_trait_ref={:?})",
4041
impl_trait_ref);
4142

4243
debug!("compare_impl_method: impl_trait_ref (liberated) = {:?}",
4344
impl_trait_ref);
4445

46+
let tcx = ccx.tcx;
4547
let mut infcx = InferCtxt::new(tcx, &tcx.tables, None, ProjectionMode::AnyFinal);
4648
let mut fulfillment_cx = traits::FulfillmentContext::new();
4749

@@ -186,7 +188,7 @@ pub fn compare_impl_method<'tcx>(tcx: &TyCtxt<'tcx>,
186188

187189
// Check region bounds. FIXME(@jroesch) refactor this away when removing
188190
// ParamBounds.
189-
if !check_region_bounds_on_impl_method(tcx,
191+
if !check_region_bounds_on_impl_method(ccx,
190192
impl_m_span,
191193
impl_m,
192194
&trait_m.generics,
@@ -364,14 +366,14 @@ pub fn compare_impl_method<'tcx>(tcx: &TyCtxt<'tcx>,
364366

365367
infcx.resolve_regions_and_report_errors(&free_regions, impl_m_body_id);
366368

367-
fn check_region_bounds_on_impl_method<'tcx>(tcx: &TyCtxt<'tcx>,
368-
span: Span,
369-
impl_m: &ty::Method<'tcx>,
370-
trait_generics: &ty::Generics<'tcx>,
371-
impl_generics: &ty::Generics<'tcx>,
372-
trait_to_skol_substs: &Substs<'tcx>,
373-
impl_to_skol_substs: &Substs<'tcx>)
374-
-> bool
369+
fn check_region_bounds_on_impl_method<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>,
370+
span: Span,
371+
impl_m: &ty::Method<'tcx>,
372+
trait_generics: &ty::Generics<'tcx>,
373+
impl_generics: &ty::Generics<'tcx>,
374+
trait_to_skol_substs: &Substs<'tcx>,
375+
impl_to_skol_substs: &Substs<'tcx>)
376+
-> bool
375377
{
376378

377379
let trait_params = trait_generics.regions.get_slice(subst::FnSpace);
@@ -397,7 +399,7 @@ pub fn compare_impl_method<'tcx>(tcx: &TyCtxt<'tcx>,
397399
// are zero. Since I don't quite know how to phrase things at
398400
// the moment, give a kind of vague error message.
399401
if trait_params.len() != impl_params.len() {
400-
span_err!(tcx.sess, span, E0195,
402+
span_err!(ccx.tcx.sess, span, E0195,
401403
"lifetime parameters or bounds on method `{}` do \
402404
not match the trait declaration",
403405
impl_m.name);
@@ -408,14 +410,15 @@ pub fn compare_impl_method<'tcx>(tcx: &TyCtxt<'tcx>,
408410
}
409411
}
410412

411-
pub fn compare_const_impl<'tcx>(tcx: &TyCtxt<'tcx>,
412-
impl_c: &ty::AssociatedConst<'tcx>,
413-
impl_c_span: Span,
414-
trait_c: &ty::AssociatedConst<'tcx>,
415-
impl_trait_ref: &ty::TraitRef<'tcx>) {
413+
pub fn compare_const_impl<'a, 'tcx>(ccx: &CrateCtxt<'a, 'tcx>,
414+
impl_c: &ty::AssociatedConst<'tcx>,
415+
impl_c_span: Span,
416+
trait_c: &ty::AssociatedConst<'tcx>,
417+
impl_trait_ref: &ty::TraitRef<'tcx>) {
416418
debug!("compare_const_impl(impl_trait_ref={:?})",
417419
impl_trait_ref);
418420

421+
let tcx = ccx.tcx;
419422
let infcx = InferCtxt::new(tcx, &tcx.tables, None, ProjectionMode::AnyFinal);
420423
let mut fulfillment_cx = traits::FulfillmentContext::new();
421424

src/librustc_typeck/check/dropck.rs

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
use CrateCtxt;
1112
use check::regionck::{self, Rcx};
1213

1314
use hir::def_id::DefId;
1415
use middle::free_region::FreeRegionMap;
1516
use rustc::infer::{self, InferCtxt};
1617
use middle::region;
1718
use rustc::ty::subst::{self, Subst};
18-
use rustc::ty::{self, Ty, TyCtxt};
19+
use rustc::ty::{self, Ty};
1920
use rustc::traits::{self, ProjectionMode};
2021
use util::nodemap::FnvHashSet;
2122

@@ -39,20 +40,20 @@ use syntax::codemap::{self, Span};
3940
/// struct/enum definition for the nominal type itself (i.e.
4041
/// cannot do `struct S<T>; impl<T:Clone> Drop for S<T> { ... }`).
4142
///
42-
pub fn check_drop_impl(tcx: &TyCtxt, drop_impl_did: DefId) -> Result<(), ()> {
43+
pub fn check_drop_impl(ccx: &CrateCtxt, drop_impl_did: DefId) -> Result<(), ()> {
4344
let ty::TypeScheme { generics: ref dtor_generics,
44-
ty: dtor_self_type } = tcx.lookup_item_type(drop_impl_did);
45-
let dtor_predicates = tcx.lookup_predicates(drop_impl_did);
45+
ty: dtor_self_type } = ccx.tcx.lookup_item_type(drop_impl_did);
46+
let dtor_predicates = ccx.tcx.lookup_predicates(drop_impl_did);
4647
match dtor_self_type.sty {
4748
ty::TyEnum(adt_def, self_to_impl_substs) |
4849
ty::TyStruct(adt_def, self_to_impl_substs) => {
49-
ensure_drop_params_and_item_params_correspond(tcx,
50+
ensure_drop_params_and_item_params_correspond(ccx,
5051
drop_impl_did,
5152
dtor_generics,
5253
&dtor_self_type,
5354
adt_def.did)?;
5455

55-
ensure_drop_predicates_are_implied_by_item_defn(tcx,
56+
ensure_drop_predicates_are_implied_by_item_defn(ccx,
5657
drop_impl_did,
5758
&dtor_predicates,
5859
adt_def.did,
@@ -61,21 +62,22 @@ pub fn check_drop_impl(tcx: &TyCtxt, drop_impl_did: DefId) -> Result<(), ()> {
6162
_ => {
6263
// Destructors only work on nominal types. This was
6364
// already checked by coherence, so we can panic here.
64-
let span = tcx.map.def_id_span(drop_impl_did, codemap::DUMMY_SP);
65+
let span = ccx.tcx.map.def_id_span(drop_impl_did, codemap::DUMMY_SP);
6566
span_bug!(span,
6667
"should have been rejected by coherence check: {}",
6768
dtor_self_type);
6869
}
6970
}
7071
}
7172

72-
fn ensure_drop_params_and_item_params_correspond<'tcx>(
73-
tcx: &TyCtxt<'tcx>,
73+
fn ensure_drop_params_and_item_params_correspond<'a, 'tcx>(
74+
ccx: &CrateCtxt<'a, 'tcx>,
7475
drop_impl_did: DefId,
7576
drop_impl_generics: &ty::Generics<'tcx>,
7677
drop_impl_ty: &ty::Ty<'tcx>,
7778
self_type_did: DefId) -> Result<(), ()>
7879
{
80+
let tcx = ccx.tcx;
7981
let drop_impl_node_id = tcx.map.as_local_node_id(drop_impl_did).unwrap();
8082
let self_type_node_id = tcx.map.as_local_node_id(self_type_did).unwrap();
8183

@@ -124,8 +126,8 @@ fn ensure_drop_params_and_item_params_correspond<'tcx>(
124126

125127
/// Confirms that every predicate imposed by dtor_predicates is
126128
/// implied by assuming the predicates attached to self_type_did.
127-
fn ensure_drop_predicates_are_implied_by_item_defn<'tcx>(
128-
tcx: &TyCtxt<'tcx>,
129+
fn ensure_drop_predicates_are_implied_by_item_defn<'a, 'tcx>(
130+
ccx: &CrateCtxt<'a, 'tcx>,
129131
drop_impl_did: DefId,
130132
dtor_predicates: &ty::GenericPredicates<'tcx>,
131133
self_type_did: DefId,
@@ -166,6 +168,8 @@ fn ensure_drop_predicates_are_implied_by_item_defn<'tcx>(
166168
// absent. So we report an error that the Drop impl injected a
167169
// predicate that is not present on the struct definition.
168170

171+
let tcx = ccx.tcx;
172+
169173
let self_type_node_id = tcx.map.as_local_node_id(self_type_did).unwrap();
170174

171175
let drop_impl_span = tcx.map.def_id_span(drop_impl_did, codemap::DUMMY_SP);
@@ -407,7 +411,7 @@ fn iterate_over_potentially_unsafe_regions_in_type<'a, 'b, 'tcx>(
407411
// unbounded type parameter `T`, we must resume the recursive
408412
// analysis on `T` (since it would be ignored by
409413
// type_must_outlive).
410-
if has_dtor_of_interest(tcx, ty) {
414+
if has_dtor_of_interest(cx, ty) {
411415
debug!("iterate_over_potentially_unsafe_regions_in_type \
412416
{}ty: {} - is a dtorck type!",
413417
(0..depth).map(|_| ' ').collect::<String>(),
@@ -499,11 +503,11 @@ fn iterate_over_potentially_unsafe_regions_in_type<'a, 'b, 'tcx>(
499503
}
500504
}
501505

502-
fn has_dtor_of_interest<'tcx>(tcx: &TyCtxt<'tcx>,
503-
ty: ty::Ty<'tcx>) -> bool {
506+
fn has_dtor_of_interest<'a, 'b, 'tcx>(cx: &DropckContext<'a, 'b, 'tcx>,
507+
ty: ty::Ty<'tcx>) -> bool {
504508
match ty.sty {
505509
ty::TyEnum(def, _) | ty::TyStruct(def, _) => {
506-
def.is_dtorck(tcx)
510+
def.is_dtorck(cx.rcx.tcx())
507511
}
508512
ty::TyTrait(..) | ty::TyProjection(..) => {
509513
debug!("ty: {:?} isn't known, and therefore is a dropck type", ty);

0 commit comments

Comments
 (0)