v icon indicating copy to clipboard operation
v copied to clipboard

Type alias cannot be a reference

Open duarteroso opened this issue 3 years ago • 2 comments

V version: 0.2.2 OS: Linux

In this example I have two types that are equal:

  1. &char
  2. ALcharptr

When translated into C, both types (although equal) will be passed to char_vstring differently:

  1. char_vstring(s)
  2. char_vstring(&s)

Full exampled here(zip).

V snippet

type ALcharptr = &char
fn C.return_text_one() ALcharptr
fn C.return_text_two() &char

fn main() {
	println('type &char\talias ALcharptr\t:\t${get_one()}')
	println('type &char\talias none\t:\t${get_two()}')
}

fn get_one() string {
	s := C.return_text_one()
	return unsafe { cstring_to_vstring(s) }
}

fn get_two() string {
	s := C.return_text_two()
	return unsafe { cstring_to_vstring(s) }
}

C snippet

void main__main(void) {
	println(_STR("type &char\talias ALcharptr\t:\t%.*s", 1, main__get_one()));
	println(_STR("type &char\talias none\t:\t%.*s", 1, main__get_two()));
}

string main__get_one(void) {
	main__ALcharptr s = return_text_one();
	 string _t101 = char_vstring(&s); // <- Nein!
	return _t101;
}

string main__get_two(void) {
	char* s = return_text_two();
	 string _t102 = char_vstring(s);
	return _t102;
}

Output

type &char      alias ALcharptr :       �lf
type &char      alias none      :       text

duarteroso avatar Apr 11 '21 14:04 duarteroso

Still an issue.

medvednikov avatar Jul 22 '22 21:07 medvednikov

Cgen error on latest v:

PS D:\Games\Proekti\V\interactions> type test2.v  
type ALcharptr = &char

fn C.return_text_one() ALcharptr
fn C.return_text_two() &char

fn main() {
        println('type &char\talias ALcharptr\t:\t${get_one()}')
        println('type &char\talias none\t:\t${get_two()}')
}

fn get_one() string {
        s := C.return_text_one()
        return unsafe { cstring_to_vstring(s) }
}

fn get_two() string {
        s := C.return_text_two()
        return unsafe { cstring_to_vstring(s) }
}
PS D:\Games\Proekti\V\interactions> v version
V 0.4.4 2a68e2b
PS D:\Games\Proekti\V\interactions> v run test2.v 
==================
C:/Users/mclr/AppData/Local/Temp/v_0/test2.01HN390CQQ1AJEKT8Z3C0PW484.tmp.c:13009: warning: cast between pointer and integer of different size
tcc: error: undefined symbol 'return_text_one'
tcc: error: undefined symbol 'return_text_two'
...
==================
(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

MCausc78 avatar Jan 26 '24 16:01 MCausc78