Skip to content

clang rejects reusing names in unexported variables between modules #101396

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
davidstone opened this issue Jul 31, 2024 · 2 comments
Closed

clang rejects reusing names in unexported variables between modules #101396

davidstone opened this issue Jul 31, 2024 · 2 comments
Labels
clang:modules C++20 modules and Clang Header Modules duplicate Resolved as duplicate

Comments

@davidstone
Copy link
Contributor

Given the following three translation units:

export module a;

constexpr int x = 0;

export void a() {
	(void)x;
}
export module b;

constexpr int x = 0;

export void b() {
	(void)x;
}
import a;
import b;

And compiling with

clang++ -std=c++23 -x c++-module -fmodule-output=a.pcm --precompile -c a.cpp
clang++ -std=c++23 -x c++-module -fmodule-output=b.pcm --precompile -c b.cpp
clang++ -std=c++23 -fmodule-file=a=a.pcm -fmodule-file=b=b.pcm -c c.cpp

causes clang to fail with

In file included from c.cpp:1:
a.cpp:3:15: error: declaration 'x' attached to named module 'a' can't be attached to other modules
    3 | constexpr int x = 0;
      |               ^
b.cpp:3:15: note: also found
    3 | constexpr int x = 0;
      |               ^
1 error generated.

I believe this code should be accepted and have the same behavior as if x were declared static in both module a and module b. x has module linkage in both module a and module b. Because of that, and based on https://eel.is/c++draft/basic.link#8, the two declarations of x are not declaring the same entity and thus the error is incorrect.

@github-actions github-actions bot added the clang Clang issues not falling into any other category label Jul 31, 2024
@EugeneZelenko EugeneZelenko added clang:modules C++20 modules and Clang Header Modules and removed clang Clang issues not falling into any other category labels Jul 31, 2024
@llvmbot
Copy link
Member

llvmbot commented Jul 31, 2024

@llvm/issue-subscribers-clang-modules

Author: David Stone (davidstone)

Given the following three translation units:
export module a;

constexpr int x = 0;

export void a() {
	(void)x;
}
export module b;

constexpr int x = 0;

export void b() {
	(void)x;
}
import a;
import b;

And compiling with

clang++ -std=c++23 -x c++-module -fmodule-output=a.pcm --precompile -c a.cpp
clang++ -std=c++23 -x c++-module -fmodule-output=b.pcm --precompile -c b.cpp
clang++ -std=c++23 -fmodule-file=a=a.pcm -fmodule-file=b=b.pcm -c c.cpp

causes clang to fail with

In file included from c.cpp:1:
a.cpp:3:15: error: declaration 'x' attached to named module 'a' can't be attached to other modules
    3 | constexpr int x = 0;
      |               ^
b.cpp:3:15: note: also found
    3 | constexpr int x = 0;
      |               ^
1 error generated.

I believe this code should be accepted and have the same behavior as if x were declared static in both module a and module b. x has module linkage in both module a and module b. Because of that, and based on https://eel.is/c++draft/basic.link#8, the two declarations of x are not declaring the same entity and thus the error is incorrect.

@ChuanqiXu9 ChuanqiXu9 added the duplicate Resolved as duplicate label Aug 1, 2024
@ChuanqiXu9
Copy link
Member

This may be duplicated with #90154.

And I don't have an expectation that how and when this problem will be solved properly. It is somewhat fundamental.

The workaround suggestion may be: 1. use namespaces. 2. Use static variable with Reduced BMI (https://clang.llvm.org/docs/StandardCPlusPlusModules.html#reduced-bmi).

@ChuanqiXu9 ChuanqiXu9 closed this as not planned Won't fix, can't repro, duplicate, stale Aug 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:modules C++20 modules and Clang Header Modules duplicate Resolved as duplicate
Projects
None yet
Development

No branches or pull requests

4 participants