From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-il1-x129.google.com (mail-il1-x129.google.com [IPv6:2607:f8b0:4864:20::129]) by sourceware.org (Postfix) with ESMTPS id CEF57386549C for ; Thu, 24 Mar 2022 01:01:47 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org CEF57386549C Received: by mail-il1-x129.google.com with SMTP id d3so2209784ilr.10 for ; Wed, 23 Mar 2022 18:01:47 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=6IineKYSPgN333UttJv0ayQZr9AIAA+ewSSVdHVdMI8=; b=1FdFJ+tEstGmCuKJCU0eg/ezEfhZe2uOXKkk1TeHqqapkreOGhZAYdW/qZwU2Erogl 7Yj32HxYIWSmdipFsK1DR/LoHggST+AaVGG1IzDdZAAhsMEoIx+VTQ8JX1/WhsIzbfQj SWa1qVQ3+hK6iuOxkTXv6O177QyHFQCgAu7H62A3P8A8z3p9KWkZ2Gj/OW+83Q4AEk9f +6Q6rk3z2ZgKWEMqahnbRKXmF9h+/MmRrm6/laPTfrAt4b7HlXOt+5of1G2fZQw7W0+B 2c3x86FiOY98FANxjjTMkfy7YrKbJQrmUexORzeBNIEVc+OHd6tsWN7BnElV2t2xAfjS O8Iw== X-Gm-Message-State: AOAM530k0LDxmzuKVGTdqXsO0GVxGa7kp59rIDHRVMxY1hpSsGfmCwce RwQbIqU5NE1vi0pJCEH3ScPlRdBZLkFbIckO7M29IqLodbg= X-Google-Smtp-Source: ABdhPJzeifK3rtJRjtwT1FmDV0KqK4v0mycOpZGaeHRgqO+cmAxiQnj542emH7YRnzhyqzxtzOibCiBv1XACJ3Byojo= X-Received: by 2002:a92:c268:0:b0:2c7:c913:a44b with SMTP id h8-20020a92c268000000b002c7c913a44bmr1367410ild.281.1648083706652; Wed, 23 Mar 2022 18:01:46 -0700 (PDT) MIME-Version: 1.0 From: Pokechu22 Date: Wed, 23 Mar 2022 18:01:20 -0700 Message-ID: Subject: [PATCH] c++: call complete_type after performing auto deduction [PR80351] To: gcc-patches@gcc.gnu.org Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-9.2 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE 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-List-Received-Date: Thu, 24 Mar 2022 01:01:50 -0000 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 on x86_64 Ubuntu under WSL1 by bootstrapping and comparing results from 24d51e749570dcb85bd43d3b528f58ad6141de26 with results from this change. As far as I can tell, this fixes Wunused-var-38.C and Wunused-var-39.C without regressions. (Wunused-var-37.C passed before this change.) gcc/cp/ChangeLog: PR c++/80351 * decl.cc (cp_finish_decl): Call complete_type after performing auto deduction. gcc/testsuite/ChangeLog: PR c++/80351 * 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 | 2 +- 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 ++++++ 4 files changed, 97 insertions(+), 1 deletion(-) 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 34d9dad9fb0..e382b8ad218 100644 --- a/gcc/cp/decl.cc +++ b/gcc/cp/decl.cc @@ -8098,7 +8098,7 @@ cp_finish_decl (tree decl, tree init, bool init_const_expr_p, TREE_TYPE (decl) = error_mark_node; return; } - cp_apply_type_quals_to_decl (cp_type_quals (type), decl); + cp_apply_type_quals_to_decl (cp_type_quals (complete_type (type)), decl); } if (ensure_literal_type_for_constexpr_object (decl) == error_mark_node) 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" } -- 2.25.1