Skip to content

Commit ae97362

Browse files
committed
Auto merge of rust-lang#121979 - GuillaumeGomez:warning-too-complex-match, r=<try>
[DO NOT MERGE] crater: emit a warning if a `match` is too complex `@Nadrieril` suggested that it could be interesting to suggest to users when a `match` is too complex and that they should try to simplify it. The goal here is to find a threeshold where we will emit a warning suggesting to simplify a `match`. To do so, we'll run a crater run first and advise what to do from there. Currently the PR SHOULD NOT be merged as is and SHOULD NOT panic. r? `@Nadrieril`
2 parents 7606c13 + 7d43f33 commit ae97362

File tree

5 files changed

+10
-120
lines changed

5 files changed

+10
-120
lines changed

compiler/rustc_pattern_analysis/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@ pub trait TypeCx: Sized + fmt::Debug {
145145

146146
/// The maximum pattern complexity limit was reached.
147147
fn complexity_exceeded(&self) -> Result<(), Self::Error>;
148+
149+
fn too_complex_match(&self);
148150
}
149151

150152
/// The arm of a match expression.

compiler/rustc_pattern_analysis/src/rustc.rs

+4
Original file line numberDiff line numberDiff line change
@@ -900,6 +900,10 @@ impl<'p, 'tcx: 'p> TypeCx for RustcMatchCheckCtxt<'p, 'tcx> {
900900
let span = self.whole_match_span.unwrap_or(self.scrut_span);
901901
Err(self.tcx.dcx().span_err(span, "reached pattern complexity limit"))
902902
}
903+
904+
fn too_complex_match(&self) {
905+
panic!("match too complex!");
906+
}
903907
}
904908

905909
/// Recursively expand this pattern into its subpatterns. Only useful for or-patterns.

compiler/rustc_pattern_analysis/src/usefulness.rs

+4
Original file line numberDiff line numberDiff line change
@@ -747,6 +747,10 @@ impl<'a, Cx: TypeCx> UsefulnessCtxt<'a, Cx> {
747747
{
748748
return self.tycx.complexity_exceeded();
749749
}
750+
// FIXME: Will be turned into a warning once we have a somewhat useable threeshold.
751+
if self.complexity_level > 10_000_000 {
752+
self.tycx.too_complex_match();
753+
}
750754
Ok(())
751755
}
752756
}

tests/ui/pattern/complexity_limit.rs

-106
This file was deleted.

tests/ui/pattern/complexity_limit.stderr

-14
This file was deleted.

0 commit comments

Comments
 (0)