Skip to content

checker: fix checking give const map as default or init value to struct fields(fix #20512)#20546

Merged
spytheman merged 2 commits intovlang:masterfrom
shove70:patch-1
Jan 15, 2024
Merged

checker: fix checking give const map as default or init value to struct fields(fix #20512)#20546
spytheman merged 2 commits intovlang:masterfrom
shove70:patch-1

Conversation

@shove70
Copy link
Copy Markdown
Contributor

@shove70 shove70 commented Jan 15, 2024

  1. Fixed Mutable map field, initialised with const, makes const mutable #20512
  2. Add tests.
const const_map = {
	'key': 4
}

struct Foo {
mut:
	field map[string]int = const_map
}

fn main() {
	_ = Foo{
		field: const_map
	}
}

outputs:

a.v:7:25: error: cannot copy map: call `move` or `clone` method (or use a reference)
    5 | struct Foo {
    6 | mut:
    7 |     field map[string]int = const_map
      |                            ~~~~~~~~~
    8 | }
    9 |
a.v:12:10: error: cannot assign a const map to mut struct field, call `clone` method (or use a reference)
   10 | fn main() {
   11 |     _ = Foo{
   12 |         field: const_map
      |                ~~~~~~~~~
   13 |     }
   14 | }

@shove70 shove70 changed the title checker: fix checking give static map as default or init value to struct fields(fix #20512) checker: fix checking give const map as default or init value to struct fields(fix #20512) Jan 15, 2024
Comment thread vlib/v/checker/tests/struct_field_init_and_default_is_map_err.out
@spytheman spytheman merged commit df8f8ae into vlang:master Jan 15, 2024
@shove70 shove70 deleted the patch-1 branch January 15, 2024 21:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Mutable map field, initialised with const, makes const mutable

2 participants