Normally we check for incomplete type in start_decl, but that obviously doesn't work for auto variables. Thanks to Pokechu22 for the analysis and testcases:
"When cp_finish_decl calls cp_apply_type_quals_to_decl on a const auto or constexpr auto variable, the type might not be complete the first time (this happened when auto deduces to an initializer_list). cp_apply_type_quals_to_decl removes the const qualifier if the type is not complete, which is appropriate for grokdeclarator, on the assumption that the type will be complete when called by cp_finish_decl." Tested x86_64-pc-linux-gnu, applying to trunk. PR c++/80351 gcc/cp/ChangeLog: * decl.cc (cp_finish_decl): Check completeness of deduced type. gcc/testsuite/ChangeLog: * g++.dg/cpp0x/constexpr-77482.C: Adjust message. * g++.dg/cpp1y/auto-fn27.C: Likewise. * g++.dg/cpp1y/lambda-generic-variadic22.C: Likewise. * g++.dg/cpp1z/decomp54.C: Likewise. * g++.dg/cpp0x/initlist-const1.C: New test. * g++.dg/warn/Wunused-var-37.C: New test. * g++.dg/warn/Wunused-var-38.C: New test. * g++.dg/warn/Wunused-var-39.C: New test. --- gcc/cp/decl.cc | 11 ++++ gcc/testsuite/g++.dg/cpp0x/constexpr-77482.C | 2 +- gcc/testsuite/g++.dg/cpp0x/initlist-const1.C | 7 ++ gcc/testsuite/g++.dg/cpp1y/auto-fn27.C | 2 +- .../g++.dg/cpp1y/lambda-generic-variadic22.C | 2 +- gcc/testsuite/g++.dg/cpp1z/decomp54.C | 4 +- gcc/testsuite/g++.dg/warn/Wunused-var-37.C | 64 +++++++++++++++++++ gcc/testsuite/g++.dg/warn/Wunused-var-38.C | 16 +++++ gcc/testsuite/g++.dg/warn/Wunused-var-39.C | 16 +++++ 9 files changed, 119 insertions(+), 5 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp0x/initlist-const1.C create mode 100644 gcc/testsuite/g++.dg/warn/Wunused-var-37.C create mode 100644 gcc/testsuite/g++.dg/warn/Wunused-var-38.C create mode 100644 gcc/testsuite/g++.dg/warn/Wunused-var-39.C diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc index 2852093d624..45206c236c1 100644 --- a/gcc/cp/decl.cc +++ b/gcc/cp/decl.cc @@ -8129,6 +8129,17 @@ cp_finish_decl (tree decl, tree init, bool init_const_expr_p, TREE_TYPE (decl) = error_mark_node; return; } + /* As in start_decl_1, complete so TREE_READONLY is set properly. */ + if (!processing_template_decl + && !type_uses_auto (type) + && !COMPLETE_TYPE_P (complete_type (type))) + { + error_at (location_of (decl), + "deduced type %qT for %qD is incomplete", type, decl); + cxx_incomplete_type_inform (type); + TREE_TYPE (decl) = error_mark_node; + return; + } cp_apply_type_quals_to_decl (cp_type_quals (type), decl); } diff --git a/gcc/testsuite/g++.dg/cpp0x/constexpr-77482.C b/gcc/testsuite/g++.dg/cpp0x/constexpr-77482.C index 6f5c91621ce..bed66c7fa81 100644 --- a/gcc/testsuite/g++.dg/cpp0x/constexpr-77482.C +++ b/gcc/testsuite/g++.dg/cpp0x/constexpr-77482.C @@ -3,4 +3,4 @@ constexpr auto x; // { dg-error "declaration\[^\n\r]*has no initializer" } extern struct S s; -constexpr auto y = s; // { dg-error "has incomplete type" } +constexpr auto y = s; // { dg-error "incomplete" } diff --git a/gcc/testsuite/g++.dg/cpp0x/initlist-const1.C b/gcc/testsuite/g++.dg/cpp0x/initlist-const1.C new file mode 100644 index 00000000000..de807316be6 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/initlist-const1.C @@ -0,0 +1,7 @@ +// { dg-do compile { target c++11 } } + +#include <initializer_list> + +const auto x = { 1, 2 }; + +// { dg-final { scan-assembler-not {\.data} } } diff --git a/gcc/testsuite/g++.dg/cpp1y/auto-fn27.C b/gcc/testsuite/g++.dg/cpp1y/auto-fn27.C index c019d9ef583..b22647a38de 100644 --- a/gcc/testsuite/g++.dg/cpp1y/auto-fn27.C +++ b/gcc/testsuite/g++.dg/cpp1y/auto-fn27.C @@ -31,7 +31,7 @@ F<T>::bar (const G &) { auto s = I; typedef decltype (s) L; - auto u =[&](L) { auto t = foo (J::K (), 0); }; // { dg-error "25:declared void" } + auto u =[&](L) { auto t = foo (J::K (), 0); }; // { dg-error "25:incomplete|void" } } struct B { typedef int G; diff --git a/gcc/testsuite/g++.dg/cpp1y/lambda-generic-variadic22.C b/gcc/testsuite/g++.dg/cpp1y/lambda-generic-variadic22.C index 670c598a865..9c02d0715c1 100644 --- a/gcc/testsuite/g++.dg/cpp1y/lambda-generic-variadic22.C +++ b/gcc/testsuite/g++.dg/cpp1y/lambda-generic-variadic22.C @@ -4,7 +4,7 @@ template <typename T> auto f (T) { - auto a = [](auto ... i) // { dg-prune-output "incomplete type" } + auto a = [](auto ... i) // { dg-prune-output "incomplete" } { int x[][i] = { 0 }; // { dg-error "not expanded" } }(); diff --git a/gcc/testsuite/g++.dg/cpp1z/decomp54.C b/gcc/testsuite/g++.dg/cpp1z/decomp54.C index 1bee772d5ba..1094329e69d 100644 --- a/gcc/testsuite/g++.dg/cpp1z/decomp54.C +++ b/gcc/testsuite/g++.dg/cpp1z/decomp54.C @@ -3,9 +3,9 @@ // { dg-options "" } extern int a[]; -auto [b] { a }; // { dg-error "has incomplete type" } +auto [b] { a }; // { dg-error "incomplete" } // { dg-warning "only available with" "" { target c++14_down } .-1 } -auto [c] = a; // { dg-error "has incomplete type" } +auto [c] = a; // { dg-error "incomplete" } // { dg-warning "only available with" "" { target c++14_down } .-1 } extern int d[0]; auto [e] { d }; // { dg-error "too many initializers for" } diff --git a/gcc/testsuite/g++.dg/warn/Wunused-var-37.C b/gcc/testsuite/g++.dg/warn/Wunused-var-37.C new file mode 100644 index 00000000000..54e76ac4e11 --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/Wunused-var-37.C @@ -0,0 +1,64 @@ +// Tangentially to PR c++/80351 +// { dg-do compile { target c++17 } } +// { dg-options "-Wunused-variable" } +#include <initializer_list> + +// Warnings: +static int int_s1 = 0; // { dg-warning "defined but not used" } +static int int_s2 = 0; // { dg-warning "defined but not used" } +inline static int int_is1 = 0; // { dg-warning "defined but not used" } +inline static int int_is2 = 0; // { dg-warning "defined but not used" } +// No warnings: +constexpr static int int_cs1 = 0; // { dg-bogus "defined but not used" } +constexpr static int int_cs2 = 0; // { dg-bogus "defined but not used" } +int int_1 = 0; // { dg-bogus "defined but not used" } +int int_2 = 0; // { dg-bogus "defined but not used" } +inline int int_i1 = 0; // { dg-bogus "defined but not used" } +inline int int_i2 = 0; // { dg-bogus "defined but not used" } +constexpr int int_c1 = 0; // { dg-bogus "defined but not used" } +constexpr int int_c2 = 0; // { dg-bogus "defined but not used" } + +// Warnings: +static auto int_as1 = 0; // { dg-warning "defined but not used" } +static auto int_as2 = 0; // { dg-warning "defined but not used" } +inline static auto int_ais1 = 0; // { dg-warning "defined but not used" } +inline static auto int_ais2 = 0; // { dg-warning "defined but not used" } +// No warnings: +constexpr static auto int_acs1 = 0; // { dg-bogus "defined but not used" } +constexpr static auto int_acs2 = 0; // { dg-bogus "defined but not used" } +auto int_a1 = 0; // { dg-bogus "defined but not used" } +auto int_a2 = 0; // { dg-bogus "defined but not used" } +inline auto int_ai1 = 0; // { dg-bogus "defined but not used" } +inline auto int_ai2 = 0; // { dg-bogus "defined but not used" } +constexpr auto int_ac1 = 0; // { dg-bogus "defined but not used" } +constexpr auto int_ac2 = 0; // { dg-bogus "defined but not used" } + +// Warnings: +static std::initializer_list<int> il_s1 = {0, 1}; // { dg-warning "defined but not used" } +static std::initializer_list<int> il_s2 = {0, 1}; // { dg-warning "defined but not used" } +inline static std::initializer_list<int> il_is1 = {0, 1}; // { dg-warning "defined but not used" } +inline static std::initializer_list<int> il_is2 = {0, 1}; // { dg-warning "defined but not used" } +// No warnings: +constexpr static std::initializer_list<int> il_cs1 = {0, 1}; // { dg-bogus "defined but not used" } +constexpr static std::initializer_list<int> il_cs2 = {0, 1}; // { dg-bogus "defined but not used" } +std::initializer_list<int> il_1 = {0, 1}; // { dg-bogus "defined but not used" } +std::initializer_list<int> il_2 = {0, 1}; // { dg-bogus "defined but not used" } +inline std::initializer_list<int> il_i1 = {0, 1}; // { dg-bogus "defined but not used" } +inline std::initializer_list<int> il_i2 = {0, 1}; // { dg-bogus "defined but not used" } +constexpr std::initializer_list<int> il_c1 = {0, 1}; // { dg-bogus "defined but not used" } +constexpr std::initializer_list<int> il_c2 = {0, 1}; // { dg-bogus "defined but not used" } + +// Warnings: +static auto il_as1 = {0, 1}; // { dg-warning "defined but not used" } +static auto il_as2 = {0, 1}; // { dg-warning "defined but not used" } +inline static auto il_ais1 = {0, 1}; // { dg-warning "defined but not used" } +inline static auto il_ais2 = {0, 1}; // { dg-warning "defined but not used" } +// No warnings: +constexpr static auto il_acs1 = {0, 1}; // { dg-bogus "defined but not used" } +constexpr static auto il_acs2 = {0, 1}; // { dg-bogus "defined but not used" } +auto il_a1 = {0, 1}; // { dg-bogus "defined but not used" } +auto il_a2 = {0, 1}; // { dg-bogus "defined but not used" } +inline auto il_ai1 = {0, 1}; // { dg-bogus "defined but not used" } +inline auto il_ai2 = {0, 1}; // { dg-bogus "defined but not used" } +constexpr auto il_ac1 = {0, 1}; // { dg-bogus "defined but not used" } +constexpr auto il_ac2 = {0, 1}; // { dg-bogus "defined but not used" } diff --git a/gcc/testsuite/g++.dg/warn/Wunused-var-38.C b/gcc/testsuite/g++.dg/warn/Wunused-var-38.C new file mode 100644 index 00000000000..3d34bc8e8c1 --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/Wunused-var-38.C @@ -0,0 +1,16 @@ +// PR c++/80351 +// { dg-do compile { target c++11 } } +// { dg-options "-Wunused-variable" } +#include <initializer_list> + +constexpr auto int_1 = 1; // { dg-bogus "defined but not used" } +constexpr auto int_2 = 2; // { dg-bogus "defined but not used" } + +constexpr auto il_int_1 = {3, 3}; // { dg-bogus "defined but not used" "Triggered by PR80351" } +constexpr auto il_int_2 = {4, 4}; // { dg-bogus "defined but not used" "Not triggered by PR80351" } + +constexpr auto il_uint_1 = {5u, 5u}; // { dg-bogus "defined but not used" "Triggered by PR80351" } +constexpr auto il_uint_2 = {6u, 6u}; // { dg-bogus "defined but not used" "Not triggered by PR80351" } + +constexpr auto uint_1 = 7u; // { dg-bogus "defined but not used" } +constexpr auto uint_2 = 8u; // { dg-bogus "defined but not used" } diff --git a/gcc/testsuite/g++.dg/warn/Wunused-var-39.C b/gcc/testsuite/g++.dg/warn/Wunused-var-39.C new file mode 100644 index 00000000000..d25a74c43c2 --- /dev/null +++ b/gcc/testsuite/g++.dg/warn/Wunused-var-39.C @@ -0,0 +1,16 @@ +// PR c++/80351 +// { dg-do compile { target c++11 } } +// { dg-options "-Wunused-variable" } +#include <initializer_list> + +const auto int_1 = 1; // { dg-bogus "defined but not used" } +const auto int_2 = 2; // { dg-bogus "defined but not used" } + +const auto il_int_1 = {3, 3}; // { dg-bogus "defined but not used" "Triggered by PR80351" } +const auto il_int_2 = {4, 4}; // { dg-bogus "defined but not used" "Not triggered by PR80351" } + +const auto il_uint_1 = {5u, 5u}; // { dg-bogus "defined but not used" "Triggered by PR80351" } +const auto il_uint_2 = {6u, 6u}; // { dg-bogus "defined but not used" "Not triggered by PR80351" } + +const auto uint_1 = 7u; // { dg-bogus "defined but not used" } +const auto uint_2 = 8u; // { dg-bogus "defined but not used" } base-commit: 4a8b45e8bc8246bd141dad65f571a3e0cc499c6b -- 2.27.0