On Mon, Mar 08, 2021 at 06:10:05PM -0700, Martin Sebor wrote:
> On 3/5/21 3:03 PM, Jason Merrill via Gcc-patches wrote:
> > On 3/4/21 9:37 PM, Marek Polacek wrote:
> > > This PR complains that we issue a -Wconversion warning in
> > >
> > > template <int N> struct X {};
> > > template <class T> X<sizeof(T)> foo();
> > >
> > > saying "conversion from 'long unsigned int' to 'int' may change value".
> > > While it's not technically wrong, I suspect -Wconversion warnings aren't
> > > all that useful for value-dependent expressions. So this patch disables
> > > them, though I'm open to other ideas.
> >
> > How about suppressing -Wconversion in
> > build_converted_constant_expr_internal? If the size_t value ended up
> > being too large for the int parameter, we would give an error about
> > overflow in a constant expression, not just a warning.
>
> I was going to suggest to continue to issue some warning in contexts
> where the result of the conversion cannot be represented in the type
> but not otherwise. As in:
>
> template <char N> struct X { };
> template <class T> X<sizeof(T)> foo();
>
> void bar ()
> {
> foo<char[256]>();
> }
>
> Does your suggestion have the same effect?
Either of the patches I've posted suppresses the -Wconversion warning
in this test. But that should be ok -- rather, we should probably
reject the conversion in check_narrowing. Which we currently don't.
It looks like we just fold the SIZEOF_EXPR to 0 and never detect the
overflow.
Marek