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

cgen: direct assigning string to ?string with -autofree #20937

Closed
MCausc78 opened this issue Mar 1, 2024 · 0 comments · Fixed by #21051
Closed

cgen: direct assigning string to ?string with -autofree #20937

MCausc78 opened this issue Mar 1, 2024 · 0 comments · Fixed by #21051
Assignees
Labels
Autofree Bugs/feature requests, that are related to -autofree. Bug This tag is applied to issues which reports bugs. Option Type Bugs/feature requests, that are related to `?Type`.

Comments

@MCausc78
Copy link
Contributor

MCausc78 commented Mar 1, 2024

Describe the bug

title

Reproduction Steps

struct Attr {
	name    string
	has_arg bool
	arg     string
}

fn main() {
	mut m := ?string(none)
	attrs := [Attr{'foo', false, 'bar'}, Attr{'baz', true, 'qux'}]
	for attr in attrs {
		if attr.name == 'baz' && attr.has_arg {
			m = attr.arg
		}
	}
	println(m)
}

run with -autofree

Expected Behavior

PS D:\Games\Proekti\V\interactions> v -autofree run test.v
Option('qux')

Current Behavior

PS D:\Games\Proekti\V\interactions> v -autofree test.v
==================
C:/Users/mclr/AppData/Local/Temp/v_0/test.01HQXW2JYMT0R4K06RTW1Q7TDN.tmp.c:6771: warning: implicit declaration of function 'tcc_backtrace'
C:/Users/mclr/AppData/Local/Temp/v_0/test.01HQXW2JYMT0R4K06RTW1Q7TDN.tmp.c:13028: error: cannot convert 'struct string' to 'struct _option_string'
...
==================
(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

PS D:\Games\Proekti\V\interactions> 

Possible Solution

No response

Additional Information/Context

generated C code with -autofree:

VV_LOCAL_SYMBOL void main__main(void) {
	_option_string m = (_option_string){ .state=2, .err=_const_none__, .data={EMPTY_STRUCT_INITIALIZATION} };
	Array_main__Attr attrs = new_array_from_c_array(2, 2, sizeof(main__Attr), _MOV((main__Attr[2]){((main__Attr){.name = /*clone1*/_SLIT("foo"),.has_arg = false,.arg = /*clone1*/_SLIT("bar"),}), ((main__Attr){.name = /*clone1*/_SLIT("baz"),.has_arg = true,.arg = /*clone1*/_SLIT("qux"),})}));
	for (int _t1 = 0; _t1 < attrs.len; ++_t1) {
		main__Attr attr = ((main__Attr*)attrs.data)[_t1];
		if (string__eq(attr.name, _SLIT("baz")) && attr.has_arg) {
			m =  string_clone_static(attr.arg);
		}
	}
	println(_option_string_str(m));
	array_free(&attrs); // autofreed var main false
}

Without:

VV_LOCAL_SYMBOL void main__main(void) {
	_option_string m = (_option_string){ .state=2, .err=_const_none__, .data={EMPTY_STRUCT_INITIALIZATION} };
	Array_main__Attr attrs = new_array_from_c_array(2, 2, sizeof(main__Attr), _MOV((main__Attr[2]){((main__Attr){.name = _SLIT("foo"),.has_arg = false,.arg = _SLIT("bar"),}), ((main__Attr){.name = _SLIT("baz"),.has_arg = true,.arg = _SLIT("qux"),})}));
	for (int _t1 = 0; _t1 < attrs.len; ++_t1) {
		main__Attr attr = ((main__Attr*)attrs.data)[_t1];
		if (string__eq(attr.name, _SLIT("baz")) && attr.has_arg) {
			_option_string _t2;
			_option_ok(&(string[]) { attr.arg }, (_option*)(&_t2), sizeof(string));
			m = _t2;
		}
	}
	println(_option_string_str(m));
}

V version

V 0.4.4 febc4a7

Environment details (OS name and version, etc.)

V full version: V 0.4.4 57a33c3.febc4a7
OS: windows, Microsoft Windows 11 Pro v22000 64-bit
Processor: 12 cpus, 64bit, little endian, 

getwd: D:\Games\Proekti\V\interactions
vexe: D:\Games\Proekti\V\v\v.exe
vexe mtime: 2024-03-01 20:41:30

vroot: OK, value: D:\Games\Proekti\V\v
VMODULES: OK, value: C:\Users\mclr\.vmodules
VTMP: OK, value: C:\Users\mclr\AppData\Local\Temp\v_0

Git version: git version 2.37.0.windows.1
Git vroot status: weekly.2024.07-80-gfebc4a7e
.git/config present: true

CC version:
thirdparty/tcc status: thirdparty-windows-amd64 a39eb79b

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.

@MCausc78 MCausc78 added the Bug This tag is applied to issues which reports bugs. label Mar 1, 2024
@felipensp felipensp added Option Type Bugs/feature requests, that are related to `?Type`. Autofree Bugs/feature requests, that are related to -autofree. labels Mar 3, 2024
@felipensp felipensp self-assigned this Mar 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Autofree Bugs/feature requests, that are related to -autofree. Bug This tag is applied to issues which reports bugs. Option Type Bugs/feature requests, that are related to `?Type`.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants