From patchwork Wed Jan 19 16:15:42 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 50238 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 ABD2D3858029 for ; Wed, 19 Jan 2022 16:16:21 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org ABD2D3858029 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1642608981; bh=F3OAlHUfe0adxu7K7ExTscA2Wy5BzlKtGc+fVm/Xw2c=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=oNrkVMOHpxQaaPLGCQ4QpKHirhZ5zGHaPF//G0dshU7IhViw18mnNcNziG7ZDFx26 QxaVS1h45H/IP/lXwEs78stxihEc5eKsRGWa8OK7MU1luPjwlrXxursa47NZ1CDDYH DTlk5lODNasqAXv3l0wlio67OFzg+TzwwdJWUhuw= 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 D59E53858404 for ; Wed, 19 Jan 2022 16:15:52 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org D59E53858404 Received: from mail-qk1-f200.google.com (mail-qk1-f200.google.com [209.85.222.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-267-gXbBZoU4OMWGUG6CH9m4dQ-1; Wed, 19 Jan 2022 11:15:46 -0500 X-MC-Unique: gXbBZoU4OMWGUG6CH9m4dQ-1 Received: by mail-qk1-f200.google.com with SMTP id y185-20020a3764c2000000b0047a8c8b3febso2235673qkb.1 for ; Wed, 19 Jan 2022 08:15:46 -0800 (PST) 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=F3OAlHUfe0adxu7K7ExTscA2Wy5BzlKtGc+fVm/Xw2c=; b=YdNQaK1QAOj54LzXq/KKc4dNOyQUAbmbntq2Ka9UV76mOXeMLnEZMB5nnpiNN92eHa k3lOzQeG+PJX+kmdo0ooKxfhNyka4+rjIN7EBUGd5aQyj+w8118Z45LtkteKoDz34ovd WsZ42+B2ykeXt8ajSlkzn/ySjBjo6sfTgMyYk36CmjkWd32JjDnZmqbOxFkdNsDj7Vea gejQqH9gPS65XpE81jQPTkD/itRDFFavBFqchwXPDRntgF4XE8okgDc1qfXAHVPLcaHN oCrk/fE+ORdg3He34DgQK+ySaiwCGJOo6DZ0kSRHL64nHjN2z6TC2EaHtsHXyBWR94s2 fFQQ== X-Gm-Message-State: AOAM530gPg6B+5jDR6ZQ7hnF8gUQSAdypfkh9yDHonP5kTWimhLrzDDQ cQ78hYzcdvFSCYN2zdrN6RWYSvSWLhZ+1KELwKs2++p3Vz8oo3kLevxwTeKk5/jRooSiNc19ppc OrBh7nzm2xNj2ohFrqJzOn/HfOrio9ouTF9MJUTmdfbRJ1hhza+whohGNJ6wRz4fGiQk= X-Received: by 2002:ad4:5d6d:: with SMTP id fn13mr19746518qvb.25.1642608945368; Wed, 19 Jan 2022 08:15:45 -0800 (PST) X-Google-Smtp-Source: ABdhPJx3TDU6Iq73dK16EfMIPH6NAX8aqHREfe5o4nN+DIJE3vwxZ/25GsrOZLD0O9kG9J2iurqA7Q== X-Received: by 2002:ad4:5d6d:: with SMTP id fn13mr19746484qvb.25.1642608945039; Wed, 19 Jan 2022 08:15:45 -0800 (PST) Received: from localhost.localdomain (ool-18e40894.dyn.optonline.net. [24.228.8.148]) by smtp.gmail.com with ESMTPSA id o6sm11928008qtk.50.2022.01.19.08.15.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 19 Jan 2022 08:15:44 -0800 (PST) To: gcc-patches@gcc.gnu.org Subject: [PATCH] c++: non-dependent immediate member fn call [PR99895] Date: Wed, 19 Jan 2022 11:15:42 -0500 Message-Id: <20220119161542.1259608-1-ppalka@redhat.com> X-Mailer: git-send-email 2.35.0.rc1 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-14.6 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, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, 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: Patrick Palka via Gcc-patches From: Patrick Palka Reply-To: Patrick Palka Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" Here we're emitting a bogus error during ahead of time evaluation of a non-dependent immediate member function call such as a.f(args) because the defacto templated form for such a call is (a.f)(args) but we're trying to evaluate it using the intermediate CALL_EXPR built by build_over_call, which has the non-member form f(a, args). The defacto member form is built in build_new_method_call, so it seems we should handle the immediate call there instead. Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for trunk and perhaps 11? PR c++/99895 gcc/cp/ChangeLog: * call.cc (build_over_call): Don't evaluate non-dependent immediate member function calls here. (build_new_method_call): Instead evaluate them here. gcc/testsuite/ChangeLog: * g++.dg/cpp2a/consteval-memfn1.C: New test. * g++.dg/cpp2a/consteval-memfn2.C: New test. --- gcc/cp/call.cc | 9 ++++- gcc/testsuite/g++.dg/cpp2a/consteval-memfn1.C | 15 ++++++++ gcc/testsuite/g++.dg/cpp2a/consteval-memfn2.C | 34 +++++++++++++++++++ 3 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/g++.dg/cpp2a/consteval-memfn1.C create mode 100644 gcc/testsuite/g++.dg/cpp2a/consteval-memfn2.C diff --git a/gcc/cp/call.cc b/gcc/cp/call.cc index d4a07a7a9b3..0583cc0083b 100644 --- a/gcc/cp/call.cc +++ b/gcc/cp/call.cc @@ -9241,7 +9241,10 @@ build_over_call (struct z_candidate *cand, int flags, tsubst_flags_t complain) addr, nargs, argarray); if (TREE_THIS_VOLATILE (fn) && cfun) current_function_returns_abnormally = 1; - if (immediate_invocation_p (fn, nargs)) + if (!DECL_FUNCTION_MEMBER_P (fn) + /* Non-dependent immediate member function calls are evaluated in + build_new_method_call. */ + && immediate_invocation_p (fn, nargs)) { tree obj_arg = NULL_TREE, exprimm = expr; if (DECL_CONSTRUCTOR_P (fn)) @@ -11227,6 +11230,10 @@ skip_prune: call = convert_from_reference (call); if (cast_to_void) call = build_nop (void_type_node, call); + + if (immediate_invocation_p (fn, vec_safe_length (orig_args))) + fold_non_dependent_expr (call, complain, + /*manifestly_const_eval=*/true); } /* Free all the conversions we allocated. */ diff --git a/gcc/testsuite/g++.dg/cpp2a/consteval-memfn1.C b/gcc/testsuite/g++.dg/cpp2a/consteval-memfn1.C new file mode 100644 index 00000000000..d2df2e9b5ae --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/consteval-memfn1.C @@ -0,0 +1,15 @@ +// PR c++/99895 +// { dg-do compile { target c++20 } } + +struct fixed_string { + consteval int size(int n) const { + if (n < 0) throw; // { dg-error "not a constant" } + return n; + } +}; + +template +void VerifyHash(fixed_string s) { + s.size(0); // { dg-bogus "" } + s.size(-1); // { dg-message "expansion of" } +} diff --git a/gcc/testsuite/g++.dg/cpp2a/consteval-memfn2.C b/gcc/testsuite/g++.dg/cpp2a/consteval-memfn2.C new file mode 100644 index 00000000000..71748f46b13 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/consteval-memfn2.C @@ -0,0 +1,34 @@ +// PR c++/99895 +// { dg-do compile { target c++20 } } + +static constexpr unsigned hash(const char* s, unsigned length) +{ + s=s; + return length; +} +template +struct fixed_string +{ + constexpr fixed_string(const char (&s)[N]) + { + for (int i = 0; i < N; i++) + str[i] = s[i]; + } + consteval const char* data() const { return str; } + consteval unsigned size() const { return N-1; } + char str[N]; +}; +template +static consteval void VerifyHash() +{ + ( + [](auto){static_assert(hash(s.data(), s.size()) == expected_hash);}(s) + ,...); + // The compiler mistakenly translates s.data() into s.data(&s) + // and then complains that the call is not valid, because + // the function expects 0 parameters and 1 "was provided". +} +void foo() +{ + VerifyHash<5, "khaki", "plums">(); +}