Bug 65642 - [C++11] GCC rejects valid constant expression
Summary: [C++11] GCC rejects valid constant expression
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 5.0
: P3 normal
Target Milestone: 12.0
Assignee: Not yet assigned to anyone
URL:
Keywords: rejects-valid
Depends on:
Blocks: constexpr
  Show dependency treegraph
 
Reported: 2015-03-31 17:43 UTC by Mitsuru Kariya
Modified: 2022-07-06 10:49 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2015-04-01 00:00:00


Attachments
pr65642.patch (926 bytes, patch)
2015-04-02 13:56 UTC, Marek Polacek
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Mitsuru Kariya 2015-03-31 17:43:06 UTC

    
Comment 1 Mitsuru Kariya 2015-03-31 17:51:19 UTC
Please see the sample codes below.

========================== sample code 1 ==========================
constexpr char f(const char* s)
{
        return *(s + 1);
}

constexpr char s[] = "ss";

constexpr auto c = f(&s[0]);

int main() {}
========================== sample code 1 ==========================
========================= error message 1 =========================
sample1.cpp:8:21:   in constexpr expansion of 'f((& s[0]))'
sample1.cpp:8:27: error: '*(s + 1u)' is not a constant expression
 constexpr auto c = f(&s[0]);
                           ^
========================= error message 1 =========================

========================== sample code 2 ==========================
constexpr bool g(char const* found, char const* last) {
        return found == last;
}

constexpr bool f(const char* s)
{
        return g(s, s + 1);
}

constexpr char s[] = "ss";

constexpr auto b = f(&s[0]);

int main() {}
========================== sample code 2 ==========================
========================= error message 2 =========================
sample2.cpp:2:15: error: '((& s[0]) == ((& s[0]) + 1u))' is not a constant expression
  return found == last;
               ^
========================= error message 2 =========================

========================== sample code 3 ==========================
constexpr char s1[] = "s1";
constexpr char s2[] = "s2";

constexpr auto eq = (s1 + 0) == (s2 + 1);

int main() {}
========================== sample code 3 ==========================
========================= error message 3 =========================
sample3.cpp:4:30: error: '(((const char*)(& s1)) == (((const char*)(& s2)) + 1u))' is not a constant expression
 constexpr auto eq = (s1 + 0) == (s2 + 1);
                              ^
========================= error message 3 =========================

The sample code 1 and 2 are compiled successfully on the 4.9.2, but they cause an error on the head revision.
The sample code 3 causes an error on the 4.9.2 and the head revision.

These testcases are derived from bug 65398.
Comment 2 Marek Polacek 2015-04-01 11:00:44 UTC
I'll take a look.
Comment 3 Marek Polacek 2015-04-02 13:56:40 UTC
Created attachment 35214 [details]
pr65642.patch

Untested patch for example 1 and 3.
Comment 4 Marek Polacek 2015-04-02 14:09:35 UTC
(In reply to Marek Polacek from comment #3)
> Created attachment 35214 [details]
> pr65642.patch
> 
> Untested patch for example 1 and 3.

1 and 2.  3 is different.
Comment 5 Marek Polacek 2015-04-02 16:51:59 UTC
Author: mpolacek
Date: Thu Apr  2 16:51:27 2015
New Revision: 221843

URL: https://gcc.gnu.org/viewcvs?rev=221843&root=gcc&view=rev
Log:
	PR c++/65642
	* constexpr.c (cxx_eval_pointer_plus_expression): Call
	cxx_eval_constant_expression on the first operand.

	* g++.dg/cpp0x/constexpr-fold1.C: New test.
	* g++.dg/cpp0x/constexpr-fold2.C: New test.

Added:
    trunk/gcc/testsuite/g++.dg/cpp0x/constexpr-fold1.C
    trunk/gcc/testsuite/g++.dg/cpp0x/constexpr-fold2.C
Modified:
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/constexpr.c
    trunk/gcc/testsuite/ChangeLog
Comment 6 Marek Polacek 2015-04-07 17:19:19 UTC
I think that fixing the third case is going to be harder, and doesn't look like something that should be fixed in cp/constexpr.c.

While fold_comparison is able to fold the NE_EXPR here:
constexpr char s1[] = "s1";
constexpr char s2[] = "s2";
constexpr auto eq = (s2 + 2) != (s2 + 1);

it isn't able to fold it here:
constexpr char s1[] = "s1";
constexpr char s2[] = "s2";
constexpr auto eq = (s1 + 2) != (s2 + 1);

So not sure what to do here, but in any case, I think it is a GCC 6 material.
Comment 7 Jakub Jelinek 2016-04-27 10:56:18 UTC
GCC 6.1 has been released.
Comment 8 Jakub Jelinek 2016-12-21 10:55:43 UTC
GCC 6.3 is being released, adjusting target milestone.
Comment 9 Richard Biener 2017-07-04 08:44:32 UTC
GCC 6.4 is being released, adjusting target milestone.
Comment 10 Martin Liška 2018-11-19 13:38:02 UTC
Can the bug be marked as resolved?
Comment 11 Andrew Pinski 2021-12-20 07:24:20 UTC
I think the last example is similar to bug 89074 comment #5.
Comment 12 Jonathan Wakely 2022-07-06 10:49:05 UTC
(In reply to Martin Liška from comment #10)
> Can the bug be marked as resolved?

At the time you asked, no, because the third example still failed.

But it compiles since r12-6382 (PR89074, as Andrew suggested).

So now it can be closed as fixed in GCC 12.