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

Jason Merrill jason@redhat.com
Wed Aug 28 06:58:00 GMT 2019


On Tue, Aug 27, 2019 at 4:10 PM Marek Polacek <polacek@redhat.com> wrote:
> On Fri, Aug 23, 2019 at 02:20:53PM -0700, Jason Merrill wrote:
> > 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.
>
> I didn't want to mark the decls as read multiple times but we do it anyway
> so that's no longer my concern.  So...
>
> Bootstrapped/regtested on x86_64-linux, ok for trunk?
>
> 2019-08-27  Marek Polacek  <polacek@redhat.com>
>
>         PR c++/81676 - bogus -Wunused warnings in constexpr if.
>         * semantics.c (maybe_mark_exp_read_r): New function.
>         (finish_if_stmt): Call it on THEN_CLAUSE and ELSE_CLAUSE.

I was thinking of adding mark_exp_read to places where we currently
take a shortcut in templates, like check_return_expr or
build_x_binary_op, but this is probably simpler.  The patch is OK.

Jason



More information about the Gcc-patches mailing list