Skip to content

Commit 64b8cc9

Browse files
committed
Introduce traits::util::filter_predicates
1 parent a539a0e commit 64b8cc9

File tree

3 files changed

+33
-57
lines changed

3 files changed

+33
-57
lines changed

compiler/rustc_borrowck/src/diagnostics/region_errors.rs

+6-14
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ use rustc_infer::infer::{
1919
error_reporting::unexpected_hidden_region_diagnostic,
2020
BoundRegionConversionTime, NllRegionVariableOrigin, RelateParamBound,
2121
};
22-
use rustc_infer::traits::util::elaborate_predicates_of;
22+
use rustc_infer::traits::util::{elaborate_predicates_of, filter_predicates};
2323
use rustc_middle::hir::place::PlaceBase;
2424
use rustc_middle::mir::{ConstraintCategory, ReturnConstraint};
2525
use rustc_middle::traits::ObligationCauseCode;
@@ -691,20 +691,12 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
691691
let mut predicates: Vec<Span> = elaborate_predicates_of(tcx, def_id)
692692
.chain(elaborate_predicates_of(tcx, parent))
693693
.chain(trait_preds)
694-
.filter_map(|(pred, pred_span)| {
695-
if let ty::PredicateKind::Clause(clause) = pred.kind().skip_binder()
696-
&& let ty::ClauseKind::TypeOutlives(ty::OutlivesPredicate(pred_ty, r)) = clause
697-
&& r.kind() == ty::ReStatic
698-
&& (self.infcx.can_eq(self.param_env, ty, pred_ty)
699-
|| matches!(
694+
.filter_map(filter_predicates(tcx.lifetimes.re_static, |pred_ty| {
695+
self.infcx.can_eq(self.param_env, ty, pred_ty)
696+
|| matches!(
700697
pred_ty.kind(),
701-
ty::Param(name) if name.name == kw::SelfUpper))
702-
{
703-
Some(pred_span)
704-
} else {
705-
None
706-
}
707-
})
698+
ty::Param(name) if name.name == kw::SelfUpper)
699+
}))
708700
.collect();
709701
debug!(?predicates);
710702

compiler/rustc_infer/src/infer/error_reporting/mod.rs

+6-43
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ use crate::errors::{self, ObligationCauseFailureCode, TypeErrorAdditionalDiags};
5353
use crate::infer;
5454
use crate::infer::error_reporting::nice_region_error::find_anon_type::find_anon_type;
5555
use crate::infer::ExpectedFound;
56-
use crate::traits::util::elaborate_predicates_of;
56+
use crate::traits::util::{elaborate_predicates_of, filter_predicates};
5757
use crate::traits::{
5858
IfExpressionCause, MatchExpressionArmCause, ObligationCause, ObligationCauseCode,
5959
PredicateObligation,
@@ -498,20 +498,9 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
498498
self.tcx,
499499
generic_param_scope.into(),
500500
))
501-
.filter_map(|(pred, pred_span)| {
502-
if let ty::PredicateKind::Clause(clause) =
503-
pred.kind().skip_binder()
504-
&& let ty::ClauseKind::TypeOutlives(ty::OutlivesPredicate(
505-
_pred_ty,
506-
r,
507-
)) = clause
508-
&& r.kind() == ty::ReStatic
509-
{
510-
Some(pred_span)
511-
} else {
512-
None
513-
}
514-
})
501+
.filter_map(filter_predicates(self.tcx.lifetimes.re_static, |_| {
502+
true
503+
}))
515504
.collect();
516505

517506
if !spans.is_empty() {
@@ -2761,19 +2750,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
27612750
if let Some(def_id) = ptr.trait_ref.trait_def_id() {
27622751
// Find the bounds on the trait with the lifetime that couldn't be met.
27632752
let bindings: Vec<Span> = elaborate_predicates_of(self.tcx, def_id)
2764-
.filter_map(|(pred, pred_span)| {
2765-
if let ty::PredicateKind::Clause(clause) =
2766-
pred.kind().skip_binder()
2767-
&& let ty::ClauseKind::TypeOutlives(
2768-
ty::OutlivesPredicate(_pred_ty, r),
2769-
) = clause
2770-
&& r == self.found_region
2771-
{
2772-
Some(pred_span)
2773-
} else {
2774-
None
2775-
}
2776-
})
2753+
.filter_map(filter_predicates(self.found_region, |_| true))
27772754
.collect();
27782755
if !bindings.is_empty() {
27792756
self.lifetime_spans.insert(ptr.span);
@@ -2786,21 +2763,7 @@ impl<'tcx> TypeErrCtxt<'_, 'tcx> {
27862763
// definition of that associated item couldn't meet.
27872764
hir::TyKind::Path(hir::QPath::Resolved(Some(_), path)) => {
27882765
self.pred_spans = elaborate_predicates_of(self.tcx, path.res.def_id())
2789-
.filter_map(|(pred, pred_span)| {
2790-
match pred.kind().skip_binder() {
2791-
ty::PredicateKind::Clause(ty::ClauseKind::TypeOutlives(
2792-
ty::OutlivesPredicate(
2793-
// What should I filter this with?
2794-
_pred_ty,
2795-
r,
2796-
),
2797-
)) if r == self.found_region => Some(pred_span),
2798-
ty::PredicateKind::Clause(ty::ClauseKind::RegionOutlives(
2799-
ty::OutlivesPredicate(_, r),
2800-
)) if r == self.found_region => Some(pred_span),
2801-
_ => None,
2802-
}
2803-
})
2766+
.filter_map(filter_predicates(self.found_region, |_| true))
28042767
.collect();
28052768
}
28062769
_ => {}

compiler/rustc_infer/src/traits/util.rs

+21
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,27 @@ pub fn elaborate_predicates_of<'tcx>(
246246
)
247247
}
248248

249+
pub fn filter_predicates<'tcx>(
250+
region: ty::Region<'tcx>,
251+
check_ty: impl Fn(Ty<'tcx>) -> bool,
252+
) -> impl Fn((ty::Predicate<'tcx>, Span)) -> Option<Span> {
253+
move |(pred, span)| {
254+
let ty::PredicateKind::Clause(clause) = pred.kind().skip_binder() else {
255+
return None;
256+
};
257+
match clause {
258+
ty::ClauseKind::TypeOutlives(ty::OutlivesPredicate(pred_ty, r))
259+
if r == region && check_ty(pred_ty) =>
260+
{
261+
Some(span)
262+
}
263+
ty::ClauseKind::RegionOutlives(ty::OutlivesPredicate(_, r)) if r == region => {
264+
Some(span)
265+
}
266+
_ => None,
267+
}
268+
}
269+
}
249270
impl<'tcx, O: Elaboratable<'tcx>> Elaborator<'tcx, O> {
250271
fn extend_deduped(&mut self, obligations: impl IntoIterator<Item = O>) {
251272
// Only keep those bounds that we haven't already seen.

0 commit comments

Comments
 (0)