Skip to content

Conversation

mpark
Copy link
Member

@mpark mpark commented Aug 28, 2025

I'm having a hard time figuring out what the expected behavior is for this case.

If I get rid of the anonymous union and just keep S like this:

template <typename T>
struct S { T x; };

There are no issues. However, I see that merging occurs to avoid any issues in this case. With the union, merging logic also occurs there but it doesn't seem to work out properly. But, I'm not sure if this should be triggering merging logic at all?

If I structure the main.cpp like this:

import "hu-01.h";
import "hu-02.h";

void g(S<int>) {}
void h() { f(); }

There are also no issues, and this time there doesn't seem to be any merging either.

Question: Should this case (1) not be merging at all? or (2) is it correct to be merging, and we need to fix the logic for merging anonymous unions?

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:modules C++20 modules and Clang Header Modules labels Aug 28, 2025
@llvmbot
Copy link
Member

llvmbot commented Aug 28, 2025

@llvm/pr-subscribers-clang-modules

@llvm/pr-subscribers-clang

Author: Michael Park (mpark)

Changes

I'm having a hard time figuring out what the expected behavior is for this case.

If I get rid of the anonymous union and just keep S like this:

template &lt;typename T&gt;
struct S { T x; };

There are no issues. However, I see that merging occurs to avoid any issues in this case. With the union, merging logic also occurs there but it doesn't seem to work out properly. But, I'm not sure if this should be triggering merging logic at all?

If I structure the main.cpp like this:

import "hu-01.h";
import "hu-02.h";

void g(S&lt;int&gt;) {}
void h() { f(); }

There are also no issues, and this time there doesn't seem to be any merging either.

Question: Should this case (1) not be merging at all? or (2) is it correct to be merging, and we need to fix the logic for merging anonymous unions?


Full diff: https://github.com/llvm/llvm-project/pull/155948.diff

1 Files Affected:

  • (added) clang/test/Modules/anon-union-in-template.cpp (+47)
diff --git a/clang/test/Modules/anon-union-in-template.cpp b/clang/test/Modules/anon-union-in-template.cpp
new file mode 100644
index 0000000000000..97fcdc7db86be
--- /dev/null
+++ b/clang/test/Modules/anon-union-in-template.cpp
@@ -0,0 +1,47 @@
+// RUN: rm -rf %t
+// RUN: mkdir -p %t
+// RUN: split-file %s %t
+
+// RUN: %clang_cc1 -std=c++20 -fmodule-name=hu-01 -emit-header-unit -xc++-user-header %t/hu-01.h \
+// RUN:  -o %t/hu-01.pcm
+
+// RUN: %clang_cc1 -std=c++20 -fmodule-name=hu-02 -emit-header-unit -xc++-user-header %t/hu-02.h \
+// RUN:  -Wno-experimental-header-units \
+// RUN:  -fmodule-map-file=%t/hu-01.map -fmodule-file=hu-01=%t/hu-01.pcm \
+// RUN:  -o %t/hu-02.pcm
+
+// RUN: %clang_cc1 -std=c++20 -emit-obj %t/main.cpp \
+// RUN:  -Wno-experimental-header-units \
+// RUN:  -fmodule-map-file=%t/hu-01.map -fmodule-file=hu-01=%t/hu-01.pcm \
+// RUN:  -fmodule-map-file=%t/hu-02.map -fmodule-file=hu-02=%t/hu-02.pcm
+
+//--- hu-01.map
+module "hu-01" {
+  header "hu-01.h"
+  export *
+}
+
+//--- hu-02.map
+module "hu-02" {
+  header "hu-02.h"
+  export *
+}
+
+//--- hu-01.h
+#pragma once
+
+template <typename T>
+struct S { union { T x; }; };
+
+using SI = S<int>;
+
+//--- hu-02.h
+import "hu-01.h";
+inline void f(S<int> s = {}) { s.x; }
+
+//--- main.cpp
+import "hu-01.h";
+void g(S<int>) {}
+
+import "hu-02.h";
+void h() { f(); }

Copy link
Member

@ChuanqiXu9 ChuanqiXu9 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we need to merge it. Why do you think we don't need to merge it?

@mpark
Copy link
Member Author

mpark commented Aug 29, 2025

It's just that this is a failing test case that I'd like to ship with a fix.

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 clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants