v icon indicating copy to clipboard operation
v copied to clipboard

Code generation issue

Open thomas-mangin opened this issue 2 years ago • 3 comments

Describe the bug

The compiler did not warn me of a programming mistake or generated some invalid code

Expected Behavior

The v compiler not crashing

Current Behavior

The compiler crashed, reporting:

env VFLAGS="" v -cg run bug.v
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

/tmp/v_501/bug.9486755840243341581.tmp.c:2203:4: warning: expression result unused [-Wunused-value]
  (*(int*)_t1.data);
   ^~~~~~~~~~~~~~~
/tmp/v_501/bug.9486755840243341581.tmp.c:12277:27: error: passing 'main__Type *' (aka 'struct main__Type *') to parameter of incompatible type 'main__Type' (aka 'struct main__Type'); dereference with *
        if (main__Type_struct_eq(t, 94 /* main.Type */)) {
                                 ^
                                 *
/tmp/v_501/bug.9486755840243341581.tmp.c:2101:45: note: passing argument to parameter 'a' here
static bool main__Type_struct_eq(main__Type a, main__Type b) {
                                            ^
1 warning and 1 error generated.

Reproduction Steps

compile this code

struct Type {
mut:
        something string
}

type Union = Type

fn (mut t Type) returns() Union {
    match t {
        Type {
                        t.something = "else"
        }
        else {
        }
    }

        return t
}

fn main() {
        mut t := &Type{ something: "something"}
        t.returns()
}

or this one which also fails but slightly differently

struct Type {
}

fn (t Type) returns(){
    match t {
        Type {
        }
        else {
        }
    }
}

fn main() {
        mut t := &Type{}
        t.returns()
}

Possible Solution

No response

Additional Information/Context

No response

V version

V 0.3.3 00aecf9

Environment details (OS name and version, etc.)

OS: macos, macOS, 13.2.1, 22D68
Processor: 10 cpus, 64bit, little endian, Apple M1 Max
CC version: Apple clang version 14.0.0 (clang-1400.0.29.202)

getwd: /Users/thomas/Source/github.com/ze-core/network/main
vmodules: /Users/thomas/.vmodules
vroot: /Users/thomas/Unix/v
vexe: /Users/thomas/Unix/v/v
vexe mtime: 2023-02-25 23:50:11
is vroot writable: true
is vmodules writable: true
V full version: V 0.3.3 4c13a4c.00aecf9
env VFLAGS: "-cc tcc -no-retry-compilation -d dynamic_boehm -g -stats -d panics_break_into_debugger"

Git version: git version 2.39.2
Git vroot status: weekly.2023.08-28-g00aecf92
.git/config present: true
thirdparty/tcc status: thirdparty-macos-arm64 a668e5a0

thomas-mangin avatar Feb 25 '23 23:02 thomas-mangin

I think the usage is error, we can match sum type or interface type. we can do like the following:

struct Type {
mut:
	something string
}

type Union = Type

fn (mut t Type) returns() Union {
	zero := Type{}
	match t {
		zero {
			t.something = 'else'
		}
		else {}
	}

	return t
}

fn main() {
	mut t := &Type{
		something: 'something'
	}
	t.returns()
}

yuyi98 avatar Feb 26 '23 05:02 yuyi98

Thank you @yuyi98 for taking some time to look into the issue, it is appreciated.

I did ultimately realise that the V code was invalid, but not until looking at the generated C code - which took me some time :-)

I am reporting that the compiler let me shoot myself in the foot without blinking.

thomas-mangin avatar Feb 26 '23 12:02 thomas-mangin

Yep... it should've given a nice error message, instead of pawning if off on the C compiler.

JalonSolov avatar Feb 26 '23 14:02 JalonSolov