public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jason Merrill <jason@redhat.com>
To: Marek Polacek <polacek@redhat.com>,
	GCC Patches <gcc-patches@gcc.gnu.org>
Subject: Re: [PATCH] c++: fix ICE with sizeof in a template [PR112869]
Date: Fri, 8 Dec 2023 12:09:18 -0500	[thread overview]
Message-ID: <9c08e101-0392-4c01-b336-6558e80a4193@redhat.com> (raw)
In-Reply-To: <20231205203136.94832-1-polacek@redhat.com>

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<long unsigned int>(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?

> gcc/testsuite/ChangeLog:
> 
> 	* g++.dg/template/sizeof18.C: New test.
> ---
>   gcc/cp/cp-gimplify.cc                    | 8 ++------
>   gcc/testsuite/g++.dg/template/sizeof18.C | 8 ++++++++
>   2 files changed, 10 insertions(+), 6 deletions(-)
>   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..46c3eb91853 100644
> --- a/gcc/cp/cp-gimplify.cc
> +++ b/gcc/cp/cp-gimplify.cc
> @@ -1177,13 +1177,9 @@ cp_fold_immediate_r (tree *stmt_p, int *walk_subtrees, void *data_)
>   				   ? tf_error : tf_none);
>     const tree_code code = TREE_CODE (stmt);
>   
> -  /* No need to look into types or unevaluated operands.
> -     NB: This affects cp_fold_r as well.  */
> +  /* No need to look into types or unevaluated operands.  */
>     if (TYPE_P (stmt) || unevaluated_p (code) || in_immediate_context ())
> -    {
> -      *walk_subtrees = 0;
> -      return NULL_TREE;
> -    }
> +    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 <class T> void Binaryread(int &, T, unsigned long);
> +template <> void Binaryread(int &, float, unsigned long bytecount) {
> +  min(bytecount, sizeof(int));
> +}
> 
> base-commit: 9c3a880feecf81c310b4ade210fbd7004c9aece7


  reply	other threads:[~2023-12-08 17:09 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-05 20:31 Marek Polacek
2023-12-08 17:09 ` Jason Merrill [this message]
2023-12-08 21:15   ` [PATCH v2] " Marek Polacek
2023-12-09  4:09     ` Jason Merrill
2023-12-12 22:48       ` [PATCH v3] " Marek Polacek
2023-12-13 20:28         ` Jason Merrill
2023-12-14 21:01           ` [PATCH v4] " Marek Polacek
2023-12-14 21:04             ` Jason Merrill

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=9c08e101-0392-4c01-b336-6558e80a4193@redhat.com \
    --to=jason@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=polacek@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).