Skip to content

Commit b58be7c

Browse files
committed
Auto merge of #119396 - Nadrieril:intersection-tracking, r=<try>
Experiment: track overlapping ranges precisely The `overlapping_range_endpoints` lint has false positives, e.g. #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 95613d1 + 80df6f9 commit b58be7c

File tree

6 files changed

+156
-128
lines changed

6 files changed

+156
-128
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

+19-80
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
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::{
116
NonExhaustiveOmittedPattern, NonExhaustiveOmittedPatternLintOnArm, Overlap,
127
OverlappingRangeEndpoints, Uncovered,
138
};
149
use crate::rustc::{
15-
Constructor, DeconstructedPat, MatchArm, MatchCtxt, PlaceCtxt, RustcMatchCheckCtxt,
10+
self, Constructor, DeconstructedPat, MatchArm, MatchCtxt, PlaceCtxt, RustcMatchCheckCtxt,
1611
SplitConstructorSet, WitnessPat,
1712
};
1813
use crate::TypeCx;
@@ -64,10 +59,6 @@ impl<'p, 'tcx> PatternColumn<'p, 'tcx> {
6459
pcx.ctors_for_ty().split(pcx, column_ctors)
6560
}
6661

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

215-
/// Traverse the patterns to warn the user about ranges that overlap on their endpoints.
216-
#[instrument(level = "debug", skip(cx))]
217206
pub(crate) fn lint_overlapping_range_endpoints<'a, 'p, 'tcx>(
218207
cx: MatchCtxt<'a, 'p, 'tcx>,
219-
column: &PatternColumn<'p, 'tcx>,
208+
overlapping_range_endpoints: &[rustc::OverlappingRanges<'p, 'tcx>],
220209
) {
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-
}
210+
let rcx = cx.tycx;
211+
for overlap in overlapping_range_endpoints {
212+
let overlap_as_pat = rcx.hoist_pat_range(&overlap.overlaps_on, overlap.pat.ty());
213+
let overlaps: Vec<_> = overlap
214+
.overlaps_with
215+
.iter()
216+
.map(|pat| pat.data().unwrap().span)
217+
.map(|span| Overlap { range: overlap_as_pat.clone(), span })
218+
.collect();
219+
let pat_span = overlap.pat.data().unwrap().span;
220+
rcx.tcx.emit_spanned_lint(
221+
lint::builtin::OVERLAPPING_RANGE_ENDPOINTS,
222+
rcx.match_lint_level,
223+
pat_span,
224+
OverlappingRangeEndpoints { overlap: overlaps, range: pat_span },
225+
);
287226
}
288227
}

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)