* [Bug c++/104507] internal compiler error: unexpected expression ‘(int)(__ret)’ of kind cast_expr
2022-02-11 21:45 [Bug c++/104507] New: internal compiler error: unexpected expression ‘(int)(__ret)’ of kind cast_expr junk at sigpwr dot com
@ 2022-02-11 21:51 ` pinskia at gcc dot gnu.org
2022-02-11 21:53 ` [Bug c++/104507] [10/11 Regression] " pinskia at gcc dot gnu.org
` (11 subsequent siblings)
12 siblings, 0 replies; 14+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-02-11 21:51 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104507
Andrew Pinski <pinskia at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Keywords| |ice-on-valid-code,
| |needs-bisection
--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Looks to be fixed on the trunk.
^ permalink raw reply [flat|nested] 14+ messages in thread
* [Bug c++/104507] [10/11 Regression] internal compiler error: unexpected expression ‘(int)(__ret)’ of kind cast_expr
2022-02-11 21:45 [Bug c++/104507] New: internal compiler error: unexpected expression ‘(int)(__ret)’ of kind cast_expr junk at sigpwr dot com
2022-02-11 21:51 ` [Bug c++/104507] " pinskia at gcc dot gnu.org
@ 2022-02-11 21:53 ` pinskia at gcc dot gnu.org
2022-02-13 8:47 ` junk at sigpwr dot com
` (10 subsequent siblings)
12 siblings, 0 replies; 14+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-02-11 21:53 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104507
Andrew Pinski <pinskia at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Known to work| |10.1.0, 9.3.0, 9.4.0
Summary|internal compiler error: |[10/11 Regression] internal
|unexpected expression |compiler error: unexpected
|‘(int)(__ret)’ of kind |expression ‘(int)(__ret)’
|cast_expr |of kind cast_expr
Known to fail| |10.2.0, 10.3.0, 11.1.0
Target Milestone|--- |10.4
^ permalink raw reply [flat|nested] 14+ messages in thread
* [Bug c++/104507] [10/11 Regression] internal compiler error: unexpected expression ‘(int)(__ret)’ of kind cast_expr
2022-02-11 21:45 [Bug c++/104507] New: internal compiler error: unexpected expression ‘(int)(__ret)’ of kind cast_expr junk at sigpwr dot com
2022-02-11 21:51 ` [Bug c++/104507] " pinskia at gcc dot gnu.org
2022-02-11 21:53 ` [Bug c++/104507] [10/11 Regression] " pinskia at gcc dot gnu.org
@ 2022-02-13 8:47 ` junk at sigpwr dot com
2022-02-13 23:03 ` pinskia at gcc dot gnu.org
` (9 subsequent siblings)
12 siblings, 0 replies; 14+ messages in thread
From: junk at sigpwr dot com @ 2022-02-13 8:47 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104507
--- Comment #2 from Daniel Nelson <junk at sigpwr dot com> ---
Had a few moments to do some testing/bisecting, and found the commit that fixed
this in mainline.
commit 9927ecbb42d5be48fa933adc26f8601fab5007ca
Author: Patrick Palka <ppalka@redhat.com>
Date: Thu Oct 28 10:05:14 2021 -0400
c++: quadratic constexpr behavior for left-assoc logical exprs [PR102780]
In the testcase below the two left fold expressions each expand into a
constant logical expression with 1024 terms, for which potential_const_expr
takes more than a minute to return true. This happens because p_c_e_1
performs trial evaluation of the first operand of a &&/|| in order to
determine whether to consider the potentiality of the second operand.
And because the expanded expression is left-associated, this trial
evaluation causes p_c_e_1 to be quadratic in the number of terms of the
expression.
This patch fixes this quadratic behavior by making p_c_e_1 preemptively
compute potentiality of the second operand of a &&/||, and perform trial
evaluation of the first operand only if the second operand isn't
potentially constant. We must be careful to avoid emitting bogus
diagnostics during the preemptive computation; to that end, we perform
this shortcut only when tf_error is cleared, and when tf_error is set we
now first check potentiality of the whole expression quietly and replay
the check noisily for diagnostics.
Apart from fixing the quadraticness for left-associated logical exprs,
this change also reduces compile time for the libstdc++ testcase
20_util/variant/87619.cc by about 15% even though our <variant> uses
right folds instead of left folds. Likewise for the testcase in the PR,
for which compile time is reduced by 30%. The reason for these speedups
is that p_c_e_1 no longer performs expensive trial evaluation of each term
of large constant logical expressions when determining their potentiality.
PR c++/102780
gcc/cp/ChangeLog:
* constexpr.c (potential_constant_expression_1) <case
TRUTH_*_EXPR>:
When tf_error isn't set, preemptively check potentiality of the
second operand before performing trial evaluation of the first
operand.
(potential_constant_expression_1): When tf_error is set, first
check
potentiality quietly and return true if successful, otherwise
proceed noisily to give errors.
gcc/testsuite/ChangeLog:
* g++.dg/cpp1z/fold13.C: New test.
gcc/cp/constexpr.c | 26 +++++++++++++++++++++-----
gcc/testsuite/g++.dg/cpp1z/fold13.C | 29 +++++++++++++++++++++++++++++
2 files changed, 50 insertions(+), 5 deletions(-)
create mode 100644 gcc/testsuite/g++.dg/cpp1z/fold13.C
^ permalink raw reply [flat|nested] 14+ messages in thread
* [Bug c++/104507] [10/11 Regression] internal compiler error: unexpected expression ‘(int)(__ret)’ of kind cast_expr
2022-02-11 21:45 [Bug c++/104507] New: internal compiler error: unexpected expression ‘(int)(__ret)’ of kind cast_expr junk at sigpwr dot com
` (2 preceding siblings ...)
2022-02-13 8:47 ` junk at sigpwr dot com
@ 2022-02-13 23:03 ` pinskia at gcc dot gnu.org
2022-02-14 0:57 ` junk at sigpwr dot com
` (8 subsequent siblings)
12 siblings, 0 replies; 14+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-02-13 23:03 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104507
Andrew Pinski <pinskia at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
See Also| |https://gcc.gnu.org/bugzill
| |a/show_bug.cgi?id=102780
CC| |ppalka at gcc dot gnu.org
Keywords|needs-bisection |
--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to Daniel Nelson from comment #2)
> Had a few moments to do some testing/bisecting, and found the commit that
> fixed this in mainline.
I kinda of see how that might help here but not really. I wonder if there is
another testcase which might shows the issue on the trunk still.
^ permalink raw reply [flat|nested] 14+ messages in thread
* [Bug c++/104507] [10/11 Regression] internal compiler error: unexpected expression ‘(int)(__ret)’ of kind cast_expr
2022-02-11 21:45 [Bug c++/104507] New: internal compiler error: unexpected expression ‘(int)(__ret)’ of kind cast_expr junk at sigpwr dot com
` (3 preceding siblings ...)
2022-02-13 23:03 ` pinskia at gcc dot gnu.org
@ 2022-02-14 0:57 ` junk at sigpwr dot com
2022-02-14 8:32 ` rguenth at gcc dot gnu.org
` (7 subsequent siblings)
12 siblings, 0 replies; 14+ messages in thread
From: junk at sigpwr dot com @ 2022-02-14 0:57 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104507
--- Comment #4 from Daniel Nelson <junk at sigpwr dot com> ---
Yeah, that was my thought as well (though I'm 99% unfamiliar with the GCC
codebase).
Some more bisecting reveals that this was introduced with
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=95508
ae2ebf011fec926e003645c33c07a03619ea216a is the first bad commit
commit ae2ebf011fec926e003645c33c07a03619ea216a
Author: Marek Polacek <polacek@redhat.com>
Date: Wed Jun 17 09:19:02 2020 -0400
c++: ICE with IMPLICIT_CONV_EXPR in array subscript [PR95508]
...
^ permalink raw reply [flat|nested] 14+ messages in thread
* [Bug c++/104507] [10/11 Regression] internal compiler error: unexpected expression ‘(int)(__ret)’ of kind cast_expr
2022-02-11 21:45 [Bug c++/104507] New: internal compiler error: unexpected expression ‘(int)(__ret)’ of kind cast_expr junk at sigpwr dot com
` (4 preceding siblings ...)
2022-02-14 0:57 ` junk at sigpwr dot com
@ 2022-02-14 8:32 ` rguenth at gcc dot gnu.org
2022-02-14 11:18 ` jakub at gcc dot gnu.org
` (6 subsequent siblings)
12 siblings, 0 replies; 14+ messages in thread
From: rguenth at gcc dot gnu.org @ 2022-02-14 8:32 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104507
Richard Biener <rguenth at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Priority|P3 |P2
^ permalink raw reply [flat|nested] 14+ messages in thread
* [Bug c++/104507] [10/11 Regression] internal compiler error: unexpected expression ‘(int)(__ret)’ of kind cast_expr
2022-02-11 21:45 [Bug c++/104507] New: internal compiler error: unexpected expression ‘(int)(__ret)’ of kind cast_expr junk at sigpwr dot com
` (5 preceding siblings ...)
2022-02-14 8:32 ` rguenth at gcc dot gnu.org
@ 2022-02-14 11:18 ` jakub at gcc dot gnu.org
2022-02-14 16:57 ` [Bug c++/104507] [10/11/12 " ppalka at gcc dot gnu.org
` (5 subsequent siblings)
12 siblings, 0 replies; 14+ messages in thread
From: jakub at gcc dot gnu.org @ 2022-02-14 11:18 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104507
Jakub Jelinek <jakub at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
CC| |jakub at gcc dot gnu.org
--- Comment #5 from Jakub Jelinek <jakub at gcc dot gnu.org> ---
Yeah, I confirm it doesn't ICE starting with
r12-4769-g9927ecbb42d5be48fa933adc26f8601fab5007ca
and ICE started with r11-1449-gae2ebf011fec926e003645c33c07a03619ea216a
which has been backported to 10 in
r10-8315-g1bab254fd30c2b94a675b9057349fc80946375b1
^ permalink raw reply [flat|nested] 14+ messages in thread
* [Bug c++/104507] [10/11/12 Regression] internal compiler error: unexpected expression ‘(int)(__ret)’ of kind cast_expr
2022-02-11 21:45 [Bug c++/104507] New: internal compiler error: unexpected expression ‘(int)(__ret)’ of kind cast_expr junk at sigpwr dot com
` (6 preceding siblings ...)
2022-02-14 11:18 ` jakub at gcc dot gnu.org
@ 2022-02-14 16:57 ` ppalka at gcc dot gnu.org
2022-02-15 16:17 ` ppalka at gcc dot gnu.org
` (4 subsequent siblings)
12 siblings, 0 replies; 14+ messages in thread
From: ppalka at gcc dot gnu.org @ 2022-02-14 16:57 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104507
Patrick Palka <ppalka at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Ever confirmed|0 |1
Summary|[10/11 Regression] internal |[10/11/12 Regression]
|compiler error: unexpected |internal compiler error:
|expression ‘(int)(__ret)’ |unexpected expression
|of kind cast_expr |‘(int)(__ret)’ of kind
| |cast_expr
Status|UNCONFIRMED |NEW
Last reconfirmed| |2022-02-14
Known to fail| |12.0
--- Comment #6 from Patrick Palka <ppalka at gcc dot gnu.org> ---
(In reply to Andrew Pinski from comment #3)
> (In reply to Daniel Nelson from comment #2)
> > Had a few moments to do some testing/bisecting, and found the commit that
> > fixed this in mainline.
>
> I kinda of see how that might help here but not really. I wonder if there is
> another testcase which might shows the issue on the trunk still.
trunk crashes on this version:
#define ERR_MAX -20
#define s_errmsg(r) \
_k_errmsg[(((int)r) <= 0 && r) ? -(r) : -ERR_MAX]
extern const char *_k_errmsg[];
template <int size>
inline int DoFoo() {
int __ret = 0;
const char *n = s_errmsg(__ret);
return __ret;
}
^ permalink raw reply [flat|nested] 14+ messages in thread
* [Bug c++/104507] [10/11/12 Regression] internal compiler error: unexpected expression ‘(int)(__ret)’ of kind cast_expr
2022-02-11 21:45 [Bug c++/104507] New: internal compiler error: unexpected expression ‘(int)(__ret)’ of kind cast_expr junk at sigpwr dot com
` (7 preceding siblings ...)
2022-02-14 16:57 ` [Bug c++/104507] [10/11/12 " ppalka at gcc dot gnu.org
@ 2022-02-15 16:17 ` ppalka at gcc dot gnu.org
2022-02-16 17:42 ` cvs-commit at gcc dot gnu.org
` (3 subsequent siblings)
12 siblings, 0 replies; 14+ messages in thread
From: ppalka at gcc dot gnu.org @ 2022-02-15 16:17 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104507
Patrick Palka <ppalka at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Assignee|unassigned at gcc dot gnu.org |ppalka at gcc dot gnu.org
Status|NEW |ASSIGNED
^ permalink raw reply [flat|nested] 14+ messages in thread
* [Bug c++/104507] [10/11/12 Regression] internal compiler error: unexpected expression ‘(int)(__ret)’ of kind cast_expr
2022-02-11 21:45 [Bug c++/104507] New: internal compiler error: unexpected expression ‘(int)(__ret)’ of kind cast_expr junk at sigpwr dot com
` (8 preceding siblings ...)
2022-02-15 16:17 ` ppalka at gcc dot gnu.org
@ 2022-02-16 17:42 ` cvs-commit at gcc dot gnu.org
2022-02-16 17:43 ` [Bug c++/104507] [10/11 " ppalka at gcc dot gnu.org
` (2 subsequent siblings)
12 siblings, 0 replies; 14+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2022-02-16 17:42 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104507
--- Comment #7 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Patrick Palka <ppalka@gcc.gnu.org>:
https://gcc.gnu.org/g:c19f317a78c0e4c1b51d0e5a8e4c0a3b985b7a8e
commit r12-7264-gc19f317a78c0e4c1b51d0e5a8e4c0a3b985b7a8e
Author: Patrick Palka <ppalka@redhat.com>
Date: Wed Feb 16 12:41:35 2022 -0500
c++: treat NON_DEPENDENT_EXPR as not potentially constant [PR104507]
Here we're crashing from potential_constant_expression because it tries
to perform trial evaluation of the first operand '(bool)__r' of the
conjunction (which is overall wrapped in a NON_DEPENDENT_EXPR), but
cxx_eval_constant_expression ICEs on unsupported trees (of which CAST_EXPR
is one). The sequence of events is:
1. build_non_dependent_expr for the array subscript yields
NON_DEPENDENT_EXPR<<<(bool)__r && __s>>> ? 1 : 2
2. cp_build_array_ref calls fold_non_dependent_expr on this subscript
(after this point, processing_template_decl is cleared)
3. during which, the COND_EXPR case of tsubst_copy_and_build calls
fold_non_dependent_expr on the first operand
4. during which, we crash from p_c_e_1 because it attempts trial
evaluation of the CAST_EXPR '(bool)__r'.
Note that even if this crash didn't happen, fold_non_dependent_expr
from cp_build_array_ref would still ultimately be one big no-op here
since neither constexpr evaluation nor tsubst handle NON_DEPENDENT_EXPR.
In light of this and of the observation that we should never see
NON_DEPENDENT_EXPR in a context where a constant expression is needed
(it's used primarily in the build_x_* family of functions), it seems
futile for p_c_e_1 to ever return true for NON_DEPENDENT_EXPR. And the
otherwise inconsistent handling of NON_DEPENDENT_EXPR between p_c_e_1,
cxx_evaluate_constexpr_expression and tsubst apparently leads to weird
bugs such as this one.
PR c++/104507
gcc/cp/ChangeLog:
* constexpr.cc (potential_constant_expression_1)
<case NON_DEPENDENT_EXPR>: Return false instead of recursing.
Assert tf_error isn't set.
gcc/testsuite/ChangeLog:
* g++.dg/template/non-dependent21.C: New test.
^ permalink raw reply [flat|nested] 14+ messages in thread
* [Bug c++/104507] [10/11 Regression] internal compiler error: unexpected expression ‘(int)(__ret)’ of kind cast_expr
2022-02-11 21:45 [Bug c++/104507] New: internal compiler error: unexpected expression ‘(int)(__ret)’ of kind cast_expr junk at sigpwr dot com
` (9 preceding siblings ...)
2022-02-16 17:42 ` cvs-commit at gcc dot gnu.org
@ 2022-02-16 17:43 ` ppalka at gcc dot gnu.org
2022-04-12 23:44 ` cvs-commit at gcc dot gnu.org
2022-05-04 0:22 ` [Bug c++/104507] [10 " ppalka at gcc dot gnu.org
12 siblings, 0 replies; 14+ messages in thread
From: ppalka at gcc dot gnu.org @ 2022-02-16 17:43 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104507
Patrick Palka <ppalka at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Summary|[10/11/12 Regression] |[10/11 Regression] internal
|internal compiler error: |compiler error: unexpected
|unexpected expression |expression ‘(int)(__ret)’
|‘(int)(__ret)’ of kind |of kind cast_expr
|cast_expr |
--- Comment #8 from Patrick Palka <ppalka at gcc dot gnu.org> ---
Fixed for GCC 12 so far.
^ permalink raw reply [flat|nested] 14+ messages in thread
* [Bug c++/104507] [10/11 Regression] internal compiler error: unexpected expression ‘(int)(__ret)’ of kind cast_expr
2022-02-11 21:45 [Bug c++/104507] New: internal compiler error: unexpected expression ‘(int)(__ret)’ of kind cast_expr junk at sigpwr dot com
` (10 preceding siblings ...)
2022-02-16 17:43 ` [Bug c++/104507] [10/11 " ppalka at gcc dot gnu.org
@ 2022-04-12 23:44 ` cvs-commit at gcc dot gnu.org
2022-05-04 0:22 ` [Bug c++/104507] [10 " ppalka at gcc dot gnu.org
12 siblings, 0 replies; 14+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2022-04-12 23:44 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104507
--- Comment #9 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The releases/gcc-11 branch has been updated by Patrick Palka
<ppalka@gcc.gnu.org>:
https://gcc.gnu.org/g:c8aaa9cca96207b5674048972c82a338ef81ce7e
commit r11-9843-gc8aaa9cca96207b5674048972c82a338ef81ce7e
Author: Patrick Palka <ppalka@redhat.com>
Date: Wed Feb 16 12:41:35 2022 -0500
c++: treat NON_DEPENDENT_EXPR as not potentially constant [PR104507]
Here we're crashing from potential_constant_expression because it tries
to perform trial evaluation of the first operand '(bool)__r' of the
conjunction (which is overall wrapped in a NON_DEPENDENT_EXPR), but
cxx_eval_constant_expression ICEs on unsupported trees (of which CAST_EXPR
is one). The sequence of events is:
1. build_non_dependent_expr for the array subscript yields
NON_DEPENDENT_EXPR<<<(bool)__r && __s>>> ? 1 : 2
2. cp_build_array_ref calls fold_non_dependent_expr on this subscript
(after this point, processing_template_decl is cleared)
3. during which, the COND_EXPR case of tsubst_copy_and_build calls
fold_non_dependent_expr on the first operand
4. during which, we crash from p_c_e_1 because it attempts trial
evaluation of the CAST_EXPR '(bool)__r'.
Note that even if this crash didn't happen, fold_non_dependent_expr
from cp_build_array_ref would still ultimately be one big no-op here
since neither constexpr evaluation nor tsubst handle NON_DEPENDENT_EXPR.
In light of this and of the observation that we should never see
NON_DEPENDENT_EXPR in a context where a constant expression is needed
(it's used primarily in the build_x_* family of functions), it seems
futile for p_c_e_1 to ever return true for NON_DEPENDENT_EXPR. And the
otherwise inconsistent handling of NON_DEPENDENT_EXPR between p_c_e_1,
cxx_evaluate_constexpr_expression and tsubst apparently leads to weird
bugs such as this one.
PR c++/104507
gcc/cp/ChangeLog:
* constexpr.c (potential_constant_expression_1)
<case NON_DEPENDENT_EXPR>: Return false instead of recursing.
Assert tf_error isn't set.
gcc/testsuite/ChangeLog:
* g++.dg/template/non-dependent21.C: New test.
(cherry picked from commit c19f317a78c0e4c1b51d0e5a8e4c0a3b985b7a8e)
^ permalink raw reply [flat|nested] 14+ messages in thread
* [Bug c++/104507] [10 Regression] internal compiler error: unexpected expression ‘(int)(__ret)’ of kind cast_expr
2022-02-11 21:45 [Bug c++/104507] New: internal compiler error: unexpected expression ‘(int)(__ret)’ of kind cast_expr junk at sigpwr dot com
` (11 preceding siblings ...)
2022-04-12 23:44 ` cvs-commit at gcc dot gnu.org
@ 2022-05-04 0:22 ` ppalka at gcc dot gnu.org
12 siblings, 0 replies; 14+ messages in thread
From: ppalka at gcc dot gnu.org @ 2022-05-04 0:22 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=104507
Patrick Palka <ppalka at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Target Milestone|10.4 |11.3
Status|ASSIGNED |RESOLVED
Resolution|--- |FIXED
--- Comment #10 from Patrick Palka <ppalka at gcc dot gnu.org> ---
Fixed for GCC 11.3/12, thanks for the bug report.
^ permalink raw reply [flat|nested] 14+ messages in thread