]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: non-static member, array bound, sizeof [PR93314]
authorJason Merrill <jason@redhat.com>
Wed, 14 Apr 2021 13:30:05 +0000 (09:30 -0400)
committerJason Merrill <jason@redhat.com>
Wed, 14 Apr 2021 17:47:46 +0000 (13:47 -0400)
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.

gcc/cp/semantics.c
gcc/testsuite/g++.dg/parse/uneval1.C [new file with mode: 0644]

index 125772238d3cb7681262c8b096d21e8e5c1479e3..4520181d4e594e566dbcb038b49102875991cc6d 100644 (file)
@@ -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 (file)
index 0000000..dfc1bb4
--- /dev/null
@@ -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" }
+}