public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Jason Merrill <jason@gcc.gnu.org>
To: gcc-cvs@gcc.gnu.org
Subject: [gcc r13-2965] c++: check DECL_INITIAL for constexpr
Date: Thu, 29 Sep 2022 17:52:31 +0000 (GMT)	[thread overview]
Message-ID: <20220929175231.80BE03854164@sourceware.org> (raw)

https://gcc.gnu.org/g:73d9b0e5947e162386f7e25d3851097cee1bb366

commit r13-2965-g73d9b0e5947e162386f7e25d3851097cee1bb366
Author: Jason Merrill <jason@redhat.com>
Date:   Tue Sep 20 17:12:29 2022 -0400

    c++: check DECL_INITIAL for constexpr
    
    We were overlooking non-potentially-constant bits in variable initializer
    because we didn't walk into DECL_INITIAL.
    
    gcc/cp/ChangeLog:
    
            * constexpr.cc (potential_constant_expression_1): Look into
            DECL_INITIAL.  Use location wrappers.
    
    gcc/testsuite/ChangeLog:
    
            * g++.dg/cpp1y/constexpr-local4.C: Expect error sooner.
            * g++.dg/cpp2a/consteval24.C: Likewise.
            * g++.dg/cpp2a/consteval7.C: Likewise.
            * g++.dg/cpp2a/inline-asm3.C: Likewise.

Diff:
---
 gcc/cp/constexpr.cc                           | 7 +++++--
 gcc/testsuite/g++.dg/cpp1y/constexpr-local4.C | 4 ++--
 gcc/testsuite/g++.dg/cpp2a/consteval24.C      | 2 +-
 gcc/testsuite/g++.dg/cpp2a/consteval7.C       | 2 +-
 gcc/testsuite/g++.dg/cpp2a/inline-asm3.C      | 2 +-
 5 files changed, 10 insertions(+), 7 deletions(-)

diff --git a/gcc/cp/constexpr.cc b/gcc/cp/constexpr.cc
index 10639876d9c..ed41d755269 100644
--- a/gcc/cp/constexpr.cc
+++ b/gcc/cp/constexpr.cc
@@ -8892,7 +8892,10 @@ potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now,
       {
         tree from = TREE_OPERAND (t, 0);
 	if (location_wrapper_p (t))
-	  return (RECUR (from, want_rval));
+	  {
+	    iloc_sentinel ils = loc;
+	    return (RECUR (from, want_rval));
+	  }
 	if (INDIRECT_TYPE_P (TREE_TYPE (t)))
 	  {
 	    STRIP_ANY_LOCATION_WRAPPER (from);
@@ -9348,7 +9351,7 @@ potential_constant_expression_1 (tree t, bool want_rval, bool strict, bool now,
 		   (tmp, /*constexpr_context_p=*/true, flags))
 	    return false;
 	}
-      return RECUR (tmp, want_rval);
+      return RECUR (DECL_INITIAL (tmp), want_rval);
 
     case TRY_FINALLY_EXPR:
       return (RECUR (TREE_OPERAND (t, 0), want_rval)
diff --git a/gcc/testsuite/g++.dg/cpp1y/constexpr-local4.C b/gcc/testsuite/g++.dg/cpp1y/constexpr-local4.C
index bef62488579..647b5dcd7cd 100644
--- a/gcc/testsuite/g++.dg/cpp1y/constexpr-local4.C
+++ b/gcc/testsuite/g++.dg/cpp1y/constexpr-local4.C
@@ -10,8 +10,8 @@ const A a = 42;
 
 constexpr int f()
 {
-  const int j = a.i;		// { dg-message "'a'" }
+  const int j = a.i;		// { dg-error "'a'" }
   return j;
 }
 
-static_assert (f() == 42,"");	// { dg-error "non-constant" }
+static_assert (f() == 42,"");	// { dg-error "" }
diff --git a/gcc/testsuite/g++.dg/cpp2a/consteval24.C b/gcc/testsuite/g++.dg/cpp2a/consteval24.C
index 6d0c63c749b..6d7034c5515 100644
--- a/gcc/testsuite/g++.dg/cpp2a/consteval24.C
+++ b/gcc/testsuite/g++.dg/cpp2a/consteval24.C
@@ -27,4 +27,4 @@ bar ()
   return fn1 () + fn2 () + (s.*fn3) () + (s.*fn4) () + fn5 () + (s.*fn6) () + (s.*fn7) ();
 }
 
-auto a = bar ();
+auto a = bar ();		// { dg-error "bar" }
diff --git a/gcc/testsuite/g++.dg/cpp2a/consteval7.C b/gcc/testsuite/g++.dg/cpp2a/consteval7.C
index 23f3d25a39e..74996d31a82 100644
--- a/gcc/testsuite/g++.dg/cpp2a/consteval7.C
+++ b/gcc/testsuite/g++.dg/cpp2a/consteval7.C
@@ -10,4 +10,4 @@ consteval int qux () { S s = baz (); return s.b + s.c (); }
 consteval int quux () { constexpr S s = baz (); return s.b + s.c (); }	// { dg-error "immediate evaluation returns address of immediate function 'consteval int foo\\(\\)'" }
 constexpr auto d = baz ();	// { dg-error "immediate evaluation returns address of immediate function 'consteval int foo\\(\\)'" }
 constexpr auto e = qux ();
-constexpr auto f = quux ();
+constexpr auto f = quux ();	// { dg-error "quux" }
diff --git a/gcc/testsuite/g++.dg/cpp2a/inline-asm3.C b/gcc/testsuite/g++.dg/cpp2a/inline-asm3.C
index a7476b1d9d1..a6f612e2447 100644
--- a/gcc/testsuite/g++.dg/cpp2a/inline-asm3.C
+++ b/gcc/testsuite/g++.dg/cpp2a/inline-asm3.C
@@ -9,4 +9,4 @@ foo ()
  return i;
 }
 
-constexpr int i = foo ();
+constexpr int i = foo ();	// { dg-error "foo" }

                 reply	other threads:[~2022-09-29 17:52 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=20220929175231.80BE03854164@sourceware.org \
    --to=jason@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).