public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r11-8181] c++: non-static member, array bound, sizeof [PR93314]
@ 2021-04-14 17:48 Jason Merrill
  0 siblings, 0 replies; only message in thread
From: Jason Merrill @ 2021-04-14 17:48 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:9b53edc796d284b6adec7f2996772dbddf4c341e

commit r11-8181-g9b53edc796d284b6adec7f2996772dbddf4c341e
Author: Jason Merrill <jason@redhat.com>
Date:   Wed Apr 14 09:30:05 2021 -0400

    c++: non-static member, array bound, sizeof [PR93314]
    
    N2253 allowed referring to non-static data members without an object in
    unevaluated operands like that of sizeof, but in a constant-expression
    context like an array bound or template argument within such an unevaluated
    operand we do actually need a value, so that permission cannot apply.
    
    gcc/cp/ChangeLog:
    
            PR c++/93314
            * semantics.c (finish_id_expression_1): Clear cp_unevaluated_operand
            for a non-static data member in a constant-expression.
    
    gcc/testsuite/ChangeLog:
    
            PR c++/93314
            * g++.dg/parse/uneval1.C: New test.

Diff:
---
 gcc/cp/semantics.c                   | 10 ++++++++++
 gcc/testsuite/g++.dg/parse/uneval1.C | 14 ++++++++++++++
 2 files changed, 24 insertions(+)

diff --git a/gcc/cp/semantics.c b/gcc/cp/semantics.c
index 125772238d3..4520181d4e5 100644
--- a/gcc/cp/semantics.c
+++ b/gcc/cp/semantics.c
@@ -4093,6 +4093,12 @@ finish_id_expression_1 (tree id_expression,
 
 	  cp_warn_deprecated_use_scopes (scope);
 
+	  /* In a constant-expression context, turn off cp_unevaluated_operand
+	     so finish_non_static_data_member will complain (93314).  */
+	  auto eval = make_temp_override (cp_unevaluated_operand);
+	  if (integral_constant_expression_p && TREE_CODE (decl) == FIELD_DECL)
+	    cp_unevaluated_operand = 0;
+
 	  if (TYPE_P (scope))
 	    decl = finish_qualified_id_expr (scope,
 					     decl,
@@ -4106,6 +4112,10 @@ finish_id_expression_1 (tree id_expression,
 	}
       else if (TREE_CODE (decl) == FIELD_DECL)
 	{
+	  auto eval = make_temp_override (cp_unevaluated_operand);
+	  if (integral_constant_expression_p)
+	    cp_unevaluated_operand = 0;
+
 	  /* Since SCOPE is NULL here, this is an unqualified name.
 	     Access checking has been performed during name lookup
 	     already.  Turn off checking to avoid duplicate errors.  */
diff --git a/gcc/testsuite/g++.dg/parse/uneval1.C b/gcc/testsuite/g++.dg/parse/uneval1.C
new file mode 100644
index 00000000000..dfc1bb4e0c3
--- /dev/null
+++ b/gcc/testsuite/g++.dg/parse/uneval1.C
@@ -0,0 +1,14 @@
+// PR c++/93314
+
+struct S {
+  int m;
+  static int f() {
+    return sizeof(char[m]);	// { dg-error "S::m" }
+  }
+};
+
+int main()
+{
+  return S().f()
+    + sizeof(char[S::m]);	// { dg-error "S::m" }
+}


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2021-04-14 17:48 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-04-14 17:48 [gcc r11-8181] c++: non-static member, array bound, sizeof [PR93314] Jason Merrill

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