Skip to content

Commit bab8ede

Browse files
committed
Move rustc-specific entrypoint to the rustc module
1 parent 42103d6 commit bab8ede

File tree

3 files changed

+39
-40
lines changed

3 files changed

+39
-40
lines changed

Diff for: compiler/rustc_mir_build/src/thir/pattern/check_match.rs

+10-6
Original file line numberDiff line numberDiff line change
@@ -391,12 +391,16 @@ impl<'p, 'tcx> MatchVisitor<'p, 'tcx> {
391391
) -> Result<UsefulnessReport<'p, 'tcx>, ErrorGuaranteed> {
392392
let pattern_complexity_limit =
393393
get_limit_size(cx.tcx.hir().krate_attrs(), cx.tcx.sess, sym::pattern_complexity);
394-
let report =
395-
rustc_pattern_analysis::analyze_match(&cx, &arms, scrut_ty, pattern_complexity_limit)
396-
.map_err(|err| {
397-
self.error = Err(err);
398-
err
399-
})?;
394+
let report = rustc_pattern_analysis::rustc::analyze_match(
395+
&cx,
396+
&arms,
397+
scrut_ty,
398+
pattern_complexity_limit,
399+
)
400+
.map_err(|err| {
401+
self.error = Err(err);
402+
err
403+
})?;
400404

401405
// Warn unreachable subpatterns.
402406
for (arm, is_useful) in report.arm_usefulness.iter() {

Diff for: compiler/rustc_pattern_analysis/src/lib.rs

+3-34
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
//! Analysis of patterns, notably match exhaustiveness checking.
1+
//! Analysis of patterns, notably match exhaustiveness checking. The main entrypoint for this crate
2+
//! is [`usefulness::compute_match_usefulness`]. For rustc-specific types and entrypoints, see the
3+
//! [`rustc`] module.
24
35
// tidy-alphabetical-start
46
#![allow(rustc::diagnostic_outside_of_impl)]
@@ -23,14 +25,8 @@ use std::fmt;
2325

2426
pub use rustc_index::{Idx, IndexVec}; // re-exported to avoid rustc_index version issues
2527

26-
#[cfg(feature = "rustc")]
27-
use rustc_middle::ty::Ty;
28-
#[cfg(feature = "rustc")]
29-
use rustc_span::ErrorGuaranteed;
30-
3128
use crate::constructor::{Constructor, ConstructorSet, IntRange};
3229
use crate::pat::DeconstructedPat;
33-
use crate::pat_column::PatternColumn;
3430

3531
pub trait Captures<'a> {}
3632
impl<'a, T: ?Sized> Captures<'a> for T {}
@@ -128,30 +124,3 @@ impl<'p, Cx: PatCx> Clone for MatchArm<'p, Cx> {
128124
}
129125

130126
impl<'p, Cx: PatCx> Copy for MatchArm<'p, Cx> {}
131-
132-
/// The entrypoint for this crate. Computes whether a match is exhaustive and which of its arms are
133-
/// useful, and runs some lints.
134-
#[cfg(feature = "rustc")]
135-
pub fn analyze_match<'p, 'tcx>(
136-
tycx: &rustc::RustcPatCtxt<'p, 'tcx>,
137-
arms: &[rustc::MatchArm<'p, 'tcx>],
138-
scrut_ty: Ty<'tcx>,
139-
pattern_complexity_limit: Option<usize>,
140-
) -> Result<rustc::UsefulnessReport<'p, 'tcx>, ErrorGuaranteed> {
141-
use lints::lint_nonexhaustive_missing_variants;
142-
use usefulness::{compute_match_usefulness, PlaceValidity};
143-
144-
let scrut_ty = tycx.reveal_opaque_ty(scrut_ty);
145-
let scrut_validity = PlaceValidity::from_bool(tycx.known_valid_scrutinee);
146-
let report =
147-
compute_match_usefulness(tycx, arms, scrut_ty, scrut_validity, pattern_complexity_limit)?;
148-
149-
// Run the non_exhaustive_omitted_patterns lint. Only run on refutable patterns to avoid hitting
150-
// `if let`s. Only run if the match is exhaustive otherwise the error is redundant.
151-
if tycx.refutable && report.non_exhaustiveness_witnesses.is_empty() {
152-
let pat_column = PatternColumn::new(arms);
153-
lint_nonexhaustive_missing_variants(tycx, arms, &pat_column, scrut_ty)?;
154-
}
155-
156-
Ok(report)
157-
}

Diff for: compiler/rustc_pattern_analysis/src/rustc.rs

+26
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ use rustc_target::abi::{FieldIdx, Integer, VariantIdx, FIRST_VARIANT};
2020
use crate::constructor::{
2121
IntRange, MaybeInfiniteInt, OpaqueId, RangeEnd, Slice, SliceKind, VariantVisibility,
2222
};
23+
use crate::lints::lint_nonexhaustive_missing_variants;
24+
use crate::pat_column::PatternColumn;
25+
use crate::usefulness::{compute_match_usefulness, PlaceValidity};
2326
use crate::{errors, Captures, PatCx, PrivateUninhabitedField};
2427

2528
use crate::constructor::Constructor::*;
@@ -1058,3 +1061,26 @@ fn expand_or_pat<'p, 'tcx>(pat: &'p Pat<'tcx>) -> Vec<&'p Pat<'tcx>> {
10581061
expand(pat, &mut pats);
10591062
pats
10601063
}
1064+
1065+
/// The entrypoint for this crate. Computes whether a match is exhaustive and which of its arms are
1066+
/// useful, and runs some lints.
1067+
pub fn analyze_match<'p, 'tcx>(
1068+
tycx: &RustcPatCtxt<'p, 'tcx>,
1069+
arms: &[MatchArm<'p, 'tcx>],
1070+
scrut_ty: Ty<'tcx>,
1071+
pattern_complexity_limit: Option<usize>,
1072+
) -> Result<UsefulnessReport<'p, 'tcx>, ErrorGuaranteed> {
1073+
let scrut_ty = tycx.reveal_opaque_ty(scrut_ty);
1074+
let scrut_validity = PlaceValidity::from_bool(tycx.known_valid_scrutinee);
1075+
let report =
1076+
compute_match_usefulness(tycx, arms, scrut_ty, scrut_validity, pattern_complexity_limit)?;
1077+
1078+
// Run the non_exhaustive_omitted_patterns lint. Only run on refutable patterns to avoid hitting
1079+
// `if let`s. Only run if the match is exhaustive otherwise the error is redundant.
1080+
if tycx.refutable && report.non_exhaustiveness_witnesses.is_empty() {
1081+
let pat_column = PatternColumn::new(arms);
1082+
lint_nonexhaustive_missing_variants(tycx, arms, &pat_column, scrut_ty)?;
1083+
}
1084+
1085+
Ok(report)
1086+
}

0 commit comments

Comments
 (0)