Here we're rejecting the use of the captured 't' (of empty type) as a
template argument ultimately because convert_nontype_argument checks
potentiality using is_constant_expression which returns false for
captured variables since want_rval=false.  But in this case an
lvalue-to-rvalue conversion of the argument is implied, so I believe we
should be checking is_rvalue_constant_expression.

This patch defines an rvalue version of is_nondep_const_expr and uses
it in convert_nontype_argument when the target type is a non-reference.

Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look right?
Alternatively, since 'non_dep' in convert_nontype_argument controls only
whether we should call instantiate_non_dependent_expr, it seems weird to
me that we care about potentiality at all.  So I experimented with using
instantiation_dependent_expression_p here instead, and that seemed to
work well and even fixed the dg-ice'd test cpp1z/constexpr-lambda26.C.
In r12-7564-gec0f53a3a542e7 we made a similar change to decltype.

        PR c++/107437

gcc/cp/ChangeLog:

        * constexpr.cc (is_nondependent_rvalue_constant_expression):
        Define.
        * cp-tree.h (is_nondependent_rvalue_constant_expression):
        Declare.
        * pt.cc (convert_nontype_argument): Use it instead of the
        non-rvalue version when converting to a non-reference type.

gcc/testsuite/ChangeLog:

        * g++.dg/cpp1y/lambda-generic-107437.C: New test.
---
 gcc/cp/constexpr.cc                           | 10 +++++++++
 gcc/cp/cp-tree.h                              |  1 +
 gcc/cp/pt.cc                                  |  4 +++-
 .../g++.dg/cpp1y/lambda-generic-107437.C      | 22 +++++++++++++++++++
 4 files changed, 36 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/g++.dg/cpp1y/lambda-generic-107437.C

diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc
index e43d92864f5..77e0e261cca 100644
--- a/gcc/cp/constexpr.cc
+++ b/gcc/cp/constexpr.cc
@@ -10139,6 +10139,16 @@ is_nondependent_constant_expression (tree t)
          && !instantiation_dependent_expression_p (t));
 }
 
+/* As above, but expect an rvalue.  */
+
+bool
+is_nondependent_rvalue_constant_expression (tree t)
+{
+  return (!type_unknown_p (t)
+         && is_rvalue_constant_expression (t)
+         && !instantiation_dependent_expression_p (t));
+}
+
 /* Returns true if T is a potential static initializer expression that is not
    instantiation-dependent.  */
 
diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h
index 0d6c234b3b0..2d4c5cc3ebd 100644
--- a/gcc/cp/cp-tree.h
+++ b/gcc/cp/cp-tree.h
@@ -8471,6 +8471,7 @@ extern bool potential_constant_expression       (tree);
 extern bool is_constant_expression (tree);
 extern bool is_rvalue_constant_expression (tree);
 extern bool is_nondependent_constant_expression (tree);
+extern bool is_nondependent_rvalue_constant_expression (tree);
 extern bool is_nondependent_static_init_expression (tree);
 extern bool is_static_init_expression    (tree);
 extern bool is_std_allocator (tree);
diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
index bc566ab702b..87caea5f202 100644
--- a/gcc/cp/pt.cc
+++ b/gcc/cp/pt.cc
@@ -7318,7 +7318,9 @@ convert_nontype_argument (tree type, tree expr, 
tsubst_flags_t complain)
       && has_value_dependent_address (expr))
     /* If we want the address and it's value-dependent, don't fold.  */;
   else if (processing_template_decl
-          && is_nondependent_constant_expression (expr))
+          && (TYPE_REF_P (type)
+              ? is_nondependent_constant_expression (expr)
+              : is_nondependent_rvalue_constant_expression (expr)))
     non_dep = true;
   if (error_operand_p (expr))
     return error_mark_node;
diff --git a/gcc/testsuite/g++.dg/cpp1y/lambda-generic-107437.C 
b/gcc/testsuite/g++.dg/cpp1y/lambda-generic-107437.C
new file mode 100644
index 00000000000..934f863659d
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp1y/lambda-generic-107437.C
@@ -0,0 +1,22 @@
+// PR c++/107437
+// { dg-do compile { target c++11 } }
+
+struct integral_constant {
+  constexpr operator int() const { return 42; }
+};
+
+template<int N>
+struct A {
+  static constexpr int value = N;
+};
+
+template<class T>
+void f(T t) {
+  [=](auto) {
+    A<t> a; // { dg-bogus "constant" }
+    return a.value;
+  }(0);
+}
+
+template void f(integral_constant);
+
-- 
2.39.0.56.g57e2c6ebbe

Reply via email to