@@ -315,20 +315,7 @@ impl<'a, 'b> PatternTyper<'a, 'b> {
315
315
// Unify each pattern in the multi-pattern with the corresponding subject
316
316
let mut typed_multi = Vec :: with_capacity ( multi_pattern. len ( ) ) ;
317
317
for ( pattern, subject) in multi_pattern. into_iter ( ) . zip ( subjects) {
318
- let subject_variable = match subject {
319
- TypedExpr :: Var {
320
- constructor :
321
- ValueConstructor {
322
- // Records should not be considered local variables
323
- // See: https://github.com/gleam-lang/gleam/issues/3861
324
- variant : ValueConstructorVariant :: Record { .. } ,
325
- ..
326
- } ,
327
- ..
328
- } => None ,
329
- TypedExpr :: Var { name, .. } => Some ( name. clone ( ) ) ,
330
- _ => None ,
331
- } ;
318
+ let subject_variable = Self :: subject_variable ( subject) ;
332
319
333
320
let pattern = self . unify ( pattern, subject. type_ ( ) , subject_variable) ;
334
321
typed_multi. push ( pattern) ;
@@ -344,7 +331,15 @@ impl<'a, 'b> PatternTyper<'a, 'b> {
344
331
pattern : UntypedPattern ,
345
332
subject : & TypedExpr ,
346
333
) -> TypedPattern {
347
- let subject_variable = match subject {
334
+ let subject_variable = Self :: subject_variable ( subject) ;
335
+
336
+ let typed_pattern = self . unify ( pattern, subject. type_ ( ) , subject_variable) ;
337
+ self . register_variables ( ) ;
338
+ typed_pattern
339
+ }
340
+
341
+ fn subject_variable ( subject : & TypedExpr ) -> Option < EcoString > {
342
+ match subject {
348
343
TypedExpr :: Var {
349
344
constructor :
350
345
ValueConstructor {
@@ -356,12 +351,15 @@ impl<'a, 'b> PatternTyper<'a, 'b> {
356
351
..
357
352
} => None ,
358
353
TypedExpr :: Var { name, .. } => Some ( name. clone ( ) ) ,
354
+ // If the subject of a `case` expression is something like
355
+ // `echo some_variable`, we still want to narrow the variant for
356
+ // `some_variable`.
357
+ TypedExpr :: Echo {
358
+ expression : Some ( subject) ,
359
+ ..
360
+ } => Self :: subject_variable ( subject) ,
359
361
_ => None ,
360
- } ;
361
-
362
- let typed_pattern = self . unify ( pattern, subject. type_ ( ) , subject_variable) ;
363
- self . register_variables ( ) ;
364
- typed_pattern
362
+ }
365
363
}
366
364
367
365
/// Register the variables bound in this pattern in the environment
0 commit comments