Skip to content

Commit

Permalink
Swap Vec<PredicateObligation> to type alias
Browse files Browse the repository at this point in the history
  • Loading branch information
GnomedDev committed Oct 12, 2024
1 parent 1ac72b9 commit 7ec06b0
Show file tree
Hide file tree
Showing 33 changed files with 250 additions and 215 deletions.
4 changes: 2 additions & 2 deletions compiler/rustc_borrowck/src/type_check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use rustc_infer::infer::region_constraints::RegionConstraintData;
use rustc_infer::infer::{
BoundRegion, BoundRegionConversionTime, InferCtxt, NllRegionVariableOrigin,
};
use rustc_infer::traits::PredicateObligations;
use rustc_middle::mir::tcx::PlaceTy;
use rustc_middle::mir::visit::{NonMutatingUseContext, PlaceContext, Visitor};
use rustc_middle::mir::*;
Expand All @@ -40,7 +41,6 @@ use rustc_span::source_map::Spanned;
use rustc_span::symbol::sym;
use rustc_span::{DUMMY_SP, Span};
use rustc_target::abi::{FIRST_VARIANT, FieldIdx};
use rustc_trait_selection::traits::PredicateObligation;
use rustc_trait_selection::traits::query::type_op::custom::{
CustomTypeOp, scrape_region_constraints,
};
Expand Down Expand Up @@ -2940,7 +2940,7 @@ impl NormalizeLocation for Location {
pub(super) struct InstantiateOpaqueType<'tcx> {
pub base_universe: Option<ty::UniverseIndex>,
pub region_constraints: Option<RegionConstraintData<'tcx>>,
pub obligations: Vec<PredicateObligation<'tcx>>,
pub obligations: PredicateObligations<'tcx>,
}

impl<'tcx> TypeOp<'tcx> for InstantiateOpaqueType<'tcx> {
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_data_structures/src/obligation_forest/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -402,9 +402,10 @@ impl<O: ForestObligation> ObligationForest<O> {
}

/// Returns the set of obligations that are in a pending state.
pub fn map_pending_obligations<P, F>(&self, f: F) -> Vec<P>
pub fn map_pending_obligations<P, F, R>(&self, f: F) -> R
where
F: Fn(&O) -> P,
R: FromIterator<P>,
{
self.nodes
.iter()
Expand Down
11 changes: 6 additions & 5 deletions compiler/rustc_hir_analysis/src/autoderef.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use rustc_infer::infer::InferCtxt;
use rustc_infer::traits::PredicateObligations;
use rustc_middle::ty::{self, Ty, TyCtxt, TypeVisitableExt};
use rustc_session::Limit;
use rustc_span::Span;
Expand All @@ -23,7 +24,7 @@ struct AutoderefSnapshot<'tcx> {
reached_recursion_limit: bool,
steps: Vec<(Ty<'tcx>, AutoderefKind)>,
cur_ty: Ty<'tcx>,
obligations: Vec<traits::PredicateObligation<'tcx>>,
obligations: PredicateObligations<'tcx>,
}

pub struct Autoderef<'a, 'tcx> {
Expand Down Expand Up @@ -119,7 +120,7 @@ impl<'a, 'tcx> Autoderef<'a, 'tcx> {
state: AutoderefSnapshot {
steps: vec![],
cur_ty: infcx.resolve_vars_if_possible(base_ty),
obligations: vec![],
obligations: PredicateObligations::new(),
at_start: true,
reached_recursion_limit: false,
},
Expand Down Expand Up @@ -165,7 +166,7 @@ impl<'a, 'tcx> Autoderef<'a, 'tcx> {
pub fn structurally_normalize(
&self,
ty: Ty<'tcx>,
) -> Option<(Ty<'tcx>, Vec<traits::PredicateObligation<'tcx>>)> {
) -> Option<(Ty<'tcx>, PredicateObligations<'tcx>)> {
let ocx = ObligationCtxt::new(self.infcx);
let Ok(normalized_ty) = ocx.structurally_normalize(
&traits::ObligationCause::misc(self.span, self.body_id),
Expand Down Expand Up @@ -204,11 +205,11 @@ impl<'a, 'tcx> Autoderef<'a, 'tcx> {
self.state.steps.len()
}

pub fn into_obligations(self) -> Vec<traits::PredicateObligation<'tcx>> {
pub fn into_obligations(self) -> PredicateObligations<'tcx> {
self.state.obligations
}

pub fn current_obligations(&self) -> Vec<traits::PredicateObligation<'tcx>> {
pub fn current_obligations(&self) -> PredicateObligations<'tcx> {
self.state.obligations.clone()
}

Expand Down
5 changes: 3 additions & 2 deletions compiler/rustc_hir_typeck/src/autoderef.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::iter;
use itertools::Itertools;
use rustc_hir_analysis::autoderef::{Autoderef, AutoderefKind};
use rustc_infer::infer::InferOk;
use rustc_infer::traits::PredicateObligations;
use rustc_middle::ty::adjustment::{Adjust, Adjustment, OverloadedDeref};
use rustc_middle::ty::{self, Ty};
use rustc_span::Span;
Expand Down Expand Up @@ -36,10 +37,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
) -> InferOk<'tcx, Vec<Adjustment<'tcx>>> {
let steps = autoderef.steps();
if steps.is_empty() {
return InferOk { obligations: vec![], value: vec![] };
return InferOk { obligations: PredicateObligations::new(), value: vec![] };
}

let mut obligations = vec![];
let mut obligations = PredicateObligations::new();
let targets =
steps.iter().skip(1).map(|&(ty, _)| ty).chain(iter::once(autoderef.final_ty(false)));
let steps: Vec<_> = steps
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_hir_typeck/src/closure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use rustc_hir as hir;
use rustc_hir::lang_items::LangItem;
use rustc_hir_analysis::hir_ty_lowering::HirTyLowerer;
use rustc_infer::infer::{BoundRegionConversionTime, DefineOpaqueTypes, InferOk, InferResult};
use rustc_infer::traits::ObligationCauseCode;
use rustc_infer::traits::{ObligationCauseCode, PredicateObligations};
use rustc_macros::{TypeFoldable, TypeVisitable};
use rustc_middle::span_bug;
use rustc_middle::ty::visit::{TypeVisitable, TypeVisitableExt};
Expand Down Expand Up @@ -805,7 +805,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// [c1]: https://github.com/rust-lang/rust/pull/45072#issuecomment-341089706
// [c2]: https://github.com/rust-lang/rust/pull/45072#issuecomment-341096796
self.commit_if_ok(|_| {
let mut all_obligations = vec![];
let mut all_obligations = PredicateObligations::new();
let supplied_sig = self.instantiate_binder_with_fresh_vars(
self.tcx.def_span(expr_def_id),
BoundRegionConversionTime::FnCall,
Expand Down
33 changes: 17 additions & 16 deletions compiler/rustc_hir_typeck/src/coercion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ use rustc_infer::infer::relate::RelateResult;
use rustc_infer::infer::{Coercion, DefineOpaqueTypes, InferOk, InferResult};
use rustc_infer::traits::{
IfExpressionCause, MatchExpressionArmCause, Obligation, PredicateObligation,
PredicateObligations,
};
use rustc_middle::lint::in_external_macro;
use rustc_middle::span_bug;
Expand Down Expand Up @@ -120,7 +121,7 @@ fn simple<'tcx>(kind: Adjust<'tcx>) -> impl FnOnce(Ty<'tcx>) -> Vec<Adjustment<'
fn success<'tcx>(
adj: Vec<Adjustment<'tcx>>,
target: Ty<'tcx>,
obligations: Vec<traits::PredicateObligation<'tcx>>,
obligations: PredicateObligations<'tcx>,
) -> CoerceResult<'tcx> {
Ok(InferOk { value: (adj, target), obligations })
}
Expand Down Expand Up @@ -184,7 +185,7 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
// Coercing from `!` to any type is allowed:
if a.is_never() {
if self.coerce_never {
return success(simple(Adjust::NeverToAny)(b), b, vec![]);
return success(simple(Adjust::NeverToAny)(b), b, PredicateObligations::new());
} else {
// Otherwise the only coercion we can do is unification.
return self.unify_and(a, b, identity);
Expand Down Expand Up @@ -278,7 +279,7 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
// Two unresolved type variables: create a `Coerce` predicate.
let target_ty = if self.use_lub { self.next_ty_var(self.cause.span) } else { b };

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

// Check the obligations of the cast -- for example, when casting
// `usize` to `dyn* Clone + 'static`:
let mut obligations: Vec<_> = predicates
let obligations = predicates
.iter()
.map(|predicate| {
// For each existential predicate (e.g., `?Self: Clone`) instantiate
Expand All @@ -764,21 +765,21 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
ty::OutlivesPredicate(a, b_region),
))),
),
// Enforce that the type is `usize`/pointer-sized.
Obligation::new(
self.tcx,
self.cause.clone(),
self.param_env,
ty::TraitRef::new(
self.tcx,
self.tcx
.require_lang_item(hir::LangItem::PointerLike, Some(self.cause.span)),
[a],
),
),
])
.collect();

// Enforce that the type is `usize`/pointer-sized.
obligations.push(Obligation::new(
self.tcx,
self.cause.clone(),
self.param_env,
ty::TraitRef::new(
self.tcx,
self.tcx.require_lang_item(hir::LangItem::PointerLike, Some(self.cause.span)),
[a],
),
));

Ok(InferOk {
value: (
vec![Adjustment { kind: Adjust::Pointer(PointerCoercion::DynStar), target: b }],
Expand Down
13 changes: 5 additions & 8 deletions compiler/rustc_hir_typeck/src/fn_ctxt/inspect_obligations.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! A utility module to inspect currently ambiguous obligations in the current context.
use rustc_infer::traits::{self, ObligationCause};
use rustc_infer::traits::{self, ObligationCause, PredicateObligations};
use rustc_middle::traits::solve::Goal;
use rustc_middle::ty::{self, Ty, TypeVisitableExt};
use rustc_span::Span;
Expand All @@ -15,10 +15,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
/// Returns a list of all obligations whose self type has been unified
/// with the unconstrained type `self_ty`.
#[instrument(skip(self), level = "debug")]
pub(crate) fn obligations_for_self_ty(
&self,
self_ty: ty::TyVid,
) -> Vec<traits::PredicateObligation<'tcx>> {
pub(crate) fn obligations_for_self_ty(&self, self_ty: ty::TyVid) -> PredicateObligations<'tcx> {
if self.next_trait_solver() {
self.obligations_for_self_ty_next(self_ty)
} else {
Expand Down Expand Up @@ -75,10 +72,10 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
pub(crate) fn obligations_for_self_ty_next(
&self,
self_ty: ty::TyVid,
) -> Vec<traits::PredicateObligation<'tcx>> {
) -> PredicateObligations<'tcx> {
let obligations = self.fulfillment_cx.borrow().pending_obligations();
debug!(?obligations);
let mut obligations_for_self_ty = vec![];
let mut obligations_for_self_ty = PredicateObligations::new();
for obligation in obligations {
let mut visitor = NestedObligationsForSelfTy {
fcx: self,
Expand All @@ -103,7 +100,7 @@ struct NestedObligationsForSelfTy<'a, 'tcx> {
fcx: &'a FnCtxt<'a, 'tcx>,
self_ty: ty::TyVid,
root_cause: &'a ObligationCause<'tcx>,
obligations_for_self_ty: &'a mut Vec<traits::PredicateObligation<'tcx>>,
obligations_for_self_ty: &'a mut PredicateObligations<'tcx>,
}

impl<'a, 'tcx> ProofTreeVisitor<'tcx> for NestedObligationsForSelfTy<'a, 'tcx> {
Expand Down
3 changes: 2 additions & 1 deletion compiler/rustc_hir_typeck/src/method/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use rustc_hir as hir;
use rustc_hir::def::{CtorOf, DefKind, Namespace};
use rustc_hir::def_id::DefId;
use rustc_infer::infer::{self, InferOk};
use rustc_infer::traits::PredicateObligations;
use rustc_middle::query::Providers;
use rustc_middle::traits::ObligationCause;
use rustc_middle::ty::{
Expand Down Expand Up @@ -412,7 +413,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
}

debug!("lookup_in_trait_adjusted: method_item={:?}", method_item);
let mut obligations = vec![];
let mut obligations = PredicateObligations::new();

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

Expand Down
7 changes: 4 additions & 3 deletions compiler/rustc_infer/src/infer/canonical/query_response.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ use crate::infer::region_constraints::{Constraint, RegionConstraintData};
use crate::infer::{DefineOpaqueTypes, InferCtxt, InferOk, InferResult};
use crate::traits::query::NoSolution;
use crate::traits::{
Obligation, ObligationCause, PredicateObligation, ScrubbedTraitError, TraitEngine,
Obligation, ObligationCause, PredicateObligation, PredicateObligations, ScrubbedTraitError,
TraitEngine,
};

impl<'tcx> InferCtxt<'tcx> {
Expand Down Expand Up @@ -493,7 +494,7 @@ impl<'tcx> InferCtxt<'tcx> {
),
};

let mut obligations = vec![];
let mut obligations = PredicateObligations::new();

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

Expand Down
10 changes: 6 additions & 4 deletions compiler/rustc_infer/src/infer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ use tracing::{debug, instrument};
use type_variable::TypeVariableOrigin;

use crate::infer::region_constraints::UndoLog;
use crate::traits::{self, ObligationCause, ObligationInspector, PredicateObligation, TraitEngine};
use crate::traits::{
self, ObligationCause, ObligationInspector, PredicateObligations, TraitEngine,
};

pub mod at;
pub mod canonical;
Expand All @@ -68,7 +70,7 @@ pub(crate) mod snapshot;
mod type_variable;

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

Expand Down Expand Up @@ -658,7 +660,7 @@ impl<'tcx, T> InferOk<'tcx, T> {
}

impl<'tcx> InferOk<'tcx, ()> {
pub fn into_obligations(self) -> Vec<PredicateObligation<'tcx>> {
pub fn into_obligations(self) -> PredicateObligations<'tcx> {
self.obligations
}
}
Expand Down
8 changes: 4 additions & 4 deletions compiler/rustc_infer/src/infer/opaque_types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use tracing::{debug, instrument};
use super::DefineOpaqueTypes;
use crate::errors::OpaqueHiddenTypeDiag;
use crate::infer::{InferCtxt, InferOk};
use crate::traits::{self, Obligation};
use crate::traits::{self, Obligation, PredicateObligations};

mod table;

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

if !value.has_opaque_types() {
return InferOk { value, obligations: vec![] };
return InferOk { value, obligations: PredicateObligations::new() };
}

let mut obligations = vec![];
let mut obligations = PredicateObligations::new();
let value = value.fold_with(&mut BottomUpFolder {
tcx: self.tcx,
lt_op: |lt| lt,
Expand Down
4 changes: 2 additions & 2 deletions compiler/rustc_infer/src/infer/projection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use rustc_middle::traits::ObligationCause;
use rustc_middle::ty::{self, Ty};

use super::InferCtxt;
use crate::traits::{Obligation, PredicateObligation};
use crate::traits::{Obligation, PredicateObligations};

impl<'tcx> InferCtxt<'tcx> {
/// Instead of normalizing an associated type projection,
Expand All @@ -17,7 +17,7 @@ impl<'tcx> InferCtxt<'tcx> {
projection_ty: ty::AliasTy<'tcx>,
cause: ObligationCause<'tcx>,
recursion_depth: usize,
obligations: &mut Vec<PredicateObligation<'tcx>>,
obligations: &mut PredicateObligations<'tcx>,
) -> Ty<'tcx> {
debug_assert!(!self.next_trait_solver());
let ty_var = self.next_ty_var(self.tcx.def_span(projection_ty.def_id));
Expand Down
Loading

0 comments on commit 7ec06b0

Please sign in to comment.