autofree: handle tmp vars in for loops
							parent
							
								
									80c48ebe81
								
							
						
					
					
						commit
						1d6c604664
					
				|  | @ -369,8 +369,9 @@ pub mut: | ||||||
| 	pos             token.Position | 	pos             token.Position | ||||||
| 	is_used         bool | 	is_used         bool | ||||||
| 	is_changed      bool // to detect mutable vars that are never changed
 | 	is_changed      bool // to detect mutable vars that are never changed
 | ||||||
| 	is_or           bool // `x := foo() or { ... }`
 |  | ||||||
| 	// (for setting the position after the or block for autofree)
 | 	// (for setting the position after the or block for autofree)
 | ||||||
|  | 	is_or           bool // `x := foo() or { ... }`
 | ||||||
|  | 	is_tmp          bool // for tmp for loop vars, so that autofree can skip them
 | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| // used for smartcasting only
 | // used for smartcasting only
 | ||||||
|  |  | ||||||
|  | @ -2100,9 +2100,14 @@ fn (mut g Gen) autofree_scope_vars2(scope &ast.Scope, start_pos int, end_pos int | ||||||
| 					continue | 					continue | ||||||
| 				} | 				} | ||||||
| 				if obj.is_or { | 				if obj.is_or { | ||||||
| 					g.writeln('// skipping `or{}` var "$obj.name"') |  | ||||||
| 					// Skip vars inited with the `or {}`, since they are generated
 | 					// Skip vars inited with the `or {}`, since they are generated
 | ||||||
| 					// after the or block in C.
 | 					// after the or block in C.
 | ||||||
|  | 					g.writeln('// skipping `or{}` var "$obj.name"') | ||||||
|  | 					continue | ||||||
|  | 				} | ||||||
|  | 				if obj.is_tmp { | ||||||
|  | 					// Skip for loop vars
 | ||||||
|  | 					g.writeln('// skipping tmp var "$obj.name"') | ||||||
| 					continue | 					continue | ||||||
| 				} | 				} | ||||||
| 				// if var.typ == 0 {
 | 				// if var.typ == 0 {
 | ||||||
|  |  | ||||||
|  | @ -105,6 +105,7 @@ fn (mut p Parser) for_stmt() ast.Stmt { | ||||||
| 				name: key_var_name | 				name: key_var_name | ||||||
| 				typ: table.int_type | 				typ: table.int_type | ||||||
| 				pos: key_var_pos | 				pos: key_var_pos | ||||||
|  | 				is_tmp: true | ||||||
| 			}) | 			}) | ||||||
| 		} else if p.scope.known_var(val_var_name) { | 		} else if p.scope.known_var(val_var_name) { | ||||||
| 			p.error('redefinition of value iteration variable `$val_var_name`') | 			p.error('redefinition of value iteration variable `$val_var_name`') | ||||||
|  | @ -130,6 +131,7 @@ fn (mut p Parser) for_stmt() ast.Stmt { | ||||||
| 				name: val_var_name | 				name: val_var_name | ||||||
| 				typ: table.int_type | 				typ: table.int_type | ||||||
| 				pos: val_var_pos | 				pos: val_var_pos | ||||||
|  | 				is_tmp: true | ||||||
| 			}) | 			}) | ||||||
| 			if key_var_name.len > 0 { | 			if key_var_name.len > 0 { | ||||||
| 				p.error_with_pos('cannot declare index variable with range `for`', key_var_pos) | 				p.error_with_pos('cannot declare index variable with range `for`', key_var_pos) | ||||||
|  | @ -141,6 +143,7 @@ fn (mut p Parser) for_stmt() ast.Stmt { | ||||||
| 				name: val_var_name | 				name: val_var_name | ||||||
| 				pos: val_var_pos | 				pos: val_var_pos | ||||||
| 				is_mut: val_is_mut | 				is_mut: val_is_mut | ||||||
|  | 				is_tmp: true | ||||||
| 			}) | 			}) | ||||||
| 		} | 		} | ||||||
| 		p.inside_for = false | 		p.inside_for = false | ||||||
|  |  | ||||||
|  | @ -291,6 +291,11 @@ fn free_before_break() { | ||||||
| 			continue | 			continue | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
|  | 	x := ['1', '2', '3'] | ||||||
|  | 	for n in x { | ||||||
|  | 		f := 'f' | ||||||
|  | 		println('$n => $f') | ||||||
|  | 	} | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| struct User { | struct User { | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue