gcc/gcc/cp
Nathaniel Shead 1d260ab0e3 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 <nathanieloshead@gmail.com>
Reviewed-by: Jason Merrill <jason@redhat.com>
2023-10-19 23:25:46 -04:00
..
call.cc c++: indirect change of active union member in constexpr [PR101631,PR102286] 2023-10-19 23:25:46 -04:00
cfns.gperf Update copyright years. 2023-01-16 11:52:17 +01:00
cfns.h Update copyright years. 2023-01-16 11:52:17 +01:00
ChangeLog Daily bump. 2023-10-20 00:16:39 +00:00
ChangeLog-1993
ChangeLog-1994
ChangeLog-1995
ChangeLog-1996
ChangeLog-1997
ChangeLog-1998
ChangeLog-1999
ChangeLog-2000
ChangeLog-2001
ChangeLog-2002
ChangeLog-2003
ChangeLog-2004
ChangeLog-2005
ChangeLog-2006
ChangeLog-2007
ChangeLog-2008
ChangeLog-2009
ChangeLog-2010
ChangeLog-2011
ChangeLog-2012
ChangeLog-2013
ChangeLog-2014
ChangeLog-2015
ChangeLog-2016
ChangeLog-2017
ChangeLog-2018
ChangeLog-2019
ChangeLog-2020
ChangeLog-2021
ChangeLog-2022 Rotate ChangeLog files. 2023-01-01 16:20:13 +01:00
ChangeLog.ptr
ChangeLog.tree-ssa
class.cc c++: indirect change of active union member in constexpr [PR101631,PR102286] 2023-10-19 23:25:46 -04:00
config-lang.in Update copyright years. 2023-01-16 11:52:17 +01:00
constexpr.cc c++: indirect change of active union member in constexpr [PR101631,PR102286] 2023-10-19 23:25:46 -04:00
constraint.cc c++: improve fold-expr location 2023-10-16 11:11:30 -04:00
contracts.cc c++: Allow translations of check_postcondition_result messages [PR109309] 2023-03-28 17:49:23 +02:00
contracts.h Update copyright years. 2023-01-16 11:52:17 +01:00
coroutines.cc c++: use _P() defines from tree.h 2023-05-18 21:49:02 +02:00
cp-gimplify.cc c++: small tweak for cp_fold_r 2023-10-19 16:18:14 -04:00
cp-lang.cc Update copyright years. 2023-01-16 11:52:17 +01:00
cp-name-hint.h c++: provide #include hint for missing includes [PR110164] 2023-06-23 17:56:14 -04:00
cp-objcp-common.cc Update copyright years. 2023-01-16 11:52:17 +01:00
cp-objcp-common.h Update copyright years. 2023-01-16 11:52:17 +01:00
cp-trait.def c++: Define built-in for std::tuple_element [PR100157] 2023-04-19 15:36:34 -04:00
cp-tree.def Update copyright years. 2023-01-16 11:52:17 +01:00
cp-tree.h c++: indirect change of active union member in constexpr [PR101631,PR102286] 2023-10-19 23:25:46 -04:00
cp-ubsan.cc Update copyright years. 2023-01-16 11:52:17 +01:00
cvt.cc c++: overeager type completion in convert_to_void [PR111419] 2023-09-19 08:29:39 -04:00
cxx-pretty-print.cc c++: improve class NTTP object pretty printing [PR111471] 2023-09-20 12:09:36 -04:00
cxx-pretty-print.h Update copyright years. 2023-01-16 11:52:17 +01:00
cxxapi-data.csv Update copyright years. 2023-01-16 11:52:17 +01:00
decl2.cc c++: missing SFINAE in grok_array_decl [PR111493] 2023-09-22 06:27:48 -04:00
decl.cc c++: use G_ instead of _ 2023-10-19 11:30:03 -04:00
decl.h Update copyright years. 2023-01-16 11:52:17 +01:00
dump.cc Update copyright years. 2023-01-16 11:52:17 +01:00
error.cc c++: print source code in print_instantiation_partial_context_line 2023-10-03 19:46:33 -04:00
except.cc c++: enable NRVO from inner block [PR51571] 2023-06-06 21:31:31 -04:00
expr.cc Update copyright years. 2023-01-16 11:52:17 +01:00
friend.cc c++: over-eager friend matching [PR109649] 2023-05-03 15:24:52 -04:00
g++spec.cc Update copyright years. 2023-01-16 11:52:17 +01:00
gen-cxxapi-file.py Update copyright years. 2023-01-16 11:52:17 +01:00
init.cc frontend: Add novector C++ pragma 2023-08-04 13:50:53 +01:00
lambda.cc c++: use _P() defines from tree.h 2023-05-18 21:49:02 +02:00
lang-specs.h Fix driver/33980: Precompiled header file not removed on error 2023-05-19 17:58:54 +00:00
lex.cc Update copyright years. 2023-01-16 11:52:17 +01:00
logic.cc Update copyright years. 2023-01-16 11:52:17 +01:00
Make-lang.in Remove .PHONY targets when building .fda files during autoprofiledbootstrap 2023-09-29 18:13:03 -07:00
mangle.cc c++: mangling tweaks 2023-10-17 17:20:02 -04:00
mapper-client.cc c++modules: report module mapper files as a dependency 2023-09-19 17:32:23 -04:00
mapper-client.h c++modules: report module mapper files as a dependency 2023-09-19 17:32:23 -04:00
mapper-resolver.cc Update copyright years. 2023-01-16 11:52:17 +01:00
method.cc c++: extend cold, hot attributes to classes 2023-09-19 15:03:28 -04:00
module.cc middle-end: Allow _BitInt(65535) [PR102989] 2023-10-14 09:35:44 +02:00
name-lookup.cc openmp: Add omp::decl attribute support [PR111392] 2023-09-20 08:43:02 +02:00
name-lookup.h c++: Diagnose [basic.scope.block]/2 violations even in compound-stmt of function-try-block [PR52953] 2023-09-05 17:26:59 +02:00
operators.def Update copyright years. 2023-01-16 11:52:17 +01:00
optimize.cc c++: inherited constructor attributes 2023-06-28 00:42:12 -04:00
parser.cc c++: use G_ instead of _ 2023-10-19 11:30:03 -04:00
parser.h openmp: Add omp::decl attribute support [PR111392] 2023-09-20 08:43:02 +02:00
pt.cc c++: improve fold-expr location 2023-10-16 11:11:30 -04:00
ptree.cc c++: fix cxx_print_type's template-info dumping 2023-09-19 14:38:10 -04:00
rtti.cc c++: Fix up ICE in emit_support_tinfo_1 [PR109042] 2023-03-07 16:28:20 +01:00
search.cc c++: look for empty base at specific offset [PR109678] 2023-05-02 16:24:20 -04:00
semantics.cc c++: improve fold-expr location 2023-10-16 11:11:30 -04:00
std-name-hint.gperf Update copyright years. 2023-01-16 11:52:17 +01:00
std-name-hint.h Update copyright years. 2023-01-16 11:52:17 +01:00
tree.cc c++: Move consteval folding to cp_fold_r 2023-09-19 09:24:44 -04:00
type-utils.h Update copyright years. 2023-01-16 11:52:17 +01:00
typeck2.cc diagnostic: rename new permerror overloads 2023-10-19 11:44:13 -04:00
typeck.cc c++: non-dependent assignment checking [PR63198, PR18474] 2023-09-18 14:47:52 -04:00
vtable-class-hierarchy.cc Update copyright years. 2023-01-16 11:52:17 +01:00