Skip to content

Commit 832ac11

Browse files
committed
remove the typeck::autoderef::Autoderef fcx field
This allows using it in an fcx-independent context
1 parent 0720124 commit 832ac11

File tree

6 files changed

+43
-42
lines changed

6 files changed

+43
-42
lines changed

src/librustc_typeck/check/autoderef.rs

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

11-
use astconv::AstConv;
12-
1311
use super::{FnCtxt, PlaceOp, Needs};
1412
use super::method::MethodCallee;
1513

16-
use rustc::infer::InferOk;
14+
use rustc::infer::{InferCtxt, InferOk};
1715
use rustc::session::DiagnosticMessageId;
1816
use rustc::traits::{self, TraitEngine};
1917
use rustc::ty::{self, Ty, TraitRef};
2018
use rustc::ty::{ToPredicate, TypeFoldable};
2119
use rustc::ty::adjustment::{Adjustment, Adjust, OverloadedDeref};
2220

2321
use syntax_pos::Span;
24-
use syntax::ast::Ident;
22+
use syntax::ast::{NodeId, Ident};
2523

2624
use std::iter;
2725

@@ -32,7 +30,9 @@ enum AutoderefKind {
3230
}
3331

3432
pub struct Autoderef<'a, 'gcx: 'tcx, 'tcx: 'a> {
35-
fcx: &'a FnCtxt<'a, 'gcx, 'tcx>,
33+
infcx: &'a InferCtxt<'a, 'gcx, 'tcx>,
34+
body_id: NodeId,
35+
param_env: ty::ParamEnv<'tcx>,
3636
steps: Vec<(Ty<'tcx>, AutoderefKind)>,
3737
cur_ty: Ty<'tcx>,
3838
obligations: Vec<traits::PredicateObligation<'tcx>>,
@@ -45,7 +45,7 @@ impl<'a, 'gcx, 'tcx> Iterator for Autoderef<'a, 'gcx, 'tcx> {
4545
type Item = (Ty<'tcx>, usize);
4646

4747
fn next(&mut self) -> Option<Self::Item> {
48-
let tcx = self.fcx.tcx;
48+
let tcx = self.infcx.tcx;
4949

5050
debug!("autoderef: steps={:?}, cur_ty={:?}",
5151
self.steps,
@@ -110,35 +110,35 @@ impl<'a, 'gcx, 'tcx> Autoderef<'a, 'gcx, 'tcx> {
110110
fn overloaded_deref_ty(&mut self, ty: Ty<'tcx>) -> Option<Ty<'tcx>> {
111111
debug!("overloaded_deref_ty({:?})", ty);
112112

113-
let tcx = self.fcx.tcx();
113+
let tcx = self.infcx.tcx;
114114

115115
// <cur_ty as Deref>
116116
let trait_ref = TraitRef {
117117
def_id: tcx.lang_items().deref_trait()?,
118118
substs: tcx.mk_substs_trait(self.cur_ty, &[]),
119119
};
120120

121-
let cause = traits::ObligationCause::misc(self.span, self.fcx.body_id);
121+
let cause = traits::ObligationCause::misc(self.span, self.body_id);
122122

123123
let obligation = traits::Obligation::new(cause.clone(),
124-
self.fcx.param_env,
124+
self.param_env,
125125
trait_ref.to_predicate());
126-
if !self.fcx.predicate_may_hold(&obligation) {
126+
if !self.infcx.predicate_may_hold(&obligation) {
127127
debug!("overloaded_deref_ty: cannot match obligation");
128128
return None;
129129
}
130130

131131
let mut fulfillcx = traits::FulfillmentContext::new_in_snapshot();
132132
let normalized_ty = fulfillcx.normalize_projection_type(
133-
&self.fcx,
134-
self.fcx.param_env,
133+
&self.infcx,
134+
self.param_env,
135135
ty::ProjectionTy::from_ref_and_name(
136136
tcx,
137137
trait_ref,
138138
Ident::from_str("Target"),
139139
),
140140
cause);
141-
if let Err(e) = fulfillcx.select_where_possible(&self.fcx) {
141+
if let Err(e) = fulfillcx.select_where_possible(&self.infcx) {
142142
// This shouldn't happen, except for evaluate/fulfill mismatches,
143143
// but that's not a reason for an ICE (`predicate_may_hold` is conservative
144144
// by design).
@@ -151,39 +151,39 @@ impl<'a, 'gcx, 'tcx> Autoderef<'a, 'gcx, 'tcx> {
151151
ty, normalized_ty, obligations);
152152
self.obligations.extend(obligations);
153153

154-
Some(self.fcx.resolve_type_vars_if_possible(&normalized_ty))
154+
Some(self.infcx.resolve_type_vars_if_possible(&normalized_ty))
155155
}
156156

157157
/// Returns the final type, generating an error if it is an
158158
/// unresolved inference variable.
159-
pub fn unambiguous_final_ty(&self) -> Ty<'tcx> {
160-
self.fcx.structurally_resolved_type(self.span, self.cur_ty)
159+
pub fn unambiguous_final_ty(&self, fcx: &FnCtxt<'a, 'gcx, 'tcx>) -> Ty<'tcx> {
160+
fcx.structurally_resolved_type(self.span, self.cur_ty)
161161
}
162162

163163
/// Returns the final type we ended up with, which may well be an
164164
/// inference variable (we will resolve it first, if possible).
165165
pub fn maybe_ambiguous_final_ty(&self) -> Ty<'tcx> {
166-
self.fcx.resolve_type_vars_if_possible(&self.cur_ty)
166+
self.infcx.resolve_type_vars_if_possible(&self.cur_ty)
167167
}
168168

169169
pub fn step_count(&self) -> usize {
170170
self.steps.len()
171171
}
172172

173173
/// Returns the adjustment steps.
174-
pub fn adjust_steps(&self, needs: Needs)
174+
pub fn adjust_steps(&self, fcx: &FnCtxt<'a, 'gcx, 'tcx>, needs: Needs)
175175
-> Vec<Adjustment<'tcx>> {
176-
self.fcx.register_infer_ok_obligations(self.adjust_steps_as_infer_ok(needs))
176+
fcx.register_infer_ok_obligations(self.adjust_steps_as_infer_ok(fcx, needs))
177177
}
178178

179-
pub fn adjust_steps_as_infer_ok(&self, needs: Needs)
179+
pub fn adjust_steps_as_infer_ok(&self, fcx: &FnCtxt<'a, 'gcx, 'tcx>, needs: Needs)
180180
-> InferOk<'tcx, Vec<Adjustment<'tcx>>> {
181181
let mut obligations = vec![];
182182
let targets = self.steps.iter().skip(1).map(|&(ty, _)| ty)
183183
.chain(iter::once(self.cur_ty));
184184
let steps: Vec<_> = self.steps.iter().map(|&(source, kind)| {
185185
if let AutoderefKind::Overloaded = kind {
186-
self.fcx.try_overloaded_deref(self.span, source, needs)
186+
fcx.try_overloaded_deref(self.span, source, needs)
187187
.and_then(|InferOk { value: method, obligations: o }| {
188188
obligations.extend(o);
189189
if let ty::Ref(region, _, mutbl) = method.sig.output().sty {
@@ -220,8 +220,7 @@ impl<'a, 'gcx, 'tcx> Autoderef<'a, 'gcx, 'tcx> {
220220
self
221221
}
222222

223-
pub fn finalize(self) {
224-
let fcx = self.fcx;
223+
pub fn finalize(self, fcx: &FnCtxt<'a, 'gcx, 'tcx>) {
225224
fcx.register_predicates(self.into_obligations());
226225
}
227226

@@ -233,7 +232,9 @@ impl<'a, 'gcx, 'tcx> Autoderef<'a, 'gcx, 'tcx> {
233232
impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
234233
pub fn autoderef(&'a self, span: Span, base_ty: Ty<'tcx>) -> Autoderef<'a, 'gcx, 'tcx> {
235234
Autoderef {
236-
fcx: self,
235+
infcx: &self.infcx,
236+
body_id: self.body_id,
237+
param_env: self.param_env,
237238
steps: vec![],
238239
cur_ty: self.resolve_type_vars_if_possible(&base_ty),
239240
obligations: vec![],

src/librustc_typeck/check/callee.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
5757
while result.is_none() && autoderef.next().is_some() {
5858
result = self.try_overloaded_call_step(call_expr, callee_expr, &autoderef);
5959
}
60-
autoderef.finalize();
60+
autoderef.finalize(self);
6161

6262
let output = match result {
6363
None => {
@@ -89,15 +89,15 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
8989
callee_expr: &'gcx hir::Expr,
9090
autoderef: &Autoderef<'a, 'gcx, 'tcx>)
9191
-> Option<CallStep<'tcx>> {
92-
let adjusted_ty = autoderef.unambiguous_final_ty();
92+
let adjusted_ty = autoderef.unambiguous_final_ty(self);
9393
debug!("try_overloaded_call_step(call_expr={:?}, adjusted_ty={:?})",
9494
call_expr,
9595
adjusted_ty);
9696

9797
// If the callee is a bare function or a closure, then we're all set.
9898
match adjusted_ty.sty {
9999
ty::FnDef(..) | ty::FnPtr(_) => {
100-
let adjustments = autoderef.adjust_steps(Needs::None);
100+
let adjustments = autoderef.adjust_steps(self, Needs::None);
101101
self.apply_adjustments(callee_expr, adjustments);
102102
return Some(CallStep::Builtin(adjusted_ty));
103103
}
@@ -115,7 +115,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
115115
infer::FnCall,
116116
&closure_ty
117117
).0;
118-
let adjustments = autoderef.adjust_steps(Needs::None);
118+
let adjustments = autoderef.adjust_steps(self, Needs::None);
119119
self.record_deferred_call_resolution(def_id, DeferredCallResolution {
120120
call_expr,
121121
callee_expr,
@@ -145,7 +145,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
145145
}
146146

147147
self.try_overloaded_call_traits(call_expr, adjusted_ty).map(|(autoref, method)| {
148-
let mut adjustments = autoderef.adjust_steps(Needs::None);
148+
let mut adjustments = autoderef.adjust_steps(self, Needs::None);
149149
adjustments.extend(autoref);
150150
self.apply_adjustments(callee_expr, adjustments);
151151
CallStep::Overloaded(method)

src/librustc_typeck/check/coercion.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,7 +419,7 @@ impl<'f, 'gcx, 'tcx> Coerce<'f, 'gcx, 'tcx> {
419419

420420
let needs = Needs::maybe_mut_place(mt_b.mutbl);
421421
let InferOk { value: mut adjustments, obligations: o }
422-
= autoderef.adjust_steps_as_infer_ok(needs);
422+
= autoderef.adjust_steps_as_infer_ok(self, needs);
423423
obligations.extend(o);
424424
obligations.extend(autoderef.into_obligations());
425425

src/librustc_typeck/check/method/confirm.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,9 @@ impl<'a, 'gcx, 'tcx> ConfirmContext<'a, 'gcx, 'tcx> {
161161
let (_, n) = autoderef.nth(pick.autoderefs).unwrap();
162162
assert_eq!(n, pick.autoderefs);
163163

164-
let mut adjustments = autoderef.adjust_steps(Needs::None);
164+
let mut adjustments = autoderef.adjust_steps(self, Needs::None);
165165

166-
let mut target = autoderef.unambiguous_final_ty();
166+
let mut target = autoderef.unambiguous_final_ty(self);
167167

168168
if let Some(mutbl) = pick.autoref {
169169
let region = self.next_region_var(infer::Autoref(self.span));
@@ -202,7 +202,7 @@ impl<'a, 'gcx, 'tcx> ConfirmContext<'a, 'gcx, 'tcx> {
202202
assert!(pick.unsize.is_none());
203203
}
204204

205-
autoderef.finalize();
205+
autoderef.finalize(self);
206206

207207
// Write out the final adjustments.
208208
self.apply_adjustments(self.self_expr, adjustments);

src/librustc_typeck/check/mod.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2554,7 +2554,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
25542554
while result.is_none() && autoderef.next().is_some() {
25552555
result = self.try_index_step(expr, base_expr, &autoderef, needs, idx_ty);
25562556
}
2557-
autoderef.finalize();
2557+
autoderef.finalize(self);
25582558
result
25592559
}
25602560

@@ -2571,7 +2571,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
25712571
index_ty: Ty<'tcx>)
25722572
-> Option<(/*index type*/ Ty<'tcx>, /*element type*/ Ty<'tcx>)>
25732573
{
2574-
let adjusted_ty = autoderef.unambiguous_final_ty();
2574+
let adjusted_ty = autoderef.unambiguous_final_ty(self);
25752575
debug!("try_index_step(expr={:?}, base_expr={:?}, adjusted_ty={:?}, \
25762576
index_ty={:?})",
25772577
expr,
@@ -2601,7 +2601,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
26012601
debug!("try_index_step: success, using overloaded indexing");
26022602
let method = self.register_infer_ok_obligations(ok);
26032603

2604-
let mut adjustments = autoderef.adjust_steps(needs);
2604+
let mut adjustments = autoderef.adjust_steps(self, needs);
26052605
if let ty::Ref(region, _, r_mutbl) = method.sig.inputs()[0].sty {
26062606
let mutbl = match r_mutbl {
26072607
hir::MutImmutable => AutoBorrowMutability::Immutable,
@@ -3295,9 +3295,9 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
32953295
// of error recovery.
32963296
self.write_field_index(expr.id, index);
32973297
if field.vis.is_accessible_from(def_scope, self.tcx) {
3298-
let adjustments = autoderef.adjust_steps(needs);
3298+
let adjustments = autoderef.adjust_steps(self, needs);
32993299
self.apply_adjustments(base, adjustments);
3300-
autoderef.finalize();
3300+
autoderef.finalize(self);
33013301

33023302
self.tcx.check_stability(field.did, Some(expr.id), expr.span);
33033303
return field_ty;
@@ -3310,9 +3310,9 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
33103310
if let Ok(index) = fstr.parse::<usize>() {
33113311
if fstr == index.to_string() {
33123312
if let Some(field_ty) = tys.get(index) {
3313-
let adjustments = autoderef.adjust_steps(needs);
3313+
let adjustments = autoderef.adjust_steps(self, needs);
33143314
self.apply_adjustments(base, adjustments);
3315-
autoderef.finalize();
3315+
autoderef.finalize(self);
33163316

33173317
self.write_field_index(expr.id, index);
33183318
return field_ty;
@@ -3323,7 +3323,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
33233323
_ => {}
33243324
}
33253325
}
3326-
autoderef.unambiguous_final_ty();
3326+
autoderef.unambiguous_final_ty(self);
33273327

33283328
if let Some((did, field_ty)) = private_candidate {
33293329
let struct_path = self.tcx().item_path_str(did);

src/librustc_typeck/check/wfcheck.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -766,7 +766,7 @@ fn check_method_receiver<'fcx, 'gcx, 'tcx>(fcx: &FnCtxt<'fcx, 'gcx, 'tcx>,
766766
potential_self_ty, self_ty);
767767

768768
if fcx.infcx.can_eq(fcx.param_env, self_ty, potential_self_ty).is_ok() {
769-
autoderef.finalize();
769+
autoderef.finalize(fcx);
770770
if let Some(mut err) = fcx.demand_eqtype_with_origin(
771771
&cause, self_ty, potential_self_ty) {
772772
err.emit();

0 commit comments

Comments
 (0)