https://gcc.gnu.org/bugzilla/show_bug.cgi?id=127131

contact.yiran.wang at gmail dot com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |contact.yiran.wang at gmail 
dot co
                   |                            |m

--- Comment #2 from contact.yiran.wang at gmail dot com ---
Created attachment 66369
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=66369&action=edit
Proposed fix with testcase

Analysis: outside of a template, the body of the expansion statement is parsed
as if in a template, so item.value() takes the address of the original
for-range-declaration decl (implicit object argument) and marks it
TREE_ADDRESSABLE.  After lowering (a fresh decl per iteration),
finish_expansion_stmt (the args == NULL_TREE path) sets TREE_TYPE of the
original decl to error_mark_node, but the dead decl stays in BIND_EXPR_VARS of
the enclosing scope.  Being TREE_ADDRESSABLE, gimplify_bind_expr then emits an
end-of-scope clobber "item = {CLOBBER(eos)}" whose type is error_mark_node, and
verify_gimple_assign_single ICEs in useless_type_conversion_p.  Reading a data
member instead of calling a member function doesn't take the address and
doesn't ICE; release-checking builds compile it fine.

Another reduced testcase (same ICE, still on trunk 20261201):

struct lit {
  const char *p;
  constexpr const char *c_str() const { return p; }
};
constexpr lit arr[] = {{"a"}, {"bits"}};
const char *f() {
  template for (constexpr lit n : arr) {
    return n.c_str();
  }
  return nullptr;
}
int main() { return !f(); }

$ g++ -std=c++26 -c pr.C
pr.C: In function 'const char* f()':
pr.C:6:13: internal compiler error: tree check: expected class 'type', have
'exceptional' (error_mark) in useless_type_conversion_p, at gimple-expr.cc:84

The attached patch (clear TREE_ADDRESSABLE where the type is set to
error_mark_node, for the range decl and for structured-binding decls) fixes
both testcases.  Tested on x86_64-linux on a tree based on trunk
871ec62eab32: dg.exp=cpp26/expansion-stmt* has no failures, and the new run
test (g++.dg/cpp26/expansion-stmt46.C, covering constexpr and non-constexpr
range decls, std::array, std::tuple, structured bindings and the template
case) passes with -std=c++17/20/23/26; an unpatched compiler ICEs on it.

It does not fix PR127640, which is the same dead decl reaching the coroutine
frame / ipa-polymorphic-call; that probably wants the dead decls removed from
BIND_EXPR_VARS instead.

The pt.cc part:

--- a/gcc/cp/pt.cc
+++ b/gcc/cp/pt.cc
@@ -34907,7 +34907,14 @@ finish_expansion_stmt (tree expansion_stmt, tree args,
     add_stmt (pop_stmt_list (stmt_list));
   if (args == NULL_TREE)
     {
+      /* The original for-range-declaration decls stay in the
+        BIND_EXPR_VARS of the enclosing scope, but are dead after the
+        lowering above.  The body was parsed referring to them, so they
+        can be TREE_ADDRESSABLE (e.g. from the implicit object argument
+        of a member function call); clear that, otherwise the gimplifier
+        emits an end-of-scope clobber with error_mark_node type.  */
       TREE_TYPE (range_decl) = error_mark_node;
+      TREE_ADDRESSABLE (range_decl) = 0;
       if (DECL_HAS_VALUE_EXPR_P (range_decl))
        {
          SET_DECL_VALUE_EXPR (range_decl, NULL_TREE);
@@ -34920,6 +34927,7 @@ finish_expansion_stmt (tree expansion_stmt, tree args,
            {
              tree d = TREE_VEC_ELT (v, i);
              TREE_TYPE (d) = error_mark_node;
+             TREE_ADDRESSABLE (d) = 0;
              if (DECL_HAS_VALUE_EXPR_P (d))
                {
                  SET_DECL_VALUE_EXPR (d, NULL_TREE);

Reply via email to