Derive Obligation's fold impls

This commit is contained in:
Michael Goulet 2025-04-13 22:43:16 +00:00
parent c580c498a1
commit 13b4734e31
2 changed files with 7 additions and 32 deletions

View file

@ -12,6 +12,7 @@ use std::hash::{Hash, Hasher};
use hir::def_id::LocalDefId;
use rustc_hir as hir;
use rustc_macros::{TypeFoldable, TypeVisitable};
use rustc_middle::traits::query::NoSolution;
use rustc_middle::traits::solve::Certainty;
pub use rustc_middle::traits::*;
@ -35,9 +36,11 @@ use crate::infer::InferCtxt;
/// either identifying an `impl` (e.g., `impl Eq for i32`) that
/// satisfies the obligation, or else finding a bound that is in
/// scope. The eventual result is usually a `Selection` (defined below).
#[derive(Clone)]
#[derive(Clone, TypeFoldable, TypeVisitable)]
pub struct Obligation<'tcx, T> {
/// The reason we have to prove this thing.
#[type_foldable(identity)]
#[type_visitable(ignore)]
pub cause: ObligationCause<'tcx>,
/// The environment in which we should prove this thing.
@ -51,6 +54,8 @@ pub struct Obligation<'tcx, T> {
/// If this goes over a certain threshold, we abort compilation --
/// in such cases, we can not say whether or not the predicate
/// holds for certain. Stupid halting problem; such a drag.
#[type_foldable(identity)]
#[type_visitable(ignore)]
pub recursion_depth: usize,
}

View file

@ -1,8 +1,6 @@
use std::fmt;
use rustc_middle::ty::{
self, FallibleTypeFolder, TyCtxt, TypeFoldable, TypeVisitable, TypeVisitor, try_visit,
};
use rustc_middle::ty;
use crate::traits;
use crate::traits::project::Normalized;
@ -34,31 +32,3 @@ impl<'tcx> fmt::Debug for traits::MismatchedProjectionTypes<'tcx> {
write!(f, "MismatchedProjectionTypes({:?})", self.err)
}
}
///////////////////////////////////////////////////////////////////////////
// TypeFoldable implementations.
impl<'tcx, O: TypeFoldable<TyCtxt<'tcx>>> TypeFoldable<TyCtxt<'tcx>>
for traits::Obligation<'tcx, O>
{
fn try_fold_with<F: FallibleTypeFolder<TyCtxt<'tcx>>>(
self,
folder: &mut F,
) -> Result<Self, F::Error> {
Ok(traits::Obligation {
cause: self.cause,
recursion_depth: self.recursion_depth,
predicate: self.predicate.try_fold_with(folder)?,
param_env: self.param_env.try_fold_with(folder)?,
})
}
}
impl<'tcx, O: TypeVisitable<TyCtxt<'tcx>>> TypeVisitable<TyCtxt<'tcx>>
for traits::Obligation<'tcx, O>
{
fn visit_with<V: TypeVisitor<TyCtxt<'tcx>>>(&self, visitor: &mut V) -> V::Result {
try_visit!(self.predicate.visit_with(visitor));
self.param_env.visit_with(visitor)
}
}