re PR c++/5293 (confusing message when binding a temporary to a reference)
authorGabriel Dos Reis <gdr@integrable-solutions.net>
Mon, 14 Jul 2003 15:04:32 +0000 (15:04 +0000)
committerGabriel Dos Reis <gdr@gcc.gnu.org>
Mon, 14 Jul 2003 15:04:32 +0000 (15:04 +0000)
PR c++/5293
* call.c (initialize_reference): Improve diagnostic.

From-SVN: r69332

gcc/cp/ChangeLog
gcc/cp/call.c
gcc/testsuite/g++.dg/init/ref8.C [new file with mode: 0644]

index 8cc02342dbe7365e11bbf4b7b83b297418375349..2627e944e75475c89ac5a0441000c55515d68788 100644 (file)
@@ -1,3 +1,8 @@
+2003-07-14  Gabriel Dos Reis  <gdr@integrable-solutions.net>
+
+       PR c++/5293
+       * call.c (initialize_reference): Improve diagnostic.
+
 2003-07-14  Kriang Lerdsuwanakij  <lerdsuwa@users.sourceforge.net>
 
        PR c++/11154
index 39f82211b759ff0221ff1a9d7464bb1bb4837f2c..c71b3f85dfa86bc79dd70134ff95e863f1d558ec 100644 (file)
@@ -6035,7 +6035,13 @@ initialize_reference (tree type, tree expr, tree decl)
   conv = reference_binding (type, TREE_TYPE (expr), expr, LOOKUP_NORMAL);
   if (!conv || ICS_BAD_FLAG (conv))
     {
-      error ("could not convert `%E' to `%T'", expr, type);
+      if (!(TYPE_QUALS (TREE_TYPE (type)) & TYPE_QUAL_CONST)
+          && !real_lvalue_p (expr))
+        error ("invalid initialization of non-const reference of "
+               "type '%T' from a temporary of type '%T'",
+               type, TREE_TYPE (expr));
+      else
+        error ("could not convert `%E' to `%T'", expr, type);
       return error_mark_node;
     }
 
diff --git a/gcc/testsuite/g++.dg/init/ref8.C b/gcc/testsuite/g++.dg/init/ref8.C
new file mode 100644 (file)
index 0000000..406cc10
--- /dev/null
@@ -0,0 +1,10 @@
+struct A {
+   A operator=(const A&);
+};
+
+A operator*(A, A);
+
+A& operator+=(A& a, const A& b)
+{
+   return a = a * b;            // { dg-error "non-const reference" }
+}