Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

checker: fix it variable marked as auto_heap #23799

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions vlib/v/ast/ast.v
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions vlib/v/checker/checker.v
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
}
Expand Down
9 changes: 5 additions & 4 deletions vlib/v/checker/fn.v
Original file line number Diff line number Diff line change
Expand Up @@ -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'
})
}

Expand Down
25 changes: 25 additions & 0 deletions vlib/v/tests/builtin_arrays/array_map_cast_interface_test.v
Original file line number Diff line number Diff line change
@@ -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
})]'
}
Loading