Skip to content

Commit afa884c

Browse files
committed
Instantiate all bound vars existentially
1 parent 79b6c41 commit afa884c

File tree

15 files changed

+68
-65
lines changed

15 files changed

+68
-65
lines changed

src/librustc/infer/canonical/substitute.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,6 @@ where
8080
}
8181
};
8282

83-
tcx.replace_escaping_bound_vars(value, fld_r, fld_t)
83+
tcx.replace_escaping_bound_vars(value, fld_r, fld_t).0
8484
}
8585
}

src/librustc/infer/higher_ranked/mod.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,11 @@ impl<'a, 'gcx, 'tcx> CombineFields<'a, 'gcx, 'tcx> {
5959
// with a fresh region variable. These region variables --
6060
// but no other pre-existing region variables -- can name
6161
// the placeholders.
62-
let (a_prime, _) =
63-
self.infcx.replace_late_bound_regions_with_fresh_var(
64-
span,
65-
HigherRankedType,
66-
a);
62+
let (a_prime, _) = self.infcx.replace_bound_vars_with_fresh_vars(
63+
span,
64+
HigherRankedType,
65+
a
66+
);
6767

6868
debug!("a_prime={:?}", a_prime);
6969
debug!("b_prime={:?}", b_prime);

src/librustc/infer/mod.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -1328,18 +1328,18 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
13281328
self.report_and_explain_type_error(trace, &err)
13291329
}
13301330

1331-
pub fn replace_late_bound_regions_with_fresh_var<T>(
1331+
pub fn replace_bound_vars_with_fresh_vars<T>(
13321332
&self,
13331333
span: Span,
13341334
lbrct: LateBoundRegionConversionTime,
1335-
value: &ty::Binder<T>,
1335+
value: &ty::Binder<T>
13361336
) -> (T, BTreeMap<ty::BoundRegion, ty::Region<'tcx>>)
13371337
where
1338-
T: TypeFoldable<'tcx>,
1338+
T: TypeFoldable<'tcx>
13391339
{
1340-
self.tcx.replace_late_bound_regions(value, |br| {
1341-
self.next_region_var(LateBoundRegion(span, br, lbrct))
1342-
})
1340+
let fld_r = |br| self.next_region_var(LateBoundRegion(span, br, lbrct));
1341+
let fld_t = |_| self.next_ty_var(TypeVariableOrigin::MiscVariable(span));
1342+
self.tcx.replace_bound_vars(value, fld_r, fld_t)
13431343
}
13441344

13451345
/// Given a higher-ranked projection predicate like:

src/librustc/traits/error_reporting.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -212,10 +212,11 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
212212
// cause I have no idea for a good error message.
213213
if let ty::Predicate::Projection(ref data) = predicate {
214214
let mut selcx = SelectionContext::new(self);
215-
let (data, _) = self.replace_late_bound_regions_with_fresh_var(
215+
let (data, _) = self.replace_bound_vars_with_fresh_vars(
216216
obligation.cause.span,
217217
infer::LateBoundRegionConversionTime::HigherRankedType,
218-
data);
218+
data
219+
);
219220
let mut obligations = vec![];
220221
let normalized_ty = super::normalize_projection_type(
221222
&mut selcx,

src/librustc/ty/fold.rs

+14-16
Original file line numberDiff line numberDiff line change
@@ -520,22 +520,14 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
520520
pub fn replace_late_bound_regions<T, F>(
521521
self,
522522
value: &Binder<T>,
523-
mut fld_r: F
523+
fld_r: F
524524
) -> (T, BTreeMap<ty::BoundRegion, ty::Region<'tcx>>)
525525
where F: FnMut(ty::BoundRegion) -> ty::Region<'tcx>,
526526
T: TypeFoldable<'tcx>
527527
{
528-
let mut map = BTreeMap::new();
529-
let mut real_fldr = |br| {
530-
*map.entry(br).or_insert_with(|| fld_r(br))
531-
};
532-
533528
// identity for bound types
534-
let mut fld_t = |bound_ty| self.mk_ty(ty::Bound(bound_ty));
535-
536-
let mut replacer = BoundVarReplacer::new(self, &mut real_fldr, &mut fld_t);
537-
let result = value.skip_binder().fold_with(&mut replacer);
538-
(result, map)
529+
let fld_t = |bound_ty| self.mk_ty(ty::Bound(bound_ty));
530+
self.replace_escaping_bound_vars(value.skip_binder(), fld_r, fld_t)
539531
}
540532

541533
/// Replace all escaping bound vars. The `fld_r` closure replaces escaping
@@ -545,17 +537,23 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
545537
value: &T,
546538
mut fld_r: F,
547539
mut fld_t: G
548-
) -> T
540+
) -> (T, BTreeMap<ty::BoundRegion, ty::Region<'tcx>>)
549541
where F: FnMut(ty::BoundRegion) -> ty::Region<'tcx>,
550542
G: FnMut(ty::BoundTy) -> ty::Ty<'tcx>,
551543
T: TypeFoldable<'tcx>
552544
{
545+
let mut map = BTreeMap::new();
546+
553547
if !value.has_escaping_bound_vars() {
554-
value.clone()
548+
(value.clone(), map)
555549
} else {
556-
let mut replacer = BoundVarReplacer::new(self, &mut fld_r, &mut fld_t);
550+
let mut real_fld_r = |br| {
551+
*map.entry(br).or_insert_with(|| fld_r(br))
552+
};
553+
554+
let mut replacer = BoundVarReplacer::new(self, &mut real_fld_r, &mut fld_t);
557555
let result = value.fold_with(&mut replacer);
558-
result
556+
(result, map)
559557
}
560558
}
561559

@@ -567,7 +565,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
567565
value: &Binder<T>,
568566
fld_r: F,
569567
fld_t: G
570-
) -> T
568+
) -> (T, BTreeMap<ty::BoundRegion, ty::Region<'tcx>>)
571569
where F: FnMut(ty::BoundRegion) -> ty::Region<'tcx>,
572570
G: FnMut(ty::BoundTy) -> ty::Ty<'tcx>,
573571
T: TypeFoldable<'tcx>

src/librustc_mir/borrow_check/nll/type_check/input_output.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
6262
// "inside" the closure.
6363
Some(
6464
self.infcx
65-
.replace_late_bound_regions_with_fresh_var(
65+
.replace_bound_vars_with_fresh_vars(
6666
mir.span,
6767
LateBoundRegionConversionTime::FnCall,
6868
&poly_sig,

src/librustc_mir/borrow_check/nll/type_check/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1406,7 +1406,7 @@ impl<'a, 'gcx, 'tcx> TypeChecker<'a, 'gcx, 'tcx> {
14061406
return;
14071407
}
14081408
};
1409-
let (sig, map) = self.infcx.replace_late_bound_regions_with_fresh_var(
1409+
let (sig, map) = self.infcx.replace_bound_vars_with_fresh_vars(
14101410
term.source_info.span,
14111411
LateBoundRegionConversionTime::FnCall,
14121412
&sig,

src/librustc_traits/chalk_context/mod.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -347,12 +347,11 @@ impl context::UnificationOps<ChalkArenas<'gcx>, ChalkArenas<'tcx>>
347347
&mut self,
348348
arg: &ty::Binder<Goal<'tcx>>,
349349
) -> Goal<'tcx> {
350-
let (value, _map) = self.infcx.replace_late_bound_regions_with_fresh_var(
350+
self.infcx.replace_bound_vars_with_fresh_vars(
351351
DUMMY_SP,
352352
LateBoundRegionConversionTime::HigherRankedType,
353-
arg,
354-
);
355-
value
353+
arg
354+
).0
356355
}
357356

358357
fn debug_ex_clause(&mut self, value: &'v ChalkExClause<'tcx>) -> Box<dyn Debug + 'v> {

src/librustc_typeck/check/callee.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -110,10 +110,11 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
110110
// fnmut vs fnonce. If so, we have to defer further processing.
111111
if self.closure_kind(def_id, substs).is_none() {
112112
let closure_ty = self.closure_sig(def_id, substs);
113-
let fn_sig = self.replace_late_bound_regions_with_fresh_var(call_expr.span,
114-
infer::FnCall,
115-
&closure_ty)
116-
.0;
113+
let fn_sig = self.replace_bound_vars_with_fresh_vars(
114+
call_expr.span,
115+
infer::FnCall,
116+
&closure_ty
117+
).0;
117118
let adjustments = autoderef.adjust_steps(Needs::None);
118119
self.record_deferred_call_resolution(def_id, DeferredCallResolution {
119120
call_expr,
@@ -284,7 +285,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
284285
// previously appeared within a `Binder<>` and hence would not
285286
// have been normalized before.
286287
let fn_sig =
287-
self.replace_late_bound_regions_with_fresh_var(call_expr.span, infer::FnCall, &fn_sig)
288+
self.replace_bound_vars_with_fresh_vars(call_expr.span, infer::FnCall, &fn_sig)
288289
.0;
289290
let fn_sig = self.normalize_associated_types_in(call_expr.span, &fn_sig);
290291

src/librustc_typeck/check/closure.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
564564
// `liberated_sig` is E'.
565565
{
566566
// Instantiate (this part of..) S to S', i.e., with fresh variables.
567-
let (supplied_ty, _) = self.infcx.replace_late_bound_regions_with_fresh_var(
567+
let (supplied_ty, _) = self.infcx.replace_bound_vars_with_fresh_vars(
568568
hir_ty.span,
569569
LateBoundRegionConversionTime::FnCall,
570570
&ty::Binder::bind(supplied_ty),
@@ -605,7 +605,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
605605
);
606606
}
607607

608-
let (supplied_output_ty, _) = self.infcx.replace_late_bound_regions_with_fresh_var(
608+
let (supplied_output_ty, _) = self.infcx.replace_bound_vars_with_fresh_vars(
609609
decl.output.span(),
610610
LateBoundRegionConversionTime::FnCall,
611611
&supplied_sig.output(),

src/librustc_typeck/check/compare_method.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ fn compare_predicate_entailment<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
233233
let mut selcx = traits::SelectionContext::new(&infcx);
234234

235235
let impl_m_own_bounds = impl_m_predicates.instantiate_own(tcx, impl_to_skol_substs);
236-
let (impl_m_own_bounds, _) = infcx.replace_late_bound_regions_with_fresh_var(
236+
let (impl_m_own_bounds, _) = infcx.replace_bound_vars_with_fresh_vars(
237237
impl_m_span,
238238
infer::HigherRankedType,
239239
&ty::Binder::bind(impl_m_own_bounds.predicates)
@@ -262,10 +262,11 @@ fn compare_predicate_entailment<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
262262
// Compute placeholder form of impl and trait method tys.
263263
let tcx = infcx.tcx;
264264

265-
let (impl_sig, _) =
266-
infcx.replace_late_bound_regions_with_fresh_var(impl_m_span,
267-
infer::HigherRankedType,
268-
&tcx.fn_sig(impl_m.def_id));
265+
let (impl_sig, _) = infcx.replace_bound_vars_with_fresh_vars(
266+
impl_m_span,
267+
infer::HigherRankedType,
268+
&tcx.fn_sig(impl_m.def_id)
269+
);
269270
let impl_sig =
270271
inh.normalize_associated_types_in(impl_m_span,
271272
impl_m_node_id,

src/librustc_typeck/check/method/confirm.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ impl<'a, 'gcx, 'tcx> ConfirmContext<'a, 'gcx, 'tcx> {
245245
let original_poly_trait_ref = principal.with_self_ty(this.tcx, object_ty);
246246
let upcast_poly_trait_ref = this.upcast(original_poly_trait_ref, trait_def_id);
247247
let upcast_trait_ref =
248-
this.replace_late_bound_regions_with_fresh_var(&upcast_poly_trait_ref);
248+
this.replace_bound_vars_with_fresh_vars(&upcast_poly_trait_ref);
249249
debug!("original_poly_trait_ref={:?} upcast_trait_ref={:?} target_trait={:?}",
250250
original_poly_trait_ref,
251251
upcast_trait_ref,
@@ -268,7 +268,7 @@ impl<'a, 'gcx, 'tcx> ConfirmContext<'a, 'gcx, 'tcx> {
268268
probe::WhereClausePick(ref poly_trait_ref) => {
269269
// Where clauses can have bound regions in them. We need to instantiate
270270
// those to convert from a poly-trait-ref to a trait-ref.
271-
self.replace_late_bound_regions_with_fresh_var(&poly_trait_ref).substs
271+
self.replace_bound_vars_with_fresh_vars(&poly_trait_ref).substs
272272
}
273273
}
274274
}
@@ -398,7 +398,7 @@ impl<'a, 'gcx, 'tcx> ConfirmContext<'a, 'gcx, 'tcx> {
398398
// NB: Instantiate late-bound regions first so that
399399
// `instantiate_type_scheme` can normalize associated types that
400400
// may reference those regions.
401-
let method_sig = self.replace_late_bound_regions_with_fresh_var(&sig);
401+
let method_sig = self.replace_bound_vars_with_fresh_vars(&sig);
402402
debug!("late-bound lifetimes from method instantiated, method_sig={:?}",
403403
method_sig);
404404

@@ -633,11 +633,9 @@ impl<'a, 'gcx, 'tcx> ConfirmContext<'a, 'gcx, 'tcx> {
633633
upcast_trait_refs.into_iter().next().unwrap()
634634
}
635635

636-
fn replace_late_bound_regions_with_fresh_var<T>(&self, value: &ty::Binder<T>) -> T
636+
fn replace_bound_vars_with_fresh_vars<T>(&self, value: &ty::Binder<T>) -> T
637637
where T: TypeFoldable<'tcx>
638638
{
639-
self.fcx
640-
.replace_late_bound_regions_with_fresh_var(self.span, infer::FnCall, value)
641-
.0
639+
self.fcx.replace_bound_vars_with_fresh_vars(self.span, infer::FnCall, value).0
642640
}
643641
}

src/librustc_typeck/check/method/mod.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -311,9 +311,11 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
311311
// `instantiate_type_scheme` can normalize associated types that
312312
// may reference those regions.
313313
let fn_sig = tcx.fn_sig(def_id);
314-
let fn_sig = self.replace_late_bound_regions_with_fresh_var(span,
315-
infer::FnCall,
316-
&fn_sig).0;
314+
let fn_sig = self.replace_bound_vars_with_fresh_vars(
315+
span,
316+
infer::FnCall,
317+
&fn_sig
318+
).0;
317319
let fn_sig = fn_sig.subst(self.tcx, substs);
318320
let fn_sig = match self.normalize_associated_types_in_as_infer_ok(span, &fn_sig) {
319321
InferOk { value, obligations: o } => {

src/librustc_typeck/check/method/probe.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -755,8 +755,11 @@ impl<'a, 'gcx, 'tcx> ProbeContext<'a, 'gcx, 'tcx> {
755755
self.probe(|_| {
756756
let substs = self.fresh_substs_for_item(self.span, method.def_id);
757757
let fty = fty.subst(self.tcx, substs);
758-
let (fty, _) = self.replace_late_bound_regions_with_fresh_var(
759-
self.span, infer::FnCall, &fty);
758+
let (fty, _) = self.replace_bound_vars_with_fresh_vars(
759+
self.span,
760+
infer::FnCall,
761+
&fty
762+
);
760763

761764
if let Some(self_ty) = self_ty {
762765
if self.at(&ObligationCause::dummy(), self.param_env)

src/librustc_typeck/check/mod.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -1921,11 +1921,11 @@ impl<'a, 'gcx, 'tcx> AstConv<'gcx, 'tcx> for FnCtxt<'a, 'gcx, 'tcx> {
19211921
poly_trait_ref: ty::PolyTraitRef<'tcx>)
19221922
-> Ty<'tcx>
19231923
{
1924-
let (trait_ref, _) =
1925-
self.replace_late_bound_regions_with_fresh_var(
1926-
span,
1927-
infer::LateBoundRegionConversionTime::AssocTypeProjection(item_def_id),
1928-
&poly_trait_ref);
1924+
let (trait_ref, _) = self.replace_bound_vars_with_fresh_vars(
1925+
span,
1926+
infer::LateBoundRegionConversionTime::AssocTypeProjection(item_def_id),
1927+
&poly_trait_ref
1928+
);
19291929

19301930
self.tcx().mk_projection(item_def_id, trait_ref.substs)
19311931
}

0 commit comments

Comments
 (0)