Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit ab918af

Browse files
committedNov 5, 2023
Cleanup span passing
1 parent 0075a5f commit ab918af

File tree

3 files changed

+37
-31
lines changed

3 files changed

+37
-31
lines changed
 

‎compiler/rustc_mir_build/src/thir/pattern/check_match.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -291,6 +291,7 @@ impl<'thir, 'p, 'tcx> MatchVisitor<'thir, 'p, 'tcx> {
291291
&self,
292292
refutability: RefutableFlag,
293293
match_span: Option<Span>,
294+
scrut_span: Span,
294295
) -> MatchCheckCtxt<'p, 'tcx> {
295296
let refutable = match refutability {
296297
Irrefutable => false,
@@ -301,7 +302,9 @@ impl<'thir, 'p, 'tcx> MatchVisitor<'thir, 'p, 'tcx> {
301302
param_env: self.param_env,
302303
module: self.tcx.parent_module(self.lint_level).to_def_id(),
303304
pattern_arena: &self.pattern_arena,
305+
match_lint_level: self.lint_level,
304306
match_span,
307+
scrut_span,
305308
refutable,
306309
}
307310
}
@@ -332,7 +335,8 @@ impl<'thir, 'p, 'tcx> MatchVisitor<'thir, 'p, 'tcx> {
332335
source: hir::MatchSource,
333336
expr_span: Span,
334337
) {
335-
let cx = self.new_cx(Refutable, Some(expr_span));
338+
let scrut = &self.thir[scrut];
339+
let cx = self.new_cx(Refutable, Some(expr_span), scrut.span);
336340

337341
let mut tarms = Vec::with_capacity(arms.len());
338342
for &arm in arms {
@@ -348,9 +352,8 @@ impl<'thir, 'p, 'tcx> MatchVisitor<'thir, 'p, 'tcx> {
348352
}
349353
}
350354

351-
let scrut = &self.thir[scrut];
352355
let scrut_ty = scrut.ty;
353-
let report = compute_match_usefulness(&cx, &tarms, self.lint_level, scrut_ty, scrut.span);
356+
let report = compute_match_usefulness(&cx, &tarms, scrut_ty);
354357

355358
match source {
356359
// Don't report arm reachability of desugared `match $iter.into_iter() { iter => .. }`
@@ -455,10 +458,10 @@ impl<'thir, 'p, 'tcx> MatchVisitor<'thir, 'p, 'tcx> {
455458
pat: &Pat<'tcx>,
456459
refutability: RefutableFlag,
457460
) -> Result<(MatchCheckCtxt<'p, 'tcx>, UsefulnessReport<'p, 'tcx>), ErrorGuaranteed> {
458-
let cx = self.new_cx(refutability, None);
461+
let cx = self.new_cx(refutability, None, pat.span);
459462
let pat = self.lower_pattern(&cx, pat)?;
460463
let arms = [MatchArm { pat, hir_id: self.lint_level, has_guard: false }];
461-
let report = compute_match_usefulness(&cx, &arms, self.lint_level, pat.ty(), pat.span());
464+
let report = compute_match_usefulness(&cx, &arms, pat.ty());
462465
Ok((cx, report))
463466
}
464467

‎compiler/rustc_mir_build/src/thir/pattern/deconstruct_pat.rs

+14-10
Original file line numberDiff line numberDiff line change
@@ -806,7 +806,12 @@ impl<'tcx> Constructor<'tcx> {
806806
#[inline]
807807
pub(super) fn is_covered_by<'p>(&self, pcx: &PatCtxt<'_, 'p, 'tcx>, other: &Self) -> bool {
808808
match (self, other) {
809-
(Wildcard, _) => bug!("Constructor splitting should not have returned `Wildcard`"),
809+
(Wildcard, _) => {
810+
span_bug!(
811+
pcx.cx.scrut_span,
812+
"Constructor splitting should not have returned `Wildcard`"
813+
)
814+
}
810815
// Wildcards cover anything
811816
(_, Wildcard) => true,
812817
// Only a wildcard pattern can match these special constructors.
@@ -846,7 +851,7 @@ impl<'tcx> Constructor<'tcx> {
846851
(Opaque(..), _) | (_, Opaque(..)) => false,
847852

848853
_ => span_bug!(
849-
pcx.span,
854+
pcx.cx.scrut_span,
850855
"trying to compare incompatible constructors {:?} and {:?}",
851856
self,
852857
other
@@ -1247,9 +1252,8 @@ impl<'p, 'tcx> Fields<'p, 'tcx> {
12471252
fn wildcards_from_tys(
12481253
cx: &MatchCheckCtxt<'p, 'tcx>,
12491254
tys: impl IntoIterator<Item = Ty<'tcx>>,
1250-
span: Span,
12511255
) -> Self {
1252-
Fields::from_iter(cx, tys.into_iter().map(|ty| DeconstructedPat::wildcard(ty, span)))
1256+
Fields::from_iter(cx, tys.into_iter().map(|ty| DeconstructedPat::wildcard(ty, DUMMY_SP)))
12531257
}
12541258

12551259
// In the cases of either a `#[non_exhaustive]` field list or a non-public field, we hide
@@ -1285,26 +1289,26 @@ impl<'p, 'tcx> Fields<'p, 'tcx> {
12851289
pub(super) fn wildcards(pcx: &PatCtxt<'_, 'p, 'tcx>, constructor: &Constructor<'tcx>) -> Self {
12861290
let ret = match constructor {
12871291
Single | Variant(_) => match pcx.ty.kind() {
1288-
ty::Tuple(fs) => Fields::wildcards_from_tys(pcx.cx, fs.iter(), pcx.span),
1289-
ty::Ref(_, rty, _) => Fields::wildcards_from_tys(pcx.cx, once(*rty), pcx.span),
1292+
ty::Tuple(fs) => Fields::wildcards_from_tys(pcx.cx, fs.iter()),
1293+
ty::Ref(_, rty, _) => Fields::wildcards_from_tys(pcx.cx, once(*rty)),
12901294
ty::Adt(adt, args) => {
12911295
if adt.is_box() {
12921296
// The only legal patterns of type `Box` (outside `std`) are `_` and box
12931297
// patterns. If we're here we can assume this is a box pattern.
1294-
Fields::wildcards_from_tys(pcx.cx, once(args.type_at(0)), pcx.span)
1298+
Fields::wildcards_from_tys(pcx.cx, once(args.type_at(0)))
12951299
} else {
12961300
let variant = &adt.variant(constructor.variant_index_for_adt(*adt));
12971301
let tys = Fields::list_variant_nonhidden_fields(pcx.cx, pcx.ty, variant)
12981302
.map(|(_, ty)| ty);
1299-
Fields::wildcards_from_tys(pcx.cx, tys, pcx.span)
1303+
Fields::wildcards_from_tys(pcx.cx, tys)
13001304
}
13011305
}
13021306
_ => bug!("Unexpected type for `Single` constructor: {:?}", pcx),
13031307
},
13041308
Slice(slice) => match *pcx.ty.kind() {
13051309
ty::Slice(ty) | ty::Array(ty, _) => {
13061310
let arity = slice.arity();
1307-
Fields::wildcards_from_tys(pcx.cx, (0..arity).map(|_| ty), pcx.span)
1311+
Fields::wildcards_from_tys(pcx.cx, (0..arity).map(|_| ty))
13081312
}
13091313
_ => bug!("bad slice pattern {:?} {:?}", constructor, pcx),
13101314
},
@@ -1623,7 +1627,7 @@ impl<'p, 'tcx> DeconstructedPat<'p, 'tcx> {
16231627
let wildcard: &_ = pcx
16241628
.cx
16251629
.pattern_arena
1626-
.alloc(DeconstructedPat::wildcard(inner_ty, pcx.span));
1630+
.alloc(DeconstructedPat::wildcard(inner_ty, DUMMY_SP));
16271631
let extra_wildcards = other_slice.arity() - self_slice.arity();
16281632
let extra_wildcards = (0..extra_wildcards).map(|_| wildcard);
16291633
prefix.iter().chain(extra_wildcards).chain(suffix).collect()

‎compiler/rustc_mir_build/src/thir/pattern/usefulness.rs

+15-16
Original file line numberDiff line numberDiff line change
@@ -521,8 +521,12 @@ pub(crate) struct MatchCheckCtxt<'p, 'tcx> {
521521
pub(crate) module: DefId,
522522
pub(crate) param_env: ty::ParamEnv<'tcx>,
523523
pub(crate) pattern_arena: &'p TypedArena<DeconstructedPat<'p, 'tcx>>,
524+
/// Lint level at the match.
525+
pub(crate) match_lint_level: HirId,
524526
/// The span of the whole match, if applicable.
525527
pub(crate) match_span: Option<Span>,
528+
/// Span of the scrutinee.
529+
pub(crate) scrut_span: Span,
526530
/// Only produce `NON_EXHAUSTIVE_OMITTED_PATTERNS` lint on refutable patterns.
527531
pub(crate) refutable: bool,
528532
}
@@ -552,8 +556,6 @@ pub(super) struct PatCtxt<'a, 'p, 'tcx> {
552556
pub(super) cx: &'a MatchCheckCtxt<'p, 'tcx>,
553557
/// Type of the current column under investigation.
554558
pub(super) ty: Ty<'tcx>,
555-
/// Span of the current pattern under investigation.
556-
pub(super) span: Span,
557559
/// Whether the current pattern is the whole pattern as found in a match arm, or if it's a
558560
/// subpattern.
559561
pub(super) is_top_level: bool,
@@ -1023,7 +1025,7 @@ fn compute_exhaustiveness_and_reachability<'p, 'tcx>(
10231025
};
10241026

10251027
debug!("ty: {ty:?}");
1026-
let pcx = &PatCtxt { cx, ty, span: DUMMY_SP, is_top_level };
1028+
let pcx = &PatCtxt { cx, ty, is_top_level };
10271029

10281030
// Analyze the constructors present in this column.
10291031
let ctors = matrix.heads().map(|p| p.ctor());
@@ -1167,7 +1169,7 @@ fn collect_nonexhaustive_missing_variants<'p, 'tcx>(
11671169
let Some(ty) = column.head_ty() else {
11681170
return Vec::new();
11691171
};
1170-
let pcx = &PatCtxt { cx, ty, span: DUMMY_SP, is_top_level: false };
1172+
let pcx = &PatCtxt { cx, ty, is_top_level: false };
11711173

11721174
let set = column.analyze_ctors(pcx);
11731175
if set.present.is_empty() {
@@ -1208,16 +1210,15 @@ fn collect_nonexhaustive_missing_variants<'p, 'tcx>(
12081210
}
12091211

12101212
/// Traverse the patterns to warn the user about ranges that overlap on their endpoints.
1211-
#[instrument(level = "debug", skip(cx, lint_root))]
1213+
#[instrument(level = "debug", skip(cx))]
12121214
fn lint_overlapping_range_endpoints<'p, 'tcx>(
12131215
cx: &MatchCheckCtxt<'p, 'tcx>,
12141216
column: &PatternColumn<'p, 'tcx>,
1215-
lint_root: HirId,
12161217
) {
12171218
let Some(ty) = column.head_ty() else {
12181219
return;
12191220
};
1220-
let pcx = &PatCtxt { cx, ty, span: DUMMY_SP, is_top_level: false };
1221+
let pcx = &PatCtxt { cx, ty, is_top_level: false };
12211222

12221223
let set = column.analyze_ctors(pcx);
12231224

@@ -1231,7 +1232,7 @@ fn lint_overlapping_range_endpoints<'p, 'tcx>(
12311232
.collect();
12321233
cx.tcx.emit_spanned_lint(
12331234
lint::builtin::OVERLAPPING_RANGE_ENDPOINTS,
1234-
lint_root,
1235+
cx.match_lint_level,
12351236
this_span,
12361237
OverlappingRangeEndpoints { overlap: overlaps, range: this_span },
12371238
);
@@ -1276,7 +1277,7 @@ fn lint_overlapping_range_endpoints<'p, 'tcx>(
12761277
// Recurse into the fields.
12771278
for ctor in set.present {
12781279
for col in column.specialize(pcx, &ctor) {
1279-
lint_overlapping_range_endpoints(cx, &col, lint_root);
1280+
lint_overlapping_range_endpoints(cx, &col);
12801281
}
12811282
}
12821283
}
@@ -1317,9 +1318,7 @@ pub(crate) struct UsefulnessReport<'p, 'tcx> {
13171318
pub(crate) fn compute_match_usefulness<'p, 'tcx>(
13181319
cx: &MatchCheckCtxt<'p, 'tcx>,
13191320
arms: &[MatchArm<'p, 'tcx>],
1320-
lint_root: HirId,
13211321
scrut_ty: Ty<'tcx>,
1322-
scrut_span: Span,
13231322
) -> UsefulnessReport<'p, 'tcx> {
13241323
let mut matrix = Matrix::new(cx, arms.iter(), scrut_ty);
13251324
let non_exhaustiveness_witnesses =
@@ -1343,13 +1342,13 @@ pub(crate) fn compute_match_usefulness<'p, 'tcx>(
13431342

13441343
let pat_column = PatternColumn::new(matrix.heads().collect());
13451344
// Lint on ranges that overlap on their endpoints, which is likely a mistake.
1346-
lint_overlapping_range_endpoints(cx, &pat_column, lint_root);
1345+
lint_overlapping_range_endpoints(cx, &pat_column);
13471346

13481347
// Run the non_exhaustive_omitted_patterns lint. Only run on refutable patterns to avoid hitting
13491348
// `if let`s. Only run if the match is exhaustive otherwise the error is redundant.
13501349
if cx.refutable && report.non_exhaustiveness_witnesses.is_empty() {
13511350
if !matches!(
1352-
cx.tcx.lint_level_at_node(NON_EXHAUSTIVE_OMITTED_PATTERNS, lint_root).0,
1351+
cx.tcx.lint_level_at_node(NON_EXHAUSTIVE_OMITTED_PATTERNS, cx.match_lint_level).0,
13531352
rustc_session::lint::Level::Allow
13541353
) {
13551354
let witnesses = collect_nonexhaustive_missing_variants(cx, &pat_column);
@@ -1360,11 +1359,11 @@ pub(crate) fn compute_match_usefulness<'p, 'tcx>(
13601359
// NB: The partner lint for structs lives in `compiler/rustc_hir_analysis/src/check/pat.rs`.
13611360
cx.tcx.emit_spanned_lint(
13621361
NON_EXHAUSTIVE_OMITTED_PATTERNS,
1363-
lint_root,
1364-
scrut_span,
1362+
cx.match_lint_level,
1363+
cx.scrut_span,
13651364
NonExhaustiveOmittedPattern {
13661365
scrut_ty,
1367-
uncovered: Uncovered::new(scrut_span, cx, witnesses),
1366+
uncovered: Uncovered::new(cx.scrut_span, cx, witnesses),
13681367
},
13691368
);
13701369
}

0 commit comments

Comments
 (0)
Please sign in to comment.