Skip to content

Commit

Permalink
c++: Report invalid id-expression in decltype [PR100482]
Browse files Browse the repository at this point in the history
Sorry, noticed I provided the wrong version of the test. Here is the
correct version (not relying on 'namespace std' being implicitly
defined). Bootstrapped + regtested on x86_64-pc-linux-gnu.

-- 8< --

This patch ensures that any errors raised by finish_id_expression when
parsing a decltype expression are properly reported, rather than
potentially going ignored and causing invalid code to be accepted.

We can also now remove the separate check for templates without args as
this is also checked for in finish_id_expression.

	PR c++/100482

gcc/cp/ChangeLog:

	* parser.cc (cp_parser_decltype_expr): Report errors raised by
        finish_id_expression.

gcc/testsuite/ChangeLog:

	* g++.dg/cpp0x/decltype-100482.C: New test.

Signed-off-by: Nathaniel Shead <nathanieloshead@gmail.com>
  • Loading branch information
wreien authored and ouuleilei-bot committed Aug 8, 2023
1 parent c2d62cd commit 64f4278
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
22 changes: 11 additions & 11 deletions gcc/cp/parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16508,10 +16508,6 @@ cp_parser_decltype_expr (cp_parser *parser,
expr = cp_parser_lookup_name_simple (parser, expr,
id_expr_start_token->location);

if (expr && TREE_CODE (expr) == TEMPLATE_DECL)
/* A template without args is not a complete id-expression. */
expr = error_mark_node;

if (expr
&& expr != error_mark_node
&& TREE_CODE (expr) != TYPE_DECL
Expand All @@ -16532,13 +16528,17 @@ cp_parser_decltype_expr (cp_parser *parser,
&error_msg,
id_expr_start_token->location));

if (expr == error_mark_node)
/* We found an id-expression, but it was something that we
should not have found. This is an error, not something
we can recover from, so note that we found an
id-expression and we'll recover as gracefully as
possible. */
id_expression_or_member_access_p = true;
if (error_msg)
{
/* We found an id-expression, but it was something that we
should not have found. This is an error, not something
we can recover from, so report the error we found and
we'll recover as gracefully as possible. */
cp_parser_parse_definitely (parser);
cp_parser_error (parser, error_msg);
id_expression_or_member_access_p = true;
return error_mark_node;
}
}

if (expr
Expand Down
12 changes: 12 additions & 0 deletions gcc/testsuite/g++.dg/cpp0x/decltype-100482.C
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// PR c++/100482
// { dg-do compile { target c++11 } }

namespace N {}
decltype(N) x; // { dg-error "expected primary-expression" }

struct S {};
decltype(S) y; // { dg-error "argument to .decltype. must be an expression" }

template <typename T>
struct U {};
decltype(U) z; // { dg-error "missing template arguments" }

0 comments on commit 64f4278

Please sign in to comment.