Skip to content

Commit 31ef33b

Browse files
committed
move limit to rustc_ast_ir
1 parent e6f12c8 commit 31ef33b

File tree

10 files changed

+84
-67
lines changed

10 files changed

+84
-67
lines changed

compiler/rustc_ast/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ pub mod token;
4444
pub mod tokenstream;
4545
pub mod visit;
4646

47+
pub use rustc_ast_ir::Limit;
48+
4749
pub use self::ast::*;
4850
pub use self::ast_traits::{AstDeref, AstNodeWrapper, HasAttrs, HasNodeId, HasTokens};
4951

compiler/rustc_ast_ir/src/lib.rs

+51
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
#![warn(unreachable_pub)]
66
// tidy-alphabetical-end
77

8+
use std::fmt;
9+
use std::ops::{Div, Mul};
10+
811
#[cfg(feature = "nightly")]
912
use rustc_macros::{Decodable, Encodable, HashStable_NoContext};
1013

@@ -86,3 +89,51 @@ pub enum Pinnedness {
8689
Not,
8790
Pinned,
8891
}
92+
93+
/// New-type wrapper around `usize` for representing limits. Ensures that comparisons against
94+
/// limits are consistent throughout the compiler.
95+
#[derive(Clone, Copy, Debug)]
96+
#[cfg_attr(feature = "nightly", derive(Encodable, Decodable, HashStable_NoContext))]
97+
pub struct Limit(pub usize);
98+
99+
impl Limit {
100+
/// Create a new limit from a `usize`.
101+
pub fn new(value: usize) -> Self {
102+
Limit(value)
103+
}
104+
105+
/// Check that `value` is within the limit. Ensures that the same comparisons are used
106+
/// throughout the compiler, as mismatches can cause ICEs, see #72540.
107+
#[inline]
108+
pub fn value_within_limit(&self, value: usize) -> bool {
109+
value <= self.0
110+
}
111+
}
112+
113+
impl From<usize> for Limit {
114+
fn from(value: usize) -> Self {
115+
Self::new(value)
116+
}
117+
}
118+
119+
impl fmt::Display for Limit {
120+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
121+
self.0.fmt(f)
122+
}
123+
}
124+
125+
impl Div<usize> for Limit {
126+
type Output = Limit;
127+
128+
fn div(self, rhs: usize) -> Self::Output {
129+
Limit::new(self.0 / rhs)
130+
}
131+
}
132+
133+
impl Mul<usize> for Limit {
134+
type Output = Limit;
135+
136+
fn mul(self, rhs: usize) -> Self::Output {
137+
Limit::new(self.0 * rhs)
138+
}
139+
}

compiler/rustc_errors/src/diagnostic_impls.rs

+7
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use std::path::{Path, PathBuf};
66
use std::process::ExitStatus;
77

88
use rustc_abi::TargetDataLayoutErrors;
9+
use rustc_ast::Limit;
910
use rustc_ast::util::parser::ExprPrecedence;
1011
use rustc_ast_pretty::pprust;
1112
use rustc_macros::Subdiagnostic;
@@ -190,6 +191,12 @@ impl IntoDiagArg for PathBuf {
190191
}
191192
}
192193

194+
impl IntoDiagArg for Limit {
195+
fn into_diag_arg(self) -> rustc_errors::DiagArgValue {
196+
self.to_string().into_diag_arg()
197+
}
198+
}
199+
193200
impl IntoDiagArg for PanicStrategy {
194201
fn into_diag_arg(self) -> DiagArgValue {
195202
DiagArgValue::Str(Cow::Owned(self.desc().to_string()))

compiler/rustc_middle/src/ty/context.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -305,8 +305,8 @@ impl<'tcx> Interner for TyCtxt<'tcx> {
305305
self.parent(def_id)
306306
}
307307

308-
fn recursion_limit(self) -> usize {
309-
self.recursion_limit().0
308+
fn recursion_limit(self) -> Limit {
309+
self.recursion_limit()
310310
}
311311

312312
type Features = &'tcx rustc_feature::Features;

compiler/rustc_next_trait_solver/src/solve/eval_ctxt/canonical.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ where
168168
if !self.is_normalizes_to_goal {
169169
let num_non_region_vars =
170170
canonical.variables.iter().filter(|c| !c.is_region() && c.is_existential()).count();
171-
if num_non_region_vars > self.cx().recursion_limit() {
171+
if !self.cx().recursion_limit().value_within_limit(num_non_region_vars) {
172172
debug!(?num_non_region_vars, "too many inference variables -> overflow");
173173
return Ok(self.make_ambiguous_response_no_constraints(MaybeCause::Overflow {
174174
suggest_increasing_limit: true,

compiler/rustc_next_trait_solver/src/solve/eval_ctxt/mod.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use std::ops::ControlFlow;
22

33
use derive_where::derive_where;
4+
use rustc_ast_ir::Limit;
45
#[cfg(feature = "nightly")]
56
use rustc_macros::{HashStable_NoContext, TyDecodable, TyEncodable};
67
use rustc_type_ir::data_structures::{HashMap, HashSet, ensure_sufficient_stack};
@@ -148,7 +149,7 @@ pub trait SolverDelegateEvalExt: SolverDelegate {
148149
/// in coherence checking.
149150
fn root_goal_may_hold_with_depth(
150151
&self,
151-
root_depth: usize,
152+
root_depth: Limit,
152153
goal: Goal<Self::Interner, <Self::Interner as Interner>::Predicate>,
153154
) -> bool;
154155

@@ -182,7 +183,7 @@ where
182183

183184
fn root_goal_may_hold_with_depth(
184185
&self,
185-
root_depth: usize,
186+
root_depth: Limit,
186187
goal: Goal<Self::Interner, <Self::Interner as Interner>::Predicate>,
187188
) -> bool {
188189
self.probe(|| {
@@ -227,7 +228,7 @@ where
227228
/// over using this manually (such as [`SolverDelegateEvalExt::evaluate_root_goal`]).
228229
pub(super) fn enter_root<R>(
229230
delegate: &D,
230-
root_depth: usize,
231+
root_depth: Limit,
231232
generate_proof_tree: GenerateProofTree,
232233
f: impl FnOnce(&mut EvalCtxt<'_, D>) -> R,
233234
) -> (R, Option<inspect::GoalEvaluation<I>>) {

compiler/rustc_session/src/session.rs

+2-55
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
use std::any::Any;
2-
use std::ops::{Div, Mul};
32
use std::path::{Path, PathBuf};
43
use std::str::FromStr;
54
use std::sync::Arc;
65
use std::sync::atomic::AtomicBool;
7-
use std::{env, fmt, io};
6+
use std::{env, io};
87

8+
pub use rustc_ast::Limit;
99
use rustc_data_structures::flock;
1010
use rustc_data_structures::fx::{FxHashMap, FxIndexSet};
1111
use rustc_data_structures::profiling::{SelfProfiler, SelfProfilerRef};
@@ -58,59 +58,6 @@ pub enum CtfeBacktrace {
5858
Immediate,
5959
}
6060

61-
/// New-type wrapper around `usize` for representing limits. Ensures that comparisons against
62-
/// limits are consistent throughout the compiler.
63-
#[derive(Clone, Copy, Debug, HashStable_Generic)]
64-
pub struct Limit(pub usize);
65-
66-
impl Limit {
67-
/// Create a new limit from a `usize`.
68-
pub fn new(value: usize) -> Self {
69-
Limit(value)
70-
}
71-
72-
/// Check that `value` is within the limit. Ensures that the same comparisons are used
73-
/// throughout the compiler, as mismatches can cause ICEs, see #72540.
74-
#[inline]
75-
pub fn value_within_limit(&self, value: usize) -> bool {
76-
value <= self.0
77-
}
78-
}
79-
80-
impl From<usize> for Limit {
81-
fn from(value: usize) -> Self {
82-
Self::new(value)
83-
}
84-
}
85-
86-
impl fmt::Display for Limit {
87-
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
88-
self.0.fmt(f)
89-
}
90-
}
91-
92-
impl Div<usize> for Limit {
93-
type Output = Limit;
94-
95-
fn div(self, rhs: usize) -> Self::Output {
96-
Limit::new(self.0 / rhs)
97-
}
98-
}
99-
100-
impl Mul<usize> for Limit {
101-
type Output = Limit;
102-
103-
fn mul(self, rhs: usize) -> Self::Output {
104-
Limit::new(self.0 * rhs)
105-
}
106-
}
107-
108-
impl rustc_errors::IntoDiagArg for Limit {
109-
fn into_diag_arg(self) -> rustc_errors::DiagArgValue {
110-
self.to_string().into_diag_arg()
111-
}
112-
}
113-
11461
#[derive(Clone, Copy, Debug, HashStable_Generic)]
11562
pub struct Limits {
11663
/// The maximum recursion limit for potentially infinitely recursive

compiler/rustc_trait_selection/src/traits/coherence.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
77
use std::fmt::Debug;
88

9+
use rustc_ast_ir::Limit;
910
use rustc_data_structures::fx::{FxHashSet, FxIndexSet};
1011
use rustc_errors::{Diag, EmissionGuarantee};
1112
use rustc_hir::def::DefKind;
@@ -350,8 +351,10 @@ fn impl_intersection_has_impossible_obligation<'a, 'cx, 'tcx>(
350351
// a very low recursion depth and bail if any of them don't
351352
// hold.
352353
if !obligations.iter().all(|o| {
353-
<&SolverDelegate<'tcx>>::from(infcx)
354-
.root_goal_may_hold_with_depth(8, Goal::new(infcx.tcx, o.param_env, o.predicate))
354+
<&SolverDelegate<'tcx>>::from(infcx).root_goal_may_hold_with_depth(
355+
Limit(8),
356+
Goal::new(infcx.tcx, o.param_env, o.predicate),
357+
)
355358
}) {
356359
return IntersectionHasImpossibleObligations::Yes;
357360
}

compiler/rustc_type_ir/src/interner.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::fmt::Debug;
22
use std::hash::Hash;
33
use std::ops::Deref;
44

5-
use rustc_ast_ir::Movability;
5+
use rustc_ast_ir::{Limit, Movability};
66
use rustc_index::bit_set::DenseBitSet;
77
use smallvec::SmallVec;
88

@@ -177,7 +177,7 @@ pub trait Interner:
177177

178178
fn parent(self, def_id: Self::DefId) -> Self::DefId;
179179

180-
fn recursion_limit(self) -> usize;
180+
fn recursion_limit(self) -> Limit;
181181

182182
type Features: Features<Self>;
183183
fn features(self) -> Self::Features;

compiler/rustc_type_ir/src/search_graph/mod.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ use std::hash::Hash;
1818
use std::marker::PhantomData;
1919

2020
use derive_where::derive_where;
21+
use rustc_ast_ir::Limit;
2122
use rustc_index::{Idx, IndexVec};
2223
use tracing::debug;
2324

@@ -142,6 +143,11 @@ impl UsageKind {
142143

143144
#[derive(Debug, Clone, Copy)]
144145
struct AvailableDepth(usize);
146+
impl From<Limit> for AvailableDepth {
147+
fn from(Limit(value): Limit) -> AvailableDepth {
148+
AvailableDepth(value)
149+
}
150+
}
145151
impl AvailableDepth {
146152
/// Returns the remaining depth allowed for nested goals.
147153
///
@@ -368,9 +374,9 @@ pub struct SearchGraph<D: Delegate<Cx = X>, X: Cx = <D as Delegate>::Cx> {
368374
}
369375

370376
impl<D: Delegate<Cx = X>, X: Cx> SearchGraph<D> {
371-
pub fn new(root_depth: usize) -> SearchGraph<D> {
377+
pub fn new(recursion_limit: Limit) -> SearchGraph<D> {
372378
Self {
373-
root_depth: AvailableDepth(root_depth),
379+
root_depth: recursion_limit.into(),
374380
stack: Default::default(),
375381
provisional_cache: Default::default(),
376382
_marker: PhantomData,

0 commit comments

Comments
 (0)