public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jonathan Wakely <jwakely.gcc@gmail.com>
To: Florian Weimer <fweimer@redhat.com>
Cc: Marek Polacek <polacek@redhat.com>, gcc@gcc.gnu.org
Subject: Re: -Wint-conversion, -Wincompatible-pointer-types, -Wpointer-sign: Are they hiding constraint C violations?
Date: Thu, 10 Nov 2022 23:05:52 +0000	[thread overview]
Message-ID: <CAH6eHdT_ot6-qW+kAkMgv1Z3AGOUe6Gg5Cc+Md6HkibiqWnjkQ@mail.gmail.com> (raw)
In-Reply-To: <87h6z6sjqp.fsf@oldenburg.str.redhat.com>

On Thu, 10 Nov 2022 at 19:17, Florian Weimer via Gcc <gcc@gcc.gnu.org> wrote:
>
> * Marek Polacek:
>
> > On Thu, Nov 10, 2022 at 07:25:21PM +0100, Florian Weimer via Gcc wrote:
> >> GCC accepts various conversions between pointers and ints and different
> >> types of pointers by default, issuing a warning.
> >>
> >> I've been reading the (hopefully) relevant partso f the C99 standard,
> >> and it seems to me that C implementations are actually required to
> >> diagnose errors in these cases because they are constraint violations:
> >> the types are not compatible.
> >
> > It doesn't need to be a hard error, a warning is a diagnostic message,
> > which is enough to diagnose a violation of any syntax rule or
> > constraint.
> >
> > IIRC, the only case where the compiler _must_ emit a hard error is for
> > #error.
>
> Hmm, you could be right.
>
> The standard says that constraint violations are not undefiend behavior,
> but of course it does not define what happens in the presence of a
> constraint violation.  So the behavior is undefined by omission.  This
> seems to be a contradiction.

As long as a diagnostic is issued, the invalid program can be
successfully translated. Presumably the implementation defines what it
means in that case, because it's not a valid C program, so the
standard no longer applies. As you say, that's undefined by omission.


> I assumed that there was a rule similar to the the rule for #error for
> any kind of diagnostic, which would mean that GCC errors are diagnostic
> messages in the sense of the standard, but GCC warnings are not.
>
> I wonder how C++ handles this.

Similarly. We don't have constraint violations, we have "diagnosable
rules", see [intro.compliance.general]. If a program contains a
violation of a diagnosable rule, the implementation must issue a
diagnostic message. What it does after that is up to the
implementation, the program has left the realm of valid C++ code.

It's not exactly easy for a user to know whether a given compiler
warning was issued because of a constraint (or diagnosable rule)
violation, or because e.g. the compiler thinks extra parens or more
consistent indentation would be a good idea. In the former case, the
program has undefined behaviour (strictly speaking, but in practice
the implementation might "define" its behaviour) and in the latter
case it doesn't. -pedantic-errors can help, as that turns the former
class into errors.

  reply	other threads:[~2022-11-10 23:06 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-10 18:25 Florian Weimer
2022-11-10 18:43 ` Marek Polacek
2022-11-10 19:16   ` Florian Weimer
2022-11-10 23:05     ` Jonathan Wakely [this message]
2022-11-10 23:33     ` Joseph Myers
2022-11-11  9:21     ` David Brown

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=CAH6eHdT_ot6-qW+kAkMgv1Z3AGOUe6Gg5Cc+Md6HkibiqWnjkQ@mail.gmail.com \
    --to=jwakely.gcc@gmail.com \
    --cc=fweimer@redhat.com \
    --cc=gcc@gcc.gnu.org \
    --cc=polacek@redhat.com \
    /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).