Skip to content

Commit

Permalink
Derive traversable impls for Obligation
Browse files Browse the repository at this point in the history
Ever since folding was first added in c5754f3, obligations have not
folded or visited their causes the ObligationCauseCode potentially
containing types that may be of interest to a folder or visitor.

By using the derive macro to implement the visitable traits, we ensure
that all contained types of interest are folded/visited.
  • Loading branch information
eggyal committed Mar 21, 2023
1 parent 408ba98 commit ef97776
Show file tree
Hide file tree
Showing 2 changed files with 2 additions and 33 deletions.
2 changes: 1 addition & 1 deletion compiler/rustc_infer/src/traits/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ pub use rustc_middle::traits::*;
/// 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, PartialEq, Eq, Hash)]
#[derive(Clone, PartialEq, Eq, Hash, TypeFoldable, TypeVisitable)]
pub struct Obligation<'tcx, T> {
/// The reason we have to prove this thing.
pub cause: ObligationCause<'tcx>,
Expand Down
33 changes: 1 addition & 32 deletions compiler/rustc_infer/src/traits/structural_impls.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
use crate::traits;
use crate::traits::project::Normalized;
use rustc_middle::ty::fold::{FallibleTypeFolder, TypeFoldable};
use rustc_middle::ty::visit::{TypeVisitable, TypeVisitor};
use rustc_middle::ty::{self, TyCtxt};
use rustc_middle::ty;

use std::fmt;
use std::ops::ControlFlow;

// Structural impls for the structs in `traits`.

Expand Down Expand Up @@ -57,31 +54,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) -> ControlFlow<V::BreakTy> {
self.predicate.visit_with(visitor)?;
self.param_env.visit_with(visitor)
}
}

0 comments on commit ef97776

Please sign in to comment.