From f9ceb12e2257d8d16b5f9639db62438b6c13faa9 Mon Sep 17 00:00:00 2001 From: Louis Schmieder Date: Wed, 29 Sep 2021 23:43:08 +0200 Subject: [PATCH] checker: fix orm cast check (#12018) --- vlib/v/checker/checker.v | 4 ++++ vlib/v/checker/tests/orm_not_a_struct.out | 7 +++++++ vlib/v/checker/tests/orm_not_a_struct.vv | 12 ++++++++++++ 3 files changed, 23 insertions(+) create mode 100644 vlib/v/checker/tests/orm_not_a_struct.out create mode 100644 vlib/v/checker/tests/orm_not_a_struct.vv diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 536e7110d9..f4a2bc803c 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -7930,6 +7930,10 @@ fn (mut c Checker) sql_expr(mut node ast.SqlExpr) ast.Type { sym := c.table.get_type_symbol(node.table_expr.typ) c.ensure_type_exists(node.table_expr.typ, node.pos) or { return ast.void_type } c.cur_orm_ts = *sym + if sym.info !is ast.Struct { + c.error('The table symbol `$sym.name` has to be a struct', node.table_expr.pos) + return ast.void_type + } info := sym.info as ast.Struct fields := c.fetch_and_verify_orm_fields(info, node.table_expr.pos, sym.name) mut sub_structs := map[int]ast.SqlExpr{} diff --git a/vlib/v/checker/tests/orm_not_a_struct.out b/vlib/v/checker/tests/orm_not_a_struct.out new file mode 100644 index 0000000000..26f2f6ec77 --- /dev/null +++ b/vlib/v/checker/tests/orm_not_a_struct.out @@ -0,0 +1,7 @@ +vlib/v/checker/tests/orm_not_a_struct.vv:10:15: error: The table symbol `Person` has to be a struct + 8 | db := sqlite.connect(':memory:')? + 9 | _ := sql db { + 10 | select from Person + | ~~~~~~ + 11 | } + 12 | } diff --git a/vlib/v/checker/tests/orm_not_a_struct.vv b/vlib/v/checker/tests/orm_not_a_struct.vv new file mode 100644 index 0000000000..05b18913f4 --- /dev/null +++ b/vlib/v/checker/tests/orm_not_a_struct.vv @@ -0,0 +1,12 @@ +import sqlite + +enum Person { + test +} + +fn main() { + db := sqlite.connect(':memory:')? + _ := sql db { + select from Person + } +}