This is the mail archive of the
gcc-patches@gcc.gnu.org
mailing list for the GCC project.
C++ PATCH for c++/23287
- From: Jason Merrill <jason at redhat dot com>
- To: gcc-patches List <gcc-patches at gcc dot gnu dot org>
- Date: Mon, 19 Jan 2009 17:50:04 -0500
- Subject: C++ PATCH for c++/23287
This code worked in the old parser because it would accept any
identifier after ~ and just check it at instantiation time. This patch
does the same if we can't find a suitable type name when parsing the
template.
Tested x86_64-pc-linux-gnu, applied to trunk.
2009-01-19 Jason Merrill <jason@redhat.com>
PR c++/23287
* parser.c (cp_parser_unqualified_id): In a template,
accept ~identifier.
* typeck.c (lookup_destructor): Handle IDENTIFIER_NODE.
* g++.dg/template/dtor5.C: New test.
Index: cp/typeck.c
===================================================================
*** cp/typeck.c (revision 143499)
--- cp/typeck.c (working copy)
*************** build_class_member_access_expr (tree obj
*** 2101,2108 ****
return result;
}
! /* Return the destructor denoted by OBJECT.SCOPE::~DTOR_NAME, or, if
! SCOPE is NULL, by OBJECT.~DTOR_NAME. */
static tree
lookup_destructor (tree object, tree scope, tree dtor_name)
--- 2101,2108 ----
return result;
}
! /* Return the destructor denoted by OBJECT.SCOPE::DTOR_NAME, or, if
! SCOPE is NULL, by OBJECT.DTOR_NAME, where DTOR_NAME is ~type. */
static tree
lookup_destructor (tree object, tree scope, tree dtor_name)
*************** lookup_destructor (tree object, tree sco
*** 2117,2123 ****
scope, dtor_type);
return error_mark_node;
}
! if (!DERIVED_FROM_P (dtor_type, TYPE_MAIN_VARIANT (object_type)))
{
error ("the type being destroyed is %qT, but the destructor refers to %qT",
TYPE_MAIN_VARIANT (object_type), dtor_type);
--- 2117,2137 ----
scope, dtor_type);
return error_mark_node;
}
! if (TREE_CODE (dtor_type) == IDENTIFIER_NODE)
! {
! /* In a template, names we can't find a match for are still accepted
! destructor names, and we check them here. */
! if (check_dtor_name (object_type, dtor_type))
! dtor_type = object_type;
! else
! {
! error ("object type %qT does not match destructor name ~%qT",
! object_type, dtor_type);
! return error_mark_node;
! }
!
! }
! else if (!DERIVED_FROM_P (dtor_type, TYPE_MAIN_VARIANT (object_type)))
{
error ("the type being destroyed is %qT, but the destructor refers to %qT",
TYPE_MAIN_VARIANT (object_type), dtor_type);
Index: cp/parser.c
===================================================================
*** cp/parser.c (revision 143499)
--- cp/parser.c (working copy)
*************** cp_parser_unqualified_id (cp_parser* par
*** 3880,3885 ****
--- 3880,3887 ----
parser->scope = NULL_TREE;
parser->object_scope = NULL_TREE;
parser->qualifying_scope = NULL_TREE;
+ if (processing_template_decl)
+ cp_parser_parse_tentatively (parser);
type_decl
= cp_parser_class_name (parser,
/*typename_keyword_p=*/false,
*************** cp_parser_unqualified_id (cp_parser* par
*** 3888,3893 ****
--- 3890,3903 ----
/*check_dependency=*/false,
/*class_head_p=*/false,
declarator_p);
+ if (processing_template_decl
+ && ! cp_parser_parse_definitely (parser))
+ {
+ /* We couldn't find a type with this name, so just accept
+ it and check for a match at instantiation time. */
+ type_decl = cp_parser_identifier (parser);
+ return build_nt (BIT_NOT_EXPR, type_decl);
+ }
}
/* If an error occurred, assume that the name of the
destructor is the same as the name of the qualifying
Index: testsuite/g++.dg/template/dtor5.C
===================================================================
*** testsuite/g++.dg/template/dtor5.C (revision 0)
--- testsuite/g++.dg/template/dtor5.C (revision 0)
***************
*** 0 ****
--- 1,21 ----
+ // PR c++/23287
+
+ template <class T> struct A
+ {
+ int i;
+ ~A();
+ };
+
+ template <class T> void f(A<T> *ap) {
+ ap->~A();
+ }
+
+ template <class T> void g(A<T> *ap) {
+ ap->~B(); // { dg-error "destructor name" }
+ }
+
+ int main()
+ {
+ f(new A<int>);
+ g(new A<int>);
+ }