From patchwork Fri Apr 29 18:23:39 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 53363 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 511FA3858C2C for ; Fri, 29 Apr 2022 18:24:15 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 511FA3858C2C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1651256655; bh=HCOhtnI55V4VaD2W3ctGKwI3rx0btJLrQsHHIC7xWRQ=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=XSxPfV7fIyqmHm0+DT+/5fPOgAAQkWyD46WpnhRqZ5wZNj8Ofb/Qty5Q7z7OeBJ/u Wp7DYv0kfFcuPKGXBu0LgXnQZ7+u0f/P//N5sd5xaiJkpDLvCO+lrIW+nqORHiIL15 IEftNq6Xbz57eaTwpyPjtT/ezI38v54/L9dAfpdw= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by sourceware.org (Postfix) with ESMTPS id ECAB03858D28 for ; Fri, 29 Apr 2022 18:23:44 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org ECAB03858D28 Received: from mail-qt1-f198.google.com (mail-qt1-f198.google.com [209.85.160.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-67-U4cT4D-kNqiuu7zN_2MPqQ-1; Fri, 29 Apr 2022 14:23:43 -0400 X-MC-Unique: U4cT4D-kNqiuu7zN_2MPqQ-1 Received: by mail-qt1-f198.google.com with SMTP id q18-20020a05622a031200b002f399b6ed83so988277qtw.22 for ; Fri, 29 Apr 2022 11:23:43 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=HCOhtnI55V4VaD2W3ctGKwI3rx0btJLrQsHHIC7xWRQ=; b=Pb/e/C/htxzuoVatKVb3faJ4+623pEQW4F1d2zmUtg+ccfcABVXH5oaCgnpOieibqF fP34SDLygWr8pZSjZiVnkVTgzS3TyHma4gSSOINRusNrFh/UOrCoTNm5yeI4wayYWRsj /myKFViXW5Z6TYtzysbn8C/cSQTxMfhkLPy3Iafl1iy2UDWUM0xKXb7SdmvpphjyO7HF ZVJ7gI3UAMgs5hTSCZdA30EamNZpnM6UD3vf3OLucIjHinOWEHqu/7yjoZO3oJHLaEVr 0Jka/cxeisWLiGVnzNHoS+xjQOz0CHmUyQz/MWT7ijjWJDNE9H0PbCUtKilBnFIpIULe mxag== X-Gm-Message-State: AOAM5300X9ToXzTkGq22HFpo+ajw7VYbdeCKK1QZX58Ce9tLg3ul6sjc j6f/DNEiWs4GCnoFbkOUELvcaXz23dw0xSzBqUnDkyfZkLbiwC+1XvKVfeQmpTefW51SLdsH+HP trN2z1zpynASTICbUCZFzUPoVxXNmldJu/n2krN7DQilHxdDECK0XG8XtKmWC9GtraQ== X-Received: by 2002:ac8:5a03:0:b0:2f3:4450:1ed1 with SMTP id n3-20020ac85a03000000b002f344501ed1mr655017qta.214.1651256622370; Fri, 29 Apr 2022 11:23:42 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxmVJSfRl/M5o6/upFwn9+ZlGFtHa3L7vqdrOJw3NBDR5s+93r3rtdSCtpuGivjXr4SKk24Mg== X-Received: by 2002:ac8:5a03:0:b0:2f3:4450:1ed1 with SMTP id n3-20020ac85a03000000b002f344501ed1mr654989qta.214.1651256621880; Fri, 29 Apr 2022 11:23:41 -0700 (PDT) Received: from barrymore.redhat.com (130-44-159-43.s15913.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.159.43]) by smtp.gmail.com with ESMTPSA id v67-20020a379346000000b0069bdb3bb132sm1807155qkd.37.2022.04.29.11.23.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 29 Apr 2022 11:23:40 -0700 (PDT) To: gcc-patches@gcc.gnu.org Subject: [pushed] c++: check completeness after auto deduction [PR80351] Date: Fri, 29 Apr 2022 14:23:39 -0400 Message-Id: <20220429182339.1209446-1-jason@redhat.com> X-Mailer: git-send-email 2.27.0 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-13.4 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jason Merrill via Gcc-patches From: Jason Merrill Reply-To: Jason Merrill Cc: Pokechu022@gmail.com Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" 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 base-commit: 4a8b45e8bc8246bd141dad65f571a3e0cc499c6b 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 + +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::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 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 + +// 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 il_s1 = {0, 1}; // { dg-warning "defined but not used" } +static std::initializer_list il_s2 = {0, 1}; // { dg-warning "defined but not used" } +inline static std::initializer_list il_is1 = {0, 1}; // { dg-warning "defined but not used" } +inline static std::initializer_list il_is2 = {0, 1}; // { dg-warning "defined but not used" } +// No warnings: +constexpr static std::initializer_list il_cs1 = {0, 1}; // { dg-bogus "defined but not used" } +constexpr static std::initializer_list il_cs2 = {0, 1}; // { dg-bogus "defined but not used" } +std::initializer_list il_1 = {0, 1}; // { dg-bogus "defined but not used" } +std::initializer_list il_2 = {0, 1}; // { dg-bogus "defined but not used" } +inline std::initializer_list il_i1 = {0, 1}; // { dg-bogus "defined but not used" } +inline std::initializer_list il_i2 = {0, 1}; // { dg-bogus "defined but not used" } +constexpr std::initializer_list il_c1 = {0, 1}; // { dg-bogus "defined but not used" } +constexpr std::initializer_list 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 + +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 + +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" }