Skip to content

Commit 2fa64d0

Browse files
committed
Auto merge of rust-lang#99137 - jackh726:wf-no-infcx, r=estebank
Don't pass InferCtxt to WfPredicates Simple cleanup. Infer vars will get passed up as obligations and shallowed resolved later. This actually improves one test output.
2 parents e1d9a20 + a479f23 commit 2fa64d0

File tree

2 files changed

+54
-61
lines changed

2 files changed

+54
-61
lines changed

compiler/rustc_trait_selection/src/traits/wf.rs

+46-58
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,19 @@ pub fn obligations<'a, 'tcx>(
6060
GenericArgKind::Lifetime(..) => return Some(Vec::new()),
6161
};
6262

63-
let mut wf =
64-
WfPredicates { infcx, param_env, body_id, span, out: vec![], recursion_depth, item: None };
63+
let mut wf = WfPredicates {
64+
tcx: infcx.tcx,
65+
param_env,
66+
body_id,
67+
span,
68+
out: vec![],
69+
recursion_depth,
70+
item: None,
71+
};
6572
wf.compute(arg);
6673
debug!("wf::obligations({:?}, body_id={:?}) = {:?}", arg, body_id, wf.out);
6774

68-
let result = wf.normalize();
75+
let result = wf.normalize(infcx);
6976
debug!("wf::obligations({:?}, body_id={:?}) ~~> {:?}", arg, body_id, result);
7077
Some(result)
7178
}
@@ -83,7 +90,7 @@ pub fn trait_obligations<'a, 'tcx>(
8390
item: &'tcx hir::Item<'tcx>,
8491
) -> Vec<traits::PredicateObligation<'tcx>> {
8592
let mut wf = WfPredicates {
86-
infcx,
93+
tcx: infcx.tcx,
8794
param_env,
8895
body_id,
8996
span,
@@ -93,7 +100,7 @@ pub fn trait_obligations<'a, 'tcx>(
93100
};
94101
wf.compute_trait_ref(trait_ref, Elaborate::All);
95102
debug!(obligations = ?wf.out);
96-
wf.normalize()
103+
wf.normalize(infcx)
97104
}
98105

99106
pub fn predicate_obligations<'a, 'tcx>(
@@ -104,7 +111,7 @@ pub fn predicate_obligations<'a, 'tcx>(
104111
span: Span,
105112
) -> Vec<traits::PredicateObligation<'tcx>> {
106113
let mut wf = WfPredicates {
107-
infcx,
114+
tcx: infcx.tcx,
108115
param_env,
109116
body_id,
110117
span,
@@ -159,11 +166,11 @@ pub fn predicate_obligations<'a, 'tcx>(
159166
}
160167
}
161168

162-
wf.normalize()
169+
wf.normalize(infcx)
163170
}
164171

165-
struct WfPredicates<'a, 'tcx> {
166-
infcx: &'a InferCtxt<'a, 'tcx>,
172+
struct WfPredicates<'tcx> {
173+
tcx: TyCtxt<'tcx>,
167174
param_env: ty::ParamEnv<'tcx>,
168175
body_id: hir::HirId,
169176
span: Span,
@@ -260,18 +267,17 @@ fn extend_cause_with_original_assoc_item_obligation<'tcx>(
260267
}
261268
}
262269

263-
impl<'a, 'tcx> WfPredicates<'a, 'tcx> {
270+
impl<'tcx> WfPredicates<'tcx> {
264271
fn tcx(&self) -> TyCtxt<'tcx> {
265-
self.infcx.tcx
272+
self.tcx
266273
}
267274

268275
fn cause(&self, code: traits::ObligationCauseCode<'tcx>) -> traits::ObligationCause<'tcx> {
269276
traits::ObligationCause::new(self.span, self.body_id, code)
270277
}
271278

272-
fn normalize(mut self) -> Vec<traits::PredicateObligation<'tcx>> {
279+
fn normalize(self, infcx: &InferCtxt<'_, 'tcx>) -> Vec<traits::PredicateObligation<'tcx>> {
273280
let cause = self.cause(traits::WellFormed(None));
274-
let infcx = &mut self.infcx;
275281
let param_env = self.param_env;
276282
let mut obligations = Vec::with_capacity(self.out.len());
277283
for mut obligation in self.out {
@@ -296,7 +302,7 @@ impl<'a, 'tcx> WfPredicates<'a, 'tcx> {
296302

297303
/// Pushes the obligations required for `trait_ref` to be WF into `self.out`.
298304
fn compute_trait_ref(&mut self, trait_ref: &ty::TraitRef<'tcx>, elaborate: Elaborate) {
299-
let tcx = self.infcx.tcx;
305+
let tcx = self.tcx;
300306
let obligations = self.nominal_obligations(trait_ref.def_id, trait_ref.substs);
301307

302308
debug!("compute_trait_ref obligations {:?}", obligations);
@@ -410,14 +416,14 @@ impl<'a, 'tcx> WfPredicates<'a, 'tcx> {
410416
if !subty.has_escaping_bound_vars() {
411417
let cause = self.cause(cause);
412418
let trait_ref = ty::TraitRef {
413-
def_id: self.infcx.tcx.require_lang_item(LangItem::Sized, None),
414-
substs: self.infcx.tcx.mk_substs_trait(subty, &[]),
419+
def_id: self.tcx.require_lang_item(LangItem::Sized, None),
420+
substs: self.tcx.mk_substs_trait(subty, &[]),
415421
};
416422
self.out.push(traits::Obligation::with_depth(
417423
cause,
418424
self.recursion_depth,
419425
self.param_env,
420-
ty::Binder::dummy(trait_ref).without_const().to_predicate(self.infcx.tcx),
426+
ty::Binder::dummy(trait_ref).without_const().to_predicate(self.tcx),
421427
));
422428
}
423429
}
@@ -452,26 +458,16 @@ impl<'a, 'tcx> WfPredicates<'a, 'tcx> {
452458
predicate,
453459
));
454460
}
455-
ty::ConstKind::Infer(infer) => {
456-
let resolved = self.infcx.shallow_resolve(infer);
457-
// the `InferConst` changed, meaning that we made progress.
458-
if resolved != infer {
459-
let cause = self.cause(traits::WellFormed(None));
460-
461-
let resolved_constant = self.infcx.tcx.mk_const(ty::ConstS {
462-
kind: ty::ConstKind::Infer(resolved),
463-
ty: constant.ty(),
464-
});
465-
self.out.push(traits::Obligation::with_depth(
466-
cause,
467-
self.recursion_depth,
468-
self.param_env,
469-
ty::Binder::dummy(ty::PredicateKind::WellFormed(
470-
resolved_constant.into(),
471-
))
461+
ty::ConstKind::Infer(_) => {
462+
let cause = self.cause(traits::WellFormed(None));
463+
464+
self.out.push(traits::Obligation::with_depth(
465+
cause,
466+
self.recursion_depth,
467+
self.param_env,
468+
ty::Binder::dummy(ty::PredicateKind::WellFormed(constant.into()))
472469
.to_predicate(self.tcx()),
473-
));
474-
}
470+
));
475471
}
476472
ty::ConstKind::Error(_)
477473
| ty::ConstKind::Param(_)
@@ -627,7 +623,7 @@ impl<'a, 'tcx> WfPredicates<'a, 'tcx> {
627623
// of whatever returned this exact `impl Trait`.
628624

629625
// for named opaque `impl Trait` types we still need to check them
630-
if ty::is_impl_trait_defn(self.infcx.tcx, did).is_none() {
626+
if ty::is_impl_trait_defn(self.tcx, did).is_none() {
631627
let obligations = self.nominal_obligations(did, substs);
632628
self.out.extend(obligations);
633629
}
@@ -675,22 +671,14 @@ impl<'a, 'tcx> WfPredicates<'a, 'tcx> {
675671
// See also the comment on `fn obligations`, describing "livelock"
676672
// prevention, which happens before this can be reached.
677673
ty::Infer(_) => {
678-
let ty = self.infcx.shallow_resolve(ty);
679-
if let ty::Infer(ty::TyVar(_)) = ty.kind() {
680-
// Not yet resolved, but we've made progress.
681-
let cause = self.cause(traits::WellFormed(None));
682-
self.out.push(traits::Obligation::with_depth(
683-
cause,
684-
self.recursion_depth,
685-
param_env,
686-
ty::Binder::dummy(ty::PredicateKind::WellFormed(ty.into()))
687-
.to_predicate(self.tcx()),
688-
));
689-
} else {
690-
// Yes, resolved, proceed with the result.
691-
// FIXME(eddyb) add the type to `walker` instead of recursing.
692-
self.compute(ty.into());
693-
}
674+
let cause = self.cause(traits::WellFormed(None));
675+
self.out.push(traits::Obligation::with_depth(
676+
cause,
677+
self.recursion_depth,
678+
param_env,
679+
ty::Binder::dummy(ty::PredicateKind::WellFormed(ty.into()))
680+
.to_predicate(self.tcx()),
681+
));
694682
}
695683
}
696684
}
@@ -701,15 +689,15 @@ impl<'a, 'tcx> WfPredicates<'a, 'tcx> {
701689
def_id: DefId,
702690
substs: SubstsRef<'tcx>,
703691
) -> Vec<traits::PredicateObligation<'tcx>> {
704-
let predicates = self.infcx.tcx.predicates_of(def_id);
692+
let predicates = self.tcx.predicates_of(def_id);
705693
let mut origins = vec![def_id; predicates.predicates.len()];
706694
let mut head = predicates;
707695
while let Some(parent) = head.parent {
708-
head = self.infcx.tcx.predicates_of(parent);
696+
head = self.tcx.predicates_of(parent);
709697
origins.extend(iter::repeat(parent).take(head.predicates.len()));
710698
}
711699

712-
let predicates = predicates.instantiate(self.infcx.tcx, substs);
700+
let predicates = predicates.instantiate(self.tcx, substs);
713701
debug_assert_eq!(predicates.predicates.len(), origins.len());
714702

715703
iter::zip(iter::zip(predicates.predicates, predicates.spans), origins.into_iter().rev())
@@ -764,7 +752,7 @@ impl<'a, 'tcx> WfPredicates<'a, 'tcx> {
764752
// Note: in fact we only permit builtin traits, not `Bar<'d>`, I
765753
// am looking forward to the future here.
766754
if !data.has_escaping_bound_vars() && !region.has_escaping_bound_vars() {
767-
let implicit_bounds = object_region_bounds(self.infcx.tcx, data);
755+
let implicit_bounds = object_region_bounds(self.tcx, data);
768756

769757
let explicit_bound = region;
770758

@@ -777,7 +765,7 @@ impl<'a, 'tcx> WfPredicates<'a, 'tcx> {
777765
cause,
778766
self.recursion_depth,
779767
self.param_env,
780-
outlives.to_predicate(self.infcx.tcx),
768+
outlives.to_predicate(self.tcx),
781769
));
782770
}
783771
}

src/test/ui/issues/issue-98299.stderr

+8-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
1-
error[E0282]: type annotations needed
2-
--> $DIR/issue-98299.rs:4:5
1+
error[E0282]: type annotations needed for `SmallCString<N>`
2+
--> $DIR/issue-98299.rs:4:36
33
|
44
LL | SmallCString::try_from(p).map(|cstr| cstr);
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot infer type for enum `Result<SmallCString<{_: usize}>, ()>`
5+
| ^^^^
6+
|
7+
help: consider giving this closure parameter an explicit type, where the the value of const parameter `N` is specified
8+
|
9+
LL | SmallCString::try_from(p).map(|cstr: SmallCString<N>| cstr);
10+
| +++++++++++++++++
611

712
error: aborting due to previous error
813

0 commit comments

Comments
 (0)