Bug 6905 - Suggested g++ warning: changed semantics of conditional expressions
Summary: Suggested g++ warning: changed semantics of conditional expressions
Status: RESOLVED WONTFIX
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 3.1
: P3 enhancement
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic
Depends on:
Blocks:
 
Reported: 2002-06-01 15:46 UTC by 33975
Modified: 2017-02-28 00:27 UTC (History)
3 users (show)

See Also:
Host: i386-linux
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2005-12-11 22:55:33


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description 33975 2002-06-01 15:46:01 UTC
[ Reported to the Debian BTS as report #33975.
  Please CC 33975@bugs.debian.org on replies.
  Log of report can be found at http://bugs.debian.org/33975 ]
	

This is a suggestion for a warning which would greatly help those
converting old C++ programs to g++ and ANSI/ISO standard C++, by
helping to detect code which gets a clean compile from a strict
compiler, but whose behaviour is different at run time as a result of
a change in defined semantics imposed by the standard. 

An example which g++ already handles very well is the change in the
scope of variables declared in a for-init-statement causes g++ to
issue clear warnings if an ambiguous case is detected (see the
documentation for the `-ffor-scope' option).

This case is similar, except that g++ has enforced the now standard
interpretation since at least gcc 2.7.2 - namely the change in the
scope of a conditional expression with respect to a trailing
assignment.  This change means that, for example, an expression such
as (a ? b : c = d) which originally meant ((a ? b : c) = d) will now
be interpreted to mean (a ? b : (c = d)) instead.

I suggest that expressions like this, when written without parentheses
to assert the author's intent, should trigger a warning if the option
`-Wparentheses' is active (whether or not `-ansi' was specified),
since it seems to match the description `when operators are nested
whose precedence people often get confused about'. Alternatively,
since the erroneous interpretation is an anachronism, perhaps
`-Wtraditional' would be the appropriate option.


An example program follows, with output. I'm using

   gcc version gcc-3.1

on Debian 3.0.

Thanks,

Patrick

// --------------------------------------------------------------------
#include <stdio.h>
int main (void)
{
    int x = 0;
    if (x = 3) // this assignment will raise a parentheses warning...
        ;

    // ...but this dubious conditional expression will not do so
    int a = 1, b = 2, c = 3, d = 4;
    x = (a ? b : c = d);

    // just to prove g++ obeys the standard:
    printf ("(a ? b : c = d) means %s\n", 
            ((b != 4) ? 
             "(a ? b : (c = d))" :   // ansi
             "((a ? b : c) = d)"));  // ancient
    exit(0);
}
// --------------------------------------------------------------------


$ g++.3,1 -Wparentheses a.cc && ./a.out
a.cc: In function `int main()':
a.cc:5: warning: suggest parentheses around assignment used as truth value
(a ? b : c = d) means (a ? b : (c = d))

Release:
3.1 (Debian) (Debian unstable)

Environment:
System: Debian GNU/Linux (unstable)
Architecture: i686
	
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Installed/Config-files/Unpacked/Failed-config/Half-installed
|/ Err?=(none)/Hold/Reinst-required/X=both-problems (Status,Err: uppercase=bad)
||/ Name           Version        Description
+++-==============-==============-============================================
ii  gcc-3.1        3.1-2          The GNU C compiler.
ii  g++-3.1        3.1-2          The GNU C++ compiler.
ii  libstdc++4     3.1-2          The GNU stdc++ library version 3
ii  libstdc++4-dev 3.1-2          The GNU stdc++ library version 3 (developmen
ii  binutils       2.12.90.0.7-1  The GNU assembler, linker and binary utiliti
ii  libc6          2.2.5-6        GNU C Library: Shared libraries and Timezone
host: i386-linux
configured with: /mnt/data/gcc-3.1/gcc-3.1-3.1ds2/src/configure -v --enable-languages=c,c++,java,f77,proto,objc,ada --prefix=/usr --mandir=$\(prefix\)/share/man --infodir=$\(prefix\)/share/info --with-gxx-include-dir=$\(prefix\)/include/g++-v3-3.1 --enable-shared --with-system-zlib --enable-long-long --enable-nls --without-included-gettext --enable-clocale=gnu --enable-threads=posix --enable-java-gc=boehm --enable-objc-gc i386-linux
Comment 1 owner 2016-01-14 14:15:38 UTC
Thank you for the additional information you have supplied regarding
this Bug report.

This is an automatically generated reply to let you know your message
has been received.

Your message is being forwarded to the package maintainers and other
interested parties for their attention; they will reply in due course.

Your message has been sent to the package maintainer(s):
 Debian GCC Maintainers <debian-gcc@lists.debian.org>

If you wish to submit further information on this problem, please
send it to 33975@bugs.debian.org.

Please do not send mail to owner@bugs.debian.org unless you wish
to report a problem with the Bug-tracking system.
Comment 2 Martin Sebor 2017-02-28 00:15:16 UTC
Resolving as Won't Fix due to no progress in 14+ years.  The standard rules are now well established and so the usefulness of such a warning diminishes with each passing year.
Comment 3 owner 2017-02-28 00:27:36 UTC
Thank you for the additional information you have supplied regarding
this Bug report.

This is an automatically generated reply to let you know your message
has been received.

Your message is being forwarded to the package maintainers and other
interested parties for their attention; they will reply in due course.

Your message has been sent to the package maintainer(s):
 Debian GCC Maintainers <debian-gcc@lists.debian.org>

If you wish to submit further information on this problem, please
send it to 33975@bugs.debian.org.

Please do not send mail to owner@bugs.debian.org unless you wish
to report a problem with the Bug-tracking system.