checker: disallow invalid T cast (fix #22346) (#22354)

This commit is contained in:
Swastik Baranwal 2024-10-03 04:22:42 +05:30 committed by GitHub
parent dafca9353c
commit 7b8e059a3c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 48 additions and 0 deletions

View file

@ -3191,6 +3191,11 @@ fn (mut c Checker) cast_expr(mut node ast.CastExpr) ast.Type {
return to_type
}
if !c.expected_type.has_flag(.generic) && to_sym.name.len == 1
&& to_sym.name.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)

View file

@ -0,0 +1,13 @@
vlib/v/checker/tests/cast_expr_T_type_err.vv:4:9: error: unknown type `A`
2 |
3 | fn main() {
4 | _ = A(0)
| ~~~~
5 | _ = &A(0)
6 | }
vlib/v/checker/tests/cast_expr_T_type_err.vv:5:9: error: unknown type `A`
3 | fn main() {
4 | _ = A(0)
5 | _ = &A(0)
| ~~~~~
6 | }

View file

@ -0,0 +1,6 @@
module main
fn main() {
_ = A(0)
_ = &A(0)
}

View file

@ -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 |

View file

@ -0,0 +1,10 @@
module main
__global (
fo = A(0)
fo1 = &A(0)
)
fn main() {
}