Skip to content

Commit 7ff489d

Browse files
committed
Auto merge of rust-lang#119396 - Nadrieril:intersection-tracking, r=<try>
Experiment: track overlapping ranges precisely The `overlapping_range_endpoints` lint has false positives, e.g. rust-lang#117648. I expected that removing these false positives would have too much of a perf impact but never measured it. This PR is an experiment to see if the perf loss is manageable. r? `@ghost`
2 parents 2a3e635 + 2327f2e commit 7ff489d

File tree

7 files changed

+199
-131
lines changed

7 files changed

+199
-131
lines changed

compiler/rustc_pattern_analysis/src/lib.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,10 @@ pub fn analyze_match<'p, 'tcx>(
121121

122122
let pat_column = PatternColumn::new(arms);
123123

124-
// Lint on ranges that overlap on their endpoints, which is likely a mistake.
125-
lint_overlapping_range_endpoints(cx, &pat_column);
124+
// Lint ranges that overlap on their endpoints, which is likely a mistake.
125+
if !report.overlapping_range_endpoints.is_empty() {
126+
lint_overlapping_range_endpoints(cx, &report.overlapping_range_endpoints);
127+
}
126128

127129
// Run the non_exhaustive_omitted_patterns lint. Only run on refutable patterns to avoid hitting
128130
// `if let`s. Only run if the match is exhaustive otherwise the error is redundant.

compiler/rustc_pattern_analysis/src/lints.rs

+20-82
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,12 @@
1-
use smallvec::SmallVec;
2-
3-
use rustc_data_structures::captures::Captures;
4-
use rustc_middle::ty::{self, Ty};
1+
use rustc_middle::ty::Ty;
52
use rustc_session::lint;
63
use rustc_session::lint::builtin::NON_EXHAUSTIVE_OMITTED_PATTERNS;
7-
use rustc_span::Span;
84

9-
use crate::constructor::{IntRange, MaybeInfiniteInt};
105
use crate::errors::{
11-
NonExhaustiveOmittedPattern, NonExhaustiveOmittedPatternLintOnArm, Overlap,
12-
OverlappingRangeEndpoints, Uncovered,
6+
self, NonExhaustiveOmittedPattern, NonExhaustiveOmittedPatternLintOnArm, Uncovered,
137
};
148
use crate::rustc::{
15-
Constructor, DeconstructedPat, MatchArm, MatchCtxt, PlaceCtxt, RustcMatchCheckCtxt,
9+
self, Constructor, DeconstructedPat, MatchArm, MatchCtxt, PlaceCtxt, RustcMatchCheckCtxt,
1610
SplitConstructorSet, WitnessPat,
1711
};
1812
use crate::TypeCx;
@@ -64,10 +58,6 @@ impl<'p, 'tcx> PatternColumn<'p, 'tcx> {
6458
pcx.ctors_for_ty().split(pcx, column_ctors)
6559
}
6660

67-
fn iter(&self) -> impl Iterator<Item = &'p DeconstructedPat<'p, 'tcx>> + Captures<'_> {
68-
self.patterns.iter().copied()
69-
}
70-
7161
/// Does specialization: given a constructor, this takes the patterns from the column that match
7262
/// the constructor, and outputs their fields.
7363
/// This returns one column per field of the constructor. They usually all have the same length
@@ -212,77 +202,25 @@ pub(crate) fn lint_nonexhaustive_missing_variants<'a, 'p, 'tcx>(
212202
}
213203
}
214204

215-
/// Traverse the patterns to warn the user about ranges that overlap on their endpoints.
216-
#[instrument(level = "debug", skip(cx))]
217205
pub(crate) fn lint_overlapping_range_endpoints<'a, 'p, 'tcx>(
218206
cx: MatchCtxt<'a, 'p, 'tcx>,
219-
column: &PatternColumn<'p, 'tcx>,
207+
overlapping_range_endpoints: &[rustc::OverlappingRanges<'p, 'tcx>],
220208
) {
221-
let Some(ty) = column.head_ty(cx) else {
222-
return;
223-
};
224-
let pcx = &PlaceCtxt::new_dummy(cx, ty);
225-
let rcx: &RustcMatchCheckCtxt<'_, '_> = cx.tycx;
226-
227-
let set = column.analyze_ctors(pcx);
228-
229-
if matches!(ty.kind(), ty::Char | ty::Int(_) | ty::Uint(_)) {
230-
let emit_lint = |overlap: &IntRange, this_span: Span, overlapped_spans: &[Span]| {
231-
let overlap_as_pat = rcx.hoist_pat_range(overlap, ty);
232-
let overlaps: Vec<_> = overlapped_spans
233-
.iter()
234-
.copied()
235-
.map(|span| Overlap { range: overlap_as_pat.clone(), span })
236-
.collect();
237-
rcx.tcx.emit_spanned_lint(
238-
lint::builtin::OVERLAPPING_RANGE_ENDPOINTS,
239-
rcx.match_lint_level,
240-
this_span,
241-
OverlappingRangeEndpoints { overlap: overlaps, range: this_span },
242-
);
243-
};
244-
245-
// If two ranges overlapped, the split set will contain their intersection as a singleton.
246-
let split_int_ranges = set.present.iter().filter_map(|c| c.as_int_range());
247-
for overlap_range in split_int_ranges.clone() {
248-
if overlap_range.is_singleton() {
249-
let overlap: MaybeInfiniteInt = overlap_range.lo;
250-
// Ranges that look like `lo..=overlap`.
251-
let mut prefixes: SmallVec<[_; 1]> = Default::default();
252-
// Ranges that look like `overlap..=hi`.
253-
let mut suffixes: SmallVec<[_; 1]> = Default::default();
254-
// Iterate on patterns that contained `overlap`.
255-
for pat in column.iter() {
256-
let Constructor::IntRange(this_range) = pat.ctor() else { continue };
257-
let this_span = pat.data().unwrap().span;
258-
if this_range.is_singleton() {
259-
// Don't lint when one of the ranges is a singleton.
260-
continue;
261-
}
262-
if this_range.lo == overlap {
263-
// `this_range` looks like `overlap..=this_range.hi`; it overlaps with any
264-
// ranges that look like `lo..=overlap`.
265-
if !prefixes.is_empty() {
266-
emit_lint(overlap_range, this_span, &prefixes);
267-
}
268-
suffixes.push(this_span)
269-
} else if this_range.hi == overlap.plus_one() {
270-
// `this_range` looks like `this_range.lo..=overlap`; it overlaps with any
271-
// ranges that look like `overlap..=hi`.
272-
if !suffixes.is_empty() {
273-
emit_lint(overlap_range, this_span, &suffixes);
274-
}
275-
prefixes.push(this_span)
276-
}
277-
}
278-
}
279-
}
280-
} else {
281-
// Recurse into the fields.
282-
for ctor in set.present {
283-
for col in column.specialize(pcx, &ctor) {
284-
lint_overlapping_range_endpoints(cx, &col);
285-
}
286-
}
209+
let rcx = cx.tycx;
210+
for overlap in overlapping_range_endpoints {
211+
let overlap_as_pat = rcx.hoist_pat_range(&overlap.overlaps_on, overlap.pat.ty());
212+
let overlaps: Vec<_> = overlap
213+
.overlaps_with
214+
.iter()
215+
.map(|pat| pat.data().unwrap().span)
216+
.map(|span| errors::Overlap { range: overlap_as_pat.clone(), span })
217+
.collect();
218+
let pat_span = overlap.pat.data().unwrap().span;
219+
rcx.tcx.emit_spanned_lint(
220+
lint::builtin::OVERLAPPING_RANGE_ENDPOINTS,
221+
rcx.match_lint_level,
222+
pat_span,
223+
errors::OverlappingRangeEndpoints { overlap: overlaps, range: pat_span },
224+
);
287225
}
288226
}

compiler/rustc_pattern_analysis/src/rustc.rs

+2
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ pub type DeconstructedPat<'p, 'tcx> =
3232
crate::pat::DeconstructedPat<'p, RustcMatchCheckCtxt<'p, 'tcx>>;
3333
pub type MatchArm<'p, 'tcx> = crate::MatchArm<'p, RustcMatchCheckCtxt<'p, 'tcx>>;
3434
pub type MatchCtxt<'a, 'p, 'tcx> = crate::MatchCtxt<'a, 'p, RustcMatchCheckCtxt<'p, 'tcx>>;
35+
pub type OverlappingRanges<'p, 'tcx> =
36+
crate::usefulness::OverlappingRanges<'p, RustcMatchCheckCtxt<'p, 'tcx>>;
3537
pub(crate) type PlaceCtxt<'a, 'p, 'tcx> =
3638
crate::usefulness::PlaceCtxt<'a, 'p, RustcMatchCheckCtxt<'p, 'tcx>>;
3739
pub(crate) type SplitConstructorSet<'p, 'tcx> =

0 commit comments

Comments
 (0)