v icon indicating copy to clipboard operation
v copied to clipboard

[ORM] | Incompatible types when assigning to type ‘_option_string’ from type ‘string’

Open Ddiidev opened this issue 2 years ago • 5 comments

Describe the bug

When trying to create tables in Postgres, some errors occurred when generating the C code. Errors with option and an "else" placed in the wrong place.

If you need a postgres bank for testing, a good one is supabase, in 5 minutes you can get it lit

Reproduction Steps

I have the following relationship of structs via sql:

pub struct Address {
	id int [primary; sql: serial; nonull]
	create_at string [default: 'now()'; nonull; sql_type: 'TIMESTAMP']
	update_at string [default: 'now()'; nonull; sql_type: 'TIMESTAMP']
	street string [nonull]
	city string [nonull]
	state string [nonull]
	zip_code string [nonull]
	proximity string [nonull]
}
[table: 'RealStates']
pub struct RealState {
	id string [primary; sql_type: 'uuid'; default: "uuid_generate_v4()"; nonull]
	parent_id int
	name string [sql_type: 'varchar(80)'; nonull]
	cnpj string [sql_type: 'varchar(14)'; nonull]
	address Address [fkey: 'id']
}
[table: 'Realtors']
pub struct Realtor {
	id string [primary; sql_type: 'uuid'; default: "uuid_generate_v4()"; nonull]
	first_name string [sql_type: "VARCHAR(30)"; nonull]
	last_name string [sql_type: "VARCHAR(30)"; nonull]
	creci string [sql_type: "VARCHAR(8)"; nonull]
	cnpj ?string [sql_type: "VARCHAR(15)"]
	cpf ?string [sql_type: "VARCHAR(12)"]
	phone string [sql_type: "VARCHAR(15)"; nonull]
	real_state RealState [fkey: 'id']
}

A bug occurred when compiling:

mut db := pg.connect(host: pg_host, user: pg_user, password: pg_pass, dbname: pg_db)!
defer {
	db.close()
}

dump(sql db {
	select from Realtor
}!)

Expected Behavior

something like...

[./src/main.v:37] ast.SqlExpr: [Realtor{
	id: 'ffce0e60-084d-49c0-9e19-a272f257b1a4',
	first_name: 'asdsd',
	last_name: 'qweqwe',
	creci: '4312',
	cnpj: '',
	cpf: '',
	phone: '1241233',
	real_state: '1'
}]

Current Behavior

andre_luiz@DESKTOP-BEQBL96:/mnt/c/Users/AndreLuiz/Documents/SourceApps/QRImovel/QRIMovel-backend$ v -cc gcc run .
==================
/tmp/v_1000/QRIMovel-backend.16381370299362744446.tmp.c: In function ‘main__main’:
/tmp/v_1000/QRIMovel-backend.16381370299362744446.tmp.c:17894:25: error: ‘else’ without a previous ‘if’
17894 |                         else {
      |                         ^~~~
/tmp/v_1000/QRIMovel-backend.16381370299362744446.tmp.c:17905:44: error: incompatible types when assigning to type ‘_option_string’ from type ‘string’
17905 |                                 _t7.cnpj = *((*(orm__Primitive*) array_get((*(Array_orm__Primitive*) array_get(_t6, _t8)), 4))._string);
      |                                            ^
/tmp/v_1000/QRIMovel-backend.16381370299362744446.tmp.c:17906:43: error: incompatible types when assigning to type ‘_option_string’ from type ‘string’
17906 |                                 _t7.cpf = *((*(orm__Primitive*) array_get((*(Array_orm__Primitive*) array_get(_t6, _t8)), 5))._string);
      |                                           ^
...
==================
(Use `v -cg` to print the entire error message)

builder error:
==================
C error. This should never happen.

This is a compiler bug, please report it using `v bug file.v`.

https://github.com/vlang/v/issues/new/choose

You can also use #help on Discord: https://discord.gg/vlang

Possible Solution

I don't know exactly how to solve it, but the "else" problem I believe it should look like this...

image

Rather than ... image

I tested it and it "worked", the only thing missing were the bugs with the options: image

Additional Information/Context

I've already tried using tcc, gcc and clang. I can't do the test on Windows (my native OS) because I couldn't install Postgres at all, I've already read the README, downloaded and installed the Postgres installer, but it always gives an error stating that the 'libpq-' file is missing. fe.h', I downloaded the Postgres repo and added all the necessary files and it worked, but soon header files were missing that I couldn't find in the repo, since then I have only developed within WSL with Ubuntu.

V version

V 0.4.1 12dd6e8

Environment details (OS name and version, etc.)

V full version: V 0.4.1 45e6e7d.12dd6e8 OS: linux, Ubuntu 22.04.3 LTS (WSL 2) Processor: 8 cpus, 64bit, little endian, 11th Gen Intel(R) Core(TM) i7-1165G7 @ 2.80GHz

getwd: /mnt/c/Users/AndreLuiz/Documents/SourceApps/QRImovel/QRIMovel-backend vexe: /home/andre_luiz/system/v/v vexe mtime: 2023-09-09 23:52:49

vroot: OK, value: /home/andre_luiz/system/v VMODULES: OK, value: /home/andre_luiz/.vmodules VTMP: OK, value: /tmp/v_1000

Git version: git version 2.34.1 Git vroot status: weekly.2023.33-138-g12dd6e8b (4 commit(s) behind V master) .git/config present: true

CC version: cc (Ubuntu 11.4.0-1ubuntu1~22.04) 11.4.0 thirdparty/tcc status: Error: fatal: detected dubious ownership in repository at '/home/andre_luiz/system/v/thirdparty/tcc' To add an exception for this directory, call:

    git config --global --add safe.directory /home/andre_luiz/system/v/thirdparty/tcc

Error: fatal: detected dubious ownership in repository at '/home/andre_luiz/system/v/thirdparty/tcc' To add an exception for this directory, call:

    git config --global --add safe.directory /home/andre_luiz/system/v/thirdparty/tcc

Ddiidev avatar Sep 11 '23 02:09 Ddiidev

Is this is issue yet reproducible with latest version?

felipensp avatar Dec 11 '23 12:12 felipensp

Today I needed to do this relationship and I had the same problem. print of the set of structs.

image

link for test: play

Ddiidev avatar Dec 22 '23 09:12 Ddiidev

It doesn't seem like a compilation error to me, but an internal error in V. (I could be very wrong).

I recompiled V, with the following... v -cc msvc -showcc self

However, when trying to compile an example code... v -showcc -cc msvc run teste.v

result:

Unhandled Exception 0xC0000005
25: v__ast__Scope_find         ?? : address = 0x1a3d87ed750
24: v__ast__Scope_find_var     ?? : address = 0x1a3d87ed780
23: v__checker__Checker_support_lambda_expr_one_param  ?? : address = 0x1a3d87ed7a0
22: v__checker__Checker_support_lambda_expr_one_param  ?? : address = 0x1a3d87ed6b0
21: v__checker__Checker_support_lambda_expr_one_param  ?? : address = 0x1a3d87ed7b0
20: v__checker__Checker_support_lambda_expr_one_param  ?? : address = 0x1a3d87ed7c0
19: v__checker__Checker_check_files  ?? : address = 0x1a3d87ed740
18: v__checker__Checker_check_files  ?? : address = 0x1a3d87ed660
17: v__checker__Checker_check_files  ?? : address = 0x1a3d87ed5f0
16: v__checker__Checker_expr   ?? : address = 0x1a3d87ed580
15: v__checker__Checker_check_files  ?? : address = 0x1a3d87ed600
14: v__checker__Checker_check  ?? : address = 0x1a3d87ed7f0
13: v__checker__Checker_check_files  ?? : address = 0x1a3d87ed910
12: v__builder__Builder_middle_stages  ?? : address = 0x1a3d87ed5a0
11: v__builder__Builder_front_and_middle_stages  ?? : address = 0x1a3d87ed720
10: v__builder__cbuilder__gen_c  ?? : address = 0x1a3d87ed8b0
9 : v__builder__cbuilder__build_c  ?? : address = 0x1a3d87ed5c0
8 : v__builder__cbuilder__compile_c  ?? : address = 0x1a3d87ed730
7 : v__builder__Builder_rebuild  ?? : address = 0x1a3d87ed850
6 : v__builder__compile        ?? : address = 0x1a3d87ed860
5 : v__builder__cbuilder__gen_c  ?? : address = 0x1a3d87ed690
4 : v__builder__cbuilder__gen_c  ?? : address = 0x1a3d87ed5e0
3 : wmain                      ?? : address = 0x1a3d87ed6a0
2 : __scrt_common_main_seh     D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:288
1 : BaseThreadInitThunk        ?? : address = 0x1a3d87edb00
0 : RtlUserThreadStart         ?? : address = 0x1a3d87edb20
signal 11: segmentation fault
32: print_backtrace            ?? : address = 0x1a3d87ed6c0
31: log2f                      ?? : address = 0x1a3d87ed6d0
30: `__scrt_common_main_seh'::`1'::filt$0  D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:304
29: _C_specific_handler        ?? : address = 0x1a3d87ed6f0
28: _chkstk                    ?? : address = 0x1a3d87ed700
27: RtlFindCharInUnicodeString  ?? : address = 0x1a3d87ed880
26: KiUserExceptionDispatcher  ?? : address = 0x1a3d87ed8f0
25: v__ast__Scope_find         ?? : address = 0x1a3d87ed900
24: v__ast__Scope_find_var     ?? : address = 0x1a3d87edb40
23: v__checker__Checker_support_lambda_expr_one_param  ?? : address = 0x1a3d87ed9e0
22: v__checker__Checker_support_lambda_expr_one_param  ?? : address = 0x1a3d87ed9b0
21: v__checker__Checker_support_lambda_expr_one_param  ?? : address = 0x1a3d87edd50
20: v__checker__Checker_support_lambda_expr_one_param  ?? : address = 0x1a3d87edd70
19: v__checker__Checker_check_files  ?? : address = 0x1a3d87edaa0
18: v__checker__Checker_check_files  ?? : address = 0x1a3d87edc80
17: v__checker__Checker_check_files  ?? : address = 0x1a3d87edbb0
16: v__checker__Checker_expr   ?? : address = 0x1a3d87ed9a0
15: v__checker__Checker_check_files  ?? : address = 0x1a3d87eda20
14: v__checker__Checker_check  ?? : address = 0x1a3d87edbd0
13: v__checker__Checker_check_files  ?? : address = 0x1a3d87edc30
12: v__builder__Builder_middle_stages  ?? : address = 0x1a3d87eda30
11: v__builder__Builder_front_and_middle_stages  ?? : address = 0x1a3d87edb10
10: v__builder__cbuilder__gen_c  ?? : address = 0x1a3d87eda40
9 : v__builder__cbuilder__build_c  ?? : address = 0x1a3d87eda50
8 : v__builder__cbuilder__compile_c  ?? : address = 0x1a3d87eda70
7 : v__builder__Builder_rebuild  ?? : address = 0x1a3d87edb50
6 : v__builder__compile        ?? : address = 0x1a3d87edd40
5 : v__builder__cbuilder__gen_c  ?? : address = 0x1a3d87edb70
4 : v__builder__cbuilder__gen_c  ?? : address = 0x1a3d87edc50
3 : wmain                      ?? : address = 0x1a3d87edd30
2 : __scrt_common_main_seh     D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:288
1 : BaseThreadInitThunk        ?? : address = 0x1a3d87edc60
0 : RtlUserThreadStart         ?? : address = 0x1a3d87edd60

Something interesting is that it doesn't display -showcc, so I imagine it's something related to V itself.

Ddiidev avatar Dec 22 '23 10:12 Ddiidev

Is this still crashing for you on latest version?

felipensp avatar Feb 13 '24 20:02 felipensp

Is this still crashing for you on latest version?

Before there was an error in cgen, but now it's partly ok!

But it still doesn't work.

https://vosca.dev/p/075dd2aa4d

Running code...
Can't run code. The server returned an error:
V panic: as cast: cannot cast `unknown` to `v.ast.InfixExpr`
v hash: 4f742ad
/tmp/v_0/v2.01HPNNKGWX7GVNSYE1BXAAAAH8.tmp.c:21529: at _v_panic: Backtrace
/tmp/v_0/v2.01HPNNKGWX7GVNSYE1BXAAAAH8.tmp.c:22025: by __as_cast
/tmp/v_0/v2.01HPNNKGWX7GVNSYE1BXAAAAH8.tmp.c:48433: by v__gen__c__Gen_write_orm_select
/tmp/v_0/v2.01HPNNKGWX7GVNSYE1BXAAAAH8.tmp.c:48479: by v__gen__c__Gen_write_orm_select
/tmp/v_0/v2.01HPNNKGWX7GVNSYE1BXAAAAH8.tmp.c:47346: by v__gen__c__Gen_sql_select_expr
/tmp/v_0/v2.01HPNNKGWX7GVNSYE1BXAAAAH8.tmp.c:33913: by v__gen__c__Gen_expr
/tmp/v_0/v2.01HPNNKGWX7GVNSYE1BXAAAAH8.tmp.c:27742: by v__gen__c__Gen_assign_stmt
/tmp/v_0/v2.01HPNNKGWX7GVNSYE1BXAAAAH8.tmp.c:32507: by v__gen__c__Gen_stmt
/tmp/v_0/v2.01HPNNKGWX7GVNSYE1BXAAAAH8.tmp.c:32373: by v__gen__c__Gen_stmts_with_tmp_var
/tmp/v_0/v2.01HPNNKGWX7GVNSYE1BXAAAAH8.tmp.c:32279: by v__gen__c__Gen_stmts
/tmp/v_0/v2.01HPNNKGWX7GVNSYE1BXAAAAH8.tmp.c:40680: by v__gen__c__Gen_gen_fn_decl
/tmp/v_0/v2.01HPNNKGWX7GVNSYE1BXAAAAH8.tmp.c:40290: by v__gen__c__Gen_fn_decl
/tmp/v_0/v2.01HPNNKGWX7GVNSYE1BXAAAAH8.tmp.c:32563: by v__gen__c__Gen_stmt
/tmp/v_0/v2.01HPNNKGWX7GVNSYE1BXAAAAH8.tmp.c:32373: by v__gen__c__Gen_stmts_with_tmp_var
/tmp/v_0/v2.01HPNNKGWX7GVNSYE1BXAAAAH8.tmp.c:32279: by v__gen__c__Gen_stmts
/tmp/v_0/v2.01HPNNKGWX7GVNSYE1BXAAAAH8.tmp.c:31220: by v__gen__c__Gen_gen_file
/tmp/v_0/v2.01HPNNKGWX7GVNSYE1BXAAAAH8.tmp.c:30785: by v__gen__c__gen
/tmp/v_0/v2.01HPNNKGWX7GVNSYE1BXAAAAH8.tmp.c:53662: by v__builder__cbuilder__gen_c
/tmp/v_0/v2.01HPNNKGWX7GVNSYE1BXAAAAH8.tmp.c:53631: by v__builder__cbuilder__build_c
/tmp/v_0/v2.01HPNNKGWX7GVNSYE1BXAAAAH8.tmp.c:53621: by v__builder__cbuilder__compile_c
/tmp/v_0/v2.01HPNNKGWX7GVNSYE1BXAAAAH8.tmp.c:53527: by v__builder__Builder_rebuild
/tmp/v_0/v2.01HPNNKGWX7GVNSYE1BXAAAAH8.tmp.c:52632: by v__builder__compile
/tmp/v_0/v2.01HPNNKGWX7GVNSYE1BXAAAAH8.tmp.c:53962: by main__rebuild
/tmp/v_0/v2.01HPNNKGWX7GVNSYE1BXAAAAH8.tmp.c:53910: by main__main
/tmp/v_0/v2.01HPNNKGWX7GVNSYE1BXAAAAH8.tmp.c:54619: by main
Exited with error status 1
Please try again.

Ddiidev avatar Feb 15 '24 16:02 Ddiidev

I see no errors on my testing. Can you check again @Ddiidev ?

felipensp avatar Jun 15 '24 14:06 felipensp

...
data := Realtor{
	first_name: 'John',
	last_name:  'Doe',
	creci:      '12345678',
	cnpj:       '12345678901234',
	cpf:        '12345678901',
	phone:      '1234567890',
	real_state: RealState{
		name: 'Teste',
		cnpj: '12345678901234',
		address: Address{
			street:    'Teste',
			city:      'Teste',
			state:     'Teste',
			zip_code:  'Teste',
			proximity: 'Teste',
		},
	}
}

sql db {
	create table Address
	create table RealState
	create table Realtor
}!

sql db {
	insert data into Realtor
}!

dump(sql db {
	select from Realtor
}!)
NOTICE:  relation "Address" already exists, skipping
NOTICE:  relation "RealStates" already exists, skipping
NOTICE:  relation "Realtors" already exists, skipping
[teste.v:77] ast.SqlExpr: [Realtor{
    id: Option('5a359520-fd26-4aca-8f28-1361d1c0db23')
    first_name: 'John'
    last_name: 'Doe'
    creci: '12345678'
    cnpj: Option('12345678901234')
    cpf: Option('12345678901')
    phone: '1234567890'
    real_state: RealState{
        id: ''
        parent_id: 0
        name: ''
        cnpj: ''
        address: Address{
            id: 0
            create_at: ''
            update_at: ''
            street: ''
            city: ''
            state: ''
            zip_code: ''
            proximity: ''
        }
    }
}]

Now it's working perfectly, thank you and congratulations to those involved! 🥳

✅ Sqlite ✅ PostgreSql

Ddiidev avatar Jun 18 '24 11:06 Ddiidev

Thanks for a detailed report :)

medvednikov avatar Jun 18 '24 13:06 medvednikov