Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C compile error in option field in a struct #23446

Open
penguindark opened this issue Jan 12, 2025 · 4 comments · May be fixed by #23459
Open

C compile error in option field in a struct #23446

penguindark opened this issue Jan 12, 2025 · 4 comments · May be fixed by #23459
Labels
Bug This tag is applied to issues which reports bugs. Status: Confirmed This bug has been confirmed to be valid by a contributor. Unit: cgen Bugs/feature requests, that are related to the default C generating backend. Unit: Compiler Bugs/feature requests, that are related to the V compiler in general.

Comments

@penguindark
Copy link
Member

penguindark commented Jan 12, 2025

Describe the bug

Using an option field with map in structs generate C compiler error

Reproduction Steps

compile:

// module comconf

import os

struct BatchCommonConfig {
	ingestor_db map[string]string
	working_db map[string]string
	remote_db map[string]string
	ready_db ?map[string]string
	mut: client_db ?map[string]string
}

fn main(){

	new_era_db_env := os.getenv_opt('NEW_ERA_DB_ENV') or {'local_container'}
	mut config := match new_era_db_env {
		'local_container' {BatchCommonConfig{

			ingestor_db: {
				'username': 'username_data',
				'password': 'psw_data',
				'hostname': 'postgres_wh',
				'database': 'ingestor_db_test',
				'port': '5432'
        	}

			working_db: {
				'username': 'username_data',
				'password': 'psw_data',
				'hostname': 'postgres_wh',
				'database': 'working_db_test',
				'port': '5432'
			}

			remote_db: {
				'username': 'username_data',
				'password': 'psw_data',
				'hostname': 'postgres_wh',
				'database': 'remote_db_test',
				'port': '5432'
			}
			client_db: {
				'test' : 'test'
			}

		}}
		'local' {BatchCommonConfig{

			ingestor_db: {
				'username': 'postgres',
				'password': 'postgres',
				'hostname': '127.0.0.1',
				'database': 'ingestor_db_test',
				'port': '5432'
        	}

			working_db: {
				'username': 'postgres',
				'password': 'postgres',
				'hostname': '127.0.0.1',
				'database': 'working_db_test',
				'port': '5432'
			}

			remote_db: {
				'username': 'postgres',
				'password': 'postgres',
				'hostname': '127.0.0.1',
				'database': 'remote_db_test',
				'port': '5432'
			}

			ready_db: {
				'username': 'postgres',
				'password': 'postgres',
				'hostname': '127.0.0.1',
				'database': 'acme_db_test',
				'port': '5432'
			}
			
			client_db: {
				'username': 'postgres',
				'password': 'postgres',
				'hostname': '127.0.0.1',
				'database': 'remote_db_test_remote',
				'port': '5432'
			}

		}}
		else {panic('what is the target db?')} 
	}

	b := 'print test ${config.client_db['test'] or {'test'}}' 
	println(b)

}

Expected Behavior

The print of the field or its option

Current Behavior

cc: C:/Users/dario/AppData/Local/Temp/v_0/marco.01JHDMTGSCW33PCJ7ETMJQF2CH.tmp.c:4628: warning: implicit declaration of function 'tcc_backtrace'
cc: C:/Users/dario/AppData/Local/Temp/v_0/marco.01JHDMTGSCW33PCJ7ETMJQF2CH.tmp.c:7722: error: cannot convert 'struct _option_Map_string_string' to 'int'
... (the original output was 3 lines long, and was truncated to 2 lines)
=================================================================
(You can pass `-cg`, or `-show-c-output` as well, to print all the C error messages).
builder error:
==================
C error found. It should never happen, when compiling pure V code.
This is a V compiler bug, please report it using `v bug file.v`,
or goto https://github.com/vlang/v/issues/new/choose .
You can also use #help on Discord: https://discord.gg/vlang .

Possible Solution

No response

Additional Information/Context

No response

V version

V 0.4.9 d2b30df

Environment details (OS name and version, etc.)

Ubuntu 22, Windows 10

Note

You can use the 👍 reaction to increase the issue's priority for developers.

Please note that only the 👍 reaction to the issue itself counts as a vote.
Other reactions and those to comments will not be taken into account.

@penguindark penguindark added Bug This tag is applied to issues which reports bugs. Compiler Panic The V compiler itself paniced. It should not, it should produce nice and readable errors instead. labels Jan 12, 2025
Copy link

Connected to Huly®: V_0.6-21876

@spytheman spytheman added Unit: Compiler Bugs/feature requests, that are related to the V compiler in general. Unit: cgen Bugs/feature requests, that are related to the default C generating backend. Status: Confirmed This bug has been confirmed to be valid by a contributor. and removed Compiler Panic The V compiler itself paniced. It should not, it should produce nice and readable errors instead. labels Jan 12, 2025
@spytheman
Copy link
Member

A smaller reproduction is:

struct BatchCommonConfig {
    mut:
    client_db ?map[string]string
}

fn main(){
    mut config := BatchCommonConfig{}
    b := 'print test ${config.client_db['test'] or {'test'}}'
    println(b)
}

@spytheman
Copy link
Member

Using b := 'print test ${config.client_db?['test'] or {'test'}}' makes it compile cleanly.

@spytheman
Copy link
Member

An even smaller example leading to the same cgen issue is:

struct Abc {
mut:
    m ?map[string]string
}
fn main(){
    mut x := Abc{}
    v := x.m['test'] or {'test'}
    println(v)
}

@Delta456 Delta456 linked a pull request Jan 13, 2025 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug This tag is applied to issues which reports bugs. Status: Confirmed This bug has been confirmed to be valid by a contributor. Unit: cgen Bugs/feature requests, that are related to the default C generating backend. Unit: Compiler Bugs/feature requests, that are related to the V compiler in general.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants