[C++ PATCH] Don't ICE on invalid __decltype (~type) (PR c++/34268)
Jakub Jelinek
jakub@redhat.com
Thu Nov 29 14:26:00 GMT 2007
Hi!
~type is a valid id-expression, but
1) finish_id_expression doesn't expect to be called with it
2) finish_decltype_type assertion catches it anyway
The following patch handles BIT_NOT_EXPR with TYPE_P argument gracefully.
Ok for trunk?
2007-11-29 Jakub Jelinek <jakub@redhat.com>
PR c++/34268
* parser.c (cp_parser_decltype): Don't call finish_id_expression
on ~type.
* semantics.c (finish_decltype_type): Handle BIT_NOT_EXPR.
* g++.dg/cpp0x/decltype7.C: New test.
--- gcc/cp/parser.c.jj 2007-11-20 14:51:47.000000000 +0100
+++ gcc/cp/parser.c 2007-11-29 12:01:33.000000000 +0100
@@ -8512,10 +8512,12 @@ cp_parser_decltype (cp_parser *parser)
/*check_dependency=*/true,
/*ambiguous_decls=*/NULL);
- if (expr
+ if (expr
&& expr != error_mark_node
&& TREE_CODE (expr) != TEMPLATE_ID_EXPR
&& TREE_CODE (expr) != TYPE_DECL
+ && (TREE_CODE (expr) != BIT_NOT_EXPR
+ || !TYPE_P (TREE_OPERAND (expr, 0)))
&& cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
{
/* Complete lookup of the id-expression. */
--- gcc/cp/semantics.c.jj 2007-11-18 22:56:11.000000000 +0100
+++ gcc/cp/semantics.c 2007-11-29 12:05:34.000000000 +0100
@@ -4154,6 +4154,12 @@ finish_decltype_type (tree expr, bool id
type = TREE_TYPE (expr);
break;
+ case BIT_NOT_EXPR:
+ /* ~type is an id-expression, but not valid for decltype. */
+ gcc_assert (TYPE_P (TREE_OPERAND (expr, 0)));
+ error ("argument to decltype must be an expression");
+ return error_mark_node;
+
default:
gcc_assert (TYPE_P (expr) || DECL_P (expr));
error ("argument to decltype must be an expression");
--- gcc/testsuite/g++.dg/cpp0x/decltype7.C.jj 2007-11-29 12:09:54.000000000 +0100
+++ gcc/testsuite/g++.dg/cpp0x/decltype7.C 2007-11-29 12:09:28.000000000 +0100
@@ -0,0 +1,14 @@
+// PR c++/34268
+// { dg-do compile }
+
+struct A
+{
+ __decltype (A); // { dg-error "must be an expression" }
+ __decltype (~A); // { dg-error "must be an expression" }
+};
+
+struct B
+{
+ __typeof__ (B);
+ __typeof__ (~B); // { dg-error "expected primary-expression" }
+};
Jakub
More information about the Gcc-patches
mailing list