[PATCH] D39679: [C++11] Fix warning when dropping cv-qualifiers when assigning to a reference with a braced initializer list

Nicolas Lesser via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Nov 6 03:35:22 PST 2017


Rakete1111 created this revision.
Rakete1111 added a project: clang.

This fixes PR34912 <http://llvm.org/pr34912> :)


https://reviews.llvm.org/D39679

Files:
  lib/Sema/SemaInit.cpp
  test/SemaCXX/references.cpp


Index: test/SemaCXX/references.cpp
===================================================================
--- test/SemaCXX/references.cpp
+++ test/SemaCXX/references.cpp
@@ -1,4 +1,5 @@
 // RUN: %clang_cc1 -fsyntax-only -verify %s
+// RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s 
 int g(int);
 
 void f() {
@@ -55,6 +56,13 @@
   //  const double& rcd2 = 2; // rcd2 refers to temporary with value 2.0
   const volatile int cvi = 1;
   const int& r = cvi; // expected-error{{binding value of type 'const volatile int' to reference to type 'const int' drops 'volatile' qualifier}}
+
+#if __cplusplus >= 201103L
+  const int& r2{cvi}; // expected-error{{binding value of type 'const volatile int' to reference to type 'const int' drops 'volatile' qualifier}}
+
+  const int a = 2;
+  int& r3 = a; // expected-error{{binding value of type 'const int' to reference to type 'int' drops 'const'}}
+#endif
 }
 
 // C++ [dcl.init.ref]p3
Index: lib/Sema/SemaInit.cpp
===================================================================
--- lib/Sema/SemaInit.cpp
+++ lib/Sema/SemaInit.cpp
@@ -7692,6 +7692,12 @@
 
   case FK_ReferenceInitDropsQualifiers: {
     QualType SourceType = Args[0]->getType();
+
+    // For braced initializer lists, we want to get the type
+    // of its (only) element, and not the "type" of the list itself.
+    if (const auto *List = dyn_cast<InitListExpr>(Args[0]))
+      SourceType = List->getInit(0)->getType();
+
     QualType NonRefType = DestType.getNonReferenceType();
     Qualifiers DroppedQualifiers =
         SourceType.getQualifiers() - NonRefType.getQualifiers();


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D39679.121710.patch
Type: text/x-patch
Size: 1603 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20171106/f69d6cd5/attachment.bin>


More information about the cfe-commits mailing list