summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorjason <jason@138bc75d-0d04-0410-961f-82ee72b054a4>2017-08-09 20:17:55 +0000
committerjason <jason@138bc75d-0d04-0410-961f-82ee72b054a4>2017-08-09 20:17:55 +0000
commitd176193543720f850849adaed571852c767083f3 (patch)
tree88230b46cdcd8f45ad943e18381eaf05344e6a10
parent012a99d33ea8acb5e220a4bbae2577a9afc2dc6b (diff)
downloadgcc-d176193543720f850849adaed571852c767083f3.tar.gz
PR c++/81525 - wrong constant value with generic lambda
* pt.c (tsubst_decl) [VAR_DECL]: Avoid clobbering auto. (tsubst_copy) [VAR_DECL]: Handle auto. git-svn-id: svn+ssh://gcc.gnu.org/svn/gcc/branches/gcc-7-branch@251001 138bc75d-0d04-0410-961f-82ee72b054a4
-rw-r--r--gcc/cp/ChangeLog6
-rw-r--r--gcc/cp/pt.c12
-rw-r--r--gcc/testsuite/g++.dg/cpp1y/lambda-generic-const4.C20
3 files changed, 38 insertions, 0 deletions
diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog
index a385276ff40..aa1a9d8bfe2 100644
--- a/gcc/cp/ChangeLog
+++ b/gcc/cp/ChangeLog
@@ -1,3 +1,9 @@
+2017-08-09 Jason Merrill <jason@redhat.com>
+
+ PR c++/81525 - wrong constant value with generic lambda
+ * pt.c (tsubst_decl) [VAR_DECL]: Avoid clobbering auto.
+ (tsubst_copy) [VAR_DECL]: Handle auto.
+
2017-08-09 Leonid Koppel <lkoppel@uwaterloo.ca>
PR c++/67054 - Inherited ctor with non-default-constructible members
diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c
index d1c846ecf44..f6afcf20edf 100644
--- a/gcc/cp/pt.c
+++ b/gcc/cp/pt.c
@@ -12896,7 +12896,15 @@ tsubst_decl (tree t, tree args, tsubst_flags_t complain)
&& VAR_HAD_UNKNOWN_BOUND (t)
&& type != error_mark_node)
type = strip_array_domain (type);
+ tree auto_node = type_uses_auto (type);
+ int len = TREE_VEC_LENGTH (args);
+ if (auto_node)
+ /* Mask off any template args past the variable's context so we
+ don't replace the auto with an unrelated argument. */
+ TREE_VEC_LENGTH (args) = TEMPLATE_TYPE_LEVEL (auto_node) - 1;
type = tsubst (type, args, complain, in_decl);
+ if (auto_node)
+ TREE_VEC_LENGTH (args) = len;
}
if (VAR_P (r))
{
@@ -14687,6 +14695,10 @@ tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl)
DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (r)
= TREE_CONSTANT (r) = true;
DECL_INITIAL (r) = init;
+ if (tree auto_node = type_uses_auto (TREE_TYPE (r)))
+ TREE_TYPE (r)
+ = do_auto_deduction (TREE_TYPE (r), init, auto_node,
+ complain, adc_variable_type);
}
gcc_assert (cp_unevaluated_operand || TREE_STATIC (r)
|| decl_constant_var_p (r)
diff --git a/gcc/testsuite/g++.dg/cpp1y/lambda-generic-const4.C b/gcc/testsuite/g++.dg/cpp1y/lambda-generic-const4.C
new file mode 100644
index 00000000000..52f4373ccbd
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp1y/lambda-generic-const4.C
@@ -0,0 +1,20 @@
+// PR c++/81525
+// { dg-do compile { target c++14 } }
+
+template <int i> struct A {
+ constexpr operator int () const { return i; }
+};
+template <int i> constexpr A<i> a = {};
+
+template <typename F> void foo (F f) {
+ f (A<0>{});
+}
+template <typename T>
+void bar (T) {
+ constexpr auto N = a<1>;
+ auto f = [&] (auto i) {
+ static_assert (static_cast<int>(N) == 1, "");
+ };
+ foo (f);
+}
+int main () { bar (0); }