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

Missing parentheses on reference applied to a cast #1084

Open
glguy opened this issue Apr 22, 2024 · 0 comments · May be fixed by #1121
Open

Missing parentheses on reference applied to a cast #1084

glguy opened this issue Apr 22, 2024 · 0 comments · May be fixed by #1121
Assignees
Labels
bug Something isn't working

Comments

@glguy
Copy link
Contributor

glguy commented Apr 22, 2024

int main(void) {
    int * x = &(int){0};
}

generates

#![allow(dead_code, mutable_transmutes, non_camel_case_types, non_snake_case, non_upper_case_globals, unused_assignments, unused_mut)]
unsafe fn main_0() -> libc::c_int {
    let mut x: *mut libc::c_int = &mut 0 as libc::c_int as *mut libc::c_int;
    return 0;
}
pub fn main() {
    unsafe { ::std::process::exit(main_0() as i32) }
}

but extra parentheses are needed

    let mut x: *mut libc::c_int = &mut (0 as libc::c_int) as *mut libc::c_int;

This can happen in more realistic code when an output function parameter is being ignored f(&(int){0});

If someone reads this issue and wonders if this is even legal C, check out compound literal

The value category of a compound literal is lvalue (its address can be taken). ...

@fw-immunant fw-immunant self-assigned this Sep 5, 2024
@fw-immunant fw-immunant added the bug Something isn't working label Sep 5, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants