[clang][reflection] Mangle deduction-guide reflections instead of hitting unreachable - #299
Open
Cfretz244 wants to merge 2 commits into
Open
[clang][reflection] Mangle deduction-guide reflections instead of hitting unreachable#299Cfretz244 wants to merge 2 commits into
Cfretz244 wants to merge 2 commits into
Conversation
…ions in NTTP mangling A reflection of a template (ReflectionKind::Template) mangled as the template's NAME only. Class/variable/alias templates cannot be overloaded, so a name suffices for them -- but function templates overload, and two same-named siblings (e.g. absl raw_hash_map's operator[] and its SFINAE-false lifetimebound pack twin) produced byte-identical manglings. Any template dispatcher taking such a reflection as an NTTP -- e.g. template <typename T, std::meta::info tmpl> void level2(); instantiated per member from a `template for` over members_of(^^T) -- therefore got ONE mangled name for its two specializations. The AST-level specializations are correct and distinct; CodeGen then silently folds the linkonce_odr definitions by mangled name, and a single body serves both call sites. No diagnostic at any point. Fix: for FunctionTemplateDecl reflections, append a '$'-bracketed ODR hash of the template parameter list + the templated declaration pattern. The ODR hash is cross-TU-stable by design (modules use it to compare decls across TUs), so legitimate linkonce_odr merging is preserved. A structural mangling of the pattern's function type was tried first and abandoned: real-world dependent pattern types (lifetimebound SFINAE, noexcept(...) referencing parameters) embed ParmVarDecl-referencing expressions that mangleFunctionParam cannot encode outside a function-declaration context. The regression test asserts at RUNTIME that the two dispatcher instantiations are distinct functions with their own bodies; static_asserts cannot catch the fold because the AST is always correct. Fixes llvm#286. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
…ting unreachable
members_of over a namespace enumerates deduction guides like any other
member; lifting that list into define_static_array makes each element
reflection a template argument of the backing array specialization, so guide
reflections MUST mangle. mangleReflection's Template case encoded a
reflected template via mangleTemplateName -> mangleUnqualifiedName, which has
no case for CXXDeductionGuideName -- llvm_unreachable("Can't mangle a
deduction guide name!", ItaniumMangle.cpp:1774). The field shape is
TartanLlama/expected's namespace-scope `unexpected(E) -> unexpected<E>`: any
reflection consumer walking the tl namespace ICE'd at codegen.
Fix, local to mangleReflection (mangleUnqualifiedName's unreachable is a
sound invariant for normal symbol mangling and stays): encode "dg" + the
DEDUCED template's name + the same '$'-bracketed ODR-hash discriminator
already used for overloaded function templates. Every guide for one template
shares a single DeclarationName -- and Sema's IMPLICIT guides
(per-constructor + copy) enumerate alongside explicit ones, with the
per-constructor guide structurally identical to a same-signature explicit
guide -- so the hash also folds in isImplicit() and the deduction-candidate
kind. Deterministic and cross-TU-stable, preserving legitimate linkonce_odr
merging; distinct from a reflection of the deduced class template itself.
Test: deduction-guide-reflection-mangling.pass.cpp -- two explicit + two
implicit guides lifted via define_static_array, pinned as &probe<m> NTTPs,
asserted pairwise distinct at runtime (a fold is invisible to static_assert).
Stacks on the same-named-function-template NTTP discriminator change (it
extends the same mangleReflection hash block).
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This was referenced Jun 10, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #298.
Problem
members_ofover a namespace enumerates deduction guides like any other member, and lifting that member list intodefine_static_arraymangles each element reflection as a template argument of the backing array specialization.mangleReflection'sTemplatecase encodes a reflected template viamangleTemplateName→mangleUnqualifiedName, which has no case forCXXDeductionGuideName:llvm_unreachable("Can't mangle a deduction guide name!", ItaniumMangle.cpp:1774)— a sound invariant for normal symbol mangling (guides are never odr-used) that reflection now makes reachable from ordinary user code. Field shape: TartanLlama/expected's namespace-scopetemplate <class E> unexpected(E) -> unexpected<E>;— a reflection-driven binding generator walking thetlnamespace ICE'd at codegen while binding ANYtlclass (see #298 for the reproducer + build matrix).Fix
clang/lib/AST/ItaniumMangle.cpp, local tomangleReflection'sReflectionKind::Templatecase (themangleUnqualifiedNameunreachable stays): a guide reflection encodes as"dg"+ the deduced template's name + the same'$'-bracketed ODR-hash discriminator used for overloaded function templates (#287). Two subtleties force more than crash avoidance:CXXDeductionGuideName, and Sema's implicit guides (per-constructor + the copy guide) are enumerated alongside explicit ones once CTAD has been used in the TU;X(X<E>)), so the hash also folds inisImplicit()and the deduction-candidate kind.Deterministic and cross-TU-stable (ODR hash), preserving legitimate linkonce_odr merging; distinct from a reflection of the deduced class template itself (no
dgtag).Test
libcxx/test/std/experimental/reflection/deduction-guide-reflection-mangling.pass.cpp(new): thedefine_static_array(members_of(...))lift shape (the ICE), with four guides in the enumeration — two explicit (one structurally identical to the implicit per-constructor guide) + two implicit — pinned as&probe<m>NTTPs and asserted pairwise distinct at runtime, plus distinct from the deduced class template's own reflection. A fold is invisible tostatic_assert(the AST is always correct), so the observations are runtime.Validation
caac14845397, itself on837da39eb88c); builds standalone.-c(clean at-fsyntax-onlyand without the guide); the new test ICEs.substitute-nested-dependent.pass.cpp) still passes on the stack.clang/test/Reflection: 16/16 with the fix on this machine — identical to base.tlclass — passes its full differential suite.🤖 Generated with Claude Code