]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: Fix -Wtype-limits in templates.
authorJason Merrill <jason@redhat.com>
Thu, 30 Jan 2020 18:12:05 +0000 (13:12 -0500)
committerJason Merrill <jason@redhat.com>
Thu, 30 Jan 2020 18:47:48 +0000 (13:47 -0500)
When instantiating a template tsubst_copy_and_build suppresses -Wtype-limits
warnings about e.g. == always being false because it might not always be
false for an instantiation with other template arguments.  But we should
warn if the operands don't depend on template arguments.

PR c++/82521
* pt.c (tsubst_copy_and_build) [EQ_EXPR]: Only suppress warnings if
the expression was dependent before substitution.

gcc/cp/ChangeLog
gcc/cp/pt.c
gcc/testsuite/g++.dg/warn/Wtype-limits3.C [new file with mode: 0644]

index a402b975ce3bab09e9cfed0781f2fd70fc94333b..f260d5d5d809d5dcc679b3bac204f05f1713f464 100644 (file)
@@ -1,3 +1,9 @@
+2020-01-29  Jason Merrill  <jason@redhat.com>
+
+       PR c++/82521
+       * pt.c (tsubst_copy_and_build) [EQ_EXPR]: Only suppress warnings if
+       the expression was dependent before substitution.
+
 2020-01-30  Bin Cheng  <bin.cheng@linux.alibaba.com>
 
        * coroutines.cc (act_des_fn): New.
index 416ff63ca3ccd76a3fe9b5c2cd414aed86ce1ee3..40ff3c3a089ca547fc148ce3fcc8a347bfb37c54 100644 (file)
@@ -19279,10 +19279,14 @@ tsubst_copy_and_build (tree t,
     case MEMBER_REF:
     case DOTSTAR_EXPR:
       {
-       warning_sentinel s1(warn_type_limits);
-       warning_sentinel s2(warn_div_by_zero);
-       warning_sentinel s3(warn_logical_op);
-       warning_sentinel s4(warn_tautological_compare);
+       /* If T was type-dependent, suppress warnings that depend on the range
+          of the types involved.  */
+       bool was_dep = uses_template_parms (t);
+       warning_sentinel s1(warn_type_limits, was_dep);
+       warning_sentinel s2(warn_div_by_zero, was_dep);
+       warning_sentinel s3(warn_logical_op, was_dep);
+       warning_sentinel s4(warn_tautological_compare, was_dep);
+
        tree op0 = RECUR (TREE_OPERAND (t, 0));
        tree op1 = RECUR (TREE_OPERAND (t, 1));
        tree r = build_x_binary_op
diff --git a/gcc/testsuite/g++.dg/warn/Wtype-limits3.C b/gcc/testsuite/g++.dg/warn/Wtype-limits3.C
new file mode 100644 (file)
index 0000000..b9059ac
--- /dev/null
@@ -0,0 +1,13 @@
+// PR c++/82521
+// { dg-additional-options "-Wtype-limits" }
+
+template <typename T>
+const char * g(const unsigned char value)
+{
+  return value == -1 ? "-1" : "no"; // { dg-warning "always false" }
+}
+
+int main()
+{
+  g<int>(-1);
+}