public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "Keith.S.Thompson at gmail dot com" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug c/107091] New: Misleading error message "incompatible types when returning ..."
Date: Thu, 29 Sep 2022 18:30:52 +0000	[thread overview]
Message-ID: <bug-107091-4@http.gcc.gnu.org/bugzilla/> (raw)

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.

             reply	other threads:[~2022-09-29 18:31 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-09-29 18:30 Keith.S.Thompson at gmail dot com [this message]
2022-09-29 21:18 ` [Bug c/107091] " pinskia at gcc dot gnu.org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-107091-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).