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

Clang cannot use _Atomic qualified integer type as controlling expression in switch statement #65557

Closed
Christian-Gibbons opened this issue Sep 7, 2023 · 8 comments
Labels
c clang:frontend Language frontend issues, e.g. anything involving "Sema" confirmed Verified by a second party

Comments

@Christian-Gibbons
Copy link

Christian-Gibbons commented Sep 7, 2023

Clang cannot use _Atomic qualified integer type as controlling expression in switch statement.
Minimal example program that will fail to compile:

#include <stdatomic.h>
#include <stdio.h>
#include <stdlib.h>

int main(void) {
    _Atomic int x = 0;
    switch(x) {
        default:
            break;
    }
    return EXIT_SUCCESS;
}

Building with Clang 16.0.6 results in build error with following message:

main.c:7:5: error: statement requires expression of integer type ('_Atomic(int)' invalid)
    switch(x) {
    ^      ~
@github-actions github-actions bot added clang Clang issues not falling into any other category new issue labels Sep 7, 2023
@EugeneZelenko EugeneZelenko added clang:frontend Language frontend issues, e.g. anything involving "Sema" and removed clang Clang issues not falling into any other category new issue labels Sep 7, 2023
@llvmbot
Copy link
Collaborator

llvmbot commented Sep 7, 2023

@llvm/issue-subscribers-clang-frontend

@tbaederr tbaederr added the confirmed Verified by a second party label Sep 7, 2023
@tbaederr
Copy link
Contributor

tbaederr commented Sep 7, 2023

Also broken in trunk: https://godbolt.org/z/oaTxcG59e

@cor3ntin cor3ntin added the c label Sep 7, 2023
@llvmbot
Copy link
Collaborator

llvmbot commented Sep 7, 2023

@llvm/issue-subscribers-c

@cor3ntin
Copy link
Contributor

cor3ntin commented Sep 7, 2023

@to268 @AaronBallman

@to268
Copy link
Contributor

to268 commented Sep 7, 2023

It looks like on Godbolt that we have never supported this.
But it seems that it's a bug rather than a restriction in the C standard.
I need to look more about it, since the handling of the _Atomic qualifier is somewhat strange.

@to268
Copy link
Contributor

to268 commented Sep 15, 2023

The C standard requires an integral type for a controlling expression in a switch statement, but I have written a fix for this at https://reviews.llvm.org/D159522.

@AaronBallman
Copy link
Collaborator

The C standard requires an integral type for a controlling expression in a switch statement, but I have written a fix for this at https://reviews.llvm.org/D159522.

FYI: the controlling expression has to be of integer type, but the primary expression x (in the original example) is in a context that will undergo lvalue conversion (C23 6.3.2.1p2) and that conversion will strip off the atomic qualification and leave an integer type. So by the time we're checking the switch condition, we should have already lost the atomic type and only be seeing the integer type.

@to268
Copy link
Contributor

to268 commented Sep 15, 2023

So this is valid in the C23 standard, good to know!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c clang:frontend Language frontend issues, e.g. anything involving "Sema" confirmed Verified by a second party
Projects
None yet
Development

No branches or pull requests

7 participants