public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/112826] New: gcc rejects the valid code and reports undefined reference
@ 2023-12-02 15:32 jwzeng at nuaa dot edu.cn
  2023-12-02 15:43 ` [Bug c/112826] " arsen at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: jwzeng at nuaa dot edu.cn @ 2023-12-02 15:32 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 112826
           Summary: gcc rejects the valid code and reports undefined
                    reference
           Product: gcc
           Version: 14.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: jwzeng at nuaa dot edu.cn
  Target Milestone: ---

Hi, the following code seems valid. Gcc rejects it while clang accepts.

Compiler explorer: https://godbolt.org/z/sPK9sYqc6

```c
$ cat test.c
int* a();
static int b() { int *c = a();}
int main() {}
$
$ gcc test.c
/tmp/cchtt1Nc.o:test.c:function b: error: undefined reference to 'a'
collect2: error: ld returned 1 exit status
$
$ clang test.c
test.c:2:31: warning: non-void function does not return a value [-Wreturn-type]
    2 | static int b() { int *c = a();}
      |                               ^
1 warning generated.
$
$ gcc --version
gcc (GCC) 13.1.0
Copyright (C) 2023 Free Software Foundation, Inc.
$
$ clang --version
clang version 16.0.0
Target: x86_64-unknown-linux-gnu
Thread model: posix
```

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

* [Bug c/112826] gcc rejects the valid code and reports undefined reference
  2023-12-02 15:32 [Bug c/112826] New: gcc rejects the valid code and reports undefined reference jwzeng at nuaa dot edu.cn
@ 2023-12-02 15:43 ` arsen at gcc dot gnu.org
  2023-12-02 16:31 ` xry111 at gcc dot gnu.org
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: arsen at gcc dot gnu.org @ 2023-12-02 15:43 UTC (permalink / raw)
  To: gcc-bugs

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

Arsen Arsenović <arsen at gcc dot gnu.org> changed:

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

--- Comment #1 from Arsen Arsenović <arsen at gcc dot gnu.org> ---
I can't assert the validity of this code, but -O1 does elide the function as
expected.

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

* [Bug c/112826] gcc rejects the valid code and reports undefined reference
  2023-12-02 15:32 [Bug c/112826] New: gcc rejects the valid code and reports undefined reference jwzeng at nuaa dot edu.cn
  2023-12-02 15:43 ` [Bug c/112826] " arsen at gcc dot gnu.org
@ 2023-12-02 16:31 ` xry111 at gcc dot gnu.org
  2023-12-02 22:43 ` pinskia at gcc dot gnu.org
  2023-12-03  8:01 ` jwzeng at nuaa dot edu.cn
  3 siblings, 0 replies; 5+ messages in thread
From: xry111 at gcc dot gnu.org @ 2023-12-02 16:31 UTC (permalink / raw)
  To: gcc-bugs

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

Xi Ruoyao <xry111 at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |INVALID
             Status|UNCONFIRMED                 |RESOLVED
                 CC|                            |xry111 at gcc dot gnu.org

--- Comment #2 from Xi Ruoyao <xry111 at gcc dot gnu.org> ---
N1570 6.9 p2:

"If an identifier declared with external linkage is used in an expression
(other than as part of the operand of a sizeof or _Alignof operator whose
result is an integer constant), somewhere in the entire program there shall be
exactly one external definition for the identifier"

and annex J.2 explicitly lists "An identifier with external linkage is used,
but in the program there does not exist exactly one external definition for the
identifier" as undefined behavior.

Note that you cannot interpret "used" as "really executed at runtime" because
it's impossible to solve halting problem, so such an "interpretation" will
render the entire standard nonsense.

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

* [Bug c/112826] gcc rejects the valid code and reports undefined reference
  2023-12-02 15:32 [Bug c/112826] New: gcc rejects the valid code and reports undefined reference jwzeng at nuaa dot edu.cn
  2023-12-02 15:43 ` [Bug c/112826] " arsen at gcc dot gnu.org
  2023-12-02 16:31 ` xry111 at gcc dot gnu.org
@ 2023-12-02 22:43 ` pinskia at gcc dot gnu.org
  2023-12-03  8:01 ` jwzeng at nuaa dot edu.cn
  3 siblings, 0 replies; 5+ messages in thread
From: pinskia at gcc dot gnu.org @ 2023-12-02 22:43 UTC (permalink / raw)
  To: gcc-bugs

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

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

           What    |Removed                     |Added
----------------------------------------------------------------------------
           See Also|                            |https://gcc.gnu.org/bugzill
                   |                            |a/show_bug.cgi?id=14105

--- Comment #3 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
Note PR 14105 is the opposite request but both are invalid as both behavior
with and without optimization is allowed by the C standard.

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

* [Bug c/112826] gcc rejects the valid code and reports undefined reference
  2023-12-02 15:32 [Bug c/112826] New: gcc rejects the valid code and reports undefined reference jwzeng at nuaa dot edu.cn
                   ` (2 preceding siblings ...)
  2023-12-02 22:43 ` pinskia at gcc dot gnu.org
@ 2023-12-03  8:01 ` jwzeng at nuaa dot edu.cn
  3 siblings, 0 replies; 5+ messages in thread
From: jwzeng at nuaa dot edu.cn @ 2023-12-03  8:01 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from Junwei Zeng <jwzeng at nuaa dot edu.cn> ---
Thank you for your explanation.

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

end of thread, other threads:[~2023-12-03  8:01 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-12-02 15:32 [Bug c/112826] New: gcc rejects the valid code and reports undefined reference jwzeng at nuaa dot edu.cn
2023-12-02 15:43 ` [Bug c/112826] " arsen at gcc dot gnu.org
2023-12-02 16:31 ` xry111 at gcc dot gnu.org
2023-12-02 22:43 ` pinskia at gcc dot gnu.org
2023-12-03  8:01 ` jwzeng at nuaa dot edu.cn

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