Skip to content

Commit ba3c419

Browse files
committed
Move the definitions of the two Ctxts to the top
1 parent 2184a14 commit ba3c419

File tree

1 file changed

+46
-47
lines changed
  • compiler/rustc_mir_build/src/thir/pattern

1 file changed

+46
-47
lines changed

compiler/rustc_mir_build/src/thir/pattern/_match.rs

+46-47
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,52 @@ use smallvec::{smallvec, SmallVec};
324324
use std::fmt;
325325
use std::iter::{FromIterator, IntoIterator};
326326

327+
crate struct MatchCheckCtxt<'a, 'tcx> {
328+
crate tcx: TyCtxt<'tcx>,
329+
/// The module in which the match occurs. This is necessary for
330+
/// checking inhabited-ness of types because whether a type is (visibly)
331+
/// inhabited can depend on whether it was defined in the current module or
332+
/// not. E.g., `struct Foo { _private: ! }` cannot be seen to be empty
333+
/// outside its module and should not be matchable with an empty match statement.
334+
crate module: DefId,
335+
crate param_env: ty::ParamEnv<'tcx>,
336+
crate pattern_arena: &'a TypedArena<Pat<'tcx>>,
337+
}
338+
339+
impl<'a, 'tcx> MatchCheckCtxt<'a, 'tcx> {
340+
pub(super) fn is_uninhabited(&self, ty: Ty<'tcx>) -> bool {
341+
if self.tcx.features().exhaustive_patterns {
342+
self.tcx.is_ty_uninhabited_from(self.module, ty, self.param_env)
343+
} else {
344+
false
345+
}
346+
}
347+
348+
/// Returns whether the given type is an enum from another crate declared `#[non_exhaustive]`.
349+
pub(super) fn is_foreign_non_exhaustive_enum(&self, ty: Ty<'tcx>) -> bool {
350+
match ty.kind() {
351+
ty::Adt(def, ..) => {
352+
def.is_enum() && def.is_variant_list_non_exhaustive() && !def.did.is_local()
353+
}
354+
_ => false,
355+
}
356+
}
357+
}
358+
359+
#[derive(Copy, Clone)]
360+
pub(super) struct PatCtxt<'a, 'p, 'tcx> {
361+
pub(super) cx: &'a MatchCheckCtxt<'p, 'tcx>,
362+
/// Current state of the matrix.
363+
pub(super) matrix: &'a Matrix<'p, 'tcx>,
364+
/// Type of the current column under investigation.
365+
pub(super) ty: Ty<'tcx>,
366+
/// Span of the current pattern under investigation.
367+
pub(super) span: Span,
368+
/// Whether the current pattern is the whole pattern as found in a match arm, or if it's a
369+
/// subpattern.
370+
pub(super) is_top_level: bool,
371+
}
372+
327373
crate fn expand_pattern<'tcx>(pat: Pat<'tcx>) -> Pat<'tcx> {
328374
LiteralExpander.fold_pattern(&pat)
329375
}
@@ -572,39 +618,6 @@ impl<'p, 'tcx> FromIterator<PatStack<'p, 'tcx>> for Matrix<'p, 'tcx> {
572618
}
573619
}
574620

575-
crate struct MatchCheckCtxt<'a, 'tcx> {
576-
crate tcx: TyCtxt<'tcx>,
577-
/// The module in which the match occurs. This is necessary for
578-
/// checking inhabited-ness of types because whether a type is (visibly)
579-
/// inhabited can depend on whether it was defined in the current module or
580-
/// not. E.g., `struct Foo { _private: ! }` cannot be seen to be empty
581-
/// outside it's module and should not be matchable with an empty match
582-
/// statement.
583-
crate module: DefId,
584-
crate param_env: ty::ParamEnv<'tcx>,
585-
crate pattern_arena: &'a TypedArena<Pat<'tcx>>,
586-
}
587-
588-
impl<'a, 'tcx> MatchCheckCtxt<'a, 'tcx> {
589-
pub(super) fn is_uninhabited(&self, ty: Ty<'tcx>) -> bool {
590-
if self.tcx.features().exhaustive_patterns {
591-
self.tcx.is_ty_uninhabited_from(self.module, ty, self.param_env)
592-
} else {
593-
false
594-
}
595-
}
596-
597-
/// Returns whether the given type is an enum from another crate declared `#[non_exhaustive]`.
598-
pub(super) fn is_foreign_non_exhaustive_enum(&self, ty: Ty<'tcx>) -> bool {
599-
match ty.kind() {
600-
ty::Adt(def, ..) => {
601-
def.is_enum() && def.is_variant_list_non_exhaustive() && !def.did.is_local()
602-
}
603-
_ => false,
604-
}
605-
}
606-
}
607-
608621
#[derive(Clone, Debug)]
609622
crate enum Usefulness<'tcx> {
610623
/// Carries, for each column in the matrix, a set of sub-branches that have been found to be
@@ -684,20 +697,6 @@ enum WitnessPreference {
684697
LeaveOutWitness,
685698
}
686699

687-
#[derive(Copy, Clone)]
688-
pub(super) struct PatCtxt<'a, 'p, 'tcx> {
689-
pub(super) cx: &'a MatchCheckCtxt<'p, 'tcx>,
690-
/// Current state of the matrix.
691-
pub(super) matrix: &'a Matrix<'p, 'tcx>,
692-
/// Type of the current column under investigation.
693-
pub(super) ty: Ty<'tcx>,
694-
/// Span of the current pattern under investigation.
695-
pub(super) span: Span,
696-
/// Whether the current pattern is the whole pattern as found in a match arm, or if it's a
697-
/// subpattern.
698-
pub(super) is_top_level: bool,
699-
}
700-
701700
/// A witness of non-exhaustiveness for error reporting, represented
702701
/// as a list of patterns (in reverse order of construction) with
703702
/// wildcards inside to represent elements that can take any inhabitant

0 commit comments

Comments
 (0)