public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Marek Polacek <polacek@redhat.com>
To: Jason Merrill <jason@redhat.com>
Cc: GCC Patches <gcc-patches@gcc.gnu.org>
Subject: Re: [PATCH] c++: explicit inst of template method not generated [PR110323]
Date: Fri, 15 Mar 2024 13:48:48 -0400	[thread overview]
Message-ID: <ZfSKAIz_ozFVUBHN@redhat.com> (raw)
In-Reply-To: <f4e7791f-aa72-4747-a6a1-a0683b9a1db7@redhat.com>

On Thu, Mar 14, 2024 at 03:39:04PM -0400, Jason Merrill wrote:
> On 3/8/24 12:02, Marek Polacek wrote:
> > Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk?
> > 
> > -- >8 --
> > Consider
> > 
> >    constexpr int VAL = 1;
> >    struct foo {
> >        template <int B>
> >        void bar(typename std::conditional<B==VAL, int, float>::type arg) { }
> >    };
> >    template void foo::bar<1>(int arg);
> > 
> > where we since r11-291 fail to emit the code for the explicit
> > instantiation.  That's because cp_walk_subtrees/TYPENAME_TYPE now
> > walks TYPE_CONTEXT ('conditional' here) as well, and in a template
> > finds the B==VAL template argument.  VAL is constexpr, which implies const,
> > which in the global scope implies static.  constrain_visibility_for_template
> > then makes "struct conditional<(B == VAL), int, float>" non-TREE_PUBLIC.
> > Then symtab_node::needed_p checks TREE_PUBLIC, sees it's 0, and we don't
> > emit any code.
> > 
> > I thought the fix would be some ODR-esque check to not consider
> > constexpr variables/fns that are used just for their value.  But
> > it turned out to be tricky.  For instance, we can't skip
> > determine_visibility in a template; we can't even skip it for value-dep
> > expressions.  For example, no-linkage-expr1.C has
> > 
> >    using P = struct {}*;
> >    template <int N>
> >    void f(int(*)[((P)0, N)]) {}
> > 
> > where ((P)0, N) is value-dep, but N is not relevant here: we have to
> > ferret out the anonymous type.  When instantiating, it's already gone.
> 
> Hmm, how is that different from the B == VAL case?  In both cases we're
> naming an internal entity that gets folded away.
> 
> I guess the difference is that B == VAL falls under the special allowance in
> https://eel.is/c++draft/basic.def.odr#14.5.1 because it's a constant used as
> a prvalue, and therefore is not odr-used under
> https://eel.is/c++draft/basic.def.odr#5.2
> 
> So I would limit this change to decl_constant_var_p.  Really we should also
> be checking that the lvalue-rvalue conversion is applied, but that's more
> complicated.

Thanks.  My previous version had it, but it didn't handle

  static constexpr int getval () { return 1; }

  template <int B>
  void baz(typename conditional<B == getval (), int, float>::type arg) { }

I'd say that "getval()" is one of "manifestly constant-evaluated expressions that
are not value-dependent", so it should be treated the same as B == VAL.  I
don't know if this is important to handle.  Do you want me to poke further or
should we just go with decl_constant_var_p and leave it at that for now?

Marek


  reply	other threads:[~2024-03-15 17:48 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-08 17:02 Marek Polacek
2024-03-14 19:39 ` Jason Merrill
2024-03-15 17:48   ` Marek Polacek [this message]
2024-03-19  1:10     ` Jason Merrill
2024-03-19 19:30       ` [PATCH v2] " Marek Polacek
2024-03-21  3:29         ` Jason Merrill

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=ZfSKAIz_ozFVUBHN@redhat.com \
    --to=polacek@redhat.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=jason@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).