1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

checker: make map literals have a real type

This commit is contained in:
Uwe Krüger 2020-06-19 15:00:27 +02:00 committed by GitHub
parent d76e94bba8
commit c78cfa43bc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 54 additions and 2 deletions

View File

@ -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 }
}
}

View File

@ -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 {

View File

@ -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 | }

View File

@ -0,0 +1,4 @@
fn main() {
mut a := map[string]f32{}
a = { 'x': 12.3 }
}