public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/66618] New: Failure to diagnose non-constant initializer for static object with -O1
@ 2015-06-21 21:09 Keith.S.Thompson at gmail dot com
  2015-06-22 12:58 ` [Bug c/66618] " mpolacek at gcc dot gnu.org
                   ` (6 more replies)
  0 siblings, 7 replies; 8+ messages in thread
From: Keith.S.Thompson at gmail dot com @ 2015-06-21 21:09 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66618

            Bug ID: 66618
           Summary: Failure to diagnose non-constant initializer for
                    static object with -O1
           Product: gcc
           Version: 4.8.4
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: Keith.S.Thompson at gmail dot com
  Target Milestone: ---

$ uname -a
Linux m5 3.13.0-37-generic #64-Ubuntu SMP Mon Sep 22 21:28:38 UTC 2014 x86_64
x86_64 x86_64 GNU/Linux
$ gcc --version | head -n 1
gcc (Ubuntu 4.8.4-2ubuntu1~14.04) 4.8.4
$ cat bug.c
int main(void) {
    const int not_a_constant = 0;
    static int n = not_a_constant; /* constraint violation */
    return n;                      /* avoid unused variable warning */
}
$ gcc -O0 -c -std=c11 -pedantic-errors -Wall -Wextra bug.c
bug.c: In function ‘main’:
bug.c:3:5: error: initializer element is not constant
     static int n = not_a_constant; /* constraint violation */
     ^
$ gcc -O1 -c -std=c11 -pedantic-errors -Wall -Wextra bug.c
$ 

In the test program "bug.c", the object "not_a_constant" is defined as "const",
but its name is not a constant expression. The initializer for a static
expression must be a constant expression; using a non-constant expression is a
constraint violation, requiring a diagnostic (N1570 6.7.9p4).

With "-O0", gcc correctly diagnoses this. I see the same problem with "-O1",
"-O2", and "-O3", and with "-std=c90", "-std=c99", and "-std=c11".

This is based on a question posted to Stack Overflow:
http://stackoverflow.com/q/30962512/827263
by user "meet". Grzegorz Szpetkowski gets the credit for realizing that the
"-O" option triggers the bug.
>From gcc-bugs-return-489531-listarch-gcc-bugs=gcc.gnu.org@gcc.gnu.org Sun Jun 21 21:10:49 2015
Return-Path: <gcc-bugs-return-489531-listarch-gcc-bugs=gcc.gnu.org@gcc.gnu.org>
Delivered-To: listarch-gcc-bugs@gcc.gnu.org
Received: (qmail 109229 invoked by alias); 21 Jun 2015 21:10:49 -0000
Mailing-List: contact gcc-bugs-help@gcc.gnu.org; run by ezmlm
Precedence: bulk
List-Id: <gcc-bugs.gcc.gnu.org>
List-Archive: <http://gcc.gnu.org/ml/gcc-bugs/>
List-Post: <mailto:gcc-bugs@gcc.gnu.org>
List-Help: <mailto:gcc-bugs-help@gcc.gnu.org>
Sender: gcc-bugs-owner@gcc.gnu.org
Delivered-To: mailing list gcc-bugs@gcc.gnu.org
Received: (qmail 109198 invoked by uid 48); 21 Jun 2015 21:10:45 -0000
From: "tkoenig at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug fortran/66578] [F2008] Invalid free on allocate(...,source=a(:)) in block
Date: Sun, 21 Jun 2015 21:10:00 -0000
X-Bugzilla-Reason: CC
X-Bugzilla-Type: changed
X-Bugzilla-Watch-Reason: None
X-Bugzilla-Product: gcc
X-Bugzilla-Component: fortran
X-Bugzilla-Version: 6.0
X-Bugzilla-Keywords: wrong-code
X-Bugzilla-Severity: normal
X-Bugzilla-Who: tkoenig at gcc dot gnu.org
X-Bugzilla-Status: ASSIGNED
X-Bugzilla-Resolution:
X-Bugzilla-Priority: P3
X-Bugzilla-Assigned-To: vehre at gcc dot gnu.org
X-Bugzilla-Target-Milestone: ---
X-Bugzilla-Flags:
X-Bugzilla-Changed-Fields:
Message-ID: <bug-66578-4-qWgmQAvgpn@http.gcc.gnu.org/bugzilla/>
In-Reply-To: <bug-66578-4@http.gcc.gnu.org/bugzilla/>
References: <bug-66578-4@http.gcc.gnu.org/bugzilla/>
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: 7bit
X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/
Auto-Submitted: auto-generated
MIME-Version: 1.0
X-SW-Source: 2015-06/txt/msg01863.txt.bz2
Content-length: 357

https://gcc.gnu.org/bugzilla/show_bug.cgi?idf578

--- Comment #15 from Thomas Koenig <tkoenig at gcc dot gnu.org> ---
Is this something which could also have caused wrong-code issues in
other circumstances, which we just didn't happen to find until
this was exposed by Andre's patch and the test case?

In other words, should we backport Mikael's patch?


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug c/66618] Failure to diagnose non-constant initializer for static object with -O1
  2015-06-21 21:09 [Bug c/66618] New: Failure to diagnose non-constant initializer for static object with -O1 Keith.S.Thompson at gmail dot com
@ 2015-06-22 12:58 ` mpolacek at gcc dot gnu.org
  2015-06-22 13:46 ` joseph at codesourcery dot com
                   ` (5 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2015-06-22 12:58 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66618

Marek Polacek <mpolacek at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2015-06-22
                 CC|                            |mpolacek at gcc dot gnu.org
     Ever confirmed|0                           |1

--- Comment #1 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
Confirmed.  What's going on here is that digest_init has
 6630   inside_init = c_fully_fold (inside_init, require_constant,
&maybe_const);
 6631   inside_init = decl_constant_value_for_optimization (inside_init);
and that decl_constant_value_for_optimization when optimizing is able to turn a
"const int" into an integer constant.  And an integer constant is fine as an
initializer thus we don't error.


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug c/66618] Failure to diagnose non-constant initializer for static object with -O1
  2015-06-21 21:09 [Bug c/66618] New: Failure to diagnose non-constant initializer for static object with -O1 Keith.S.Thompson at gmail dot com
  2015-06-22 12:58 ` [Bug c/66618] " mpolacek at gcc dot gnu.org
@ 2015-06-22 13:46 ` joseph at codesourcery dot com
  2015-07-01  0:48 ` Keith.S.Thompson at gmail dot com
                   ` (4 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: joseph at codesourcery dot com @ 2015-06-22 13:46 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66618

--- Comment #2 from joseph at codesourcery dot com <joseph at codesourcery dot com> ---
Although diagnosing this probably makes sense, it's not required by the 
standard ("An implementation may accept other forms of constant 
expressions." - and this expression doesn't contain "assignment, 
increment, decrement, function-call, or comma operators", so isn't 
required by the Constraints for constant expressions not to be one).


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug c/66618] Failure to diagnose non-constant initializer for static object with -O1
  2015-06-21 21:09 [Bug c/66618] New: Failure to diagnose non-constant initializer for static object with -O1 Keith.S.Thompson at gmail dot com
  2015-06-22 12:58 ` [Bug c/66618] " mpolacek at gcc dot gnu.org
  2015-06-22 13:46 ` joseph at codesourcery dot com
@ 2015-07-01  0:48 ` Keith.S.Thompson at gmail dot com
  2023-05-22 18:57 ` alexhenrie24 at gmail dot com
                   ` (3 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: Keith.S.Thompson at gmail dot com @ 2015-07-01  0:48 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66618

--- Comment #3 from Keith Thompson <Keith.S.Thompson at gmail dot com> ---
It would be easier to argue that gcc accepts "other forms of constant 
expressions" if (a) those other forms were documented and (b) they were
accepted at all optimization levels.

Admittedly the standard doesn't seem to require such documentation (which I
find a little surprising), and you could argue that "gcc -O0" and "gcc -O1" are
two different implementations.


^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug c/66618] Failure to diagnose non-constant initializer for static object with -O1
  2015-06-21 21:09 [Bug c/66618] New: Failure to diagnose non-constant initializer for static object with -O1 Keith.S.Thompson at gmail dot com
                   ` (2 preceding siblings ...)
  2015-07-01  0:48 ` Keith.S.Thompson at gmail dot com
@ 2023-05-22 18:57 ` alexhenrie24 at gmail dot com
  2023-05-22 19:04 ` pinskia at gcc dot gnu.org
                   ` (2 subsequent siblings)
  6 siblings, 0 replies; 8+ messages in thread
From: alexhenrie24 at gmail dot com @ 2023-05-22 18:57 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66618

Alex Henrie <alexhenrie24 at gmail dot com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |alexhenrie24 at gmail dot com

--- Comment #6 from Alex Henrie <alexhenrie24 at gmail dot com> ---
This wasn't fixed properly, or it was broken again before the release of GCC
8.1: In GCC 8.1 and later no warning at all is issued for the example program,
even at -O0.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug c/66618] Failure to diagnose non-constant initializer for static object with -O1
  2015-06-21 21:09 [Bug c/66618] New: Failure to diagnose non-constant initializer for static object with -O1 Keith.S.Thompson at gmail dot com
                   ` (3 preceding siblings ...)
  2023-05-22 18:57 ` alexhenrie24 at gmail dot com
@ 2023-05-22 19:04 ` pinskia at gcc dot gnu.org
  2023-05-22 19:04 ` pinskia at gcc dot gnu.org
  2023-05-22 19:11 ` alexhenrie24 at gmail dot com
  6 siblings, 0 replies; 8+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-05-22 19:04 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66618

--- Comment #7 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to Alex Henrie from comment #6)
> This wasn't fixed properly, or it was broken again before the release of GCC
> 8.1: In GCC 8.1 and later no warning at all is issued for the example
> program, even at -O0.

The fix was to change it to be a valid constant expression ....

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug c/66618] Failure to diagnose non-constant initializer for static object with -O1
  2015-06-21 21:09 [Bug c/66618] New: Failure to diagnose non-constant initializer for static object with -O1 Keith.S.Thompson at gmail dot com
                   ` (4 preceding siblings ...)
  2023-05-22 19:04 ` pinskia at gcc dot gnu.org
@ 2023-05-22 19:04 ` pinskia at gcc dot gnu.org
  2023-05-22 19:11 ` alexhenrie24 at gmail dot com
  6 siblings, 0 replies; 8+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-05-22 19:04 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66618

--- Comment #8 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
(In reply to Andrew Pinski from comment #7)
> (In reply to Alex Henrie from comment #6)
> > This wasn't fixed properly, or it was broken again before the release of GCC
> > 8.1: In GCC 8.1 and later no warning at all is issued for the example
> > program, even at -O0.
> 
> The fix was to change it to be a valid constant expression ....

At -O0 even.

^ permalink raw reply	[flat|nested] 8+ messages in thread

* [Bug c/66618] Failure to diagnose non-constant initializer for static object with -O1
  2015-06-21 21:09 [Bug c/66618] New: Failure to diagnose non-constant initializer for static object with -O1 Keith.S.Thompson at gmail dot com
                   ` (5 preceding siblings ...)
  2023-05-22 19:04 ` pinskia at gcc dot gnu.org
@ 2023-05-22 19:11 ` alexhenrie24 at gmail dot com
  6 siblings, 0 replies; 8+ messages in thread
From: alexhenrie24 at gmail dot com @ 2023-05-22 19:11 UTC (permalink / raw)
  To: gcc-bugs

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=66618

--- Comment #9 from Alex Henrie <alexhenrie24 at gmail dot com> ---
(In reply to Andrew Pinski from comment #7)
> (In reply to Alex Henrie from comment #6)
> > This wasn't fixed properly, or it was broken again before the release of GCC
> > 8.1: In GCC 8.1 and later no warning at all is issued for the example
> > program, even at -O0.
> 
> The fix was to change it to be a valid constant expression ....
Oh, okay. When I read Comment 2 I thought that a -Wpedantic warning was
intended. If the intention was to have no warning ever then everything is fine.

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2023-05-22 19:11 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-06-21 21:09 [Bug c/66618] New: Failure to diagnose non-constant initializer for static object with -O1 Keith.S.Thompson at gmail dot com
2015-06-22 12:58 ` [Bug c/66618] " mpolacek at gcc dot gnu.org
2015-06-22 13:46 ` joseph at codesourcery dot com
2015-07-01  0:48 ` Keith.S.Thompson at gmail dot com
2023-05-22 18:57 ` alexhenrie24 at gmail dot com
2023-05-22 19:04 ` pinskia at gcc dot gnu.org
2023-05-22 19:04 ` pinskia at gcc dot gnu.org
2023-05-22 19:11 ` alexhenrie24 at gmail dot com

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