Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C error on string concat with comptime and generics #23341

Closed
islonely opened this issue Jan 2, 2025 · 0 comments · Fixed by #23344
Closed

C error on string concat with comptime and generics #23341

islonely opened this issue Jan 2, 2025 · 0 comments · Fixed by #23344
Assignees
Labels
Bug This tag is applied to issues which reports bugs. Comptime Features processed during compile time, like $if, $for, $env etc Unit: Checker Bugs/feature requests, that are related to the type checker.

Comments

@islonely
Copy link
Contributor

islonely commented Jan 2, 2025

V doctor:

V full version: V 0.4.9 4225a34.7cf77fb
OS: macos, macOS, 15.1.1, 24B91
Processor: 8 cpus, 64bit, little endian, Apple M3

getwd: /Users/adamoates/.vmodules/islonely/hex
vexe: /Users/adamoates/v/v
vexe mtime: 2025-01-02 04:35:14

vroot: OK, value: /Users/adamoates/v
VMODULES: OK, value: /Users/adamoates/.vmodules
VTMP: OK, value: /tmp/v_501

Git version: git version 2.39.5 (Apple Git-154)
Git vroot status: weekly.2024.53-10-g7cf77fbd
.git/config present: true

CC version: Apple clang version 16.0.0 (clang-1600.0.26.4)
emcc version: N/A
thirdparty/tcc status: thirdparty-macos-arm64 713692d4

What did you do?
./v -g -o vdbg cmd/v && ./vdbg /Users/adamoates/Documents/test.v && /Users/adamoates/Documents/test

fn main() {
	println(enc(Test{}))
}
enum Flag {
	usa_old_glory
	all_other_bad_excuses_for_a_flag
}

struct Test {
	is_foo bool
	name [5]u8
}

fn enc[T](item T) string {
	$if T is $int {
		len := match typeof(item).name {
			'i8', 'u8' { u8(2) }
			'i16', 'u16' { 4 }
			'int', 'u32', 'i32' { 8 }
			'i64', 'u64' { 16 }
			else { return '' }
		}
		return u64_to_hex(item, len)
	} $else $if T is $array {
		mut hex := ''
		for val in item {
			hex += enc(val)
		}
		return hex
	} $else $if T is $struct {
		mut hex := ''
		$for field in T.fields {
			hex += enc(item.$(field.name)) + '_'
		}
		return hex
	} $else {
		if typeof(item).name == 'bool' {
			return enc(int(item))
		}
		$if debug {
			println('cannot encode ${T}(s)')
		}
		return ''
	}
}

@[direct_array_access; inline]
fn u64_to_hex(nn u64, len u8) string {
	mut n := nn
	mut buf := [17]u8{}
	buf[len] = 0
	mut i := 0
	for i = len - 1; i >= 0; i-- {
		d := u8(n & 0xF)
		buf[i] = if d < 10 { d + `0` } else { d + 87 }
		n = n >> 4
	}
	return unsafe { tos(memdup(&buf[0], len + 1), len) }
}

What did you see instead?

================== C compilation error (from cc): ==============
cc: /tmp/v_501/test.01JGJX16A7PGJ1TBVGE9ANTRWG.tmp.c:5852:42: error: call to undeclared function 'main__enc_T_Array_fixed_u8_5'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
cc:  5852 |                                 hex = string__plus(hex, string__plus(main__enc_T_Array_fixed_u8_5(item.name), _SLIT("_")));
cc:       |                                                                      ^
cc: /tmp/v_501/test.01JGJX16A7PGJ1TBVGE9ANTRWG.tmp.c:5852:42: error: passing 'int' to parameter of incompatible type 'string' (aka 'struct string')
cc:  5852 |                                 hex = string__plus(hex, string__plus(main__enc_T_Array_fixed_u8_5(item.name), _SLIT("_")));
cc:       |                                                                      ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
cc: /tmp/v_501/test.01JGJX16A7PGJ1TBVGE9ANTRWG.tmp.c:4923:44: note: passing argument to parameter 's' here
cc:  4923 | VV_LOCAL_SYMBOL string string__plus(string s, string a) {
cc:       |                                            ^
cc: 2 errors generated.
================================================================
(You can pass `-cg`, or `-show-c-output` as well, to print all the C error messages).
builder error: 
==================
C error found. It should never happen, when compiling pure V code.
This is a V compiler bug, please report it using `v bug file.v`,
or goto https://github.com/vlang/v/issues/new/choose .
You can also use #help on Discord: https://discord.gg/vlang .

What did you expect to see?

Expected code to compile and output a line of zeros.

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.

Huly®: V_0.6-21772

@felipensp felipensp self-assigned this Jan 2, 2025
@felipensp felipensp added Bug This tag is applied to issues which reports bugs. Comptime Features processed during compile time, like $if, $for, $env etc Unit: Checker Bugs/feature requests, that are related to the type checker. labels Jan 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug This tag is applied to issues which reports bugs. Comptime Features processed during compile time, like $if, $for, $env etc Unit: Checker Bugs/feature requests, that are related to the type checker.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants