2023-12-10 19:42:30 +00:00
|
|
|
//! Analysis of patterns, notably match exhaustiveness checking.
|
|
|
|
|
Use `tidy` to sort crate attributes for all compiler crates.
We already do this for a number of crates, e.g. `rustc_middle`,
`rustc_span`, `rustc_metadata`, `rustc_span`, `rustc_errors`.
For the ones we don't, in many cases the attributes are a mess.
- There is no consistency about order of attribute kinds (e.g.
`allow`/`deny`/`feature`).
- Within attribute kind groups (e.g. the `feature` attributes),
sometimes the order is alphabetical, and sometimes there is no
particular order.
- Sometimes the attributes of a particular kind aren't even grouped
all together, e.g. there might be a `feature`, then an `allow`, then
another `feature`.
This commit extends the existing sorting to all compiler crates,
increasing consistency. If any new attribute line is added there is now
only one place it can go -- no need for arbitrary decisions.
Exceptions:
- `rustc_log`, `rustc_next_trait_solver` and `rustc_type_ir_macros`,
because they have no crate attributes.
- `rustc_codegen_gcc`, because it's quasi-external to rustc (e.g. it's
ignored in `rustfmt.toml`).
2024-06-12 03:49:36 +00:00
|
|
|
// tidy-alphabetical-start
|
2024-02-05 22:51:39 +00:00
|
|
|
#![allow(rustc::diagnostic_outside_of_impl)]
|
Use `tidy` to sort crate attributes for all compiler crates.
We already do this for a number of crates, e.g. `rustc_middle`,
`rustc_span`, `rustc_metadata`, `rustc_span`, `rustc_errors`.
For the ones we don't, in many cases the attributes are a mess.
- There is no consistency about order of attribute kinds (e.g.
`allow`/`deny`/`feature`).
- Within attribute kind groups (e.g. the `feature` attributes),
sometimes the order is alphabetical, and sometimes there is no
particular order.
- Sometimes the attributes of a particular kind aren't even grouped
all together, e.g. there might be a `feature`, then an `allow`, then
another `feature`.
This commit extends the existing sorting to all compiler crates,
increasing consistency. If any new attribute line is added there is now
only one place it can go -- no need for arbitrary decisions.
Exceptions:
- `rustc_log`, `rustc_next_trait_solver` and `rustc_type_ir_macros`,
because they have no crate attributes.
- `rustc_codegen_gcc`, because it's quasi-external to rustc (e.g. it's
ignored in `rustfmt.toml`).
2024-06-12 03:49:36 +00:00
|
|
|
#![allow(rustc::untranslatable_diagnostic)]
|
|
|
|
// tidy-alphabetical-end
|
2024-02-05 22:51:39 +00:00
|
|
|
|
2023-12-10 19:42:30 +00:00
|
|
|
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;
|
2024-01-24 20:29:58 +00:00
|
|
|
pub mod pat_column;
|
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;
|
|
|
|
|
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;
|
|
|
|
|
2024-03-30 15:18:18 +00:00
|
|
|
// Re-exports to avoid rustc_index version issues.
|
|
|
|
pub use rustc_index::Idx;
|
|
|
|
pub use rustc_index::IndexVec;
|
2024-01-17 01:59:47 +00:00
|
|
|
|
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
|
|
|
|
2024-01-15 18:17:28 +00:00
|
|
|
use crate::constructor::{Constructor, ConstructorSet, IntRange};
|
2023-12-11 09:40:31 +00:00
|
|
|
use crate::pat::DeconstructedPat;
|
2024-01-24 20:29:58 +00:00
|
|
|
use crate::pat_column::PatternColumn;
|
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 {}
|
|
|
|
|
2024-02-28 16:56:01 +00:00
|
|
|
/// `bool` newtype that indicates whether this is a privately uninhabited field that we should skip
|
|
|
|
/// during analysis.
|
2024-02-06 01:44:48 +00:00
|
|
|
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
2024-02-28 16:56:01 +00:00
|
|
|
pub struct PrivateUninhabitedField(pub bool);
|
2024-02-06 01:44:48 +00:00
|
|
|
|
2023-12-15 15:53:29 +00:00
|
|
|
/// Context that provides type information about constructors.
|
|
|
|
///
|
|
|
|
/// Most of the crate is parameterized on a type that implements this trait.
|
2024-03-13 12:56:06 +00:00
|
|
|
pub trait PatCx: Sized + fmt::Debug {
|
2023-12-15 15:18:21 +00:00
|
|
|
/// The type of a pattern.
|
2023-12-22 23:21:27 +00:00
|
|
|
type Ty: Clone + fmt::Debug;
|
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.
|
2024-03-30 15:18:18 +00:00
|
|
|
type VariantIdx: Clone + Idx + fmt::Debug;
|
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;
|
2024-01-18 18:22:44 +00:00
|
|
|
fn is_min_exhaustive_patterns_feature_on(&self) -> bool;
|
2023-12-11 19:01:02 +00:00
|
|
|
|
|
|
|
/// The number of fields for this constructor.
|
2023-12-22 23:21:27 +00:00
|
|
|
fn ctor_arity(&self, ctor: &Constructor<Self>, ty: &Self::Ty) -> usize;
|
2023-12-11 19:01:02 +00:00
|
|
|
|
2024-02-06 02:50:22 +00:00
|
|
|
/// The types of the fields for this constructor. The result must contain `ctor_arity()` fields.
|
2024-01-24 15:55:26 +00:00
|
|
|
fn ctor_sub_tys<'a>(
|
|
|
|
&'a self,
|
|
|
|
ctor: &'a Constructor<Self>,
|
|
|
|
ty: &'a Self::Ty,
|
2024-02-28 16:56:01 +00:00
|
|
|
) -> impl Iterator<Item = (Self::Ty, PrivateUninhabitedField)> + ExactSizeIterator + Captures<'a>;
|
2023-12-11 19:01:02 +00:00
|
|
|
|
|
|
|
/// The set of all the constructors for `ty`.
|
|
|
|
///
|
|
|
|
/// This must follow the invariants of `ConstructorSet`
|
2023-12-22 23:21:27 +00:00
|
|
|
fn ctors_for_ty(&self, ty: &Self::Ty) -> Result<ConstructorSet<Self>, Self::Error>;
|
2023-12-11 19:01:02 +00:00
|
|
|
|
2024-01-24 19:04:33 +00:00
|
|
|
/// Write the name of the variant represented by `pat`. Used for the best-effort `Debug` impl of
|
|
|
|
/// `DeconstructedPat`. Only invoqued when `pat.ctor()` is `Struct | Variant(_) | UnionField`.
|
|
|
|
fn write_variant_name(
|
|
|
|
f: &mut fmt::Formatter<'_>,
|
2024-03-04 23:16:11 +00:00
|
|
|
ctor: &crate::constructor::Constructor<Self>,
|
|
|
|
ty: &Self::Ty,
|
2024-01-24 19:04:33 +00:00
|
|
|
) -> fmt::Result;
|
2023-12-11 19:01:02 +00:00
|
|
|
|
|
|
|
/// Raise a bug.
|
2024-01-24 15:24:52 +00:00
|
|
|
fn bug(&self, fmt: fmt::Arguments<'_>) -> Self::Error;
|
2024-01-15 18:17:28 +00:00
|
|
|
|
|
|
|
/// Lint that the range `pat` overlapped with all the ranges in `overlaps_with`, where the range
|
|
|
|
/// they overlapped over is `overlaps_on`. We only detect singleton overlaps.
|
|
|
|
/// The default implementation does nothing.
|
|
|
|
fn lint_overlapping_range_endpoints(
|
|
|
|
&self,
|
2024-01-25 02:37:24 +00:00
|
|
|
_pat: &DeconstructedPat<Self>,
|
2024-01-15 18:17:28 +00:00
|
|
|
_overlaps_on: IntRange,
|
2024-01-25 02:37:24 +00:00
|
|
|
_overlaps_with: &[&DeconstructedPat<Self>],
|
2024-01-15 18:17:28 +00:00
|
|
|
) {
|
|
|
|
}
|
2024-03-02 21:48:41 +00:00
|
|
|
|
|
|
|
/// The maximum pattern complexity limit was reached.
|
|
|
|
fn complexity_exceeded(&self) -> Result<(), Self::Error>;
|
2024-01-14 21:24:10 +00:00
|
|
|
|
|
|
|
/// Lint that there is a gap `gap` between `pat` and all of `gapped_with` such that the gap is
|
|
|
|
/// not matched by another range. If `gapped_with` is empty, then `gap` is `T::MAX`. We only
|
|
|
|
/// detect singleton gaps.
|
|
|
|
/// The default implementation does nothing.
|
|
|
|
fn lint_non_contiguous_range_endpoints(
|
|
|
|
&self,
|
|
|
|
_pat: &DeconstructedPat<Self>,
|
|
|
|
_gap: IntRange,
|
|
|
|
_gapped_with: &[&DeconstructedPat<Self>],
|
|
|
|
) {
|
|
|
|
}
|
2023-12-11 19:01:02 +00:00
|
|
|
}
|
|
|
|
|
2023-12-11 09:40:31 +00:00
|
|
|
/// The arm of a match expression.
|
2023-12-19 17:09:31 +00:00
|
|
|
#[derive(Debug)]
|
2024-03-13 12:56:06 +00:00
|
|
|
pub struct MatchArm<'p, Cx: PatCx> {
|
2024-01-25 02:37:24 +00:00
|
|
|
pub pat: &'p DeconstructedPat<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
|
|
|
}
|
|
|
|
|
2024-03-13 12:56:06 +00:00
|
|
|
impl<'p, Cx: PatCx> Clone for MatchArm<'p, Cx> {
|
2024-01-27 12:18:33 +00:00
|
|
|
fn clone(&self) -> Self {
|
|
|
|
Self { pat: self.pat, has_guard: self.has_guard, arm_data: self.arm_data }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-13 12:56:06 +00:00
|
|
|
impl<'p, Cx: PatCx> Copy for MatchArm<'p, Cx> {}
|
2024-01-27 12:18:33 +00:00
|
|
|
|
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>(
|
2024-03-13 13:07:44 +00:00
|
|
|
tycx: &rustc::RustcPatCtxt<'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-03-02 21:48:41 +00:00
|
|
|
pattern_complexity_limit: Option<usize>,
|
2024-01-07 20:20:16 +00:00
|
|
|
) -> Result<rustc::UsefulnessReport<'p, 'tcx>, ErrorGuaranteed> {
|
2024-01-14 21:24:10 +00:00
|
|
|
use lints::lint_nonexhaustive_missing_variants;
|
2024-03-13 12:53:18 +00:00
|
|
|
use usefulness::{compute_match_usefulness, PlaceValidity};
|
2024-01-14 21:24:10 +00:00
|
|
|
|
2023-12-26 12:21:35 +00:00
|
|
|
let scrut_ty = tycx.reveal_opaque_ty(scrut_ty);
|
2024-03-13 12:53:18 +00:00
|
|
|
let scrut_validity = PlaceValidity::from_bool(tycx.known_valid_scrutinee);
|
2024-03-02 21:48:41 +00:00
|
|
|
let report =
|
|
|
|
compute_match_usefulness(tycx, arms, scrut_ty, scrut_validity, pattern_complexity_limit)?;
|
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-15 18:17:28 +00:00
|
|
|
let pat_column = PatternColumn::new(arms);
|
2024-01-24 20:16:57 +00:00
|
|
|
lint_nonexhaustive_missing_variants(tycx, 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
|
|
|
}
|