public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* C++ PATCH for c++/91678 - wrong error with decltype and location wrapper
@ 2019-09-06  2:25 Marek Polacek
  2019-09-13  2:26 ` Marek Polacek
  2019-09-15 14:18 ` Jason Merrill
  0 siblings, 2 replies; 7+ messages in thread
From: Marek Polacek @ 2019-09-06  2:25 UTC (permalink / raw)
  To: GCC Patches, Jason Merrill, David Malcolm, Nathan Sidwell

Compiling this testcase results in a bogus "invalid cast" error; this occurs
since the introduction of location wrappers in finish_id_expression.

Here we are parsing the decltype expression via cp_parser_decltype_expr which
can lead to calling various fold_* and c-family routines.  They use
non_lvalue_loc, but that won't create a NON_LVALUE_EXPR wrapper around a location
wrapper.

So before the location wrappers addition cp_parser_decltype_expr would return
NON_LVALUE_EXPR <c>.  Now it returns VIEW_CONVERT_EXPR<float *>(c), but the
STRIP_ANY_LOCATION_WRAPPER immediately following it strips the location wrapper,
and suddenly we don't know whether we have an lvalue anymore.  And that's sad
because then decltype produces the wrong type, causing nonsense errors.

Bootstrapped/regtested on x86_64-linux, ok for trunk and 9?

2019-09-05  Marek Polacek  <polacek@redhat.com>

	PR c++/91678 - wrong error with decltype and location wrapper.
	* parser.c (cp_parser_decltype): Use auto_suppress_location_wrappers
	sentinel.  Don't strip location wrappers.

	* g++.dg/cpp0x/decltype73.C: New test.

diff --git gcc/cp/parser.c gcc/cp/parser.c
index baa60b8834e..b3c7bff5988 100644
--- gcc/cp/parser.c
+++ gcc/cp/parser.c
@@ -14729,8 +14729,13 @@ cp_parser_decltype (cp_parser *parser)
       /* Do not warn about problems with the expression.  */
       ++c_inhibit_evaluation_warnings;
 
+      /* Don't create wrapper nodes within decltype.  non_lvalue_loc won't
+	 create a NON_LVALUE_EXPR wrapper around a location wrapper, and a
+	 subsequent STRIP_ANY_LOCATION_WRAPPER would destroy the information
+	 about lvalueness of the expression.  */
+      auto_suppress_location_wrappers sentinel;
+
       expr = cp_parser_decltype_expr (parser, id_expression_or_member_access_p);
-      STRIP_ANY_LOCATION_WRAPPER (expr);
 
       /* Go back to evaluating expressions.  */
       --cp_unevaluated_operand;
diff --git gcc/testsuite/g++.dg/cpp0x/decltype73.C gcc/testsuite/g++.dg/cpp0x/decltype73.C
new file mode 100644
index 00000000000..cbe94a898e3
--- /dev/null
+++ gcc/testsuite/g++.dg/cpp0x/decltype73.C
@@ -0,0 +1,4 @@
+// PR c++/91678 - wrong error with decltype and location wrapper.
+// { dg-do compile { target c++11 } }
+
+float* test(float* c) { return (decltype(c + 0))(float*)c; }

^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2019-12-07  4:03 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-09-06  2:25 C++ PATCH for c++/91678 - wrong error with decltype and location wrapper Marek Polacek
2019-09-13  2:26 ` Marek Polacek
2019-09-15 14:18 ` Jason Merrill
2019-09-16 18:12   ` Marek Polacek
2019-09-18  3:59     ` Jason Merrill
2019-12-07  0:19       ` Marek Polacek
2019-12-07  4:03         ` Jason Merrill

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).