public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "redi at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c++/97572] [c++ 20] Constraining is broken
Date: Tue, 27 Apr 2021 10:22:39 +0000	[thread overview]
Message-ID: <bug-97572-4-nliYAMV5kh@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-97572-4@http.gcc.gnu.org/bugzilla/>

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=97572

--- Comment #5 from Jonathan Wakely <redi at gcc dot gnu.org> ---
The diagnostic is different now and the "cannot convert 'T' to 'bool'" part is
gone. For comment 0 we get:


97572.C: In substitution of 'template<class auto:1>  requires  Any<auto:1>
constexpr int f(auto:1) [with auto:1 = bool]':
97572.C:4:22:   required from here
97572.C:2:63: error: 't' is not a constant expression
    2 | template <typename T> concept Any = requires (T t) { requires any (t);
};
      |                                                      ~~~~~~~~~^~~~~~~
97572.C:4:22: error: no matching function for call to 'f(bool)'
    4 | constexpr auto q = f (false);
      |                    ~~^~~~~~~
97572.C:3:22: note: candidate: 'template<class auto:1>  requires  Any<auto:1>
constexpr int f(auto:1)'
    3 | constexpr static int f (Any auto) { return 42; };
      |                      ^
97572.C:3:22: note:   substitution of deduced template arguments resulted in
errors seen above


This seems good enough to me. It tells you that 't' is not a constant
expression, which is correct, and that means that the call any(t) is not a
constant expression and so can't be used as the operand to 'requires'. Because
that constraint fails, it means that you can't call f(Any auto), and so there
is not matching function.

I still think it might be helpful to say that local parameters of a
requires-expression are never constant expressions.

For comment 2 we get:

97572-c2.C:2:63: error: 't' is not a constant expression
    2 | template <typename T> concept Any = requires (T t) { requires any (t);
};
      |                                                      ~~~~~~~~~^~~~~~~
97572-c2.C:3:16: error: static assertion failed
    3 | static_assert (Any <bool>);
      |                ^~~~~~~~~~
97572-c2.C:3:16: note: constraints not satisfied
97572-c2.C:2:31:   required by the constraints of 'template<class T> concept
Any'
97572-c2.C:2:37:   in requirements with 'T t' [with T = bool]
97572-c2.C:2:63: error: 't' is not a constant expression
    2 | template <typename T> concept Any = requires (T t) { requires any (t);
};
      |                                                      ~~~~~~~~~^~~~~~~
97572-c2.C:2:63: error: 't' is not a constant expression

Again, this seems good enough.

(In reply to Dimitri Gorokhovik from comment #4)
> -- arguably, a close contender is "error: ‘t’ is not a constant expression".

But it's absolutely correct.

> Understanding how it can happen was hard to me. 't' looks as constant as
> they get with requires-local-parameters (I mean, constexpr-ness of a
> local-parameter here should be understood as coming only from its
> declaration shouldn't it?).

Yes, and function parameters are *never* constant expressions, even in
consteval functions. Why do you think it would be different here?

constexpr int f(int n) { static_assert(n!=1); }

ce.C:1:41: error: ‘n’ is not a constant expression

It's constexpr-ness *does* come from its declaration, and it's not declared as
a constant.

> The (S s) form of requires-local-var is a common
> form, so when the compiler says 's' is not a constant expression' here, how
> would it be constant in other situations?

By using ... a constant of course.

e.g. a literal like 1 or true, or a constant like std::is_integral_v<T>.

  parent reply	other threads:[~2021-04-27 10:22 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-10-25 19:21 [Bug c++/97572] New: " dimitri.gorokhovik at free dot fr
2020-10-25 22:06 ` [Bug c++/97572] " redi at gcc dot gnu.org
2020-10-26 10:45 ` dimitri.gorokhovik at free dot fr
2020-10-26 11:37 ` redi at gcc dot gnu.org
2020-10-26 12:22 ` dimitri.gorokhovik at free dot fr
2021-04-27 10:22 ` redi at gcc dot gnu.org [this message]
2021-04-27 10:27 ` redi at gcc dot gnu.org

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=bug-97572-4-nliYAMV5kh@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /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).