From 244dfb95119106e9267f37583caac565c39eb0ec Mon Sep 17 00:00:00 2001 From: Marek Polacek Date: Tue, 20 Apr 2021 20:24:09 -0400 Subject: [PATCH] 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) : Test op0 and op1 separately for value- or type-dependence. gcc/testsuite/ChangeLog: PR c++/100161 * g++.dg/warn/Wtype-limits6.C: New test. --- gcc/cp/pt.c | 24 +++++++++++++++--------- gcc/testsuite/g++.dg/warn/Wtype-limits6.C | 17 +++++++++++++++++ 2 files changed, 32 insertions(+), 9 deletions(-) create mode 100644 gcc/testsuite/g++.dg/warn/Wtype-limits6.C diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c index 7bcbe6d..8d64fef 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 0000000..9d5886d --- /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 +void g() +{ + for (unsigned i = 0; i < n; i++) { // { dg-bogus "always false" } + f(i); + } +} + +void h() +{ + g<0>(); +} -- 2.7.4