public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r12-71] c++: Prevent bogus -Wtype-limits warning with NTTP [PR100161]
@ 2021-04-22 21:37 Marek Polacek
  0 siblings, 0 replies; only message in thread
From: Marek Polacek @ 2021-04-22 21:37 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:244dfb95119106e9267f37583caac565c39eb0ec

commit r12-71-g244dfb95119106e9267f37583caac565c39eb0ec
Author: Marek Polacek <polacek@redhat.com>
Date:   Tue Apr 20 20:24:09 2021 -0400

    c++: Prevent bogus -Wtype-limits warning with NTTP [PR100161]
    
    Recently, we made sure that we never call value_dependent_expression_p
    on an expression that isn't potential_constant_expression.  That caused
    this bogus warning with a non-type template parameter, something that
    users don't want to see.
    
    The problem is that in tsubst_copy_and_build/LE_EXPR 't' is "i < n",
    which, due to 'i', is not p_c_e, therefore we call t_d_e_p.  But the
    type of 'n' isn't dependent, so we think the whole 't' expression is
    not dependent.  It seems we need to test both op0 and op1 separately
    to suppress this warning.
    
    gcc/cp/ChangeLog:
    
            PR c++/100161
            * pt.c (tsubst_copy_and_build) <case PLUS_EXPR>: Test op0 and
            op1 separately for value- or type-dependence.
    
    gcc/testsuite/ChangeLog:
    
            PR c++/100161
            * g++.dg/warn/Wtype-limits6.C: New test.

Diff:
---
 gcc/cp/pt.c                               | 24 +++++++++++++++---------
 gcc/testsuite/g++.dg/warn/Wtype-limits6.C | 17 +++++++++++++++++
 2 files changed, 32 insertions(+), 9 deletions(-)

diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c
index 7bcbe6dc3ce..8d64fef957d 100644
--- a/gcc/cp/pt.c
+++ b/gcc/cp/pt.c
@@ -19906,15 +19906,21 @@ tsubst_copy_and_build (tree t,
     case MEMBER_REF:
     case DOTSTAR_EXPR:
       {
-	/* If T was type-dependent, suppress warnings that depend on the range
-	   of the types involved.  */
-	++processing_template_decl;
-	const bool was_dep = (potential_constant_expression (t)
-			      ? value_dependent_expression_p (t)
-			      : type_dependent_expression_p (t));
-	--processing_template_decl;
-	tree op0 = RECUR (TREE_OPERAND (t, 0));
-	tree op1 = RECUR (TREE_OPERAND (t, 1));
+	/* If either OP0 or OP1 was value- or type-dependent, suppress
+	   warnings that depend on the range of the types involved.  */
+	tree op0 = TREE_OPERAND (t, 0);
+	tree op1 = TREE_OPERAND (t, 1);
+	auto dep_p = [](tree t) {
+	  ++processing_template_decl;
+	  bool r = (potential_constant_expression (t)
+		    ? value_dependent_expression_p (t)
+		    : type_dependent_expression_p (t));
+	  --processing_template_decl;
+	  return r;
+	};
+	const bool was_dep = dep_p (op0) || dep_p (op1);
+	op0 = RECUR (op0);
+	op1 = RECUR (op1);
 
 	warning_sentinel s1(warn_type_limits, was_dep);
 	warning_sentinel s2(warn_div_by_zero, was_dep);
diff --git a/gcc/testsuite/g++.dg/warn/Wtype-limits6.C b/gcc/testsuite/g++.dg/warn/Wtype-limits6.C
new file mode 100644
index 00000000000..9d5886d5323
--- /dev/null
+++ b/gcc/testsuite/g++.dg/warn/Wtype-limits6.C
@@ -0,0 +1,17 @@
+// PR c++/100161
+// { dg-additional-options "-Wtype-limits" }
+
+void f(unsigned);
+
+template<unsigned n>
+void g()
+{
+    for (unsigned i = 0; i < n; i++) { // { dg-bogus "always false" }
+        f(i);
+    }
+}
+
+void h()
+{
+    g<0>();
+}


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2021-04-22 21:37 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-04-22 21:37 [gcc r12-71] c++: Prevent bogus -Wtype-limits warning with NTTP [PR100161] Marek Polacek

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).