From patchwork Thu Jun 18 20:12:25 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 1312370 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: 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@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=gcc.gnu.org Authentication-Results: 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=VzS2kIqH; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 49ntQY31wHz9sR4 for ; Fri, 19 Jun 2020 06:12:35 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 25BDB386F430; Thu, 18 Jun 2020 20:12:33 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 25BDB386F430 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1592511153; bh=t73P/OU1WpMa4MH7zMLpRWFQ3aQSvA6544gQzSanBpA=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=VzS2kIqHzLbzy02figC6SjudMlEO9+GmHj0FSkd0byxh0iNcJTyChkMeWO2zpAHvy wqSq0Vhlgx4nyPLyaQVMSGmZUUYQiOD3ld6HfmqlfTXK6T3lfRLsI/OWCpPlS/4pXZ 0hQJyDoYDpYoRUC6bG1ufWG/kK5G5hQBzno8O0G0= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from us-smtp-delivery-1.mimecast.com (us-smtp-delivery-1.mimecast.com [205.139.110.120]) by sourceware.org (Postfix) with ESMTP id 8ABE23858D34 for ; Thu, 18 Jun 2020 20:12:30 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 8ABE23858D34 Received: from mail-qv1-f71.google.com (mail-qv1-f71.google.com [209.85.219.71]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-132-l2idqPgKPT2_kT3V2kbUNA-1; Thu, 18 Jun 2020 16:12:28 -0400 X-MC-Unique: l2idqPgKPT2_kT3V2kbUNA-1 Received: by mail-qv1-f71.google.com with SMTP id h4so4914337qvr.21 for ; Thu, 18 Jun 2020 13:12:28 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id; bh=t73P/OU1WpMa4MH7zMLpRWFQ3aQSvA6544gQzSanBpA=; b=VbgoHHdEc1Gf4EGQErj1BpOC8+IzrwowbpzP8pxTAXtqLLZzjAbZVc4MA+xd1Ipgcg UcO8YvQOIUfLrPsCk0dGg0IGJhsYwdAMkHDJpRBK8uZNgNAxm7N+9Pe10fk3yPKbBhMD RrA9DlYjIMygeSXno7/MMmWVbHK/t/u8hqa7HglTPAu2phW5iVobihxCsZJNLTeLNn8e ZM67j4cYBvVK2NPiHEJjhF+ukxX108cWZqRh8yalYlNDE8QkSFpRYYBIQhkO4bZswrZW zeISydoj2OiFegfBpQ+KbhpmykS9NTH39P93PnGyxRtuV1xjSbRbEMocn9yp9NNE+OrU y6kQ== X-Gm-Message-State: AOAM530XnR1/2oOdYimHxIajQEInt5nItRuJXP59OxrhRtZtE2wuSUih bwn1FhTq9fEtkukqF79aZw5MmdlmmgfN8ts0W+M51iuU9zKxs0Am8TeZdfxeCL3xWPyW2f/Pus2 BJiAltGAFtOxuy2uOpQ== X-Received: by 2002:a37:e107:: with SMTP id c7mr109722qkm.483.1592511147245; Thu, 18 Jun 2020 13:12:27 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzkTxfPv7q16oRKb/r/vDQOBaf0flXbAHH9rgynibZGlbzqC+B88ucDGknlJepaBL8wdxiEiA== X-Received: by 2002:a37:e107:: with SMTP id c7mr109689qkm.483.1592511146822; Thu, 18 Jun 2020 13:12:26 -0700 (PDT) Received: from barrymore.redhat.com (209-6-216-142.s141.c3-0.smr-cbr1.sbo-smr.ma.cable.rcncustomer.com. [209.6.216.142]) by smtp.gmail.com with ESMTPSA id v14sm4253178qtj.31.2020.06.18.13.12.26 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 18 Jun 2020 13:12:26 -0700 (PDT) To: gcc-patches@gcc.gnu.org Subject: [pushed] c++: More P2002 operator<=> refinements. Date: Thu, 18 Jun 2020 16:12:25 -0400 Message-Id: <20200618201225.13976-1-jason@redhat.com> X-Mailer: git-send-email 2.18.1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-17.4 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_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) 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: Jason Merrill via Gcc-patches From: Jason Merrill Reply-To: Jason Merrill Errors-To: gcc-patches-bounces@gcc.gnu.org Sender: "Gcc-patches" * Disallow && references. * Allow empty unions. * Improve diagnostics for a subobject comparison with non-comparison-category type. Tested x86_64-pc-linux-gnu, applying to trunk. gcc/cp/ChangeLog: * method.c (early_check_defaulted_comparison): Check for &&. (build_comparison_op): Allow empty union. Diagnose non-category type. (common_comparison_type): Remove handling for non-category type. gcc/testsuite/ChangeLog: * g++.dg/cpp2a/spaceship-ref1.C: New test. * g++.dg/cpp2a/spaceship-synth-neg4.C: New test. * g++.dg/cpp2a/spaceship-union1.C: New test. --- gcc/cp/method.c | 49 ++++++++++++++----- gcc/testsuite/g++.dg/cpp2a/spaceship-ref1.C | 12 +++++ .../g++.dg/cpp2a/spaceship-synth-neg4.C | 20 ++++++++ gcc/testsuite/g++.dg/cpp2a/spaceship-union1.C | 12 +++++ 4 files changed, 81 insertions(+), 12 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp2a/spaceship-ref1.C create mode 100644 gcc/testsuite/g++.dg/cpp2a/spaceship-synth-neg4.C create mode 100644 gcc/testsuite/g++.dg/cpp2a/spaceship-union1.C base-commit: e54353a72a0fd780a2f233a057f3f8121b476192 diff --git a/gcc/cp/method.c b/gcc/cp/method.c index 1a819b29173..b23764b3d54 100644 --- a/gcc/cp/method.c +++ b/gcc/cp/method.c @@ -1134,6 +1134,11 @@ early_check_defaulted_comparison (tree fn) error_at (loc, "defaulted %qD must be %", fn); ok = false; } + if (mem && type_memfn_rqual (TREE_TYPE (fn)) == REF_QUAL_RVALUE) + { + error_at (loc, "defaulted %qD must not have %<&&%> ref-qualifier", fn); + ok = false; + } tree parmnode = FUNCTION_FIRST_USER_PARMTYPE (fn); bool saw_byval = false; bool saw_byref = mem; @@ -1144,6 +1149,7 @@ early_check_defaulted_comparison (tree fn) if (same_type_p (parmtype, ctx)) saw_byval = true; else if (TREE_CODE (parmtype) != REFERENCE_TYPE + || TYPE_REF_IS_RVALUE (parmtype) || TYPE_QUALS (TREE_TYPE (parmtype)) != TYPE_QUAL_CONST || !(same_type_ignoring_top_level_qualifiers_p (TREE_TYPE (parmtype), ctx))) @@ -1186,11 +1192,9 @@ common_comparison_type (vec &comps) tree comp = comps[i]; tree ctype = TREE_TYPE (comp); comp_cat_tag tag = cat_tag_for (ctype); - if (tag < cc_last) - seen[tag] = ctype; - else - /* If any Ti is not a comparison category type, U is void. */ - return void_type_node; + /* build_comparison_op already checked this. */ + gcc_checking_assert (tag < cc_last); + seen[tag] = ctype; } /* Otherwise, if at least one T i is std::partial_ordering, U is @@ -1312,8 +1316,9 @@ build_comparison_op (tree fndecl, tsubst_flags_t complain) iloc_sentinel ils (info.loc); /* A defaulted comparison operator function for class C is defined as - deleted if ... C is a union-like class. */ - if (TREE_CODE (ctype) == UNION_TYPE) + deleted if ... C has variant members. */ + if (TREE_CODE (ctype) == UNION_TYPE + && next_initializable_field (TYPE_FIELDS (ctype))) { if (complain & tf_error) inform (info.loc, "cannot default compare union %qT", ctype); @@ -1336,6 +1341,7 @@ build_comparison_op (tree fndecl, tsubst_flags_t complain) if (code == EQ_EXPR || code == SPACESHIP_EXPR) { + bool bad = false; auto_vec comps; /* Compare each of the subobjects. Note that we get bases from @@ -1348,21 +1354,22 @@ build_comparison_op (tree fndecl, tsubst_flags_t complain) /* A defaulted comparison operator function for class C is defined as deleted if any non-static data member of C is of reference type or - C is a union-like class. */ + C has variant members. */ if (TREE_CODE (expr_type) == REFERENCE_TYPE) { if (complain & tf_error) inform (DECL_SOURCE_LOCATION (field), "cannot default compare " "reference member %qD", field); - DECL_DELETED_FN (fndecl) = true; + bad = true; continue; } - else if (ANON_UNION_TYPE_P (expr_type)) + else if (ANON_UNION_TYPE_P (expr_type) + && next_initializable_field (TYPE_FIELDS (expr_type))) { if (complain & tf_error) inform (DECL_SOURCE_LOCATION (field), "cannot default compare " "anonymous union member"); - DECL_DELETED_FN (fndecl) = true; + bad = true; continue; } @@ -1374,7 +1381,19 @@ build_comparison_op (tree fndecl, tsubst_flags_t complain) NULL_TREE, NULL, complain); if (comp == error_mark_node) { - DECL_DELETED_FN (fndecl) = true; + bad = true; + continue; + } + if (code == SPACESHIP_EXPR + && cat_tag_for (TREE_TYPE (comp)) == cc_last) + { + /* The operator function is defined as deleted if ... Ri is not a + comparison category type. */ + if (complain & tf_error) + inform (DECL_SOURCE_LOCATION (field), + "three-way comparison of %qD has type %qT, not a " + "comparison category type", field, TREE_TYPE (comp)); + bad = true; continue; } comps.safe_push (comp); @@ -1384,6 +1403,11 @@ build_comparison_op (tree fndecl, tsubst_flags_t complain) rettype = common_comparison_type (comps); apply_deduced_return_type (fndecl, rettype); } + if (bad) + { + DECL_DELETED_FN (fndecl) = true; + goto out; + } for (unsigned i = 0; i < comps.length(); ++i) { tree comp = comps[i]; @@ -1468,6 +1492,7 @@ build_comparison_op (tree fndecl, tsubst_flags_t complain) finish_return_stmt (comp2); } + out: if (info.defining) finish_compound_stmt (compound_stmt); else diff --git a/gcc/testsuite/g++.dg/cpp2a/spaceship-ref1.C b/gcc/testsuite/g++.dg/cpp2a/spaceship-ref1.C new file mode 100644 index 00000000000..5f888dfc3b4 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/spaceship-ref1.C @@ -0,0 +1,12 @@ +// Reject &&. +// { dg-do compile { target c++20 } } + +struct A +{ + bool operator==(const A&) const && = default; // { dg-error "ref-qualifier" } +}; + +struct B +{ + friend bool operator==(const B&&, const B&&) = default; // { dg-error "" } +}; diff --git a/gcc/testsuite/g++.dg/cpp2a/spaceship-synth-neg4.C b/gcc/testsuite/g++.dg/cpp2a/spaceship-synth-neg4.C new file mode 100644 index 00000000000..e99aa0a2041 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/spaceship-synth-neg4.C @@ -0,0 +1,20 @@ +// P2002: deleted if a subobject <=> has non-category type. +// { dg-do compile { target c++20 } } + +#include + +struct A +{ + bool operator<=>(const A&) const; +}; + +struct B +{ + A a; // { dg-message "bool" } + auto operator<=>(const B&) const = default; +}; + +int main() +{ + auto x = B() <=> B(); // { dg-error "deleted" } +} diff --git a/gcc/testsuite/g++.dg/cpp2a/spaceship-union1.C b/gcc/testsuite/g++.dg/cpp2a/spaceship-union1.C new file mode 100644 index 00000000000..a08ba0de69c --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/spaceship-union1.C @@ -0,0 +1,12 @@ +// P2002: Allow default comparison of unions with no members. +// { dg-do compile { target c++20 } } + +union A +{ + bool operator==(const A&) const = default; +}; + +int main() +{ + A() == A(); +}