diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index 218076af0adcbb..af37be7e0ce14c 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -896,6 +896,7 @@ pub: is_autofree_tmp bool is_inherited bool has_inherited bool + is_it_var bool // is `it` var pub mut: is_arg bool // fn args should not be autofreed is_auto_deref bool diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 2b715e9af2b6f6..84abfdd2e14cc2 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -298,7 +298,7 @@ pub fn (mut c Checker) check_scope_vars(sc &ast.Scope) { } } } - if obj.is_mut && !obj.is_changed && !c.is_builtin_mod && obj.name != 'it' { + if obj.is_mut && !obj.is_changed && !c.is_builtin_mod && !obj.is_it_var { // if obj.is_mut && !obj.is_changed && !c.is_builtin { //TODO C error bad field not checked // c.warn('`$obj.name` is declared as mutable, but it was never changed', // obj.pos) @@ -4591,7 +4591,7 @@ fn (mut c Checker) mark_as_referenced(mut node ast.Expr, as_interface bool) { match type_sym.kind { .struct { info := type_sym.info as ast.Struct - if !info.is_heap { + if !info.is_heap && !node.obj.is_it_var { node.obj.is_auto_heap = true } } diff --git a/vlib/v/checker/fn.v b/vlib/v/checker/fn.v index 1205fc20803fe3..540f4e40595fb7 100644 --- a/vlib/v/checker/fn.v +++ b/vlib/v/checker/fn.v @@ -3749,10 +3749,11 @@ fn scope_register_a_b(mut s ast.Scope, pos token.Pos, typ ast.Type) { fn scope_register_var_name(mut s ast.Scope, pos token.Pos, typ ast.Type, name string) { s.register(ast.Var{ - name: name - pos: pos - typ: typ - is_used: true + name: name + pos: pos + typ: typ + is_used: true + is_it_var: name == 'it' }) } diff --git a/vlib/v/tests/builtin_arrays/array_map_cast_interface_test.v b/vlib/v/tests/builtin_arrays/array_map_cast_interface_test.v new file mode 100644 index 00000000000000..a4f1d7baae4e7a --- /dev/null +++ b/vlib/v/tests/builtin_arrays/array_map_cast_interface_test.v @@ -0,0 +1,25 @@ +interface Rect { + width u32 + height u32 +} + +struct Square { + side u32 + width u32 + height u32 +} + +fn test_main() { + squares := [Square{ + side: 5 + width: 5 + height: 5 + }] + rects := squares.map(Rect(it)) + + assert rects.str() == '[Rect(Square{ + side: 5 + width: 5 + height: 5 +})]' +}