c++: build_extra_args recapturing local specs [PR114303]

r13-6452-g341e6cd8d603a3 made build_extra_args walk evaluated contexts
first so that we prefer processing a local specialization in an evaluated
context even if its first use is in an unevaluated context.  But this
means we need to avoid walking a tree that already has extra args/specs
saved because the list of saved specs appears to be an evaluated
context which we'll now walk first.  It seems then that we should be
calculating the saved specs from scratch each time, rather than
potentially walking the saved specs list from an earlier partial
instantiation when calling build_extra_args a second time around.

	PR c++/114303

gcc/cp/ChangeLog:

	* constraint.cc (tsubst_requires_expr): Clear
	REQUIRES_EXPR_EXTRA_ARGS before calling build_extra_args.
	* pt.cc (tree_extra_args): Define.
	(extract_locals_r): Assert *_EXTRA_ARGS is empty.
	(tsubst_stmt) <case IF_STMT>: Clear IF_SCOPE on the new
	IF_STMT.  Call build_extra_args on the new IF_STMT instead
	of t which might already have IF_STMT_EXTRA_ARGS.

gcc/testsuite/ChangeLog:

	* g++.dg/cpp1z/constexpr-if-lambda6.C: New test.

Reviewed-by: Jason Merrill <jason@redhat.com>
This commit is contained in:
Patrick Palka 2024-04-11 10:16:41 -04:00
parent 0dc39dee83
commit b262b17636
3 changed files with 47 additions and 1 deletions

View file

@ -2362,6 +2362,7 @@ tsubst_requires_expr (tree t, tree args, sat_info info)
matching or dguide constraint rewriting), in which case we need
to partially substitute. */
t = copy_node (t);
REQUIRES_EXPR_EXTRA_ARGS (t) = NULL_TREE;
REQUIRES_EXPR_EXTRA_ARGS (t) = build_extra_args (t, args, info.complain);
return t;
}

View file

@ -3858,6 +3858,24 @@ has_extra_args_mechanism_p (const_tree t)
&& IF_STMT_CONSTEXPR_P (t))); /* IF_STMT_EXTRA_ARGS */
}
/* Return *_EXTRA_ARGS of the given supported tree T. */
static tree&
tree_extra_args (tree t)
{
gcc_checking_assert (has_extra_args_mechanism_p (t));
if (PACK_EXPANSION_P (t))
return PACK_EXPANSION_EXTRA_ARGS (t);
else if (TREE_CODE (t) == REQUIRES_EXPR)
return REQUIRES_EXPR_EXTRA_ARGS (t);
else if (TREE_CODE (t) == IF_STMT
&& IF_STMT_CONSTEXPR_P (t))
return IF_STMT_EXTRA_ARGS (t);
gcc_unreachable ();
}
/* Structure used to track the progress of find_parameter_packs_r. */
struct find_parameter_pack_data
{
@ -13291,6 +13309,16 @@ extract_locals_r (tree *tp, int *walk_subtrees, void *data_)
/* Remember local typedefs (85214). */
tp = &TYPE_NAME (*tp);
if (has_extra_args_mechanism_p (*tp))
/* Assert *_EXTRA_ARGS is empty, because we don't want to walk it and
potentially see a previously captured local in an evaluated context
that's really only used in an unevaluated context (PR114303). This
means callers of build_extra_args need to clear *_EXTRA_ARGS of the
outermost tree. Nested *_EXTRA_ARGS should naturally be empty since
the outermost (extra-args) tree will intercept any substitution before
a nested tree can. */
gcc_checking_assert (tree_extra_args (*tp) == NULL_TREE);
if (TREE_CODE (*tp) == DECL_EXPR)
{
tree decl = DECL_EXPR_DECL (*tp);
@ -18716,10 +18744,11 @@ tsubst_stmt (tree t, tree args, tsubst_flags_t complain, tree in_decl)
of the constexpr if is still dependent. Don't substitute into the
branches now, just remember the template arguments. */
do_poplevel (IF_SCOPE (stmt));
IF_SCOPE (stmt) = NULL_TREE;
IF_COND (stmt) = IF_COND (t);
THEN_CLAUSE (stmt) = THEN_CLAUSE (t);
ELSE_CLAUSE (stmt) = ELSE_CLAUSE (t);
IF_STMT_EXTRA_ARGS (stmt) = build_extra_args (t, args, complain);
IF_STMT_EXTRA_ARGS (stmt) = build_extra_args (stmt, args, complain);
add_stmt (stmt);
break;
}

View file

@ -0,0 +1,16 @@
// PR c++/114303
// { dg-do compile { target c++17 } }
struct A { static constexpr bool value = true; };
int main() {
[](auto x1) {
return [&](auto) {
return [&](auto x3) {
if constexpr (decltype(x3)::value) {
static_assert(decltype(x1)::value);
}
}(A{});
}(0);
}(A{});
}