rust/compiler/rustc_pattern_analysis/src/lib.rs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

111 lines
3.7 KiB
Rust
Raw Normal View History

//! Analysis of patterns, notably match exhaustiveness checking.
pub mod constructor;
#[cfg(feature = "rustc")]
pub mod errors;
#[cfg(feature = "rustc")]
2023-12-11 09:40:31 +00:00
pub(crate) mod lints;
pub mod pat;
#[cfg(feature = "rustc")]
2023-12-11 19:15:52 +00:00
pub mod rustc;
pub mod usefulness;
#[macro_use]
extern crate tracing;
#[cfg(feature = "rustc")]
#[macro_use]
extern crate rustc_middle;
#[cfg(feature = "rustc")]
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;
#[cfg(feature = "rustc")]
2023-12-11 09:40:31 +00:00
use rustc_middle::ty::Ty;
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;
#[cfg(feature = "rustc")]
2023-12-14 16:54:11 +00:00
use crate::rustc::RustcMatchCheckCtxt;
#[cfg(feature = "rustc")]
use crate::usefulness::{compute_match_usefulness, ValidityConstraint};
2023-12-11 09:40:31 +00:00
2023-12-11 19:01:02 +00:00
pub trait MatchCx: Sized + Clone + fmt::Debug {
type Ty: Copy + Clone + fmt::Debug; // FIXME: remove Copy
type Span: Clone + Default;
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-11 19:45:34 +00:00
/// Extra data to store on a match arm.
type ArmData: Copy + Clone + fmt::Debug;
2023-12-11 19:01:02 +00:00
fn is_opaque_ty(ty: Self::Ty) -> bool;
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`
fn ctors_for_ty(&self, ty: Self::Ty) -> ConstructorSet<Self>;
/// 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-11 09:40:31 +00:00
/// The arm of a match expression.
2023-12-11 19:01:02 +00:00
#[derive(Clone, Debug)]
pub struct MatchArm<'p, Cx: MatchCx> {
2023-12-11 09:40:31 +00:00
/// The pattern must have been lowered through `check_match::MatchVisitor::lower_pattern`.
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
}
2023-12-11 19:01:02 +00:00
impl<'p, Cx: MatchCx> Copy for MatchArm<'p, Cx> {}
pub trait Captures<'a> {}
impl<'a, T: ?Sized> Captures<'a> for T {}
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.
#[cfg(feature = "rustc")]
2023-12-11 09:40:31 +00:00
pub fn analyze_match<'p, 'tcx>(
2023-12-14 16:54:11 +00:00
cx: &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>,
2023-12-11 19:15:52 +00:00
) -> rustc::UsefulnessReport<'p, 'tcx> {
// Arena to store the extra wildcards we construct during analysis.
let wildcard_arena = cx.pattern_arena;
2023-12-11 09:40:31 +00:00
let pat_column = PatternColumn::new(arms);
2023-12-11 19:01:02 +00:00
let scrut_validity = ValidityConstraint::from_bool(cx.known_valid_scrutinee);
let report = compute_match_usefulness(cx, arms, scrut_ty, scrut_validity, wildcard_arena);
2023-12-11 09:40:31 +00:00
// Lint on ranges that overlap on their endpoints, which is likely a mistake.
lint_overlapping_range_endpoints(cx, &pat_column, wildcard_arena);
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.
if cx.refutable && report.non_exhaustiveness_witnesses.is_empty() {
lint_nonexhaustive_missing_variants(cx, arms, &pat_column, scrut_ty, wildcard_arena)
2023-12-11 09:40:31 +00:00
}
report
}