From patchwork Fri Jan 19 18:33:36 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 189653 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:2bc4:b0:101:a8e8:374 with SMTP id hx4csp1200930dyb; Fri, 19 Jan 2024 10:37:07 -0800 (PST) X-Google-Smtp-Source: AGHT+IE8giPS4YxbjgI+bgFEGqE67qv7Ca0hXn1rRVdW1AlSZPT5iL1yZE7xkQ66z8Q63DSvS8Be X-Received: by 2002:a05:6870:332b:b0:210:99d2:d5ad with SMTP id x43-20020a056870332b00b0021099d2d5admr188592oae.37.1705689426912; Fri, 19 Jan 2024 10:37:06 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1705689426; cv=pass; d=google.com; s=arc-20160816; b=Aw3qCVxYogqBp1GFJKS1ytAKf86nRMfXyNFpX6QeCLEVlF7cE7LbgLifJgIo6ny3+9 LmK+cmcDQpMXyC4aNngeybo6tF88i1wh4Gq09g0NQrYvpnonkCpy0up/3cvmAMPHzlOw F+jm+AP+f2oXp5JO6J1DnzaMTfxITzih6TKLcJ4F+N3pmecnXCdAF8HMRAz89zZ1zq+x AgZs7XIeRpiWf1/JftXwTb9/PkjUQRFjHUPeINqWsctn1VHHQsJYAeTQWcCcl/tz+BpL O7WzdGAvza0BGtRl1DrjNG201qKSpze0HMFnxV6QoxmCIBOGWmeuq16bzRKllOesmkY4 sDwA== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:message-id:date:subject:to:from:dkim-signature :arc-filter:dmarc-filter:delivered-to; bh=9PQMBAFc8wmCyhgj5vZ0KYCxgZ0i7vdKjh2INo12bBM=; fh=hPrbWPhweUx4V0GV9uXJqbyAzg2ABmTz7kczrAQqMmM=; b=zqMNDRYTIhQtvx8ym4u8AN5teyZO5RM208GUtOyPdLQ9paBWrO+ppj26Vlq95iVIrr hN11bfdsxJ79XKFgY7dEpmzzkRb0TVC46gLNpACW0qPZBtjqo2Xz/sbByGjYCuMPVQEV 0EKkykPvOoEqgfscVuRWb0opbLuR7UCDhefYeCHc0pY3FSv7e75LRuOtUJ0XrgZTMHBd ugFg8YDXtNHUnow2m3fMI7DtDg3NPx3jA/Sfyb4wX+IPSoz/PWLZlc964C36j2vod8E/ tF2XHSg9rOwD1Jvd8llrySvq/oVxmvKsA6tDMYjJEPy2dkt5guM8k1RaTlxz0udL/sVg 4bSQ== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=M5C5NH5C; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id h6-20020a05620a400600b007836be0249bsi8783718qko.323.2024.01.19.10.37.06 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 Jan 2024 10:37:06 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@redhat.com header.s=mimecast20190719 header.b=M5C5NH5C; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=redhat.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 92EEC385841B for ; Fri, 19 Jan 2024 18:37:06 +0000 (GMT) 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 814013858419 for ; Fri, 19 Jan 2024 18:36:11 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 814013858419 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 814013858419 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1705689374; cv=none; b=gE0DvVRZHhbcHTL0oH8SFefInTrPSzimTiGEGDo4K/kzpKMPDG4LOGLqX9L2bY7fzDu/KqyIKjQJjaI4uxc1ZbP3QqI9m4ynUXBEtYFX/th+NplhVo9O9mJc05uwgCHqh6+pQtPuFMj53Nen5hkeapriiokDPVMvL7VQmzEJ5cA= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1705689374; c=relaxed/simple; bh=T8Yxl3gYfB+7AFSfbb3rgSWR5qXuplfofGjDla41Pz0=; h=DKIM-Signature:From:To:Subject:Date:Message-Id:MIME-Version; b=mYU87msCuMItmrFynHtMQWf9x/F8zv3mH2Kv05GRybiCpZcB+RoSGqHsce8iQU/AQiSG2A1AzQSf3OddXWsj+enMmsOhSQckMjHp/QhFRHqARjjBHh7S/9f3qaVZ8HRYlHXwa6a3CqogAnfcdXT3gTlpe1IqX2W78NsFuV/c7Wk= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1705689371; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=9PQMBAFc8wmCyhgj5vZ0KYCxgZ0i7vdKjh2INo12bBM=; b=M5C5NH5CN/5qtPdopW7fzvxbDlvfagQpaBGCU5TfLjv143xJigpMDAcH12+0r9UjJl0TiO 9TDyqeJkWxbCix0bKKKHEc6G+AFo8Atf2JE2f4+VuMqEXR3f+Rcal6ApiV7GdUnAysqaRJ okOExhotF4PQ3bwGRoKtDtAwDVHftIc= Received: from mail-qv1-f69.google.com (mail-qv1-f69.google.com [209.85.219.69]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-64-ExGOAmM1OSuZoFTnS_jl1Q-1; Fri, 19 Jan 2024 13:33:40 -0500 X-MC-Unique: ExGOAmM1OSuZoFTnS_jl1Q-1 Received: by mail-qv1-f69.google.com with SMTP id 6a1803df08f44-68058b0112cso17799396d6.1 for ; Fri, 19 Jan 2024 10:33:40 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1705689220; x=1706294020; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=9PQMBAFc8wmCyhgj5vZ0KYCxgZ0i7vdKjh2INo12bBM=; b=WjEHBGKjFchPkFk6dampHWZR016MZwU2ACOneYBBJjBswf5nBSjJkzmuXlviylrKAc OadlN5bULZGpeJrlps2g399QcTF7BZ1tYCAhF8l6bzTGzI9GlRaZclIM43W1xyw+OjjI zBXz5UEAjri8PG7iLP3XrJemtJz8GJlJ9iIM08b6nbeGyKC56pMClUsfqiKcJZQHJJHI GJIM3H8tIDj9Wzbh+SGLFILw5hbYGf4TAEwijzBBqIpS85H0TqWGa9xNqyrxK9beIvPO Yj8jQsuHc9LVc7x8ax9RHZlU2JtLpQmjp9ti0AutAz+V2ywucY3+0wMJdyY/qQDCB3LG 7cDg== X-Gm-Message-State: AOJu0YxLbTP79+qO+me5i/hyQxclWzacqr/odO10Q3oFF9Gbi4THRF3h 6sUHK3PtTsfFO+yDwSc7qDxYcvY3QUeHe3DYh/dVsTB9DbiaTuK8kPONebDzUIe1nwJdouOxrSK TgS6KZWsQnvCo4+sbltlrMVkSplwCWIak0u15aqgUSs+3RWGhmI5njm47qvpB9SjemSqxI/dzmu fjrJhj0wbIJtxlZ7592fKhjJhbP2EG53MDzq0= X-Received: by 2002:a0c:cb8e:0:b0:680:fa99:9edf with SMTP id p14-20020a0ccb8e000000b00680fa999edfmr289374qvk.68.1705689219818; Fri, 19 Jan 2024 10:33:39 -0800 (PST) X-Received: by 2002:a0c:cb8e:0:b0:680:fa99:9edf with SMTP id p14-20020a0ccb8e000000b00680fa999edfmr289360qvk.68.1705689219350; Fri, 19 Jan 2024 10:33:39 -0800 (PST) Received: from jason.com (130-44-146-16.s12558.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.146.16]) by smtp.gmail.com with ESMTPSA id lu19-20020a0562145a1300b0068200592351sm935592qvb.89.2024.01.19.10.33.38 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 19 Jan 2024 10:33:38 -0800 (PST) From: Jason Merrill To: gcc-patches@gcc.gnu.org Subject: [pushed] c++: alias template argument conversion [PR112632] Date: Fri, 19 Jan 2024 13:33:36 -0500 Message-Id: <20240119183336.3505523-1-jason@redhat.com> X-Mailer: git-send-email 2.39.3 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.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_H3, RCVD_IN_MSPIKE_WL, RCVD_IN_SORBS_WEB, SPF_HELO_NONE, SPF_NONE, TXREP, T_SCC_BODY_TEXT_LINE 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.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1788544996047090170 X-GMAIL-MSGID: 1788544996047090170 Tested x86_64-pc-linux-gnu, applying to trunk. -- 8< -- We've had a problem with lost conversions to template parameter types for a while now; looking at this PR, it occurred to me that the problem is really with alias (and concept) templates, since we do substitution of dependent arguments into them in a way that we don't for other templates. And fixing that specific problem is a lot simpler than adding IMPLICIT_CONV_EXPR around all dependent template arguments the way I gave up on for 111357. The other part of the fix was changing tsubst_expr to actually call convert_nontype_argument instead of assuming it will eventually happen. I waffled about stripping the forced conversion when !force_conv vs. skipping them in iterative_hash_template_arg and template_args_equal (like we already do for some other conversions) and decided to go with the former, but that isn't a strong preference if it turns out to be somehow problematic. PR c++/112632 PR c++/112594 PR c++/111357 PR c++/104594 PR c++/67898 gcc/cp/ChangeLog: * cp-tree.h (IMPLICIT_CONV_EXPR_FORCED): New. * pt.cc (expand_integer_pack): Remove 111357 workaround. (maybe_convert_nontype_argument): Add force parm. (convert_template_argument): Handle alias template args specially. (tsubst_expr): Don't ignore IMPLICIT_CONV_EXPR_NONTYPE_ARG. * error.cc (dump_expr) [CASE_CONVERT]: Handle null optype. gcc/testsuite/ChangeLog: * g++.dg/cpp0x/alias-decl-nontype1.C: New test. * g++.dg/cpp2a/concepts-narrowing1.C: New test. * g++.dg/cpp2a/nontype-class63.C: New test. * g++.dg/cpp2a/nontype-class63a.C: New test. --- gcc/cp/cp-tree.h | 5 ++ gcc/cp/error.cc | 4 +- gcc/cp/pt.cc | 48 +++++++++++++------ .../g++.dg/cpp0x/alias-decl-nontype1.C | 9 ++++ .../g++.dg/cpp2a/concepts-narrowing1.C | 16 +++++++ gcc/testsuite/g++.dg/cpp2a/nontype-class63.C | 24 ++++++++++ gcc/testsuite/g++.dg/cpp2a/nontype-class63a.C | 24 ++++++++++ 7 files changed, 114 insertions(+), 16 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp0x/alias-decl-nontype1.C create mode 100644 gcc/testsuite/g++.dg/cpp2a/concepts-narrowing1.C create mode 100644 gcc/testsuite/g++.dg/cpp2a/nontype-class63.C create mode 100644 gcc/testsuite/g++.dg/cpp2a/nontype-class63a.C base-commit: e04376b336502016456eaf4e90c3ea792c77c8df diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h index d9b14d7c4f5..60e6dafc549 100644 --- a/gcc/cp/cp-tree.h +++ b/gcc/cp/cp-tree.h @@ -4717,6 +4717,11 @@ get_vec_init_expr (tree t) #define IMPLICIT_CONV_EXPR_BRACED_INIT(NODE) \ (TREE_LANG_FLAG_2 (IMPLICIT_CONV_EXPR_CHECK (NODE))) +/* True if NODE represents a conversion forced to be represented in + maybe_convert_nontype_argument, i.e. for an alias template. */ +#define IMPLICIT_CONV_EXPR_FORCED(NODE) \ + (TREE_LANG_FLAG_3 (IMPLICIT_CONV_EXPR_CHECK (NODE))) + /* Nonzero means that an object of this type cannot be initialized using an initializer list. */ #define CLASSTYPE_NON_AGGREGATE(NODE) \ diff --git a/gcc/cp/error.cc b/gcc/cp/error.cc index 52e24fb086c..d3fcac70ea1 100644 --- a/gcc/cp/error.cc +++ b/gcc/cp/error.cc @@ -2673,6 +2673,8 @@ dump_expr (cxx_pretty_printer *pp, tree t, int flags) tree ttype = TREE_TYPE (t); tree optype = TREE_TYPE (op); + if (!optype) + optype = unknown_type_node; if (TREE_CODE (ttype) != TREE_CODE (optype) && INDIRECT_TYPE_P (ttype) @@ -2691,7 +2693,7 @@ dump_expr (cxx_pretty_printer *pp, tree t, int flags) else dump_unary_op (pp, "&", t, flags); } - else if (!same_type_p (TREE_TYPE (op), TREE_TYPE (t))) + else if (!same_type_p (optype, ttype)) { /* It is a cast, but we cannot tell whether it is a reinterpret or static cast. Use the C style notation. */ diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc index f82d018c981..fbbca469219 100644 --- a/gcc/cp/pt.cc +++ b/gcc/cp/pt.cc @@ -3760,13 +3760,6 @@ expand_integer_pack (tree call, tree args, tsubst_flags_t complain, { if (hi != ohi) { - /* Work around maybe_convert_nontype_argument not doing this for - dependent arguments. Don't use IMPLICIT_CONV_EXPR_NONTYPE_ARG - because that will make tsubst_expr ignore it. */ - tree type = tsubst (TREE_TYPE (ohi), args, complain, in_decl); - if (!TREE_TYPE (hi) || !same_type_p (type, TREE_TYPE (hi))) - hi = build1 (IMPLICIT_CONV_EXPR, type, hi); - call = copy_node (call); CALL_EXPR_ARG (call, 0) = hi; } @@ -8457,23 +8450,30 @@ convert_wildcard_argument (tree parm, tree arg) conversion for the benefit of cp_tree_equal. */ static tree -maybe_convert_nontype_argument (tree type, tree arg) +maybe_convert_nontype_argument (tree type, tree arg, bool force) { /* Auto parms get no conversion. */ if (type_uses_auto (type)) return arg; + /* ??? Do we need to push the IMPLICIT_CONV_EXPR into the pack expansion? + That would complicate other things, and it doesn't seem necessary. */ + if (TREE_CODE (arg) == EXPR_PACK_EXPANSION) + return arg; /* We don't need or want to add this conversion now if we're going to use the argument for deduction. */ - if (value_dependent_expression_p (arg)) + if (!value_dependent_expression_p (arg)) + force = false; + else if (!force) return arg; type = cv_unqualified (type); tree argtype = TREE_TYPE (arg); - if (same_type_p (type, argtype)) + if (argtype && same_type_p (type, argtype)) return arg; arg = build1 (IMPLICIT_CONV_EXPR, type, arg); IMPLICIT_CONV_EXPR_NONTYPE_ARG (arg) = true; + IMPLICIT_CONV_EXPR_FORCED (arg) = force; return arg; } @@ -8741,6 +8741,22 @@ convert_template_argument (tree parm, if (t != TREE_TYPE (parm)) t = canonicalize_type_argument (t, complain); + /* We need to handle arguments for alias or concept templates + differently: we need to force building an IMPLICIT_CONV_EXPR, because + these arguments are going to be substituted directly into the + dependent type; they might not get another chance at + convert_nontype_argument. But if the argument ends up here again for + a template that isn't one of those, remove the conversion for + consistency between naming the same dependent type directly or through + an alias. */ + bool force_conv = in_decl && (DECL_ALIAS_TEMPLATE_P (in_decl) + || concept_definition_p (in_decl)); + if (!force_conv + && TREE_CODE (orig_arg) == IMPLICIT_CONV_EXPR + && IMPLICIT_CONV_EXPR_FORCED (orig_arg) + && same_type_p (TREE_TYPE (orig_arg), t)) + orig_arg = TREE_OPERAND (orig_arg, 0); + if (!type_dependent_expression_p (orig_arg) && !uses_template_parms (t)) /* We used to call digest_init here. However, digest_init @@ -8757,10 +8773,9 @@ convert_template_argument (tree parm, else { val = canonicalize_expr_argument (orig_arg, complain); - val = maybe_convert_nontype_argument (t, val); + val = maybe_convert_nontype_argument (t, val, force_conv); } - if (val == NULL_TREE) val = error_mark_node; else if (val == error_mark_node && (complain & tf_error)) @@ -20056,9 +20071,12 @@ tsubst_expr (tree t, tree args, tsubst_flags_t complain, tree in_decl) RETURN (retval); } if (IMPLICIT_CONV_EXPR_NONTYPE_ARG (t)) - /* We'll pass this to convert_nontype_argument again, we don't need - to actually perform any conversion here. */ - RETURN (expr); + { + tree r = convert_nontype_argument (type, expr, complain); + if (r == NULL_TREE) + r = error_mark_node; + RETURN (r); + } int flags = LOOKUP_IMPLICIT; if (IMPLICIT_CONV_EXPR_DIRECT_INIT (t)) flags = LOOKUP_NORMAL; diff --git a/gcc/testsuite/g++.dg/cpp0x/alias-decl-nontype1.C b/gcc/testsuite/g++.dg/cpp0x/alias-decl-nontype1.C new file mode 100644 index 00000000000..3a9f0de834e --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp0x/alias-decl-nontype1.C @@ -0,0 +1,9 @@ +// PR c++/67898 +// { dg-do compile { target c++11 } } + +template struct A; +template using B = A; + +using type = A; +using type = B; // incorrectly resolves to A + // instead of A diff --git a/gcc/testsuite/g++.dg/cpp2a/concepts-narrowing1.C b/gcc/testsuite/g++.dg/cpp2a/concepts-narrowing1.C new file mode 100644 index 00000000000..dcfe2729f70 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/concepts-narrowing1.C @@ -0,0 +1,16 @@ +// PR c++/104594 +// { dg-do compile { target c++20 } } + +template +concept Geometry = (DIM_FROM == -1); + +template +requires Geometry +auto GaussNewton(const INIT& init) -> void {} + +template +struct X { + static constexpr int n = N; +}; + +int main() { GaussNewton(X<-1>{}); } // { dg-error "no match|narrowing" } diff --git a/gcc/testsuite/g++.dg/cpp2a/nontype-class63.C b/gcc/testsuite/g++.dg/cpp2a/nontype-class63.C new file mode 100644 index 00000000000..2e617396912 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/nontype-class63.C @@ -0,0 +1,24 @@ +// PR c++/112632 +// { dg-do compile { target c++20 } } + +template +inline constexpr bool C = true; + +struct n { + constexpr n(int a) : i(a) {} + int i; +}; + +template +using get_n_i_type = decltype(N.i); + +template +int f() { + using iii = get_n_i_type; +#if 1 // Change to 0 and this compiles + static_assert(C); +#endif + return iii{}; +} + +template int f<3>(); diff --git a/gcc/testsuite/g++.dg/cpp2a/nontype-class63a.C b/gcc/testsuite/g++.dg/cpp2a/nontype-class63a.C new file mode 100644 index 00000000000..489a90d9fdb --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/nontype-class63a.C @@ -0,0 +1,24 @@ +// PR c++/112594 +// { dg-do compile { target c++20 } } + +template +concept C = true; + +struct n { + constexpr n(int) {} + static int i; +}; + +template +using get_n_i_type = decltype(N.i); + +template +int f() { + using iii = get_n_i_type; +#if 1 // Change to 0 and this compiles + static_assert(C); +#endif + return iii{}; +} + +template int f<3>();