ast: allow `a := match x { 101 { ... for {...} ... y }`
							parent
							
								
									8a18f9175a
								
							
						
					
					
						commit
						2080557f50
					
				| 
						 | 
				
			
			@ -1821,6 +1821,9 @@ pub fn (stmt Stmt) check_c_expr() ? {
 | 
			
		|||
		AssignStmt {
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
		ForCStmt, ForInStmt, ForStmt {
 | 
			
		||||
			return
 | 
			
		||||
		}
 | 
			
		||||
		ExprStmt {
 | 
			
		||||
			if stmt.expr.is_expr() {
 | 
			
		||||
				return
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -1,10 +1,3 @@
 | 
			
		|||
vlib/v/checker/tests/if_match_expr.vv:8:6: error: `if` expression branch has unsupported statement (`v.ast.ForStmt`)
 | 
			
		||||
    6 |     if true {1} else {-1} // result
 | 
			
		||||
    7 | } else {
 | 
			
		||||
    8 |     for {break}
 | 
			
		||||
      |         ^
 | 
			
		||||
    9 |     {}
 | 
			
		||||
   10 |     match true {true {} else {}} // statement not expression
 | 
			
		||||
vlib/v/checker/tests/if_match_expr.vv:9:2: error: `if` expression branch has unsupported statement (`v.ast.Block`)
 | 
			
		||||
    7 | } else {
 | 
			
		||||
    8 |     for {break}
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -0,0 +1,58 @@
 | 
			
		|||
fn test_match_with_for_in_loop() {
 | 
			
		||||
	a := 101
 | 
			
		||||
	b := match a {
 | 
			
		||||
		101 {
 | 
			
		||||
			mut aa := []int{}
 | 
			
		||||
			for i in 0 .. 3 {
 | 
			
		||||
				aa << i
 | 
			
		||||
			}
 | 
			
		||||
			aa
 | 
			
		||||
		}
 | 
			
		||||
		else {
 | 
			
		||||
			[0]
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	println(b)
 | 
			
		||||
	assert b == [0, 1, 2]
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn test_match_with_for_c_loop() {
 | 
			
		||||
	a := 101
 | 
			
		||||
	b := match a {
 | 
			
		||||
		101 {
 | 
			
		||||
			mut aa := []int{}
 | 
			
		||||
			for i := 0; i < 3; i++ {
 | 
			
		||||
				aa << i
 | 
			
		||||
			}
 | 
			
		||||
			aa
 | 
			
		||||
		}
 | 
			
		||||
		else {
 | 
			
		||||
			[0]
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	println(b)
 | 
			
		||||
	assert b == [0, 1, 2]
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
fn test_match_with_for_loop() {
 | 
			
		||||
	a := 101
 | 
			
		||||
	b := match a {
 | 
			
		||||
		101 {
 | 
			
		||||
			mut aa := []int{}
 | 
			
		||||
			mut i := 0
 | 
			
		||||
			for {
 | 
			
		||||
				aa << i
 | 
			
		||||
				i++
 | 
			
		||||
				if i == 3 {
 | 
			
		||||
					break
 | 
			
		||||
				}
 | 
			
		||||
			}
 | 
			
		||||
			aa
 | 
			
		||||
		}
 | 
			
		||||
		else {
 | 
			
		||||
			[0]
 | 
			
		||||
		}
 | 
			
		||||
	}
 | 
			
		||||
	println(b)
 | 
			
		||||
	assert b == [0, 1, 2]
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
		Reference in New Issue