Skip to content

Commit 7ec06b0

Browse files
committed
Swap Vec<PredicateObligation> to type alias
1 parent 1ac72b9 commit 7ec06b0

File tree

33 files changed

+250
-215
lines changed

33 files changed

+250
-215
lines changed

compiler/rustc_borrowck/src/type_check/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use rustc_infer::infer::region_constraints::RegionConstraintData;
1818
use rustc_infer::infer::{
1919
BoundRegion, BoundRegionConversionTime, InferCtxt, NllRegionVariableOrigin,
2020
};
21+
use rustc_infer::traits::PredicateObligations;
2122
use rustc_middle::mir::tcx::PlaceTy;
2223
use rustc_middle::mir::visit::{NonMutatingUseContext, PlaceContext, Visitor};
2324
use rustc_middle::mir::*;
@@ -40,7 +41,6 @@ use rustc_span::source_map::Spanned;
4041
use rustc_span::symbol::sym;
4142
use rustc_span::{DUMMY_SP, Span};
4243
use rustc_target::abi::{FIRST_VARIANT, FieldIdx};
43-
use rustc_trait_selection::traits::PredicateObligation;
4444
use rustc_trait_selection::traits::query::type_op::custom::{
4545
CustomTypeOp, scrape_region_constraints,
4646
};
@@ -2940,7 +2940,7 @@ impl NormalizeLocation for Location {
29402940
pub(super) struct InstantiateOpaqueType<'tcx> {
29412941
pub base_universe: Option<ty::UniverseIndex>,
29422942
pub region_constraints: Option<RegionConstraintData<'tcx>>,
2943-
pub obligations: Vec<PredicateObligation<'tcx>>,
2943+
pub obligations: PredicateObligations<'tcx>,
29442944
}
29452945

29462946
impl<'tcx> TypeOp<'tcx> for InstantiateOpaqueType<'tcx> {

compiler/rustc_data_structures/src/obligation_forest/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -402,9 +402,10 @@ impl<O: ForestObligation> ObligationForest<O> {
402402
}
403403

404404
/// Returns the set of obligations that are in a pending state.
405-
pub fn map_pending_obligations<P, F>(&self, f: F) -> Vec<P>
405+
pub fn map_pending_obligations<P, F, R>(&self, f: F) -> R
406406
where
407407
F: Fn(&O) -> P,
408+
R: FromIterator<P>,
408409
{
409410
self.nodes
410411
.iter()

compiler/rustc_hir_analysis/src/autoderef.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use rustc_infer::infer::InferCtxt;
2+
use rustc_infer::traits::PredicateObligations;
23
use rustc_middle::ty::{self, Ty, TyCtxt, TypeVisitableExt};
34
use rustc_session::Limit;
45
use rustc_span::Span;
@@ -23,7 +24,7 @@ struct AutoderefSnapshot<'tcx> {
2324
reached_recursion_limit: bool,
2425
steps: Vec<(Ty<'tcx>, AutoderefKind)>,
2526
cur_ty: Ty<'tcx>,
26-
obligations: Vec<traits::PredicateObligation<'tcx>>,
27+
obligations: PredicateObligations<'tcx>,
2728
}
2829

2930
pub struct Autoderef<'a, 'tcx> {
@@ -119,7 +120,7 @@ impl<'a, 'tcx> Autoderef<'a, 'tcx> {
119120
state: AutoderefSnapshot {
120121
steps: vec![],
121122
cur_ty: infcx.resolve_vars_if_possible(base_ty),
122-
obligations: vec![],
123+
obligations: PredicateObligations::new(),
123124
at_start: true,
124125
reached_recursion_limit: false,
125126
},
@@ -165,7 +166,7 @@ impl<'a, 'tcx> Autoderef<'a, 'tcx> {
165166
pub fn structurally_normalize(
166167
&self,
167168
ty: Ty<'tcx>,
168-
) -> Option<(Ty<'tcx>, Vec<traits::PredicateObligation<'tcx>>)> {
169+
) -> Option<(Ty<'tcx>, PredicateObligations<'tcx>)> {
169170
let ocx = ObligationCtxt::new(self.infcx);
170171
let Ok(normalized_ty) = ocx.structurally_normalize(
171172
&traits::ObligationCause::misc(self.span, self.body_id),
@@ -204,11 +205,11 @@ impl<'a, 'tcx> Autoderef<'a, 'tcx> {
204205
self.state.steps.len()
205206
}
206207

207-
pub fn into_obligations(self) -> Vec<traits::PredicateObligation<'tcx>> {
208+
pub fn into_obligations(self) -> PredicateObligations<'tcx> {
208209
self.state.obligations
209210
}
210211

211-
pub fn current_obligations(&self) -> Vec<traits::PredicateObligation<'tcx>> {
212+
pub fn current_obligations(&self) -> PredicateObligations<'tcx> {
212213
self.state.obligations.clone()
213214
}
214215

compiler/rustc_hir_typeck/src/autoderef.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::iter;
55
use itertools::Itertools;
66
use rustc_hir_analysis::autoderef::{Autoderef, AutoderefKind};
77
use rustc_infer::infer::InferOk;
8+
use rustc_infer::traits::PredicateObligations;
89
use rustc_middle::ty::adjustment::{Adjust, Adjustment, OverloadedDeref};
910
use rustc_middle::ty::{self, Ty};
1011
use rustc_span::Span;
@@ -36,10 +37,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
3637
) -> InferOk<'tcx, Vec<Adjustment<'tcx>>> {
3738
let steps = autoderef.steps();
3839
if steps.is_empty() {
39-
return InferOk { obligations: vec![], value: vec![] };
40+
return InferOk { obligations: PredicateObligations::new(), value: vec![] };
4041
}
4142

42-
let mut obligations = vec![];
43+
let mut obligations = PredicateObligations::new();
4344
let targets =
4445
steps.iter().skip(1).map(|&(ty, _)| ty).chain(iter::once(autoderef.final_ty(false)));
4546
let steps: Vec<_> = steps

compiler/rustc_hir_typeck/src/closure.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use rustc_hir as hir;
88
use rustc_hir::lang_items::LangItem;
99
use rustc_hir_analysis::hir_ty_lowering::HirTyLowerer;
1010
use rustc_infer::infer::{BoundRegionConversionTime, DefineOpaqueTypes, InferOk, InferResult};
11-
use rustc_infer::traits::ObligationCauseCode;
11+
use rustc_infer::traits::{ObligationCauseCode, PredicateObligations};
1212
use rustc_macros::{TypeFoldable, TypeVisitable};
1313
use rustc_middle::span_bug;
1414
use rustc_middle::ty::visit::{TypeVisitable, TypeVisitableExt};
@@ -805,7 +805,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
805805
// [c1]: https://github.com/rust-lang/rust/pull/45072#issuecomment-341089706
806806
// [c2]: https://github.com/rust-lang/rust/pull/45072#issuecomment-341096796
807807
self.commit_if_ok(|_| {
808-
let mut all_obligations = vec![];
808+
let mut all_obligations = PredicateObligations::new();
809809
let supplied_sig = self.instantiate_binder_with_fresh_vars(
810810
self.tcx.def_span(expr_def_id),
811811
BoundRegionConversionTime::FnCall,

compiler/rustc_hir_typeck/src/coercion.rs

+17-16
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ use rustc_infer::infer::relate::RelateResult;
4646
use rustc_infer::infer::{Coercion, DefineOpaqueTypes, InferOk, InferResult};
4747
use rustc_infer::traits::{
4848
IfExpressionCause, MatchExpressionArmCause, Obligation, PredicateObligation,
49+
PredicateObligations,
4950
};
5051
use rustc_middle::lint::in_external_macro;
5152
use rustc_middle::span_bug;
@@ -120,7 +121,7 @@ fn simple<'tcx>(kind: Adjust<'tcx>) -> impl FnOnce(Ty<'tcx>) -> Vec<Adjustment<'
120121
fn success<'tcx>(
121122
adj: Vec<Adjustment<'tcx>>,
122123
target: Ty<'tcx>,
123-
obligations: Vec<traits::PredicateObligation<'tcx>>,
124+
obligations: PredicateObligations<'tcx>,
124125
) -> CoerceResult<'tcx> {
125126
Ok(InferOk { value: (adj, target), obligations })
126127
}
@@ -184,7 +185,7 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
184185
// Coercing from `!` to any type is allowed:
185186
if a.is_never() {
186187
if self.coerce_never {
187-
return success(simple(Adjust::NeverToAny)(b), b, vec![]);
188+
return success(simple(Adjust::NeverToAny)(b), b, PredicateObligations::new());
188189
} else {
189190
// Otherwise the only coercion we can do is unification.
190191
return self.unify_and(a, b, identity);
@@ -278,7 +279,7 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
278279
// Two unresolved type variables: create a `Coerce` predicate.
279280
let target_ty = if self.use_lub { self.next_ty_var(self.cause.span) } else { b };
280281

281-
let mut obligations = Vec::with_capacity(2);
282+
let mut obligations = PredicateObligations::with_capacity(2);
282283
for &source_ty in &[a, b] {
283284
if source_ty != target_ty {
284285
obligations.push(Obligation::new(
@@ -744,7 +745,7 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
744745

745746
// Check the obligations of the cast -- for example, when casting
746747
// `usize` to `dyn* Clone + 'static`:
747-
let mut obligations: Vec<_> = predicates
748+
let obligations = predicates
748749
.iter()
749750
.map(|predicate| {
750751
// For each existential predicate (e.g., `?Self: Clone`) instantiate
@@ -764,21 +765,21 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
764765
ty::OutlivesPredicate(a, b_region),
765766
))),
766767
),
768+
// Enforce that the type is `usize`/pointer-sized.
769+
Obligation::new(
770+
self.tcx,
771+
self.cause.clone(),
772+
self.param_env,
773+
ty::TraitRef::new(
774+
self.tcx,
775+
self.tcx
776+
.require_lang_item(hir::LangItem::PointerLike, Some(self.cause.span)),
777+
[a],
778+
),
779+
),
767780
])
768781
.collect();
769782

770-
// Enforce that the type is `usize`/pointer-sized.
771-
obligations.push(Obligation::new(
772-
self.tcx,
773-
self.cause.clone(),
774-
self.param_env,
775-
ty::TraitRef::new(
776-
self.tcx,
777-
self.tcx.require_lang_item(hir::LangItem::PointerLike, Some(self.cause.span)),
778-
[a],
779-
),
780-
));
781-
782783
Ok(InferOk {
783784
value: (
784785
vec![Adjustment { kind: Adjust::Pointer(PointerCoercion::DynStar), target: b }],

compiler/rustc_hir_typeck/src/fn_ctxt/inspect_obligations.rs

+5-8
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! A utility module to inspect currently ambiguous obligations in the current context.
22
3-
use rustc_infer::traits::{self, ObligationCause};
3+
use rustc_infer::traits::{self, ObligationCause, PredicateObligations};
44
use rustc_middle::traits::solve::Goal;
55
use rustc_middle::ty::{self, Ty, TypeVisitableExt};
66
use rustc_span::Span;
@@ -15,10 +15,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
1515
/// Returns a list of all obligations whose self type has been unified
1616
/// with the unconstrained type `self_ty`.
1717
#[instrument(skip(self), level = "debug")]
18-
pub(crate) fn obligations_for_self_ty(
19-
&self,
20-
self_ty: ty::TyVid,
21-
) -> Vec<traits::PredicateObligation<'tcx>> {
18+
pub(crate) fn obligations_for_self_ty(&self, self_ty: ty::TyVid) -> PredicateObligations<'tcx> {
2219
if self.next_trait_solver() {
2320
self.obligations_for_self_ty_next(self_ty)
2421
} else {
@@ -75,10 +72,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
7572
pub(crate) fn obligations_for_self_ty_next(
7673
&self,
7774
self_ty: ty::TyVid,
78-
) -> Vec<traits::PredicateObligation<'tcx>> {
75+
) -> PredicateObligations<'tcx> {
7976
let obligations = self.fulfillment_cx.borrow().pending_obligations();
8077
debug!(?obligations);
81-
let mut obligations_for_self_ty = vec![];
78+
let mut obligations_for_self_ty = PredicateObligations::new();
8279
for obligation in obligations {
8380
let mut visitor = NestedObligationsForSelfTy {
8481
fcx: self,
@@ -103,7 +100,7 @@ struct NestedObligationsForSelfTy<'a, 'tcx> {
103100
fcx: &'a FnCtxt<'a, 'tcx>,
104101
self_ty: ty::TyVid,
105102
root_cause: &'a ObligationCause<'tcx>,
106-
obligations_for_self_ty: &'a mut Vec<traits::PredicateObligation<'tcx>>,
103+
obligations_for_self_ty: &'a mut PredicateObligations<'tcx>,
107104
}
108105

109106
impl<'a, 'tcx> ProofTreeVisitor<'tcx> for NestedObligationsForSelfTy<'a, 'tcx> {

compiler/rustc_hir_typeck/src/method/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use rustc_hir as hir;
1212
use rustc_hir::def::{CtorOf, DefKind, Namespace};
1313
use rustc_hir::def_id::DefId;
1414
use rustc_infer::infer::{self, InferOk};
15+
use rustc_infer::traits::PredicateObligations;
1516
use rustc_middle::query::Providers;
1617
use rustc_middle::traits::ObligationCause;
1718
use rustc_middle::ty::{
@@ -412,7 +413,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
412413
}
413414

414415
debug!("lookup_in_trait_adjusted: method_item={:?}", method_item);
415-
let mut obligations = vec![];
416+
let mut obligations = PredicateObligations::new();
416417

417418
// FIXME(effects): revisit when binops get `#[const_trait]`
418419

compiler/rustc_infer/src/infer/canonical/query_response.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ use crate::infer::region_constraints::{Constraint, RegionConstraintData};
2828
use crate::infer::{DefineOpaqueTypes, InferCtxt, InferOk, InferResult};
2929
use crate::traits::query::NoSolution;
3030
use crate::traits::{
31-
Obligation, ObligationCause, PredicateObligation, ScrubbedTraitError, TraitEngine,
31+
Obligation, ObligationCause, PredicateObligation, PredicateObligations, ScrubbedTraitError,
32+
TraitEngine,
3233
};
3334

3435
impl<'tcx> InferCtxt<'tcx> {
@@ -493,7 +494,7 @@ impl<'tcx> InferCtxt<'tcx> {
493494
),
494495
};
495496

496-
let mut obligations = vec![];
497+
let mut obligations = PredicateObligations::new();
497498

498499
// Carry all newly resolved opaque types to the caller's scope
499500
for &(a, b) in &query_response.value.opaque_types {
@@ -598,7 +599,7 @@ impl<'tcx> InferCtxt<'tcx> {
598599
variables1: &OriginalQueryValues<'tcx>,
599600
variables2: impl Fn(BoundVar) -> GenericArg<'tcx>,
600601
) -> InferResult<'tcx, ()> {
601-
let mut obligations = vec![];
602+
let mut obligations = PredicateObligations::new();
602603
for (index, value1) in variables1.var_values.iter().enumerate() {
603604
let value2 = variables2(BoundVar::new(index));
604605

compiler/rustc_infer/src/infer/mod.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ use tracing::{debug, instrument};
5050
use type_variable::TypeVariableOrigin;
5151

5252
use crate::infer::region_constraints::UndoLog;
53-
use crate::traits::{self, ObligationCause, ObligationInspector, PredicateObligation, TraitEngine};
53+
use crate::traits::{
54+
self, ObligationCause, ObligationInspector, PredicateObligations, TraitEngine,
55+
};
5456

5557
pub mod at;
5658
pub mod canonical;
@@ -68,7 +70,7 @@ pub(crate) mod snapshot;
6870
mod type_variable;
6971

7072
/// `InferOk<'tcx, ()>` is used a lot. It may seem like a useless wrapper
71-
/// around `Vec<PredicateObligation<'tcx>>`, but it has one important property:
73+
/// around `PredicateObligations<'tcx>`, but it has one important property:
7274
/// because `InferOk` is marked with `#[must_use]`, if you have a method
7375
/// `InferCtxt::f` that returns `InferResult<'tcx, ()>` and you call it with
7476
/// `infcx.f()?;` you'll get a warning about the obligations being discarded
@@ -78,7 +80,7 @@ mod type_variable;
7880
#[derive(Debug)]
7981
pub struct InferOk<'tcx, T> {
8082
pub value: T,
81-
pub obligations: Vec<PredicateObligation<'tcx>>,
83+
pub obligations: PredicateObligations<'tcx>,
8284
}
8385
pub type InferResult<'tcx, T> = Result<InferOk<'tcx, T>, TypeError<'tcx>>;
8486

@@ -658,7 +660,7 @@ impl<'tcx, T> InferOk<'tcx, T> {
658660
}
659661

660662
impl<'tcx> InferOk<'tcx, ()> {
661-
pub fn into_obligations(self) -> Vec<PredicateObligation<'tcx>> {
663+
pub fn into_obligations(self) -> PredicateObligations<'tcx> {
662664
self.obligations
663665
}
664666
}

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use tracing::{debug, instrument};
1616
use super::DefineOpaqueTypes;
1717
use crate::errors::OpaqueHiddenTypeDiag;
1818
use crate::infer::{InferCtxt, InferOk};
19-
use crate::traits::{self, Obligation};
19+
use crate::traits::{self, Obligation, PredicateObligations};
2020

2121
mod table;
2222

@@ -46,14 +46,14 @@ impl<'tcx> InferCtxt<'tcx> {
4646
) -> InferOk<'tcx, T> {
4747
// We handle opaque types differently in the new solver.
4848
if self.next_trait_solver() {
49-
return InferOk { value, obligations: vec![] };
49+
return InferOk { value, obligations: PredicateObligations::new() };
5050
}
5151

5252
if !value.has_opaque_types() {
53-
return InferOk { value, obligations: vec![] };
53+
return InferOk { value, obligations: PredicateObligations::new() };
5454
}
5555

56-
let mut obligations = vec![];
56+
let mut obligations = PredicateObligations::new();
5757
let value = value.fold_with(&mut BottomUpFolder {
5858
tcx: self.tcx,
5959
lt_op: |lt| lt,

compiler/rustc_infer/src/infer/projection.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use rustc_middle::traits::ObligationCause;
22
use rustc_middle::ty::{self, Ty};
33

44
use super::InferCtxt;
5-
use crate::traits::{Obligation, PredicateObligation};
5+
use crate::traits::{Obligation, PredicateObligations};
66

77
impl<'tcx> InferCtxt<'tcx> {
88
/// Instead of normalizing an associated type projection,
@@ -17,7 +17,7 @@ impl<'tcx> InferCtxt<'tcx> {
1717
projection_ty: ty::AliasTy<'tcx>,
1818
cause: ObligationCause<'tcx>,
1919
recursion_depth: usize,
20-
obligations: &mut Vec<PredicateObligation<'tcx>>,
20+
obligations: &mut PredicateObligations<'tcx>,
2121
) -> Ty<'tcx> {
2222
debug_assert!(!self.next_trait_solver());
2323
let ty_var = self.next_ty_var(self.tcx.def_span(projection_ty.def_id));

0 commit comments

Comments
 (0)