Skip to content

Unable to generate constants introduced in error handling #24040

@CryingN

Description

@CryingN

Describe the bug

In the newer versions, when constants are introduced using import and if they are only imported for error handling, the variables will not be defined after compilation to C.

Reproduction Steps

there is a simple example. When using the following code, it fails to compile properly:

import db.sqlite
import math

@[table: 'personal']
pub struct Personal {
        pid             int                 @[primary; sql:serial]
}


pub fn connect_db() sqlite.DB {
    mut db := sqlite.connect('./data.db')  or {
        println('Error: sqlite调用错误')
        panic(err)
    }
    return db
}


pub fn create_db(db sqlite.DB) {
    sql db {
        create table Personal
    } or {
        println(math.pi)
    }
}

fn main() {
    mut db := connect_db()
    create_db(db)
}

Expected Behavior

Under normal circumstances, it should compile successfully.

Current Behavior

return:

================== C compilation error (from tcc): ==============
cc: /tmp/v_1000/sql_db.01JQ667HSZ6YXR64C12Q540344.tmp.c:10105: error: '_const_math__pi' undeclared
=================================================================
(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 .

when test.v to test.c, I can't find define _const_math__pi, but in line: 10105, there is:

[root_cn@archlinux sql_db]$ cat test.c | head -n 10107 | tail -n +10103
        if (_t2.is_error) {
                IError err = _t2.err;
                println(float_literal_str(_const_math__pi));
        ;
        }

Possible Solution

No response

Additional Information/Context

No response

V version

V 0.4.10 54c10ec in archlinux, and V 0.4.10 9b1937a in windows

Environment details (OS name and version, etc.)

linux and windows

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugThis tag is applied to issues which reports bugs.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions