public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/107926] New: wrong error message for excess elements in array initializer using a string literal
@ 2022-11-30  1:33 stephenheumann at gmail dot com
  2022-11-30  1:47 ` [Bug c/107926] " pinskia at gcc dot gnu.org
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: stephenheumann at gmail dot com @ 2022-11-30  1:33 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 107926
           Summary: wrong error message for excess elements in array
                    initializer using a string literal
           Product: gcc
           Version: 13.0
            Status: UNCONFIRMED
          Keywords: diagnostic
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: stephenheumann at gmail dot com
  Target Milestone: ---

char s[5] = {"abc",1};

gives the following error message:

<source>:1:20: error: excess elements in struct initializer
    1 | char s[5] = {"abc",1};
      |                    ^
<source>:1:20: note: (near initialization for 's')

There is no struct involved here, so the message is incorrect. It should
probably be 
"excess elements in array initializer".

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

* [Bug c/107926] wrong error message for excess elements in array initializer using a string literal
  2022-11-30  1:33 [Bug c/107926] New: wrong error message for excess elements in array initializer using a string literal stephenheumann at gmail dot com
@ 2022-11-30  1:47 ` pinskia at gcc dot gnu.org
  2022-11-30  2:13 ` pinskia at gcc dot gnu.org
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-11-30  1:47 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
      Known to fail|                            |12.1.0, 4.1.2
     Ever confirmed|0                           |1
             Status|UNCONFIRMED                 |NEW
   Last reconfirmed|                            |2022-11-30

--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Hmm, this could should have handled that:
  /* Handle superfluous braces around string cst as in
     char x[] = {"foo"}; */
  if (string_flag
      && constructor_type
      && !was_designated
      && TREE_CODE (constructor_type) == ARRAY_TYPE
      && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
      && integer_zerop (constructor_unfilled_index))
    {
      if (constructor_stack->replacement_value.value)
        error_init (loc, "excess elements in %<char%> array initializer");
      constructor_stack->replacement_value = value;
      return;
    }

But didn't for some reason ...

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

* [Bug c/107926] wrong error message for excess elements in array initializer using a string literal
  2022-11-30  1:33 [Bug c/107926] New: wrong error message for excess elements in array initializer using a string literal stephenheumann at gmail dot com
  2022-11-30  1:47 ` [Bug c/107926] " pinskia at gcc dot gnu.org
@ 2022-11-30  2:13 ` pinskia at gcc dot gnu.org
  2022-11-30  3:31 ` pinskia at gcc dot gnu.org
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-11-30  2:13 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

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

--- Comment #2 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
This patch I am testing which should fix the issue:
diff --git a/gcc/c/c-typeck.cc b/gcc/c/c-typeck.cc
index e06f052eb46..0fc382cc5eb 100644
--- a/gcc/c/c-typeck.cc
+++ b/gcc/c/c-typeck.cc
@@ -10623,17 +10623,22 @@ process_init_element (location_t loc, struct c_expr
value, bool implicit,

   /* Handle superfluous braces around string cst as in
      char x[] = {"foo"}; */
-  if (string_flag
-      && constructor_type
+  if (constructor_type
       && !was_designated
       && TREE_CODE (constructor_type) == ARRAY_TYPE
       && INTEGRAL_TYPE_P (TREE_TYPE (constructor_type))
       && integer_zerop (constructor_unfilled_index))
     {
       if (constructor_stack->replacement_value.value)
-       error_init (loc, "excess elements in %<char%> array initializer");
-      constructor_stack->replacement_value = value;
-      return;
+       {
+         error_init (loc, "excess elements in %<char%> array initializer");
+         return;
+       }
+      else if (string_flag)
+       {
+         constructor_stack->replacement_value = value;
+         return;
+       }
     }

   if (constructor_stack->replacement_value.value != NULL_TREE)

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

* [Bug c/107926] wrong error message for excess elements in array initializer using a string literal
  2022-11-30  1:33 [Bug c/107926] New: wrong error message for excess elements in array initializer using a string literal stephenheumann at gmail dot com
  2022-11-30  1:47 ` [Bug c/107926] " pinskia at gcc dot gnu.org
  2022-11-30  2:13 ` pinskia at gcc dot gnu.org
@ 2022-11-30  3:31 ` pinskia at gcc dot gnu.org
  2023-04-30 21:49 ` cvs-commit at gcc dot gnu.org
  2023-04-30 21:50 ` pinskia at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-11-30  3:31 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Note we reference char array still with:
unsigned short s[5] = {u"abc",1};
__WCHAR_TYPE__ s1[5] = {L"abc", 1};

But I also notice clang does too so maybe it is not so bad ...
Just for reference the C++ front-end does better (note I changed the types
since for C++, there are distant types):
<source>:1:26: error: too many initializers for 'char16_t [5]'
    1 | char16_t s[5] = {u"abc",1};
      |                          ^
<source>:2:27: error: too many initializers for 'wchar_t [5]'
    2 | wchar_t s1[5] = {L"abc", 1};
      |                           ^

And this is better error message than clang gives too:
<source>:1:21: error: excess elements in char array initializer
char s0[5] = {"abc",1};
                    ^
<source>:2:25: error: excess elements in char array initializer
char16_t s[5] = {u"abc",1};
                        ^
<source>:3:26: error: excess elements in char array initializer
wchar_t s1[5] = {L"abc", 1};
                         ^

I am thinking maybe I should copy the C++ front-end code ....

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

* [Bug c/107926] wrong error message for excess elements in array initializer using a string literal
  2022-11-30  1:33 [Bug c/107926] New: wrong error message for excess elements in array initializer using a string literal stephenheumann at gmail dot com
                   ` (2 preceding siblings ...)
  2022-11-30  3:31 ` pinskia at gcc dot gnu.org
@ 2023-04-30 21:49 ` cvs-commit at gcc dot gnu.org
  2023-04-30 21:50 ` pinskia at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2023-04-30 21:49 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The trunk branch has been updated by Andrew Pinski <pinskia@gcc.gnu.org>:

https://gcc.gnu.org/g:a6b810ae783acf8cec2d2272a46bd6de0976f496

commit r14-363-ga6b810ae783acf8cec2d2272a46bd6de0976f496
Author: Andrew Pinski <apinski@marvell.com>
Date:   Wed Nov 30 02:54:57 2022 +0000

    Fix C/107926: Wrong error message when initializing char array

    The problem here is the code which handles {"a"} is supposed
    to handle the case where the is something after the string but
    it only handles the case where there is another string so
    we go down the other path and error out saying "excess elements
    in struct initializer" even though this was a character array.
    To fix this, we need to move the ckeck if the initializer is
    a string after the check for array and initializer.

    OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions.

    Thanks,
    Adnrew Pinski

    gcc/c/ChangeLog:

            PR c/107926
            * c-typeck.cc (process_init_element): Move the check
            for string cst until after the error message.

    gcc/testsuite/ChangeLog:

            PR c/107926
            * gcc.dg/init-excess-3.c: New test.

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

* [Bug c/107926] wrong error message for excess elements in array initializer using a string literal
  2022-11-30  1:33 [Bug c/107926] New: wrong error message for excess elements in array initializer using a string literal stephenheumann at gmail dot com
                   ` (3 preceding siblings ...)
  2023-04-30 21:49 ` cvs-commit at gcc dot gnu.org
@ 2023-04-30 21:50 ` pinskia at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-04-30 21:50 UTC (permalink / raw)
  To: gcc-bugs

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

Andrew Pinski <pinskia at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |FIXED
   Target Milestone|---                         |14.0
             Status|ASSIGNED                    |RESOLVED

--- Comment #5 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Fixed.

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

end of thread, other threads:[~2023-04-30 21:50 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-30  1:33 [Bug c/107926] New: wrong error message for excess elements in array initializer using a string literal stephenheumann at gmail dot com
2022-11-30  1:47 ` [Bug c/107926] " pinskia at gcc dot gnu.org
2022-11-30  2:13 ` pinskia at gcc dot gnu.org
2022-11-30  3:31 ` pinskia at gcc dot gnu.org
2023-04-30 21:49 ` cvs-commit at gcc dot gnu.org
2023-04-30 21:50 ` pinskia 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).