Skip to content

cgen: ensure variable names do not conflict with builtin methods - #25178

Merged
spytheman merged 1 commit into
vlang:masterfrom
dy-tea:master
Sep 4, 2025
Merged

spytheman merged 1 commit into
vlang:masterfrom
dy-tea:master

Conversation

@dy-tea

@dy-tea dy-tea commented Aug 27, 2025

Copy link
Copy Markdown
Member

See #25063.

This will probably cover most methods. Might be better to have a map of method names or something? Not sure how significant the performance hit of doing these checks is.

@huly-for-github

Copy link
Copy Markdown

Connected to Huly®: V_0.6-24208

@spytheman

Copy link
Copy Markdown
Contributor

Not sure how significant the performance hit of doing these checks is.

Run ./v run .github/workflows/compare_pr_to_master.v to find out.

@dy-tea

dy-tea commented Aug 29, 2025

Copy link
Copy Markdown
Member Author

Looks like at worst about 30% slower. Not exactly amazing. I will think about a more efficient way to do these checks.

@gechandesu

Copy link
Copy Markdown
Contributor

Just as an idea. Can we prefix the builtin methods to avoid conflicts instead of doing checks? It shouldn't have such a performance impact, but the side effects are unknown

@dy-tea

dy-tea commented Aug 29, 2025 •

Copy link
Copy Markdown
Member Author

Technically you could take the approach of prefixing built-in methods with something and output an error if the user tries naming a variable with that prefix.
That would prevent name collisions entirely with little performance hit.

@dy-tea

dy-tea commented Aug 29, 2025

Copy link
Copy Markdown
Member Author

New impl is at worst around 4% slower than master, at lot better than before.

@Delta456

Copy link
Copy Markdown
Member

You would need to cover a lot more cases than you think 🥲

@dy-tea

dy-tea commented Aug 31, 2025 •

Copy link
Copy Markdown
Member Author

Indeed there are a lot more cases of this than I expected (let me know if I am missing any obvious ones). Notably I added a lot more string, array and map methods. The second check I added doesn't appear to significantly affect performance over the one I had previously but there might be a better way to combine the checks?
Will look at it more tomorrow if I have time.

@dy-tea

dy-tea commented Sep 2, 2025

Copy link
Copy Markdown
Member Author

My newest implementation seems to have basically no performance impact. I'm covering a lot more cases (although likely still missing a few).

@dy-tea

dy-tea commented Sep 2, 2025

Copy link
Copy Markdown
Member Author

Noticed that I broke autofree so I added handling for that too. Let me know if I'm missing testcases, otherwise the functionality is there.

@dy-tea
dy-tea marked this pull request as ready for review September 2, 2025 15:02
@spytheman

spytheman commented Sep 3, 2025 •

Copy link
Copy Markdown
Contributor

The code from the issue #25063 , still leads to a cgen error 🤔.

#1 05:40:19 ^ master ~/v>git switch -
Switched to branch 'pr_25178'
#0 05:40:23 ^ pr_25178 ~/v>./v self
V self compiling ...
V built successfully as executable "v".
#0 05:40:27 ^ pr_25178 ~/v>./v vlib/v/tests/clash_var_fn_name_test.v
#0 05:40:37 ^ pr_25178 ~/v>cat /home/delian/code/misc/2025_09_03__05/xx.v
fn main() {
        mut r := rune(0)
        rune_bytes := r.bytes()
        _ = rune_bytes
}

#0 05:40:44 ^ pr_25178 ~/v>./v /home/delian/code/misc/2025_09_03__05/xx.v
================== C compilation error (from tcc): ==============
cc: /tmp/v_1000/xx.01K46Q5KDGB27TBYVVGWBQ5RKA.tmp.c:1544: error: '{' expected (got ";")
=================================================================
(You can pass `-cg`, or `-show-c-output` as well, to print all the C error messages).
#1 05:40:51 ^ pr_25178 ~/v>./v -cc clang-18 /home/delian/code/misc/2025_09_03__05/xx.v
================== C compilation error (from clang-18): ==============
cc: /tmp/v_1000/xx.01K46Q5V6Z5PE53QK72MZC7690.tmp.c:1965:12: error: initializing 'Array_u8' (aka 'struct array') with an expression of incompatible type 'Array_u8 (rune)' (aka 'struct array (unsigned int)')
cc:  1965 |         {Array_u8 _ = rune_bytes;}
cc:       |                   ^   ~~~~~~~~~~
cc: 1 error generated.
======================================================================
(You can pass `-cg`, or `-show-c-output` as well, to print all the C error messages).
#1 05:40:59 ^ pr_25178 ~/v>

V generated:

VV_LOC void main__main(void) {
    rune r = ((rune)(0));
    Array_u8 _var_rune_bytes = rune_bytes(r);
    {Array_u8 _ = rune_bytes;}
    ;
}

@spytheman

Copy link
Copy Markdown
Contributor

On master, the generated code was:

VV_LOC void main__main(void) {
    rune r = ((rune)(0));
    Array_u8 rune_bytes = rune_bytes(r);
    {Array_u8 _ = rune_bytes;}
    ;
}

@dy-tea

dy-tea commented Sep 3, 2025 •

Copy link
Copy Markdown
Member Author

Can't believe I missed that 🥲. I will add some extra tests.

Edit: yes the fix was quite simple, I'm also adding a test for assignment.

@dy-tea

dy-tea commented Sep 3, 2025

Copy link
Copy Markdown
Member Author

CI seems to be timing out on installing dependencies for some checks. Should I rerun?

@spytheman

Copy link
Copy Markdown
Contributor

CI seems to be timing out on installing dependencies for some checks. Should I rerun?

Yes. Github Actions runners are flaky in that way unfortunately :-| .

@spytheman spytheman left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Excellent work.

@spytheman
spytheman merged commit f17e0fd into vlang:master Sep 4, 2025
83 checks passed
@spytheman

Copy link
Copy Markdown
Contributor

Sorry, but after merging it, the performance is much worse:
image

I've reverted the change for now.

spytheman added a commit that referenced this pull request Sep 4, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants