From c78cfa43bcda07788c72b84fbcd0f35d73fc1c62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Uwe=20Kr=C3=BCger?= <45282134+UweKrueger@users.noreply.github.com> Date: Fri, 19 Jun 2020 15:00:27 +0200 Subject: [PATCH] checker: make map literals have a real type --- vlib/builtin/map_test.v | 42 ++++++++++++++++++++ vlib/v/checker/checker.v | 4 +- vlib/v/checker/tests/map_init_wrong_type.out | 6 +++ vlib/v/checker/tests/map_init_wrong_type.vv | 4 ++ 4 files changed, 54 insertions(+), 2 deletions(-) create mode 100644 vlib/v/checker/tests/map_init_wrong_type.out create mode 100644 vlib/v/checker/tests/map_init_wrong_type.vv diff --git a/vlib/builtin/map_test.v b/vlib/builtin/map_test.v index 810d0c5463..d2356fe8d4 100644 --- a/vlib/builtin/map_test.v +++ b/vlib/builtin/map_test.v @@ -207,3 +207,45 @@ fn test_delete_size() { } } } + +struct Mstruct1 { +pub mut: + mymap map[string]int +} + +struct Mstruct2 { +pub mut: + mymap map[string]f64 +} + +struct Mstruct3 { +pub mut: + mymap map[string]u16 +} + +fn test_map_assign() { + mut a := map[string]f64{} + mut b := map[string]int{} + mut c := map[string]u16{} + a = { + 'x': 12.4 + 'y': 3 + } + b = { + 'u': -13 + 'v': 12 + } + c = { + 's': u16(5) + 't': 3 + } + d := Mstruct1 { + { 'p': 12 } + } + e := Mstruct2 { + { 'q': 1.7 } + } + f := Mstruct3 { + { 'r': u16(6), 's': 5 } + } +} diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 7474a02049..aaecfa5877 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -2515,8 +2515,8 @@ pub fn (mut c Checker) map_init(mut node ast.MapInit) table.Type { return node.typ } // `{'age': 20}` - key0_type := c.expr(node.keys[0]) - val0_type := c.expr(node.vals[0]) + key0_type := c.table.mktyp(c.expr(node.keys[0])) + val0_type := c.table.mktyp(c.expr(node.vals[0])) for i, key in node.keys { key_i := key as ast.StringLiteral for j in 0 .. i { diff --git a/vlib/v/checker/tests/map_init_wrong_type.out b/vlib/v/checker/tests/map_init_wrong_type.out new file mode 100644 index 0000000000..27d3a68247 --- /dev/null +++ b/vlib/v/checker/tests/map_init_wrong_type.out @@ -0,0 +1,6 @@ +vlib/v/checker/tests/map_init_wrong_type.v:3:10: error: cannot assign `map_string_f64` to `a` of type `map_string_f32` + 1 | fn main() { + 2 | mut a := map[string]f32{} + 3 | a = { 'x': 12.3 } + | ~~~ + 4 | } diff --git a/vlib/v/checker/tests/map_init_wrong_type.vv b/vlib/v/checker/tests/map_init_wrong_type.vv new file mode 100644 index 0000000000..273509d43d --- /dev/null +++ b/vlib/v/checker/tests/map_init_wrong_type.vv @@ -0,0 +1,4 @@ +fn main() { + mut a := map[string]f32{} + a = { 'x': 12.3 } +}