From: Jason Merrill <jason@redhat.com>
To: Marek Polacek <polacek@redhat.com>
Cc: GCC Patches <gcc-patches@gcc.gnu.org>
Subject: Re: C++ PATCH to fix static init with () in a template (PR c++/84582)
Date: Thu, 01 Mar 2018 18:57:00 -0000 [thread overview]
Message-ID: <CADzB+2nE2vcBYbkLqJTHBYMyFXJA_j1qRay+JZCMyp_OVf7KGQ@mail.gmail.com> (raw)
In-Reply-To: <20180301131725.GB16833@redhat.com>
On Thu, Mar 1, 2018 at 8:17 AM, Marek Polacek <polacek@redhat.com> wrote:
> On Wed, Feb 28, 2018 at 04:50:39PM -0500, Jason Merrill wrote:
>> On Wed, Feb 28, 2018 at 4:19 PM, Marek Polacek <polacek@redhat.com> wrote:
>> > On Wed, Feb 28, 2018 at 10:51:17AM -0500, Jason Merrill wrote:
>> >> On Wed, Feb 28, 2018 at 9:32 AM, Marek Polacek <polacek@redhat.com> wrote:
>> >> > On Tue, Feb 27, 2018 at 04:16:31PM -0500, Jason Merrill wrote:
>> >> >> On 02/27/2018 02:13 PM, Marek Polacek wrote:
>> >> >> > My recent change introducing cxx_constant_init caused this code
>> >> >> >
>> >> >> > template <class> class A {
>> >> >> > static const long b = 0;
>> >> >> > static const unsigned c = (b);
>> >> >> > };
>> >> >> >
>> >> >> > to be rejected. The reason is that force_paren_expr turns "b" into "*(const
>> >> >> > long int &) &b", where the former is not value-dependent but the latter is
>> >> >> > value-dependent. So when we get to maybe_constant_init_1:
>> >> >> > 5147 if (!is_nondependent_static_init_expression (t))
>> >> >> > 5148 /* Don't try to evaluate it. */;
>> >> >> > it's not evaluated and we get the non-constant initialization error.
>> >> >> > (Before we'd always evaluated the expression.)
>> >> >> >
>> >> >> > Bootstrapped/regtested on x86_64-linux, ok for trunk?
>> >> >> >
>> >> >> > 2018-02-27 Marek Polacek <polacek@redhat.com>
>> >> >> >
>> >> >> > PR c++/84582
>> >> >> > * semantics.c (force_paren_expr): Avoid creating a static cast
>> >> >> > when processing a template.
>> >> >> >
>> >> >> > * g++.dg/cpp1z/static1.C: New test.
>> >> >> > * g++.dg/template/static37.C: New test.
>> >> >> >
>> >> >> > diff --git gcc/cp/semantics.c gcc/cp/semantics.c
>> >> >> > index 35569d0cb0d..b48de2df4e2 100644
>> >> >> > --- gcc/cp/semantics.c
>> >> >> > +++ gcc/cp/semantics.c
>> >> >> > @@ -1697,7 +1697,7 @@ force_paren_expr (tree expr)
>> >> >> > expr = build1 (PAREN_EXPR, TREE_TYPE (expr), expr);
>> >> >> > else if (VAR_P (expr) && DECL_HARD_REGISTER (expr))
>> >> >> > /* We can't bind a hard register variable to a reference. */;
>> >> >> > - else
>> >> >> > + else if (!processing_template_decl)
>> >> >>
>> >> >> Hmm, this means that we forget about the parentheses in a template. I'm
>> >> >> surprised that this didn't break anything in the testsuite. In particular,
>> >> >> auto-fn15.C. I've attached an addition to auto-fn15.C to catch this issue.
>> >> >
>> >> > Thanks, you're right. I'll use it.
>> >> >
>> >> >> Can we use PAREN_EXPR instead of the static_cast in a template?
>> >> >
>> >> > I don't think so, it would fix the issue you pointed out in auto-fn15.C but
>> >> > it wouldn't fix the original test. The problem with using PAREN_EXPR in a
>> >> > template is that instantiate_non_dependent_expr will turn in into the
>> >> > static cast anyway; tsubst_copy_and_build has
>> >> > case PAREN_EXPR:
>> >> > RETURN (finish_parenthesized_expr (RECUR (TREE_OPERAND (t, 0))));
>> >> > so it calls force_paren_expr and this time we're not in a template. And
>> >> > then when calling cxx_constant_init we have the same issue.
>> >>
>> >> Then maybe we need something like fold_non_dependent_expr, which
>> >> checks for dependency before substitution and then immediately
>> >> evaluates the result.
>> >
>> > I hope you meant something like this. Further testing also revealed that
>> > maybe_undo_parenthesized_ref should be able to unwrap PAREN_EXPR (so that
>> > (fn1)(); in paren2.C is handled correctly), and that lvalue_kind should look
>> > into PAREN_EXPR so as to give the correct answer regarding lvalueness: we
>> > should accept
>> >
>> > template<typename T>
>> > void foo (int i)
>> > {
>> > ++(i);
>> > }
>> >
>> > Apologies if I'm on the wrong track.
>> >
>> > Bootstrapped/regtested on x86_64-linux, ok for trunk?
>> >
>> > 2018-02-28 Marek Polacek <polacek@redhat.com>
>> > Jason Merrill <jason@redhat.com>
>> >
>> > PR c++/84582
>> > * semantics.c (force_paren_expr): Avoid creating the static cast
>> > when in a template. Create a PAREN_EXPR when in a template.
>> > (maybe_undo_parenthesized_ref): Unwrap PAREN_EXPR.
>> > * typeck2.c (store_init_value): Call fold_non_dependent_expr instead
>> > of instantiate_non_dependent_expr.
>> > * tree.c (lvalue_kind): Handle PAREN_EXPR like NON_DEPENDENT_EXPR.
>> >
>> > * g++.dg/cpp1y/auto-fn15.C: Extend testing.
>> > * g++.dg/cpp1z/static1.C: New test.
>> > * g++.dg/template/static37.C: New test.
>> >
>> > diff --git gcc/cp/semantics.c gcc/cp/semantics.c
>> > index 35569d0cb0d..722e3718a14 100644
>> > --- gcc/cp/semantics.c
>> > +++ gcc/cp/semantics.c
>> > @@ -1697,7 +1697,7 @@ force_paren_expr (tree expr)
>> > expr = build1 (PAREN_EXPR, TREE_TYPE (expr), expr);
>> > else if (VAR_P (expr) && DECL_HARD_REGISTER (expr))
>> > /* We can't bind a hard register variable to a reference. */;
>> > - else
>> > + else if (!processing_template_decl)
>> > {
>> > cp_lvalue_kind kind = lvalue_kind (expr);
>> > if ((kind & ~clk_class) != clk_none)
>> > @@ -1713,6 +1713,8 @@ force_paren_expr (tree expr)
>> > REF_PARENTHESIZED_P (expr) = true;
>> > }
>> > }
>> > + else
>> > + expr = build1 (PAREN_EXPR, TREE_TYPE (expr), expr);
>>
>> There's already a branch for building PAREN_EXPR, let's just replace
>> its condition.
>
> Sure.
>
>> > - value = instantiate_non_dependent_expr (value);
>> > + value = fold_non_dependent_expr (value);
>>
>> I was thinking that we want a parallel fold_non_dependent_init (that
>> hopefully shares most of the implementation). Then we shouldn't need
>> the call to maybe_constant_init anymore.
>
> If you mean fold_non_dependent_init that would be like fold_non_dependent_expr
> but with maybe_constant_init and not maybe_constant_value
And is_nondependent_static_init_expression, and different arguments to
cxx_eval_outermost_constant_expression, yes.
> then that would break e.g.
>
> const double d = 9.0; // missing constexpr
> constexpr double j = d; // should give error
>
> because maybe_constant_value checks is_nondependent_constant_expression, and
> "d" in the example above is not a constant expression, so we don't evaluate,
> and "d" stays "d", so require_constant_expression gives the error. On the
> other hand, maybe_constant_init checks is_nondependent_static_init_expression,
> and "d" is that, so we evaluate "d" to "9.0". Then require_constant_expression
> doesn't complain.
Ah, I see. You're right, let's stick with fold_non_dependent_expr.
Jason
next prev parent reply other threads:[~2018-03-01 18:57 UTC|newest]
Thread overview: 12+ messages / expand[flat|nested] mbox.gz Atom feed top
2018-02-27 19:13 Marek Polacek
2018-02-27 21:16 ` Jason Merrill
2018-02-28 14:32 ` Marek Polacek
2018-02-28 15:51 ` Jason Merrill
2018-02-28 21:19 ` Marek Polacek
2018-02-28 21:51 ` Jason Merrill
2018-03-01 13:17 ` Marek Polacek
2018-03-01 18:57 ` Jason Merrill [this message]
2018-03-01 21:40 ` Marek Polacek
2018-03-01 21:57 ` Jason Merrill
2018-03-02 18:18 ` Jason Merrill
2018-03-02 18:20 ` 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+2nE2vcBYbkLqJTHBYMyFXJA_j1qRay+JZCMyp_OVf7KGQ@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).