public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Marek Polacek <mpolacek@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc r14-4659] c++: fix truncated diagnostic in C++23 [PR111272]
Date: Mon, 16 Oct 2023 12:33:28 +0000 (GMT)	[thread overview]
Message-ID: <20231016123328.CB2FB3858C2A@sourceware.org> (raw)

https://gcc.gnu.org/g:a22eeaca5ce753a0a3c22013ee3ecde04c71c2f4

commit r14-4659-ga22eeaca5ce753a0a3c22013ee3ecde04c71c2f4
Author: Marek Polacek <polacek@redhat.com>
Date:   Fri Oct 13 16:47:47 2023 -0400

    c++: fix truncated diagnostic in C++23 [PR111272]
    
    In C++23, since P2448, a constexpr function F that calls a non-constexpr
    function N is OK as long as we don't actually call F in a constexpr
    context.  So instead of giving an error in maybe_save_constexpr_fundef,
    we only give an error when evaluating the call.  Unfortunately, as shown
    in this PR, the diagnostic can be truncated:
    
    z.C:10:13: note: 'constexpr Jam::Jam()' is not usable as a 'constexpr' function because:
       10 |   constexpr Jam() { ft(); }
          |             ^~~
    
    ...because what?  With this patch, we say:
    
    z.C:10:13: note: 'constexpr Jam::Jam()' is not usable as a 'constexpr' function because:
       10 |   constexpr Jam() { ft(); }
          |             ^~~
    z.C:10:23: error: call to non-'constexpr' function 'int Jam::ft()'
       10 |   constexpr Jam() { ft(); }
          |                     ~~^~
    z.C:8:7: note: 'int Jam::ft()' declared here
        8 |   int ft() { return 42; }
          |       ^~
    
    Like maybe_save_constexpr_fundef, explain_invalid_constexpr_fn should
    also check the body of a constructor, not just the mem-initializer.
    
            PR c++/111272
    
    gcc/cp/ChangeLog:
    
            * constexpr.cc (explain_invalid_constexpr_fn): Also check the body of
            a constructor in C++14 and up.
    
    gcc/testsuite/ChangeLog:
    
            * g++.dg/cpp1y/constexpr-diag1.C: New test.

Diff:
---
 gcc/cp/constexpr.cc                          | 10 +++++++++-
 gcc/testsuite/g++.dg/cpp1y/constexpr-diag1.C | 21 +++++++++++++++++++++
 2 files changed, 30 insertions(+), 1 deletion(-)

diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc
index 0f948db7c2dc..dde4fec4a442 100644
--- a/gcc/cp/constexpr.cc
+++ b/gcc/cp/constexpr.cc
@@ -1098,7 +1098,15 @@ explain_invalid_constexpr_fn (tree fun)
 	  body = massage_constexpr_body (fun, body);
 	  require_potential_rvalue_constant_expression (body);
 	  if (DECL_CONSTRUCTOR_P (fun))
-	    cx_check_missing_mem_inits (DECL_CONTEXT (fun), body, true);
+	    {
+	      cx_check_missing_mem_inits (DECL_CONTEXT (fun), body, true);
+	      if (cxx_dialect > cxx11)
+		{
+		  /* Also check the body, not just the ctor-initializer.  */
+		  body = DECL_SAVED_TREE (fun);
+		  require_potential_rvalue_constant_expression (body);
+		}
+	    }
 	}
     }
 }
diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-diag1.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-diag1.C
new file mode 100644
index 000000000000..0e2909e83ef6
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-diag1.C
@@ -0,0 +1,21 @@
+// PR c++/111272
+// { dg-do compile { target c++14 } }
+// { dg-options "-Werror=invalid-constexpr" }
+// { dg-prune-output "some warnings being treated as errors" }
+
+struct Jam
+{
+  // constexpr  // n.b.
+  int ft() { return 42; } // { dg-message "declared here" }
+
+  constexpr Jam() { ft(); } // { dg-error "call to non-.constexpr. function" }
+// { dg-message "declared here" "" { target c++20_down } .-1 }
+};
+
+constexpr bool test()
+{
+  Jam j; // { dg-error "called in a constant expression" }
+  return true;
+}
+
+static_assert(test(), ""); // { dg-error "non-constant condition" }

                 reply	other threads:[~2023-10-16 12:33 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20231016123328.CB2FB3858C2A@sourceware.org \
    --to=mpolacek@gcc.gnu.org \
    --cc=gcc-cvs@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).