public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* C++ PATCH to fix static init with () in a template (PR c++/84582)
@ 2018-02-27 19:13 Marek Polacek
  2018-02-27 21:16 ` Jason Merrill
  0 siblings, 1 reply; 12+ messages in thread
From: Marek Polacek @ 2018-02-27 19:13 UTC (permalink / raw)
  To: GCC Patches, Jason Merrill

My recent change introducing cxx_constant_init caused this code

template <class> class A {
  static const long b = 0;
  static const unsigned c = (b);
};

to be rejected.  The reason is that force_paren_expr turns "b" into "*(const
long int &) &b", where the former is not value-dependent but the latter is
value-dependent.  So when we get to maybe_constant_init_1:
5147   if (!is_nondependent_static_init_expression (t))
5148     /* Don't try to evaluate it.  */;
it's not evaluated and we get the non-constant initialization error.
(Before we'd always evaluated the expression.)

Bootstrapped/regtested on x86_64-linux, ok for trunk?

2018-02-27  Marek Polacek  <polacek@redhat.com>

	PR c++/84582
	* semantics.c (force_paren_expr): Avoid creating a static cast
	when processing a template.

	* g++.dg/cpp1z/static1.C: New test.
	* g++.dg/template/static37.C: New test.

diff --git gcc/cp/semantics.c gcc/cp/semantics.c
index 35569d0cb0d..b48de2df4e2 100644
--- gcc/cp/semantics.c
+++ gcc/cp/semantics.c
@@ -1697,7 +1697,7 @@ force_paren_expr (tree expr)
     expr = build1 (PAREN_EXPR, TREE_TYPE (expr), expr);
   else if (VAR_P (expr) && DECL_HARD_REGISTER (expr))
     /* We can't bind a hard register variable to a reference.  */;
-  else
+  else if (!processing_template_decl)
     {
       cp_lvalue_kind kind = lvalue_kind (expr);
       if ((kind & ~clk_class) != clk_none)
diff --git gcc/testsuite/g++.dg/cpp1z/static1.C gcc/testsuite/g++.dg/cpp1z/static1.C
index e69de29bb2d..cb872997c5a 100644
--- gcc/testsuite/g++.dg/cpp1z/static1.C
+++ gcc/testsuite/g++.dg/cpp1z/static1.C
@@ -0,0 +1,19 @@
+// PR c++/84582
+// { dg-options -std=c++17 }
+
+class C {
+  static inline const long b = 0;
+  static inline const unsigned c = (b);
+};
+class D {
+  static inline const long b = 0;
+  static inline const unsigned c = b;
+};
+template <class> class A {
+  static inline const long b = 0;
+  static inline const unsigned c = (b);
+};
+template <class> class B {
+  static inline const long b = 0;
+  static inline const unsigned c = b;
+};
diff --git gcc/testsuite/g++.dg/template/static37.C gcc/testsuite/g++.dg/template/static37.C
index e69de29bb2d..90bc65d2fbc 100644
--- gcc/testsuite/g++.dg/template/static37.C
+++ gcc/testsuite/g++.dg/template/static37.C
@@ -0,0 +1,18 @@
+// PR c++/84582
+
+class C {
+  static const long b = 0;
+  static const unsigned c = (b);
+};
+class D {
+  static const long b = 0;
+  static const unsigned c = b;
+};
+template <class> class A {
+  static const long b = 0;
+  static const unsigned c = (b);
+};
+template <class> class B {
+  static const long b = 0;
+  static const unsigned c = b;
+};

	Marek

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

end of thread, other threads:[~2018-03-02 18:20 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-02-27 19:13 C++ PATCH to fix static init with () in a template (PR c++/84582) Marek Polacek
2018-02-27 21:16 ` Jason Merrill
2018-02-28 14:32   ` Marek Polacek
2018-02-28 15:51     ` Jason Merrill
2018-02-28 21:19       ` Marek Polacek
2018-02-28 21:51         ` Jason Merrill
2018-03-01 13:17           ` Marek Polacek
2018-03-01 18:57             ` Jason Merrill
2018-03-01 21:40               ` Marek Polacek
2018-03-01 21:57                 ` Jason Merrill
2018-03-02 18:18                   ` Jason Merrill
2018-03-02 18:20                     ` Marek Polacek

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