[Bug c++/103600] Cannot use typeid result in constant expressions

cvs-commit at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Mon Jan 3 10:24:31 GMT 2022


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

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

https://gcc.gnu.org/g:134442b2178a164ed4580255a0de007dda19b855

commit r12-6183-g134442b2178a164ed4580255a0de007dda19b855
Author: Jakub Jelinek <jakub@redhat.com>
Date:   Mon Jan 3 11:21:00 2022 +0100

    c++: Support &typeid(x) == &typeid(y) and typeid(x) == typeid(y) in
constant evaluation [PR103600]

    If the tinfo vars are emitted in the current TU, they are emitted at the
end
    of the compilation, and for some types they are exported from
    libstdc++/libsupc++ and not emitted in the current TU at all.

    The following patch allows constant folding of comparisons of typeid
    addresses and makes it possible to implement P1328R1 - making type_info
    operator== constexpr (Jonathan has a patch for that).

    As mentioned in the PR, the varpool/middle-end code is trying to be
    conservative with address comparisons of different vars if those vars
    don't bind locally, because of possible aliases in other TUs etc.
    and so while match.pd folds &typeid(int) == &typeid(int) because
    it is equality comparison with the same operands, for different typeids
    it doesn't fold it.

    On Wed, Dec 08, 2021 at 08:53:03AM -0500, Jason Merrill wrote:
    > Would it make sense to assume that DECL_ARTIFICIAL variables can't be
    > aliases?  If not, could we have some way of marking a variable as
    > non-aliasing, perhaps an attribute?

    I think DECL_ARTIFICIAL vars generally can overlap.

    The following patch adds a GCC internal attribute "non overlapping"
    and uses it in symtab_node::equal_address_to.
    Not sure what plans has Honza in that area and whether it would be useful
    to make the attribute public and let users assert that some variable will
    never overlap with other variables, won't have aliases etc.

    > During constant evaluation, the operator== could compare the type_info
    > address instead of the __name address, reducing this to the previous
    > problem.

    Ah, indeed, good idea.  FYI, clang++ seems to constant fold
    &typeid(x) != &typeid(y) already, so Jonathan could use it even for
    clang++ in the constexpr operator==.  But it folds even
    extern int &a, &b;
    constexpr bool c = &a != &b;
    regardless of whether some other TU has
    int a;
    int b __attribute__((alias (a));
    or not.

    2022-01-03  Jakub Jelinek  <jakub@redhat.com>

            PR c++/103600
    gcc/
            * symtab.c (symtab_node::equal_address_to): Return 0 if one of
            VAR_DECLs has "non overlapping" attribute and rs1 != rs2.
    gcc/c-family/
            * c-attribs.c (handle_non_overlapping_attribute): New function.
            (c_common_attribute_table): Add "non overlapping" attribute.
    gcc/cp/
            * rtti.c (get_tinfo_decl_direct): Add "non overlapping" attribute
            to DECL_TINFO_P VAR_DECLs.
    gcc/testsuite/
            * g++.dg/cpp0x/constexpr-typeid2.C: New test.


More information about the Gcc-bugs mailing list