[clang] std::meta is an associated namespace of reflect-expressions. (PR #242722)
via cfe-commits
cfe-commits at lists.llvm.org
Sat Nov 14 15:41:07 PST 2026
llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Daniel M. Katz (katzdm)
<details>
<summary>Changes</summary>
Implement C++26 [[basic.lookup.argdep]/3.1](https://eel.is/c++draft/basic.lookup.argdep):
> If T is `std::meta::info` ([[meta.syn]](https://eel.is/c++draft/meta.syn)), its associated set of entities is the singleton containing the enumeration type `std::meta::operators` ([[meta.reflection.operators]](https://eel.is/c++draft/meta.reflection.operators)).
Which has the effect of making `std::meta` an associated namespace of `^^operand`.
---
Full diff: https://github.com/llvm/llvm-project/pull/242722.diff
4 Files Affected:
- (modified) clang/include/clang/Sema/Sema.h (+6)
- (modified) clang/lib/Sema/SemaDeclCXX.cpp (+21)
- (modified) clang/lib/Sema/SemaLookup.cpp (+12-2)
- (modified) clang/test/Headers/meta.cpp (+8)
``````````diff
diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h
index 0e5548e56f678a..dc87541af3c0e3 100644
--- a/clang/include/clang/Sema/Sema.h
+++ b/clang/include/clang/Sema/Sema.h
@@ -5452,6 +5452,9 @@ class Sema final : public SemaBase {
/// implicitly define the namespace.
NamespaceDecl *getOrCreateStdNamespace();
+ /// Look up the special "std::meta" namespace (C++26).
+ NamespaceDecl *lookupStdMetaNamespace();
+
CXXRecordDecl *getStdBadAlloc() const;
EnumDecl *getStdAlignValT() const;
@@ -6706,6 +6709,9 @@ class Sema final : public SemaBase {
/// The C++ "std" namespace, where the standard library resides.
LazyDeclPtr StdNamespace;
+ /// The C++ "std::meta" namespace (C++26).
+ LazyDeclPtr StdMetaNamespace;
+
/// The C++ "std::initializer_list" template, which is defined in
/// \<initializer_list>.
ClassTemplateDecl *StdInitializerList;
diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index 62cbcc03ea42fe..93dc6605873f55 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -12706,6 +12706,27 @@ NamespaceDecl *Sema::getStdNamespace() const {
StdNamespace.get(Context.getExternalSource()));
}
+NamespaceDecl *Sema::lookupStdMetaNamespace() {
+ NamespaceDecl *StdMeta = cast_or_null<NamespaceDecl>(
+ StdMetaNamespace.get(Context.getExternalSource()));
+ if (StdMeta)
+ return StdMeta;
+
+ NamespaceDecl *Std = getStdNamespace();
+ if (!Std)
+ return Std;
+
+ LookupResult Result(*this, &PP.getIdentifierTable().get("meta"),
+ SourceLocation(), LookupNamespaceName);
+ if (!LookupQualifiedName(Result, Std) ||
+ !(StdMeta = Result.getAsSingle<NamespaceDecl>()))
+ Result.suppressDiagnostics();
+ else
+ StdMetaNamespace = StdMeta;
+
+ return StdMeta;
+}
+
namespace {
enum UnsupportedSTLSelect {
diff --git a/clang/lib/Sema/SemaLookup.cpp b/clang/lib/Sema/SemaLookup.cpp
index 5e061103437670..4a3c09c1e1f3d1 100644
--- a/clang/lib/Sema/SemaLookup.cpp
+++ b/clang/lib/Sema/SemaLookup.cpp
@@ -3206,9 +3206,19 @@ addAssociatedClassesAndNamespaces(AssociatedLookup &Result, QualType Ty) {
T = cast<ArrayType>(T)->getElementType().getTypePtr();
continue;
- // -- If T is a fundamental type, its associated sets of
- // namespaces and classes are both empty.
case Type::Builtin:
+ // C++26 [basic.lookup.argdep] p3.1
+ // -- If T is std::meta::info ([meta.syn]), its associated set of
+ // entities is the singleton containing the enumeration type
+ // std::meta::operators ([meta.reflection.operators]).
+ if (T->isMetaInfoType()) {
+ NamespaceDecl *StdMeta = Result.S.lookupStdMetaNamespace();
+ if (StdMeta)
+ Result.Namespaces.insert(StdMeta);
+ }
+
+ // -- If T is any other fundamental type, its associated set of
+ // entities is empty.
break;
// -- If T is a class type (including unions), its associated
diff --git a/clang/test/Headers/meta.cpp b/clang/test/Headers/meta.cpp
index 453ed1bcd87ba4..40d10a928f4eaa 100644
--- a/clang/test/Headers/meta.cpp
+++ b/clang/test/Headers/meta.cpp
@@ -131,3 +131,11 @@ consteval bool test_with_wrong_type() {
// std-note@*:* {{passing argument to parameter '__r' here}}
// builtin-error at -3 {{cannot initialize a parameter of type 'std::meta::info' with an rvalue of type 'int'}}
}
+
+consteval void test_associated_namespace_std_meta() {
+ const std::meta::info &ref = r;
+
+ static_assert(is_type(r));
+ static_assert(is_type(ref));
+ static_assert(is_type(^^int));
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/242722
More information about the cfe-commits
mailing list