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: undeclared variable #19584

Open
SlashScreen opened this issue Oct 16, 2023 · 6 comments
Open

CGen: undeclared variable #19584

SlashScreen opened this issue Oct 16, 2023 · 6 comments
Labels
Bug This tag is applied to issues which reports bugs. Unit: cgen Bugs/feature requests, that are related to the default C generating backend.

Comments

@SlashScreen
Copy link
Contributor

SlashScreen commented Oct 16, 2023

Describe the bug

For an unknown reason, a defined local variable is marked as undefined by the c compiler.

Reproduction Steps

Unknown minimum reproduction. Running v run src on the project should cause the error, as it does so on windows and linux. The error seems around here.

Expected Behavior

The program compiles.

Current Behavior

==================
/tmp/v_1000/src.965835628491974123.tmp.c:15045: error: '_t19' undeclared
...
==================
(Use `v -cg` to print the entire error message)

builder error: 
==================
C error. This should never happen.

Possible Solution

No response

Additional Information/Context

The error appears here, I believe. the generated c code is as follows:

_result_main__Token _t19 = main__Parser_current(/*rec*/*p); // Defined here
    if (_t19.is_error) { // This is OK
        _result_usize _t20;
        memcpy(&_t20, &_t19, sizeof(_result));
        return _t20;
    }
    
         _t13 = _t16;
    } else if ( (*(main__Token*)_t19.data).tag != main__Symbol__l_bracket) { // _t19 here is "undeclared"
    } else {
        
        _t13 = (_option_usize){ .state=2, .err=_const_none__, .data={EMPTY_STRUCT_INITIALIZATION} };
    }

This is unexpected, as _t19 is declared above.

V version

V 0.4.2 25777bd

Environment details (OS name and version, etc.)

Ubuntu, Windows 11.

Note

You can vote for this issue using the 👍 reaction. More votes increase the issue's priority for developers.

Take into account that only the 👍 reaction counts as a vote.
Only reactions to the issue itself will be counted as votes, not comments.

@SlashScreen SlashScreen added the Bug This tag is applied to issues which reports bugs. label Oct 16, 2023
@ArtemkaKun ArtemkaKun added Unit: cgen Bugs/feature requests, that are related to the default C generating backend. Build V build error on any OS/CPU architecture. labels Oct 17, 2023
@SlashScreen
Copy link
Contributor Author

This appears to be a bug with how If statement chains are resolved. It's adding an extra }.

@felipensp
Copy link
Member

Can you try again with the newer V version?

@SlashScreen
Copy link
Contributor Author

@felipensp Yes, unfortunately.

@shove70
Copy link
Contributor

shove70 commented Nov 14, 2023

Maybe it's related to this:
#19782

v run .          // no error.
v run src        // has error.

@SlashScreen
Copy link
Contributor Author

@shove70 v test . yields same result.

@felipensp
Copy link
Member

Possible workaround:

ret_type := match p.current()!.tag {
		.ident {
			?usize(p.consume_ident()!)
		}
		.l_bracket { // Compiler error
			return error('Expected type name as return type')
		}
		else {
			?usize(none)
		}
	}

@felipensp felipensp removed the Build V build error on any OS/CPU architecture. label Aug 20, 2024
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. Unit: cgen Bugs/feature requests, that are related to the default C generating backend.
Projects
None yet
Development

No branches or pull requests

4 participants