Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add fuel to match checking #16879

Merged
merged 4 commits into from
Mar 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 2 additions & 15 deletions crates/hir-ty/src/diagnostics/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ use hir_def::{ItemContainerId, Lookup};
use hir_expand::name;
use itertools::Itertools;
use rustc_hash::FxHashSet;
use rustc_pattern_analysis::usefulness::{compute_match_usefulness, PlaceValidity};
use syntax::{ast, AstNode};
use tracing::debug;
use triomphe::Arc;
Expand Down Expand Up @@ -234,13 +233,7 @@ impl ExprValidator {
return;
}

let report = match compute_match_usefulness(
&cx,
m_arms.as_slice(),
scrut_ty.clone(),
PlaceValidity::ValidOnly,
None,
) {
let report = match cx.compute_match_usefulness(m_arms.as_slice(), scrut_ty.clone()) {
Ok(report) => report,
Err(()) => return,
};
Expand Down Expand Up @@ -282,13 +275,7 @@ impl ExprValidator {
continue;
}

let report = match compute_match_usefulness(
&cx,
&[match_arm],
ty.clone(),
PlaceValidity::ValidOnly,
None,
) {
let report = match cx.compute_match_usefulness(&[match_arm], ty.clone()) {
Ok(v) => v,
Err(e) => {
debug!(?e, "match usefulness error");
Expand Down
14 changes: 13 additions & 1 deletion crates/hir-ty/src/diagnostics/match_check/pat_analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use rustc_hash::FxHashMap;
use rustc_pattern_analysis::{
constructor::{Constructor, ConstructorSet, VariantVisibility},
index::IdxContainer,
usefulness::{compute_match_usefulness, PlaceValidity, UsefulnessReport},
Captures, PatCx, PrivateUninhabitedField,
};
use smallvec::{smallvec, SmallVec};
Expand Down Expand Up @@ -59,6 +60,18 @@ impl<'p> MatchCheckCtx<'p> {
Self { module, body, db, exhaustive_patterns, min_exhaustive_patterns }
}

pub(crate) fn compute_match_usefulness(
&self,
arms: &[MatchArm<'p>],
scrut_ty: Ty,
) -> Result<UsefulnessReport<'p, Self>, ()> {
// FIXME: Determine place validity correctly. For now, err on the safe side.
let place_validity = PlaceValidity::MaybeInvalid;
// Measured to take ~100ms on modern hardware.
let complexity_limit = Some(500000);
compute_match_usefulness(self, arms, scrut_ty, place_validity, complexity_limit)
}

fn is_uninhabited(&self, ty: &Ty) -> bool {
is_ty_uninhabited_from(ty, self.module, self.db)
}
Expand Down Expand Up @@ -465,7 +478,6 @@ impl<'p> PatCx for MatchCheckCtx<'p> {
}

fn complexity_exceeded(&self) -> Result<(), Self::Error> {
// FIXME(Nadrieril): make use of the complexity counter.
Err(())
}
}
Expand Down
27 changes: 27 additions & 0 deletions crates/ide-diagnostics/src/handlers/missing_match_arms.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ mod tests {
},
DiagnosticsConfig,
};
use test_utils::skip_slow_tests;

#[track_caller]
fn check_diagnostics_no_bails(ra_fixture: &str) {
Expand Down Expand Up @@ -1004,6 +1005,32 @@ fn f() {
);
}

#[test]
fn exponential_match() {
if skip_slow_tests() {
return;
}
// Constructs a match where match checking takes exponential time. Ensures we bail early.
use std::fmt::Write;
let struct_arity = 50;
let mut code = String::new();
write!(code, "struct BigStruct {{").unwrap();
for i in 0..struct_arity {
write!(code, " field{i}: bool,").unwrap();
}
write!(code, "}}").unwrap();
write!(code, "fn big_match(s: BigStruct) {{").unwrap();
write!(code, " match s {{").unwrap();
for i in 0..struct_arity {
write!(code, " BigStruct {{ field{i}: true, ..}} => {{}},").unwrap();
write!(code, " BigStruct {{ field{i}: false, ..}} => {{}},").unwrap();
}
write!(code, " _ => {{}},").unwrap();
write!(code, " }}").unwrap();
write!(code, "}}").unwrap();
check_diagnostics_no_bails(&code);
}

mod rust_unstable {
use super::*;

Expand Down
Loading