2023-12-10 19:42:30 +00:00
|
|
|
//! Analysis of patterns, notably match exhaustiveness checking.
|
|
|
|
|
|
|
|
pub mod constructor;
|
2023-12-11 19:46:35 +00:00
|
|
|
#[cfg(feature = "rustc")]
|
2023-12-10 19:42:30 +00:00
|
|
|
pub mod errors;
|
2023-12-11 19:46:35 +00:00
|
|
|
#[cfg(feature = "rustc")]
|
2023-12-11 09:40:31 +00:00
|
|
|
pub(crate) mod lints;
|
2023-12-10 19:42:30 +00:00
|
|
|
pub mod pat;
|
2023-12-11 19:46:35 +00:00
|
|
|
#[cfg(feature = "rustc")]
|
2023-12-11 19:15:52 +00:00
|
|
|
pub mod rustc;
|
2023-12-10 19:42:30 +00:00
|
|
|
pub mod usefulness;
|
|
|
|
|
|
|
|
#[macro_use]
|
|
|
|
extern crate tracing;
|
2023-12-11 19:46:35 +00:00
|
|
|
#[cfg(feature = "rustc")]
|
2023-12-10 19:42:30 +00:00
|
|
|
#[macro_use]
|
|
|
|
extern crate rustc_middle;
|
|
|
|
|
2023-12-11 19:46:35 +00:00
|
|
|
#[cfg(feature = "rustc")]
|
2023-12-10 19:42:30 +00:00
|
|
|
rustc_fluent_macro::fluent_messages! { "../messages.ftl" }
|
2023-12-11 09:40:31 +00:00
|
|
|
|
2023-12-11 19:01:02 +00:00
|
|
|
use std::fmt;
|
|
|
|
|
|
|
|
use rustc_index::Idx;
|
2023-12-11 19:46:35 +00:00
|
|
|
#[cfg(feature = "rustc")]
|
2023-12-11 09:40:31 +00:00
|
|
|
use rustc_middle::ty::Ty;
|
2024-01-07 20:20:16 +00:00
|
|
|
#[cfg(feature = "rustc")]
|
|
|
|
use rustc_span::ErrorGuaranteed;
|
2023-12-11 09:40:31 +00:00
|
|
|
|
2023-12-11 19:46:35 +00:00
|
|
|
use crate::constructor::{Constructor, ConstructorSet};
|
|
|
|
#[cfg(feature = "rustc")]
|
|
|
|
use crate::lints::{
|
|
|
|
lint_nonexhaustive_missing_variants, lint_overlapping_range_endpoints, PatternColumn,
|
|
|
|
};
|
2023-12-11 09:40:31 +00:00
|
|
|
use crate::pat::DeconstructedPat;
|
2023-12-11 19:46:35 +00:00
|
|
|
#[cfg(feature = "rustc")]
|
2023-12-14 16:54:11 +00:00
|
|
|
use crate::rustc::RustcMatchCheckCtxt;
|
2023-12-11 19:46:35 +00:00
|
|
|
#[cfg(feature = "rustc")]
|
|
|
|
use crate::usefulness::{compute_match_usefulness, ValidityConstraint};
|
2023-12-11 09:40:31 +00:00
|
|
|
|
2023-12-15 15:53:29 +00:00
|
|
|
pub trait Captures<'a> {}
|
|
|
|
impl<'a, T: ?Sized> Captures<'a> for T {}
|
|
|
|
|
|
|
|
/// Context that provides type information about constructors.
|
|
|
|
///
|
|
|
|
/// Most of the crate is parameterized on a type that implements this trait.
|
2023-12-19 17:09:31 +00:00
|
|
|
pub trait TypeCx: Sized + fmt::Debug {
|
2023-12-15 15:18:21 +00:00
|
|
|
/// The type of a pattern.
|
2023-12-11 19:01:02 +00:00
|
|
|
type Ty: Copy + Clone + fmt::Debug; // FIXME: remove Copy
|
2024-01-07 20:20:16 +00:00
|
|
|
/// Errors that can abort analysis.
|
|
|
|
type Error: fmt::Debug;
|
2023-12-11 19:45:34 +00:00
|
|
|
/// The index of an enum variant.
|
2023-12-11 19:01:02 +00:00
|
|
|
type VariantIdx: Clone + Idx;
|
2023-12-11 19:45:34 +00:00
|
|
|
/// A string literal
|
2023-12-11 19:01:02 +00:00
|
|
|
type StrLit: Clone + PartialEq + fmt::Debug;
|
2023-12-15 16:25:11 +00:00
|
|
|
/// Extra data to store in a match arm.
|
2023-12-11 19:45:34 +00:00
|
|
|
type ArmData: Copy + Clone + fmt::Debug;
|
2023-12-22 22:47:44 +00:00
|
|
|
/// Extra data to store in a pattern.
|
|
|
|
type PatData: Clone;
|
2023-12-11 19:01:02 +00:00
|
|
|
|
|
|
|
fn is_exhaustive_patterns_feature_on(&self) -> bool;
|
|
|
|
|
|
|
|
/// The number of fields for this constructor.
|
|
|
|
fn ctor_arity(&self, ctor: &Constructor<Self>, ty: Self::Ty) -> usize;
|
|
|
|
|
|
|
|
/// The types of the fields for this constructor. The result must have a length of
|
|
|
|
/// `ctor_arity()`.
|
|
|
|
fn ctor_sub_tys(&self, ctor: &Constructor<Self>, ty: Self::Ty) -> &[Self::Ty];
|
|
|
|
|
|
|
|
/// The set of all the constructors for `ty`.
|
|
|
|
///
|
|
|
|
/// This must follow the invariants of `ConstructorSet`
|
2024-01-01 22:54:20 +00:00
|
|
|
fn ctors_for_ty(&self, ty: Self::Ty) -> Result<ConstructorSet<Self>, Self::Error>;
|
2023-12-11 19:01:02 +00:00
|
|
|
|
|
|
|
/// Best-effort `Debug` implementation.
|
|
|
|
fn debug_pat(f: &mut fmt::Formatter<'_>, pat: &DeconstructedPat<'_, Self>) -> fmt::Result;
|
|
|
|
|
|
|
|
/// Raise a bug.
|
|
|
|
fn bug(&self, fmt: fmt::Arguments<'_>) -> !;
|
|
|
|
}
|
|
|
|
|
2023-12-15 15:53:29 +00:00
|
|
|
/// Context that provides information global to a match.
|
2023-12-19 17:09:31 +00:00
|
|
|
#[derive(derivative::Derivative)]
|
|
|
|
#[derivative(Clone(bound = ""), Copy(bound = ""))]
|
2024-01-03 00:34:38 +00:00
|
|
|
pub struct MatchCtxt<'a, Cx: TypeCx> {
|
2023-12-15 15:53:29 +00:00
|
|
|
/// The context for type information.
|
|
|
|
pub tycx: &'a Cx,
|
|
|
|
}
|
|
|
|
|
2023-12-11 09:40:31 +00:00
|
|
|
/// The arm of a match expression.
|
2023-12-19 17:09:31 +00:00
|
|
|
#[derive(Debug)]
|
|
|
|
#[derive(derivative::Derivative)]
|
|
|
|
#[derivative(Clone(bound = ""), Copy(bound = ""))]
|
2023-12-15 16:25:11 +00:00
|
|
|
pub struct MatchArm<'p, Cx: TypeCx> {
|
2023-12-11 19:01:02 +00:00
|
|
|
pub pat: &'p DeconstructedPat<'p, Cx>,
|
2023-12-11 09:40:31 +00:00
|
|
|
pub has_guard: bool,
|
2023-12-11 19:45:34 +00:00
|
|
|
pub arm_data: Cx::ArmData,
|
2023-12-11 09:40:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// The entrypoint for this crate. Computes whether a match is exhaustive and which of its arms are
|
|
|
|
/// useful, and runs some lints.
|
2023-12-11 19:46:35 +00:00
|
|
|
#[cfg(feature = "rustc")]
|
2023-12-11 09:40:31 +00:00
|
|
|
pub fn analyze_match<'p, 'tcx>(
|
2023-12-15 15:53:29 +00:00
|
|
|
tycx: &RustcMatchCheckCtxt<'p, 'tcx>,
|
2023-12-11 19:15:52 +00:00
|
|
|
arms: &[rustc::MatchArm<'p, 'tcx>],
|
2023-12-11 09:40:31 +00:00
|
|
|
scrut_ty: Ty<'tcx>,
|
2024-01-07 20:20:16 +00:00
|
|
|
) -> Result<rustc::UsefulnessReport<'p, 'tcx>, ErrorGuaranteed> {
|
2023-12-26 12:21:35 +00:00
|
|
|
let scrut_ty = tycx.reveal_opaque_ty(scrut_ty);
|
2023-12-15 15:53:29 +00:00
|
|
|
let scrut_validity = ValidityConstraint::from_bool(tycx.known_valid_scrutinee);
|
2024-01-03 00:34:38 +00:00
|
|
|
let cx = MatchCtxt { tycx };
|
2023-12-15 15:53:29 +00:00
|
|
|
|
2024-01-07 20:20:16 +00:00
|
|
|
let report = compute_match_usefulness(cx, arms, scrut_ty, scrut_validity)?;
|
2023-12-11 09:40:31 +00:00
|
|
|
|
2023-12-15 15:53:29 +00:00
|
|
|
let pat_column = PatternColumn::new(arms);
|
2023-12-11 09:40:31 +00:00
|
|
|
|
2023-12-28 21:22:20 +00:00
|
|
|
// Lint ranges that overlap on their endpoints, which is likely a mistake.
|
2023-12-29 18:21:43 +00:00
|
|
|
if !report.overlapping_range_endpoints.is_empty() {
|
|
|
|
lint_overlapping_range_endpoints(cx, &report.overlapping_range_endpoints);
|
|
|
|
}
|
2023-12-11 09:40:31 +00:00
|
|
|
|
|
|
|
// 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.
|
2023-12-15 15:53:29 +00:00
|
|
|
if tycx.refutable && report.non_exhaustiveness_witnesses.is_empty() {
|
2024-01-07 20:20:16 +00:00
|
|
|
lint_nonexhaustive_missing_variants(cx, arms, &pat_column, scrut_ty)?;
|
2023-12-11 09:40:31 +00:00
|
|
|
}
|
|
|
|
|
2024-01-07 20:20:16 +00:00
|
|
|
Ok(report)
|
2023-12-11 09:40:31 +00:00
|
|
|
}
|