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: error when using or { } block in struct field init #11119

Open
larpon opened this issue Aug 9, 2021 · 2 comments
Open

cgen: error when using or { } block in struct field init #11119

larpon opened this issue Aug 9, 2021 · 2 comments
Labels
Bug This tag is applied to issues which reports bugs. Status: Confirmed This bug has been confirmed to be valid by a contributor. Unit: Compiler Bugs/feature requests, that are related to the V compiler in general.

Comments

@larpon
Copy link
Contributor

larpon commented Aug 9, 2021

As title states

struct RegexCache {
mut:
    tag_script_start    regex.RE = regex.regex_opt(r'^script.*>') or { panic(err) } // <- This gives some trouble in some cases
}

test.v

import regex

struct RegexCache {
mut:
    tag_script_start    regex.RE = regex.regex_opt(r'^script.*>') or { panic(err) }
}

pub struct Tokenizer {
mut:
    regex_cache RegexCache = RegexCache{}
}

fn new_parser() &Parser {
    mut parser := &Parser{}
    return parser
}

pub struct Parser {
mut:
    tnzr        Tokenizer
}

fn main() {
    p := new_parser()
    println(p)
}

v run test.v

/dev/shm/v/run.7054598677465549469.tmp.c:12749: warning: implicit declaration of function '_t'
/dev/shm/v/run.7054598677465549469.tmp.c:12749: error: ')' expected (got "{")

C code around the issue:

VV_LOCAL_SYMBOL main__Parser* main__new_parser(void) {
	Option_regex__RE _t1 = regex__regex_opt(_SLIT("^script.*>"));
	if (_t(main__Tokenizer){.regex_cache = 1.state != 0) { /*or block*/ 
		IError err = _t1.err;
		_v_panic(IError_str(err));
		VUNREACHABLE();
	;
	}
	
 	main__Parser* parser = (main__Parser*)memdup(&(main__Parser){.tnzr = (main__RegexCache){.tag_script_start =  (*(regex__RE*)_t1.data),},},}, sizeof(main__Parser));
	main__Parser* _t2 = parser;
	return _t2;
}

image

@larpon larpon added Bug This tag is applied to issues which reports bugs. Unit: Compiler Bugs/feature requests, that are related to the V compiler in general. labels Aug 9, 2021
@JalonSolov
Copy link
Contributor

Strangely enough, this makes it work...

Change

pub struct Parser {
mut:
    tnzr        Tokenizer
}

to just

pub struct Parser {
    Tokenizer
}

@JalonSolov JalonSolov added the Status: Confirmed This bug has been confirmed to be valid by a contributor. label Sep 5, 2021
@larpon
Copy link
Contributor Author

larpon commented Sep 5, 2021

Cool - I can't use embedding instead unfortunately 🙂

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. Status: Confirmed This bug has been confirmed to be valid by a contributor. Unit: Compiler Bugs/feature requests, that are related to the V compiler in general.
Projects
None yet
Development

No branches or pull requests

2 participants