Skip to content

Commit 1cef579

Browse files
authored
Rollup merge of rust-lang#135697 - compiler-errors:poly-trait-ref, r=lqd
Get rid of `ToPolyTraitRef` It's generally a footgun, since it throws away `PredicatePolarity`. This PR doesn't attempt to fix any related bugs having to do with binders or polarity; it just tries to pass through `TraitPredicate`s around instead of `TraitRef`s. There should be basically no functional changes.
2 parents 41ea084 + 2a180a9 commit 1cef579

File tree

9 files changed

+116
-125
lines changed

9 files changed

+116
-125
lines changed

compiler/rustc_infer/src/traits/util.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use rustc_data_structures::fx::FxHashSet;
2-
use rustc_middle::ty::{self, ToPolyTraitRef, TyCtxt};
2+
use rustc_middle::ty::{self, TyCtxt};
33
use rustc_span::{Ident, Span};
44
pub use rustc_type_ir::elaborate::*;
55

@@ -125,8 +125,8 @@ pub fn transitive_bounds_that_define_assoc_item<'tcx>(
125125
.iter_identity_copied()
126126
.map(|(clause, _)| clause.instantiate_supertrait(tcx, trait_ref))
127127
.filter_map(|clause| clause.as_trait_clause())
128-
// FIXME: Negative supertraits are elaborated here lol
129-
.map(|trait_pred| trait_pred.to_poly_trait_ref()),
128+
.filter(|clause| clause.polarity() == ty::PredicatePolarity::Positive)
129+
.map(|clause| clause.map_bound(|clause| clause.trait_ref)),
130130
);
131131

132132
return Some(trait_ref);

compiler/rustc_middle/src/ty/mod.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,7 @@ pub use self::predicate::{
7979
PolyExistentialPredicate, PolyExistentialProjection, PolyExistentialTraitRef,
8080
PolyProjectionPredicate, PolyRegionOutlivesPredicate, PolySubtypePredicate, PolyTraitPredicate,
8181
PolyTraitRef, PolyTypeOutlivesPredicate, Predicate, PredicateKind, ProjectionPredicate,
82-
RegionOutlivesPredicate, SubtypePredicate, ToPolyTraitRef, TraitPredicate, TraitRef,
83-
TypeOutlivesPredicate,
82+
RegionOutlivesPredicate, SubtypePredicate, TraitPredicate, TraitRef, TypeOutlivesPredicate,
8483
};
8584
pub use self::region::{
8685
BoundRegion, BoundRegionKind, EarlyParamRegion, LateParamRegion, LateParamRegionKind, Region,

compiler/rustc_middle/src/ty/predicate.rs

-10
Original file line numberDiff line numberDiff line change
@@ -476,16 +476,6 @@ impl<'tcx> Clause<'tcx> {
476476
}
477477
}
478478

479-
pub trait ToPolyTraitRef<'tcx> {
480-
fn to_poly_trait_ref(&self) -> PolyTraitRef<'tcx>;
481-
}
482-
483-
impl<'tcx> ToPolyTraitRef<'tcx> for PolyTraitPredicate<'tcx> {
484-
fn to_poly_trait_ref(&self) -> PolyTraitRef<'tcx> {
485-
self.map_bound_ref(|trait_pred| trait_pred.trait_ref)
486-
}
487-
}
488-
489479
impl<'tcx> UpcastFrom<TyCtxt<'tcx>, PredicateKind<'tcx>> for Predicate<'tcx> {
490480
fn upcast_from(from: PredicateKind<'tcx>, tcx: TyCtxt<'tcx>) -> Self {
491481
ty::Binder::dummy(from).upcast(tcx)

compiler/rustc_trait_selection/src/error_reporting/traits/ambiguity.rs

+14-10
Original file line numberDiff line numberDiff line change
@@ -172,14 +172,14 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
172172
let bound_predicate = predicate.kind();
173173
let mut err = match bound_predicate.skip_binder() {
174174
ty::PredicateKind::Clause(ty::ClauseKind::Trait(data)) => {
175-
let trait_ref = bound_predicate.rebind(data.trait_ref);
176-
debug!(?trait_ref);
175+
let trait_pred = bound_predicate.rebind(data);
176+
debug!(?trait_pred);
177177

178178
if let Err(e) = predicate.error_reported() {
179179
return e;
180180
}
181181

182-
if let Err(guar) = self.tcx.ensure().coherent_trait(trait_ref.def_id()) {
182+
if let Err(guar) = self.tcx.ensure().coherent_trait(trait_pred.def_id()) {
183183
// Avoid bogus "type annotations needed `Foo: Bar`" errors on `impl Bar for Foo` in case
184184
// other `Foo` impls are incoherent.
185185
return guar;
@@ -200,13 +200,13 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
200200
// avoid inundating the user with unnecessary errors, but we now
201201
// check upstream for type errors and don't add the obligations to
202202
// begin with in those cases.
203-
if self.tcx.is_lang_item(trait_ref.def_id(), LangItem::Sized) {
203+
if self.tcx.is_lang_item(trait_pred.def_id(), LangItem::Sized) {
204204
match self.tainted_by_errors() {
205205
None => {
206206
let err = self.emit_inference_failure_err(
207207
obligation.cause.body_id,
208208
span,
209-
trait_ref.self_ty().skip_binder().into(),
209+
trait_pred.self_ty().skip_binder().into(),
210210
TypeAnnotationNeeded::E0282,
211211
false,
212212
);
@@ -251,10 +251,14 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
251251

252252
let mut ambiguities = compute_applicable_impls_for_diagnostics(
253253
self.infcx,
254-
&obligation.with(self.tcx, trait_ref),
254+
&obligation.with(self.tcx, trait_pred),
255255
);
256-
let has_non_region_infer =
257-
trait_ref.skip_binder().args.types().any(|t| !t.is_ty_or_numeric_infer());
256+
let has_non_region_infer = trait_pred
257+
.skip_binder()
258+
.trait_ref
259+
.args
260+
.types()
261+
.any(|t| !t.is_ty_or_numeric_infer());
258262
// It doesn't make sense to talk about applicable impls if there are more than a
259263
// handful of them. If there are a lot of them, but only a few of them have no type
260264
// params, we only show those, as they are more likely to be useful/intended.
@@ -294,7 +298,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
294298
if impl_candidates.len() < 40 {
295299
self.report_similar_impl_candidates(
296300
impl_candidates.as_slice(),
297-
trait_ref,
301+
trait_pred,
298302
obligation.cause.body_id,
299303
&mut err,
300304
false,
@@ -306,7 +310,7 @@ impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
306310
if let ObligationCauseCode::WhereClause(def_id, _)
307311
| ObligationCauseCode::WhereClauseInExpr(def_id, ..) = *obligation.cause.code()
308312
{
309-
self.suggest_fully_qualified_path(&mut err, def_id, span, trait_ref.def_id());
313+
self.suggest_fully_qualified_path(&mut err, def_id, span, trait_pred.def_id());
310314
}
311315

312316
if let Some(ty::GenericArgKind::Type(_)) = arg.map(|arg| arg.unpack())

0 commit comments

Comments
 (0)