https://gcc.gnu.org/bugzilla/show_bug.cgi?id=120005
--- Comment #5 from GCC Commits <cvs-commit at gcc dot gnu.org> --- The master branch has been updated by Nathaniel Shead <[email protected]>: https://gcc.gnu.org/g:c388d56a889469cf05a1512403e3267af7bb62be commit r16-6431-gc388d56a889469cf05a1512403e3267af7bb62be Author: Nathaniel Shead <[email protected]> Date: Fri Nov 7 23:24:17 2025 +1100 c++: Fold non-ODR usages of potentially constant values early [PR120005] [basic.link] p14.4 says that a declaration naming a TU-local entity is an exposure, ignoring "any reference to a non-volatile const object or reference with internal or no linkage initialized with a constant expression that is not an odr-use". To implement this, we cannot stream these entities but must fold them into their underlying values beforehand. This was already done to a degree by cp_fold, but it didn't handle all cases, and notably was not performed on the saved body of a constexpr function, which would then cause errors during modules streaming. This patch implements this by supplementing cp_fold with additional rules to fold non-ODR usages of contants. We need to do this as an additional walk before saving the constexpr function definition, so we also disable as much other folding during this walk as possible to prevent removing any information that the constexpr interpreter requires to function correctly. With this we still will error on uses in templates. In general it's impossible to tell within an uninstantiated template body whether a reference is an ODR-use in the face of dependent expressions, so we don't attempt to do anything for this case. PR c++/119097 PR c++/120005 gcc/cp/ChangeLog: * constexpr.cc (potential_constant_expression_1): Fall back to location from parent expression if needed. * cp-gimplify.cc (enum fold_flags): Add ff_only_non_odr. (cp_fold_data::cp_fold_data): Assert invariant for flags. (cp_fold_omp_clause_refs_r): New function. (cp_fold_r): Specially handle OMP_CLAUSE_DECL. (cp_fold_function_non_odr_use): New function. (cp_fold_non_odr_use_1): New function. (cp_fold_maybe_rvalue): Fold non-ODR uses when requested. (cp_fold_non_odr_use): New function. (fold_caches): Increase number of caches. (get_fold_cache): Use a new cache for non-ODR use walks. (cp_fold): Skip most folding for non-ODR use walks; always fold constant-initialized references; remove dead code to fold __builtin_source_location. * cp-tree.h (cp_fold_function_non_odr_use): Declare. (cp_fold_non_odr_use): Declare. * decl.cc (finish_function): Fold non-ODR uses before saving constexpr fundef. Invoke PLUGIN_PRE_GENERICIZE before this folding. * ptree.cc (cxx_print_xnode): Handle TU_LOCAL_ENTITY. * tree.cc (bot_manip): Propagate TREE_CONSTANT. * typeck2.cc (digest_nsdmi_init): Fold non-ODR uses in NSDMIs. gcc/testsuite/ChangeLog: * g++.dg/cpp0x/constexpr-cast.C: Adjust diagnostics. * g++.dg/warn/overflow-warn-1.C: Fix diagnostic checks. * g++.dg/warn/overflow-warn-3.C: Likewise. * g++.dg/warn/overflow-warn-4.C: Likewise. * g++.dg/modules/internal-8_a.C: Remove xfails, supplement with additional testcases. * g++.dg/modules/internal-8_b.C: New test. Signed-off-by: Nathaniel Shead <[email protected]> Reviewed-by: Jason Merrill <[email protected]>
