checker: handle builtin enum init correctly

pull/10143/head
Alexander Medvednikov 2021-05-20 03:14:27 +03:00
parent ac469f5eff
commit f09a5135e9
6 changed files with 25 additions and 5 deletions

View File

@ -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 == '' {

View File

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

View File

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

View File

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

View File

@ -6455,16 +6455,24 @@ 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)
// 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 {
// TODO make more strict

View File

@ -316,7 +316,7 @@ pub fn run<T>(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
}