v icon indicating copy to clipboard operation
v copied to clipboard

constant and results error

Open Eliyaan opened this issue 1 year ago • 4 comments

Describe the bug

When a constant is used to initialize another constant using a function that returns a result (and propagating the result), it outputs an error.

Reproduction Steps

const constant = 5
const other = init_const(constant)!

fn main() {}

fn init_const(c int) !int {
    return c
}

Expected Behavior

To not have an error

Current Behavior

❯ v run .
main.v:2:35: error: to propagate the Result call, `ArrayFlags__static__zero` must return a Result
    1 | const constant = 5
    2 | const other = init_const(constant)!
      |                                   ^
    3 |
    4 | fn main() {}
Details: main.v:14:36: details: prepend ! before the declaration of the return type of `ArrayFlags__static__zero`
main.v:2:35: error: to propagate the call, `ArrayFlags__static__zero` must return a Result type
    1 | const constant = 5
    2 | const other = init_const(constant)!
      |                                   ^
    3 |
    4 | fn main() {}
Details: main.v:14:36: details: prepend ! before the declaration of the return type of `ArrayFlags__static__zero`

Possible Solution

No response

Additional Information/Context

No response

V version

V 0.4.6 294f7e4.a4afcba

Environment details (OS name and version, etc.)

V full version: V 0.4.6 294f7e4.a4afcba OS: linux, Linux version 6.7.11-200.fc39.x86_64 (mockbuild@bdb8d24f206645e2bda44c1cf867612d) (gcc (GCC) 13.2.1 20240316 (Red Hat 13.2.1-7), GNU ld version 2.40-14.fc39) #1 SMP PREEMPT_DYNAMIC Wed Mar 27 16:50:39 UTC 2024

[!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.

Eliyaan avatar May 30 '24 14:05 Eliyaan

I'd like to have this too. Funnily it works in some scenarios. Currently, spelling it out into or { panic(err) } is the reliable way.

ttytm avatar May 30 '24 21:05 ttytm

@spytheman It looks related to the p.codegen() scoping stuff.

felipensp avatar May 31 '24 13:05 felipensp

Well, part of the problem is that you can't propagate from main - it is just an exit.

In the case of const, it's even before main, in _init().

JalonSolov avatar May 31 '24 13:05 JalonSolov

Well, part of the problem is that you can't propagate from main - it is just an exit.

In the case of const, it's even before main, in _init().

It's right that the initialization of consts is different than in main. Still propagating from main (or without main in scripting mode) is the same as or { panic(err) }. So it should work for consts too. In my case, the scenarios where ! worked for consts, I think, were influenced by the sorting of the consts and if they were in different files of the same module. Unfortunately, I didn't make a report when running into this for time reasons.

ttytm avatar May 31 '24 13:05 ttytm