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

Unexpected internal linkage of const/constexpr variable within extern "C++" #99825

Closed
kamrann opened this issue Jul 21, 2024 · 2 comments · Fixed by #102574
Closed

Unexpected internal linkage of const/constexpr variable within extern "C++" #99825

kamrann opened this issue Jul 21, 2024 · 2 comments · Fixed by #102574
Assignees
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang:modules C++20 modules and Clang Header Modules constexpr Anything related to constant evaluation

Comments

@kamrann
Copy link

kamrann commented Jul 21, 2024

clang rejects the following with error: declaration of 'x' with internal linkage cannot be exported:

export module mod;

extern "C++"
{
    export constexpr auto x = 10;
}

Compiler explorer: https://godbolt.org/z/a6nhazoKr

According to [basic.link] 3.2, const (or constexpr ) implies internal linkage for a namespace scope variable, with some exceptions, and 3.2.1 states that one such exception is if the declaration is in the purview of a module interface. clang appears to respect this generally, but in the case of extern "C++" it seems to not apply this exception, as if it considers the contents of such a block to not be part of the module's purview. This feels like a corner case, since obviously the symbols are not attached to the module; but the purview is nonetheless defined as all tokens following the module declaration and I can't see anywhere that states a linkage block is exempt.

Note that MSVC accepts the code.

@EugeneZelenko EugeneZelenko added clang:frontend Language frontend issues, e.g. anything involving "Sema" constexpr Anything related to constant evaluation and removed new issue labels Jul 21, 2024
@llvmbot
Copy link
Member

llvmbot commented Jul 21, 2024

@llvm/issue-subscribers-clang-frontend

Author: Cameron Angus (kamrann)

clang rejects the following with `error: declaration of 'x' with internal linkage cannot be exported`: ``` export module mod;

extern "C++"
{
export constexpr auto x = 10;
}

Compiler explorer: https://godbolt.org/z/a6nhazoKr

According to [[basic.link] 3.2](https://eel.is/c++draft/basic.link#<!-- -->3.2), `const` (or `constexpr` ) implies internal linkage for a namespace scope variable, with some exceptions, and 3.2.1 states that one such exception is if the declaration is in the purview of a module interface. clang appears to respect this generally, but in the case of `extern "C++"` it seems to not apply this exception, as if it considers the contents of such a block to not be part of the module's purview. This feels like a corner case, since obviously the symbols are not attached to the module; but the purview is nonetheless defined as all tokens following the module declaration and I can't see anywhere that states a linkage block is exempt.

Note that MSVC accepts the code.
</details>

@AaronBallman AaronBallman added the clang:modules C++20 modules and Clang Header Modules label Jul 22, 2024
@llvmbot
Copy link
Member

llvmbot commented Jul 22, 2024

@llvm/issue-subscribers-clang-modules

Author: Cameron Angus (kamrann)

clang rejects the following with `error: declaration of 'x' with internal linkage cannot be exported`: ``` export module mod;

extern "C++"
{
export constexpr auto x = 10;
}

Compiler explorer: https://godbolt.org/z/a6nhazoKr

According to [[basic.link] 3.2](https://eel.is/c++draft/basic.link#<!-- -->3.2), `const` (or `constexpr` ) implies internal linkage for a namespace scope variable, with some exceptions, and 3.2.1 states that one such exception is if the declaration is in the purview of a module interface. clang appears to respect this generally, but in the case of `extern "C++"` it seems to not apply this exception, as if it considers the contents of such a block to not be part of the module's purview. This feels like a corner case, since obviously the symbols are not attached to the module; but the purview is nonetheless defined as all tokens following the module declaration and I can't see anywhere that states a linkage block is exempt.

Note that MSVC accepts the code.
</details>

@ChuanqiXu9 ChuanqiXu9 self-assigned this Aug 9, 2024
ChuanqiXu9 added a commit that referenced this issue Aug 9, 2024
…inkage from module interfaces (#102574)

Close #99825

The root cause of the issue is that I didn't realize the things in
implicit global module (the language linkage in module interfaces)
should be considered in module purview.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang:modules C++20 modules and Clang Header Modules constexpr Anything related to constant evaluation
Projects
None yet
5 participants