Skip to content

Commit

Permalink
checker: refactor string to enum error check, handle `EnumName(s…
Browse files Browse the repository at this point in the history
…tring_variable)` too (#20210)
  • Loading branch information
Delta456 authored Dec 18, 2023
1 parent a8e0ced commit 97d01a9
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
4 changes: 2 additions & 2 deletions vlib/v/checker/checker.v
Original file line number Diff line number Diff line change
Expand Up @@ -3318,8 +3318,8 @@ fn (mut c Checker) cast_expr(mut node ast.CastExpr) ast.Type {
}
}
}
if mut node.expr is ast.StringLiteral {
c.add_error_detail('use ${c.table.type_to_str(node.typ)}.from_string(\'${node.expr.val}\') instead')
if node.expr_type == ast.string_type_idx {
c.add_error_detail('use ${c.table.type_to_str(node.typ)}.from_string(${node.expr}) instead')
c.error('cannot cast `string` to `enum`', node.pos)
}
}
Expand Down
10 changes: 9 additions & 1 deletion vlib/v/checker/tests/string_to_enum_cast_err.out
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,13 @@ vlib/v/checker/tests/string_to_enum_cast_err.vv:8:7: error: cannot cast `string`
7 | fn main() {
8 | _ := Test('one')
| ~~~~~~~~~~~
9 | }
9 |
10 | my_str := 'one'
Details: use main.Test.from_string('one') instead
vlib/v/checker/tests/string_to_enum_cast_err.vv:11:7: error: cannot cast `string` to `enum`
9 |
10 | my_str := 'one'
11 | _ := Test(my_str)
| ~~~~~~~~~~~~
12 | }
Details: use main.Test.from_string(my_str) instead
3 changes: 3 additions & 0 deletions vlib/v/checker/tests/string_to_enum_cast_err.vv
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@ enum Test {

fn main() {
_ := Test('one')

my_str := 'one'
_ := Test(my_str)
}

0 comments on commit 97d01a9

Please sign in to comment.