From patchwork Tue Dec 6 17:57:02 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 1712914 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=iue6Qstm; dkim-atps=neutral Received: from sourceware.org (ip-8-43-85-97.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4NRSms3Qbqz23yq for ; Wed, 7 Dec 2022 04:57:30 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E3EED383FD66 for ; Tue, 6 Dec 2022 17:57:27 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E3EED383FD66 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1670349447; bh=I14z9NilI1WIoSqCBJejMobn7cyf77BGwgv5lsjcPSM=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=iue6QstmcR9XsIjfyHaYkDpAzB2RKJsu1xJfJgDnA86Oz4plEB+e0xZh1etESWK3P V0Aw+KdeMKOQ3z1HblGzQlegcov1KN5cUzQV+sASQGvIISXIVDSpo9hU8gywp7KYH4 9AAG1Iu+tgJ8ouxFoqbhIIYwH/BNEi1vvY3UiLHs= 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.133.124]) by sourceware.org (Postfix) with ESMTPS id 5AFED3842335 for ; Tue, 6 Dec 2022 17:57:08 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 5AFED3842335 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.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-448-CPi0kuKNO3W8lHLjTo9eTA-1; Tue, 06 Dec 2022 12:57:06 -0500 X-MC-Unique: CPi0kuKNO3W8lHLjTo9eTA-1 Received: by mail-qk1-f200.google.com with SMTP id x2-20020a05620a448200b006fa7dad5c1cso21648153qkp.10 for ; Tue, 06 Dec 2022 09:57:06 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=I14z9NilI1WIoSqCBJejMobn7cyf77BGwgv5lsjcPSM=; b=LEmYj8IDnp1TiUJkVRfBvdsxrEx3yRQYXvz4JAS1Yr0vh6oCUI2fygL43Pm0cYgT1w LkfYs4jZpNSbo0/kDzL4B/wYjK9dnYh1kfu8JP1siGb7ZPEcoZv2RCXjYodNvOeahxCF 5kDA9LjOpa6uz06u6l53JruMBXw3to1OcToXYCQaNGsYUZpU9aiNZeK/UKncQUtmXh/8 jBrgYi8u7UokiWJBgVtf5L5WnmkFC7/d6APanoZ6fJwCQLU1/yW9HpqakkI+pn/PLG02 aVJck8ZWkJ2/bgDiUftyanY8LQNtFY/rgIpwxc/Wm1eDBC/s4exCVIyrk/hPmcEE/Cnq SLkw== X-Gm-Message-State: ANoB5pltBWZiDmoCMqA7+vMi7F/8sxxOlvuHyjBhKfZp6+oeeBjK4Az6 lJXLqpP/gljqcZUtgrz7fHv3w1F8Zz771tgtNo+bUC5VhARkqIxg5x2rlKkzdGIkHVmcCx0OvE8 CmYBEwDg9BKPAwDxy2h8CUUj8qEjKfS2h+UTR/J5RFUJlhiScMfeAQISAzGvsnHGIRcw= X-Received: by 2002:ac8:7091:0:b0:3a5:2793:c242 with SMTP id y17-20020ac87091000000b003a52793c242mr81676425qto.14.1670349425582; Tue, 06 Dec 2022 09:57:05 -0800 (PST) X-Google-Smtp-Source: AA0mqf6dxNr1KYtWccuGPsgWIMVIjOgAOqnjywnHWgDmO0xK9zbcj1HzCVD6zS2+LOuoPmZSghEtVw== X-Received: by 2002:ac8:7091:0:b0:3a5:2793:c242 with SMTP id y17-20020ac87091000000b003a52793c242mr81676390qto.14.1670349425103; Tue, 06 Dec 2022 09:57:05 -0800 (PST) Received: from localhost.localdomain (ool-457670bb.dyn.optonline.net. [69.118.112.187]) by smtp.gmail.com with ESMTPSA id q19-20020ac87353000000b003a7b44dbd4esm5827488qtp.55.2022.12.06.09.57.04 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 06 Dec 2022 09:57:04 -0800 (PST) To: gcc-patches@gcc.gnu.org Cc: jason@redhat.com, Patrick Palka Subject: [PATCH] c++: NTTP object wrapper substitution fixes [PR103346, ...] Date: Tue, 6 Dec 2022 12:57:02 -0500 Message-Id: <20221206175702.987794-1-ppalka@redhat.com> X-Mailer: git-send-email 2.39.0.rc2 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-13.7 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_NONE, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) 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+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" This patch fixes some issues with substitution into a C++20 template parameter object wrapper: * The first testcase demonstrates a situation where the same_type_p assert in relevant case of tsubst_copy doesn't hold, because (partial) substitution of {int,} into the VIEW_CONVERT_EXPR wrapper yields A but substitution into the underlying TEMPLATE_PARM_INDEX is a nop and yields A due to tsubst's level == 1 early exit test. So this patch just gets rid of the assert; the type mismatch doesn't seem to be a problem in practice since the coercion is from one dependent type to another. * In the second testcase, dependent substitution into the underlying TEMPLATE_PARM_INDEX yields a CALL_EXPR with empty TREE_TYPE, which tsubst_copy doesn't expect. This patch fixes this by handling empty TREE_TYPE the same way as a non-const TREE_TYPE. Moreover, after this substitution we're left with a VIEW_CONVERT_EXPR wrapping a CALL_EXPR instead of a TEMPLATE_PARM_INDEX, which during the subsequent non-dependent substitution tsubst_copy doesn't expect either. So this patch also relaxes the tsubst_copy case to accept such VIEW_CONVERT_EXPR too. * In the third testcase, we end up never resolving the call to f.modify() since tsubst_copy doesn't do overload resolution. This patch fixes this by moving the handling of these VIEW_CONVERT_EXPR wrappers from tsubst_copy to tsubst_copy_and_build. And it turns out (at least according to our testsuite) that tsubst_copy doesn't directly need to handle the other kinds of NON_LVALUE_EXPR and VIEW_CONVERT_EXPR, so this patch also gets rid of the location_wrapper_p handling from tsubst_copy and moves the REF_PARENTHESIZED_P handling to tsubst_copy_and_build. After this patch, VIEW_CONVERT_EXPR substitution is ultimately just moved from tsubst_copy to tsubst_copy_and_build and made more permissive. Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for trunk? PR c++/103346 PR c++/104278 PR c++/102553 gcc/cp/ChangeLog: * pt.cc (tsubst_copy) : Remove same_type_p assert. Accept non-TEMPLATE_PARM_INDEX inner operand. Handle empty TREE_TYPE on substituted inner operand. Move all of this handling to ... (tsubst_copy_and_build): ... here and simplify accordingly. gcc/testsuite/ChangeLog: * g++.dg/cpp2a/nontype-class52a.C: New test. * g++.dg/cpp2a/nontype-class53.C: New test. * g++.dg/cpp2a/nontype-class54.C: New test. * g++.dg/cpp2a/nontype-class55.C: New test. --- gcc/cp/pt.cc | 100 +++++++----------- gcc/testsuite/g++.dg/cpp2a/nontype-class52a.C | 15 +++ gcc/testsuite/g++.dg/cpp2a/nontype-class53.C | 25 +++++ gcc/testsuite/g++.dg/cpp2a/nontype-class54.C | 23 ++++ gcc/testsuite/g++.dg/cpp2a/nontype-class55.C | 15 +++ 5 files changed, 119 insertions(+), 59 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp2a/nontype-class52a.C create mode 100644 gcc/testsuite/g++.dg/cpp2a/nontype-class53.C create mode 100644 gcc/testsuite/g++.dg/cpp2a/nontype-class54.C create mode 100644 gcc/testsuite/g++.dg/cpp2a/nontype-class55.C diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc index 2d8e4fdd4b5..021c2be9257 100644 --- a/gcc/cp/pt.cc +++ b/gcc/cp/pt.cc @@ -17257,59 +17257,6 @@ tsubst_copy (tree t, tree args, tsubst_flags_t complain, tree in_decl) /* Ordinary template template argument. */ return t; - case NON_LVALUE_EXPR: - case VIEW_CONVERT_EXPR: - { - /* Handle location wrappers by substituting the wrapped node - first, *then* reusing the resulting type. Doing the type - first ensures that we handle template parameters and - parameter pack expansions. */ - if (location_wrapper_p (t)) - { - tree op0 = tsubst_copy (TREE_OPERAND (t, 0), args, - complain, in_decl); - return maybe_wrap_with_location (op0, EXPR_LOCATION (t)); - } - tree op = TREE_OPERAND (t, 0); - if (code == VIEW_CONVERT_EXPR - && TREE_CODE (op) == TEMPLATE_PARM_INDEX) - { - /* Wrapper to make a C++20 template parameter object const. */ - op = tsubst_copy (op, args, complain, in_decl); - if (!CP_TYPE_CONST_P (TREE_TYPE (op))) - { - /* The template argument is not const, presumably because - it is still dependent, and so not the const template parm - object. */ - tree type = tsubst (TREE_TYPE (t), args, complain, in_decl); - gcc_checking_assert (same_type_ignoring_top_level_qualifiers_p - (type, TREE_TYPE (op))); - if (TREE_CODE (op) == CONSTRUCTOR - || TREE_CODE (op) == IMPLICIT_CONV_EXPR) - { - /* Don't add a wrapper to these. */ - op = copy_node (op); - TREE_TYPE (op) = type; - } - else - /* Do add a wrapper otherwise (in particular, if op is - another TEMPLATE_PARM_INDEX). */ - op = build1 (code, type, op); - } - return op; - } - /* force_paren_expr can also create a VIEW_CONVERT_EXPR. */ - else if (code == VIEW_CONVERT_EXPR && REF_PARENTHESIZED_P (t)) - { - op = tsubst_copy (op, args, complain, in_decl); - op = build1 (code, TREE_TYPE (op), op); - REF_PARENTHESIZED_P (op) = true; - return op; - } - /* We shouldn't see any other uses of these in templates. */ - gcc_unreachable (); - } - case CAST_EXPR: case REINTERPRET_CAST_EXPR: case CONST_CAST_EXPR: @@ -21568,13 +21515,48 @@ tsubst_copy_and_build (tree t, RETURN (t); case NON_LVALUE_EXPR: + gcc_checking_assert (location_wrapper_p (t)); + RETURN (maybe_wrap_with_location (RECUR (TREE_OPERAND (t, 0)), + EXPR_LOCATION (t))); + case VIEW_CONVERT_EXPR: - if (location_wrapper_p (t)) - /* We need to do this here as well as in tsubst_copy so we get the - other tsubst_copy_and_build semantics for a PARM_DECL operand. */ - RETURN (maybe_wrap_with_location (RECUR (TREE_OPERAND (t, 0)), - EXPR_LOCATION (t))); - /* fallthrough. */ + { + if (location_wrapper_p (t)) + RETURN (maybe_wrap_with_location (RECUR (TREE_OPERAND (t, 0)), + EXPR_LOCATION (t))); + tree op = TREE_OPERAND (t, 0); + if (REF_PARENTHESIZED_P (t)) + { + /* force_paren_expr can also create a VIEW_CONVERT_EXPR. */ + op = RECUR (op); + op = build1 (VIEW_CONVERT_EXPR, TREE_TYPE (op), op); + REF_PARENTHESIZED_P (op) = true; + RETURN (op); + } + /* We're dealing with a wrapper to make a C++20 template parameter + object const. */ + op = RECUR (op); + if (TREE_TYPE (op) == NULL_TREE + || !CP_TYPE_CONST_P (TREE_TYPE (op))) + { + /* The template argument is not const, presumably because + it is still dependent, and so not the const template parm + object. */ + tree type = tsubst (TREE_TYPE (t), args, complain, in_decl); + if (TREE_CODE (op) == CONSTRUCTOR + || TREE_CODE (op) == IMPLICIT_CONV_EXPR) + { + /* Don't add a wrapper to these. */ + op = copy_node (op); + TREE_TYPE (op) = type; + } + else + /* Do add a wrapper otherwise (in particular, if op is + another TEMPLATE_PARM_INDEX). */ + op = build1 (VIEW_CONVERT_EXPR, type, op); + } + RETURN (op); + } default: /* Handle Objective-C++ constructs, if appropriate. */ diff --git a/gcc/testsuite/g++.dg/cpp2a/nontype-class52a.C b/gcc/testsuite/g++.dg/cpp2a/nontype-class52a.C new file mode 100644 index 00000000000..ae5d5df70ac --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/nontype-class52a.C @@ -0,0 +1,15 @@ +// A version of nontype-class52.C where explicit template arguments are +// given in the call to f (which during deduction need to be partially +// substituted into the NTTP object V in f's signature). +// { dg-do compile { target c++20 } } + +template struct A { }; + +template struct B { }; + +template V> void f(B); + +int main() { + constexpr A a; + f(B{}); +} diff --git a/gcc/testsuite/g++.dg/cpp2a/nontype-class53.C b/gcc/testsuite/g++.dg/cpp2a/nontype-class53.C new file mode 100644 index 00000000000..9a6398c5f57 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/nontype-class53.C @@ -0,0 +1,25 @@ +// PR c++/103346 +// { dg-do compile { target c++20 } } + +struct Item {}; + +template +struct Sequence { }; + +template +using ItemSequence = Sequence; + +template +constexpr auto f() { + constexpr auto l = [](Item item) { return item; }; + return ItemSequence{}; +} + +using ty0 = decltype(f<>()); +using ty0 = ItemSequence<>; + +using ty1 = decltype(f<{}>()); +using ty1 = ItemSequence<{}>; + +using ty3 = decltype(f<{}, {}, {}>()); +using ty3 = ItemSequence<{}, {}, {}>; diff --git a/gcc/testsuite/g++.dg/cpp2a/nontype-class54.C b/gcc/testsuite/g++.dg/cpp2a/nontype-class54.C new file mode 100644 index 00000000000..8127b1f5426 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/nontype-class54.C @@ -0,0 +1,23 @@ +// PR c++/104278 +// { dg-do compile { target c++20 } } + +struct foo { + int value; + constexpr foo modify() const { return { value + 1 }; } +}; + +template +struct bar { + static void run() { } +}; + +template +struct qux { + static void run() { + bar::run(); + } +}; + +int main() { + qux::run(); +} diff --git a/gcc/testsuite/g++.dg/cpp2a/nontype-class55.C b/gcc/testsuite/g++.dg/cpp2a/nontype-class55.C new file mode 100644 index 00000000000..afcb3d64495 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/nontype-class55.C @@ -0,0 +1,15 @@ +// PR c++/102553 +// { dg-do compile { target c++20 } } + +struct s1{}; +template inline constexpr s1 ch{}; + +template struct s2{}; +template using alias1 = s2; + +template +void general(int n) { + alias1>{}; +} + +template void general(int);