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.133.124]) by sourceware.org (Postfix) with ESMTPS id 4E0343858CDB for ; Fri, 8 Dec 2023 21:15:10 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4E0343858CDB 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 4E0343858CDB 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=1702070111; cv=none; b=lrZcZHWlXMGoThv47q/tDKhY/OkcocPb89Idwu8oYqa1DIpNUl7kQpGIeX6pqPB7vleWJwSPFDBF/qJsf5+Oe2y8eK9qkh41glTosVEBa/ukQAkWehV22SDval17BhdV7DDIQNSafcs2/F8gMJzGSOrY0CPWs9dQGCtPeEZnDuM= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1702070111; c=relaxed/simple; bh=l4t5tjzk0zwVUojlXPLu49f3j4u5XoF1DNkY1bTDTeg=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=brTVXhu23pxnfeOdnISDZgA87rexSIedRciLn440fmLb6m6cajt04u+MMJCnNecMvNyZjd9HQ6IYe6a1dKhTVfwyv5kp4ZTTw0pQRk55vJyGGLPKGQsz6gb1j864W7vcbO8rL89mAa65/FV/CgtDI1S4r8Oafwu+jhKu84JAgTE= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1702070109; 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: in-reply-to:in-reply-to:references:references; bh=tkHHuEngnmzymzA9bnvz5TRIdog8Mo3fit4DOvrBQh0=; b=ICkOvVapBsgfULQFQLVUCe2QGiXiLnsfJYeuk8v5hZTLWKD2LW9q4RGEw8zhrKxmJ/Eq9Y W9ixmdf1Dl4BnVzFriXhIJeneIt+dMDOrfTFGQD6Ityh5qQgLW+66hWf6XE5WRiwXWSvLg WLQ4qJTGKC86gn7ZlaDltTEr5/Sw6Vs= Received: from mail-qt1-f198.google.com (mail-qt1-f198.google.com [209.85.160.198]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-260-CWi3HcwQMxqoSlPd-Y1cKw-1; Fri, 08 Dec 2023 16:15:08 -0500 X-MC-Unique: CWi3HcwQMxqoSlPd-Y1cKw-1 Received: by mail-qt1-f198.google.com with SMTP id d75a77b69052e-4259aebd1f7so16328141cf.3 for ; Fri, 08 Dec 2023 13:15:08 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702070107; x=1702674907; h=user-agent:in-reply-to:content-disposition:mime-version:references :message-id:subject:cc:to:from:date:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=tkHHuEngnmzymzA9bnvz5TRIdog8Mo3fit4DOvrBQh0=; b=jzq/KJoiQShuJ22aWurjs0paJ5bl1JvFHXHLnPfgWVsCK1l/uGHG1Bbx2QZV+DjC9o 8FSEtT5h6051OoacaHXr0Jms69eEtzguKFNG5xBckz8QBXd8ZXYhnrSe+0zMQmZ/IyjX xOTvISRg2ZnrGQSGM7RQuF1NNSNivEH10Dr54w3DsfWEbokR+6eomNj7zhmD4xIZAV4/ rIVgwatQI4ypgGWxRPeP9auWHoZhqRyVhPmgqhX8luSRrUjryj2dWH910s/ZIzlipcPa 7uc4u3+5yY7L8+ph4W1GIMEEzC4BU74RYVVljx77Tt5C3fSu+s9vhfSy7IHCXiJ9SquA uhMA== X-Gm-Message-State: AOJu0Yy4XreSSmo4sufmrfbjtBDh3sB0ZGB4o8Nlu6uogC6Xg72ANl49 bJhIbyKy6k/EIAwM36YAXSmfLI0TZIdEBadHa+GFZF+1ACiiWIhmW72ghcuTzJtQHvU5KuIotdY C2xKIHvb18+lD41GKayMD8e3FVw== X-Received: by 2002:ac8:5891:0:b0:411:616a:4bde with SMTP id t17-20020ac85891000000b00411616a4bdemr893131qta.18.1702070107389; Fri, 08 Dec 2023 13:15:07 -0800 (PST) X-Google-Smtp-Source: AGHT+IGoAisuC2iTX6z1K0NCALi8vqVrXdzVYYPglugZEuZvLIGN3VAi5TxgbP8TDeyWBiJPTFcfqA== X-Received: by 2002:ac8:5891:0:b0:411:616a:4bde with SMTP id t17-20020ac85891000000b00411616a4bdemr893122qta.18.1702070107086; Fri, 08 Dec 2023 13:15:07 -0800 (PST) Received: from redhat.com (2603-7000-9500-34a5-0000-0000-0000-1db4.res6.spectrum.com. [2603:7000:9500:34a5::1db4]) by smtp.gmail.com with ESMTPSA id z4-20020ac875c4000000b004180fb5c6adsm1081981qtq.25.2023.12.08.13.15.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Dec 2023 13:15:06 -0800 (PST) Date: Fri, 8 Dec 2023 16:15:05 -0500 From: Marek Polacek To: Jason Merrill Cc: GCC Patches Subject: [PATCH v2] c++: fix ICE with sizeof in a template [PR112869] Message-ID: References: <20231205203136.94832-1-polacek@redhat.com> <9c08e101-0392-4c01-b336-6558e80a4193@redhat.com> MIME-Version: 1.0 In-Reply-To: <9c08e101-0392-4c01-b336-6558e80a4193@redhat.com> User-Agent: Mutt/2.2.10 (2023-03-25) X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-Spam-Status: No, score=-12.2 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_H3,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_NONE,TXREP,T_SCC_BODY_TEXT_LINE 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: On Fri, Dec 08, 2023 at 12:09:18PM -0500, Jason Merrill wrote: > On 12/5/23 15:31, Marek Polacek wrote: > > Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? > > > > -- >8 -- > > This test shows that we cannot clear *walk_subtrees in > > cp_fold_immediate_r when we're in_immediate_context, because that, > > as the comment says, affects cp_fold_r as well. Here we had an > > expression with > > > > min ((long int) VIEW_CONVERT_EXPR(bytecount), (long int) <<< Unknown tree: sizeof_expr > > (int) <<< error >>> >>>) > > > > as its sub-expression, and we never evaluated that into > > > > min ((long int) bytecount, 4) > > > > so the SIZEOF_EXPR leaked into the middle end. > > > > (There's still one *walk_subtrees = 0; in cp_fold_immediate_r, but that > > one should be OK.) > > > > PR c++/112869 > > > > gcc/cp/ChangeLog: > > > > * cp-gimplify.cc (cp_fold_immediate_r): Don't clear *walk_subtrees > > for unevaluated operands. > > I agree that we want this change for in_immediate_context (), but I don't > see why we want it for TYPE_P or unevaluated_p (code) or > cp_unevaluated_operand? No particular reason, just paranoia. How's this? Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk? -- >8 -- This test shows that we cannot clear *walk_subtrees in cp_fold_immediate_r when we're in_immediate_context, because that, as the comment says, affects cp_fold_r as well. Here we had an expression with min ((long int) VIEW_CONVERT_EXPR(bytecount), (long int) <<< Unknown tree: sizeof_expr (int) <<< error >>> >>>) as its sub-expression, and we never evaluated that into min ((long int) bytecount, 4) so the SIZEOF_EXPR leaked into the middle end. (There's still one *walk_subtrees = 0; in cp_fold_immediate_r, but that one should be OK.) PR c++/112869 gcc/cp/ChangeLog: * cp-gimplify.cc (cp_fold_immediate_r): Don't clear *walk_subtrees for in_immediate_context. gcc/testsuite/ChangeLog: * g++.dg/template/sizeof18.C: New test. --- gcc/cp/cp-gimplify.cc | 6 +++++- gcc/testsuite/g++.dg/template/sizeof18.C | 8 ++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/g++.dg/template/sizeof18.C diff --git a/gcc/cp/cp-gimplify.cc b/gcc/cp/cp-gimplify.cc index 5abb91bbdd3..6af7c787372 100644 --- a/gcc/cp/cp-gimplify.cc +++ b/gcc/cp/cp-gimplify.cc @@ -1179,11 +1179,15 @@ cp_fold_immediate_r (tree *stmt_p, int *walk_subtrees, void *data_) /* No need to look into types or unevaluated operands. NB: This affects cp_fold_r as well. */ - if (TYPE_P (stmt) || unevaluated_p (code) || in_immediate_context ()) + if (TYPE_P (stmt) || unevaluated_p (code)) { *walk_subtrees = 0; return NULL_TREE; } + else if (in_immediate_context ()) + /* Don't clear *walk_subtrees here: we still need to walk the subtrees + of SIZEOF_EXPR and similar. */ + return NULL_TREE; tree decl = NULL_TREE; bool call_p = false; diff --git a/gcc/testsuite/g++.dg/template/sizeof18.C b/gcc/testsuite/g++.dg/template/sizeof18.C new file mode 100644 index 00000000000..afba9946258 --- /dev/null +++ b/gcc/testsuite/g++.dg/template/sizeof18.C @@ -0,0 +1,8 @@ +// PR c++/112869 +// { dg-do compile } + +void min(long, long); +template void Binaryread(int &, T, unsigned long); +template <> void Binaryread(int &, float, unsigned long bytecount) { + min(bytecount, sizeof(int)); +} base-commit: d468718c9a097aeb8794fb1a2df6db2c1064d7f7 -- 2.43.0