Skip to content

Commit 2f4d868

Browse files
committed
remove the typeck::autoderef::Autoderef fcx field
This allows using it in an fcx-independent context
1 parent 558e3e6 commit 2f4d868

File tree

7 files changed

+84
-79
lines changed

7 files changed

+84
-79
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
}
@@ -114,7 +114,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
114114
infer::FnCall,
115115
&closure_ty)
116116
.0;
117-
let adjustments = autoderef.adjust_steps(Needs::None);
117+
let adjustments = autoderef.adjust_steps(self, Needs::None);
118118
self.record_deferred_call_resolution(def_id, DeferredCallResolution {
119119
call_expr,
120120
callee_expr,
@@ -144,7 +144,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
144144
}
145145

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

src/librustc_typeck/check/coercion.rs

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

423423
let needs = Needs::maybe_mut_place(mt_b.mutbl);
424424
let InferOk { value: mut adjustments, obligations: o }
425-
= autoderef.adjust_steps_as_infer_ok(needs);
425+
= autoderef.adjust_steps_as_infer_ok(self, needs);
426426
obligations.extend(o);
427427
obligations.extend(autoderef.into_obligations());
428428

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/method/probe.rs

Lines changed: 41 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ struct ProbeContext<'a, 'gcx: 'a + 'tcx, 'tcx: 'a> {
4949
mode: Mode,
5050
method_name: Option<ast::Ident>,
5151
return_type: Option<Ty<'tcx>>,
52-
steps: Rc<Vec<CandidateStep<'tcx>>>,
52+
steps: Rc<Vec<CandidateStep<'gcx, 'tcx>>>,
5353
inherent_candidates: Vec<Candidate<'tcx>>,
5454
extension_candidates: Vec<Candidate<'tcx>>,
5555
impl_dups: FxHashSet<DefId>,
@@ -80,8 +80,8 @@ impl<'a, 'gcx, 'tcx> Deref for ProbeContext<'a, 'gcx, 'tcx> {
8080
}
8181

8282
#[derive(Debug)]
83-
struct CandidateStep<'tcx> {
84-
self_ty: Ty<'tcx>,
83+
struct CandidateStep<'gcx: 'tcx, 'tcx> {
84+
self_ty: Canonical<'gcx, QueryResult<'gcx, Ty<'gcx>>>,
8585
autoderefs: usize,
8686
// true if the type results from a dereference of a raw pointer.
8787
// when assembling candidates, we include these steps, but not when
@@ -247,31 +247,34 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
247247
-> Result<R, MethodError<'tcx>>
248248
where OP: FnOnce(ProbeContext<'a, 'gcx, 'tcx>) -> Result<R, MethodError<'tcx>>
249249
{
250-
// FIXME(#18741) -- right now, creating the steps involves evaluating the
251-
// `*` operator, which registers obligations that then escape into
252-
// the global fulfillment context and thus has global
253-
// side-effects. This is a bit of a pain to refactor. So just let
254-
// it ride, although it's really not great, and in fact could I
255-
// think cause spurious errors. Really though this part should
256-
// take place in the `self.probe` below.
250+
let mut orig_values = SmallVec::new();
251+
let self_ty = self.infcx.canonicalize_data(&self_ty, &mut orig_values);
257252
let steps = if mode == Mode::MethodCall {
258-
match self.create_steps(span, scope_expr_id, self_ty, is_suggestion) {
259-
Some(steps) => steps,
260-
None => {
261-
return Err(MethodError::NoMatch(NoMatchData::new(Vec::new(),
262-
Vec::new(),
263-
Vec::new(),
264-
None,
265-
mode)))
253+
tcx.infer_ctxt().enter(|ref infcx| {
254+
match create_steps(infcx, span, scope_expr_id, self_ty, is_suggestion) {
255+
Some(steps) => Ok(steps),
256+
None => {
257+
return Err(MethodError::NoMatch(NoMatchData::new(Vec::new(),
258+
Vec::new(),
259+
Vec::new(),
260+
None,
261+
mode)))
262+
}
266263
}
267-
}
264+
})?;
268265
} else {
269266
vec![CandidateStep {
270-
self_ty,
271-
autoderefs: 0,
272-
from_unsafe_deref: false,
273-
unsize: false,
274-
}]
267+
self_ty,
268+
autoderefs: 0,
269+
from_unsafe_deref: false,
270+
unsize: false,
271+
}];
272+
}
273+
274+
let steps =
275+
276+
} else {
277+
275278
};
276279

277280
debug!("ProbeContext: steps for self_ty={:?} are {:?}",
@@ -295,19 +298,20 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
295298
op(probe_cx)
296299
})
297300
}
301+
}
298302

299-
fn create_steps(&self,
300-
span: Span,
301-
scope_expr_id: ast::NodeId,
302-
self_ty: Ty<'tcx>,
303-
is_suggestion: IsSuggestion)
304-
-> Option<Vec<CandidateStep<'tcx>>> {
305-
// FIXME: we don't need to create the entire steps in one pass
306-
307-
let mut autoderef = self.autoderef(span, self_ty).include_raw_pointers();
308-
let mut reached_raw_pointer = false;
309-
let mut steps: Vec<_> = autoderef.by_ref()
310-
.map(|(ty, d)| {
303+
fn create_steps(&self,
304+
span: Span,
305+
scope_expr_id: ast::NodeId,
306+
self_ty: Ty<'tcx>,
307+
is_suggestion: IsSuggestion)
308+
-> Option<Vec<CandidateStep<'tcx>>> {
309+
// FIXME: we don't need to create the entire steps in one pass
310+
311+
let mut autoderef = self.autoderef(span, self_ty).include_raw_pointers();
312+
let mut reached_raw_pointer = false;
313+
let mut steps: Vec<_> = autoderef.by_ref()
314+
.map(|(ty, d)| {
311315
let step = CandidateStep {
312316
self_ty: ty,
313317
autoderefs: d,
@@ -376,7 +380,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
376380

377381
Some(steps)
378382
}
379-
}
383+
380384

381385
impl<'a, 'gcx, 'tcx> ProbeContext<'a, 'gcx, 'tcx> {
382386
fn new(fcx: &'a FnCtxt<'a, 'gcx, 'tcx>,

0 commit comments

Comments
 (0)