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

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Array.index function genereated for array of sumtype references produces C error.

islonely opened this issue · comments

V version: V 0.2.4 223b96a.1fc9e1a
OS: linux, "Manjaro Linux"

What did you do?

struct Element {
	AbstractNode
mut:
	name       string
	value      string
	attributes []&Attribute
}

struct Attribute {
	AbstractNode
mut:
	name  string
	value string
}

pub type Node = Attribute | Element

struct AbstractNode {
pub mut:
	child_nodes []&Node
}

pub fn (mut n AbstractNode) append_child(child &Node) {
	n.child_nodes << child
}

pub fn (n &AbstractNode) has_child(child &Node) int {
	return n.child_nodes.index(child)
}

fn main() {
	mut parent := &Element{
		name: 'parent'
	}
	mut child := &Element{
		name: 'child'
	}
	parent.append_child(child)

	println(parent.has_child(child))
	dump(parent)
}

What did you expect to see?
Expected code to compile and the has_child function to return an integer.

What did you see instead?
The code doesn't compile because the generated C code is invalid.

/usr/lib64/crt1.o: error: Invalid relocation entry [15] '.rela.debug_info' @ 000004f5
tcc: error: file 'crt1.o' not found
/tmp/v_1000/test.9025346761131596870.tmp.c:2213: error: cannot convert 'struct main__Node *' to 'struct main__Node'
static int Array_dom__Node_ptr_index(Array_dom__Node_ptr a, dom__Node* v) {
	dom__Node** pelem = a.data;
	for (int i = 0; i < a.len; ++i, ++pelem) {
		if (dom__Node_sumtype_eq(*pelem, v)) {
			return i;
		}
	}
	return -1;
}
ezoic increase your site revenue