Skip to content

Commit

Permalink
checker: disallow invalid T cast
Browse files Browse the repository at this point in the history
  • Loading branch information
Delta456 committed Sep 29, 2024
1 parent fca375e commit 915ab53
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
6 changes: 6 additions & 0 deletions vlib/v/checker/checker.v
Original file line number Diff line number Diff line change
Expand Up @@ -3191,6 +3191,12 @@ fn (mut c Checker) cast_expr(mut node ast.CastExpr) ast.Type {
return to_type
}

type_str := c.table.type_to_str(to_type)
type_rm_ptr := type_str.replace('&', '')
if type_rm_ptr.len == 1 && type_rm_ptr.starts_with_capital() {
c.error('unknown type `${to_sym.name}`', node.pos)
}

if to_sym.language != .c {
c.ensure_type_exists(to_type, node.pos)

Expand Down
14 changes: 14 additions & 0 deletions vlib/v/checker/tests/globals/cast_expr_T_type_err.out
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
vlib/v/checker/tests/globals/cast_expr_T_type_err.vv:4:7: error: unknown type `A`
2 |
3 | __global (
4 | fo = A(0)
| ~~~~
5 | fo1 = &A(0)
6 | )
vlib/v/checker/tests/globals/cast_expr_T_type_err.vv:5:8: error: unknown type `A`
3 | __global (
4 | fo = A(0)
5 | fo1 = &A(0)
| ~~~~~
6 | )
7 |
22 changes: 22 additions & 0 deletions vlib/v/checker/tests/globals/cast_expr_T_type_err.vv
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module main

__global (
fo = A(0)
fo1 = &A(0)
)

fn main() {

}

module main

__global (
fo = A(0)
fo1 = &A(0)
)

fn main() {

}

0 comments on commit 915ab53

Please sign in to comment.