public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/96077] New: GCC accepts ill-legal local enum definition
@ 2020-07-06 12:31 haoxintu at gmail dot com
  2020-07-06 13:44 ` [Bug c++/96077] " redi at gcc dot gnu.org
                   ` (9 more replies)
  0 siblings, 10 replies; 11+ messages in thread
From: haoxintu at gmail dot com @ 2020-07-06 12:31 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 96077
           Summary: GCC accepts ill-legal local enum definition
           Product: gcc
           Version: 11.0
            Status: UNCONFIRMED
          Keywords: accepts-invalid
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: haoxintu at gmail dot com
  Target Milestone: ---

Hi, all.

This code, test.cc, has a grammar issue in "missing ')'", but GCC accepts it
well.

$cat test.cc
enum { a = ( 1 ) ? ( 2 ) : ( 2 + 1 } g_e; //reject
int main () {
    enum { a = ( 1 ) ? ( 2 ) : ( 2 + 1 /* missing ")" here */ } l_e; //accept
    return 0;
}

$g++ test.cc
test.cc:1:35: error: expected ‘)’ before ‘}’ token
    1 | enum { a = ( 1 ) ? ( 2 ) : ( 2 + 1 } g_e; //reject
      |                            ~      ^~
      |                                  )

GCC only rejects line 1 but accepts line 3.

While in Clang

$clang++ test.cc
test.cc:1:36: error: expected ')'
enum { a = ( 1 ) ? ( 2 ) : ( 2 + 1 } g_e; //reject
                                   ^
test.cc:1:28: note: to match this '('
enum { a = ( 1 ) ? ( 2 ) : ( 2 + 1 } g_e; //reject
                           ^
test.cc:3:63: error: expected ')'
    enum { a = ( 1 ) ? ( 2 ) : ( 2 + 1 /* missing ")" here */ } l_e; //accept
                                                              ^
test.cc:3:32: note: to match this '('
    enum { a = ( 1 ) ? ( 2 ) : ( 2 + 1 /* missing ")" here */ } l_e; //accept
                               ^
2 errors generated.


$g++ -v
Using built-in specs.
COLLECT_GCC=g++
COLLECT_LTO_WRAPPER=/home/haoxin/corpus-compilers/gcc-trunk/gcc-master/build/libexec/gcc/x86_64-pc-linux-gnu/11.0.0/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: ../configure
--prefix=/home/haoxin/corpus-compilers/gcc-trunk/gcc-master/build/
--enable-languages=c,c++ CC=gcc CXX=g++
Thread model: posix
Supported LTO compression algorithms: zlib
gcc version 11.0.0 20200630 (experimental) (GCC)

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

* [Bug c++/96077] GCC accepts ill-legal local enum definition
  2020-07-06 12:31 [Bug c++/96077] New: GCC accepts ill-legal local enum definition haoxintu at gmail dot com
@ 2020-07-06 13:44 ` redi at gcc dot gnu.org
  2020-07-06 13:50 ` haoxintu at gmail dot com
                   ` (8 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: redi at gcc dot gnu.org @ 2020-07-06 13:44 UTC (permalink / raw)
  To: gcc-bugs

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

Jonathan Wakely <redi at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |NEW
     Ever confirmed|0                           |1
   Last reconfirmed|                            |2020-07-06

--- Comment #1 from Jonathan Wakely <redi at gcc dot gnu.org> ---
It's not ideal to provide a testcase that doesn't compile for an
accepts-invalid bug. A testcase that actually compiles is a better
demonstration that the invalid code is accepted, e.g.

int main () {
    enum { a = ( 1 ) ? ( 2 ) : ( 2 + 1 /* missing ")" here */ } l_e; //accept
}


Every version from 4.0 onwards behaves the same (I didn't test anything older).

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

* [Bug c++/96077] GCC accepts ill-legal local enum definition
  2020-07-06 12:31 [Bug c++/96077] New: GCC accepts ill-legal local enum definition haoxintu at gmail dot com
  2020-07-06 13:44 ` [Bug c++/96077] " redi at gcc dot gnu.org
@ 2020-07-06 13:50 ` haoxintu at gmail dot com
  2020-07-08 20:22 ` mpolacek at gcc dot gnu.org
                   ` (7 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: haoxintu at gmail dot com @ 2020-07-06 13:50 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Haoxin Tu <haoxintu at gmail dot com> ---
(In reply to Jonathan Wakely from comment #1)
> It's not ideal to provide a testcase that doesn't compile for an
> accepts-invalid bug. A testcase that actually compiles is a better
> demonstration that the invalid code is accepted

OK, I got it. Thank you Jonathan. I will do better next time.

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

* [Bug c++/96077] GCC accepts ill-legal local enum definition
  2020-07-06 12:31 [Bug c++/96077] New: GCC accepts ill-legal local enum definition haoxintu at gmail dot com
  2020-07-06 13:44 ` [Bug c++/96077] " redi at gcc dot gnu.org
  2020-07-06 13:50 ` haoxintu at gmail dot com
@ 2020-07-08 20:22 ` mpolacek at gcc dot gnu.org
  2020-07-09 20:35 ` mpolacek at gcc dot gnu.org
                   ` (6 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2020-07-08 20:22 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |mpolacek at gcc dot gnu.org

--- Comment #3 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
This is sneaky:

int main ()
{
  enum { a = true ? 2 : (3 /* missing ")" here */ } l_e;
  if (a != 2)
    __builtin_abort ();
}

we never reported the error and 'a' was set to 0 instead of 2.  I plan to poke
more.

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

* [Bug c++/96077] GCC accepts ill-legal local enum definition
  2020-07-06 12:31 [Bug c++/96077] New: GCC accepts ill-legal local enum definition haoxintu at gmail dot com
                   ` (2 preceding siblings ...)
  2020-07-08 20:22 ` mpolacek at gcc dot gnu.org
@ 2020-07-09 20:35 ` mpolacek at gcc dot gnu.org
  2020-07-09 21:40 ` mpolacek at gcc dot gnu.org
                   ` (5 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2020-07-09 20:35 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
Doesn't even need to be in the ?: operator:

int
main ()
{
  enum { E = (2 } e;
  if (E != 2)
    __builtin_abort ();
}

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

* [Bug c++/96077] GCC accepts ill-legal local enum definition
  2020-07-06 12:31 [Bug c++/96077] New: GCC accepts ill-legal local enum definition haoxintu at gmail dot com
                   ` (3 preceding siblings ...)
  2020-07-09 20:35 ` mpolacek at gcc dot gnu.org
@ 2020-07-09 21:40 ` mpolacek at gcc dot gnu.org
  2020-07-10 15:48 ` mpolacek at gcc dot gnu.org
                   ` (4 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2020-07-09 21:40 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Assignee|unassigned at gcc dot gnu.org      |mpolacek at gcc dot gnu.org
             Status|NEW                         |ASSIGNED

--- Comment #5 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
We probably need to commit like this to get errors:

--- a/gcc/cp/parser.c
+++ b/gcc/cp/parser.c
@@ -19412,7 +19412,10 @@ cp_parser_enum_specifier (cp_parser* parser)
             "ISO C++ forbids empty unnamed enum");
    }
       else
-   cp_parser_enumerator_list (parser, type);
+   {
+     cp_parser_commit_to_tentative_parse (parser);
+     cp_parser_enumerator_list (parser, type);
+   }

       /* Consume the final '}'.  */
       braces.require_close (parser);

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

* [Bug c++/96077] GCC accepts ill-legal local enum definition
  2020-07-06 12:31 [Bug c++/96077] New: GCC accepts ill-legal local enum definition haoxintu at gmail dot com
                   ` (4 preceding siblings ...)
  2020-07-09 21:40 ` mpolacek at gcc dot gnu.org
@ 2020-07-10 15:48 ` mpolacek at gcc dot gnu.org
  2020-07-13 15:16 ` cvs-commit at gcc dot gnu.org
                   ` (3 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2020-07-10 15:48 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Keywords|                            |patch

--- Comment #6 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
https://gcc.gnu.org/pipermail/gcc-patches/2020-July/549852.html

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

* [Bug c++/96077] GCC accepts ill-legal local enum definition
  2020-07-06 12:31 [Bug c++/96077] New: GCC accepts ill-legal local enum definition haoxintu at gmail dot com
                   ` (5 preceding siblings ...)
  2020-07-10 15:48 ` mpolacek at gcc dot gnu.org
@ 2020-07-13 15:16 ` cvs-commit at gcc dot gnu.org
  2020-07-13 15:19 ` cvs-commit at gcc dot gnu.org
                   ` (2 subsequent siblings)
  9 siblings, 0 replies; 11+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2020-07-13 15:16 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #7 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Marek Polacek <mpolacek@gcc.gnu.org>:

https://gcc.gnu.org/g:4fd124a23664c712f1bb1a7e91fa23fe83d72c0b

commit r11-2064-g4fd124a23664c712f1bb1a7e91fa23fe83d72c0b
Author: Marek Polacek <polacek@redhat.com>
Date:   Thu Jul 9 20:44:05 2020 -0400

    c++: Fix tentative parsing of enum-specifier [PR96077]

    Here's an interesting issue: in this code a ) is missing:

      enum { E = (2 } e;

    but we compile the code anyway, and E is set to 0 in build_enumerator,
    which is sneaky.

    The problem is that cp_parser_enum_specifier parses tentatively, because
    when we see the enum keyword, we don't know yet if we'll find an
    enum-specifier, opaque-enum-declaration, or elaborated-enum-specifier.

    In this test when we call cp_parser_enumerator_list we're still parsing
    tentatively, and as a consequence, parens.require_close (parser) in
    cp_parser_primary_expression doesn't report any errors.  But we only go
    on to parse the enumerator-list after we've seen a {, at which point we
    might as well commit -- we know we're dealing with an enum-specifier.

    gcc/cp/ChangeLog:

            PR c++/96077
            * parser.c (cp_parser_enum_specifier): Commit to tentative parse
            after we've seen an opening brace.

    gcc/testsuite/ChangeLog:

            PR c++/96077
            * g++.dg/parse/enum14.C: New test.

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

* [Bug c++/96077] GCC accepts ill-legal local enum definition
  2020-07-06 12:31 [Bug c++/96077] New: GCC accepts ill-legal local enum definition haoxintu at gmail dot com
                   ` (6 preceding siblings ...)
  2020-07-13 15:16 ` cvs-commit at gcc dot gnu.org
@ 2020-07-13 15:19 ` cvs-commit at gcc dot gnu.org
  2020-07-13 15:19 ` mpolacek at gcc dot gnu.org
  2020-07-13 15:19 ` mpolacek at gcc dot gnu.org
  9 siblings, 0 replies; 11+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2020-07-13 15:19 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #8 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The releases/gcc-10 branch has been updated by Marek Polacek
<mpolacek@gcc.gnu.org>:

https://gcc.gnu.org/g:30529e2faa482bc749c65a490763dbc2ccaf63ac

commit r10-8470-g30529e2faa482bc749c65a490763dbc2ccaf63ac
Author: Marek Polacek <polacek@redhat.com>
Date:   Thu Jul 9 20:44:05 2020 -0400

    c++: Fix tentative parsing of enum-specifier [PR96077]

    Here's an interesting issue: in this code a ) is missing:

      enum { E = (2 } e;

    but we compile the code anyway, and E is set to 0 in build_enumerator,
    which is sneaky.

    The problem is that cp_parser_enum_specifier parses tentatively, because
    when we see the enum keyword, we don't know yet if we'll find an
    enum-specifier, opaque-enum-declaration, or elaborated-enum-specifier.

    In this test when we call cp_parser_enumerator_list we're still parsing
    tentatively, and as a consequence, parens.require_close (parser) in
    cp_parser_primary_expression doesn't report any errors.  But we only go
    on to parse the enumerator-list after we've seen a {, at which point we
    might as well commit -- we know we're dealing with an enum-specifier.

    gcc/cp/ChangeLog:

            PR c++/96077
            * parser.c (cp_parser_enum_specifier): Commit to tentative parse
            after we've seen an opening brace.

    gcc/testsuite/ChangeLog:

            PR c++/96077
            * g++.dg/parse/enum14.C: New test.

    (cherry picked from commit 4fd124a23664c712f1bb1a7e91fa23fe83d72c0b)

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

* [Bug c++/96077] GCC accepts ill-legal local enum definition
  2020-07-06 12:31 [Bug c++/96077] New: GCC accepts ill-legal local enum definition haoxintu at gmail dot com
                   ` (7 preceding siblings ...)
  2020-07-13 15:19 ` cvs-commit at gcc dot gnu.org
@ 2020-07-13 15:19 ` mpolacek at gcc dot gnu.org
  2020-07-13 15:19 ` mpolacek at gcc dot gnu.org
  9 siblings, 0 replies; 11+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2020-07-13 15:19 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #9 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
Fixed for GCC 10.2 and 11.

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

* [Bug c++/96077] GCC accepts ill-legal local enum definition
  2020-07-06 12:31 [Bug c++/96077] New: GCC accepts ill-legal local enum definition haoxintu at gmail dot com
                   ` (8 preceding siblings ...)
  2020-07-13 15:19 ` mpolacek at gcc dot gnu.org
@ 2020-07-13 15:19 ` mpolacek at gcc dot gnu.org
  9 siblings, 0 replies; 11+ messages in thread
From: mpolacek at gcc dot gnu.org @ 2020-07-13 15:19 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |FIXED
             Status|ASSIGNED                    |RESOLVED

--- Comment #10 from Marek Polacek <mpolacek at gcc dot gnu.org> ---
.

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

end of thread, other threads:[~2020-07-13 15:19 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-07-06 12:31 [Bug c++/96077] New: GCC accepts ill-legal local enum definition haoxintu at gmail dot com
2020-07-06 13:44 ` [Bug c++/96077] " redi at gcc dot gnu.org
2020-07-06 13:50 ` haoxintu at gmail dot com
2020-07-08 20:22 ` mpolacek at gcc dot gnu.org
2020-07-09 20:35 ` mpolacek at gcc dot gnu.org
2020-07-09 21:40 ` mpolacek at gcc dot gnu.org
2020-07-10 15:48 ` mpolacek at gcc dot gnu.org
2020-07-13 15:16 ` cvs-commit at gcc dot gnu.org
2020-07-13 15:19 ` cvs-commit at gcc dot gnu.org
2020-07-13 15:19 ` mpolacek at gcc dot gnu.org
2020-07-13 15:19 ` mpolacek at gcc dot gnu.org

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