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

--- Comment #17 from GCC Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Jakub Jelinek <ja...@gcc.gnu.org>:

https://gcc.gnu.org/g:cb46aca0a07355abf2f0b04f52087bca8f848524

commit r14-9910-gcb46aca0a07355abf2f0b04f52087bca8f848524
Author: Jakub Jelinek <ja...@redhat.com>
Date:   Thu Apr 11 09:46:00 2024 +0200

    c++: Fix ANNOTATE_EXPR instantiation [PR114409]

    The following testcase ICEs starting with the r14-4229 PR111529
    change which moved ANNOTATE_EXPR handling from tsubst_expr to
    tsubst_copy_and_build.
    ANNOTATE_EXPR is only allowed in the IL to wrap a loop condition,
    and the loop condition of while/for loops can be a COMPOUND_EXPR
    with DECL_EXPR in the first operand and the corresponding VAR_DECL
    in the second, as created by finish_cond
          else if (!empty_expr_stmt_p (cond))
            expr = build2 (COMPOUND_EXPR, TREE_TYPE (expr), cond, expr);
    Since then Patrick reworked the instantiation, so that we have now
    tsubst_stmt and tsubst_expr and ANNOTATE_EXPR ended up in the latter,
    while only tsubst_stmt can handle DECL_EXPR.

    Now, the reason why the while/for loops with variable declaration
    in the condition works in templates without the pragmas (i.e. without
    ANNOTATE_EXPR) is that both the FOR_STMT and WHILE_STMT handling uses
    RECUR aka tsubst_stmt in handling of the *_COND operand:
        case FOR_STMT:
          stmt = begin_for_stmt (NULL_TREE, NULL_TREE);
          RECUR (FOR_INIT_STMT (t));
          finish_init_stmt (stmt);
          tmp = RECUR (FOR_COND (t));
          finish_for_cond (tmp, stmt, false, 0, false);
    and
        case WHILE_STMT:
          stmt = begin_while_stmt ();
          tmp = RECUR (WHILE_COND (t));
          finish_while_stmt_cond (tmp, stmt, false, 0, false);
    Therefore, it will handle DECL_EXPR embedded in COMPOUND_EXPR of the
    {WHILE,FOR}_COND just fine.
    But if that COMPOUND_EXPR with DECL_EXPR is wrapped with one or more
    ANNOTATE_EXPRs, because ANNOTATE_EXPR is now done solely in tsubst_expr
    and uses RECUR there (i.e. tsubst_expr), it will ICE on DECL_EXPR in there.

    This could be fixed by keeping ANNOTATE_EXPR handling in tsubst_expr but
    using tsubst_stmt for the first operand, but this patch instead
    moves ANNOTATE_EXPR handling to tsubst_stmt (and uses tsubst_expr for the
    second/third operand).

    2024-04-11  Jakub Jelinek  <ja...@redhat.com>

            PR c++/114409
            * pt.cc (tsubst_expr) <case ANNOTATE_EXPR>: Move to ...
            (tsubst_stmt) <case ANNOTATE_EXPR>: ... here.  Use tsubst_expr
            instead of RECUR for the last 2 arguments.

            * g++.dg/ext/pr114409-2.C: New test.

Reply via email to