From adc06f45fbef12b06cdbb5603a372d82dca1baec Mon Sep 17 00:00:00 2001 From: Marek Polacek Date: Fri, 5 Apr 2019 21:22:40 +0000 Subject: [PATCH] PR c++/87145 - bogus error converting class type in template arg list. * pt.c (convert_nontype_argument): Don't call build_converted_constant_expr if it could involve calling a conversion function with a instantiation-dependent constructor as its argument. * g++.dg/cpp0x/constexpr-conv3.C: New test. * g++.dg/cpp0x/constexpr-conv4.C: New test. From-SVN: r270178 --- gcc/cp/ChangeLog | 7 ++++++ gcc/cp/pt.c | 13 ++++++++++ gcc/testsuite/ChangeLog | 6 +++++ gcc/testsuite/g++.dg/cpp0x/constexpr-conv3.C | 25 ++++++++++++++++++++ gcc/testsuite/g++.dg/cpp0x/constexpr-conv4.C | 25 ++++++++++++++++++++ 5 files changed, 76 insertions(+) create mode 100644 gcc/testsuite/g++.dg/cpp0x/constexpr-conv3.C create mode 100644 gcc/testsuite/g++.dg/cpp0x/constexpr-conv4.C diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index 17c9aa712aa..f766f476f10 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,10 @@ +2019-04-05 Marek Polacek + + PR c++/87145 - bogus error converting class type in template arg list. + * pt.c (convert_nontype_argument): Don't call + build_converted_constant_expr if it could involve calling a conversion + function with a instantiation-dependent constructor as its argument. + 2019-04-05 Martin Sebor PR bootstrap/89980 diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c index 40d954d1e8a..f8001317bda 100644 --- a/gcc/cp/pt.c +++ b/gcc/cp/pt.c @@ -6837,6 +6837,19 @@ convert_nontype_argument (tree type, tree expr, tsubst_flags_t complain) else if (INTEGRAL_OR_ENUMERATION_TYPE_P (type) || cxx_dialect >= cxx17) { + /* Calling build_converted_constant_expr might create a call to + a conversion function with a value-dependent argument, which + could invoke taking the address of a temporary representing + the result of the conversion. */ + if (COMPOUND_LITERAL_P (expr) + && CONSTRUCTOR_IS_DEPENDENT (expr) + && MAYBE_CLASS_TYPE_P (expr_type) + && TYPE_HAS_CONVERSION (expr_type)) + { + expr = build1 (IMPLICIT_CONV_EXPR, type, expr); + IMPLICIT_CONV_EXPR_NONTYPE_ARG (expr) = true; + return expr; + } /* C++17: A template-argument for a non-type template-parameter shall be a converted constant expression (8.20) of the type of the template-parameter. */ diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index dc6f911fc4f..b854b575077 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,9 @@ +2019-04-05 Marek Polacek + + PR c++/87145 - bogus error converting class type in template arg list. + * g++.dg/cpp0x/constexpr-conv3.C: New test. + * g++.dg/cpp0x/constexpr-conv4.C: New test. + 2019-04-05 Martin Sebor PR bootstrap/89980 diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-conv3.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-conv3.C new file mode 100644 index 00000000000..3f47c58cd2a --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-conv3.C @@ -0,0 +1,25 @@ +// PR c++/87145 +// { dg-do compile { target c++11 } } + +template struct integral_constant { + static constexpr T value = t; +}; + +enum class Enum : unsigned {}; + +struct Pod { + unsigned val; + + constexpr operator Enum() const { + return static_cast(val); + } +}; + +template +constexpr void foo() { + using Foo = integral_constant; +} + +int main() { + foo<2>(); +} diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-conv4.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-conv4.C new file mode 100644 index 00000000000..f4e3f00a585 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-conv4.C @@ -0,0 +1,25 @@ +// PR c++/87145 +// { dg-do compile { target c++11 } } + +struct S { + int val; + + constexpr operator int() const { + return static_cast(val); + } +}; + +template +struct F { }; + +template +constexpr void foo() { + F f; + F f2; +} + +int +main() +{ + foo<2>(); +} -- 2.30.2