public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] c++: ICE with invalid structured bindings [PR107276]
@ 2022-10-21 23:29 Marek Polacek
  2022-10-24 14:31 ` Jason Merrill
  0 siblings, 1 reply; 4+ messages in thread
From: Marek Polacek @ 2022-10-21 23:29 UTC (permalink / raw)
  To: Jason Merrill, GCC Patches

This test ICEs in C++23 because we reach the new code in do_auto_deduction:

30468   if (cxx_dialect >= cxx23
30469       && context == adc_return_type
30470       && (!AUTO_IS_DECLTYPE (auto_node)
30471           || !unparenthesized_id_or_class_member_access_p (init))
30472       && (r = treat_lvalue_as_rvalue_p (maybe_undo_parenthesized_ref (init),
30473                                         /*return*/true)))

where 'init' is "VIEW_CONVERT_EXPR<<<< error >>>>(y)", and then the move
in treat_lvalue_as_rvalue_p returns error_mark_node whereupon
set_implicit_rvalue_p crashes.

I don't think such V_C_Es are useful so let's not create them.  But that
won't fix the ICE so I'm checking the return value of move.  A structured
bindings decl can have an error type, that is set in cp_finish_decomp:

 8908           TREE_TYPE (first) = error_mark_node;

therefore I think treat_lvalue_as_rvalue_p just needs to cope.

Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?

	PR c++/107276

gcc/cp/ChangeLog:

	* typeck.cc (treat_lvalue_as_rvalue_p): Check the return value of move.

gcc/ChangeLog:

	* tree.cc (maybe_wrap_with_location): Don't create a location wrapper
	when the type is erroneous.

gcc/testsuite/ChangeLog:

	* g++.dg/cpp2a/decomp4.C: New test.
---
 gcc/cp/typeck.cc                     | 7 ++++++-
 gcc/testsuite/g++.dg/cpp2a/decomp4.C | 8 ++++++++
 gcc/tree.cc                          | 3 ++-
 3 files changed, 16 insertions(+), 2 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/cpp2a/decomp4.C

diff --git a/gcc/cp/typeck.cc b/gcc/cp/typeck.cc
index 16e7d85793d..5ca191759f6 100644
--- a/gcc/cp/typeck.cc
+++ b/gcc/cp/typeck.cc
@@ -10726,7 +10726,12 @@ treat_lvalue_as_rvalue_p (tree expr, bool return_p)
   if (DECL_CONTEXT (retval) != current_function_decl)
     return NULL_TREE;
   if (return_p)
-    return set_implicit_rvalue_p (move (expr));
+    {
+      expr = move (expr);
+      if (expr == error_mark_node)
+	return NULL_TREE;
+      return set_implicit_rvalue_p (expr);
+    }
 
   /* if the operand of a throw-expression is a (possibly parenthesized)
      id-expression that names an implicitly movable entity whose scope does not
diff --git a/gcc/testsuite/g++.dg/cpp2a/decomp4.C b/gcc/testsuite/g++.dg/cpp2a/decomp4.C
new file mode 100644
index 00000000000..28b3f172b53
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp2a/decomp4.C
@@ -0,0 +1,8 @@
+// PR c++/107276
+// { dg-do run { target c++20 } }
+
+auto f(auto x) {
+  auto [y] = x; // { dg-error "cannot decompose" }
+  return y;
+}
+int i = f(0);
diff --git a/gcc/tree.cc b/gcc/tree.cc
index 81a6ceaf181..4e5b1df4d85 100644
--- a/gcc/tree.cc
+++ b/gcc/tree.cc
@@ -14352,7 +14352,8 @@ maybe_wrap_with_location (tree expr, location_t loc)
 
   /* For now, don't add wrappers to exceptional tree nodes, to minimize
      any impact of the wrapper nodes.  */
-  if (EXCEPTIONAL_CLASS_P (expr))
+  if (EXCEPTIONAL_CLASS_P (expr)
+      || (TREE_TYPE (expr) && EXCEPTIONAL_CLASS_P (TREE_TYPE (expr))))
     return expr;
 
   /* Compiler-generated temporary variables don't need a wrapper.  */

base-commit: d155442de043c1bef7d27cf2d6be4eba618afcb9
-- 
2.37.3


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

end of thread, other threads:[~2022-10-24 17:31 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-10-21 23:29 [PATCH] c++: ICE with invalid structured bindings [PR107276] Marek Polacek
2022-10-24 14:31 ` Jason Merrill
2022-10-24 17:13   ` [PATCH v2] " Marek Polacek
2022-10-24 17:31     ` 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).