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

--- Comment #5 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The trunk branch has been updated by Jason Merrill <ja...@gcc.gnu.org>:

https://gcc.gnu.org/g:1d260ab0e39ea63644e3af3ab2e0db946026b5a6

commit r14-4771-g1d260ab0e39ea63644e3af3ab2e0db946026b5a6
Author: Nathaniel Shead <nathanielosh...@gmail.com>
Date:   Thu Oct 12 19:53:55 2023 +1100

    c++: indirect change of active union member in constexpr
[PR101631,PR102286]

    This patch adds checks for attempting to change the active member of a
    union by methods other than a member access expression.

    To be able to properly distinguish `*(&u.a) = ` from `u.a = `, this
    patch redoes the solution for c++/59950 to avoid extranneous *&; it
    seems that the only case that needed the workaround was when copying
    empty classes.

    This patch also ensures that constructors for a union field mark that
    field as the active member before entering the call itself; this ensures
    that modifications of the field within the constructor's body don't
    cause false positives (as these will not appear to be member access
    expressions). This means that we no longer need to start the lifetime of
    empty union members after the constructor body completes.

    As a drive-by fix, this patch also ensures that value-initialised unions
    are considered to have activated their initial member for the purpose of
    checking stores and accesses, which catches some additional mistakes
    pre-C++20.

            PR c++/101631
            PR c++/102286

    gcc/cp/ChangeLog:

            * call.cc (build_over_call): Fold more indirect refs for trivial
            assignment op.
            * class.cc (type_has_non_deleted_trivial_default_ctor): Create.
            * constexpr.cc (cxx_eval_call_expression): Start lifetime of
            union member before entering constructor.
            (cxx_eval_component_reference): Check against first member of
            value-initialised union.
            (cxx_eval_store_expression): Activate member for
            value-initialised union. Check for accessing inactive union
            member indirectly.
            * cp-tree.h (type_has_non_deleted_trivial_default_ctor):
            Forward declare.

    gcc/testsuite/ChangeLog:

            * g++.dg/cpp1y/constexpr-89336-3.C: Fix union initialisation.
            * g++.dg/cpp1y/constexpr-union6.C: New test.
            * g++.dg/cpp1y/constexpr-union7.C: New test.
            * g++.dg/cpp2a/constexpr-union2.C: New test.
            * g++.dg/cpp2a/constexpr-union3.C: New test.
            * g++.dg/cpp2a/constexpr-union4.C: New test.
            * g++.dg/cpp2a/constexpr-union5.C: New test.
            * g++.dg/cpp2a/constexpr-union6.C: New test.

    Signed-off-by: Nathaniel Shead <nathanielosh...@gmail.com>
    Reviewed-by: Jason Merrill <ja...@redhat.com>

Reply via email to