From: Nathaniel Shead Date: Fri, 15 Dec 2023 23:59:03 +0000 (+1100) Subject: c++: Fix unchecked use of CLASSTYPE_AS_BASE [PR113031] X-Git-Tag: basepoints/gcc-15~3520 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=39f9c426f58448d6df340cdccd84e05721a20921;p=thirdparty%2Fgcc.git c++: Fix unchecked use of CLASSTYPE_AS_BASE [PR113031] My previous commit (naively) assumed that a TREE_CODE of RECORD_TYPE or UNION_TYPE was sufficient for optype to be considered a "class type". However, this does not account for e.g. template type parameters of record or union type. This patch corrects to check for CLASS_TYPE_P before checking for as-base conversion. PR c++/113031 gcc/cp/ChangeLog: * constexpr.cc (cxx_fold_indirect_ref_1): Check for CLASS_TYPE before using CLASSTYPE_AS_BASE. gcc/testsuite/ChangeLog: * g++.dg/cpp0x/pr113031.C: New test. Signed-off-by: Nathaniel Shead --- diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc index e1b2d27fc36b..051f73fb73f8 100644 --- a/gcc/cp/constexpr.cc +++ b/gcc/cp/constexpr.cc @@ -5709,7 +5709,8 @@ cxx_fold_indirect_ref_1 (const constexpr_ctx *ctx, location_t loc, tree type, } /* Handle conversion to "as base" type. */ - if (CLASSTYPE_AS_BASE (optype) == type) + if (CLASS_TYPE_P (optype) + && CLASSTYPE_AS_BASE (optype) == type) return op; /* Handle conversion to an empty base class, which is represented with a diff --git a/gcc/testsuite/g++.dg/cpp0x/pr113031.C b/gcc/testsuite/g++.dg/cpp0x/pr113031.C new file mode 100644 index 000000000000..aecdc3fc4b2c --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/pr113031.C @@ -0,0 +1,34 @@ +// PR c++/113031 +// { dg-do compile } + +template struct variant; + +template +variant<_Types> __variant_cast(_Tp __rhs) { return static_cast&>(__rhs); } + +template +struct _Move_assign_base : _Types { + void operator=(_Move_assign_base __rhs) { __variant_cast<_Types>(__rhs); } +}; + +template +struct variant : _Move_assign_base<_Types> { + void emplace() { + variant __tmp; + *this = __tmp; + } +}; + +struct _Undefined_class { + struct _Nocopy_types { + void (_Undefined_class::*_M_member_pointer)(); + }; + struct function : _Nocopy_types { + struct optional { + void test03() { + variant v; + v.emplace(); + } + }; + }; +};