public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jason Merrill <jason@redhat.com>
To: Marek Polacek <polacek@redhat.com>
Cc: GCC Patches <gcc-patches@gcc.gnu.org>
Subject: Re: C++ PATCH for c++/78244 - narrowing conversion in template not detected, part 2
Date: Sun, 27 Jan 2019 00:25:00 -0000	[thread overview]
Message-ID: <CADzB+2kcU3W1ZrmqFHpgDBruPKHM8jiC=5rcPBXcKqECgaSN8Q@mail.gmail.com> (raw)
In-Reply-To: <20190125212043.GN26714@redhat.com>

On Fri, Jan 25, 2019 at 4:20 PM Marek Polacek <polacek@redhat.com> wrote:
> On Fri, Jan 25, 2019 at 10:05:00AM -0500, Jason Merrill wrote:
> > On 1/24/19 7:17 PM, Marek Polacek wrote:
> > > On Wed, Jan 23, 2019 at 03:34:04PM -0500, Jason Merrill wrote:
> > > > On Wed, Jan 23, 2019 at 12:57 PM Marek Polacek <polacek@redhat.com> wrote:
> > > > >
> > > > > On Wed, Jan 23, 2019 at 09:00:36AM -0500, Jason Merrill wrote:
> > > > > > I was talking about digest_init, not reshape_init.  digest_init calls
> > > > > > convert_for_initialization.
> > > > >
> > > > > /facepalm
> > > > >
> > > > > So yes, digest_init calls convert_for_initialization which will end up
> > > > > calling perform_implicit_conversion_flags which could call convert_like_real
> > > > > where the narrowing warnings are given, but it doesn't, we go to this case:
> > > > >
> > > > >    else if (processing_template_decl && conv->kind != ck_identity)
> > > > >      {
> > > > >        /* In a template, we are only concerned about determining the
> > > > >           type of non-dependent expressions, so we do not have to
> > > > >           perform the actual conversion.  But for initializers, we
> > > > >           need to be able to perform it at instantiation
> > > > >           (or instantiate_non_dependent_expr) time.  */
> > > > >        expr = build1 (IMPLICIT_CONV_EXPR, type, expr);
> > > > >
> > > > > finish_decltype_type throws away the expression because it's not dependent, and
> > > > > only uses its type.  So narrowing remains undetected.  Not sure if I should mess
> > > > > with perform_implicit_conversion_flags.
> > > >
> > > > Let's try that; this is a situation where the comment is incorrect.
> > > > Perhaps just call check_narrowing here if appropriate, rather than go
> > > > through the whole conversion machinery.
> > >
> > > I have not been successful.
> > >
> > > First, I modified perform_implicit_conversion_flags to go the convert_like
> > > route when dealing with something non-dependent.  That breaks e.g. in
> > > build_value_init:
> > >   346   /* The AGGR_INIT_EXPR tweaking below breaks in templates.  */
> > >   347   gcc_assert (!processing_template_decl
> > >   348               || (SCALAR_TYPE_P (type) || TREE_CODE (type) == ARRAY_TYPE));
> > > Even if I restrict the convert_like way for non-dependent exprs in a template
> > > for scalars, it still breaks elsewhere, e.g. constexpr-template3.C where it
> > > complains about taking the address of an rvalue.
> > >
> > > Second, I added check_narrowing to the processing_template_decl case in
> > > perform_implicit_conversion_flags.  That works except it breaks
> > > constexpr-inst1.C -- we no longer get the error.  That's because currently
> > > check_narrowing in finish_compound_literal calls maybe_constant_init, which
> > > calls instantiate_constexpr_fns and we get the desired diagnostic.  But if
> > > I move check_narrowing to perform_implicit_conversion_flags, we no longer
> > > call it in this case -- processing_template_decl is 0 so we call convert_like
> > > but that doesn't do the trick.
> > >
> > > So, back to the patch that leaves check_narrowing in finish_compound_literal?
> >
> > That patch still needs a test for the aggregate case.
>
> Ok, this is a version with Wnarrowing16.C added.
>
> ...but we still don't warn for the TYPE_NON_AGGREGATE_CLASS case in
> finish_compound_literal, so the nightmare continues.

Alas.  Are you going to keep looking at that, or would you like me to take over?

> -  if (SCALAR_TYPE_P (type)
> -      && !BRACE_ENCLOSED_INITIALIZER_P (compound_literal)
> +  if (!BRACE_ENCLOSED_INITIALIZER_P (compound_literal)
>        && !check_narrowing (type, compound_literal, complain))
>      return error_mark_node;

Does this hunk actually make a difference?  It looks like
check_narrowing only does anything for arithmetic types.

OK with or without this hunk.

Jason

  reply	other threads:[~2019-01-27  0:22 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-01-17 19:09 Marek Polacek
2019-01-17 21:17 ` Jason Merrill
2019-01-18 14:12   ` Marek Polacek
2019-01-21 20:15     ` Jason Merrill
2019-01-22 21:12       ` Marek Polacek
2019-01-23 14:01         ` Jason Merrill
2019-01-23 18:29           ` Marek Polacek
2019-01-23 20:51             ` Jason Merrill
2019-01-25  0:51               ` Marek Polacek
2019-01-25 15:06                 ` Jason Merrill
2019-01-25 21:55                   ` Marek Polacek
2019-01-27  0:25                     ` Jason Merrill [this message]
2019-01-27 20:18                       ` Marek Polacek

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='CADzB+2kcU3W1ZrmqFHpgDBruPKHM8jiC=5rcPBXcKqECgaSN8Q@mail.gmail.com' \
    --to=jason@redhat.com \
    --cc=gcc-patches@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).