Skip to content

Commit 6901d34

Browse files
authored
Rollup merge of rust-lang#72508 - ecstatic-morse:poly-self-ty, r=nikomatsakis
Make `PolyTraitRef::self_ty` return `Binder<Ty>` This came up during review of rust-lang#71618. The current implementation is the same as a call to `skip_binder` but harder to audit. Make it preserve binding levels and add a call to `skip_binder` at all use sites so they can be audited as part of rust-lang#72507.
2 parents 1063d8a + b4e06b9 commit 6901d34

File tree

7 files changed

+45
-25
lines changed

7 files changed

+45
-25
lines changed

src/librustc_middle/ty/sty.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -765,8 +765,8 @@ impl<'tcx> TraitRef<'tcx> {
765765
pub type PolyTraitRef<'tcx> = Binder<TraitRef<'tcx>>;
766766

767767
impl<'tcx> PolyTraitRef<'tcx> {
768-
pub fn self_ty(&self) -> Ty<'tcx> {
769-
self.skip_binder().self_ty()
768+
pub fn self_ty(&self) -> Binder<Ty<'tcx>> {
769+
self.map_bound_ref(|tr| tr.self_ty())
770770
}
771771

772772
pub fn def_id(&self) -> DefId {

src/librustc_trait_selection/traits/error_reporting/mod.rs

+21-11
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
290290
(
291291
Some(format!(
292292
"`?` couldn't convert the error to `{}`",
293-
trait_ref.self_ty(),
293+
trait_ref.skip_binder().self_ty(),
294294
)),
295295
Some(
296296
"the question mark operation (`?`) implicitly performs a \
@@ -340,7 +340,10 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
340340
if let Some(ret_span) = self.return_type_span(obligation) {
341341
err.span_label(
342342
ret_span,
343-
&format!("expected `{}` because of this", trait_ref.self_ty()),
343+
&format!(
344+
"expected `{}` because of this",
345+
trait_ref.skip_binder().self_ty()
346+
),
344347
);
345348
}
346349
}
@@ -353,7 +356,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
353356
"{}the trait `{}` is not implemented for `{}`",
354357
pre_message,
355358
trait_ref.print_only_trait_path(),
356-
trait_ref.self_ty(),
359+
trait_ref.skip_binder().self_ty(),
357360
)
358361
};
359362

@@ -643,7 +646,10 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
643646
return;
644647
}
645648

646-
let found_trait_ty = found_trait_ref.self_ty();
649+
let found_trait_ty = match found_trait_ref.self_ty().no_bound_vars() {
650+
Some(ty) => ty,
651+
None => return,
652+
};
647653

648654
let found_did = match found_trait_ty.kind {
649655
ty::Closure(did, _) | ty::Foreign(did) | ty::FnDef(did, _) => Some(did),
@@ -1360,11 +1366,15 @@ impl<'a, 'tcx> InferCtxtPrivExt<'tcx> for InferCtxt<'a, 'tcx> {
13601366
) {
13611367
let get_trait_impl = |trait_def_id| {
13621368
let mut trait_impl = None;
1363-
self.tcx.for_each_relevant_impl(trait_def_id, trait_ref.self_ty(), |impl_def_id| {
1364-
if trait_impl.is_none() {
1365-
trait_impl = Some(impl_def_id);
1366-
}
1367-
});
1369+
self.tcx.for_each_relevant_impl(
1370+
trait_def_id,
1371+
trait_ref.skip_binder().self_ty(),
1372+
|impl_def_id| {
1373+
if trait_impl.is_none() {
1374+
trait_impl = Some(impl_def_id);
1375+
}
1376+
},
1377+
);
13681378
trait_impl
13691379
};
13701380
let required_trait_path = self.tcx.def_path_str(trait_ref.def_id());
@@ -1435,7 +1445,7 @@ impl<'a, 'tcx> InferCtxtPrivExt<'tcx> for InferCtxt<'a, 'tcx> {
14351445
let mut err = match predicate.kind() {
14361446
ty::PredicateKind::Trait(ref data, _) => {
14371447
let trait_ref = data.to_poly_trait_ref();
1438-
let self_ty = trait_ref.self_ty();
1448+
let self_ty = trait_ref.skip_binder().self_ty();
14391449
debug!("self_ty {:?} {:?} trait_ref {:?}", self_ty, self_ty.kind, trait_ref);
14401450

14411451
if predicate.references_error() {
@@ -1564,7 +1574,7 @@ impl<'a, 'tcx> InferCtxtPrivExt<'tcx> for InferCtxt<'a, 'tcx> {
15641574
}
15651575
ty::PredicateKind::Projection(ref data) => {
15661576
let trait_ref = data.to_poly_trait_ref(self.tcx);
1567-
let self_ty = trait_ref.self_ty();
1577+
let self_ty = trait_ref.skip_binder().self_ty();
15681578
let ty = data.skip_binder().ty;
15691579
if predicate.references_error() {
15701580
return;

src/librustc_trait_selection/traits/error_reporting/suggestions.rs

+17-7
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
318318
trait_ref: ty::PolyTraitRef<'tcx>,
319319
body_id: hir::HirId,
320320
) {
321-
let self_ty = trait_ref.self_ty();
321+
let self_ty = trait_ref.skip_binder().self_ty();
322322
let (param_ty, projection) = match &self_ty.kind {
323323
ty::Param(_) => (true, None),
324324
ty::Projection(projection) => (false, Some(projection)),
@@ -524,7 +524,11 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
524524
trait_ref: &ty::Binder<ty::TraitRef<'tcx>>,
525525
points_at_arg: bool,
526526
) {
527-
let self_ty = trait_ref.self_ty();
527+
let self_ty = match trait_ref.self_ty().no_bound_vars() {
528+
None => return,
529+
Some(ty) => ty,
530+
};
531+
528532
let (def_id, output_ty, callable) = match self_ty.kind {
529533
ty::Closure(def_id, substs) => (def_id, substs.as_closure().sig().output(), "closure"),
530534
ty::FnDef(def_id, _) => (def_id, self_ty.fn_sig(self.tcx).output(), "function"),
@@ -707,7 +711,10 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
707711
return;
708712
}
709713

710-
let mut suggested_ty = trait_ref.self_ty();
714+
let mut suggested_ty = match trait_ref.self_ty().no_bound_vars() {
715+
Some(ty) => ty,
716+
None => return,
717+
};
711718

712719
for refs_remaining in 0..refs_number {
713720
if let ty::Ref(_, inner_ty, _) = suggested_ty.kind {
@@ -829,6 +836,9 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
829836
span: Span,
830837
trait_ref: &ty::Binder<ty::TraitRef<'tcx>>,
831838
) {
839+
let is_empty_tuple =
840+
|ty: ty::Binder<Ty<'_>>| ty.skip_binder().kind == ty::Tuple(ty::List::empty());
841+
832842
let hir = self.tcx.hir();
833843
let parent_node = hir.get_parent_node(obligation.cause.body_id);
834844
let node = hir.find(parent_node);
@@ -840,7 +850,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
840850
if let hir::ExprKind::Block(blk, _) = &body.value.kind {
841851
if sig.decl.output.span().overlaps(span)
842852
&& blk.expr.is_none()
843-
&& "()" == &trait_ref.self_ty().to_string()
853+
&& is_empty_tuple(trait_ref.self_ty())
844854
{
845855
// FIXME(estebank): When encountering a method with a trait
846856
// bound not satisfied in the return type with a body that has
@@ -1271,7 +1281,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
12711281
ObligationCauseCode::DerivedObligation(derived_obligation)
12721282
| ObligationCauseCode::BuiltinDerivedObligation(derived_obligation)
12731283
| ObligationCauseCode::ImplDerivedObligation(derived_obligation) => {
1274-
let ty = derived_obligation.parent_trait_ref.self_ty();
1284+
let ty = derived_obligation.parent_trait_ref.skip_binder().self_ty();
12751285
debug!(
12761286
"maybe_note_obligation_cause_for_async_await: \
12771287
parent_trait_ref={:?} self_ty.kind={:?}",
@@ -1917,7 +1927,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
19171927

19181928
let impls_future = self.tcx.type_implements_trait((
19191929
future_trait,
1920-
self_ty,
1930+
self_ty.skip_binder(),
19211931
ty::List::empty(),
19221932
obligation.param_env,
19231933
));
@@ -1933,7 +1943,7 @@ impl<'a, 'tcx> InferCtxtExt<'tcx> for InferCtxt<'a, 'tcx> {
19331943
let projection_ty = ty::ProjectionTy {
19341944
// `T`
19351945
substs: self.tcx.mk_substs_trait(
1936-
trait_ref.self_ty(),
1946+
trait_ref.self_ty().skip_binder(),
19371947
self.fresh_substs_for_item(span, item_def_id),
19381948
),
19391949
// `Future::Output`

src/librustc_trait_selection/traits/specialize/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -496,7 +496,7 @@ fn to_pretty_impl_header(tcx: TyCtxt<'_>, impl_def_id: DefId) -> Option<String>
496496
for (p, _) in predicates {
497497
if let Some(poly_trait_ref) = p.to_opt_poly_trait_ref() {
498498
if Some(poly_trait_ref.def_id()) == sized_trait {
499-
types_without_default_bounds.remove(poly_trait_ref.self_ty());
499+
types_without_default_bounds.remove(poly_trait_ref.self_ty().skip_binder());
500500
continue;
501501
}
502502
}

src/librustc_typeck/check/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3813,7 +3813,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
38133813
trait_ref: ty::PolyTraitRef<'tcx>,
38143814
expected_vid: ty::TyVid,
38153815
) -> bool {
3816-
let self_ty = self.shallow_resolve(trait_ref.self_ty());
3816+
let self_ty = self.shallow_resolve(trait_ref.skip_binder().self_ty());
38173817
debug!(
38183818
"self_type_matches_expected_vid(trait_ref={:?}, self_ty={:?}, expected_vid={:?})",
38193819
trait_ref, self_ty, expected_vid

src/librustdoc/clean/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -500,7 +500,7 @@ impl<'a> Clean<WherePredicate> for ty::PolyTraitPredicate<'a> {
500500
fn clean(&self, cx: &DocContext<'_>) -> WherePredicate {
501501
let poly_trait_ref = self.map_bound(|pred| pred.trait_ref);
502502
WherePredicate::BoundPredicate {
503-
ty: poly_trait_ref.self_ty().clean(cx),
503+
ty: poly_trait_ref.skip_binder().self_ty().clean(cx),
504504
bounds: vec![poly_trait_ref.clean(cx)],
505505
}
506506
}
@@ -755,7 +755,7 @@ impl<'a, 'tcx> Clean<Generics> for (&'a ty::Generics, ty::GenericPredicates<'tcx
755755
let mut projection = None;
756756
let param_idx = (|| {
757757
if let Some(trait_ref) = p.to_opt_poly_trait_ref() {
758-
if let ty::Param(param) = trait_ref.self_ty().kind {
758+
if let ty::Param(param) = trait_ref.skip_binder().self_ty().kind {
759759
return Some(param.index);
760760
}
761761
} else if let Some(outlives) = p.to_opt_type_outlives() {

src/tools/clippy/clippy_lints/src/future_not_send.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for FutureNotSend {
9595
let trait_ref = trait_pred.to_poly_trait_ref();
9696
db.note(&*format!(
9797
"`{}` doesn't implement `{}`",
98-
trait_ref.self_ty(),
98+
trait_ref.skip_binder().self_ty(),
9999
trait_ref.print_only_trait_path(),
100100
));
101101
}

0 commit comments

Comments
 (0)