From patchwork Tue May 3 17:40:21 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Patrick Palka X-Patchwork-Id: 53427 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 67D193959E48 for ; Tue, 3 May 2022 17:40:56 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 67D193959E48 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1651599656; bh=1KYvCHAT5MLiN7h4oyI8o0uwCktjVR70wymx/8TOMjk=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=BRf9XM8RwkOpdcP+PUHAzEU0WAONmgu4vwH40ygRlAafeti3izzCYo9rforVCrN4G U/E5rkewnFX3TVlCoGMNleINf3D3/WKKh/BETzp/viLy17KNy7lmdTKW4G1KMz5LX2 milodzNEaMgIx5aerqSrRKVfgCxHidutILJYOWcI= 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 68F693955C92 for ; Tue, 3 May 2022 17:40:26 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 68F693955C92 Received: from mail-qv1-f72.google.com (mail-qv1-f72.google.com [209.85.219.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-412-oTUk4FrrOt2StLUs_NI-Ew-1; Tue, 03 May 2022 13:40:24 -0400 X-MC-Unique: oTUk4FrrOt2StLUs_NI-Ew-1 Received: by mail-qv1-f72.google.com with SMTP id s19-20020ad44b33000000b00456107e1120so13834650qvw.0 for ; Tue, 03 May 2022 10:40:24 -0700 (PDT) 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=1KYvCHAT5MLiN7h4oyI8o0uwCktjVR70wymx/8TOMjk=; b=Cp4UM/Klgsn2kgRDZQJwE5nXf2I5KgRpJS0U9k5E/DJ97v1jPispCRvHWudRNDj978 HDjHVTZZXd3/JSUBe+4TBEvucza7ogklpCt2G76mf5FwwU3RSWtBSBUD4L6um3nrRTND +/FEM5c2TVkJjrYDYRh3YFDUzhygRDyOwcYY52/OvD9W70QU+Kf6INqyMAeSyiNs/TVO nokmoWDlE5P3zGASg+ssBGigY2e4CRTjYWDuujzRigPDOhf2MJdEEcPRbo8Ak5YOEU1u kLtuaIh8GBRsu0tw8lmiYg3RTz5R9CrfuaZTi56s2h2FCMX3KpXYGcz3iwEC1FZufn8u cM5A== X-Gm-Message-State: AOAM530gSjfruOj5FfHX/knbNpkYF7ZxvRQc7v0BQadvCw0TDREip6js AFB0ST09zk+YthGmXJVTIwv0PYTLSAE8hem11x5Pfd7MjEo1clVKTsWueo+Ix3CWk0A4o1MpKnp y52A8i7BmqScGlAaxQWP/FT33jJsG5gvuo2BhiJw9mrs8UdoD+rRlG5Z58q0bf+BSiZQ= X-Received: by 2002:a05:622a:30a:b0:2f2:4a5:59b1 with SMTP id q10-20020a05622a030a00b002f204a559b1mr16056518qtw.546.1651599623829; Tue, 03 May 2022 10:40:23 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyeeIWjDL2e8y9o4SjqsigXPx8vRFVpAwmaDP81fLHRYUX9nAEgw1josbrTSbBzfgcJtrfKBQ== X-Received: by 2002:a05:622a:30a:b0:2f2:4a5:59b1 with SMTP id q10-20020a05622a030a00b002f204a559b1mr16056494qtw.546.1651599623514; Tue, 03 May 2022 10:40:23 -0700 (PDT) Received: from localhost.localdomain (ool-18e40894.dyn.optonline.net. [24.228.8.148]) by smtp.gmail.com with ESMTPSA id f4-20020ac84984000000b002f39b99f66bsm5994844qtq.5.2022.05.03.10.40.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 03 May 2022 10:40:23 -0700 (PDT) To: gcc-patches@gcc.gnu.org Subject: [PATCH] c++: make finish non_static_data_member SFINAE friendly [PR105351] Date: Tue, 3 May 2022 13:40:21 -0400 Message-Id: <20220503174021.872446-1-ppalka@redhat.com> X-Mailer: git-send-email 2.36.0.44.g0f828332d5 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, 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-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, since finish_non_static_data_member isn't SFINAE friendly, we incorrectly emit an error during overload resolution: sfinae33.C: In substitution of ‘template A f() [with T = B]’: sfinae33.C:11:7: required from here sfinae33.C:5:31: error: invalid use of non-static data member ‘B::value’ 5 | template A f(); | ^ This patch fixes this by making the function SFINAE friendly in the usual way: give it a complain parameter, check it before emitting an error, and pass it through appropriately. Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for trunk? PR c++/105351 gcc/cp/ChangeLog: * cp-tree.h (finish_non_static_data_member): Add defaulted complain parameter. * pt.cc (tsubst_copy_and_build): Pass complain to finish_non_static_data_member. * semantics.cc (finish_non_static_data_member): Respect complain parameter. (finish_qualified_id_expr): Pass complain to finish_non_static_data_member. gcc/testsuite/ChangeLog: * g++.dg/template/sfinae33.C: New test. --- gcc/cp/cp-tree.h | 3 ++- gcc/cp/pt.cc | 3 ++- gcc/cp/semantics.cc | 27 ++++++++++++++---------- gcc/testsuite/g++.dg/template/sfinae33.C | 12 +++++++++++ 4 files changed, 32 insertions(+), 13 deletions(-) create mode 100644 gcc/testsuite/g++.dg/template/sfinae33.C diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h index 72177999968..b29e60ee52f 100644 --- a/gcc/cp/cp-tree.h +++ b/gcc/cp/cp-tree.h @@ -7615,7 +7615,8 @@ inline tree force_paren_expr_uneval (tree t) { return force_paren_expr (t, true); } extern tree maybe_undo_parenthesized_ref (tree); extern tree maybe_strip_ref_conversion (tree); -extern tree finish_non_static_data_member (tree, tree, tree); +extern tree finish_non_static_data_member (tree, tree, tree, + tsubst_flags_t = tf_warning_or_error); extern tree begin_stmt_expr (void); extern tree finish_stmt_expr_expr (tree, tree); extern tree finish_stmt_expr (tree, bool); diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc index 81c3c598c71..f96f8e31885 100644 --- a/gcc/cp/pt.cc +++ b/gcc/cp/pt.cc @@ -21059,7 +21059,8 @@ tsubst_copy_and_build (tree t, } else if (TREE_CODE (member) == FIELD_DECL) { - r = finish_non_static_data_member (member, object, NULL_TREE); + r = finish_non_static_data_member (member, object, NULL_TREE, + complain); if (TREE_CODE (r) == COMPONENT_REF) REF_PARENTHESIZED_P (r) = REF_PARENTHESIZED_P (t); RETURN (r); diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc index 377f61113c0..bfb737f665b 100644 --- a/gcc/cp/semantics.cc +++ b/gcc/cp/semantics.cc @@ -2141,7 +2141,8 @@ finish_parenthesized_expr (cp_expr expr) preceded by `.' or `->'. */ tree -finish_non_static_data_member (tree decl, tree object, tree qualifying_scope) +finish_non_static_data_member (tree decl, tree object, tree qualifying_scope, + tsubst_flags_t complain /* = tf_warning_or_error */) { gcc_assert (TREE_CODE (decl) == FIELD_DECL); bool try_omp_private = !object && omp_private_member_map; @@ -2172,12 +2173,15 @@ finish_non_static_data_member (tree decl, tree object, tree qualifying_scope) if (is_dummy_object (object) && cp_unevaluated_operand == 0 && (!processing_template_decl || !current_class_ref)) { - if (current_function_decl - && DECL_STATIC_FUNCTION_P (current_function_decl)) - error ("invalid use of member %qD in static member function", decl); - else - error ("invalid use of non-static data member %qD", decl); - inform (DECL_SOURCE_LOCATION (decl), "declared here"); + if (complain & tf_error) + { + if (current_function_decl + && DECL_STATIC_FUNCTION_P (current_function_decl)) + error ("invalid use of member %qD in static member function", decl); + else + error ("invalid use of non-static data member %qD", decl); + inform (DECL_SOURCE_LOCATION (decl), "declared here"); + } return error_mark_node; } @@ -2219,8 +2223,9 @@ finish_non_static_data_member (tree decl, tree object, tree qualifying_scope) { tree access_type = TREE_TYPE (object); - perform_or_defer_access_check (TYPE_BINFO (access_type), decl, - decl, tf_warning_or_error); + if (!perform_or_defer_access_check (TYPE_BINFO (access_type), decl, + decl, complain)) + return error_mark_node; /* If the data member was named `C::M', convert `*this' to `C' first. */ @@ -2234,7 +2239,7 @@ finish_non_static_data_member (tree decl, tree object, tree qualifying_scope) ret = build_class_member_access_expr (object, decl, /*access_path=*/NULL_TREE, /*preserve_reference=*/false, - tf_warning_or_error); + complain); } if (try_omp_private) { @@ -2396,7 +2401,7 @@ finish_qualified_id_expr (tree qualifying_class, { push_deferring_access_checks (dk_no_check); expr = finish_non_static_data_member (expr, NULL_TREE, - qualifying_class); + qualifying_class, complain); pop_deferring_access_checks (); } else if (BASELINK_P (expr)) diff --git a/gcc/testsuite/g++.dg/template/sfinae33.C b/gcc/testsuite/g++.dg/template/sfinae33.C new file mode 100644 index 00000000000..cb9d4982211 --- /dev/null +++ b/gcc/testsuite/g++.dg/template/sfinae33.C @@ -0,0 +1,12 @@ +// PR c++/105351 + +template struct A; + +template A f(); +template void f(); + +struct B { int value; }; + +int main() { + f(); +}