vlang / v

Simple, fast, safe, compiled language for developing maintainable software. Compiles itself in <1s with zero library dependencies. Supports automatic C => V translation. https://vlang.io
MIT License
35.63k stars 2.15k forks source link

orm, checker: error when using options fields #18333

Open remdragon opened 1 year ago

remdragon commented 1 year ago

Describe the bug

I'm getting a panic that doesn't tell me what or even where the problem is in my code

Expected Behavior

An error message that tells me what's wrong

Current Behavior

V panic: array.get: index out of range (i == 16777236, a.len == 232)
v hash: 0fc33c6
C:/Users/royce3/AppData/Local/Temp/v_0/v2.8592022617382340753.tmp.c:21370: at _v_panic: Backtrace
C:/Users/royce3/AppData/Local/Temp/v_0/v2.8592022617382340753.tmp.c:20848: by array_get
C:/Users/royce3/AppData/Local/Temp/v_0/v2.8592022617382340753.tmp.c:16384: by anon_fn_48bf088567a1e8f8_v__ast__structfield__bool_8445
C:/Users/royce3/AppData/Local/Temp/v_0/v2.8592022617382340753.tmp.c:1980: by v__checker__Checker_fetch_and_verify_orm_fields
C:/Users/royce3/AppData/Local/Temp/v_0/v2.8592022617382340753.tmp.c:1867: by v__checker__Checker_sql_stmt_line
C:/Users/royce3/AppData/Local/Temp/v_0/v2.8592022617382340753.tmp.c:1770: by v__checker__Checker_sql_stmt
C:/Users/royce3/AppData/Local/Temp/v_0/v2.8592022617382340753.tmp.c:55921: by v__checker__Checker_stmt
C:/Users/royce3/AppData/Local/Temp/v_0/v2.8592022617382340753.tmp.c:56367: by v__checker__Checker_stmts_ending_with_expression
C:/Users/royce3/AppData/Local/Temp/v_0/v2.8592022617382340753.tmp.c:56343: by v__checker__Checker_stmts
C:/Users/royce3/AppData/Local/Temp/v_0/v2.8592022617382340753.tmp.c:62055: by v__checker__Checker_fn_decl
C:/Users/royce3/AppData/Local/Temp/v_0/v2.8592022617382340753.tmp.c:55881: by v__checker__Checker_stmt
C:/Users/royce3/AppData/Local/Temp/v_0/v2.8592022617382340753.tmp.c:54071: by v__checker__Checker_check
C:/Users/royce3/AppData/Local/Temp/v_0/v2.8592022617382340753.tmp.c:54138: by v__checker__Checker_check_files
C:/Users/royce3/AppData/Local/Temp/v_0/v2.8592022617382340753.tmp.c:42491: by v__builder__Builder_middle_stages
C:/Users/royce3/AppData/Local/Temp/v_0/v2.8592022617382340753.tmp.c:42522: by v__builder__Builder_front_and_middle_stages
C:/Users/royce3/AppData/Local/Temp/v_0/v2.8592022617382340753.tmp.c:45370: by v__builder__cbuilder__gen_c
C:/Users/royce3/AppData/Local/Temp/v_0/v2.8592022617382340753.tmp.c:45353: by v__builder__cbuilder__build_c
C:/Users/royce3/AppData/Local/Temp/v_0/v2.8592022617382340753.tmp.c:45343: by v__builder__cbuilder__compile_c
C:/Users/royce3/AppData/Local/Temp/v_0/v2.8592022617382340753.tmp.c:45245: by v__builder__Builder_rebuild
C:/Users/royce3/AppData/Local/Temp/v_0/v2.8592022617382340753.tmp.c:43975: by v__builder__compile
C:/Users/royce3/AppData/Local/Temp/v_0/v2.8592022617382340753.tmp.c:45691: by main__rebuild
C:/Users/royce3/AppData/Local/Temp/v_0/v2.8592022617382340753.tmp.c:45578: by main__main
C:/Users/royce3/AppData/Local/Temp/v_0/v2.8592022617382340753.tmp.c:46692: by wmain
00a88220 : by ???
00a88383 : by ???

Reproduction Steps

v run bug.v .

Here's bug.v:

module main

import db.sqlite

struct Client {
    id int [primary; sql: serial]
    name ?string
}

fn main() {
    mut db := sqlite.connect( 'test.sqlite' ) or { panic( 'sqlite connect failed' )}
    sql db {
        create table Client
    } or { panic( 'create table failed' )}
}

Possible Solution

no idea

Additional Information/Context

I'm guessing this might have to do with defining the name field as ?string, I'm trying to specify that this field should be nullable. I see in the ORM documentation that there is a [nonull] flag, which probably means that fields are nullable by default, but I think that if I don't define [nonull] that the proper type of the field should be ?string.

V version

V 0.3.4 0fc33c6

Environment details (OS name and version, etc.)

V 0.3.4 0fc33c6

C:\cvs\itas\openai_expert>v doctor
V full version: V 0.3.4 4dc3388.0fc33c6
OS: windows, Microsoft Windows 10 Home v19045 64-bit
Processor: 4 cpus, 64bit, little endian,

getwd: C:\cvs\itas\openai_expert
vexe: C:\cvs\v\v.exe
vexe mtime: 2023-06-02 12:55:43

vroot: OK, value: C:\cvs\v
VMODULES: OK, value: C:\Users\royce3\.vmodules
VTMP: OK, value: C:\Users\royce3\AppData\Local\Temp\v_0

Git version: git version 2.37.3.windows.1
Git vroot status: weekly.2023.22-5-g0fc33c6f
.git/config present: true

CC version: Error: exec failed (CreateProcess) with code 2: The system cannot find the file specified.
 cmd: cc --version
thirdparty/tcc status: thirdparty-windows-amd64 e90c2620
JalonSolov commented 1 year ago

Add the -g option to your V compile line.

remdragon commented 1 year ago

Doesn't look any different to me:

C:\cvs\itas\openai_expert>v -g run bug.v
V panic: array.get: index out of range (i == 16777236, a.len == 232)
v hash: 0fc33c6
C:/Users/royce3/AppData/Local/Temp/v_0/v2.8592022617382340753.tmp.c:21370: at _v_panic: Backtrace
C:/Users/royce3/AppData/Local/Temp/v_0/v2.8592022617382340753.tmp.c:20848: by array_get
C:/Users/royce3/AppData/Local/Temp/v_0/v2.8592022617382340753.tmp.c:16384: by anon_fn_48bf088567a1e8f8_v__ast__structfield__bool_8445
C:/Users/royce3/AppData/Local/Temp/v_0/v2.8592022617382340753.tmp.c:1980: by v__checker__Checker_fetch_and_verify_orm_fields
C:/Users/royce3/AppData/Local/Temp/v_0/v2.8592022617382340753.tmp.c:1867: by v__checker__Checker_sql_stmt_line
C:/Users/royce3/AppData/Local/Temp/v_0/v2.8592022617382340753.tmp.c:1770: by v__checker__Checker_sql_stmt
C:/Users/royce3/AppData/Local/Temp/v_0/v2.8592022617382340753.tmp.c:55921: by v__checker__Checker_stmt
C:/Users/royce3/AppData/Local/Temp/v_0/v2.8592022617382340753.tmp.c:56367: by v__checker__Checker_stmts_ending_with_expression
C:/Users/royce3/AppData/Local/Temp/v_0/v2.8592022617382340753.tmp.c:56343: by v__checker__Checker_stmts
C:/Users/royce3/AppData/Local/Temp/v_0/v2.8592022617382340753.tmp.c:62055: by v__checker__Checker_fn_decl
C:/Users/royce3/AppData/Local/Temp/v_0/v2.8592022617382340753.tmp.c:55881: by v__checker__Checker_stmt
C:/Users/royce3/AppData/Local/Temp/v_0/v2.8592022617382340753.tmp.c:54071: by v__checker__Checker_check
C:/Users/royce3/AppData/Local/Temp/v_0/v2.8592022617382340753.tmp.c:54138: by v__checker__Checker_check_files
C:/Users/royce3/AppData/Local/Temp/v_0/v2.8592022617382340753.tmp.c:42491: by v__builder__Builder_middle_stages
C:/Users/royce3/AppData/Local/Temp/v_0/v2.8592022617382340753.tmp.c:42522: by v__builder__Builder_front_and_middle_stages
C:/Users/royce3/AppData/Local/Temp/v_0/v2.8592022617382340753.tmp.c:45370: by v__builder__cbuilder__gen_c
C:/Users/royce3/AppData/Local/Temp/v_0/v2.8592022617382340753.tmp.c:45353: by v__builder__cbuilder__build_c
C:/Users/royce3/AppData/Local/Temp/v_0/v2.8592022617382340753.tmp.c:45343: by v__builder__cbuilder__compile_c
C:/Users/royce3/AppData/Local/Temp/v_0/v2.8592022617382340753.tmp.c:45245: by v__builder__Builder_rebuild
C:/Users/royce3/AppData/Local/Temp/v_0/v2.8592022617382340753.tmp.c:43975: by v__builder__compile
C:/Users/royce3/AppData/Local/Temp/v_0/v2.8592022617382340753.tmp.c:45691: by main__rebuild
C:/Users/royce3/AppData/Local/Temp/v_0/v2.8592022617382340753.tmp.c:45578: by main__main
C:/Users/royce3/AppData/Local/Temp/v_0/v2.8592022617382340753.tmp.c:46692: by wmain
00a88220 : by ???
00a88383 : by ???
JalonSolov commented 1 year ago

That tells you the problem isn't in your code, it's in the compiler.

walkingdevel commented 1 year ago

This problem is related to the lack of support for optional fields in the ORM. I have been working on it for some time, but I encountered issues with the database libraries. Once I resolve those issues, I will be able to complete this task. Therefore, it is not advisable to close it at this time. I will also update the issue name for better accuracy.

felipensp commented 8 months ago

Is this is issue yet reproducible with latest version?