From patchwork Wed Feb 12 18:54:55 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 106401 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 7BED43858410 for ; Wed, 12 Feb 2025 18:55:58 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7BED43858410 Authentication-Results: sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=GlhvyBR0 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 ESMTP id 4316C3858406 for ; Wed, 12 Feb 2025 18:55:09 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4316C3858406 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 4316C3858406 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=1739386509; cv=none; b=roRdaQxh6tYqPMveaG9SkGaJpwWGDctJSg5UPx90IgbiwAJoBKcwBCDPigW1LZCGMi9z6F0MsSUZ8EKKGE2FGHtofewKY1jJU6kPN0sPoypjumW6CP71GRIGanBa0d9glCbPsIdYKEzBZoA5iQy8zvBFWc/453ISLsMRehC1ZLA= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1739386509; c=relaxed/simple; bh=vw/XgP63yshHKk6ynIKyM2S4jpySlStECelaAMYt+KY=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=YwssvszX5e5QOjy65t1To7takCA+1AXYqxJe1bo8ahj/FigF5PgBNCKZ2GFSXvvsrTFGIvkPkXaAQgs5VBVLPzk+bi42Uk2yAyscD5poS5dDCYEUJFc3DbBEcftvV+xl59dQE7PhSSzEoKDqtpW5b3bHn+1DeFW6H78ueR7uwEk= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4316C3858406 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1739386508; 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=DeIQ/YPaJsmjdFzW+0o082VPy9IHTyo9vD3/qtLyURE=; b=GlhvyBR0DP512NYoFtlXWfPYc7f3bJhAR342SSwlHzyq1tO1IRhOx/UzZ0/pgrlSJ3DEgI 239e9iiXDf10byY43jHPsnuRoQV1Mq+RY9qdLZywsl9WYH7ncIgNUF91Q/RsJfU/sl9AzN HAjNDVjhACWWPA8+tBP/sS2EENaj5Ro= Received: from mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-186-vLuqYnPFOdyXJvB5SgFeNw-1; Wed, 12 Feb 2025 13:55:06 -0500 X-MC-Unique: vLuqYnPFOdyXJvB5SgFeNw-1 X-Mimecast-MFC-AGG-ID: vLuqYnPFOdyXJvB5SgFeNw_1739386506 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-02.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 853C719560A0 for ; Wed, 12 Feb 2025 18:55:05 +0000 (UTC) Received: from pdp-11.lan (unknown [10.22.80.228]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 8B82530001AB; Wed, 12 Feb 2025 18:55:04 +0000 (UTC) From: Marek Polacek To: Jason Merrill , GCC Patches Subject: [PATCH] c++: P2308, Template parameter initialization (tests) [PR113800] Date: Wed, 12 Feb 2025 13:54:55 -0500 Message-ID: <20250212185455.488233-1-polacek@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: lQ1-KHuuH2bAcS-k9rY7HVaPQ1Q5EQOFwICHM6zdLDs_1739386506 X-Mimecast-Originator: redhat.com content-type: text/plain; charset="US-ASCII"; x-default=true 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_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.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces~patchwork=sourceware.org@gcc.gnu.org Tested on x86_64-pc-linux-gnu, ok for trunk? I'll also update cxx-status.html. -- >8 -- This proposal was implemented a long time ago by my r9-5271, but it took me this long to verify that it still works as per P2308. This patch adds assorted tests, both from clang and from [temp.arg.nontype]. Fortunately I did not discover any issues in the compiler. PR c++/113800 DR 2450 gcc/testsuite/ChangeLog: * g++.dg/cpp26/pack-indexing15.C: New test. * g++.dg/cpp2a/nontype-class68.C: New test. * g++.dg/cpp2a/nontype-class69.C: New test. * g++.dg/cpp2a/nontype-class70.C: New test. * g++.dg/cpp2a/nontype-class71.C: New test. * g++.dg/cpp2a/nontype-class72.C: New test. --- gcc/testsuite/g++.dg/cpp26/pack-indexing15.C | 20 +++++++++ gcc/testsuite/g++.dg/cpp2a/nontype-class68.C | 24 ++++++++++ gcc/testsuite/g++.dg/cpp2a/nontype-class69.C | 27 +++++++++++ gcc/testsuite/g++.dg/cpp2a/nontype-class70.C | 47 ++++++++++++++++++++ gcc/testsuite/g++.dg/cpp2a/nontype-class71.C | 19 ++++++++ gcc/testsuite/g++.dg/cpp2a/nontype-class72.C | 41 +++++++++++++++++ 6 files changed, 178 insertions(+) create mode 100644 gcc/testsuite/g++.dg/cpp26/pack-indexing15.C create mode 100644 gcc/testsuite/g++.dg/cpp2a/nontype-class68.C create mode 100644 gcc/testsuite/g++.dg/cpp2a/nontype-class69.C create mode 100644 gcc/testsuite/g++.dg/cpp2a/nontype-class70.C create mode 100644 gcc/testsuite/g++.dg/cpp2a/nontype-class71.C create mode 100644 gcc/testsuite/g++.dg/cpp2a/nontype-class72.C base-commit: cfdb961588ba318a78e995d2e2cde43130acd993 diff --git a/gcc/testsuite/g++.dg/cpp26/pack-indexing15.C b/gcc/testsuite/g++.dg/cpp26/pack-indexing15.C new file mode 100644 index 00000000000..3f8382b12cd --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp26/pack-indexing15.C @@ -0,0 +1,20 @@ +// PR c++/113800 +// { dg-do compile { target c++26 } } +// From LLVM's temp_arg_nontype_cxx2c.cpp. + +template +concept C = sizeof(T...[1]) == 1; + +struct A {}; + +template auto = A{}> struct Set {}; + +template +void +foo () +{ + Set u; +} + +Set sb; +Set sf; // { dg-error "placeholder constraints not satisfied" } diff --git a/gcc/testsuite/g++.dg/cpp2a/nontype-class68.C b/gcc/testsuite/g++.dg/cpp2a/nontype-class68.C new file mode 100644 index 00000000000..ade646e391b --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/nontype-class68.C @@ -0,0 +1,24 @@ +// PR c++/113800 +// { dg-do compile { target c++20 } } +// From [temp.arg.nontype]. + +template struct B { /* ... */ }; +B<5> b1; // OK, template parameter type is int +B<'a'> b2; // OK, template parameter type is char +B<2.5> b3; // OK, template parameter type is double +B b4; // { dg-error ".void. is not a valid type for a template non-type parameter" } + +template struct C { /* ... */ }; +C<{ 42 }> c1; // OK + +struct J1 { + J1 *self = this; +}; +B j1; // { dg-error "not a constant expression" } + +struct J2 { + J2 *self = this; + constexpr J2() {} + constexpr J2(const J2&) {} +}; +B j2; // { dg-error "not a constant expression" } diff --git a/gcc/testsuite/g++.dg/cpp2a/nontype-class69.C b/gcc/testsuite/g++.dg/cpp2a/nontype-class69.C new file mode 100644 index 00000000000..08b0a5ef73c --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/nontype-class69.C @@ -0,0 +1,27 @@ +// PR c++/113800 +// { dg-do compile { target c++20 } } + +// DR 2450 +struct S { int a; }; + +template +void +f () +{ +} + +void +test () +{ + f<{0}>(); + f<{.a= 0}>(); +} + +// DR 2459 +struct A { + constexpr A (float) {} +}; + +template +struct X {}; +X<1> x; diff --git a/gcc/testsuite/g++.dg/cpp2a/nontype-class70.C b/gcc/testsuite/g++.dg/cpp2a/nontype-class70.C new file mode 100644 index 00000000000..0e50847e440 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/nontype-class70.C @@ -0,0 +1,47 @@ +// PR c++/113800 +// P2308R1 - Template parameter initialization +// { dg-do compile { target c++20 } } + +struct S { + int a = 0; + int b = 42; +}; + +template +struct A { + static constexpr auto a = t.a; + static constexpr auto b = t.b; +}; + +static_assert(A<{}>::a == 0); +static_assert(A<{}>::b == 42); +static_assert(A<{.a = 3}>::a == 3); +static_assert(A<{.b = 4}>::b == 4); + +template +struct D1 {}; + +template +struct D2 {}; + +template +struct D3 {}; + +struct E {}; + +struct I { + constexpr I(E) {}; +}; + +template +struct W {}; + +void +g () +{ + D1<> d1; + D2<> d2; + D3<> d3; + + W w; +} diff --git a/gcc/testsuite/g++.dg/cpp2a/nontype-class71.C b/gcc/testsuite/g++.dg/cpp2a/nontype-class71.C new file mode 100644 index 00000000000..36ce5b16dee --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/nontype-class71.C @@ -0,0 +1,19 @@ +// PR c++/113800 +// { dg-do compile { target c++20 } } +// From LLVM's temp_arg_nontype_cxx2c.cpp. + +template +struct A { + T x[I]; +}; + +template +A(T, U...) -> A; + +template void foo() { } + +void +bar () +{ + foo<{1}>(); +} diff --git a/gcc/testsuite/g++.dg/cpp2a/nontype-class72.C b/gcc/testsuite/g++.dg/cpp2a/nontype-class72.C new file mode 100644 index 00000000000..1c48ff57add --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp2a/nontype-class72.C @@ -0,0 +1,41 @@ +// PR c++/113800 +// P2308R1 - Template parameter initialization +// { dg-do compile { target c++20 } } +// Invalid cases. + +namespace std { +template class initializer_list { + const T *_M_array; + decltype (sizeof 0) _M_len; +}; +} + +template +struct X {}; + +struct A { + int i; +}; + +template +struct B { }; + +struct E {}; + +struct I { // { dg-message "not literal" } + I(E) {}; +}; + +template +struct W {}; + +void +g () +{ + X<{0}> x; // { dg-error "unable to deduce" } + + int i = 42; // { dg-message "not const" } + B<{i}> b; // { dg-error "not usable" } + + W w; // { dg-error "not a valid type for a template non-type parameter" } +}