Skip to content

(&v != &v) for function parameters passed by value #18194

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

Closed
slonik-az opened this issue Dec 4, 2023 · 3 comments
Closed

(&v != &v) for function parameters passed by value #18194

slonik-az opened this issue Dec 4, 2023 · 3 comments
Labels
bug Observed behavior contradicts documented or intended behavior frontend Tokenization, parsing, AstGen, Sema, and Liveness.
Milestone

Comments

@slonik-az
Copy link

slonik-az commented Dec 4, 2023

Zig Version

0.12.0-dev.1606+569182dbb

Steps to Reproduce and Observed Behavior

Related to #16343. A new stack variable is created each time the address of the function parameter is taken resulting in &v == &v being false.

Here is the repro

const std = @import("std");

fn pro_cmp_address(v: i32) bool {
    return (&v == &v);
}

test "pro compare address" {
    try std.testing.expectEqual(true, pro_cmp_address(123)); // Test fails
}

Expected Behavior

It is expected that &v == &v is true. Taking address of a variable should have no side effects.

@slonik-az slonik-az added the bug Observed behavior contradicts documented or intended behavior label Dec 4, 2023
@slonik-az slonik-az changed the title Parameter Reference Optimization (PRO) seems to violate basic address logic Parameter Reference Optimization (PRO) violates basic address logic Dec 4, 2023
@slonik-az slonik-az changed the title Parameter Reference Optimization (PRO) violates basic address logic (&v != &v) for function parameters passed by value Dec 4, 2023
@ifreund ifreund added this to the 0.12.0 milestone Dec 5, 2023
@slonik-az
Copy link
Author

One possible solution would be to follow Carbon language approach and ban taking address of the function parameter altogether (make it compile error). The rational for this drastic act being that compiler is free to put a parameter into a register, in which case no address is available anyway. If for some reason one needs the address of the object passed into the function one can change the function's signature and explicitly pass it by (const) pointer.

@andrewrk andrewrk removed the bug Observed behavior contradicts documented or intended behavior label Jan 9, 2024
@andrewrk andrewrk modified the milestones: 0.12.0, 0.13.0 Jan 9, 2024
@xdBronch
Copy link
Contributor

this can be closed now

@andrewrk andrewrk added bug Observed behavior contradicts documented or intended behavior frontend Tokenization, parsing, AstGen, Sema, and Liveness. labels Dec 10, 2024
@andrewrk
Copy link
Member

Fix landed in 8245d7f.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Observed behavior contradicts documented or intended behavior frontend Tokenization, parsing, AstGen, Sema, and Liveness.
Projects
None yet
Development

No branches or pull requests

4 participants