On Wed, Jun 27, 2018 at 12:53 PM, Marek Polacek <pola...@redhat.com> wrote: > This PR complains about us accepting invalid code like > > template<unsigned int> struct A {}; > A<-1> a; > > Where we should detect the narrowing: [temp.arg.nontype] says > "A template-argument for a non-type template-parameter shall be a converted > constant expression ([expr.const]) of the type of the template-parameter." > and a converted constant expression can contain only > - integral conversions other than narrowing conversions, > - [...]." > It spurred e.g. > <https://stackoverflow.com/questions/28184888/how-implicit-conversion-works-for-non-type-template-parameters> > and has >=3 dups so it has some visibility. > > I think build_converted_constant_expr needs to set check_narrowing. > check_narrowing also always mentions that it's in { } but that is no longer > true; in the future it will also apply to <=>. We'd probably have to add a > new > flag to struct conversion if wanted to distinguish between these. > > This does not yet fix detecting narrowing in function templates (78244). > > Bootstrapped/regtested on x86_64-linux, ok for trunk? > > 2018-06-27 Marek Polacek <pola...@redhat.com> > > PR c++/57891 > * call.c (build_converted_constant_expr): Set check_narrowing. > * decl.c (compute_array_index_type): Add warning sentinel. Use > input_location. > * pt.c (convert_nontype_argument): Return NULL_TREE if any errors > were reported. > * typeck2.c (check_narrowing): Don't mention { } in diagnostic. > > * g++.dg/cpp0x/Wnarrowing6.C: New test. > * g++.dg/cpp0x/Wnarrowing7.C: New test. > * g++.dg/cpp0x/Wnarrowing8.C: New test. > * g++.dg/cpp0x/constexpr-data2.C: Add dg-error. > * g++.dg/init/new43.C: Adjust dg-error. > * g++.dg/other/fold1.C: Likewise. > * g++.dg/parse/array-size2.C: Likewise. > * g++.dg/other/vrp1.C: Add dg-error. > * g++.dg/template/char1.C: Likewise. > * g++.dg/ext/builtin12.C: Likewise. > * g++.dg/template/dependent-name3.C: Adjust dg-error. > > diff --git gcc/cp/call.c gcc/cp/call.c > index 209c1fd2f0e..956c7b149dc 100644 > --- gcc/cp/call.c > +++ gcc/cp/call.c > @@ -4152,7 +4152,10 @@ build_converted_constant_expr (tree type, tree expr, > tsubst_flags_t complain) > } > > if (conv) > - expr = convert_like (conv, expr, complain); > + { > + conv->check_narrowing = !processing_template_decl;
Why !processing_template_decl? This needs a comment. > + expr = convert_like (conv, expr, complain); > + } > else > expr = error_mark_node; > > diff --git gcc/cp/pt.c gcc/cp/pt.c > index 3780f3492aa..12d1a1e1cd3 100644 > --- gcc/cp/pt.c > +++ gcc/cp/pt.c > @@ -6669,9 +6669,10 @@ convert_nontype_argument (tree type, tree expr, > tsubst_flags_t complain) > /* C++17: A template-argument for a non-type template-parameter > shall > be a converted constant expression (8.20) of the type of the > template-parameter. */ > + int errs = errorcount; > expr = build_converted_constant_expr (type, expr, complain); > if (expr == error_mark_node) > - return error_mark_node; > + return errorcount > errs ? NULL_TREE : error_mark_node; I suspect that what you want here is to check (complain & tf_error) rather than errorcount. Otherwise it needs a comment. Jason