Skip to content

[clang][reflection] Mangle deduction-guide reflections instead of hitting unreachable - #299

Open
Cfretz244 wants to merge 2 commits into
bloomberg:p2996from
Cfretz244:reflect-deduction-guide-mangling
Open

[clang][reflection] Mangle deduction-guide reflections instead of hitting unreachable#299
Cfretz244 wants to merge 2 commits into
bloomberg:p2996from
Cfretz244:reflect-deduction-guide-mangling

Conversation

@Cfretz244

Copy link
Copy Markdown

Fixes #298.

Stacked on #287 — the first commit here is #287's (this change extends the same mangleReflection hash block); review the last commit only.

Problem

members_of over a namespace enumerates deduction guides like any other member, and lifting that member list into define_static_array mangles each element reflection as a template argument of the backing array specialization. mangleReflection's Template case encodes a reflected template via mangleTemplateNamemangleUnqualifiedName, which has no case for CXXDeductionGuideName: 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-scope template <class E> unexpected(E) -> unexpected<E>; — a reflection-driven binding generator walking the tl namespace ICE'd at codegen while binding ANY tl class (see #298 for the reproducer + build matrix).

Fix

clang/lib/AST/ItaniumMangle.cpp, local to mangleReflection's ReflectionKind::Template case (the mangleUnqualifiedName unreachable 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:

  • every guide for one template shares the single CXXDeductionGuideName, and Sema's implicit guides (per-constructor + the copy guide) are enumerated alongside explicit ones once CTAD has been used in the TU;
  • an implicit per-constructor guide can be structurally identical to a same-signature explicit guide (and the copy guide to a per-constructor guide for X(X<E>)), so the hash also folds in isImplicit() 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 dg tag).

Test

libcxx/test/std/experimental/reflection/deduction-guide-reflection-mangling.pass.cpp (new): the define_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 to static_assert (the AST is always correct), so the observations are runtime.

Validation

🤖 Generated with Claude Code

Cfretz244 and others added 2 commits June 9, 2026 22:30
…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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant