From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: 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 071603858CDA for ; Tue, 24 Oct 2023 17:03:34 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 071603858CDA 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 071603858CDA Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698167015; cv=none; b=ks0LPpPoIRTNJqeQRBcxJ/omOmA5drg1Bhn/jDV3jDJvzD0/4bZ+jHJjXL3yRgwyIFNdLgk8MDGSlrrmig5Cb1GkBWU7MwNwIq1bB1KTieK0guZSmBImQO9O60dOKs6nTqvtT8Ua48aakYpAJTSgJw7ehDvH67N0BKVLJCnIMIQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698167015; c=relaxed/simple; bh=qcBlgqUX/I8iR9TayGB9YdRHUakj1QJR7vCbOv5Txeo=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=PnxPnLO91bgbmtHbCxWkJusL/XSDocT2H/da68EqUNbdNviS3OEosoP7CLbyaOSWsxAc1+gv40L6qoHopIogW779hOYdd8cHx8gFSDQdN4ZfL9OYZDWtIXMgBGn/pkJPpkOrryP+dKygxCtNy1yCAhrvLFTd1F3sYBNAI5y3XCc= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1698167013; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=s5LF4J4xps5aQpzAhcDMKyGyVi8I6E0crdKscQezOP4=; b=DyM7xGINCTH6bP1XDbF6W0OhmQ5PCGFwLAkqAb8iohE3rPWboolNFHqMpxzIenZxQy8k95 q5BOP4bTr95T2EY+viDQQIM7rvLuHwh0CnjuzKtNgiHAaB6ee/1qTcHoRQ0/bZYSGF6LOy eNwouTABqWs/wmVqVpHE33wOUVpEEMw= 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.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-433-jMGZOdSfOyOSrFldoQPu3A-1; Tue, 24 Oct 2023 13:03:21 -0400 X-MC-Unique: jMGZOdSfOyOSrFldoQPu3A-1 Received: by mail-qk1-f198.google.com with SMTP id af79cd13be357-77893b0a2cdso587822785a.2 for ; Tue, 24 Oct 2023 10:03:20 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698166999; x=1698771799; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=s5LF4J4xps5aQpzAhcDMKyGyVi8I6E0crdKscQezOP4=; b=NBxO/nLgMR/8nER63IqR5lL3R1opjdcjlkLxQlCB91M/+/zEsv8WIHoDiBSoYybg+Z zi1KEAEy8bmW8KxFOZMA/taye0Q81374gh+kZ8E5hBz+cZoAHm6q39HEQQ5vhNe/iM/K vH+cl1TDPHp5ATloFvul2XFv1BBZO9T57Bj7c++GAx277H854SH/P74bBxpD3Ej2AJjJ Q7jciLJWhbBwv505nQpjY71nVIP/hIsSvX3hr3vnmtaR+0Uvirh+MQL5k8O7H5e3/cn9 Os0CrQiN5J+NruMOMgOKV7C1j5btQjgvgUu3Ct8T+74u7K4u48CFOqy/Vg+4jIr4LGkP wUjg== X-Gm-Message-State: AOJu0YwFnWr59pVsJzXdMtb3mNgbWgu6V6UK3R8cZ72ewe2fBy/Xm2ZC DvIxjO+pDfMOZanZwl6aSfSF010TRHR0aQD3fgOEN4kpe0t5/+EAzWLF7wm1cSHXj6U+2QTGWnr VWO6T9kM/JaDwZ3GeaTUgqeFkrdmq1LXSXFATD9erbq8ATvS2vfI+FK1ykYNWTnrAG7tDATWd0h k= X-Received: by 2002:a05:620a:284a:b0:778:91ee:a9fb with SMTP id h10-20020a05620a284a00b0077891eea9fbmr14905908qkp.5.1698166999008; Tue, 24 Oct 2023 10:03:19 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEG5kVnIOq1aIGdv22PxB4CPPEDo9tvFz79w1aqdv7LeWSwBzAOmhBLWxMtZ9PY42Xhuqv9DQ== X-Received: by 2002:a05:620a:284a:b0:778:91ee:a9fb with SMTP id h10-20020a05620a284a00b0077891eea9fbmr14905873qkp.5.1698166998586; Tue, 24 Oct 2023 10:03:18 -0700 (PDT) Received: from localhost.localdomain (ool-457670bb.dyn.optonline.net. [69.118.112.187]) by smtp.gmail.com with ESMTPSA id po10-20020a05620a384a00b007676f3859fasm3564104qkn.30.2023.10.24.10.03.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 24 Oct 2023 10:03:18 -0700 (PDT) From: Patrick Palka To: gcc-patches@gcc.gnu.org Cc: jason@redhat.com, Patrick Palka Subject: [PATCH] c++: build_new_1 and non-dep array size [PR111929] Date: Tue, 24 Oct 2023 13:03:16 -0400 Message-ID: <20231024170316.3919946-1-ppalka@redhat.com> X-Mailer: git-send-email 2.42.0.424.gceadf0f3cf MIME-Version: 1.0 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit Content-Type: text/plain; charset="US-ASCII"; x-default=true X-Spam-Status: No, score=-13.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_NONE,RCVD_IN_MSPIKE_H4,RCVD_IN_MSPIKE_WL,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 List-Id: Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look like the right approach? -- >8 -- This PR is another instance of NON_DEPENDENT_EXPR having acted as an "analysis barrier" for middle-end routines, and now that it's gone we may end up passing weird templated trees (that have a generic tree code) to the middle-end which leads to an ICE. In the testcase below the non-dependent array size 'var + 42' is expressed as an ordinary PLUS_EXPR, but whose operand types have different precisions -- long and int respectively -- naturally because templated trees encode only the syntactic form of an expression devoid of e.g. implicit conversions (typically). This type incoherency triggers a wide_int assert during the call to size_binop in build_new_1 which requires the operand types have the same precision. This patch fixes this by replacing our incremental folding of 'size' within build_new_1 with a single call to cp_fully_fold (which is a no-op in template context) once 'size' is fully built. PR c++/111929 gcc/cp/ChangeLog: * init.cc (build_new_1): Use convert, build2, build3 instead of fold_convert, size_binop and fold_build3 when building 'size'. gcc/testsuite/ChangeLog: * g++.dg/template/non-dependent28.C: New test. --- gcc/cp/init.cc | 9 +++++---- gcc/testsuite/g++.dg/template/non-dependent28.C | 6 ++++++ 2 files changed, 11 insertions(+), 4 deletions(-) create mode 100644 gcc/testsuite/g++.dg/template/non-dependent28.C diff --git a/gcc/cp/init.cc b/gcc/cp/init.cc index d48bb16c7c5..56c1b5e9f5e 100644 --- a/gcc/cp/init.cc +++ b/gcc/cp/init.cc @@ -3261,7 +3261,7 @@ build_new_1 (vec **placement, tree type, tree nelts, max_outer_nelts = wi::udiv_trunc (max_size, inner_size); max_outer_nelts_tree = wide_int_to_tree (sizetype, max_outer_nelts); - size = size_binop (MULT_EXPR, size, fold_convert (sizetype, nelts)); + size = build2 (MULT_EXPR, sizetype, size, convert (sizetype, nelts)); if (TREE_CODE (cst_outer_nelts) == INTEGER_CST) { @@ -3344,7 +3344,7 @@ build_new_1 (vec **placement, tree type, tree nelts, /* Use a class-specific operator new. */ /* If a cookie is required, add some extra space. */ if (array_p && TYPE_VEC_NEW_USES_COOKIE (elt_type)) - size = size_binop (PLUS_EXPR, size, cookie_size); + size = build2 (PLUS_EXPR, sizetype, size, cookie_size); else { cookie_size = NULL_TREE; @@ -3358,8 +3358,8 @@ build_new_1 (vec **placement, tree type, tree nelts, if (cxx_dialect >= cxx11 && flag_exceptions) errval = throw_bad_array_new_length (); if (outer_nelts_check != NULL_TREE) - size = fold_build3 (COND_EXPR, sizetype, outer_nelts_check, - size, errval); + size = build3 (COND_EXPR, sizetype, outer_nelts_check, size, errval); + size = cp_fully_fold (size); /* Create the argument list. */ vec_safe_insert (*placement, 0, size); /* Do name-lookup to find the appropriate operator. */ @@ -3418,6 +3418,7 @@ build_new_1 (vec **placement, tree type, tree nelts, /* If size is zero e.g. due to type having zero size, try to preserve outer_nelts for constant expression evaluation purposes. */ + size = cp_fully_fold (size); if (integer_zerop (size) && outer_nelts) size = build2 (MULT_EXPR, TREE_TYPE (size), size, outer_nelts); diff --git a/gcc/testsuite/g++.dg/template/non-dependent28.C b/gcc/testsuite/g++.dg/template/non-dependent28.C new file mode 100644 index 00000000000..3e45154f61d --- /dev/null +++ b/gcc/testsuite/g++.dg/template/non-dependent28.C @@ -0,0 +1,6 @@ +// PR c++/111929 + +template +void f(long var) { + new int[var + 42]; +} -- 2.42.0.424.gceadf0f3cf