summaryrefslogtreecommitdiffstats
path: root/compiler/rustc_hir_typeck/src/gather_locals.rs
diff options
context:
space:
mode:
Diffstat (limited to 'compiler/rustc_hir_typeck/src/gather_locals.rs')
-rw-r--r--compiler/rustc_hir_typeck/src/gather_locals.rs26
1 files changed, 23 insertions, 3 deletions
diff --git a/compiler/rustc_hir_typeck/src/gather_locals.rs b/compiler/rustc_hir_typeck/src/gather_locals.rs
index 4f45a24b2..ed4c63f17 100644
--- a/compiler/rustc_hir_typeck/src/gather_locals.rs
+++ b/compiler/rustc_hir_typeck/src/gather_locals.rs
@@ -9,6 +9,26 @@ use rustc_span::def_id::LocalDefId;
use rustc_span::Span;
use rustc_trait_selection::traits;
+/// Provides context for checking patterns in declarations. More specifically this
+/// allows us to infer array types if the pattern is irrefutable and allows us to infer
+/// the size of the array. See issue #76342.
+#[derive(Debug, Copy, Clone)]
+pub(super) enum DeclOrigin<'a> {
+ // from an `if let` expression
+ LetExpr,
+ // from `let x = ..`
+ LocalDecl { els: Option<&'a hir::Block<'a>> },
+}
+
+impl<'a> DeclOrigin<'a> {
+ pub(super) fn try_get_else(&self) -> Option<&'a hir::Block<'a>> {
+ match self {
+ Self::LocalDecl { els } => *els,
+ Self::LetExpr => None,
+ }
+ }
+}
+
/// A declaration is an abstraction of [hir::Local] and [hir::Let].
///
/// It must have a hir_id, as this is how we connect gather_locals to the check functions.
@@ -18,20 +38,20 @@ pub(super) struct Declaration<'a> {
pub ty: Option<&'a hir::Ty<'a>>,
pub span: Span,
pub init: Option<&'a hir::Expr<'a>>,
- pub els: Option<&'a hir::Block<'a>>,
+ pub origin: DeclOrigin<'a>,
}
impl<'a> From<&'a hir::Local<'a>> for Declaration<'a> {
fn from(local: &'a hir::Local<'a>) -> Self {
let hir::Local { hir_id, pat, ty, span, init, els, source: _ } = *local;
- Declaration { hir_id, pat, ty, span, init, els }
+ Declaration { hir_id, pat, ty, span, init, origin: DeclOrigin::LocalDecl { els } }
}
}
impl<'a> From<&'a hir::Let<'a>> for Declaration<'a> {
fn from(let_expr: &'a hir::Let<'a>) -> Self {
let hir::Let { hir_id, pat, ty, span, init } = *let_expr;
- Declaration { hir_id, pat, ty, span, init: Some(init), els: None }
+ Declaration { hir_id, pat, ty, span, init: Some(init), origin: DeclOrigin::LetExpr }
}
}