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; + expr = convert_like (conv, expr, complain); + } else expr = error_mark_node; diff --git gcc/cp/decl.c gcc/cp/decl.c index c04b9b7d457..8da63fa2aaa 100644 --- gcc/cp/decl.c +++ gcc/cp/decl.c @@ -9508,6 +9508,8 @@ compute_array_index_type (tree name, tree size, tsubst_flags_t complain) else { size = instantiate_non_dependent_expr_sfinae (size, complain); + /* Don't warn about narrowing for VLAs. */ + warning_sentinel s (warn_narrowing, !TREE_CONSTANT (osize)); size = build_converted_constant_expr (size_type_node, size, complain); size = maybe_constant_value (size); @@ -9556,7 +9558,7 @@ compute_array_index_type (tree name, tree size, tsubst_flags_t complain) { tree folded = cp_fully_fold (size); if (TREE_CODE (folded) == INTEGER_CST) - pedwarn (location_of (size), OPT_Wpedantic, + pedwarn (input_location, OPT_Wpedantic, "size of array is not an integral constant-expression"); /* Use the folded result for VLAs, too; it will have resolved SIZEOF_EXPR. */ 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; expr = maybe_constant_value (expr); expr = convert_from_reference (expr); } diff --git gcc/cp/typeck2.c gcc/cp/typeck2.c index 91aa5a62856..d82e3b608da 100644 --- gcc/cp/typeck2.c +++ gcc/cp/typeck2.c @@ -875,7 +875,8 @@ store_init_value (tree decl, tree init, vec<tree, va_gc>** cleanups, int flags) } -/* Give diagnostic about narrowing conversions within { }. */ +/* Give diagnostic about narrowing conversions within { }, or as part of + a converted constant expression. */ bool check_narrowing (tree type, tree init, tsubst_flags_t complain) @@ -967,7 +968,7 @@ check_narrowing (tree type, tree init, tsubst_flags_t complain) { if (complain & tf_warning) warning_at (loc, OPT_Wnarrowing, "narrowing conversion of %qE " - "from %qH to %qI inside { } is ill-formed in C++11", + "from %qH to %qI is ill-formed in C++11", init, ftype, type); ok = true; } @@ -977,8 +978,7 @@ check_narrowing (tree type, tree init, tsubst_flags_t complain) { if ((!almost_ok || pedantic) && pedwarn (loc, OPT_Wnarrowing, - "narrowing conversion of %qE " - "from %qH to %qI inside { }", + "narrowing conversion of %qE from %qH to %qI", init, ftype, type) && almost_ok) inform (loc, " the expression has a constant value but is not " @@ -991,8 +991,8 @@ check_narrowing (tree type, tree init, tsubst_flags_t complain) int savederrorcount = errorcount; global_dc->pedantic_errors = 1; pedwarn (loc, OPT_Wnarrowing, - "narrowing conversion of %qE from %qH to %qI " - "inside { }", init, ftype, type); + "narrowing conversion of %qE from %qH to %qI ", + init, ftype, type); if (errorcount == savederrorcount) ok = true; global_dc->pedantic_errors = flag_pedantic_errors; diff --git gcc/testsuite/g++.dg/cpp0x/Wnarrowing6.C gcc/testsuite/g++.dg/cpp0x/Wnarrowing6.C index e69de29bb2d..989d277cd00 100644 --- gcc/testsuite/g++.dg/cpp0x/Wnarrowing6.C +++ gcc/testsuite/g++.dg/cpp0x/Wnarrowing6.C @@ -0,0 +1,8 @@ +// PR c++/57891 +// { dg-do compile { target c++11 } } + +template<unsigned int> struct A {}; +A<-1> a; // { dg-error "narrowing conversion" } + +template<signed char> struct B {}; +B<1000> b; // { dg-error "narrowing conversion" } diff --git gcc/testsuite/g++.dg/cpp0x/Wnarrowing7.C gcc/testsuite/g++.dg/cpp0x/Wnarrowing7.C index e69de29bb2d..099fdfb7d81 100644 --- gcc/testsuite/g++.dg/cpp0x/Wnarrowing7.C +++ gcc/testsuite/g++.dg/cpp0x/Wnarrowing7.C @@ -0,0 +1,9 @@ +// PR c++/57891 +// { dg-do compile { target c++11 } } +// { dg-options "-Wno-narrowing" } + +template<unsigned int> struct A {}; +A<-1> a; + +template<signed char> struct B {}; +B<1000> b; // { dg-warning "overflow" } diff --git gcc/testsuite/g++.dg/cpp0x/Wnarrowing8.C gcc/testsuite/g++.dg/cpp0x/Wnarrowing8.C index e69de29bb2d..39c924c9c6c 100644 --- gcc/testsuite/g++.dg/cpp0x/Wnarrowing8.C +++ gcc/testsuite/g++.dg/cpp0x/Wnarrowing8.C @@ -0,0 +1,6 @@ +// PR c++/57891 +// { dg-do compile { target c++11 } } + +struct X { constexpr operator int () { return 1000; } }; +template<signed char> struct C {}; +C<X{}> c; // { dg-error "narrowing conversion" } diff --git gcc/testsuite/g++.dg/cpp0x/constexpr-data2.C gcc/testsuite/g++.dg/cpp0x/constexpr-data2.C index 898102167de..dee5ed82301 100644 --- gcc/testsuite/g++.dg/cpp0x/constexpr-data2.C +++ gcc/testsuite/g++.dg/cpp0x/constexpr-data2.C @@ -43,4 +43,4 @@ extern template struct A3<int, 510>; // Use. A3<int, 1111> a31; -A3<char, 9999> a32; // { dg-warning "overflow" } +A3<char, 9999> a32; // { dg-error "narrowing conversion" } diff --git gcc/testsuite/g++.dg/ext/builtin12.C gcc/testsuite/g++.dg/ext/builtin12.C index 1d6bb75cd77..489b37777c4 100644 --- gcc/testsuite/g++.dg/ext/builtin12.C +++ gcc/testsuite/g++.dg/ext/builtin12.C @@ -5,6 +5,6 @@ template<bool> struct A {}; constexpr int foo() { - A<__builtin_constant_p(0)> a{}; + A<__builtin_constant_p(0)> a{}; // { dg-error "narrowing conversion" } return 0; } diff --git gcc/testsuite/g++.dg/init/new43.C gcc/testsuite/g++.dg/init/new43.C index 9b0866720fe..7ab2a36392e 100644 --- gcc/testsuite/g++.dg/init/new43.C +++ gcc/testsuite/g++.dg/init/new43.C @@ -31,35 +31,35 @@ void test_literal () // Verify integer literal. p = new char [-1]; // { dg-error "size of array is negative" } - p = new char [2][-3]; // { dg-error "size of array is negative" } + p = new char [2][-3]; // { dg-error "size of array is negative|narrowing conversion" } p = new char [-4][5]; // { dg-error "size of array is negative" } - p = new char [-6][-7]; // { dg-error "size of array is negative" } + p = new char [-6][-7]; // { dg-error "size of array is negative|narrowing conversion" } p = new (p) char [-1]; // { dg-error "size of array is negative" } - p = new (p) char [2][-3]; // { dg-error "size of array is negative" } + p = new (p) char [2][-3]; // { dg-error "size of array is negative|narrowing conversion" } p = new (p) char [-4][5]; // { dg-error "size of array is negative" } - p = new (p) char [-6][-7]; // { dg-error "size of array is negative" } + p = new (p) char [-6][-7]; // { dg-error "size of array is negative|narrowing conversion" } p = new (p) A [-1]; // { dg-error "size of array is negative" } - p = new (p) A [2][-3]; // { dg-error "size of array is negative" } + p = new (p) A [2][-3]; // { dg-error "size of array is negative|narrowing conversion" } p = new (p) A [-4][5]; // { dg-error "size of array is negative" } - p = new (p) A [-6][-7]; // { dg-error "size of array is negative" } + p = new (p) A [-6][-7]; // { dg-error "size of array is negative|narrowing conversion" } p = new (p) B [-1]; // { dg-error "size of array is negative" } - p = new (p) B [2][-3]; // { dg-error "size of array is negative" } + p = new (p) B [2][-3]; // { dg-error "size of array is negative|narrowing conversion" } p = new (p) B [-4][5]; // { dg-error "size of array is negative" } - p = new (p) B [-6][-7]; // { dg-error "size of array is negative" } + p = new (p) B [-6][-7]; // { dg-error "size of array is negative|narrowing conversion" } p = new (&b) B [-1]; // { dg-error "size of array is negative" } - p = new (&b) B [2][-3]; // { dg-error "size of array is negative" } + p = new (&b) B [2][-3]; // { dg-error "size of array is negative|narrowing conversion" } p = new (&b) B [-4][5]; // { dg-error "size of array is negative" } - p = new (&b) B [-6][-7]; // { dg-error "size of array is negative" } + p = new (&b) B [-6][-7]; // { dg-error "size of array is negative|narrowing conversion" } p = new char [1 - 2]; // { dg-error "size of array is negative" } p = new (p) char [2 - 3]; // { dg-error "size of array is negative" } p = new A [2 < 1 ? -1 : -2]; // { dg-error "size of array is negative" } p = new (p) B [2 - 3 * 2]; // { dg-error "size of array is negative" } - p = new (&b) B [1][2 - 3 * 2];// { dg-error "size of array is negative" } + p = new (&b) B [1][2 - 3 * 2];// { dg-error "size of array|narrowing conversion" } } void test_constant_expression () @@ -79,35 +79,35 @@ void test_constant_expression () // Verify constant expression. p = new char [i1]; // { dg-error "size of array is negative" } - p = new char [2][i3]; // { dg-error "size of array is negative" } + p = new char [2][i3]; // { dg-error "size of array is|narrowing conversion" } p = new char [i4][5]; // { dg-error "size of array is negative" } - p = new char [i6][i7]; // { dg-error "size of array is negative" } + p = new char [i6][i7]; // { dg-error "size of array is|narrowing conversion" } p = new (p) char [i1]; // { dg-error "size of array is negative" } - p = new (p) char [2][i3]; // { dg-error "size of array is negative" } + p = new (p) char [2][i3]; // { dg-error "size of array is|narrowing conversion" } p = new (p) char [i4][5]; // { dg-error "size of array is negative" } - p = new (p) char [i6][i7]; // { dg-error "size of array is negative" } + p = new (p) char [i6][i7]; // { dg-error "size of array is|narrowing conversion" } p = new (p) A [i1]; // { dg-error "size of array is negative" } - p = new (p) A [2][i3]; // { dg-error "size of array is negative" } + p = new (p) A [2][i3]; // { dg-error "size of array is|narrowing conversion" } p = new (p) A [i4][5]; // { dg-error "size of array is negative" } - p = new (p) A [i6][i7]; // { dg-error "size of array is negative" } + p = new (p) A [i6][i7]; // { dg-error "size of array is|narrowing conversion" } p = new (p) B [i1]; // { dg-error "size of array is negative" } - p = new (p) B [2][i3]; // { dg-error "size of array is negative" } + p = new (p) B [2][i3]; // { dg-error "size of array is|narrowing conversion" } p = new (p) B [i4][5]; // { dg-error "size of array is negative" } - p = new (p) B [i6][i7]; // { dg-error "size of array is negative" } + p = new (p) B [i6][i7]; // { dg-error "size of array is|narrowing conversion" } p = new (&b) B [i1]; // { dg-error "size of array is negative" } - p = new (&b) B [2][i3]; // { dg-error "size of array is negative" } + p = new (&b) B [2][i3]; // { dg-error "size of array is|narrowing conversion" } p = new (&b) B [i4][5]; // { dg-error "size of array is negative" } - p = new (&b) B [i6][i7]; // { dg-error "size of array is negative" } + p = new (&b) B [i6][i7]; // { dg-error "size of array is|narrowing conversion" } p = new short [i1 - 2]; // { dg-error "size of array is negative" } p = new (p) bool [i2 - 3]; // { dg-error "size of array is negative" } p = new A [2 < 1 ? i1 : i2]; // { dg-error "size of array is negative" } p = new (p) B [2 + i3 * 2]; // { dg-error "size of array is negative" } - p = new (&b) B [1][i1 - 3 * 2];// { dg-error "size of array is negative" } + p = new (&b) B [1][i1 - 3 * 2];// { dg-error "size of array|narrowing conversion" } } void test_constexpr () diff --git gcc/testsuite/g++.dg/other/fold1.C gcc/testsuite/g++.dg/other/fold1.C index 23d34546e0b..bf074038b04 100644 --- gcc/testsuite/g++.dg/other/fold1.C +++ gcc/testsuite/g++.dg/other/fold1.C @@ -4,5 +4,5 @@ struct A { static const int i = i; // { dg-error "not declared" } - int x[i]; // { dg-error "constant-expression" } + int x[i]; // { dg-error "constant-expression|narrowing conversion" } }; diff --git gcc/testsuite/g++.dg/other/vrp1.C gcc/testsuite/g++.dg/other/vrp1.C index 0a798c9954e..466a15b4cbb 100644 --- gcc/testsuite/g++.dg/other/vrp1.C +++ gcc/testsuite/g++.dg/other/vrp1.C @@ -9,4 +9,4 @@ long long mod (long long l, long long r) return 0LL; return l % r; } -template long long mod<-0x8000000000000000LL> (long long, long long); +template long long mod<-0x8000000000000000LL> (long long, long long); // { dg-error "template-id" "" { target { c++11 } } } diff --git gcc/testsuite/g++.dg/parse/array-size2.C gcc/testsuite/g++.dg/parse/array-size2.C index d0bc47fe746..997b95eed1a 100644 --- gcc/testsuite/g++.dg/parse/array-size2.C +++ gcc/testsuite/g++.dg/parse/array-size2.C @@ -14,7 +14,7 @@ extern void bar (char *, char *); void foo (void) { - char g[(char *) &((struct S *) 0)->b - (char *) 0]; // { dg-error "constant" } + char g[(char *) &((struct S *) 0)->b - (char *) 0]; // { dg-error "constant|narrowing conversion" } char h[(__SIZE_TYPE__) &((struct S *) 8)->b]; // { dg-error "constant" } bar (g, h); } diff --git gcc/testsuite/g++.dg/template/char1.C gcc/testsuite/g++.dg/template/char1.C index 51e72e7ad06..a6cffaaf024 100644 --- gcc/testsuite/g++.dg/template/char1.C +++ gcc/testsuite/g++.dg/template/char1.C @@ -1,4 +1,5 @@ template <class CharType, CharType line_terminator = 0> class String {}; -String<signed char, 255> s; // { dg-warning "overflow" } +String<signed char, 255> s; // { dg-error "narrowing conversion" "" { target c++11 } } +// { dg-warning "overflow" "" { target c++98_only } .-1 } diff --git gcc/testsuite/g++.dg/template/dependent-name3.C gcc/testsuite/g++.dg/template/dependent-name3.C index bbe6fb66266..f9d14055a11 100644 --- gcc/testsuite/g++.dg/template/dependent-name3.C +++ gcc/testsuite/g++.dg/template/dependent-name3.C @@ -11,7 +11,7 @@ template<int I> struct A template<int N> struct B { int x[A<N>::zero]; // { dg-error "zero" } - int y[A<N>::minus_one]; // { dg-error "negative" } + int y[A<N>::minus_one]; // { dg-error "size of array|narrowing conversion" } }; B<0> b;