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:
parent
d76e94bba8
commit
c78cfa43bc
@ -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 }
|
||||
}
|
||||
}
|
||||
|
@ -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 {
|
||||
|
6
vlib/v/checker/tests/map_init_wrong_type.out
Normal file
6
vlib/v/checker/tests/map_init_wrong_type.out
Normal 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 | }
|
4
vlib/v/checker/tests/map_init_wrong_type.vv
Normal file
4
vlib/v/checker/tests/map_init_wrong_type.vv
Normal file
@ -0,0 +1,4 @@
|
||||
fn main() {
|
||||
mut a := map[string]f32{}
|
||||
a = { 'x': 12.3 }
|
||||
}
|
Loading…
Reference in New Issue
Block a user