https://github.com/ChuanqiXu9 updated https://github.com/llvm/llvm-project/pull/214121
>From 72281ba48937660309da35dda79af85cc9bf5e9d Mon Sep 17 00:00:00 2001 From: Chuanqi Xu <[email protected]> Date: Wed, 5 Aug 2026 10:56:28 +0800 Subject: [PATCH 1/3] [C++20] [Modules] merge the type for anony enum from import and #include Close https://github.com/llvm/llvm-project/issues/213299 Ideally, we shall merge the new enum with the old enum when we creating the new enum. But the enum is anonymous and the typedef's name come after the enum body, it is too late to merge them. This is the choice 10 years ago: a523022b5384d7a0901beea7a5f36ee9c09ba339. Actually what we're merging is the typedef decls. Then https://github.com/llvm/llvm-project/pull/114240 removes the logic to remove the new ED. This the direct trigger for the above issue of ambiguous look ups. We choose to fix the problem by setting the type of new enum to the type of the old enum to fix the ambiguous lookup issue. --- clang/docs/ReleaseNotes.md | 4 +++ clang/lib/Sema/SemaDecl.cpp | 27 +++++++++++++++++++ .../Modules/include-after-imports-enums.cppm | 8 ++++++ 3 files changed, 39 insertions(+) diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md index a38b99ff8e075..209278f105ea4 100644 --- a/clang/docs/ReleaseNotes.md +++ b/clang/docs/ReleaseNotes.md @@ -385,6 +385,10 @@ features cannot lower the translation-unit ABI level; #### Bug Fixes to C++ Support +- Fixed a false type mismatch when a typedef naming an anonymous enumeration + was used through a C++20 named module and its defining header was subsequently + included. (#GH213299) + - Fixed an issue where `__typeof__` incorrectly rejected cv-qualified function types. - Fixed a bug where top-level CV qualifiers (such as ``const``) were dropped from pointers modified by Microsoft pointer attributes (like ``__ptr32`` and ``__ptr64``) and WebAssembly's ``__funcref``. diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp index c46d2d780fad7..974cd9a5debf1 100644 --- a/clang/lib/Sema/SemaDecl.cpp +++ b/clang/lib/Sema/SemaDecl.cpp @@ -2667,6 +2667,33 @@ void Sema::MergeTypedefNameDecl(Scope *S, TypedefNameDecl *New, else New->setTypeSourceInfo(OldTD->getTypeSourceInfo()); + // See https://github.com/llvm/llvm-project/issues/213299 for the case. + // + // Ideally, we shall merge the new enum with the old enum when we + // creating the new enum. But the enum is anonymous and the typedef's name + // come after the enum body, it is too late to merge them. This is the + // choice 10 years ago: a523022b5384d7a0901beea7a5f36ee9c09ba339. Actually + // what we're merging here is the typedef decls. + // + // Then https://github.com/llvm/llvm-project/pull/114240 removes the logic + // to remove the new ED. This the direct trigger for the above issue of + // ambiguous look ups. + // + // We choose to fix the problem by setting the type of new enum to the + // type of old enums. This is consistent with the above call to + // setTypeSourceInfo. + // + // FIXME: The check `M && M->isGlobalModule()` is not necessary but we + // hope to limit the impact of this change. We can relax the check when we + // find similar issue later in other cases. + if (Module *M = OldTag->getOwningModule(); M && M->isGlobalModule()) + if (auto *NewEnum = dyn_cast<EnumDecl>(NewTag)) + if (auto *OldEnum = dyn_cast<EnumDecl>(OldTag)) { + QualType OldEnumType = Context.getCanonicalTagType(OldEnum); + for (auto *ECD : NewEnum->enumerators()) + ECD->setType(OldEnumType); + } + // Make the old tag definition visible. makeMergedDefinitionVisible(Hidden); diff --git a/clang/test/Modules/include-after-imports-enums.cppm b/clang/test/Modules/include-after-imports-enums.cppm index 00affd98e299f..260f6d150f46f 100644 --- a/clang/test/Modules/include-after-imports-enums.cppm +++ b/clang/test/Modules/include-after-imports-enums.cppm @@ -9,13 +9,19 @@ // RUN: %clang_cc1 -std=c++20 %t/use.cpp -fprebuilt-module-path=%t -verify -fsyntax-only //--- enum.h +#pragma once + enum E { Value }; +typedef enum { TypedefValue } TypedefEnum; +void useTypedefEnum(TypedefEnum); + //--- M.cppm module; #include "enum.h" export module M; auto e = Value; +export TypedefEnum typedefEnum; //--- use.cpp // expected-no-diagnostics @@ -23,3 +29,5 @@ import M; #include "enum.h" auto e = Value; +static_assert(__is_same(decltype(TypedefValue), TypedefEnum)); +inline void testTypedefEnum() { useTypedefEnum(TypedefValue); } >From 6b6e9edd2c86d77c6d3e20e84ecb398ddfd00695 Mon Sep 17 00:00:00 2001 From: "yedeng.yd" <[email protected]> Date: Fri, 7 Aug 2026 17:43:20 +0800 Subject: [PATCH 2/3] Update --- clang/lib/Sema/SemaDecl.cpp | 25 +++++++------------------ 1 file changed, 7 insertions(+), 18 deletions(-) diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp index 974cd9a5debf1..721cb357f8381 100644 --- a/clang/lib/Sema/SemaDecl.cpp +++ b/clang/lib/Sema/SemaDecl.cpp @@ -2667,25 +2667,14 @@ void Sema::MergeTypedefNameDecl(Scope *S, TypedefNameDecl *New, else New->setTypeSourceInfo(OldTD->getTypeSourceInfo()); - // See https://github.com/llvm/llvm-project/issues/213299 for the case. + // An anonymous enum is recognized as a redeclaration only when its typedef + // name gets merged, at which point two distinct enum types already exist. + // Retype the new enumerators to the old enum type, matching the typedef + // merge above; otherwise the merged typedef and its enumerators disagree + // on the type (GH213299). // - // Ideally, we shall merge the new enum with the old enum when we - // creating the new enum. But the enum is anonymous and the typedef's name - // come after the enum body, it is too late to merge them. This is the - // choice 10 years ago: a523022b5384d7a0901beea7a5f36ee9c09ba339. Actually - // what we're merging here is the typedef decls. - // - // Then https://github.com/llvm/llvm-project/pull/114240 removes the logic - // to remove the new ED. This the direct trigger for the above issue of - // ambiguous look ups. - // - // We choose to fix the problem by setting the type of new enum to the - // type of old enums. This is consistent with the above call to - // setTypeSourceInfo. - // - // FIXME: The check `M && M->isGlobalModule()` is not necessary but we - // hope to limit the impact of this change. We can relax the check when we - // find similar issue later in other cases. + // FIXME: The global module restriction only limits the impact of this + // change; relax it if the issue shows up in other contexts. if (Module *M = OldTag->getOwningModule(); M && M->isGlobalModule()) if (auto *NewEnum = dyn_cast<EnumDecl>(NewTag)) if (auto *OldEnum = dyn_cast<EnumDecl>(OldTag)) { >From 5e6d7f59fc0adaa04a4116109da2dc26208ae68e Mon Sep 17 00:00:00 2001 From: Chuanqi Xu <[email protected]> Date: Mon, 10 Aug 2026 15:52:53 +0800 Subject: [PATCH 3/3] Update --- clang/lib/Sema/SemaDecl.cpp | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp index 721cb357f8381..143f9ce51bc8a 100644 --- a/clang/lib/Sema/SemaDecl.cpp +++ b/clang/lib/Sema/SemaDecl.cpp @@ -2667,21 +2667,26 @@ void Sema::MergeTypedefNameDecl(Scope *S, TypedefNameDecl *New, else New->setTypeSourceInfo(OldTD->getTypeSourceInfo()); - // An anonymous enum is recognized as a redeclaration only when its typedef - // name gets merged, at which point two distinct enum types already exist. - // Retype the new enumerators to the old enum type, matching the typedef - // merge above; otherwise the merged typedef and its enumerators disagree - // on the type (GH213299). + // An anonymous enum is recognized as a redeclaration only when its + // typedef name gets merged, at which point the new enum and its + // enumerators already have a distinct canonical type. Link the enum + // declarations, but also retype the new enumerators because + // setPreviousDecl() does not update QualTypes built before the merge; + // otherwise the merged typedef and its enumerators disagree on the type + // (GH213299). // // FIXME: The global module restriction only limits the impact of this // change; relax it if the issue shows up in other contexts. - if (Module *M = OldTag->getOwningModule(); M && M->isGlobalModule()) - if (auto *NewEnum = dyn_cast<EnumDecl>(NewTag)) + if (Module *M = OldTag->getOwningModule(); M && M->isGlobalModule()) { + if (auto *NewEnum = dyn_cast<EnumDecl>(NewTag)) { if (auto *OldEnum = dyn_cast<EnumDecl>(OldTag)) { + NewEnum->setPreviousDecl(OldEnum); QualType OldEnumType = Context.getCanonicalTagType(OldEnum); for (auto *ECD : NewEnum->enumerators()) ECD->setType(OldEnumType); } + } + } // Make the old tag definition visible. makeMergedDefinitionVisible(Hidden); _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
