* [pushed] c++: non-static member, array bound, sizeof [PR93314]
@ 2021-04-14 17:48 Jason Merrill
2021-04-29 18:30 ` [pushed] c++: constant expressions are evaluated [PR93314] Jason Merrill
0 siblings, 1 reply; 2+ messages in thread
From: Jason Merrill @ 2021-04-14 17:48 UTC (permalink / raw)
To: gcc-patches
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.
Tested x86_64-pc-linux-gnu, applying to trunk.
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 | 10 ++++++++++
gcc/testsuite/g++.dg/parse/uneval1.C | 14 ++++++++++++++
2 files changed, 24 insertions(+)
create mode 100644 gcc/testsuite/g++.dg/parse/uneval1.C
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" }
+}
base-commit: f99f64f69db49ce6343d79a39eab28dcc6b91865
--
2.27.0
^ permalink raw reply [flat|nested] 2+ messages in thread
* [pushed] c++: constant expressions are evaluated [PR93314]
2021-04-14 17:48 [pushed] c++: non-static member, array bound, sizeof [PR93314] Jason Merrill
@ 2021-04-29 18:30 ` Jason Merrill
0 siblings, 0 replies; 2+ messages in thread
From: Jason Merrill @ 2021-04-29 18:30 UTC (permalink / raw)
To: gcc-patches
My GCC 11 patch for PR93314 turned off cp_unevaluated_operand while
processing an id-expression that names a non-static data member, but the
broader issue is that in general, a constant-expression is evaluated even in
an unevaluated operand.
Tested x86_64-pc-linux-gnu, applying to trunk.
gcc/cp/ChangeLog:
* cp-tree.h (cp_evaluated): Add reset parm to constructor.
* parser.c (cp_parser_constant_expression): Change
allow_non_constant_p to int. Use cp_evaluated.
(cp_parser_initializer_clause): Pass 2 to allow_non_constant_p.
* semantics.c (finish_id_expression_1): Don't mess with
cp_unevaluated_operand here.
---
gcc/cp/cp-tree.h | 5 +++--
gcc/cp/parser.c | 15 +++++++++++----
gcc/cp/semantics.c | 10 ----------
3 files changed, 14 insertions(+), 16 deletions(-)
diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h
index e80902a2139..d3639e33460 100644
--- a/gcc/cp/cp-tree.h
+++ b/gcc/cp/cp-tree.h
@@ -5482,9 +5482,10 @@ class cp_evaluated
public:
int uneval;
int inhibit;
- cp_evaluated ()
+ cp_evaluated (bool reset = true)
: uneval(cp_unevaluated_operand), inhibit(c_inhibit_evaluation_warnings)
- { cp_unevaluated_operand = c_inhibit_evaluation_warnings = 0; }
+ { if (reset)
+ cp_unevaluated_operand = c_inhibit_evaluation_warnings = 0; }
~cp_evaluated ()
{ cp_unevaluated_operand = uneval;
c_inhibit_evaluation_warnings = inhibit; }
diff --git a/gcc/cp/parser.c b/gcc/cp/parser.c
index e1b1617da68..9603a1ef594 100644
--- a/gcc/cp/parser.c
+++ b/gcc/cp/parser.c
@@ -2163,7 +2163,7 @@ static enum tree_code cp_parser_assignment_operator_opt
static cp_expr cp_parser_expression
(cp_parser *, cp_id_kind * = NULL, bool = false, bool = false, bool = false);
static cp_expr cp_parser_constant_expression
- (cp_parser *, bool = false, bool * = NULL, bool = false);
+ (cp_parser *, int = 0, bool * = NULL, bool = false);
static cp_expr cp_parser_builtin_offsetof
(cp_parser *);
static cp_expr cp_parser_lambda_expression
@@ -10374,13 +10374,15 @@ cp_parser_expression (cp_parser* parser, cp_id_kind * pidk,
If ALLOW_NON_CONSTANT_P a non-constant expression is silently
accepted. If ALLOW_NON_CONSTANT_P is true and the expression is not
constant, *NON_CONSTANT_P is set to TRUE. If ALLOW_NON_CONSTANT_P
- is false, NON_CONSTANT_P should be NULL. If STRICT_P is true,
+ is false, NON_CONSTANT_P should be NULL. If ALLOW_NON_CONSTANT_P is
+ greater than 1, this isn't really a constant-expression, only a
+ potentially constant-evaluated expression. If STRICT_P is true,
only parse a conditional-expression, otherwise parse an
assignment-expression. See below for rationale. */
static cp_expr
cp_parser_constant_expression (cp_parser* parser,
- bool allow_non_constant_p,
+ int allow_non_constant_p,
bool *non_constant_p,
bool strict_p)
{
@@ -10416,6 +10418,11 @@ cp_parser_constant_expression (cp_parser* parser,
parser->allow_non_integral_constant_expression_p
= (allow_non_constant_p || cxx_dialect >= cxx11);
parser->non_integral_constant_expression_p = false;
+
+ /* A manifestly constant-evaluated expression is evaluated even in an
+ unevaluated operand. */
+ cp_evaluated ev (/*reset if*/allow_non_constant_p <= 1);
+
/* Although the grammar says "conditional-expression", when not STRICT_P,
we parse an "assignment-expression", which also permits
"throw-expression" and the use of assignment operators. In the case
@@ -24239,7 +24246,7 @@ cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
{
initializer
= cp_parser_constant_expression (parser,
- /*allow_non_constant_p=*/true,
+ /*allow_non_constant_p=*/2,
non_constant_p);
}
else
diff --git a/gcc/cp/semantics.c b/gcc/cp/semantics.c
index 319a3a816ed..6224f49f189 100644
--- a/gcc/cp/semantics.c
+++ b/gcc/cp/semantics.c
@@ -4097,12 +4097,6 @@ 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,
@@ -4116,10 +4110,6 @@ 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. */
base-commit: 449d7b40f6f6be8d7f9aa7232c73b0371f0963bf
prerequisite-patch-id: 39f358d55a4ba496b673d13e00215152266caa54
--
2.27.0
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2021-04-29 18:31 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-04-14 17:48 [pushed] c++: non-static member, array bound, sizeof [PR93314] Jason Merrill
2021-04-29 18:30 ` [pushed] c++: constant expressions are evaluated [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).