From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-1.mimecast.com (us-smtp-delivery-1.mimecast.com [205.139.110.120]) by sourceware.org (Postfix) with ESMTP id C4955386F819 for ; Wed, 6 May 2020 21:26:37 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org C4955386F819 Received: from mail-qk1-f200.google.com (mail-qk1-f200.google.com [209.85.222.200]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-320-9zVlm09NNiyXa1LdyofIrA-1; Wed, 06 May 2020 17:26:35 -0400 X-MC-Unique: 9zVlm09NNiyXa1LdyofIrA-1 Received: by mail-qk1-f200.google.com with SMTP id h1so3268883qkl.6 for ; Wed, 06 May 2020 14:26:35 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=eA902QBaaL0WH/w57g+DRXC5kzbLDKqWfb2+0QRND2I=; b=Xt12p+cUrMMNRWW7C6nmvvmE0Kp3KS5FVLKdQSMNQLz1FOZap318cUaqcqc5G4TyIo y19paM9l75keX7yP2AzG7ceV4fk8fPAvyHoqlNtQPhJO9KwqMFSQAilga/B5IMJ443m5 w6TauQtV8pSQMQMDIuk3pWSriKkus1SrAeiEF0cGOabkaefxYD8oG/QeFgKLQaPZXuT7 +cJjQ/QqHo8GeQOyclxgubwPAtXSZSzETQMMlTGAK72QHWi8LLRNEe+z1BClz/EfEKRI 4J07pYBb3P6uacFka14yLU07lYL/o+ozeyR5CPeUz3PHtKjrNfor0Ia2+4rxeuJ6Grpf 1VZQ== X-Gm-Message-State: AGi0Pubpxfe6pnHtKba9Tzj0Nq/xdyU0svWMUBHq+3QeRTV1y4dGURL7 3THTk8cLXrfpYh1uOK4rQewyTpqu7nsXMOq1XfAxHbhZ3sRB9HWVhjSy27y4st7wIVrjfXsFTIg 0PjVWQ4n0M1eJ2lDnlA== X-Received: by 2002:a37:a853:: with SMTP id r80mr11430877qke.435.1588800394319; Wed, 06 May 2020 14:26:34 -0700 (PDT) X-Google-Smtp-Source: APiQypK7R55JRFwiElMUb3ndsMW2fM7vy29N2YG4VEe8KnAvAlBIqbMeHEcMABLJEZCWUfdHUjPwFQ== X-Received: by 2002:a37:a853:: with SMTP id r80mr11430840qke.435.1588800393842; Wed, 06 May 2020 14:26:33 -0700 (PDT) Received: from [192.168.1.148] (209-6-216-142.s141.c3-0.smr-cbr1.sbo-smr.ma.cable.rcncustomer.com. [209.6.216.142]) by smtp.gmail.com with ESMTPSA id t67sm2656788qka.17.2020.05.06.14.26.32 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Wed, 06 May 2020 14:26:32 -0700 (PDT) Subject: Re: [PATCH] c++: ICE with -Wall and constexpr if [PR94937] To: Marek Polacek , GCC Patches References: <20200505221727.3575367-1-polacek@redhat.com> From: Jason Merrill Message-ID: Date: Wed, 6 May 2020 17:26:32 -0400 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.6.0 MIME-Version: 1.0 In-Reply-To: <20200505221727.3575367-1-polacek@redhat.com> Content-Language: en-US X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-20.9 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_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) 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: Wed, 06 May 2020 21:26:39 -0000 On 5/5/20 6:17 PM, Marek Polacek wrote: > An ICE arises here because we call cp_get_callee_fndecl_nofold in a > template, and we've got a CALL_EXPR whose CALL_EXPR_FN is a BASELINK. > This tickles the INDIRECT_TYPE_P assert in cp_get_fndecl_from_callee. > > Jakub said in the PR that he'd hit a similar problem too and dealt > with it in omp_declare_variant_finalize_one. I considered tweaking > is_std_constant_evaluated_p to return false for a BASELINK, since the > std::is_constant_evaluated call we're looking for can't be a member > function, but perhaps we could get another unexpected CALL_EXPR and > crash the same. In which case it might be better to make out the > omp_* code into a new routine and use that, as below. Why not adjust cp_get_fndecl_from_callee to, say, return null instead of aborting when !INDIRECT_TYPE_P? > As a drive-by, I'm adding a test that tests the "is_constant_evaluated > in constexpr if" warning in a template too. > > Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk/10.2? > > PR c++/94937 > * cp-tree.h (cp_get_callee_fndecl_template): Declare. > * cvt.c (cp_get_callee_fndecl_template): New. > * decl.c (omp_declare_variant_finalize_one): Use > cp_get_callee_fndecl_template. > * semantics.c (is_std_constant_evaluated_p): Call > cp_get_callee_fndecl_template instead of cp_get_callee_fndecl_nofold. > > * g++.dg/cpp1z/constexpr-if34.C: New test. > * g++.dg/cpp2a/is-constant-evaluated10.C: New test. > --- > gcc/cp/cp-tree.h | 1 + > gcc/cp/cvt.c | 17 +++++++++++ > gcc/cp/decl.c | 12 +------- > gcc/cp/semantics.c | 2 +- > gcc/testsuite/g++.dg/cpp1z/constexpr-if34.C | 15 ++++++++++ > .../g++.dg/cpp2a/is-constant-evaluated10.C | 30 +++++++++++++++++++ > 6 files changed, 65 insertions(+), 12 deletions(-) > create mode 100644 gcc/testsuite/g++.dg/cpp1z/constexpr-if34.C > create mode 100644 gcc/testsuite/g++.dg/cpp2a/is-constant-evaluated10.C > > diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h > index c4b81428e14..ecee0f479b0 100644 > --- a/gcc/cp/cp-tree.h > +++ b/gcc/cp/cp-tree.h > @@ -6477,6 +6477,7 @@ extern tree cp_fold_convert (tree, tree); > extern tree cp_get_callee (tree); > extern tree cp_get_callee_fndecl (tree); > extern tree cp_get_callee_fndecl_nofold (tree); > +extern tree cp_get_callee_fndecl_template (tree); > extern tree cp_get_fndecl_from_callee (tree, bool fold = true); > extern tree convert_to_void (tree, impl_conv_void, > tsubst_flags_t); > diff --git a/gcc/cp/cvt.c b/gcc/cp/cvt.c > index 656e7fd3ec0..5d3fba76bb9 100644 > --- a/gcc/cp/cvt.c > +++ b/gcc/cp/cvt.c > @@ -1027,6 +1027,23 @@ cp_get_callee_fndecl_nofold (tree call) > return cp_get_fndecl_from_callee (cp_get_callee (call), false); > } > > +/* As above, but is safe to call in a template too where we might get > + unexpected trees. Returns the FUNCTION_DECL or NULL_TREE. */ > + > +tree > +cp_get_callee_fndecl_template (tree call) > +{ > + call = cp_get_callee (call); > + if (call == NULL_TREE) > + return NULL_TREE; > + else if (TREE_CODE (call) == FUNCTION_DECL) > + return call; > + else if (TREE_TYPE (call) && INDIRECT_TYPE_P (TREE_TYPE (call))) > + return cp_get_fndecl_from_callee (call, /*fold=*/false); > + else > + return NULL_TREE; > +} > + > /* Subroutine of convert_to_void. Warn if we're discarding something with > attribute [[nodiscard]]. */ > > diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c > index 3e7ed98fed3..7b63304ebf3 100644 > --- a/gcc/cp/decl.c > +++ b/gcc/cp/decl.c > @@ -7297,17 +7297,7 @@ omp_declare_variant_finalize_one (tree decl, tree attr) > if (variant == error_mark_node && !processing_template_decl) > return true; > > - variant = cp_get_callee (variant); > - if (variant) > - { > - if (TREE_CODE (variant) == FUNCTION_DECL) > - ; > - else if (TREE_TYPE (variant) && INDIRECT_TYPE_P (TREE_TYPE (variant))) > - variant = cp_get_fndecl_from_callee (variant, false); > - else > - variant = NULL_TREE; > - } > - > + variant = cp_get_callee_fndecl_template (variant); > input_location = save_loc; > > if (variant) > diff --git a/gcc/cp/semantics.c b/gcc/cp/semantics.c > index 4d1592ab0d2..716ee48446d 100644 > --- a/gcc/cp/semantics.c > +++ b/gcc/cp/semantics.c > @@ -734,7 +734,7 @@ is_std_constant_evaluated_p (tree fn) > if (call_expr_nargs (fn) != 0) > return false; > > - tree fndecl = cp_get_callee_fndecl_nofold (fn); > + tree fndecl = cp_get_callee_fndecl_template (fn); > if (fndecl == NULL_TREE) > return false; > > diff --git a/gcc/testsuite/g++.dg/cpp1z/constexpr-if34.C b/gcc/testsuite/g++.dg/cpp1z/constexpr-if34.C > new file mode 100644 > index 00000000000..6e0b2597a53 > --- /dev/null > +++ b/gcc/testsuite/g++.dg/cpp1z/constexpr-if34.C > @@ -0,0 +1,15 @@ > +// PR c++/94937 - ICE with -Wall and constexpr if. > +// { dg-do compile { target c++17 } } > +// { dg-options "-Wall" } > + > +struct B { > + static constexpr bool foo() { return false; } > +}; > + > +template > +struct C { > + static void bar () > + { > + if constexpr (B::foo()) ; > + } > +}; > diff --git a/gcc/testsuite/g++.dg/cpp2a/is-constant-evaluated10.C b/gcc/testsuite/g++.dg/cpp2a/is-constant-evaluated10.C > new file mode 100644 > index 00000000000..7b2e345f448 > --- /dev/null > +++ b/gcc/testsuite/g++.dg/cpp2a/is-constant-evaluated10.C > @@ -0,0 +1,30 @@ > +// { dg-do compile { target c++2a } } > +// { dg-options "-Wtautological-compare" } > + > +namespace std { > + constexpr inline bool > + is_constant_evaluated () noexcept > + { > + return __builtin_is_constant_evaluated (); > + } > +} > + > +template > +constexpr int > +foo(int i) > +{ > + if constexpr (std::is_constant_evaluated ()) // { dg-warning ".std::is_constant_evaluated. always evaluates to true in .if constexpr." } > + return 42; > + else > + return i; > +} > + > +template > +constexpr int > +foo2(int i) > +{ > + if constexpr (__builtin_is_constant_evaluated ()) // { dg-warning ".std::is_constant_evaluated. always evaluates to true in .if constexpr." } > + return 42; > + else > + return i; > +} > > base-commit: e42f342652981aa9181cc3ceb04e0eb715506764 >