From f09a5135e976c9c135e39f6f2e715f9fd0ed1f4f Mon Sep 17 00:00:00 2001 From: Alexander Medvednikov Date: Thu, 20 May 2021 03:14:27 +0300 Subject: [PATCH] checker: handle builtin enum init correctly --- vlib/os/os.v | 4 ++++ vlib/sqlite/sqlite.v | 1 + vlib/sync/channel_1_test.v | 6 ++++++ vlib/v/ast/table.v | 2 +- vlib/v/checker/checker.v | 14 +++++++++++--- vlib/vweb/vweb.v | 3 ++- 6 files changed, 25 insertions(+), 5 deletions(-) diff --git a/vlib/os/os.v b/vlib/os/os.v index 93c8c85f0..c5e05432a 100644 --- a/vlib/os/os.v +++ b/vlib/os/os.v @@ -546,6 +546,10 @@ pub fn temp_dir() string { } } } + $if macos { + // avoid /var/folders/6j/cmsk8gd90pd.... on macs + return '/tmp' + } $if android { // TODO test+use '/data/local/tmp' on Android before using cache_dir() if path == '' { diff --git a/vlib/sqlite/sqlite.v b/vlib/sqlite/sqlite.v index f28b3d711..e42e684cc 100644 --- a/vlib/sqlite/sqlite.v +++ b/vlib/sqlite/sqlite.v @@ -92,6 +92,7 @@ pub fn (mut db DB) close() ?bool { // Only for V ORM fn (db DB) init_stmt(query string) &C.sqlite3_stmt { + // println('init_stmt("$query")') stmt := &C.sqlite3_stmt(0) C.sqlite3_prepare_v2(db.conn, &char(query.str), query.len, &stmt, 0) return stmt diff --git a/vlib/sync/channel_1_test.v b/vlib/sync/channel_1_test.v index 07383b951..17588fd48 100644 --- a/vlib/sync/channel_1_test.v +++ b/vlib/sync/channel_1_test.v @@ -17,3 +17,9 @@ fn test_channel_buffered() { } assert sum == u64(num_iterations) * (num_iterations - 1) / 2 } + +fn test_builtin_enum() { + x := ChanState.closed + assert x == .closed + println(x) +} diff --git a/vlib/v/ast/table.v b/vlib/v/ast/table.v index 5f0f71675..b58f0b323 100644 --- a/vlib/v/ast/table.v +++ b/vlib/v/ast/table.v @@ -523,7 +523,7 @@ pub fn (mut t Table) register_type_symbol(typ TypeSymbol) int { .placeholder { // override placeholder // println('overriding type placeholder `$typ.name`') - t.type_symbols[existing_idx] = TypeSymbol{ + t.type_symbols[existing_idx] = { ...typ methods: ex_type.methods } diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index ce613c97d..4c9740422 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -6455,15 +6455,23 @@ pub fn (mut c Checker) index_expr(mut node ast.IndexExpr) ast.Type { // If a short form is used, `expected_type` needs to be an enum // with this value. pub fn (mut c Checker) enum_val(mut node ast.EnumVal) ast.Type { - typ_idx := if node.enum_name == '' { + mut typ_idx := if node.enum_name == '' { c.expected_type.idx() } else { // c.table.find_type_idx(node.enum_name) } // println('checker: enum_val: $node.enum_name typeidx=$typ_idx') if typ_idx == 0 { - c.error('not an enum (name=$node.enum_name) (type_idx=0)', node.pos) - return ast.void_type + // Handle `builtin` enums like `ChanState`, so that `x := ChanState.closed` works. + // In the checker the name for such enums was set to `main.ChanState` instead of + // just `ChanState`. + if node.enum_name.starts_with('main.') { + typ_idx = c.table.find_type_idx(node.enum_name['.main'.len..]) + if typ_idx == 0 { + c.error('unknown enum `$node.enum_name` (type_idx=0)', node.pos) + return ast.void_type + } + } } mut typ := ast.new_type(typ_idx) if c.pref.translated { diff --git a/vlib/vweb/vweb.v b/vlib/vweb/vweb.v index 21cb834c0..9079ffaaf 100644 --- a/vlib/vweb/vweb.v +++ b/vlib/vweb/vweb.v @@ -316,7 +316,7 @@ pub fn run(global_app &T, port int) { //} for { // Create a new app object for each connection, copy global data like db connections - mut request_app := T{} + mut request_app := &T{} $if T is DbInterface { request_app.db = global_app.db } $else { @@ -619,6 +619,7 @@ pub fn (ctx &Context) ip() string { // Set s to the form error pub fn (mut ctx Context) error(s string) { + println('vweb error: $s') ctx.form_error = s } -- 2.30.2