From: Marek Polacek Date: Wed, 11 Apr 2018 13:10:16 +0000 (+0000) Subject: re PR c++/85032 (Wrong non-constant condition for static assertion) X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=fe0604d349c4d9eb18b17dd018383d591eed67c7;p=gcc.git re PR c++/85032 (Wrong non-constant condition for static assertion) PR c++/85032 * constexpr.c (potential_constant_expression_1): Consider conversions from classes to literal types potentially constant. * g++.dg/cpp0x/pr51225.C: Adjust error message. * g++.dg/cpp1z/constexpr-if21.C: New test. From-SVN: r259318 --- diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index 78e7ed77b45..e9a32b0d204 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,9 @@ +2018-04-11 Marek Polacek + + PR c++/85032 + * constexpr.c (potential_constant_expression_1): Consider conversions + from classes to literal types potentially constant. + 2018-04-10 Paolo Carlini PR c++/70808 diff --git a/gcc/cp/constexpr.c b/gcc/cp/constexpr.c index 3cc196b4d17..75f56df4465 100644 --- a/gcc/cp/constexpr.c +++ b/gcc/cp/constexpr.c @@ -5777,6 +5777,25 @@ potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now, TREE_TYPE (t)); return false; } + /* This might be a conversion from a class to a (potentially) literal + type. Let's consider it potentially constant since the conversion + might be a constexpr user-defined conversion. */ + else if (cxx_dialect >= cxx11 + && (dependent_type_p (TREE_TYPE (t)) + || !COMPLETE_TYPE_P (TREE_TYPE (t)) + || literal_type_p (TREE_TYPE (t))) + && TREE_OPERAND (t, 0)) + { + tree type = TREE_TYPE (TREE_OPERAND (t, 0)); + /* If this is a dependent type, it could end up being a class + with conversions. */ + if (type == NULL_TREE || WILDCARD_TYPE_P (type)) + return true; + /* Or a non-dependent class which has conversions. */ + else if (CLASS_TYPE_P (type) + && (TYPE_HAS_CONVERSION (type) || dependent_scope_p (type))) + return true; + } return (RECUR (TREE_OPERAND (t, 0), TREE_CODE (TREE_TYPE (t)) != REFERENCE_TYPE)); diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 8deaeb95974..186986aafe4 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,9 @@ +2018-04-11 Marek Polacek + + PR c++/85032 + * g++.dg/cpp0x/pr51225.C: Adjust error message. + * g++.dg/cpp1z/constexpr-if21.C: New test. + 2018-04-11 Jakub Jelinek PR target/85281 diff --git a/gcc/testsuite/g++.dg/cpp0x/pr51225.C b/gcc/testsuite/g++.dg/cpp0x/pr51225.C index f80bd0e778e..5b4e432f7ed 100644 --- a/gcc/testsuite/g++.dg/cpp0x/pr51225.C +++ b/gcc/testsuite/g++.dg/cpp0x/pr51225.C @@ -5,7 +5,7 @@ template struct A {}; template void foo() { - A a; // { dg-error "not declared|invalid type" } + A a; // { dg-error "not declared|could not convert" } } template struct bar diff --git a/gcc/testsuite/g++.dg/cpp1z/constexpr-if21.C b/gcc/testsuite/g++.dg/cpp1z/constexpr-if21.C new file mode 100644 index 00000000000..56e108be4ad --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1z/constexpr-if21.C @@ -0,0 +1,22 @@ +// PR c++/85032 +// { dg-options -std=c++17 } + +struct A +{ + constexpr operator bool () { return true; } + int i; +}; + +A a; + +template +void f() +{ + constexpr bool b = a; + static_assert (a); +} + +int main() +{ + f(); +}