Skip to content

Commit

Permalink
Cleanup span passing
Browse files Browse the repository at this point in the history
  • Loading branch information
Nadrieril committed Nov 5, 2023
1 parent 0075a5f commit ab918af
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 31 deletions.
13 changes: 8 additions & 5 deletions compiler/rustc_mir_build/src/thir/pattern/check_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ impl<'thir, 'p, 'tcx> MatchVisitor<'thir, 'p, 'tcx> {
&self,
refutability: RefutableFlag,
match_span: Option<Span>,
scrut_span: Span,
) -> MatchCheckCtxt<'p, 'tcx> {
let refutable = match refutability {
Irrefutable => false,
Expand All @@ -301,7 +302,9 @@ impl<'thir, 'p, 'tcx> MatchVisitor<'thir, 'p, 'tcx> {
param_env: self.param_env,
module: self.tcx.parent_module(self.lint_level).to_def_id(),
pattern_arena: &self.pattern_arena,
match_lint_level: self.lint_level,
match_span,
scrut_span,
refutable,
}
}
Expand Down Expand Up @@ -332,7 +335,8 @@ impl<'thir, 'p, 'tcx> MatchVisitor<'thir, 'p, 'tcx> {
source: hir::MatchSource,
expr_span: Span,
) {
let cx = self.new_cx(Refutable, Some(expr_span));
let scrut = &self.thir[scrut];
let cx = self.new_cx(Refutable, Some(expr_span), scrut.span);

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

let scrut = &self.thir[scrut];
let scrut_ty = scrut.ty;
let report = compute_match_usefulness(&cx, &tarms, self.lint_level, scrut_ty, scrut.span);
let report = compute_match_usefulness(&cx, &tarms, scrut_ty);

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

Expand Down
24 changes: 14 additions & 10 deletions compiler/rustc_mir_build/src/thir/pattern/deconstruct_pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -806,7 +806,12 @@ impl<'tcx> Constructor<'tcx> {
#[inline]
pub(super) fn is_covered_by<'p>(&self, pcx: &PatCtxt<'_, 'p, 'tcx>, other: &Self) -> bool {
match (self, other) {
(Wildcard, _) => bug!("Constructor splitting should not have returned `Wildcard`"),
(Wildcard, _) => {
span_bug!(
pcx.cx.scrut_span,
"Constructor splitting should not have returned `Wildcard`"
)
}
// Wildcards cover anything
(_, Wildcard) => true,
// Only a wildcard pattern can match these special constructors.
Expand Down Expand Up @@ -846,7 +851,7 @@ impl<'tcx> Constructor<'tcx> {
(Opaque(..), _) | (_, Opaque(..)) => false,

_ => span_bug!(
pcx.span,
pcx.cx.scrut_span,
"trying to compare incompatible constructors {:?} and {:?}",
self,
other
Expand Down Expand Up @@ -1247,9 +1252,8 @@ impl<'p, 'tcx> Fields<'p, 'tcx> {
fn wildcards_from_tys(
cx: &MatchCheckCtxt<'p, 'tcx>,
tys: impl IntoIterator<Item = Ty<'tcx>>,
span: Span,
) -> Self {
Fields::from_iter(cx, tys.into_iter().map(|ty| DeconstructedPat::wildcard(ty, span)))
Fields::from_iter(cx, tys.into_iter().map(|ty| DeconstructedPat::wildcard(ty, DUMMY_SP)))
}

// In the cases of either a `#[non_exhaustive]` field list or a non-public field, we hide
Expand Down Expand Up @@ -1285,26 +1289,26 @@ impl<'p, 'tcx> Fields<'p, 'tcx> {
pub(super) fn wildcards(pcx: &PatCtxt<'_, 'p, 'tcx>, constructor: &Constructor<'tcx>) -> Self {
let ret = match constructor {
Single | Variant(_) => match pcx.ty.kind() {
ty::Tuple(fs) => Fields::wildcards_from_tys(pcx.cx, fs.iter(), pcx.span),
ty::Ref(_, rty, _) => Fields::wildcards_from_tys(pcx.cx, once(*rty), pcx.span),
ty::Tuple(fs) => Fields::wildcards_from_tys(pcx.cx, fs.iter()),
ty::Ref(_, rty, _) => Fields::wildcards_from_tys(pcx.cx, once(*rty)),
ty::Adt(adt, args) => {
if adt.is_box() {
// The only legal patterns of type `Box` (outside `std`) are `_` and box
// patterns. If we're here we can assume this is a box pattern.
Fields::wildcards_from_tys(pcx.cx, once(args.type_at(0)), pcx.span)
Fields::wildcards_from_tys(pcx.cx, once(args.type_at(0)))
} else {
let variant = &adt.variant(constructor.variant_index_for_adt(*adt));
let tys = Fields::list_variant_nonhidden_fields(pcx.cx, pcx.ty, variant)
.map(|(_, ty)| ty);
Fields::wildcards_from_tys(pcx.cx, tys, pcx.span)
Fields::wildcards_from_tys(pcx.cx, tys)
}
}
_ => bug!("Unexpected type for `Single` constructor: {:?}", pcx),
},
Slice(slice) => match *pcx.ty.kind() {
ty::Slice(ty) | ty::Array(ty, _) => {
let arity = slice.arity();
Fields::wildcards_from_tys(pcx.cx, (0..arity).map(|_| ty), pcx.span)
Fields::wildcards_from_tys(pcx.cx, (0..arity).map(|_| ty))
}
_ => bug!("bad slice pattern {:?} {:?}", constructor, pcx),
},
Expand Down Expand Up @@ -1623,7 +1627,7 @@ impl<'p, 'tcx> DeconstructedPat<'p, 'tcx> {
let wildcard: &_ = pcx
.cx
.pattern_arena
.alloc(DeconstructedPat::wildcard(inner_ty, pcx.span));
.alloc(DeconstructedPat::wildcard(inner_ty, DUMMY_SP));
let extra_wildcards = other_slice.arity() - self_slice.arity();
let extra_wildcards = (0..extra_wildcards).map(|_| wildcard);
prefix.iter().chain(extra_wildcards).chain(suffix).collect()
Expand Down
31 changes: 15 additions & 16 deletions compiler/rustc_mir_build/src/thir/pattern/usefulness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -521,8 +521,12 @@ pub(crate) struct MatchCheckCtxt<'p, 'tcx> {
pub(crate) module: DefId,
pub(crate) param_env: ty::ParamEnv<'tcx>,
pub(crate) pattern_arena: &'p TypedArena<DeconstructedPat<'p, 'tcx>>,
/// Lint level at the match.
pub(crate) match_lint_level: HirId,
/// The span of the whole match, if applicable.
pub(crate) match_span: Option<Span>,
/// Span of the scrutinee.
pub(crate) scrut_span: Span,
/// Only produce `NON_EXHAUSTIVE_OMITTED_PATTERNS` lint on refutable patterns.
pub(crate) refutable: bool,
}
Expand Down Expand Up @@ -552,8 +556,6 @@ pub(super) struct PatCtxt<'a, 'p, 'tcx> {
pub(super) cx: &'a MatchCheckCtxt<'p, 'tcx>,
/// Type of the current column under investigation.
pub(super) ty: Ty<'tcx>,
/// Span of the current pattern under investigation.
pub(super) span: Span,
/// Whether the current pattern is the whole pattern as found in a match arm, or if it's a
/// subpattern.
pub(super) is_top_level: bool,
Expand Down Expand Up @@ -1023,7 +1025,7 @@ fn compute_exhaustiveness_and_reachability<'p, 'tcx>(
};

debug!("ty: {ty:?}");
let pcx = &PatCtxt { cx, ty, span: DUMMY_SP, is_top_level };
let pcx = &PatCtxt { cx, ty, is_top_level };

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

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

/// Traverse the patterns to warn the user about ranges that overlap on their endpoints.
#[instrument(level = "debug", skip(cx, lint_root))]
#[instrument(level = "debug", skip(cx))]
fn lint_overlapping_range_endpoints<'p, 'tcx>(
cx: &MatchCheckCtxt<'p, 'tcx>,
column: &PatternColumn<'p, 'tcx>,
lint_root: HirId,
) {
let Some(ty) = column.head_ty() else {
return;
};
let pcx = &PatCtxt { cx, ty, span: DUMMY_SP, is_top_level: false };
let pcx = &PatCtxt { cx, ty, is_top_level: false };

let set = column.analyze_ctors(pcx);

Expand All @@ -1231,7 +1232,7 @@ fn lint_overlapping_range_endpoints<'p, 'tcx>(
.collect();
cx.tcx.emit_spanned_lint(
lint::builtin::OVERLAPPING_RANGE_ENDPOINTS,
lint_root,
cx.match_lint_level,
this_span,
OverlappingRangeEndpoints { overlap: overlaps, range: this_span },
);
Expand Down Expand Up @@ -1276,7 +1277,7 @@ fn lint_overlapping_range_endpoints<'p, 'tcx>(
// Recurse into the fields.
for ctor in set.present {
for col in column.specialize(pcx, &ctor) {
lint_overlapping_range_endpoints(cx, &col, lint_root);
lint_overlapping_range_endpoints(cx, &col);
}
}
}
Expand Down Expand Up @@ -1317,9 +1318,7 @@ pub(crate) struct UsefulnessReport<'p, 'tcx> {
pub(crate) fn compute_match_usefulness<'p, 'tcx>(
cx: &MatchCheckCtxt<'p, 'tcx>,
arms: &[MatchArm<'p, 'tcx>],
lint_root: HirId,
scrut_ty: Ty<'tcx>,
scrut_span: Span,
) -> UsefulnessReport<'p, 'tcx> {
let mut matrix = Matrix::new(cx, arms.iter(), scrut_ty);
let non_exhaustiveness_witnesses =
Expand All @@ -1343,13 +1342,13 @@ pub(crate) fn compute_match_usefulness<'p, 'tcx>(

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

// 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.
if cx.refutable && report.non_exhaustiveness_witnesses.is_empty() {
if !matches!(
cx.tcx.lint_level_at_node(NON_EXHAUSTIVE_OMITTED_PATTERNS, lint_root).0,
cx.tcx.lint_level_at_node(NON_EXHAUSTIVE_OMITTED_PATTERNS, cx.match_lint_level).0,
rustc_session::lint::Level::Allow
) {
let witnesses = collect_nonexhaustive_missing_variants(cx, &pat_column);
Expand All @@ -1360,11 +1359,11 @@ pub(crate) fn compute_match_usefulness<'p, 'tcx>(
// NB: The partner lint for structs lives in `compiler/rustc_hir_analysis/src/check/pat.rs`.
cx.tcx.emit_spanned_lint(
NON_EXHAUSTIVE_OMITTED_PATTERNS,
lint_root,
scrut_span,
cx.match_lint_level,
cx.scrut_span,
NonExhaustiveOmittedPattern {
scrut_ty,
uncovered: Uncovered::new(scrut_span, cx, witnesses),
uncovered: Uncovered::new(cx.scrut_span, cx, witnesses),
},
);
}
Expand Down

0 comments on commit ab918af

Please sign in to comment.