Add pattern_complexity_limit to Limits.

It's similar to the other limits, e.g. obtained via `get_limit`. So it
makes sense to handle it consistently with the other limits. We now use
`Limit`/`usize` in most places instead of `Option<usize>`, so we use
`Limit::new(usize::MAX)`/`usize::MAX` to emulate how `None` used to work.

The commit also adds `Limit::unlimited`.
This commit is contained in:
Nicholas Nethercote 2025-02-07 10:57:14 +11:00
parent 5bc6231454
commit b023671ce2
11 changed files with 51 additions and 35 deletions

View file

@ -24,30 +24,36 @@ pub fn provide(providers: &mut Providers) {
tcx.hir().krate_attrs(), tcx.hir().krate_attrs(),
tcx.sess, tcx.sess,
sym::move_size_limit, sym::move_size_limit,
tcx.sess.opts.unstable_opts.move_size_limit.unwrap_or(0), Limit::new(tcx.sess.opts.unstable_opts.move_size_limit.unwrap_or(0)),
), ),
type_length_limit: get_limit( type_length_limit: get_limit(
tcx.hir().krate_attrs(), tcx.hir().krate_attrs(),
tcx.sess, tcx.sess,
sym::type_length_limit, sym::type_length_limit,
2usize.pow(24), Limit::new(2usize.pow(24)),
),
pattern_complexity_limit: get_limit(
tcx.hir().krate_attrs(),
tcx.sess,
sym::pattern_complexity,
Limit::unlimited(),
), ),
} }
} }
pub fn get_recursion_limit(krate_attrs: &[impl AttributeExt], sess: &Session) -> Limit { pub fn get_recursion_limit(krate_attrs: &[impl AttributeExt], sess: &Session) -> Limit {
get_limit(krate_attrs, sess, sym::recursion_limit, 128) get_limit(krate_attrs, sess, sym::recursion_limit, Limit::new(128))
} }
fn get_limit( fn get_limit(
krate_attrs: &[impl AttributeExt], krate_attrs: &[impl AttributeExt],
sess: &Session, sess: &Session,
name: Symbol, name: Symbol,
default: usize, default: Limit,
) -> Limit { ) -> Limit {
match get_limit_size(krate_attrs, sess, name) { match get_limit_size(krate_attrs, sess, name) {
Some(size) => Limit::new(size), Some(size) => Limit::new(size),
None => Limit::new(default), None => default,
} }
} }

View file

@ -2168,6 +2168,10 @@ impl<'tcx> TyCtxt<'tcx> {
self.limits(()).move_size_limit self.limits(()).move_size_limit
} }
pub fn pattern_complexity_limit(self) -> Limit {
self.limits(()).pattern_complexity_limit
}
/// All traits in the crate graph, including those not visible to the user. /// All traits in the crate graph, including those not visible to the user.
pub fn all_traits(self) -> impl Iterator<Item = DefId> + 'tcx { pub fn all_traits(self) -> impl Iterator<Item = DefId> + 'tcx {
iter::once(LOCAL_CRATE) iter::once(LOCAL_CRATE)

View file

@ -10,7 +10,6 @@ use rustc_hir::{self as hir, BindingMode, ByRef, HirId};
use rustc_infer::infer::TyCtxtInferExt; use rustc_infer::infer::TyCtxtInferExt;
use rustc_lint::Level; use rustc_lint::Level;
use rustc_middle::bug; use rustc_middle::bug;
use rustc_middle::middle::limits::get_limit_size;
use rustc_middle::thir::visit::Visitor; use rustc_middle::thir::visit::Visitor;
use rustc_middle::thir::*; use rustc_middle::thir::*;
use rustc_middle::ty::print::with_no_trimmed_paths; use rustc_middle::ty::print::with_no_trimmed_paths;
@ -25,7 +24,7 @@ use rustc_session::lint::builtin::{
}; };
use rustc_span::edit_distance::find_best_match_for_name; use rustc_span::edit_distance::find_best_match_for_name;
use rustc_span::hygiene::DesugaringKind; use rustc_span::hygiene::DesugaringKind;
use rustc_span::{Ident, Span, sym}; use rustc_span::{Ident, Span};
use rustc_trait_selection::infer::InferCtxtExt; use rustc_trait_selection::infer::InferCtxtExt;
use tracing::instrument; use tracing::instrument;
@ -404,18 +403,11 @@ impl<'p, 'tcx> MatchVisitor<'p, 'tcx> {
arms: &[MatchArm<'p, 'tcx>], arms: &[MatchArm<'p, 'tcx>],
scrut_ty: Ty<'tcx>, scrut_ty: Ty<'tcx>,
) -> Result<UsefulnessReport<'p, 'tcx>, ErrorGuaranteed> { ) -> Result<UsefulnessReport<'p, 'tcx>, ErrorGuaranteed> {
let pattern_complexity_limit = let report =
get_limit_size(cx.tcx.hir().krate_attrs(), cx.tcx.sess, sym::pattern_complexity); rustc_pattern_analysis::rustc::analyze_match(&cx, &arms, scrut_ty).map_err(|err| {
let report = rustc_pattern_analysis::rustc::analyze_match( self.error = Err(err);
&cx, err
&arms, })?;
scrut_ty,
pattern_complexity_limit,
)
.map_err(|err| {
self.error = Err(err);
err
})?;
// Warn unreachable subpatterns. // Warn unreachable subpatterns.
for (arm, is_useful) in report.arm_usefulness.iter() { for (arm, is_useful) in report.arm_usefulness.iter() {

View file

@ -1084,12 +1084,16 @@ pub fn analyze_match<'p, 'tcx>(
tycx: &RustcPatCtxt<'p, 'tcx>, tycx: &RustcPatCtxt<'p, 'tcx>,
arms: &[MatchArm<'p, 'tcx>], arms: &[MatchArm<'p, 'tcx>],
scrut_ty: Ty<'tcx>, scrut_ty: Ty<'tcx>,
pattern_complexity_limit: Option<usize>,
) -> Result<UsefulnessReport<'p, 'tcx>, ErrorGuaranteed> { ) -> Result<UsefulnessReport<'p, 'tcx>, ErrorGuaranteed> {
let scrut_ty = tycx.reveal_opaque_ty(scrut_ty); let scrut_ty = tycx.reveal_opaque_ty(scrut_ty);
let scrut_validity = PlaceValidity::from_bool(tycx.known_valid_scrutinee); let scrut_validity = PlaceValidity::from_bool(tycx.known_valid_scrutinee);
let report = let report = compute_match_usefulness(
compute_match_usefulness(tycx, arms, scrut_ty, scrut_validity, pattern_complexity_limit)?; tycx,
arms,
scrut_ty,
scrut_validity,
tycx.tcx.pattern_complexity_limit().0,
)?;
// Run the non_exhaustive_omitted_patterns lint. Only run on refutable patterns to avoid hitting // Run the non_exhaustive_omitted_patterns lint. Only run on refutable patterns to avoid hitting
// `if let`s. Only run if the match is exhaustive otherwise the error is redundant. // `if let`s. Only run if the match is exhaustive otherwise the error is redundant.

View file

@ -795,20 +795,21 @@ struct UsefulnessCtxt<'a, 'p, Cx: PatCx> {
/// Track information about the usefulness of branch patterns (see definition of "branch /// Track information about the usefulness of branch patterns (see definition of "branch
/// pattern" at [`BranchPatUsefulness`]). /// pattern" at [`BranchPatUsefulness`]).
branch_usefulness: FxHashMap<PatId, BranchPatUsefulness<'p, Cx>>, branch_usefulness: FxHashMap<PatId, BranchPatUsefulness<'p, Cx>>,
complexity_limit: Option<usize>, // Ideally this field would have type `Limit`, but this crate is used by
// rust-analyzer which cannot have a dependency on `Limit`, because `Limit`
// is from crate `rustc_session` which uses unstable Rust features.
complexity_limit: usize,
complexity_level: usize, complexity_level: usize,
} }
impl<'a, 'p, Cx: PatCx> UsefulnessCtxt<'a, 'p, Cx> { impl<'a, 'p, Cx: PatCx> UsefulnessCtxt<'a, 'p, Cx> {
fn increase_complexity_level(&mut self, complexity_add: usize) -> Result<(), Cx::Error> { fn increase_complexity_level(&mut self, complexity_add: usize) -> Result<(), Cx::Error> {
self.complexity_level += complexity_add; self.complexity_level += complexity_add;
if self if self.complexity_level <= self.complexity_limit {
.complexity_limit Ok(())
.is_some_and(|complexity_limit| complexity_limit < self.complexity_level) } else {
{ self.tycx.complexity_exceeded()
return self.tycx.complexity_exceeded();
} }
Ok(())
} }
} }
@ -1834,7 +1835,7 @@ pub fn compute_match_usefulness<'p, Cx: PatCx>(
arms: &[MatchArm<'p, Cx>], arms: &[MatchArm<'p, Cx>],
scrut_ty: Cx::Ty, scrut_ty: Cx::Ty,
scrut_validity: PlaceValidity, scrut_validity: PlaceValidity,
complexity_limit: Option<usize>, complexity_limit: usize,
) -> Result<UsefulnessReport<'p, Cx>, Cx::Error> { ) -> Result<UsefulnessReport<'p, Cx>, Cx::Error> {
let mut cx = UsefulnessCtxt { let mut cx = UsefulnessCtxt {
tycx, tycx,

View file

@ -124,7 +124,7 @@ pub fn compute_match_usefulness<'p>(
arms: &[MatchArm<'p, Cx>], arms: &[MatchArm<'p, Cx>],
ty: Ty, ty: Ty,
scrut_validity: PlaceValidity, scrut_validity: PlaceValidity,
complexity_limit: Option<usize>, complexity_limit: usize,
) -> Result<UsefulnessReport<'p, Cx>, ()> { ) -> Result<UsefulnessReport<'p, Cx>, ()> {
init_tracing(); init_tracing();
rustc_pattern_analysis::usefulness::compute_match_usefulness( rustc_pattern_analysis::usefulness::compute_match_usefulness(

View file

@ -14,7 +14,7 @@ fn check(patterns: &[DeconstructedPat<Cx>], complexity_limit: usize) -> Result<(
let ty = *patterns[0].ty(); let ty = *patterns[0].ty();
let arms: Vec<_> = let arms: Vec<_> =
patterns.iter().map(|pat| MatchArm { pat, has_guard: false, arm_data: () }).collect(); patterns.iter().map(|pat| MatchArm { pat, has_guard: false, arm_data: () }).collect();
compute_match_usefulness(arms.as_slice(), ty, PlaceValidity::ValidOnly, Some(complexity_limit)) compute_match_usefulness(arms.as_slice(), ty, PlaceValidity::ValidOnly, complexity_limit)
.map(|_report| ()) .map(|_report| ())
} }

View file

@ -14,7 +14,8 @@ fn check(patterns: Vec<DeconstructedPat<Cx>>) -> Vec<WitnessPat<Cx>> {
let arms: Vec<_> = let arms: Vec<_> =
patterns.iter().map(|pat| MatchArm { pat, has_guard: false, arm_data: () }).collect(); patterns.iter().map(|pat| MatchArm { pat, has_guard: false, arm_data: () }).collect();
let report = let report =
compute_match_usefulness(arms.as_slice(), ty, PlaceValidity::ValidOnly, None).unwrap(); compute_match_usefulness(arms.as_slice(), ty, PlaceValidity::ValidOnly, usize::MAX)
.unwrap();
report.non_exhaustiveness_witnesses report.non_exhaustiveness_witnesses
} }

View file

@ -14,7 +14,8 @@ fn check(patterns: Vec<DeconstructedPat<Cx>>) -> Vec<Vec<usize>> {
let arms: Vec<_> = let arms: Vec<_> =
patterns.iter().map(|pat| MatchArm { pat, has_guard: false, arm_data: () }).collect(); patterns.iter().map(|pat| MatchArm { pat, has_guard: false, arm_data: () }).collect();
let report = let report =
compute_match_usefulness(arms.as_slice(), ty, PlaceValidity::ValidOnly, None).unwrap(); compute_match_usefulness(arms.as_slice(), ty, PlaceValidity::ValidOnly, usize::MAX)
.unwrap();
report.arm_intersections.into_iter().map(|bitset| bitset.iter().collect()).collect() report.arm_intersections.into_iter().map(|bitset| bitset.iter().collect()).collect()
} }

View file

@ -67,6 +67,11 @@ impl Limit {
Limit(value) Limit(value)
} }
/// Create a new unlimited limit.
pub fn unlimited() -> Self {
Limit(usize::MAX)
}
/// Check that `value` is within the limit. Ensures that the same comparisons are used /// Check that `value` is within the limit. Ensures that the same comparisons are used
/// throughout the compiler, as mismatches can cause ICEs, see #72540. /// throughout the compiler, as mismatches can cause ICEs, see #72540.
#[inline] #[inline]
@ -119,6 +124,8 @@ pub struct Limits {
pub move_size_limit: Limit, pub move_size_limit: Limit,
/// The maximum length of types during monomorphization. /// The maximum length of types during monomorphization.
pub type_length_limit: Limit, pub type_length_limit: Limit,
/// The maximum pattern complexity allowed (internal only).
pub pattern_complexity_limit: Limit,
} }
pub struct CompilerIO { pub struct CompilerIO {

View file

@ -95,7 +95,7 @@ impl<'db> MatchCheckCtx<'db> {
let place_validity = PlaceValidity::from_bool(known_valid_scrutinee.unwrap_or(true)); let place_validity = PlaceValidity::from_bool(known_valid_scrutinee.unwrap_or(true));
// Measured to take ~100ms on modern hardware. // Measured to take ~100ms on modern hardware.
let complexity_limit = Some(500000); let complexity_limit = 500000;
compute_match_usefulness(self, arms, scrut_ty, place_validity, complexity_limit) compute_match_usefulness(self, arms, scrut_ty, place_validity, complexity_limit)
} }