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 wrong checking for heap struct #21511

Merged
merged 3 commits into from
May 20, 2024
Merged
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
4 changes: 2 additions & 2 deletions vlib/v/checker/checker.v
Original file line number Diff line number Diff line change
Expand Up @@ -5080,9 +5080,9 @@ fn (mut c Checker) fail_if_stack_struct_action_outside_unsafe(mut ident ast.Iden
sym := c.table.sym(obj.typ.set_nr_muls(0))
if !sym.is_heap() && !c.pref.translated && !c.file.is_translated {
suggestion := if sym.kind == .struct_ {
'declaring `${sym.name}` as `[heap]`'
'declaring `${sym.name}` as `@[heap]`'
} else {
'wrapping the `${sym.name}` object in a `struct` declared as `[heap]`'
'wrapping the `${sym.name}` object in a `struct` declared as `@[heap]`'
}
c.error('`${ident.name}` cannot be ${failed_action} outside `unsafe` blocks as it might refer to an object stored on stack. Consider ${suggestion}.',
ident.pos)
Expand Down
3 changes: 2 additions & 1 deletion vlib/v/checker/struct.v
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,8 @@ or use an explicit `unsafe{ a[..] }`, if you do not want a copy of the slice.',
node.init_fields[i].typ = got_type
node.init_fields[i].expected_type = exp_type

if got_type.is_ptr() && exp_type.is_ptr() && mut init_field.expr is ast.Ident {
if got_type.is_ptr() && exp_type.is_ptr() && mut init_field.expr is ast.Ident
&& !info.is_heap {
c.fail_if_stack_struct_action_outside_unsafe(mut init_field.expr,
'assigned')
}
Expand Down
6 changes: 3 additions & 3 deletions vlib/v/checker/tests/no_heap_struct.out
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
vlib/v/checker/tests/no_heap_struct.vv:13:6: error: `x` cannot be assigned outside `unsafe` blocks as it might refer to an object stored on stack. Consider declaring `Abc` as `[heap]`.
vlib/v/checker/tests/no_heap_struct.vv:13:6: error: `x` cannot be assigned outside `unsafe` blocks as it might refer to an object stored on stack. Consider declaring `Abc` as `@[heap]`.
11 | fn f(x &Abc) St {
12 | s := St{
13 | a: x
| ^
14 | }
15 | return s
vlib/v/checker/tests/no_heap_struct.vv:19:9: error: `x` cannot be returned outside `unsafe` blocks as it might refer to an object stored on stack. Consider declaring `Abc` as `[heap]`.
vlib/v/checker/tests/no_heap_struct.vv:19:9: error: `x` cannot be returned outside `unsafe` blocks as it might refer to an object stored on stack. Consider declaring `Abc` as `@[heap]`.
17 |
18 | fn g(mut x Abc) &Abc {
19 | return x
| ^
20 | }
21 |
vlib/v/checker/tests/no_heap_struct.vv:23:7: error: `x` cannot be assigned outside `unsafe` blocks as it might refer to an object stored on stack. Consider declaring `Abc` as `[heap]`.
vlib/v/checker/tests/no_heap_struct.vv:23:7: error: `x` cannot be assigned outside `unsafe` blocks as it might refer to an object stored on stack. Consider declaring `Abc` as `@[heap]`.
21 |
22 | fn h(x &Abc) &Abc {
23 | y := x
Expand Down
18 changes: 18 additions & 0 deletions vlib/v/tests/heap_struct_member_ptr_test.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module main

@[heap]
struct Wrapper {
a &int = unsafe { nil }
}

fn test[T](a &T) Wrapper {
$if T is int {
return Wrapper{a}
}
return Wrapper{}
}

fn test_main() {
a := 123
assert test(a).a == &a
}
Loading