diff options
author | Jakub Jelinek <jakub@redhat.com> | 2023-05-09 16:05:22 +0200 |
---|---|---|
committer | Jakub Jelinek <jakub@redhat.com> | 2023-05-09 16:07:14 +0200 |
commit | 2499540e9abb55079b5f7b7ccdac97fbc63d9ab4 (patch) | |
tree | 72134a6c1280539667880bc43a5167e0da2a1259 /gcc | |
parent | 204303c81e82ddd01e7dc5a5a63719d476f9043c (diff) | |
download | gcc-2499540e9abb55079b5f7b7ccdac97fbc63d9ab4.zip gcc-2499540e9abb55079b5f7b7ccdac97fbc63d9ab4.tar.gz gcc-2499540e9abb55079b5f7b7ccdac97fbc63d9ab4.tar.bz2 |
c++: Reject pack expansion of assume attribute [PR109756]
http://eel.is/c++draft/dcl.attr#grammar-4 says
"In an attribute-list, an ellipsis may appear only if that attribute's
specification permits it."
and doesn't explicitly permit it on any standard attribute.
The https://wg21.link/p1774r8 paper which introduced assume attribute says
"We could therefore hypothetically permit the assume attribute to directly
support pack expansion:
template <int... args>
void f() {
[[assume(args >= 0)...]];
}
However, we do not propose this. It would require substantial additional work
for a very rare use case. Note that this can instead be expressed with a fold
expression, which is equivalent to the above and works out of the box without
any extra effort:
template <int... args>
void f() {
[[assume(((args >= 0) && ...))]];
}
", but as the testcase shows, GCC 13+ ICEs on assume attribute followed by
... if it contains packs.
The following patch rejects those instead of ICE and for C++17 or later
suggests using fold expressions instead (it doesn't make sense to suggest
it for C++14 and earlier when we'd error on the fold expressions).
2023-05-09 Jakub Jelinek <jakub@redhat.com>
PR c++/109756
* cp-gimplify.cc (process_stmt_assume_attribute): Diagnose pack
expansion of assume attribute.
* g++.dg/cpp23/attr-assume11.C: New test.
Diffstat (limited to 'gcc')
-rw-r--r-- | gcc/cp/cp-gimplify.cc | 10 | ||||
-rw-r--r-- | gcc/testsuite/g++.dg/cpp23/attr-assume11.C | 22 |
2 files changed, 32 insertions, 0 deletions
diff --git a/gcc/cp/cp-gimplify.cc b/gcc/cp/cp-gimplify.cc index 1216ef1..216a623 100644 --- a/gcc/cp/cp-gimplify.cc +++ b/gcc/cp/cp-gimplify.cc @@ -3267,6 +3267,16 @@ process_stmt_assume_attribute (tree std_attrs, tree statement, for (; attr; attr = lookup_attribute ("gnu", "assume", TREE_CHAIN (attr))) { tree args = TREE_VALUE (attr); + if (args && PACK_EXPANSION_P (args)) + { + auto_diagnostic_group d; + error_at (attrs_loc, "pack expansion of %qE attribute", + get_attribute_name (attr)); + if (cxx_dialect >= cxx17) + inform (attrs_loc, "use fold expression in the attribute " + "argument instead"); + continue; + } int nargs = list_length (args); if (nargs != 1) { diff --git a/gcc/testsuite/g++.dg/cpp23/attr-assume11.C b/gcc/testsuite/g++.dg/cpp23/attr-assume11.C new file mode 100644 index 0000000..df6e589 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp23/attr-assume11.C @@ -0,0 +1,22 @@ +// PR c++/109756 +// { dg-do compile { target c++11 } } + +template <int ...args> +void +foo () +{ + [[assume (1 > 0)...]]; // { dg-error "expansion pattern '\\\(1 > 0\\\)' contains no parameter packs" } + // { dg-warning "attributes at the beginning of statement are ignored" "" { target *-*-* } .-1 } + [[assume (args > 0)...]]; // { dg-error "pack expansion of 'assume' attribute" } + // { dg-message "use fold expression in the attribute argument instead" "" { target c++17 } .-1 } +#if __cpp_fold_expressions >= 201603L + [[assume (((args > 0) && ...))]]; +#endif + [[gnu::assume (1 > 0)...]]; // { dg-error "expansion pattern '\\\(1 > 0\\\)' contains no parameter packs" } + // { dg-warning "attributes at the beginning of statement are ignored" "" { target *-*-* } .-1 } + [[gnu::assume (args > 0)...]]; // { dg-error "pack expansion of 'assume' attribute" } + // { dg-message "use fold expression in the attribute argument instead" "" { target c++17 } .-1 } +#if __cpp_fold_expressions >= 201603L + [[gnu::assume (((args > 0) && ...))]]; +#endif +} |