Skip to content

Commit 8aa0221

Browse files
Emit a warning if a match is too complex and set the default trigger when complexity reaches 10_000_000
1 parent a42873e commit 8aa0221

File tree

3 files changed

+6
-2
lines changed

3 files changed

+6
-2
lines changed

compiler/rustc_mir_build/src/thir/pattern/check_match.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,8 @@ impl<'p, 'tcx> MatchVisitor<'p, 'tcx> {
405405
scrut_ty: Ty<'tcx>,
406406
) -> Result<UsefulnessReport<'p, 'tcx>, ErrorGuaranteed> {
407407
let pattern_complexity_limit =
408-
get_limit_size(cx.tcx.hir().krate_attrs(), cx.tcx.sess, sym::pattern_complexity);
408+
get_limit_size(cx.tcx.hir().krate_attrs(), cx.tcx.sess, sym::pattern_complexity)
409+
.or(Some(10_000_000)); // Default value to emit the warning for "too complex" match.
409410
let report =
410411
rustc_pattern_analysis::analyze_match(&cx, &arms, scrut_ty, pattern_complexity_limit)
411412
.map_err(|err| {

compiler/rustc_pattern_analysis/src/rustc.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -913,7 +913,8 @@ impl<'p, 'tcx: 'p> PatCx for RustcPatCtxt<'p, 'tcx> {
913913

914914
fn complexity_exceeded(&self) -> Result<(), Self::Error> {
915915
let span = self.whole_match_span.unwrap_or(self.scrut_span);
916-
Err(self.tcx.dcx().span_err(span, "reached pattern complexity limit"))
916+
self.tcx.dcx().span_warn(span, "`match` is too complex");
917+
Ok(())
917918
}
918919

919920
fn lint_non_contiguous_range_endpoints(

compiler/rustc_pattern_analysis/src/usefulness.rs

+2
Original file line numberDiff line numberDiff line change
@@ -745,6 +745,8 @@ impl<'a, Cx: PatCx> UsefulnessCtxt<'a, Cx> {
745745
.complexity_limit
746746
.is_some_and(|complexity_limit| complexity_limit < self.complexity_level)
747747
{
748+
// We change it to `None` to prevent it from being called more than once.
749+
self.complexity_limit = None;
748750
return self.tycx.complexity_exceeded();
749751
}
750752
Ok(())

0 commit comments

Comments
 (0)