public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/106650] New: [C++23] P2280 - Using unknown references in constant expressions
@ 2022-08-16 17:08 mpolacek at gcc dot gnu.org
  2022-12-09 15:27 ` [Bug c++/106650] " pinskia at gcc dot gnu.org
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2022-08-16 17:08 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 106650
           Summary: [C++23] P2280 - Using unknown references in constant
                    expressions
           Product: gcc
           Version: unknown
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: mpolacek at gcc dot gnu.org
  Target Milestone: ---

See <https://wg21.link/p2280>.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug c++/106650] [C++23] P2280 - Using unknown references in constant expressions
  2022-08-16 17:08 [Bug c++/106650] New: [C++23] P2280 - Using unknown references in constant expressions mpolacek at gcc dot gnu.org
@ 2022-12-09 15:27 ` pinskia at gcc dot gnu.org
  2023-11-16 22:36 ` mpolacek at gcc dot gnu.org
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-12-09 15:27 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
     Ever confirmed|0                           |1
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2022-12-09

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug c++/106650] [C++23] P2280 - Using unknown references in constant expressions
  2022-08-16 17:08 [Bug c++/106650] New: [C++23] P2280 - Using unknown references in constant expressions mpolacek at gcc dot gnu.org
  2022-12-09 15:27 ` [Bug c++/106650] " pinskia at gcc dot gnu.org
@ 2023-11-16 22:36 ` mpolacek at gcc dot gnu.org
  2023-11-17 21:47 ` mpolacek at gcc dot gnu.org
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2023-11-16 22:36 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
With just

--- a/gcc/cp/constexpr.cc
+++ b/gcc/cp/constexpr.cc
@@ -7378,6 +7378,8 @@ cxx_eval_constant_expression (const constexpr_ctx *ctx,
tree t,
      r = build_constructor (TREE_TYPE (t), NULL);
      TREE_CONSTANT (r) = true;
    }
+      else if (TYPE_REF_P (TREE_TYPE (t)))
+   /* P2280 */;
       else
    {
      if (!ctx->quiet)

the test from P2280 behaves as expected:

$ xg++ -c z.C -std=c++20
z.C: In function ‘void splash(Swim&)’:
z.C:35:33: error: non-constant condition for static assertion
   35 |   static_assert(pswam->phelps() == 28);         // error:
lvalue-to-rvalue conversion on a pointer
      |                 ~~~~~~~~~~~~~~~~^~~~~
z.C:35:17: error: the value of ‘pswam’ is not usable in a constant expression
   35 |   static_assert(pswam->phelps() == 28);         // error:
lvalue-to-rvalue conversion on a pointer
      |                 ^~~~~
z.C:34:9: note: ‘pswam’ was not declared ‘constexpr’
   34 |   Swim* pswam = &swam;
      |         ^~~~~
z.C:41:31: error: non-constant condition for static assertion
   41 |   static_assert(swam.lochte() == 12);           // error: invoking
virtual function on reference
      |                 ~~~~~~~~~~~~~~^~~~~
z.C:41:28: error: ‘* & swam’ is not a constant expression
   41 |   static_assert(swam.lochte() == 12);           // error: invoking
virtual function on reference
      |                 ~~~~~~~~~~~^~
z.C:44:31: error: non-constant condition for static assertion
   44 |   static_assert(swam.coughlin == 12);           // error:
lvalue-to-rvalue conversion on an object
      |                 ~~~~~~~~~~~~~~^~~~~
z.C:44:31: error: ‘swam’ is not a constant expression
z.C: At global scope:
z.C:52:36: error: the value of ‘trident’ is not usable in a constant expression
   52 | constexpr auto& gallagher = typeid(trident);    // error:
constexpr-unknown dynamic type
      |                                    ^~~~~~~
z.C:49:14: note: ‘trident’ was not initialized with a constant expression
   49 | extern Swim& trident;
      |              ^~~~~~~


It cannot be that easy, can it?

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug c++/106650] [C++23] P2280 - Using unknown references in constant expressions
  2022-08-16 17:08 [Bug c++/106650] New: [C++23] P2280 - Using unknown references in constant expressions mpolacek at gcc dot gnu.org
  2022-12-09 15:27 ` [Bug c++/106650] " pinskia at gcc dot gnu.org
  2023-11-16 22:36 ` mpolacek at gcc dot gnu.org
@ 2023-11-17 21:47 ` mpolacek at gcc dot gnu.org
  2023-11-29 22:10 ` cvs-commit at gcc dot gnu.org
  2023-11-29 22:24 ` mpolacek at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2023-11-17 21:47 UTC (permalink / raw)
  To: gcc-bugs

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

Marek Polacek <mpolacek at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Assignee|unassigned at gcc dot gnu.org      |mpolacek at gcc dot gnu.org
             Status|NEW                         |ASSIGNED

--- Comment #3 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
Patch posted:
https://gcc.gnu.org/pipermail/gcc-patches/2023-November/637101.html
though I'm not sure it's complete.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug c++/106650] [C++23] P2280 - Using unknown references in constant expressions
  2022-08-16 17:08 [Bug c++/106650] New: [C++23] P2280 - Using unknown references in constant expressions mpolacek at gcc dot gnu.org
                   ` (2 preceding siblings ...)
  2023-11-17 21:47 ` mpolacek at gcc dot gnu.org
@ 2023-11-29 22:10 ` cvs-commit at gcc dot gnu.org
  2023-11-29 22:24 ` mpolacek at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2023-11-29 22:10 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from GCC Commits <cvs-commit at gcc dot gnu.org> ---
The trunk branch has been updated by Marek Polacek <mpolacek@gcc.gnu.org>:

https://gcc.gnu.org/g:99d114c15523e0bfe7a89ef1947f82eb5ff0260b

commit r14-5979-g99d114c15523e0bfe7a89ef1947f82eb5ff0260b
Author: Marek Polacek <polacek@redhat.com>
Date:   Fri Nov 17 14:48:44 2023 -0500

    c++: P2280R4, Using unknown refs in constant expr [PR106650]

    This patch is an attempt to implement (part of?) P2280, Using unknown
    pointers and references in constant expressions.  (Note that R4 seems to
    only allow References to unknown/Accesses via this, but not Pointers to
    unknown.)

    This patch works to the extent that the test case added in [expr.const]
    works as expected, as well as the test in
   
<https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p2280r4.html#the-this-pointer>

    Most importantly, the proposal makes this compile:

      template <typename T, size_t N>
      constexpr auto array_size(T (&)[N]) -> size_t {
          return N;
      }

      void check(int const (&param)[3]) {
          constexpr auto s = array_size(param);
          static_assert (s == 3);
      }

    and I think it would be a pity not to have it in GCC 14.

    What still doesn't work is the test in $3.2:

      struct A2 { constexpr int f() { return 0; } };
      struct B2 : virtual A2 {};
      void f2(B2 &b) { constexpr int k = b.f(); }

    where we say
    error: '* & b' is not a constant expression

    This will be fixed in the future.

            PR c++/106650

    gcc/cp/ChangeLog:

            * constexpr.cc (cxx_eval_constant_expression) <case PARM_DECL>:
Allow
            reference to unknown/this as per P2280.
            <case VAR_DECL>: Allow reference to unknown as per P2280.

    gcc/testsuite/ChangeLog:

            * g++.dg/cpp0x/constexpr-array-ptr6.C: Remove dg-error.
            * g++.dg/cpp0x/constexpr-ref12.C: Likewise.
            * g++.dg/cpp0x/constexpr-ref2.C: Adjust dg-error.
            * g++.dg/cpp0x/noexcept34.C: Remove dg-error.
            * g++.dg/cpp1y/lambda-generic-const10.C: Likewise.
            * g++.dg/cpp0x/constexpr-ref13.C: New test.
            * g++.dg/cpp1z/constexpr-ref1.C: New test.
            * g++.dg/cpp1z/constexpr-ref2.C: New test.
            * g++.dg/cpp2a/constexpr-ref1.C: New test.

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [Bug c++/106650] [C++23] P2280 - Using unknown references in constant expressions
  2022-08-16 17:08 [Bug c++/106650] New: [C++23] P2280 - Using unknown references in constant expressions mpolacek at gcc dot gnu.org
                   ` (3 preceding siblings ...)
  2023-11-29 22:10 ` cvs-commit at gcc dot gnu.org
@ 2023-11-29 22:24 ` mpolacek at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2023-11-29 22:24 UTC (permalink / raw)
  To: gcc-bugs

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

Marek Polacek <mpolacek at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|ASSIGNED                    |RESOLVED
         Resolution|---                         |FIXED

--- Comment #5 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
Implemented in GCC 14.

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2023-11-29 22:24 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-08-16 17:08 [Bug c++/106650] New: [C++23] P2280 - Using unknown references in constant expressions mpolacek at gcc dot gnu.org
2022-12-09 15:27 ` [Bug c++/106650] " pinskia at gcc dot gnu.org
2023-11-16 22:36 ` mpolacek at gcc dot gnu.org
2023-11-17 21:47 ` mpolacek at gcc dot gnu.org
2023-11-29 22:10 ` cvs-commit at gcc dot gnu.org
2023-11-29 22:24 ` mpolacek at gcc dot gnu.org

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).