about summary refs log tree commit diff
path: root/compiler/rustc_pattern_analysis/src/lib.rs
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2023-12-11 20:02:55 +0000
committerbors <bors@rust-lang.org>2023-12-11 20:02:55 +0000
commit21cce21d8c012f14cf74d5afddd795d324600dac (patch)
tree1f8f0d99025eb4f1bd7a19a97d7ae18fc121d4f6 /compiler/rustc_pattern_analysis/src/lib.rs
parent57010939ed1d00076b4af0ed06a81ec69ea5e4a8 (diff)
parent1cf538b0b10805a20d1ca610919bc0d908ed260a (diff)
downloadrust-21cce21d8c012f14cf74d5afddd795d324600dac.tar.gz
rust-21cce21d8c012f14cf74d5afddd795d324600dac.zip
Auto merge of #118838 - matthiaskrgr:rollup-8kwzpho, r=matthiaskrgr
Rollup of 8 pull requests

Successful merges:

 - #118620 (resolve: Use `def_kind` query to cleanup some code)
 - #118647 (dump bootstrap shims)
 - #118726 (Do not parenthesize exterior struct lit inside match guards)
 - #118818 (llvm-wrapper: adapt for LLVM API change)
 - #118822 (Extract exhaustiveness into its own crate)
 - #118826 (Edit target doc template to remove email)
 - #118827 (Update table for linker-plugin-lto docs)
 - #118835 (Fix again `rustc_codegen_gcc` tests)

r? `@ghost`
`@rustbot` modify labels: rollup
Diffstat (limited to 'compiler/rustc_pattern_analysis/src/lib.rs')
-rw-r--r--compiler/rustc_pattern_analysis/src/lib.rs56
1 files changed, 56 insertions, 0 deletions
diff --git a/compiler/rustc_pattern_analysis/src/lib.rs b/compiler/rustc_pattern_analysis/src/lib.rs
new file mode 100644
index 00000000000..07730aa49d3
--- /dev/null
+++ b/compiler/rustc_pattern_analysis/src/lib.rs
@@ -0,0 +1,56 @@
+//! Analysis of patterns, notably match exhaustiveness checking.
+
+pub mod constructor;
+pub mod cx;
+pub mod errors;
+pub(crate) mod lints;
+pub mod pat;
+pub mod usefulness;
+
+#[macro_use]
+extern crate tracing;
+#[macro_use]
+extern crate rustc_middle;
+
+rustc_fluent_macro::fluent_messages! { "../messages.ftl" }
+
+use lints::PatternColumn;
+use rustc_hir::HirId;
+use rustc_middle::ty::Ty;
+use usefulness::{compute_match_usefulness, UsefulnessReport};
+
+use crate::cx::MatchCheckCtxt;
+use crate::lints::{lint_nonexhaustive_missing_variants, lint_overlapping_range_endpoints};
+use crate::pat::DeconstructedPat;
+
+/// The arm of a match expression.
+#[derive(Clone, Copy, Debug)]
+pub struct MatchArm<'p, 'tcx> {
+    /// The pattern must have been lowered through `check_match::MatchVisitor::lower_pattern`.
+    pub pat: &'p DeconstructedPat<'p, 'tcx>,
+    pub hir_id: HirId,
+    pub has_guard: bool,
+}
+
+/// The entrypoint for this crate. Computes whether a match is exhaustive and which of its arms are
+/// useful, and runs some lints.
+pub fn analyze_match<'p, 'tcx>(
+    cx: &MatchCheckCtxt<'p, 'tcx>,
+    arms: &[MatchArm<'p, 'tcx>],
+    scrut_ty: Ty<'tcx>,
+) -> UsefulnessReport<'p, 'tcx> {
+    let pat_column = PatternColumn::new(arms);
+
+    let report = compute_match_usefulness(cx, arms, scrut_ty);
+
+    // Lint on ranges that overlap on their endpoints, which is likely a mistake.
+    lint_overlapping_range_endpoints(cx, &pat_column);
+
+    // 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 cx.refutable && report.non_exhaustiveness_witnesses.is_empty() {
+        lint_nonexhaustive_missing_variants(cx, arms, &pat_column, scrut_ty)
+    }
+
+    report
+}