Skip to content

Commit

Permalink
Store inference context alongside captured obligations.
Browse files Browse the repository at this point in the history
  • Loading branch information
gavinleroy committed Jan 16, 2024
1 parent b8dc663 commit 998644f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
1 change: 1 addition & 0 deletions compiler/rustc_infer/src/infer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,7 @@ impl<'tcx> InferCtxtInner<'tcx> {
}
}

#[derive(Clone)]
pub struct InferCtxt<'tcx> {
pub tcx: TyCtxt<'tcx>,

Expand Down
5 changes: 3 additions & 2 deletions compiler/rustc_infer/src/traits/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use rustc_span::Span;
pub use self::FulfillmentErrorCode::*;
pub use self::ImplSource::*;
pub use self::SelectionError::*;
use crate::infer::InferCtxt;

pub use self::engine::{TraitEngine, TraitEngineExt};
pub use self::project::MismatchedProjectionTypes;
Expand Down Expand Up @@ -110,8 +111,8 @@ impl<'tcx> PolyTraitObligation<'tcx> {
}

pub enum FulfilledObligation<'tcx> {
Success(PredicateObligation<'tcx>),
Failure(FulfillmentError<'tcx>),
Success { infcx: InferCtxt<'tcx>, data: PredicateObligation<'tcx> },
Failed { infcx: InferCtxt<'tcx>, data: Vec<FulfillmentError<'tcx>> },
}

// `PredicateObligation` is used a lot. Make sure it doesn't unintentionally get bigger.
Expand Down
25 changes: 17 additions & 8 deletions compiler/rustc_trait_selection/src/solve/fulfill.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,27 @@ impl<'tcx> FulfillmentCtxt<'tcx> {

fn track_fulfillment_errors<'b, 'a: 'b>(
&'a self,
infcx: &InferCtxt<'tcx>,
errors: impl IntoIterator<Item = &'b FulfillmentError<'tcx>>,
) {
if let Some(tracked_obligations) = &self.tracked_obligations {
tracked_obligations.borrow_mut().extend(
errors.into_iter().map(|error| FulfilledObligation::Failure(error.clone())),
);
tracked_obligations.borrow_mut().push(FulfilledObligation::Failed {
infcx: infcx.clone(),
data: errors.into_iter().map(Clone::clone).collect::<Vec<_>>(),
});
}
}

fn track_fulfillment_success(&self, predicate: &PredicateObligation<'tcx>) {
fn track_fulfillment_success(
&self,
infcx: &InferCtxt<'tcx>,
predicate: &PredicateObligation<'tcx>,
) {
if let Some(tracked_obligations) = &self.tracked_obligations {
tracked_obligations.borrow_mut().push(FulfilledObligation::Success(predicate.clone()));
tracked_obligations.borrow_mut().push(FulfilledObligation::Success {
infcx: infcx.clone(),
data: predicate.clone(),
});
}
}
}
Expand Down Expand Up @@ -114,7 +123,7 @@ impl<'tcx> TraitEngine<'tcx> for FulfillmentCtxt<'tcx> {
})
.collect();

self.track_fulfillment_errors(&errors);
self.track_fulfillment_errors(infcx, &errors);

errors
}
Expand All @@ -133,7 +142,7 @@ impl<'tcx> TraitEngine<'tcx> for FulfillmentCtxt<'tcx> {
let (changed, certainty, nested_goals) =
match infcx.evaluate_root_goal(goal, GenerateProofTree::IfEnabled).0 {
Ok(result) => {
self.track_fulfillment_success(&obligation);
self.track_fulfillment_success(infcx, &obligation);
result
}
Err(NoSolution) => {
Expand Down Expand Up @@ -214,7 +223,7 @@ impl<'tcx> TraitEngine<'tcx> for FulfillmentCtxt<'tcx> {
}
}

self.track_fulfillment_errors(&errors);
self.track_fulfillment_errors(infcx, &errors);

errors
}
Expand Down

0 comments on commit 998644f

Please sign in to comment.