public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Patrick Palka <ppalka@redhat.com>
To: Marek Polacek <polacek@redhat.com>
Cc: GCC Patches <gcc-patches@gcc.gnu.org>, Jason Merrill <jason@redhat.com>
Subject: Re: [PATCH] c++: ICE with auto(0) in requires-expression [PR103408]
Date: Wed, 1 Dec 2021 12:17:47 -0500	[thread overview]
Message-ID: <CAMOnLZYAS45VBh_N=bSMJPTOoJyPO+GdjsbvvDv53DJU7XGJfA@mail.gmail.com> (raw)
In-Reply-To: <20211201151629.1497940-1-polacek@redhat.com>

On Wed, Dec 1, 2021 at 10:26 AM Marek Polacek via Gcc-patches
<gcc-patches@gcc.gnu.org> wrote:
>
> Here we ICE on
>
>   int f() requires (auto(0));
>
> in do_auto_deduction when handling the auto: we're in a non-templated
> requires-expression which are parsed under processing_template_decl == 1
> and empty current_template_parms, so 'current_template_args ()' will
> crash.  This code is invalid as per "C++20 CA378: Remove non-templated
> constrained functions", but of course we shouldn't crash.

FWIW it looks like we can trip over the same bug with valid code:

  static_assert(requires { auto(0); });

>
> Since in the scenario above it's expected that current_template_parms is
> null, I've just added a check, and let grokfndecl issue an error.
> I guess another approch would be to fake up a template parameter list
> before calling do_auto_deduction.
>
> For good measure, I've added several well-formed cases with auto(x) in
> a requires-expression.
>
> Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?
>
>         PR c++/103408
>
> gcc/cp/ChangeLog:
>
>         * pt.c (do_auto_deduction): Check current_template_parms before
>         current_template_args ().
>
> gcc/testsuite/ChangeLog:
>
>         * g++.dg/cpp23/auto-fncast9.C: New test.
> ---
>  gcc/cp/pt.c                               |  2 +-
>  gcc/testsuite/g++.dg/cpp23/auto-fncast9.C | 27 +++++++++++++++++++++++
>  2 files changed, 28 insertions(+), 1 deletion(-)
>  create mode 100644 gcc/testsuite/g++.dg/cpp23/auto-fncast9.C
>
> diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c
> index f4b9d9673fb..012ca5d06c0 100644
> --- a/gcc/cp/pt.c
> +++ b/gcc/cp/pt.c
> @@ -30041,7 +30041,7 @@ do_auto_deduction (tree type, tree init, tree auto_node,
>         (but we still may have used them for constraint checking above).  */;
>    else if (context == adc_unify)
>      targs = add_to_template_args (outer_targs, targs);
> -  else if (processing_template_decl)
> +  else if (processing_template_decl && current_template_parms)
>      targs = add_to_template_args (current_template_args (), targs);
>    return tsubst (type, targs, complain, NULL_TREE);

Won't this mean the call to tsubst here will end up lowering the level
of the auto from 2 to 1 rather than replacing it with the actual
deduced type?

It also looks like this approach doesn't handle static_assert(requires
{ auto(auto(0)); }), probably due to this substitution issue.  I guess
we could add a dummy level to 'targs' to work around this..

>  }
> diff --git a/gcc/testsuite/g++.dg/cpp23/auto-fncast9.C b/gcc/testsuite/g++.dg/cpp23/auto-fncast9.C
> new file mode 100644
> index 00000000000..45a0ff9b460
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/cpp23/auto-fncast9.C
> @@ -0,0 +1,27 @@
> +// PR c++/103408
> +// { dg-do compile { target c++23 } }
> +
> +int bad1() requires (auto(true)); // { dg-error "constraints on a non-templated function" }
> +
> +template<bool B>
> +int f1() requires (auto(B));
> +
> +template<typename T>
> +struct S { T t; constexpr operator bool() { return true; } };
> +
> +int bad2() requires (bool(S{1})); // { dg-error "constraints on a non-templated function" }
> +int bad3() requires (bool(S(1))); // { dg-error "constraints on a non-templated function" }
> +
> +template<int N>
> +int f2() requires (bool(S{N}));
> +
> +template<int N>
> +int f3() requires (bool(S(N)));
> +
> +void
> +g ()
> +{
> +  f1<true>();
> +  f2<42>();
> +  f3<42>();
> +}
>
> base-commit: 6b8ecbc6d6652d061d7c72c64352d51eca2df6ca
> --
> 2.33.1
>


  reply	other threads:[~2021-12-01 17:18 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-12-01 15:16 Marek Polacek
2021-12-01 17:17 ` Patrick Palka [this message]
2021-12-02  0:45   ` [PATCH v2] " Marek Polacek

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='CAMOnLZYAS45VBh_N=bSMJPTOoJyPO+GdjsbvvDv53DJU7XGJfA@mail.gmail.com' \
    --to=ppalka@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jason@redhat.com \
    --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).