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.69k stars 2.16k forks source link

Returing custom error without `IError()` gives C error. This should never happen. #16921

Open yochem opened 1 year ago

yochem commented 1 year ago

Describe the bug

I'm currently working on a pathlib library for v: #16782. I wanted to implement a wrapper function around os.open, and raise a custom error if it didn't work, like so:

pub fn (p Path) open(mode string, options ...int) !os.File {
    return os.open_file(p.str(), mode, ...options) or {
        PathError{p, 200, 'cannot open "${p}"', 'open'}
    }
}

I started the v repl using a freshly built v from git (of my fork, with the only difference being that it has the pathlib module in vlib/):

$ ./v
 \   \  /   /  |  Welcome to the V REPL (for help with V itself, type  exit , then run  v help ).
  \   \/   /   |  Note: the REPL is highly experimental. For best V experience, use a text editor,
   \      /    |  save your code in a  main.v  file and execute:  v run main.v
    \    /     |  V 0.3.2 f959fbb . Use  list  to see the accumulated program so far.
     \__/      |  Use Ctrl-C or  exit  to exit, or  help  to see other available commands.

>>> import pathlib
==================
   ^~~~~~~~~~~~~~~~~~
/tmp/v_501/.noprefix.01GPB9E584ERTRQ24V00NR3H9E.vrepl_temp.7378359400266354256.tmp.c:23354:25: error: assigning to 'os__File' (aka 'struct os__File') from incompatible type 'pathlib__PathError' (aka 'struct pathlib__PathError')
                *(os__File*) _t2.data = ((pathlib__PathError){.path = p,.code = 200,.msg =  str_intp(2, _MOV((StrIntpData[]){{_SLIT("cannot open \""), /*115 &pathlib.Path*/0xfe10, {.d_s = pathlib__Path_str(p)}}, {_SLIT("\""), 0, { .d_c = 0 }}})),.func = _SLIT("open"),});
                                      ^ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/tmp/v_501/.noprefix.01GPB9E584ERTRQ24V00NR3H9E.vrepl_temp.7378359400266354256.tmp.c:23450:4: warning: expression result unused [-Wunused-value]
  (*(os__File*)_t1.data);
   ^~~~~~~~~~~~~~~~~~~~
9 warnings and 1 error generated.
...
==================
(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
>>>

Here's the C output: https://gist.github.com/yochem/60d58b76372191d143bdf6d4683a6b68.

Expected Behavior

Not crashing

Current Behavior

Crashing

Reproduction Steps

$ git clone https://github.com/yochem/v
$ git checkout pathlib
$ make
$ ./v
>>> import pathlib

Possible Solution

No response

Additional Information/Context

No response

V version

V 0.3.2 f959fbb

Environment details (OS name and version, etc.)

OS: macos, macOS, 13.0.1, 22A400 Processor: 4 cpus, 64bit, little endian, Intel(R) Core(TM) i5-8210Y CPU @ 1.60GHz CC version: Apple clang version 14.0.0 (clang-1400.0.29.202)

getwd: /Users/yochem/Documents/nv vmodules: /Users/yochem/.vmodules vroot: /Users/yochem/Documents/nv vexe: /Users/yochem/Documents/nv/v vexe mtime: 2023-01-09 12:52:32 is vroot writable: true is vmodules writable: false V full version: V 0.3.2 2ec6e2b.f959fbb

Git version: git version 2.39.0 Git vroot status: weekly.2022.52-14-gf959fbbc (72 commit(s) behind V master) .git/config present: true thirdparty/tcc status: thirdparty-macos-amd64 46662e20

yochem commented 1 year ago

Oops, seems that wrapping the custom errors in IError() solves the problem 🙊

JalonSolov commented 1 year ago

Ah, good catch then. V should still give a nicer error message, but at least you already know how to avoid it. :-)

yochem commented 1 year ago

A better error message would indeed be nice ;)

JalonSolov commented 1 year ago

Let's leave it open until that better error message comes along.