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

--- Comment #2 from GCC Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Jakub Jelinek <[email protected]>:

https://gcc.gnu.org/g:6920277db5e2587f2312a36c7d7262a7c8049b55

commit r17-7060-g6920277db5e2587f2312a36c7d7262a7c8049b55
Author: Jakub Jelinek <[email protected]>
Date:   Thu Dec 3 07:12:02 2026 +0100

    c++: Implement P3568R4 - break label; and continue label; [PR129104]

    The following patch implements the proposed
    P3568R4 - break label; and continue label;
    paper.  This is mostly something I've already implemented in the C FE
    for C2Y 2 years ago and uses the same lazy strategy, instead of always
    collecting all LABEL_DECLs which could label any statement this just
    remembers the last statement in cur_stmt_list if any before we start
    parsing the labels, and passes it down to the selection/iteration/expansion
    stmt parsing routines, where if it is one of the possibly named statements,
    it uses a function to walk back up to the last statement if any and
collects
    LABEL_DECLs it found.  Similarly to the C2Y case, this is pedwarned for
    C++26 and down.
    The second part, which is something not yet in the C2Y implementation but
    WG14 paper exists for it (dunno about its state), is a change where
    the paper now allows multiple labels with the same name in the same
    function but disallows goto (and by extension &&label) for those.  This
    part is implemented as a pure C++29 change, so for C++26 and below
    when using named loops as an extension, one simply has to use unique
    labels.  In C++29 mode when we already have some outer named loop/switch,
    we actually have to remember all parsed labels actively, not lazily,
because
    the P3568R4 wording requires rejecting e.g.
    a: while (true) while (true) a: { break a; }
    instead of breaking the outer loop.  Also, identifier labels are now in
C++29
    mode allowed in expansion statement bodies, but goto is disallowed to those
    (compared to C++26 mode which still rejects them when they are defined).

    2026-12-03  Jakub Jelinek  <[email protected]>

            P3568R4
            PR c++/129104
    gcc/c-family/
            * c-gimplify.cc (c_genericize): Delete bc_hash_map even for C++.
            * c-cppbuiltin.cc (c_cpp_builtins): Predefine __cpp_break_label
            to 202611L for C++29.
    gcc/cp/
            * parser.cc: Implement C++29 P3568R4 - break label; and continue
label;
            Include spellcheck.h.
            (cp_parser_new): Clear parser->loop_names and
parser->loop_names_hash.
            (cp_parser_lambda_expression): Temporarily override those.
            (get_before_labels): New function.
            (struct loop_names_sentinel): New RAII class.
            (cp_parser_statement): Add variable of that type.  Pass result of
            get_before_labels to cp_parser_selection_statement,
            cp_parser_iteration_statement and cp_parser_expansion_statement and
            cp_parser_pragma.
            (cp_parser_label_for_labeled_statement): Only diagnose identifier
            labels in expansion stmt body for C++26 and earlier.  For C++29
            if parser->loop_names is not empty add label to that vector and
            update parser->loop_names_hash accordingly if needed.
            (cp_update_loop_names_hash, cp_get_loop_names,
cp_release_loop_names,
            finish_bc_name): New functions.
            (cp_parser_selection_statement): Add before_labels argument.  For
            RID_SWITCH call cp_get_loop_names before parsing, set
            SWITCH_STMT_NAME, or in IN_NAMED_STMT into parser->in_statement
            before parsing body, set LABEL_DECL_LOOP_SWITCH_NAME_VALID
            before parsing switch body and clear it after that and at the end
            clear SWITCH_STMT_NAME if unused, otherwise clear
            SWITCH_STMT_NO_BREAK_P.
            (cp_parser_iteration_statement): Add before_labels argument.  Call
            cp_get_loop_names before parsing, set
{WHILE,DO,FOR,RANGE_FOR}_NAME,
            or in IN_NAMED_STMT into parser->in_statement before parsing
bodies,
            set LABEL_DECL_LOOP_SWITCH_NAME_VALID before parsing loop body and
            clear it after that and at the end clear *_NAME if unused.
            (cp_parser_expansion_statement): Add before_labels argument.  Call
            cp_get_loop_names before parsing, set TEMPLATE_FOR_NAME, or in
            IN_NAMED_STMT into parser->in_statement before parsing body, set
            LABEL_DECL_LOOP_SWITCH_NAME_VALID before parsing expansion stmt
body
            and clear it after that and at the end clear TEMPLATE_FOR_NAME if
            unused.  Fix comment typo.
            (cp_parser_jump_statement): Also ignore IN_NAMED_STMT bit.  Parse
            identifier after break or continue keywords.
            (cp_parser_declaration): Adjust cp_parser_pragma caller.
            (cp_parser_toplevel_declaration): Likewise.
            (cp_parser_member_specification_opt): Likewise.
            (cp_parser_function_definition_after_declarator): Temporarily
override
            parser->loop_names and parser->loop_names_hash.
            (cp_parser_objc_interstitial_code): Adjust cp_parser_pragma caller.
            (cp_parser_omp_loop_nest): Likewise.
            (cp_parser_omp_declare_simd): Likewise.
            (cp_maybe_parse_omp_decl): Likewise.
            (cp_parser_omp_metadirective): Likewise.
            (cp_parser_pragma): Add before_labels argument, pass it down to
            cp_parser_iteration_statement.
            * cp-tree.h (LABEL_DECL_LOOP_NAME, LABEL_DECL_SWITCH_NAME,
            LABEL_DECL_LOOP_SWITCH_NAME_VALID,
LABEL_DECL_LOOP_SWITCH_NAME_USED,
            LABEL_DECL_IN_TEMPLATE_FOR_P, RANGE_FOR_NAME, TEMPLATE_FOR_NAME):
            Define.
            (maybe_lookup_label): Declare.
            * parser.h (IN_NAMED_STMT): Define.
            (struct cp_parser): Add loop_names and loop_names_hash members.
            * pt.cc (tsubst_stmt): Handle FOR_NAME, RANGE_FOR_NAME, WHILE_NAME,
            DO_NAME, TEMPLATE_FOR_NAME, BREAK_NAME, CONTINUE_NAME,
            SWITCH_STMT_NAME.  For LABEL_EXPR with LABEL_DECL_LOOP_NAME or
            LABEL_DECL_SWITCH_NAME LABEL_DECL copy those flags and
            register_local_specialization and also copy
            LABEL_DECL_IN_TEMPLATE_FOR_P and TREE_USED flag.  Adjust
            TEMPLATE_FOR_STMT build_stmt call.
            (struct expansion_stmt_bc): Add name, break_name and in_loop
members.
            (expansion_stmt_find_bc_r): Walk WHILE_BODY, FOR_BODY, DO_BODY or
            TEMPLATE_FOR_BODY if bc_data->name, with bc_data->in_loop
temporarily
            set.  Walk SWITCH_STMT_BODY with bc_data->in_switch temporarily set
            even if bc_data->name.  Handle BREAK_NAME and CONTINUE_NAME.
            (finish_expansion_stmt): Initialize bc_data.name,
bc_data.break_name
            and bc_data.in_loop.  Create do ... while (0) loop even if just
            bc_data.break_name is non-NULL, in that case set DO_NAME.
            * decl.cc (struct named_label_entry): Add goto_locus,
other_label_locus
            and ambiguous members.
            (adjust_backward_gotos): Don't call adjust_backward_goto on
LABEL_EXPRs
            with LABEL_DECLs if they are ambiguous and not the ent->label_decl.
            (pop_labels): If label has DECL_CHAIN, call check_label_used on all
            chained labels and set DECL_CHAIN of the last one to previous
            BLOCK_VARS.
            (lookup_label): For C++29 diagnose references to ambiguous labels
            or labels in template for bodies.
            (maybe_lookup_label): New function.
            (mark_label_addressed): When setting addressed for the first time,
            also remember input_location in ent->goto_locus.
            (check_goto_1): Set ent->goto_locus if unset.
            (define_label): For C++29 don't diagnose duplicate labels with the
            exception of __label__ labels, instead diagnose previous goto or
            &&label and return a new LABEL_DECL and set ent->ambiguous.  Chain
            the new LABEL_DECL to DECL_CHAIN of ent->label_decl.  For C++29
            and labels defined in expansion stmt body diagnose previous goto to
it
            and set LABEL_DECL_IN_TEMPLATE_FOR_P flag.
            * cp-tree.def (RANGE_FOR_STMT): Add another operand -
RANGE_FOR_NAME.
            (TEMPLATE_FOR_STMT): Add another operand - TEMPLATE_FOR_NAME.
            * constexpr.cc (cxx_eval_loop_expr): For breaks in named loops,
check
            if BREAK_STMT argument matches it.  Similarly for continues in
named
            loops, though in those cases also don't clear *jump_target for
            LABEL_DECLs.  In cleanup_cond evaluate cleanup into separate
jmp_target
            and merge_jump_target afterwards.
            (cxx_eval_switch_expr): For breaks in named switches, check if
            BREAK_STMT argument matches it.
            (check_for_return_continue): For CONTINUE_NAME or BREAK_NAME set
            could_throw.
            (potential_constant_expression_1): When trying to clear
*jump_target
            at the end of handling loops, don't clear it if it is BREAK_STMT or
            CONTINUE_STMT referencing some outer named loop or switch.
            * coroutines.cc (struct replace_continue_data): New type.
            (replace_continue): Handle CONTINUE_STMT with NULL CONTINUE_NAME
when
            not nested inside of some other loop or CONTINUE_STMT with
            CONTINUE_NAME matching the FOR_NAME of outermost FOR_STMT being
            lowered.  For nested loops walk everything but their body and when
            FOR_NAME on the loop being lowered is non-NULL, also on the bodies
but
            with a flag then.  Don't ignore subtrees of ASM_EXPR.
            (await_statement_walker): Pass different argument to cp_walk_tree
with
            replace_continue.
            * semantics.cc (begin_range_for_stmt): Adjust build_stmt call for
            new RANGE_FOR_STMT operand.
    gcc/testsuite/
            * g++.dg/DRs/dr2581-1.C: Add test for __cpp_break_label.
            * g++.dg/DRs/dr2581-2.C: Likewise.
            * g++.dg/ext/label7.C: Don't expect duplicate label for C++29.
            * g++.dg/other/label1.C: Likewise
            * g++.dg/cpp26/expansion-stmt5.C: Expect no diagnostics for
            unused named labels defined in expansion stmt body.  Add further
            tests with goto to those.
            * g++.dg/warn/Wunused-label-4.C: Change dg-warning to dg-bogus
            and add another dg-bogus.
            * g++.dg/warn/Wunused-label-5.C: New test.
            * g++.dg/cpp29/named-loops1.C: New test.
            * g++.dg/cpp29/named-loops2.C: New test.
            * g++.dg/cpp29/named-loops3.C: New test.
            * g++.dg/cpp29/named-loops4.C: New test.
            * g++.dg/cpp29/named-loops5.C: New test.
            * g++.dg/cpp29/named-loops6.C: New test.
            * g++.dg/cpp29/named-loops7.C: New test.
            * g++.dg/cpp29/named-loops8.C: New test.
            * g++.dg/cpp29/named-loops9.C: New test.
            * g++.dg/cpp29/named-loops10.C: New test.
            * g++.dg/cpp29/named-loops11.C: New test.
            * g++.dg/cpp29/named-loops12.C: New test.
            * g++.dg/cpp29/named-loops13.C: New test.
            * g++.dg/cpp29/named-loops14.C: New test.
            * g++.dg/cpp29/named-loops15.C: New test.
            * g++.dg/cpp29/named-loops16.C: New test.
            * g++.dg/cpp29/named-loops17.C: New test.
            * g++.dg/cpp29/named-loops18.C: New test.
            * g++.dg/cpp29/named-loops19.C: New test.
            * g++.dg/cpp29/named-loops20.C: New test.
            * g++.dg/cpp29/feat-cxx29.C: Add test for __cpp_break_label.

    Reviewed-by: Jason Merrill <[email protected]>

Reply via email to