Skip to content

[WIP] Lint self-overlapping or-patterns under guard #143541

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

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
10 changes: 10 additions & 0 deletions compiler/rustc_pattern_analysis/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,16 @@ pub trait PatCx: Sized + fmt::Debug {
_gapped_with: &[&DeconstructedPat<Self>],
) {
}

/// Lint that an or-pattern will cause a guard to be tried several times because there's a value
/// that matches several of the or-alternatives.
/// The default implementation does nothing.
fn lint_overlapping_alternatives_under_guard(
&self,
_pat1: &DeconstructedPat<Self>,
_pat2: &DeconstructedPat<Self>,
) {
}
}

/// The arm of a match expression.
Expand Down
15 changes: 15 additions & 0 deletions compiler/rustc_pattern_analysis/src/rustc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1027,6 +1027,21 @@ impl<'p, 'tcx: 'p> PatCx for RustcPatCtxt<'p, 'tcx> {
);
}
}

fn lint_overlapping_alternatives_under_guard(
&self,
pat1: &crate::pat::DeconstructedPat<Self>,
pat2: &crate::pat::DeconstructedPat<Self>,
) {
self.tcx
.dcx()
.struct_span_err(
pat1.data().span,
format!("pattern overlaps with or-alternative under a guard"),
)
.with_span_label(pat2.data().span, "overlaps with")
.emit();
}
}

/// Recursively expand this pattern into its subpatterns. Only useful for or-patterns.
Expand Down
19 changes: 18 additions & 1 deletion compiler/rustc_pattern_analysis/src/usefulness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1105,14 +1105,20 @@ impl<'p, Cx: PatCx> fmt::Debug for PatStack<'p, Cx> {
/// A row of the matrix.
#[derive(Clone)]
struct MatrixRow<'p, Cx: PatCx> {
// The patterns in the row.
/// The patterns in the row.
pats: PatStack<'p, Cx>,
/// A (sub)pattern this row comes from. When expanding or-patterns, this tracks the last
/// alternative expanded, e.g. in `(0|1, 2|3)` we'd keep `3` for the last row. Used only for
/// diagnostics.
origin: &'p DeconstructedPat<Cx>,
/// Whether the original arm had a guard. This is inherited when specializing.
is_under_guard: bool,
/// When we specialize, we remember which row of the original matrix produced a given row of the
/// specialized matrix. When we unspecialize, we use this to propagate usefulness back up the
/// callstack. On creation, this stores the index of the original match arm.
parent_row: usize,
/// Remember the match arm this came from.
arm_id: usize,
/// False when the matrix is just built. This is set to `true` by
/// [`compute_exhaustiveness_and_usefulness`] if the arm is found to be useful.
/// This is reset to `false` when specializing.
Expand Down Expand Up @@ -1142,7 +1148,9 @@ impl<'p, Cx: PatCx> MatrixRow<'p, Cx> {
fn new(arm: &MatchArm<'p, Cx>, arm_id: usize) -> Self {
MatrixRow {
pats: PatStack::from_pattern(arm.pat),
origin: arm.pat,
parent_row: arm_id,
arm_id,
is_under_guard: arm.has_guard,
useful: false,
intersects_at_least: DenseBitSet::new_empty(0), // Initialized in `Matrix::push`.
Expand All @@ -1167,8 +1175,10 @@ impl<'p, Cx: PatCx> MatrixRow<'p, Cx> {
fn expand_or_pat(&self, parent_row: usize) -> impl Iterator<Item = MatrixRow<'p, Cx>> {
let is_or_pat = self.pats.head().is_or_pat();
self.pats.expand_or_pat().map(move |patstack| MatrixRow {
origin: if is_or_pat { patstack.head().as_pat().unwrap() } else { self.origin },
pats: patstack,
parent_row,
arm_id: self.arm_id,
is_under_guard: self.is_under_guard,
useful: false,
intersects_at_least: DenseBitSet::new_empty(0), // Initialized in `Matrix::push`.
Expand All @@ -1188,7 +1198,9 @@ impl<'p, Cx: PatCx> MatrixRow<'p, Cx> {
) -> Result<MatrixRow<'p, Cx>, Cx::Error> {
Ok(MatrixRow {
pats: self.pats.pop_head_constructor(cx, ctor, ctor_arity, ctor_is_relevant)?,
origin: self.origin,
parent_row,
arm_id: self.arm_id,
is_under_guard: self.is_under_guard,
useful: false,
intersects_at_least: DenseBitSet::new_empty(0), // Initialized in `Matrix::push`.
Expand Down Expand Up @@ -1724,6 +1736,11 @@ fn compute_exhaustiveness_and_usefulness<'a, 'p, Cx: PatCx>(
// The next rows stays useful if this one is under a guard.
useful &= row.is_under_guard;
}
for (row1, row2) in matrix.rows().zip(matrix.rows().skip(1)) {
if row1.arm_id == row2.arm_id && row1.is_under_guard {
mcx.tycx.lint_overlapping_alternatives_under_guard(row1.origin, row2.origin);
}
}
return if useful && matrix.wildcard_row_is_relevant {
// The wildcard row is useful; the match is non-exhaustive.
Ok(WitnessMatrix::unit_witness())
Expand Down
Loading