[PATCH] D34370: Fix for Bug 33471: Preventing operator auto from resolving to a template operator.

Erich Keane via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Jun 19 15:23:32 PDT 2017


erichkeane created this revision.

As the bug report says,
struct A
{

  template<typename T> operator T();

};

void foo()
{

  A().operator auto();

}

causes: "undeduced type in IR-generation
UNREACHABLE executed at llvm/tools/clang/lib/CodeGen/CodeGenFunction.cpp:208!"

The problem is that in this case, "T" is being deduced as "auto", which I believe is incorrect.

The 'operator auto' implementation in Clang is standards compliant, however there is a defect report against core (1670).


https://reviews.llvm.org/D34370

Files:
  lib/Sema/SemaLookup.cpp
  test/SemaCXX/cxx1y-deduced-return-type.cpp


Index: test/SemaCXX/cxx1y-deduced-return-type.cpp
===================================================================
--- test/SemaCXX/cxx1y-deduced-return-type.cpp
+++ test/SemaCXX/cxx1y-deduced-return-type.cpp
@@ -55,6 +55,20 @@
   return "goodbye";
 }
 
+// Allow 'operator auto' to call only the explicit operator auto.
+struct BothOps{
+  template<typename T> operator T();
+  operator auto() { return 0; }
+};
+struct JustTemplateOp {
+  template<typename T> operator T();
+};
+
+auto c() {
+  BothOps().operator auto(); // ok
+  JustTemplateOp().operator auto(); // expected-error {{no member named 'operator auto' in 'JustTemplateOp'}}
+}
+
 auto *ptr_1() {
   return 100; // expected-error {{cannot deduce return type 'auto *' from returned value of type 'int'}}
 }
Index: lib/Sema/SemaLookup.cpp
===================================================================
--- lib/Sema/SemaLookup.cpp
+++ lib/Sema/SemaLookup.cpp
@@ -862,6 +862,17 @@
   if (!Record->isCompleteDefinition())
     return Found;
 
+  // For conversion operators, 'operator auto' should only match
+  // 'operator auto'.  Since 'auto' is not a type, it shouldn't be considered
+  // as a candidate for template substitution.
+  if (R.getLookupName().getNameKind() ==
+          DeclarationName::CXXConversionFunctionName) {
+    auto *Ty =
+        dyn_cast<AutoType>(R.getLookupName().getCXXNameType());
+    if (Ty && Ty->isUndeducedType())
+      return Found;
+  }
+
   for (CXXRecordDecl::conversion_iterator U = Record->conversion_begin(),
          UEnd = Record->conversion_end(); U != UEnd; ++U) {
     FunctionTemplateDecl *ConvTemplate = dyn_cast<FunctionTemplateDecl>(*U);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D34370.103115.patch
Type: text/x-patch
Size: 1665 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20170619/3c277d75/attachment.bin>


More information about the cfe-commits mailing list