mirror of
https://github.com/vlang/v.git
synced 2025-09-11 08:25:42 -04:00
checker: check enum static from_string arguments errors (#19163)
This commit is contained in:
parent
f4d2ec70a9
commit
757d26cd89
@ -865,6 +865,15 @@ fn (mut c Checker) fn_call(mut node ast.CallExpr, mut continue_check &bool) ast.
|
|||||||
}
|
}
|
||||||
idx := c.table.type_idxs[full_enum_name]
|
idx := c.table.type_idxs[full_enum_name]
|
||||||
ret_typ := ast.Type(idx).set_flag(.option)
|
ret_typ := ast.Type(idx).set_flag(.option)
|
||||||
|
if node.args.len != 1 {
|
||||||
|
c.error('expected 1 argument, but got ${node.args.len}', node.pos)
|
||||||
|
} else {
|
||||||
|
node.args[0].typ = c.expr(mut node.args[0].expr)
|
||||||
|
if node.args[0].typ != ast.string_type {
|
||||||
|
styp := c.table.type_to_str(node.args[0].typ)
|
||||||
|
c.error('expected `string` argument, but got `${styp}`', node.pos)
|
||||||
|
}
|
||||||
|
}
|
||||||
node.return_type = ret_typ
|
node.return_type = ret_typ
|
||||||
return ret_typ
|
return ret_typ
|
||||||
}
|
}
|
||||||
|
34
vlib/v/checker/tests/enum_from_string_args_err.out
Normal file
34
vlib/v/checker/tests/enum_from_string_args_err.out
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
vlib/v/checker/tests/enum_from_string_args_err.vv:8:2: error: expected 1 argument, but got 0
|
||||||
|
6 |
|
||||||
|
7 | fn main() {
|
||||||
|
8 | Color.from_string()
|
||||||
|
| ~~~~~~~~~~~~~~~~~~~
|
||||||
|
9 | Color.from_string('red', 'blue')
|
||||||
|
10 | Color.from_string(22)
|
||||||
|
vlib/v/checker/tests/enum_from_string_args_err.vv:9:2: error: expected 1 argument, but got 2
|
||||||
|
7 | fn main() {
|
||||||
|
8 | Color.from_string()
|
||||||
|
9 | Color.from_string('red', 'blue')
|
||||||
|
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
10 | Color.from_string(22)
|
||||||
|
11 | Color.from_string(22.22)
|
||||||
|
vlib/v/checker/tests/enum_from_string_args_err.vv:10:2: error: expected `string` argument, but got `int literal`
|
||||||
|
8 | Color.from_string()
|
||||||
|
9 | Color.from_string('red', 'blue')
|
||||||
|
10 | Color.from_string(22)
|
||||||
|
| ~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
11 | Color.from_string(22.22)
|
||||||
|
12 | Color.from_string(true)
|
||||||
|
vlib/v/checker/tests/enum_from_string_args_err.vv:11:2: error: expected `string` argument, but got `float literal`
|
||||||
|
9 | Color.from_string('red', 'blue')
|
||||||
|
10 | Color.from_string(22)
|
||||||
|
11 | Color.from_string(22.22)
|
||||||
|
| ~~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
12 | Color.from_string(true)
|
||||||
|
13 | }
|
||||||
|
vlib/v/checker/tests/enum_from_string_args_err.vv:12:2: error: expected `string` argument, but got `bool`
|
||||||
|
10 | Color.from_string(22)
|
||||||
|
11 | Color.from_string(22.22)
|
||||||
|
12 | Color.from_string(true)
|
||||||
|
| ~~~~~~~~~~~~~~~~~~~~~~~
|
||||||
|
13 | }
|
13
vlib/v/checker/tests/enum_from_string_args_err.vv
Normal file
13
vlib/v/checker/tests/enum_from_string_args_err.vv
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
enum Color {
|
||||||
|
red
|
||||||
|
green
|
||||||
|
blue
|
||||||
|
}
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
Color.from_string()
|
||||||
|
Color.from_string('red', 'blue')
|
||||||
|
Color.from_string(22)
|
||||||
|
Color.from_string(22.22)
|
||||||
|
Color.from_string(true)
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user