cgen: fix for_in_mut_val for maps (#8221)
							parent
							
								
									3ecbf78707
								
							
						
					
					
						commit
						f13ba3a9a8
					
				|  | @ -1362,11 +1362,21 @@ fn (mut g Gen) for_in(it ast.ForInStmt) { | |||
| 				g.write(' = (*(voidptr*)') | ||||
| 			} else { | ||||
| 				val_styp := g.typ(it.val_type) | ||||
| 				if it.val_type.is_ptr() { | ||||
| 					g.write('\t$val_styp ${c_name(it.val_var)} = &(*($val_styp)') | ||||
| 				} else { | ||||
| 					g.write('\t$val_styp ${c_name(it.val_var)} = (*($val_styp*)') | ||||
| 				} | ||||
| 			} | ||||
| 			g.writeln('DenseArray_value(&$atmp${arw_or_pt}key_values, $idx));') | ||||
| 		} | ||||
| 		if it.val_is_mut { | ||||
| 			g.for_in_mul_val_name = it.val_var | ||||
| 		} | ||||
| 		g.stmts(it.stmts) | ||||
| 		if it.val_is_mut { | ||||
| 			g.for_in_mul_val_name = '' | ||||
| 		} | ||||
| 		if it.key_type == table.string_type && !g.is_builtin_mod { | ||||
| 			// g.writeln('string_free(&$key);')
 | ||||
| 		} | ||||
|  |  | |||
|  | @ -49,3 +49,12 @@ fn test_for_in_mut_val_of_map() { | |||
| 	println(m) | ||||
| 	assert '$m' == "{'hello': [2, 4, 6]}" | ||||
| } | ||||
| 
 | ||||
| fn test_for_in_mut_val_of_map_direct() { | ||||
| 	mut m := {'foo': 1, 'bar': 2} | ||||
| 	for _, mut j in m { | ||||
| 		j = 3 | ||||
| 	} | ||||
| 	println(m) | ||||
| 	assert '$m' == "{'foo': 3, 'bar': 3}" | ||||
| } | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue