[PATCH] D116792: [AST] lookup in parent DeclContext for transparent DeclContext

Chuanqi Xu via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Jan 10 18:17:17 PST 2022


This revision was automatically updated to reflect the committed changes.
Closed by commit rGd9d63fc1088c: [AST] lookup in parent DeclContext for transparent DeclContext (authored by ChuanqiXu).

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D116792/new/

https://reviews.llvm.org/D116792

Files:
  clang/lib/AST/DeclBase.cpp
  clang/test/Modules/Inputs/template_name_lookup/foo.cppm
  clang/test/Modules/template_name_lookup.cpp
  clang/test/SemaCXX/lookup-template-name-extern-CXX.cpp


Index: clang/test/SemaCXX/lookup-template-name-extern-CXX.cpp
===================================================================
--- /dev/null
+++ clang/test/SemaCXX/lookup-template-name-extern-CXX.cpp
@@ -0,0 +1,12 @@
+// Tests that the lookup in transparent declaration context
+// (linkage specifiaction context) wouldn't cause compiler crash.
+// RUN: %clang_cc1 -std=c++20 %s -fsyntax-only -verify
+extern "C++" {
+template <class T>
+class X {}; // expected-note {{candidate template ignored: couldn't infer template argument 'T'}}
+            // expected-note at -1 {{candidate function template not viable: requires 1 argument, but 0 were provided}}
+}
+
+void foo() {
+  X x; // expected-error {{no viable constructor or deduction guide for deduction of template arguments of 'X'}}
+}
Index: clang/test/Modules/template_name_lookup.cpp
===================================================================
--- /dev/null
+++ clang/test/Modules/template_name_lookup.cpp
@@ -0,0 +1,11 @@
+// RUN: rm -rf %t
+// RUN: mkdir -p %t
+// RUN: %clang_cc1 -std=c++20 %S/Inputs/template_name_lookup/foo.cppm -emit-module-interface -o %t/foo.pcm
+// RUN: %clang_cc1 -std=c++20 -fprebuilt-module-path=%t %s -fsyntax-only -verify
+
+import foo;
+void use() {
+  X x; // expected-error {{no viable constructor or deduction guide for deduction of template arguments of 'X'}}
+       // expected-note at Inputs/template_name_lookup/foo.cppm:3 {{candidate template ignored: couldn't infer template argument 'T'}}
+       // expected-note at Inputs/template_name_lookup/foo.cppm:3 {{candidate function template not viable: requires 1 argument, but 0 were provided}}
+}
Index: clang/test/Modules/Inputs/template_name_lookup/foo.cppm
===================================================================
--- /dev/null
+++ clang/test/Modules/Inputs/template_name_lookup/foo.cppm
@@ -0,0 +1,3 @@
+export module foo;
+export template <typename T>
+class X {};
Index: clang/lib/AST/DeclBase.cpp
===================================================================
--- clang/lib/AST/DeclBase.cpp
+++ clang/lib/AST/DeclBase.cpp
@@ -1644,9 +1644,9 @@
 
 DeclContext::lookup_result
 DeclContext::lookup(DeclarationName Name) const {
-  assert(getDeclKind() != Decl::LinkageSpec &&
-         getDeclKind() != Decl::Export &&
-         "should not perform lookups into transparent contexts");
+  // For transparent DeclContext, we should lookup in their enclosing context.
+  if (getDeclKind() == Decl::LinkageSpec || getDeclKind() == Decl::Export)
+    return getParent()->lookup(Name);
 
   const DeclContext *PrimaryContext = getPrimaryContext();
   if (PrimaryContext != this)


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D116792.398799.patch
Type: text/x-patch
Size: 2646 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220111/f5a4cfdb/attachment.bin>


More information about the cfe-commits mailing list