This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: C++ PATCH for c++/81676 - bogus -Wunused warnings in constexpr if


On 8/19/19 11:28 AM, Marek Polacek wrote:
On Fri, Aug 16, 2019 at 06:20:20PM -0700, Jason Merrill wrote:
On 8/16/19 2:29 PM, Marek Polacek wrote:
This patch is an attempt to fix the annoying -Wunused-but-set-* warnings that
tend to occur with constexpr if.  When we have something like

    template < typename T >
    int f(T v){
        if constexpr(sizeof(T) == sizeof(int)){
	  return v;
        }else{
	  return 0;
        }
    }

and call f('a'), then the condition is false, meaning that we won't instantiate
the then-branch, as per tsubst_expr/IF_STMT:
17284       if (IF_STMT_CONSTEXPR_P (t) && integer_zerop (tmp))
17285         /* Don't instantiate the THEN_CLAUSE. */;
so we'll never get round to mark_exp_read-ing the decls used in the
then-branch, causing finish_function to emit "parameter set but not used"
warnings.

It's unclear how to best deal with this.  Marking the decls DECL_READ_P while
parsing doesn't seem like a viable approach

Why not?

Well, while parsing, we're in a template and so the condition won't be
evaluated until tsubst_expr.  So we can't tell which branch is dead.

But if a decl is used on one branch, we shouldn't warn even if it isn't used on the selected branch.

Jason


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]