Skip to content

Commit 6607280

Browse files
authored
Rollup merge of rust-lang#112870 - compiler-errors:clause-2, r=oli-obk
Migrate `item_bounds` to `ty::Clause` Should be simpler than the next PR that's coming up. Last three commits are the relevant ones. r? `@oli-obk` or `@lcnr`
2 parents ed25a8f + 471830b commit 6607280

File tree

39 files changed

+243
-174
lines changed

39 files changed

+243
-174
lines changed

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -705,7 +705,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
705705
ty::Alias(ty::Opaque, ty::AliasTy { def_id, substs, .. }) => tcx
706706
.explicit_item_bounds(def_id)
707707
.subst_iter_copied(tcx, substs)
708-
.find_map(find_fn_kind_from_did),
708+
.find_map(|(clause, span)| find_fn_kind_from_did((clause.as_predicate(), span))),
709709
ty::Closure(_, substs) => match substs.as_closure().kind() {
710710
ty::ClosureKind::Fn => Some(hir::Mutability::Not),
711711
ty::ClosureKind::FnMut => Some(hir::Mutability::Mut),

compiler/rustc_hir_analysis/src/check/mod.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -409,9 +409,7 @@ fn fn_sig_suggestion<'tcx>(
409409
output = if let ty::Alias(_, alias_ty) = *output.kind() {
410410
tcx.explicit_item_bounds(alias_ty.def_id)
411411
.subst_iter_copied(tcx, alias_ty.substs)
412-
.find_map(|(bound, _)| {
413-
bound.to_opt_poly_projection_pred()?.no_bound_vars()?.term.ty()
414-
})
412+
.find_map(|(bound, _)| bound.as_projection_clause()?.no_bound_vars()?.term.ty())
415413
.unwrap_or_else(|| {
416414
span_bug!(
417415
ident.span,

compiler/rustc_hir_analysis/src/check/wfcheck.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1086,7 +1086,7 @@ fn check_associated_type_bounds(wfcx: &WfCheckingCtxt<'_, '_>, item: ty::AssocIt
10861086
wfcx.infcx,
10871087
wfcx.param_env,
10881088
wfcx.body_def_id,
1089-
normalized_bound,
1089+
normalized_bound.as_predicate(),
10901090
bound_span,
10911091
)
10921092
});
@@ -1562,7 +1562,7 @@ impl<'tcx> TypeVisitor<TyCtxt<'tcx>> for ImplTraitInTraitFinder<'_, 'tcx> {
15621562
self.wfcx.infcx,
15631563
self.wfcx.param_env,
15641564
self.wfcx.body_def_id,
1565-
bound,
1565+
bound.as_predicate(),
15661566
bound_span,
15671567
));
15681568
// Set the debruijn index back to innermost here, since we already eagerly

compiler/rustc_hir_analysis/src/collect/item_bounds.rs

+14-20
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ fn associated_type_bounds<'tcx>(
1919
assoc_item_def_id: LocalDefId,
2020
ast_bounds: &'tcx [hir::GenericBound<'tcx>],
2121
span: Span,
22-
) -> &'tcx [(ty::Predicate<'tcx>, Span)] {
22+
) -> &'tcx [(ty::Clause<'tcx>, Span)] {
2323
let item_ty = tcx.mk_projection(
2424
assoc_item_def_id.to_def_id(),
2525
InternalSubsts::identity_for_item(tcx, assoc_item_def_id),
@@ -33,8 +33,11 @@ fn associated_type_bounds<'tcx>(
3333
let trait_def_id = tcx.local_parent(assoc_item_def_id);
3434
let trait_predicates = tcx.trait_explicit_predicates_and_bounds(trait_def_id);
3535

36-
let bounds_from_parent = trait_predicates.predicates.iter().copied().filter(|(pred, _)| {
37-
match pred.kind().skip_binder() {
36+
let bounds_from_parent = trait_predicates
37+
.predicates
38+
.iter()
39+
.copied()
40+
.filter(|(pred, _)| match pred.kind().skip_binder() {
3841
ty::PredicateKind::Clause(ty::ClauseKind::Trait(tr)) => tr.self_ty() == item_ty,
3942
ty::PredicateKind::Clause(ty::ClauseKind::Projection(proj)) => {
4043
proj.projection_ty.self_ty() == item_ty
@@ -43,15 +46,10 @@ fn associated_type_bounds<'tcx>(
4346
outlives.0 == item_ty
4447
}
4548
_ => false,
46-
}
47-
});
49+
})
50+
.map(|(pred, span)| (pred.expect_clause(), span));
4851

49-
let all_bounds = tcx.arena.alloc_from_iter(
50-
bounds
51-
.clauses()
52-
.map(|(clause, span)| (clause.as_predicate(), span))
53-
.chain(bounds_from_parent),
54-
);
52+
let all_bounds = tcx.arena.alloc_from_iter(bounds.clauses().chain(bounds_from_parent));
5553
debug!(
5654
"associated_type_bounds({}) = {:?}",
5755
tcx.def_path_str(assoc_item_def_id.to_def_id()),
@@ -71,23 +69,22 @@ fn opaque_type_bounds<'tcx>(
7169
ast_bounds: &'tcx [hir::GenericBound<'tcx>],
7270
item_ty: Ty<'tcx>,
7371
span: Span,
74-
) -> &'tcx [(ty::Predicate<'tcx>, Span)] {
72+
) -> &'tcx [(ty::Clause<'tcx>, Span)] {
7573
ty::print::with_no_queries!({
7674
let icx = ItemCtxt::new(tcx, opaque_def_id);
7775
let mut bounds = icx.astconv().compute_bounds(item_ty, ast_bounds, OnlySelfBounds(false));
7876
// Opaque types are implicitly sized unless a `?Sized` bound is found
7977
icx.astconv().add_implicitly_sized(&mut bounds, item_ty, ast_bounds, None, span);
8078
debug!(?bounds);
8179

82-
tcx.arena
83-
.alloc_from_iter(bounds.clauses().map(|(clause, span)| (clause.as_predicate(), span)))
80+
tcx.arena.alloc_from_iter(bounds.clauses())
8481
})
8582
}
8683

8784
pub(super) fn explicit_item_bounds(
8885
tcx: TyCtxt<'_>,
8986
def_id: LocalDefId,
90-
) -> ty::EarlyBinder<&'_ [(ty::Predicate<'_>, Span)]> {
87+
) -> ty::EarlyBinder<&'_ [(ty::Clause<'_>, Span)]> {
9188
match tcx.opt_rpitit_info(def_id.to_def_id()) {
9289
// RPITIT's bounds are the same as opaque type bounds, but with
9390
// a projection self type.
@@ -139,11 +136,8 @@ pub(super) fn explicit_item_bounds(
139136
pub(super) fn item_bounds(
140137
tcx: TyCtxt<'_>,
141138
def_id: DefId,
142-
) -> ty::EarlyBinder<&'_ ty::List<ty::Predicate<'_>>> {
139+
) -> ty::EarlyBinder<&'_ ty::List<ty::Clause<'_>>> {
143140
tcx.explicit_item_bounds(def_id).map_bound(|bounds| {
144-
tcx.mk_predicates_from_iter(util::elaborate(
145-
tcx,
146-
bounds.iter().map(|&(bound, _span)| bound),
147-
))
141+
tcx.mk_clauses_from_iter(util::elaborate(tcx, bounds.iter().map(|&(bound, _span)| bound)))
148142
})
149143
}

compiler/rustc_hir_analysis/src/outlives/mod.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use rustc_hir as hir;
33
use rustc_hir::def_id::LocalDefId;
44
use rustc_middle::query::Providers;
55
use rustc_middle::ty::subst::GenericArgKind;
6-
use rustc_middle::ty::{self, CratePredicatesMap, TyCtxt};
6+
use rustc_middle::ty::{self, CratePredicatesMap, ToPredicate, TyCtxt};
77
use rustc_span::symbol::sym;
88
use rustc_span::Span;
99

@@ -17,7 +17,7 @@ pub fn provide(providers: &mut Providers) {
1717
*providers = Providers { inferred_outlives_of, inferred_outlives_crate, ..*providers };
1818
}
1919

20-
fn inferred_outlives_of(tcx: TyCtxt<'_>, item_def_id: LocalDefId) -> &[(ty::ClauseKind<'_>, Span)] {
20+
fn inferred_outlives_of(tcx: TyCtxt<'_>, item_def_id: LocalDefId) -> &[(ty::Clause<'_>, Span)] {
2121
let id = tcx.hir().local_def_id_to_hir_id(item_def_id);
2222

2323
if matches!(tcx.def_kind(item_def_id), hir::def::DefKind::AnonConst)
@@ -52,7 +52,7 @@ fn inferred_outlives_of(tcx: TyCtxt<'_>, item_def_id: LocalDefId) -> &[(ty::Clau
5252
if tcx.has_attr(item_def_id, sym::rustc_outlives) {
5353
let mut pred: Vec<String> = predicates
5454
.iter()
55-
.map(|(out_pred, _)| match out_pred {
55+
.map(|(out_pred, _)| match out_pred.kind().skip_binder() {
5656
ty::ClauseKind::RegionOutlives(p) => p.to_string(),
5757
ty::ClauseKind::TypeOutlives(p) => p.to_string(),
5858
err => bug!("unexpected clause {:?}", err),
@@ -104,13 +104,15 @@ fn inferred_outlives_crate(tcx: TyCtxt<'_>, (): ()) -> CratePredicatesMap<'_> {
104104
|(ty::OutlivesPredicate(kind1, region2), &span)| {
105105
match kind1.unpack() {
106106
GenericArgKind::Type(ty1) => Some((
107-
ty::ClauseKind::TypeOutlives(ty::OutlivesPredicate(ty1, *region2)),
107+
ty::ClauseKind::TypeOutlives(ty::OutlivesPredicate(ty1, *region2))
108+
.to_predicate(tcx),
108109
span,
109110
)),
110111
GenericArgKind::Lifetime(region1) => Some((
111112
ty::ClauseKind::RegionOutlives(ty::OutlivesPredicate(
112113
region1, *region2,
113-
)),
114+
))
115+
.to_predicate(tcx),
114116
span,
115117
)),
116118
GenericArgKind::Const(_) => {

compiler/rustc_hir_analysis/src/variance/mod.rs

+5-8
Original file line numberDiff line numberDiff line change
@@ -162,28 +162,25 @@ fn variance_of_opaque(tcx: TyCtxt<'_>, item_def_id: LocalDefId) -> &[ty::Varianc
162162
// which thus mentions `'a` and should thus accept hidden types that borrow 'a
163163
// instead of requiring an additional `+ 'a`.
164164
match pred.kind().skip_binder() {
165-
ty::PredicateKind::Clause(ty::ClauseKind::Trait(ty::TraitPredicate {
165+
ty::ClauseKind::Trait(ty::TraitPredicate {
166166
trait_ref: ty::TraitRef { def_id: _, substs, .. },
167167
constness: _,
168168
polarity: _,
169-
})) => {
169+
}) => {
170170
for subst in &substs[1..] {
171171
subst.visit_with(&mut collector);
172172
}
173173
}
174-
ty::PredicateKind::Clause(ty::ClauseKind::Projection(ty::ProjectionPredicate {
174+
ty::ClauseKind::Projection(ty::ProjectionPredicate {
175175
projection_ty: ty::AliasTy { substs, .. },
176176
term,
177-
})) => {
177+
}) => {
178178
for subst in &substs[1..] {
179179
subst.visit_with(&mut collector);
180180
}
181181
term.visit_with(&mut collector);
182182
}
183-
ty::PredicateKind::Clause(ty::ClauseKind::TypeOutlives(ty::OutlivesPredicate(
184-
_,
185-
region,
186-
))) => {
183+
ty::ClauseKind::TypeOutlives(ty::OutlivesPredicate(_, region)) => {
187184
region.visit_with(&mut collector);
188185
}
189186
_ => {

compiler/rustc_hir_typeck/src/_match.rs

+6-10
Original file line numberDiff line numberDiff line change
@@ -536,33 +536,29 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
536536
}
537537

538538
for ty in [first_ty, second_ty] {
539-
for (pred, _) in self
539+
for (clause, _) in self
540540
.tcx
541541
.explicit_item_bounds(rpit_def_id)
542542
.subst_iter_copied(self.tcx, substs)
543543
{
544-
let pred = pred.kind().rebind(match pred.kind().skip_binder() {
545-
ty::PredicateKind::Clause(ty::ClauseKind::Trait(trait_pred)) => {
544+
let pred = clause.kind().rebind(match clause.kind().skip_binder() {
545+
ty::ClauseKind::Trait(trait_pred) => {
546546
// FIXME(rpitit): This will need to be fixed when we move to associated types
547547
assert!(matches!(
548548
*trait_pred.trait_ref.self_ty().kind(),
549549
ty::Alias(_, ty::AliasTy { def_id, substs: alias_substs, .. })
550550
if def_id == rpit_def_id && substs == alias_substs
551551
));
552-
ty::PredicateKind::Clause(ty::ClauseKind::Trait(
553-
trait_pred.with_self_ty(self.tcx, ty),
554-
))
552+
ty::ClauseKind::Trait(trait_pred.with_self_ty(self.tcx, ty))
555553
}
556-
ty::PredicateKind::Clause(ty::ClauseKind::Projection(
557-
mut proj_pred,
558-
)) => {
554+
ty::ClauseKind::Projection(mut proj_pred) => {
559555
assert!(matches!(
560556
*proj_pred.projection_ty.self_ty().kind(),
561557
ty::Alias(_, ty::AliasTy { def_id, substs: alias_substs, .. })
562558
if def_id == rpit_def_id && substs == alias_substs
563559
));
564560
proj_pred = proj_pred.with_self_ty(self.tcx, ty);
565-
ty::PredicateKind::Clause(ty::ClauseKind::Projection(proj_pred))
561+
ty::ClauseKind::Projection(proj_pred)
566562
}
567563
_ => continue,
568564
});

compiler/rustc_hir_typeck/src/closure.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
174174
ty::Alias(ty::Opaque, ty::AliasTy { def_id, substs, .. }) => self
175175
.deduce_closure_signature_from_predicates(
176176
expected_ty,
177-
self.tcx.explicit_item_bounds(def_id).subst_iter_copied(self.tcx, substs),
177+
self.tcx
178+
.explicit_item_bounds(def_id)
179+
.subst_iter_copied(self.tcx, substs)
180+
.map(|(c, s)| (c.as_predicate(), s)),
178181
),
179182
ty::Dynamic(ref object_type, ..) => {
180183
let sig = object_type.projection_bounds().find_map(|pb| {
@@ -717,13 +720,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
717720
.tcx
718721
.explicit_item_bounds(def_id)
719722
.subst_iter_copied(self.tcx, substs)
720-
.find_map(|(p, s)| get_future_output(p, s))?,
723+
.find_map(|(p, s)| get_future_output(p.as_predicate(), s))?,
721724
ty::Error(_) => return None,
722725
ty::Alias(ty::Projection, proj) if self.tcx.is_impl_trait_in_trait(proj.def_id) => self
723726
.tcx
724727
.explicit_item_bounds(proj.def_id)
725728
.subst_iter_copied(self.tcx, proj.substs)
726-
.find_map(|(p, s)| get_future_output(p, s))?,
729+
.find_map(|(p, s)| get_future_output(p.as_predicate(), s))?,
727730
_ => span_bug!(
728731
self.tcx.def_span(expr_def_id),
729732
"async fn generator return type not an inference variable: {ret_ty}"

compiler/rustc_hir_typeck/src/generator_interior/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -577,7 +577,7 @@ fn check_must_not_suspend_ty<'tcx>(
577577
let mut has_emitted = false;
578578
for &(predicate, _) in fcx.tcx.explicit_item_bounds(def).skip_binder() {
579579
// We only look at the `DefId`, so it is safe to skip the binder here.
580-
if let ty::PredicateKind::Clause(ty::ClauseKind::Trait(ref poly_trait_predicate)) =
580+
if let ty::ClauseKind::Trait(ref poly_trait_predicate) =
581581
predicate.kind().skip_binder()
582582
{
583583
let def_id = poly_trait_predicate.trait_ref.def_id;

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -408,9 +408,9 @@ impl<'tcx> InferCtxt<'tcx> {
408408
predicate
409409
.kind()
410410
.map_bound(|kind| match kind {
411-
ty::PredicateKind::Clause(ty::ClauseKind::Projection(
412-
projection_predicate,
413-
)) if projection_predicate.projection_ty.def_id == item_def_id => {
411+
ty::ClauseKind::Projection(projection_predicate)
412+
if projection_predicate.projection_ty.def_id == item_def_id =>
413+
{
414414
projection_predicate.term.ty()
415415
}
416416
_ => None,

compiler/rustc_infer/src/infer/opaque_types.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -649,9 +649,7 @@ impl<'tcx> InferCtxt<'tcx> {
649649
ct_op: |ct| ct,
650650
});
651651

652-
if let ty::PredicateKind::Clause(ty::ClauseKind::Projection(projection)) =
653-
predicate.kind().skip_binder()
654-
{
652+
if let ty::ClauseKind::Projection(projection) = predicate.kind().skip_binder() {
655653
if projection.term.references_error() {
656654
// No point on adding any obligations since there's a type error involved.
657655
obligations.clear();

compiler/rustc_infer/src/infer/outlives/verify.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ impl<'cx, 'tcx> VerifyBoundCx<'cx, 'tcx> {
296296
trace!("{:#?}", bounds.skip_binder());
297297
bounds
298298
.subst_iter(tcx, alias_ty.substs)
299-
.filter_map(|p| p.to_opt_type_outlives())
299+
.filter_map(|p| p.as_type_outlives_clause())
300300
.filter_map(|p| p.no_bound_vars())
301301
.map(|OutlivesPredicate(_, r)| r)
302302
}

compiler/rustc_infer/src/traits/util.rs

+22-2
Original file line numberDiff line numberDiff line change
@@ -167,13 +167,33 @@ impl<'tcx> Elaboratable<'tcx> for (ty::Predicate<'tcx>, Span) {
167167
}
168168
}
169169

170+
impl<'tcx> Elaboratable<'tcx> for (ty::Clause<'tcx>, Span) {
171+
fn predicate(&self) -> ty::Predicate<'tcx> {
172+
self.0.as_predicate()
173+
}
174+
175+
fn child(&self, predicate: ty::Predicate<'tcx>) -> Self {
176+
(predicate.expect_clause(), self.1)
177+
}
178+
179+
fn child_with_derived_cause(
180+
&self,
181+
predicate: ty::Predicate<'tcx>,
182+
_span: Span,
183+
_parent_trait_pred: ty::PolyTraitPredicate<'tcx>,
184+
_index: usize,
185+
) -> Self {
186+
(predicate.expect_clause(), self.1)
187+
}
188+
}
189+
170190
impl<'tcx> Elaboratable<'tcx> for ty::Clause<'tcx> {
171191
fn predicate(&self) -> ty::Predicate<'tcx> {
172192
self.as_predicate()
173193
}
174194

175195
fn child(&self, predicate: ty::Predicate<'tcx>) -> Self {
176-
predicate.as_clause().unwrap()
196+
predicate.expect_clause()
177197
}
178198

179199
fn child_with_derived_cause(
@@ -183,7 +203,7 @@ impl<'tcx> Elaboratable<'tcx> for ty::Clause<'tcx> {
183203
_parent_trait_pred: ty::PolyTraitPredicate<'tcx>,
184204
_index: usize,
185205
) -> Self {
186-
predicate.as_clause().unwrap()
206+
predicate.expect_clause()
187207
}
188208
}
189209

compiler/rustc_lint/src/builtin.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -1984,12 +1984,12 @@ declare_lint_pass!(ExplicitOutlivesRequirements => [EXPLICIT_OUTLIVES_REQUIREMEN
19841984

19851985
impl ExplicitOutlivesRequirements {
19861986
fn lifetimes_outliving_lifetime<'tcx>(
1987-
inferred_outlives: &'tcx [(ty::ClauseKind<'tcx>, Span)],
1987+
inferred_outlives: &'tcx [(ty::Clause<'tcx>, Span)],
19881988
def_id: DefId,
19891989
) -> Vec<ty::Region<'tcx>> {
19901990
inferred_outlives
19911991
.iter()
1992-
.filter_map(|(clause, _)| match *clause {
1992+
.filter_map(|(clause, _)| match clause.kind().skip_binder() {
19931993
ty::ClauseKind::RegionOutlives(ty::OutlivesPredicate(a, b)) => match *a {
19941994
ty::ReEarlyBound(ebr) if ebr.def_id == def_id => Some(b),
19951995
_ => None,
@@ -2000,12 +2000,12 @@ impl ExplicitOutlivesRequirements {
20002000
}
20012001

20022002
fn lifetimes_outliving_type<'tcx>(
2003-
inferred_outlives: &'tcx [(ty::ClauseKind<'tcx>, Span)],
2003+
inferred_outlives: &'tcx [(ty::Clause<'tcx>, Span)],
20042004
index: u32,
20052005
) -> Vec<ty::Region<'tcx>> {
20062006
inferred_outlives
20072007
.iter()
2008-
.filter_map(|(clause, _)| match *clause {
2008+
.filter_map(|(clause, _)| match clause.kind().skip_binder() {
20092009
ty::ClauseKind::TypeOutlives(ty::OutlivesPredicate(a, b)) => {
20102010
a.is_param(index).then_some(b)
20112011
}

0 commit comments

Comments
 (0)