@@ -9,6 +9,24 @@ use rustc_span::def_id::LocalDefId;
99use rustc_span:: Span ;
1010use rustc_trait_selection:: traits;
1111
12+ #[ derive( Debug , Copy , Clone ) ]
13+ pub ( super ) enum DeclOrigin {
14+ // from an `if let` expression
15+ LetExpr ,
16+ // from `let x = ..`
17+ LocalDecl ,
18+ }
19+
20+ /// Provides context for checking patterns in declarations. More specifically this
21+ /// allows us to infer array types if the pattern is irrefutable and allows us to infer
22+ /// the size of the array. See issue #76342.
23+ #[ derive( Debug , Copy , Clone ) ]
24+ pub ( crate ) struct DeclContext {
25+ // whether we're in a let-else context
26+ pub ( super ) has_else : bool ,
27+ pub ( super ) origin : DeclOrigin ,
28+ }
29+
1230/// A declaration is an abstraction of [hir::Local] and [hir::Let].
1331///
1432/// It must have a hir_id, as this is how we connect gather_locals to the check functions.
@@ -19,19 +37,28 @@ pub(super) struct Declaration<'a> {
1937 pub span : Span ,
2038 pub init : Option < & ' a hir:: Expr < ' a > > ,
2139 pub els : Option < & ' a hir:: Block < ' a > > ,
40+ pub origin : DeclOrigin ,
2241}
2342
2443impl < ' a > From < & ' a hir:: Local < ' a > > for Declaration < ' a > {
2544 fn from ( local : & ' a hir:: Local < ' a > ) -> Self {
2645 let hir:: Local { hir_id, pat, ty, span, init, els, source : _ } = * local;
27- Declaration { hir_id, pat, ty, span, init, els }
46+ Declaration { hir_id, pat, ty, span, init, els, origin : DeclOrigin :: LocalDecl }
2847 }
2948}
3049
3150impl < ' a > From < & ' a hir:: Let < ' a > > for Declaration < ' a > {
3251 fn from ( let_expr : & ' a hir:: Let < ' a > ) -> Self {
3352 let hir:: Let { hir_id, pat, ty, span, init } = * let_expr;
34- Declaration { hir_id, pat, ty, span, init : Some ( init) , els : None }
53+ Declaration {
54+ hir_id,
55+ pat,
56+ ty,
57+ span,
58+ init : Some ( init) ,
59+ els : None ,
60+ origin : DeclOrigin :: LetExpr ,
61+ }
3562 }
3663}
3764
0 commit comments