Skip to content

Commit 826bee7

Browse files
committed
Implement repeat_while_none for both SearchGraph and EvalCtxt
1 parent 873c83b commit 826bee7

File tree

3 files changed

+38
-24
lines changed

3 files changed

+38
-24
lines changed

compiler/rustc_trait_selection/src/solve/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ use rustc_middle::ty::{
3131
};
3232
use rustc_span::DUMMY_SP;
3333

34+
use crate::solve::search_graph::overflow::OverflowHandler;
3435
use crate::traits::ObligationCause;
3536

3637
mod assembly;

compiler/rustc_trait_selection/src/solve/search_graph/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
mod cache;
2-
mod overflow;
2+
pub(crate) mod overflow;
33

44
use self::cache::ProvisionalEntry;
55
use super::{CanonicalGoal, Certainty, MaybeCause, QueryResult};
@@ -18,7 +18,7 @@ struct StackElem<'tcx> {
1818
has_been_used: bool,
1919
}
2020

21-
pub(super) struct SearchGraph<'tcx> {
21+
pub(crate) struct SearchGraph<'tcx> {
2222
/// The stack of goals currently being computed.
2323
///
2424
/// An element is *deeper* in the stack if its index is *lower*.

compiler/rustc_trait_selection/src/solve/search_graph/overflow.rs

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -50,36 +50,49 @@ impl OverflowData {
5050
}
5151
}
5252

53-
impl<'tcx> SearchGraph<'tcx> {
54-
pub fn deal_with_overflow(
55-
&mut self,
56-
tcx: TyCtxt<'tcx>,
57-
goal: Canonical<'tcx, impl Sized>,
58-
) -> QueryResult<'tcx> {
59-
self.overflow_data.deal_with_overflow();
60-
response_no_constraints(tcx, goal, Certainty::Maybe(MaybeCause::Overflow))
61-
}
62-
}
53+
pub(crate) trait OverflowHandler<'tcx> {
54+
fn search_graph(&mut self) -> &mut SearchGraph<'tcx>;
6355

64-
impl<'tcx> EvalCtxt<'_, 'tcx> {
65-
/// A `while`-loop which tracks overflow.
66-
pub fn repeat_while_none<T>(
56+
fn repeat_while_none<T>(
6757
&mut self,
68-
mut overflow_body: impl FnMut(&mut Self) -> T,
58+
on_overflow: impl FnOnce(&mut Self) -> T,
6959
mut loop_body: impl FnMut(&mut Self) -> Option<Result<T, NoSolution>>,
7060
) -> Result<T, NoSolution> {
71-
let start_depth = self.search_graph.overflow_data.additional_depth;
72-
let depth = self.search_graph.stack.len();
73-
while !self.search_graph.overflow_data.has_overflow(depth) {
61+
let start_depth = self.search_graph().overflow_data.additional_depth;
62+
let depth = self.search_graph().stack.len();
63+
while !self.search_graph().overflow_data.has_overflow(depth) {
7464
if let Some(result) = loop_body(self) {
75-
self.search_graph.overflow_data.additional_depth = start_depth;
65+
self.search_graph().overflow_data.additional_depth = start_depth;
7666
return result;
7767
}
7868

79-
self.search_graph.overflow_data.additional_depth += 1;
69+
self.search_graph().overflow_data.additional_depth += 1;
8070
}
81-
self.search_graph.overflow_data.additional_depth = start_depth;
82-
self.search_graph.overflow_data.deal_with_overflow();
83-
Ok(overflow_body(self))
71+
self.search_graph().overflow_data.additional_depth = start_depth;
72+
self.search_graph().overflow_data.deal_with_overflow();
73+
Ok(on_overflow(self))
74+
}
75+
}
76+
77+
impl<'tcx> OverflowHandler<'tcx> for EvalCtxt<'_, 'tcx> {
78+
fn search_graph(&mut self) -> &mut SearchGraph<'tcx> {
79+
&mut self.search_graph
80+
}
81+
}
82+
83+
impl<'tcx> OverflowHandler<'tcx> for SearchGraph<'tcx> {
84+
fn search_graph(&mut self) -> &mut SearchGraph<'tcx> {
85+
self
86+
}
87+
}
88+
89+
impl<'tcx> SearchGraph<'tcx> {
90+
pub fn deal_with_overflow(
91+
&mut self,
92+
tcx: TyCtxt<'tcx>,
93+
goal: Canonical<'tcx, impl Sized>,
94+
) -> QueryResult<'tcx> {
95+
self.overflow_data.deal_with_overflow();
96+
response_no_constraints(tcx, goal, Certainty::Maybe(MaybeCause::Overflow))
8497
}
8598
}

0 commit comments

Comments
 (0)