public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c/107091] New: Misleading error message "incompatible types when returning ..."
@ 2022-09-29 18:30 Keith.S.Thompson at gmail dot com
2022-09-29 21:18 ` [Bug c/107091] " pinskia at gcc dot gnu.org
0 siblings, 1 reply; 2+ messages in thread
From: Keith.S.Thompson at gmail dot com @ 2022-09-29 18:30 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107091
Bug ID: 107091
Summary: Misleading error message "incompatible types when
returning ..."
Product: gcc
Version: 12.2.0
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: ---
When I compile this source file:
double bad(void) {
return (void*)0;
}
double good(void) {
return 42;
}
I get this output from gcc 12.2.0 (and from earlier versions):
c.c: In function ‘bad’:
c.c:2:12: error: incompatible types when returning type ‘void *’ but ‘double’
was expected
2 | return (void*)0;
| ^
It's true that it's a constraint violation, and it's true that void* and double
are incompatible, but type compatibility is not required here. The problem is
that there is no implicit conversion from void* to double.
In the "good" function, int and double are also incompatible types, but there
is an implicit conversion so the statement is valid.
Note that g++ produces this correct message for the equivalent C++ code:
c.cpp: In function ‘double bad()’:
c.cpp:2:19: error: cannot convert ‘void*’ to ‘double’ in return
2 | return (void*)0;
|
This was brought to my attention by this post on Stack Overflow:
https://stackoverflow.com/q/73899947/827263
Reference for compatible types: C11 (or N1570) 6.2.7.
^ permalink raw reply [flat|nested] 2+ messages in thread
* [Bug c/107091] Misleading error message "incompatible types when returning ..."
2022-09-29 18:30 [Bug c/107091] New: Misleading error message "incompatible types when returning ..." Keith.S.Thompson at gmail dot com
@ 2022-09-29 21:18 ` pinskia at gcc dot gnu.org
0 siblings, 0 replies; 2+ messages in thread
From: pinskia at gcc dot gnu.org @ 2022-09-29 21:18 UTC (permalink / raw)
To: gcc-bugs
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=107091
Andrew Pinski <pinskia at gcc dot gnu.org> changed:
What |Removed |Added
----------------------------------------------------------------------------
Ever confirmed|0 |1
Last reconfirmed| |2022-09-29
Status|UNCONFIRMED |NEW
--- Comment #1 from Andrew Pinski <pinskia at gcc dot gnu.org> ---
clang produces:
<source>:3:12: error: returning 'void *' from a function with incompatible
result type 'double'
return (void *)0;
^~~~~~~~~
MSVC produces:
<source>(3): error C2440: 'return': cannot convert from 'void *' to 'double'
ICC Produces:
<source>(3): error: return value type does not match the function type
return (void *)0;
^
cproc (https://github.com/michaelforney/cproc) produces:
<source>:2:22: error: assignment to arithmetic type must be from arithmetic
type
MSVC seems similar to what is produced for GCC's C++ front-end.
I don't know the best wording here really because convert might even be wrong.
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2022-09-29 21:18 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-09-29 18:30 [Bug c/107091] New: Misleading error message "incompatible types when returning ..." Keith.S.Thompson at gmail dot com
2022-09-29 21:18 ` [Bug c/107091] " 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).