@@ -1647,7 +1647,7 @@ fn (mut c Checker) selector_expr(mut node ast.SelectorExpr) ast.Type {
1647
1647
node.typ = field.typ
1648
1648
if node.or_block.kind == .block {
1649
1649
c.expected_or_type = node.typ.clear_option_and_result ()
1650
- c.stmts_ending_with_expression (mut node.or_block.stmts)
1650
+ c.stmts_ending_with_expression (mut node.or_block.stmts, c.expected_or_type )
1651
1651
c.check_or_expr (node.or_block, node.typ, c.expected_or_type, node)
1652
1652
c.expected_or_type = ast.void_type
1653
1653
}
@@ -2593,7 +2593,7 @@ fn (mut c Checker) import_stmt(node ast.Import) {
2593
2593
fn (mut c Checker) stmts (mut stmts []ast.Stmt) {
2594
2594
old_stmt_level := c.stmt_level
2595
2595
c.stmt_level = 0
2596
- c.stmts_ending_with_expression (mut stmts)
2596
+ c.stmts_ending_with_expression (mut stmts, c.expected_or_type )
2597
2597
c.stmt_level = old_stmt_level
2598
2598
}
2599
2599
@@ -2602,7 +2602,7 @@ fn (mut c Checker) stmts(mut stmts []ast.Stmt) {
2602
2602
// `x := opt() or { stmt1 stmt2 ExprStmt }`,
2603
2603
// `x := if cond { stmt1 stmt2 ExprStmt } else { stmt2 stmt3 ExprStmt }`,
2604
2604
// `x := match expr { Type1 { stmt1 stmt2 ExprStmt } else { stmt2 stmt3 ExprStmt }`.
2605
- fn (mut c Checker) stmts_ending_with_expression (mut stmts []ast.Stmt) {
2605
+ fn (mut c Checker) stmts_ending_with_expression (mut stmts []ast.Stmt, expected_or_type ast.Type ) {
2606
2606
if stmts.len == 0 {
2607
2607
c.scope_returns = false
2608
2608
return
@@ -2623,7 +2623,10 @@ fn (mut c Checker) stmts_ending_with_expression(mut stmts []ast.Stmt) {
2623
2623
unreachable = stmt.pos
2624
2624
}
2625
2625
}
2626
+ prev_expected_or_type := c.expected_or_type
2627
+ c.expected_or_type = expected_or_type
2626
2628
c.stmt (mut stmt)
2629
+ c.expected_or_type = prev_expected_or_type
2627
2630
if ! c.inside_anon_fn && c.in_for_count > 0 && stmt is ast.BranchStmt
2628
2631
&& stmt.kind in [.key_continue, .key_break] {
2629
2632
c.scope_returns = true
@@ -3649,7 +3652,7 @@ fn (mut c Checker) ident(mut node ast.Ident) ast.Type {
3649
3652
}
3650
3653
unwrapped_typ := typ.clear_option_and_result ()
3651
3654
c.expected_or_type = unwrapped_typ
3652
- c.stmts_ending_with_expression (mut node.or_expr.stmts)
3655
+ c.stmts_ending_with_expression (mut node.or_expr.stmts, c.expected_or_type )
3653
3656
c.check_or_expr (node.or_expr, typ, c.expected_or_type, node)
3654
3657
return unwrapped_typ
3655
3658
}
@@ -3759,7 +3762,7 @@ fn (mut c Checker) ident(mut node ast.Ident) ast.Type {
3759
3762
}
3760
3763
unwrapped_typ := typ.clear_option_and_result ()
3761
3764
c.expected_or_type = unwrapped_typ
3762
- c.stmts_ending_with_expression (mut node.or_expr.stmts)
3765
+ c.stmts_ending_with_expression (mut node.or_expr.stmts, c.expected_or_type )
3763
3766
c.check_or_expr (node.or_expr, typ, c.expected_or_type, node)
3764
3767
return unwrapped_typ
3765
3768
}
@@ -3821,7 +3824,7 @@ fn (mut c Checker) ident(mut node ast.Ident) ast.Type {
3821
3824
if node.or_expr.kind != .absent {
3822
3825
unwrapped_typ := typ.clear_option_and_result ()
3823
3826
c.expected_or_type = unwrapped_typ
3824
- c.stmts_ending_with_expression (mut node.or_expr.stmts)
3827
+ c.stmts_ending_with_expression (mut node.or_expr.stmts, c.expected_or_type )
3825
3828
c.check_or_expr (node.or_expr, typ, c.expected_or_type, node)
3826
3829
}
3827
3830
return typ
@@ -4455,7 +4458,7 @@ fn (mut c Checker) prefix_expr(mut node ast.PrefixExpr) ast.Type {
4455
4458
if node.op == .arrow {
4456
4459
raw_right_sym := c.table.final_sym (right_type)
4457
4460
if raw_right_sym.kind == .chan {
4458
- c.stmts_ending_with_expression (mut node.or_block.stmts)
4461
+ c.stmts_ending_with_expression (mut node.or_block.stmts, c.expected_or_type )
4459
4462
return raw_right_sym.chan_info ().elem_type
4460
4463
}
4461
4464
c.type_error_for_operator ('<-' , '`chan`' , raw_right_sym.name, node.pos)
@@ -4653,7 +4656,7 @@ fn (mut c Checker) index_expr(mut node ast.IndexExpr) ast.Type {
4653
4656
if node.or_expr.stmts.len > 0 && node.or_expr.stmts.last () is ast.ExprStmt {
4654
4657
c.expected_or_type = typ
4655
4658
}
4656
- c.stmts_ending_with_expression (mut node.or_expr.stmts)
4659
+ c.stmts_ending_with_expression (mut node.or_expr.stmts, c.expected_or_type )
4657
4660
c.check_expr_option_or_result_call (node, typ)
4658
4661
return typ
4659
4662
}
0 commit comments