This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
[C++ PATCH] [PR7841] Fix invalid name of constructor
- From: "Giovanni Bajo" <giovannibajo at libero dot it>
- To: <gcc-patches at gcc dot gnu dot org>
- Cc: "Mark Mitchell" <mark at codesourcery dot com>
- Date: Mon, 19 Apr 2004 22:59:06 +0200
- Subject: [C++ PATCH] [PR7841] Fix invalid name of constructor
Hello,
the testcase presented in PR 7841 is invalid but accepted by GCC:
-----------------------------------------
template <int dim> class X { X(); };
template <int dim> X<dim>::X<dim> () {};
----------------------------^^^^^--------
In fact, the constructor is not a template, and the template argument list can
be specified only after template names (14.2/2). Notice also that X cannot be
the injected class name (in template form) because it is qualified (14.6.1/1).
The following patch detects and rejects this code in the parser. The error
message which the patch adds is:
pr7841.cc: At global scope:
pr7841.cc:2: error: invalid use of argument list for a constructor in a
qualified name
pr7841.cc:2: note: use `X<dim>::X' instead of `X<dim>::X<dim>' to name the
constructor in a qualified name
There was already a test in old-deja that was testing for this construct as
valid (it's probably pre-ISO), so I just added a dg-error mark to it.
Tested on i686-pc-linux-gnu, OK for mainline?
Giovanni Bajo
cp/
2004-04-19 Giovanni Bajo <giovannibajo@gcc.gnu.org>
PR c++/7841
* parser.c (cp_parser_direct_declarator): Reject constructor named
as qualified template-id.
testsuite/
2004-04-19 Giovanni Bajo <giovannibajo@gcc.gnu.org>
PR c++/7841
* g++.old-deja/g++.pt/ctor2.C: Add error mark.
Index: cp/parser.c
===================================================================
RCS file: /cvs/gcc/gcc/gcc/cp/parser.c,v
retrieving revision 1.188
diff -c -3 -p -r1.188 parser.c
*** cp/parser.c 30 Mar 2004 19:19:01 -0000 1.188
--- cp/parser.c 19 Apr 2004 20:55:26 -0000
*************** cp_parser_direct_declarator (cp_parser*
*** 10633,10638 ****
--- 10633,10649 ----
&& same_type_p (TREE_TYPE (unqualified_name),
class_type)))
*ctor_dtor_or_conv_p = -1;
+ if (TREE_CODE (declarator) == SCOPE_REF
+ && TREE_CODE (unqualified_name) == TYPE_DECL
+ && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (unqualified_name)))
+ {
+ error ("invalid use of argument list for a constructor "
+ "in a qualified name");
+ inform ("use `%T::%D' instead of `%T::%T' to name the "
+ "constructor in a qualified name", class_type,
+ DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
+ class_type, class_type);
+ }
}
handle_declarator:;
Index: testsuite/g++.old-deja/g++.pt/ctor2.C
===================================================================
RCS file: /cvs/gcc/gcc/gcc/testsuite/g++.old-deja/g++.pt/ctor2.C,v
retrieving revision 1.3
diff -c -3 -p -r1.3 ctor2.C
*** testsuite/g++.old-deja/g++.pt/ctor2.C 1 May 2003 02:02:53 -0000 1.3
--- testsuite/g++.old-deja/g++.pt/ctor2.C 19 Apr 2004 20:55:26 -0000
*************** struct A {
*** 8,13 ****
};
template <class T>
! A<T>::A<T>()
{
}
--- 8,13 ----
};
template <class T>
! A<T>::A<T>() // { dg-error "invalid use of argument list|qualified name" }
{
}