From patchwork Fri Jan 28 20:49:43 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Polacek X-Patchwork-Id: 50553 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 87A463857027 for ; Fri, 28 Jan 2022 20:50:44 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 87A463857027 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1643403044; bh=ef4BoldUX1fLw7LmLWjwgIHQF+wR7K2s8bh32+l8Aa8=; h=Date:To:Subject:References:In-Reply-To:List-Id:List-Unsubscribe: List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:Cc: From; b=a54RA1b4bzQBKQrEN4+r1iQhNYCcuTlNbMO+StcdO+xNKnmq1MepBHz+fJO5BvoNO U1ljdOnI6tCtbhI3TWNvHRjsNh3+PE3tEY0WsgA4wBBGc6VzIVPEGF37nBzRXEbS43 gWBwPbSlCMSBmURp805B2RyWoTGdZMllPjZIhfGM= 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 8A0AA386EC78 for ; Fri, 28 Jan 2022 20:49:52 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 8A0AA386EC78 Received: from mail-qk1-f198.google.com (mail-qk1-f198.google.com [209.85.222.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-319-wksY2dlmNXe1NVke2dTe0A-1; Fri, 28 Jan 2022 15:49:46 -0500 X-MC-Unique: wksY2dlmNXe1NVke2dTe0A-1 Received: by mail-qk1-f198.google.com with SMTP id g3-20020a05620a108300b0047e16fc0d6cso5758895qkk.3 for ; Fri, 28 Jan 2022 12:49:46 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=ef4BoldUX1fLw7LmLWjwgIHQF+wR7K2s8bh32+l8Aa8=; b=i9Xi9wWjYB199OyNU1S1iXZ51OkM728tOmjH5FkML36PErpSsHReRbABpPBOnR4h2s iOIu+5Vo4p3V+nbi26LmuZLAIhIGQGjRmTLCNcl+u4aF8NoD5A62Cfqmpxqt8FAT6cam ubt/uPdqaBiLBhjkdkhllvZs0cI2F20UB25nf+XZEWjCPvB6E391NwfsQOjtLGM7+Sad mcVDvvDSjm0WH6iRxCENUspCqzOJH5TKTSUZq9AUCY6SIxGAlV2zFM2uo9qGpWanKC9/ ewpQpKoRJhVWdwE5h9p1Eeb4l8x6y4pbBZ6WSHJKpKrBZbrziaEHPuSP89Wi+omAxyb/ eRgA== X-Gm-Message-State: AOAM531gKIq7AE6gBRraCEVJV7pqupAWHbiw5VAYZugdTZbypijn+k8P vFCESTbBywoBibW7kzX2UMeWuc4Tz5n1KamvSx/6sbg7IiaRTtIFuRYNlBI3Ex8ncfDKGzd1G4i /JGnEri7tdmTXKmuUYg== X-Received: by 2002:a05:622a:34d:: with SMTP id r13mr7448414qtw.599.1643402985655; Fri, 28 Jan 2022 12:49:45 -0800 (PST) X-Google-Smtp-Source: ABdhPJzfY4PbPdARaqo6iEVqflmGpNfbLfkM4IPXNDftvAoXqvWH50eQUnr0lgr20nxZ3Zan+qEMNw== X-Received: by 2002:a05:622a:34d:: with SMTP id r13mr7448402qtw.599.1643402985356; Fri, 28 Jan 2022 12:49:45 -0800 (PST) Received: from redhat.com ([2601:184:4780:4310::7156]) by smtp.gmail.com with ESMTPSA id f22sm3676011qtb.1.2022.01.28.12.49.44 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 28 Jan 2022 12:49:44 -0800 (PST) Date: Fri, 28 Jan 2022 15:49:43 -0500 To: Jason Merrill Subject: [PATCH v2] c++: ICE with auto[] and VLA [PR102414] Message-ID: References: <20220128010209.586323-1-polacek@redhat.com> <211ffc65-cf61-a2dd-2066-abc2d1307941@redhat.com> MIME-Version: 1.0 In-Reply-To: <211ffc65-cf61-a2dd-2066-abc2d1307941@redhat.com> User-Agent: Mutt/2.1.5 (2021-12-30) X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Disposition: inline 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, RCVD_IN_MSPIKE_H2, 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: Marek Polacek via Gcc-patches From: Marek Polacek Reply-To: Marek Polacek Cc: GCC Patches Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" On Thu, Jan 27, 2022 at 10:17:00PM -0500, Jason Merrill wrote: > On 1/27/22 20:02, Marek Polacek wrote: > > @@ -11159,8 +11159,16 @@ create_array_type_for_decl (tree name, tree type, tree size, location_t loc) > > /* Figure out the index type for the array. */ > > if (size) > > - itype = compute_array_index_type_loc (loc, name, size, > > - tf_warning_or_error); > > + { > > + itype = compute_array_index_type_loc (loc, name, size, > > + tf_warning_or_error); > > + if (type_uses_auto (type) > > + && !TREE_CONSTANT (maybe_constant_value (size))) > > Maybe variably_modified_type_p (itype)? Ah yes, I missed that v_m_t_p actually looks at TYPE_MAX_VALUE... Fixed. > > + { > > + sorry_at (loc, "variable-length array of %"); > > + return error_mark_node; > > + } > > + } > > return build_cplus_array_type (type, itype); > > } > > diff --git a/gcc/testsuite/g++.dg/cpp23/auto-array3.C b/gcc/testsuite/g++.dg/cpp23/auto-array3.C > > new file mode 100644 > > index 00000000000..e383a17d0ee > > --- /dev/null > > +++ b/gcc/testsuite/g++.dg/cpp23/auto-array3.C > > @@ -0,0 +1,17 @@ > > +// PR c++/102414 > > +// { dg-do compile { target c++11 } } > > +// { dg-options "" } > > + > > +constexpr int sz () { return 3; } > > + > > +void f () > > +{ > > + int a[3]; > > + auto (*a1)[0/0] = &a; // { dg-message "variable-length array of .auto." } > > +// { dg-warning "division by zero" "" { target *-*-* } .-1 } > > Let's move the error into the other testcase? Done. Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? -- >8 -- Here we ICE in unify_array_domain when we're trying to deduce the type of an array, as in auto(*p)[i] = (int(*)[i])0; but unify_array_domain doesn't arbitrarily complex bounds. Another test is, e.g., auto (*b)[0/0] = &a; where the type of the array is <<< Unknown tree: template_type_parm >>>[0:(sizetype) ((ssizetype) (0 / 0) - 1)] It seems to me that we need not handle these. PR c++/102414 PR c++/101874 gcc/cp/ChangeLog: * decl.cc (create_array_type_for_decl): Use template_placeholder_p. Sorry on a variable-length array of auto. gcc/testsuite/ChangeLog: * g++.dg/cpp23/auto-array3.C: New test. * g++.dg/cpp23/auto-array4.C: New test. --- gcc/cp/decl.cc | 14 +++++++++++--- gcc/testsuite/g++.dg/cpp23/auto-array3.C | 16 ++++++++++++++++ gcc/testsuite/g++.dg/cpp23/auto-array4.C | 14 ++++++++++++++ 3 files changed, 41 insertions(+), 3 deletions(-) create mode 100644 gcc/testsuite/g++.dg/cpp23/auto-array3.C create mode 100644 gcc/testsuite/g++.dg/cpp23/auto-array4.C base-commit: 3a5fdf986dc6ebb6e244087b462132590ad0a184 diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc index 26ce9bfefec..09eed9ceba6 100644 --- a/gcc/cp/decl.cc +++ b/gcc/cp/decl.cc @@ -11099,7 +11099,7 @@ create_array_type_for_decl (tree name, tree type, tree size, location_t loc) /* [dcl.type.class.deduct] prohibits forming an array of placeholder for a deduced class type. */ - if (is_auto (type) && CLASS_PLACEHOLDER_TEMPLATE (type)) + if (template_placeholder_p (type)) { if (name) error_at (loc, "%qD declared as array of template placeholder " @@ -11169,8 +11169,16 @@ create_array_type_for_decl (tree name, tree type, tree size, location_t loc) /* Figure out the index type for the array. */ if (size) - itype = compute_array_index_type_loc (loc, name, size, - tf_warning_or_error); + { + itype = compute_array_index_type_loc (loc, name, size, + tf_warning_or_error); + if (type_uses_auto (type) + && variably_modified_type_p (itype, /*fn=*/NULL_TREE)) + { + sorry_at (loc, "variable-length array of %"); + return error_mark_node; + } + } return build_cplus_array_type (type, itype); } diff --git a/gcc/testsuite/g++.dg/cpp23/auto-array3.C b/gcc/testsuite/g++.dg/cpp23/auto-array3.C new file mode 100644 index 00000000000..fdf25470fc2 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp23/auto-array3.C @@ -0,0 +1,16 @@ +// PR c++/102414 +// PR c++/101874 +// { dg-do compile { target c++11 } } +// { dg-options "" } + +constexpr int sz () { return 3; } + +void f () +{ + int a[3]; + const int N = 3; + auto (*a2)[N] = &a; + constexpr int M = 3; + auto (*a3)[M] = &a; + auto (*a4)[sz()] = &a; +} diff --git a/gcc/testsuite/g++.dg/cpp23/auto-array4.C b/gcc/testsuite/g++.dg/cpp23/auto-array4.C new file mode 100644 index 00000000000..4385c4a6bf9 --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp23/auto-array4.C @@ -0,0 +1,14 @@ +// PR c++/102414 +// PR c++/101874 +// { dg-do compile { target c++11 } } +// { dg-options "" } + +void +f (int i) +{ + auto x[i] = { 0 }; // { dg-message "variable-length array of .auto." } + auto(*p)[i] = (int(*)[i])0; // { dg-message "variable-length array of .auto." } + int a[3]; + auto (*a1)[0/0] = &a; // { dg-message "variable-length array of .auto." } +// { dg-warning "division by zero" "" { target *-*-* } .-1 } +}