[PATCH] PR c++/21930: Conversions in diagnostics

Roger Sayle roger@eyesopen.com
Sun Jun 12 20:32:00 GMT 2005


The following patch fixes PR c++/21930 which is a long standing
problem with handling conversions in C++ error messages that was made
more visible by my recent patch to introduce a UNARY_PLUS_EXPR tree
code.

It turns out that prior to the new tree code for unary plus, the g++
front-end already had "ambiguity" problems with it's misuse of
CONVERT_EXPR, as witnessed by the code in dump_expr and the error
messages generated for the example in the PR.  The overloading of
CONVERT_EXPR meant that sometimes it represented a conversion/cast
and sometimes a unary plus.  The code in dump_expr assumed the
later, which further exposed/empahasized this issue when I moved
this unary plus handling to it's own tree code.

The patch below corrects this by treating CONVERT_EXPR identically
to NOP_EXPR, and removing the vestiages of handling unary plus as
a cast for void types.  For the code in the attached testcase, the
error message now reads:

testc.C: In function 'void foo(const A<N>&) [with int N = 0]':
testc.C:13:   instantiated from here
testc.C:9: error: no match for 'operator-' in '-((const A<0>&)((const A<0>*)a))'

which is an improvement upon both current mainline and the erroneous
message we generated with 4.0 and earlier (see the PR for details).


The following patch has been tested on i686-pc-linux-gnu with a full
"make bootstrap", all default languages, and regression tested with
a top-level "make -k check" with no new failures.

Ok for mainline?



2005-06-12  Roger Sayle  <roger@eyesopen.com>

	PR c++/21930
	* error.c (dump_expr): UNARY_PLUS_EXPR need not handle void types.
	Treat CONVERT_EXPR identically to NOP_EXPR.

	* g++.dg/other/error10.C: New test case.


Index: error.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cp/error.c,v
retrieving revision 1.282
diff -c -3 -p -r1.282 error.c
*** error.c	6 Jun 2005 19:31:33 -0000	1.282
--- error.c	12 Jun 2005 16:21:05 -0000
*************** dump_expr (tree t, int flags)
*** 1512,1526 ****
        break;

      case UNARY_PLUS_EXPR:
!       if (TREE_TYPE (t) && VOID_TYPE_P (TREE_TYPE (t)))
! 	{
! 	  pp_cxx_left_paren (cxx_pp);
! 	  dump_type (TREE_TYPE (t), flags);
! 	  pp_cxx_right_paren (cxx_pp);
! 	  dump_expr (TREE_OPERAND (t, 0), flags);
! 	}
!       else
! 	dump_unary_op ("+", t, flags);
        break;

      case ADDR_EXPR:
--- 1512,1518 ----
        break;

      case UNARY_PLUS_EXPR:
!       dump_unary_op ("+", t, flags);
        break;

      case ADDR_EXPR:
*************** dump_expr (tree t, int flags)
*** 1600,1605 ****
--- 1592,1598 ----
        break;

      case NOP_EXPR:
+     case CONVERT_EXPR:
        {
  	tree op = TREE_OPERAND (t, 0);


// PR c++/21930
// Test case by Volker Reichelt
// { dg-do compile }

template<int> struct A {};

template<int N>
void foo(const A<N> &a)
{ -A<N>(a); } // { dg-error "\\(\\(const A<0>\\*\\)a\\)" "" }

void bar()
{
    foo(A<0>()); // { dg-error "instantiated from here" "" }
}



Roger
--
Roger Sayle,                         E-mail: roger@eyesopen.com
OpenEye Scientific Software,         WWW: http://www.eyesopen.com/
Suite 1107, 3600 Cerrillos Road,     Tel: (+1) 505-473-7385
Santa Fe, New Mexico, 87507.         Fax: (+1) 505-473-0833



More information about the Gcc-patches mailing list