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

v.parser: support imported enums as map keys (#10234)

This commit is contained in:
Lukas Neubert 2021-05-28 19:09:03 +02:00 committed by GitHub
parent e09f0234ea
commit 6b683d31ac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 34 additions and 3 deletions

View File

@ -35,6 +35,7 @@ const (
'vlib/v/tests/interop_test.v', /* bad comment formatting */
'vlib/v/tests/string_interpolation_test.v' /* TODO byteptr: &byte.str() behaves differently than byteptr.str() */,
'vlib/v/gen/js/tests/js.v', /* local `hello` fn, gets replaced with module `hello` aliased as `hl` */
'vlib/v/tests/map_enum_keys_test.v' /* temporary here, till PR#10235 is merged */,
'examples/c_interop_wkhtmltopdf.v' /* &charptr --> &&char */,
]
vfmt_verify_list = [

View File

@ -92,8 +92,8 @@ pub fn (mut p Parser) parse_map_type() ast.Type {
return 0
}
key_type_supported := key_type in [ast.string_type_idx, ast.voidptr_type_idx]
|| key_sym.kind == .enum_ || ((key_type.is_int() || key_type.is_float()
|| is_alias) && !key_type.is_ptr())
|| key_sym.kind == .enum_ || key_sym.kind == .placeholder
|| ((key_type.is_int() || key_type.is_float() || is_alias) && !key_type.is_ptr())
if !key_type_supported {
if is_alias {
p.error('cannot use the alias type as the parent type is unsupported')

View File

@ -1,3 +1,7 @@
// This tests that V can import and use enums from other modules,
// and that vfmt can handle all edge cases.
import geometry { Shape }
enum Token {
aa = 2
bb
@ -10,8 +14,25 @@ fn test_map_with_enum_keys() {
m[Token.bb] = 'def'
assert m[Token.aa] == 'abc'
assert m[.bb] == 'def'
//
s := '$m'
assert s == "{aa: 'abc', bb: 'def'}"
println(m)
}
fn test_map_with_imported_enum_keys() {
mut fm := map[geometry.Form3D]string{}
fm[.cube] = 'a cube'
fm[geometry.Form3D.sphere] = 'a sphere'
assert fm[.invalid] == ''
assert geometry.Form3D.cube in fm
assert fm[.sphere] == 'a sphere'
}
fn test_map_with_selective_imported_enum_keys() {
mut shapes := map[Shape]string{}
shapes[.circle] = 'a circle'
shapes[Shape.rectangle] = 'a rectangle'
assert shapes[.circle] == 'a circle'
shapes.delete(Shape.circle)
assert Shape.circle !in shapes
}

View File

@ -10,6 +10,15 @@ pub enum Shape {
triangle
}
// used by vlib/v/tests/map_enum_keys_test.v
pub enum Form3D {
sphere
cylinder
cone
cube
invalid
}
pub struct Point {
pub mut:
x int