From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 178963858D39 for ; Fri, 4 Mar 2022 18:25:09 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 178963858D39 Received: from mail-qk1-f197.google.com (mail-qk1-f197.google.com [209.85.222.197]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-120-Ns60XPrcOaOfWbGaImFZXA-1; Fri, 04 Mar 2022 13:25:05 -0500 X-MC-Unique: Ns60XPrcOaOfWbGaImFZXA-1 Received: by mail-qk1-f197.google.com with SMTP id t10-20020a37aa0a000000b00605b9764f71so6107700qke.22 for ; Fri, 04 Mar 2022 10:25:05 -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=l07eVs0sotH4ep3xqtjDhI5Za96HtTLSxcCUBmdB1sw=; b=dYYz8WYgbzcAqdyf8fzppfqW9vU6MCUaaQh9fSS3KXFplehFmDA68sEw468u2Rqi4e HGaeTqSdxAoFGmoo8R6qoBndKKOCmIA4AEwZwFmUyy2Njm6ckrKjomMhA6dXz6tXLhY6 PaxL4S+WqFRpuCciyxfdsDGVoo1xk8iiylWT1Zg4dr9mwVZpkGeWp+GYDrosHHHAucMY Cf+vxKbCAHfBnxtYhiuQLbtbaFovsxPro+MDXN+Xe2raMzm299Srmky6tEsEfHkYoIBT irz3EH6Fsmq/SHsSHS+/d3wZy4pDLbhUkaRDjOOq/Gxcf5HXwcGYB/hFaAPISEozUblO TpnA== X-Gm-Message-State: AOAM532yQ9m00V/UcavU8WmHrsSIvXPDQ4zuo9n/VCVh/JbzEAJ03bAd mWPBZ/ms7uq3w6vwjgc2jLhWWY4XJMkeeIVEDCKMDPN94RjoCXx45jt3gET5LEQ8bPJCHs9Rp0S Wwk7MtVWHzVBW2+h4SSX8sFzrVNMq0m8SYjVCkkcsXM2PY/CEET9SDa5PVOdMhG6+HsY= X-Received: by 2002:a05:620a:e1c:b0:47d:87eb:18b2 with SMTP id y28-20020a05620a0e1c00b0047d87eb18b2mr3334040qkm.527.1646418304323; Fri, 04 Mar 2022 10:25:04 -0800 (PST) X-Google-Smtp-Source: ABdhPJw4/dw4LpSISCedSidKpLuEUJ9GOY6Kih+3yY4HB2PDOwTC/5kQ0YhO/QDU2G8Yk62YSyYqiA== X-Received: by 2002:a05:620a:e1c:b0:47d:87eb:18b2 with SMTP id y28-20020a05620a0e1c00b0047d87eb18b2mr3334022qkm.527.1646418303895; Fri, 04 Mar 2022 10:25:03 -0800 (PST) Received: from localhost.localdomain (ool-18e40894.dyn.optonline.net. [24.228.8.148]) by smtp.gmail.com with ESMTPSA id h17-20020ac85e11000000b002ddd5fab777sm3984160qtx.41.2022.03.04.10.25.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 04 Mar 2022 10:25:03 -0800 (PST) From: Patrick Palka To: gcc-patches@gcc.gnu.org Subject: [PATCH] c++: detecting copy-init context during CTAD [PR102137] Date: Fri, 4 Mar 2022 13:24:36 -0500 Message-Id: <20220304182436.122498-1-ppalka@redhat.com> X-Mailer: git-send-email 2.35.1.354.g715d08a9e5 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII" X-Spam-Status: No, score=-14.5 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_H5, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, 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: Fri, 04 Mar 2022 18:25:11 -0000 Here we're failing to communicate to cp_finish_decl from tsubst_expr that we're in a copy-initialization context (via the LOOKUP_ONLYCONVERTING flag), which causes do_class_deduction to always consider explicit deduction guides when performing CTAD for a templated variable initializer. We could fix this by passing LOOKUP_ONLYCONVERTING appropriately when calling cp_finish_decl from tsubst_expr, but it seems do_class_deduction can determine if we're in a copy-init context by simply inspecting the initializer, and thus render its flags parameter unnecessary, which is what this patch implements. (If we were to fix this in tsubst_expr instead, I think we'd have to inspect the initializer in the same way in order to detect a copy-init context?) Bootstrapped and regtestd on x86_64-pc-linux-gnu, does this look OK for trunk? PR c++/102137 gcc/cp/ChangeLog: * cp-tree.h (do_auto_deduction): Remove flags parameter. * decl.cc (cp_finish_decl): Adjust call to do_auto_deduction. * pt.cc (convert_template_argument): Likewise. (do_class_deduction): Remove flags parameter and instead determine if we're in a copy-init context by inspecting the initializer. (do_auto_deduction): Adjust call to do_class_deduction. gcc/testsuite/ChangeLog: * g++.dg/cpp1z/class-deduction108.C: New test. --- gcc/cp/cp-tree.h | 3 +- gcc/cp/decl.cc | 2 +- gcc/cp/pt.cc | 23 +++++++------ .../g++.dg/cpp1z/class-deduction108.C | 32 +++++++++++++++++++ 4 files changed, 48 insertions(+), 12 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp1z/class-deduction108.C diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h index ac723901098..c2ef6544389 100644 --- a/gcc/cp/cp-tree.h +++ b/gcc/cp/cp-tree.h @@ -7279,8 +7279,7 @@ extern tree do_auto_deduction (tree, tree, tree, = tf_warning_or_error, auto_deduction_context = adc_unspecified, - tree = NULL_TREE, - int = LOOKUP_NORMAL); + tree = NULL_TREE); extern tree type_uses_auto (tree); extern tree type_uses_auto_or_concept (tree); extern void append_type_to_template_for_access_check (tree, tree, tree, diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc index 199ac768d43..152f657e9f2 100644 --- a/gcc/cp/decl.cc +++ b/gcc/cp/decl.cc @@ -8039,7 +8039,7 @@ cp_finish_decl (tree decl, tree init, bool init_const_expr_p, outer_targs = DECL_TI_ARGS (decl); type = TREE_TYPE (decl) = do_auto_deduction (type, d_init, auto_node, tf_warning_or_error, adc, - outer_targs, flags); + outer_targs); if (type == error_mark_node) return; if (TREE_CODE (type) == FUNCTION_TYPE) diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc index d94d4538faa..66fc8cacdc6 100644 --- a/gcc/cp/pt.cc +++ b/gcc/cp/pt.cc @@ -8567,8 +8567,7 @@ convert_template_argument (tree parm, can happen in the context of -fnew-ttp-matching. */; else if (tree a = type_uses_auto (t)) { - t = do_auto_deduction (t, arg, a, complain, adc_unify, args, - LOOKUP_IMPLICIT); + t = do_auto_deduction (t, arg, a, complain, adc_unify, args); if (t == error_mark_node) return error_mark_node; } @@ -29832,8 +29831,7 @@ ctad_template_p (tree tmpl) type. */ static tree -do_class_deduction (tree ptype, tree tmpl, tree init, - int flags, tsubst_flags_t complain) +do_class_deduction (tree ptype, tree tmpl, tree init, tsubst_flags_t complain) { /* We should have handled this in the caller. */ if (DECL_TEMPLATE_TEMPLATE_PARM_P (tmpl)) @@ -29881,6 +29879,13 @@ do_class_deduction (tree ptype, tree tmpl, tree init, if (type_dependent_expression_p (init)) return ptype; + bool copy_init_p = true; + if (!init + || TREE_CODE (init) == TREE_LIST + || (BRACE_ENCLOSED_INITIALIZER_P (init) + && CONSTRUCTOR_IS_DIRECT_INIT (init))) + copy_init_p = false; + tree type = TREE_TYPE (tmpl); bool try_list_ctor = false; @@ -29929,7 +29934,7 @@ do_class_deduction (tree ptype, tree tmpl, tree init, /* Prune explicit deduction guides in copy-initialization context (but not copy-list-initialization). */ bool elided = false; - if (!list_init_p && (flags & LOOKUP_ONLYCONVERTING)) + if (!list_init_p && copy_init_p) { for (lkp_iterator iter (cands); !elided && iter; ++iter) if (DECL_NONCONVERTING_P (STRIP_TEMPLATE (*iter))) @@ -30007,7 +30012,7 @@ do_class_deduction (tree ptype, tree tmpl, tree init, } /* [over.match.list]/1: In copy-list-initialization, if an explicit constructor is chosen, the initialization is ill-formed. */ - else if (flags & LOOKUP_ONLYCONVERTING) + else if (copy_init_p) { if (DECL_NONCONVERTING_P (fndecl)) { @@ -30045,7 +30050,7 @@ do_class_deduction (tree ptype, tree tmpl, tree init, /* Replace occurrences of 'auto' in TYPE with the appropriate type deduced from INIT. AUTO_NODE is the TEMPLATE_TYPE_PARM used for 'auto' in TYPE. The CONTEXT determines the context in which auto deduction is performed - and is used to control error diagnostics. FLAGS are the LOOKUP_* flags. + and is used to control error diagnostics. OUTER_TARGS is used during template argument deduction (context == adc_unify) to properly substitute the result. It's also used in the adc_unify and @@ -30058,7 +30063,7 @@ do_class_deduction (tree ptype, tree tmpl, tree init, tree do_auto_deduction (tree type, tree init, tree auto_node, tsubst_flags_t complain, auto_deduction_context context, - tree outer_targs, int flags) + tree outer_targs) { if (init == error_mark_node) return error_mark_node; @@ -30079,7 +30084,7 @@ do_auto_deduction (tree type, tree init, tree auto_node, if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node)) /* C++17 class template argument deduction. */ - return do_class_deduction (type, tmpl, init, flags, complain); + return do_class_deduction (type, tmpl, init, complain); if (init == NULL_TREE || TREE_TYPE (init) == NULL_TREE) /* Nothing we can do with this, even in deduction context. */ diff --git a/gcc/testsuite/g++.dg/cpp1z/class-deduction108.C b/gcc/testsuite/g++.dg/cpp1z/class-deduction108.C new file mode 100644 index 00000000000..f76671c287a --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1z/class-deduction108.C @@ -0,0 +1,32 @@ +// PR c++/102137 +// { dg-do compile { target c++17 } } + +template +struct A { + A(); + A(int); +}; + +explicit A(int) -> A; +explicit A() -> A; + +template class T> +void f() { + T x1 = 0; // { dg-error "deduction|no match" } + T x2 = {0}; // { dg-error "explicit deduction guide" } + T x3(0); + T x4{0}; + T x5; +} + +template +void g(T t) { + A a1 = t; // { dg-error "deduction|no match" } + A a2 = {t}; // { dg-error "explicit deduction guide" } + A a3(t); + A a4{t}; + A a5; +} + +template void f(); +template void g(int); -- 2.35.1.354.g715d08a9e5