From patchwork Thu Feb 3 04:20:55 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jason Merrill X-Patchwork-Id: 1587881 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.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=Fdv33jzt; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (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 bilbo.ozlabs.org (Postfix) with ESMTPS id 4Jq592302gz9s8q for ; Thu, 3 Feb 2022 15:21:57 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id D3E27385BF84 for ; Thu, 3 Feb 2022 04:21:54 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org D3E27385BF84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1643862114; bh=4hrTTav+A4lBfKGJSuzkLM+2Ag5oMNVHn7i1LJ8YdLg=; h=To:Subject:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=Fdv33jztQ9ED76d1Ke9S2Rc2hPPUhj0K+v/Kk3yITFc6m85pQr5RmTlj5IZcXfzcc TkZ4NC8QYVJs2JabFfBQybA6fU6gJsjwBvvGx9+QQM+aQ6ZUq+xsIF49v80RCx/ESV PWos2YDAjNrkIjnuAR2OTyDsuo2Qv2c2z7MGKspY= 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.129.124]) by sourceware.org (Postfix) with ESMTPS id 041173858D37 for ; Thu, 3 Feb 2022 04:21:06 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 041173858D37 Received: from mail-qt1-f200.google.com (mail-qt1-f200.google.com [209.85.160.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-117-Y2YA6QbAO0qQ_zEXFIlduw-1; Wed, 02 Feb 2022 23:21:00 -0500 X-MC-Unique: Y2YA6QbAO0qQ_zEXFIlduw-1 Received: by mail-qt1-f200.google.com with SMTP id s1-20020ac85ec1000000b002cf7c93d56dso981248qtx.21 for ; Wed, 02 Feb 2022 20:21:00 -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=4hrTTav+A4lBfKGJSuzkLM+2Ag5oMNVHn7i1LJ8YdLg=; b=OMGbchL9avGs+wrErd661Q8ofjfndP8VdLMbeEykIp1oAEPBQnKfheJwZ2/MK5sdph SGvSjUOqcs8jDFx4gOoHivUqTcIj1e8DrStHBfwowGdnWC8otzC6ZtF2DNTnIHWqdWQ1 30biJEvtjJvuT1nAhZEBRKRR1BRzNZA0wN+Mm3VJLUBnmeKKAnod+dLA5MTlW9x6X3TV pMOtLRpkmF8UBA/bYPmT5NU9x8rUNzv001TbxWiJ8wcnrt3ByCPigp/Qh2k34IOBTY6f y+TYNOcmVvGvXhregMd02ClXA4QsIPgQFFi+hdaT3q1SoHv0W7e5CVMF83OxBYcnWvTs bD2w== X-Gm-Message-State: AOAM531ttpwGgnxUCPmJl5Garv2vm9iOsWFxyBOvRbw0+PiKXRCw+QWx WHmUg28A33iFjeFnIrN5ssxBslVo8kgCsG4j0JMCvo6MW5eRxlTCwlp9KnScd8DnzhZUcNIHdET ZIu/1aOa4PtQm6ax0uHwIqinauecL0QbENhfpcKWGxyI/Vjq7LgmwmkrbWOamly/ZRQ== X-Received: by 2002:a05:622a:199c:: with SMTP id u28mr24688591qtc.221.1643862059392; Wed, 02 Feb 2022 20:20:59 -0800 (PST) X-Google-Smtp-Source: ABdhPJxio78LF0/sHwFvLUx197JCZj7GMgWNLZFYX1Hl3VMumF8NtV0jo+6dGTWOlKom3O/gVhMEmg== X-Received: by 2002:a05:622a:199c:: with SMTP id u28mr24688583qtc.221.1643862058989; Wed, 02 Feb 2022 20:20:58 -0800 (PST) Received: from barrymore.redhat.com (130-44-159-43.s15913.c3-0.arl-cbr1.sbo-arl.ma.cable.rcncustomer.com. [130.44.159.43]) by smtp.gmail.com with ESMTPSA id o3sm5859662qtw.3.2022.02.02.20.20.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 02 Feb 2022 20:20:57 -0800 (PST) To: gcc-patches@gcc.gnu.org Subject: [PATCH] c++: dependent array bounds completion [PR104302] Date: Wed, 2 Feb 2022 23:20:55 -0500 Message-Id: <20220203042055.488698-1-jason@redhat.com> X-Mailer: git-send-email 2.27.0 MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-13.1 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: Jason Merrill via Gcc-patches From: Jason Merrill Reply-To: Jason Merrill Cc: Will Wray Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" The patch for PR55227 changed the minimal init-list handling in cp_complete_array_type to a call to reshape_init, which broke on the dependent initializer. It occurred to me that trying to deduce the array size from a dependent init-list is wrong in general, as we can see with the second testcase, so let's just not. I also limited the reshape_init call to the case of a char array, as before the patch for 55227; that's the only case where we want to strip a level of braces from an array. Tested x86_64-pc-linux-gnu, applying to trunk. PR c++/104302 gcc/cp/ChangeLog: * decl.cc (maybe_deduce_size_from_array_init): Give up on type-dependent init. (cp_complete_array_type): Only call reshape_init for character array. gcc/testsuite/ChangeLog: * g++.dg/template/array35.C: New test. * g++.dg/template/array36.C: New test. --- gcc/cp/decl.cc | 10 ++++++++-- gcc/testsuite/g++.dg/template/array35.C | 11 +++++++++++ gcc/testsuite/g++.dg/template/array36.C | 15 +++++++++++++++ 3 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 gcc/testsuite/g++.dg/template/array35.C create mode 100644 gcc/testsuite/g++.dg/template/array36.C base-commit: dc898b2ba5c50a7311bc3137f0987a7831362ed8 diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc index 09eed9ceba6..7b48b56231b 100644 --- a/gcc/cp/decl.cc +++ b/gcc/cp/decl.cc @@ -6017,6 +6017,11 @@ maybe_deduce_size_from_array_init (tree decl, tree init) return; if (!check_array_designated_initializer (ce, i)) failure = 1; + /* If an un-designated initializer is type-dependent, we can't + check brace elision yet. */ + if (ce->index == NULL_TREE + && type_dependent_expression_p (ce->value)) + return; } } @@ -8113,7 +8118,7 @@ cp_finish_decl (tree decl, tree init, bool init_const_expr_p, else { gcc_assert (!DECL_PRETTY_FUNCTION_P (decl)); - /* Deduce array size even if the initializer is dependent. */ + /* Try to deduce array size. */ maybe_deduce_size_from_array_init (decl, init); /* And complain about multiple initializers. */ if (init && TREE_CODE (init) == TREE_LIST && TREE_CHAIN (init) @@ -9570,7 +9575,8 @@ cp_complete_array_type (tree *ptype, tree initial_value, bool do_default) /* An array of character type can be initialized from a brace-enclosed string constant so call reshape_init to remove the optional braces from a braced string literal. */ - if (BRACE_ENCLOSED_INITIALIZER_P (initial_value)) + if (char_type_p (TYPE_MAIN_VARIANT (TREE_TYPE (*ptype))) + && BRACE_ENCLOSED_INITIALIZER_P (initial_value)) initial_value = reshape_init (*ptype, initial_value, tf_warning_or_error); diff --git a/gcc/testsuite/g++.dg/template/array35.C b/gcc/testsuite/g++.dg/template/array35.C new file mode 100644 index 00000000000..9fd02633523 --- /dev/null +++ b/gcc/testsuite/g++.dg/template/array35.C @@ -0,0 +1,11 @@ +// PR c++/104302 + +struct ss {}; +static ss ff(void* const v); +template +void f1(void) { + int mem[mem_size]; + ss StateRegs[] = { + ff(mem) + }; +} diff --git a/gcc/testsuite/g++.dg/template/array36.C b/gcc/testsuite/g++.dg/template/array36.C new file mode 100644 index 00000000000..1511da7020d --- /dev/null +++ b/gcc/testsuite/g++.dg/template/array36.C @@ -0,0 +1,15 @@ +// Don't try to deduce array bounds from a dependent initializer. +// { dg-do compile { target c++11 } } + +struct A { int i,j; }; + +template void f(T t) +{ + A ar[] = { t, t }; + static_assert (sizeof(ar)/sizeof(A) == 1, ""); +} + +int main() +{ + f(42); +}