public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
From: Patrick Palka <ppalka@redhat.com>
To: Patrick Palka <ppalka@redhat.com>
Cc: gcc-patches@gcc.gnu.org, jason@redhat.com
Subject: Re: [PATCH] c++: cv-quals of dummy obj for non-dep memfn call [PR105637]
Date: Thu, 26 May 2022 14:57:44 -0400 (EDT)	[thread overview]
Message-ID: <527705e5-b69c-f1bd-f531-6bb43e10713b@idea> (raw)
In-Reply-To: <20220526183450.2331967-1-ppalka@redhat.com>

On Thu, 26 May 2022, Patrick Palka wrote:

> Here we expect the calls to BaseClass::baseDevice resolve to the second,
> third and fourth overloads respectively in light of the cv-qualifiers
> of 'this' in each case.  But ever since r12-6075-g2decd2cabe5a4f, the
> calls incorrectly resolve to the first overload at instantiation time.
> 
> This happens because the calls to BaseClass::baseDevice are all deemed
> non-dependent (ever since r7-755-g23cb72663051cd made us ignore the
> dependentness of 'this' when considering the dependence of a non-static
> memfn call), hence we end up checking the call ahead of time, using as
> the object argument a dummy object of type BaseClass.  Since this object
> argument is cv-unqualified, the calls incoherently resolve to the first
> overload of baseDevice.  Before r12-6075, this incorrect result would
> just get silently discarded and we'd end up redoing OR at instantiation
> time using 'this' as the object argument.  But after r12-6075, we now
> reuse this incorrect result at instantiation time.
> 
> This patch fixes this by making finish_call_expr request from
> maybe_dummy_object a cv-qualified object consistent with the cv-quals of
> 'this'.  That way, ahead of time OR using a dummy object will give us
> the right answer and we could safely reuse it at instantiation time.
> 
> NB: r7-755 is also the cause of the related issue PR105742.  Not sure
> if there's a fix that could resolve both PRs at once..
> 
> Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK
> for trunk/12?
> 
> 	PR c++/105637
> 
> gcc/cp/ChangeLog:
> 
> 	* semantics.cc (finish_call_expr): Pass a cv-qualified object
> 	type to maybe_dummy_object that is consistent with the
> 	cv-qualifiers of 'this' if available.
> 
> gcc/testsuite/ChangeLog:
> 
> 	* g++.dg/template/non-dependent23.C: New test.
> ---
>  gcc/cp/semantics.cc                           | 15 ++++++++---
>  .../g++.dg/template/non-dependent23.C         | 25 +++++++++++++++++++
>  2 files changed, 37 insertions(+), 3 deletions(-)
>  create mode 100644 gcc/testsuite/g++.dg/template/non-dependent23.C
> 
> diff --git a/gcc/cp/semantics.cc b/gcc/cp/semantics.cc
> index cd7a2818feb..1d9348c6cf1 100644
> --- a/gcc/cp/semantics.cc
> +++ b/gcc/cp/semantics.cc
> @@ -2802,16 +2802,25 @@ finish_call_expr (tree fn, vec<tree, va_gc> **args, bool disallow_virtual,
>  	[class.access.base] says that we need to convert 'this' to B* as
>  	part of the access, so we pass 'B' to maybe_dummy_object.  */
>  
> +      tree object_type = BINFO_TYPE (BASELINK_ACCESS_BINFO (fn));
>        if (DECL_MAYBE_IN_CHARGE_CONSTRUCTOR_P (get_first_fn (fn)))
>  	{
>  	  /* A constructor call always uses a dummy object.  (This constructor
>  	     call which has the form A::A () is actually invalid and we are
>  	     going to reject it later in build_new_method_call.)  */
> -	  object = build_dummy_object (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)));
> +	  object = build_dummy_object (object_type);
>  	}
>        else
> -	object = maybe_dummy_object (BINFO_TYPE (BASELINK_ACCESS_BINFO (fn)),
> -				     NULL);
> +	{
> +	  if (current_class_ref)
> +	    {
> +	      /* Make sure that if maybe_dummy_object gives us a dummy object,
> +		 it'll have the same cv-quals as '*this'.  */
> +	      int quals = cp_type_quals (TREE_TYPE (current_class_ref));
> +	      object_type = cp_build_qualified_type (object_type, quals);
> +	    }
> +	  object = maybe_dummy_object (object_type, NULL);
> +	}
>  
>        result = build_new_method_call (object, fn, args, NULL_TREE,
>  				      (disallow_virtual

Drat, this fix doesn't interact well with 'this'-capturing lambdas:

    struct BaseClass {
      void baseDevice();                // #1
      void baseDevice() const = delete; // #2
    };

    template<class T>
    struct TopClass : T {
      void failsToCompile() {
        [this] { BaseClass::baseDevice(); }();
      }
    };

    template struct TopClass<BaseClass>;

Here after the fix, we'd incorrectly select the const #2 overload at
template definition time because current_class_ref is the const 'this'
for the lambda rather than the non-const 'this' for TopClass..  I suppose
we need something like current_nonlambda_class_type for getting at the
innermost non-lambda 'this'?


> diff --git a/gcc/testsuite/g++.dg/template/non-dependent23.C b/gcc/testsuite/g++.dg/template/non-dependent23.C
> new file mode 100644
> index 00000000000..ef95c591b75
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/template/non-dependent23.C
> @@ -0,0 +1,25 @@
> +// PR c++/105637
> +
> +struct BaseClass {
> +  void baseDevice();                // #1
> +  void baseDevice() const;          // #2
> +  void baseDevice() volatile;       // #3
> +  void baseDevice() const volatile; // #4
> +};
> +
> +template<class T>
> +struct TopClass : T {
> +  void failsToCompile() const {
> +    BaseClass::baseDevice(); // should select #2, not #1
> +  }
> +
> +  void failsToCompile() volatile {
> +    BaseClass::baseDevice();  // should select #3, not #1
> +  }
> +
> +  void failsToCompile() const volatile {
> +    BaseClass::baseDevice();  // should select #4, not #1
> +  }
> +};
> +
> +template struct TopClass<BaseClass>;
> -- 
> 2.36.1.182.g6afdb07b7b
> 
> 


  reply	other threads:[~2022-05-26 18:57 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-05-26 18:34 Patrick Palka
2022-05-26 18:57 ` Patrick Palka [this message]
2022-05-26 20:39   ` Jason Merrill
2022-05-26 21:54     ` Patrick Palka
2022-05-27 13:57       ` Patrick Palka
2022-06-02 15:57         ` Patrick Palka
2022-06-02 19:44         ` Jason Merrill
2022-06-02 19:57           ` Patrick Palka
2022-06-02 20:30             ` Jason Merrill
2022-06-03 14:46               ` Patrick Palka
2022-06-03 14:53                 ` Jason Merrill
2022-06-03 15:04                   ` Patrick Palka
2022-06-03 15:16                     ` Jason Merrill
2022-06-03 15:22                       ` Marek Polacek
2022-06-03 16:04                         ` Patrick Palka

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=527705e5-b69c-f1bd-f531-6bb43e10713b@idea \
    --to=ppalka@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).