v icon indicating copy to clipboard operation
v copied to clipboard

cgen: `-autofree` fails

Open malisipi opened this issue 2 years ago • 2 comments

Describe the bug

The code can work without -autofree but if you try to compile with -autofree, your code will not be compiled.

import strconv

fn color_code_to_rgb(color string) []int {
	clr := color.replace('#', '')
	return [int(strconv.parse_int(clr[0..2], 16, 0) or { return [0, 0, 0] }),
		int(strconv.parse_int(clr[2..4], 16, 0) or { return [0, 0, 0] }),
		int(strconv.parse_int(clr[4..6],
			16, 0) or { return [0, 0, 0] })]
}

println(color_code_to_rgb('#abcdef'))

Expected Behavior

Works fine and writes the output to console

[171, 205, 239]

Current Behavior

Compiler fails with

[malisipi@malisipi-20351 ~]$ v -cg -autofree free.v 
/tmp/v_1000/free.11594424565698986927.tmp.c:12879: error: cannot convert 'typedef struct string' to 'int'
builder error: 
==================
C error. This should never happen.

Reproduction Steps

Compile the code with v -autofree file_name.v

Possible Solution

No response

Additional Information/Context

No response

V version

V 0.3.3 0a8a0fd

Environment details (OS name and version, etc.)

V full version: V 0.3.3 3197ec1.0a8a0fd OS: linux, "Garuda Linux" Processor: 4 cpus, 64bit, little endian, Intel(R) Core(TM) i5-4210U CPU @ 1.70GHz

getwd: /home/malisipi vexe: /home/malisipi/Programlar/v/v vexe mtime: 2023-03-25 13:43:23

vroot: OK, value: /home/malisipi/Programlar/v VMODULES: OK, value: /home/malisipi/.vmodules VTMP: OK, value: /tmp/v_1000

Git version: git version 2.39.2 Git vroot status: weekly.2023.05-256-g0a8a0fdb-dirty .git/config present: true

CC version: cc (GCC) 12.2.1 20230201 thirdparty/tcc status: thirdparty-linux-amd64 12f392c3

malisipi avatar Mar 25 '23 14:03 malisipi

Adding -g points to the line with the first call to strconv:

$ v -autofree -g run free.v
/tmp/v_1000/../../../../../../home/jalon/free.v:5: error: cannot convert 'typedef struct string' to 'int'
builder error: 
==================
C error. This should never happen.
...

JalonSolov avatar Mar 25 '23 14:03 JalonSolov

VV_LOCAL_SYMBOL Array_int main__color_code_to_rgb(string color) {
	string clr = string_replace(color, _SLIT("#"), _SLIT(""));
string _arg_expr_strconv_parse_int_1_111 = string_substr(clr, 0, 2);// new af2 pre/* inserted before */
		_result_i64 _t2 = strconv__parse_int(/*af arg*/_arg_expr_strconv_parse_int_1_111, 16, 0);
	if (_t2.is_error) {
		IError err = _t2.err;
		Array_int _t3 = new_array_from_c_array_noscan(3, 3,string _arg_expr_strconv_parse_int_1_179 = string_substr(clr, 2, 4);// new af2 pre/* inserted before */
		_result_i64 _t4 = strconv__parse_int(/*af arg*/_arg_expr_strconv_parse_int_1_179, 16, 0);
	if (_t4.is_error) {
		IError err = _t4.err;
		Array_int _t5 = new_array_from_c_array_noscan(3, 3, sizeof(int), _MOV((int[3]){0, 0, 0}));
			string_free(&_arg_expr_strconv_parsstring _arg_expr_strconv_parse_int_1_247 = string_substr(clr, 4, 6);// new af2 pre/* inserted before */
		_result_i64 _t6 = strconv__parse_int(/*af arg*/_arg_expr_strconv_parse_int_1_247, 16, 0);
	if (_t6.is_error) {
		IError err = _t6.err;
		Array_int _t7 = new_array_from_c_array_noscan(3, 3, sizeof(int), _MOV((int[3]){0, 0, 0}));
			string_free(&_arg_expr_strconv_parse_int_1_179); // autofreed var main false
	string_free(&_arg_expr_strconv_parse_int_1_111); // autofreed var main false
	string_free(&clr); // autofreed var main false
return _t7;
	}
	
 e_int_1_111); // autofreed var main false
	string_free(&clr); // autofreed var main false
return _t5;
	}
	
  sizeof(int), _MOV((int[3]){0, 0, 0}));
			string_free(&clr); // autofreed var main false
return _t3;
	}
	
 	Array_int _t1 = new_array_from_c_array_noscan(3, 3, sizeof(int), _MOV((int[3]){((int)( (*(i64*)_t2.data))), ((int)( (*(i64*)_t4.data))), ((int)( (*(i64*)_t6.data)))}));
		string_free(&_arg_expr_strconv_parse_int_1_247); // autofreed var main false
	string_free(&_arg_expr_strconv_parse_int_1_179); // autofreed var main false
	string_free(&_arg_expr_strconv_parse_int_1_111); // autofreed var main false
	string_free(&clr); // autofreed var main false
return _t1;
	string_free(&_arg_expr_strconv_parse_int_1_247); // autofreed var main false
	string_free(&_arg_expr_strconv_parse_int_1_179); // autofreed var main false
	string_free(&_arg_expr_strconv_parse_int_1_111); // autofreed var main false
	string_free(&clr); // autofreed var main false
}

The C code that was generated by V. I'm not understand correctly C code since I don't know cgen how works. But it is clearly the code has missing characters (like ), }) and wrong usage of new_array_from_c_array_noscan (at 7. line)

malisipi avatar Mar 25 '23 19:03 malisipi