From a119affebaa6e4e26a6e4ee6944ec82186b62231 Mon Sep 17 00:00:00 2001 From: yuyi Date: Thu, 18 Feb 2021 15:38:57 +0800 Subject: [PATCH] cgen: check unknown sizeof type (#8815) --- .../v/checker/tests/unknown_sizeof_type_err_a.out | 6 ++++++ vlib/v/checker/tests/unknown_sizeof_type_err_a.vv | 15 +++++++++++++++ .../v/checker/tests/unknown_sizeof_type_err_b.out | 6 ++++++ vlib/v/checker/tests/unknown_sizeof_type_err_b.vv | 15 +++++++++++++++ vlib/v/gen/c/cgen.v | 4 ++++ vlib/v/parser/pratt.v | 2 +- 6 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 vlib/v/checker/tests/unknown_sizeof_type_err_a.out create mode 100644 vlib/v/checker/tests/unknown_sizeof_type_err_a.vv create mode 100644 vlib/v/checker/tests/unknown_sizeof_type_err_b.out create mode 100644 vlib/v/checker/tests/unknown_sizeof_type_err_b.vv diff --git a/vlib/v/checker/tests/unknown_sizeof_type_err_a.out b/vlib/v/checker/tests/unknown_sizeof_type_err_a.out new file mode 100644 index 0000000000..5c6e6cb7de --- /dev/null +++ b/vlib/v/checker/tests/unknown_sizeof_type_err_a.out @@ -0,0 +1,6 @@ +vlib/v/checker/tests/unknown_sizeof_type_err_a.vv:14:34: cgen error: unknown type `T` + 12 | println("size of Abc: ${sizeof(Abc)}") + 13 | println("size of Xyz: ${sizeof(Xyz)}") + 14 | println("size of Test: ${sizeof(T)}") + | ^ + 15 | } diff --git a/vlib/v/checker/tests/unknown_sizeof_type_err_a.vv b/vlib/v/checker/tests/unknown_sizeof_type_err_a.vv new file mode 100644 index 0000000000..dd15843501 --- /dev/null +++ b/vlib/v/checker/tests/unknown_sizeof_type_err_a.vv @@ -0,0 +1,15 @@ +struct Abc { + i int +} + +struct Xyz { + f f64 +} + +type Test = Abc | Xyz + +fn main() { + println("size of Abc: ${sizeof(Abc)}") + println("size of Xyz: ${sizeof(Xyz)}") + println("size of Test: ${sizeof(T)}") +} diff --git a/vlib/v/checker/tests/unknown_sizeof_type_err_b.out b/vlib/v/checker/tests/unknown_sizeof_type_err_b.out new file mode 100644 index 0000000000..7daa009254 --- /dev/null +++ b/vlib/v/checker/tests/unknown_sizeof_type_err_b.out @@ -0,0 +1,6 @@ +vlib/v/checker/tests/unknown_sizeof_type_err_b.vv:14:34: cgen error: unknown type `Zabc` + 12 | println("size of Abc: ${sizeof(Abc)}") + 13 | println("size of Xyz: ${sizeof(Xyz)}") + 14 | println("size of Test: ${sizeof(Zabc)}") + | ~~~~ + 15 | } diff --git a/vlib/v/checker/tests/unknown_sizeof_type_err_b.vv b/vlib/v/checker/tests/unknown_sizeof_type_err_b.vv new file mode 100644 index 0000000000..154f406b06 --- /dev/null +++ b/vlib/v/checker/tests/unknown_sizeof_type_err_b.vv @@ -0,0 +1,15 @@ +struct Abc { + i int +} + +struct Xyz { + f f64 +} + +type Test = Abc | Xyz + +fn main() { + println("size of Abc: ${sizeof(Abc)}") + println("size of Xyz: ${sizeof(Xyz)}") + println("size of Test: ${sizeof(Zabc)}") +} diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index 4b3541151a..256d862aa7 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -2786,6 +2786,10 @@ fn (mut g Gen) expr(node ast.Expr) { } ast.SizeOf { node_typ := g.unwrap_generic(node.typ) + sym := g.table.get_type_symbol(node_typ) + if sym.language == .v && sym.kind in [.placeholder, .any] { + g.error('unknown type `$sym.name`', node.pos) + } styp := g.typ(node_typ) g.write('/*SizeOf*/ sizeof(${util.no_dots(styp)})') } diff --git a/vlib/v/parser/pratt.v b/vlib/v/parser/pratt.v index 70afeddfe1..3717eabe3b 100644 --- a/vlib/v/parser/pratt.v +++ b/vlib/v/parser/pratt.v @@ -163,9 +163,9 @@ pub fn (mut p Parser) expr(precedence int) ast.Expr { } } .key_sizeof { - pos := p.tok.position() p.next() // sizeof p.check(.lpar) + pos := p.tok.position() is_known_var := p.mark_var_as_used(p.tok.lit) // assume mod. prefix leads to a type if is_known_var || !(p.known_import(p.tok.lit) || p.tok.kind.is_start_of_type()) {