From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id A1B4938515CB for ; Wed, 12 Oct 2022 22:38:14 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org A1B4938515CB Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1665614294; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=wdZm89txpFxeOtwkoT9uW7RfLGZDOLhaqFlYLT5uKDA=; b=ZLvm/snkohdJ9FbIwV4/+eylhZVNmSri/BPX6T8XatItp94LrrDITAqfl1WmUMHUiwkVkb U3PnA4pr2PaFQQ3gDsxyFX5j6foiai61slR0lOIkXq25G+ykc8lJOjVf++dbpxhIZuHTFb zhKJaD8QEbrR9Q+6B6jOvjeE4+eDRqs= Received: from mail-qv1-f72.google.com (mail-qv1-f72.google.com [209.85.219.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-114-3cdDa1KqMEuWo7fTn0NUMA-1; Wed, 12 Oct 2022 18:38:13 -0400 X-MC-Unique: 3cdDa1KqMEuWo7fTn0NUMA-1 Received: by mail-qv1-f72.google.com with SMTP id kr13-20020a0562142b8d00b004b1d5953a2cso216034qvb.3 for ; Wed, 12 Oct 2022 15:38:13 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=cc:to:subject:message-id:date:from:in-reply-to:references :mime-version:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=wdZm89txpFxeOtwkoT9uW7RfLGZDOLhaqFlYLT5uKDA=; b=0WLuqBa7T1TDlKf5zfU4o6o9mQGDa+Plj9ceMEcoLd7FNVG/5K7xvRi39jPxVOlnL5 o7f+XM/+MgtOZZjOfQSFvSMpn6QNJn6qpv//aoy3XcWWsl9fzLoNdZEPxspDJnysvCDh CUdDjN6YQEW/1L63dJg+InpU0H9URPnseiLGBKV/s4/BOLL1w+BxBwxJdTDaEA5gj38a B7Tx4SUnJpatPK3Fgn1vl31tfU8D0YbKiPiEake9FKtaOnjcQYhWtSXsshqNCwIxThcX TVS4zAiVFZtOnFmX53lSxiQBA3ukshd4k4iTP3U1Y9jlAO3ya8gnv3CE4wX3Rp8bHida 1Aaw== X-Gm-Message-State: ACrzQf2RGI9Q0q7J8+4H76nNTZwopDwcpsyTjU1iCgUsBGsbvF0OL+fr qgnCs+ZEU1J/4N/08JAAswvQWzOaRa5ku6UI1YWsM2JAuCYhs1zu7LT9wmeXxJIdHqMrpOzWmaZ CTEfpOTv6yGMli/1VCPm8waaKsfI1ZfcXUw== X-Received: by 2002:a05:620a:4709:b0:6ce:cb19:3eb2 with SMTP id bs9-20020a05620a470900b006cecb193eb2mr21851527qkb.272.1665614292721; Wed, 12 Oct 2022 15:38:12 -0700 (PDT) X-Google-Smtp-Source: AMsMyM7iYSIwsrQCog7bpR5uwahSrr6qLKQbS/JX0zFHjSyFXAc/J9jcKT0fvDPDuCVHr91M84oOySZ3Fg42oeBHuaE= X-Received: by 2002:a05:620a:4709:b0:6ce:cb19:3eb2 with SMTP id bs9-20020a05620a470900b006cecb193eb2mr21851512qkb.272.1665614292427; Wed, 12 Oct 2022 15:38:12 -0700 (PDT) MIME-Version: 1.0 References: In-Reply-To: From: Jonathan Wakely Date: Wed, 12 Oct 2022 23:38:01 +0100 Message-ID: Subject: Re: [wwwdocs] porting_to: Two-stage overload resolution for implicit move removed To: Marek Polacek Cc: GCC Patches , Jason Merrill X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="UTF-8" X-Spam-Status: No, score=-12.6 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,KAM_SHORT,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_NONE,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On Wed, 12 Oct 2022 at 23:24, Marek Polacek wrote: > > On Wed, Oct 12, 2022 at 09:50:36PM +0100, Jonathan Wakely wrote: > > On Wed, 12 Oct 2022 at 20:39, Marek Polacek wrote: > > > > > > As I promised in > > > , > > > I'd like to update our GCC 13 porting_to.html with the following note. > > > > > > Does this look OK to commit? Thanks, > > > > > > diff --git a/htdocs/gcc-13/porting_to.html b/htdocs/gcc-13/porting_to.html > > > index 84a00f21..243ed29d 100644 > > > --- a/htdocs/gcc-13/porting_to.html > > > +++ b/htdocs/gcc-13/porting_to.html > > > @@ -42,5 +42,57 @@ be included explicitly when compiled with GCC 13: > > > > > > > > > > > > +

Two-stage overload resolution for implicit move removed

> > > +

> > > +GCC 13 removed the two-stage overload resolution when performing > > > +implicit move, whereby the compiler does two separate overload resolutions: > > > +one treating the operand as an rvalue, and then (if that resolution fails) > > > +another one treating the operand as an lvalue. In the standard this was > > > +introduced in C++11 and implemented in gcc in > > > + > > > +r251035. In > > > + > > > +r11-2412, the fallback overload resolution was disabled in C++20 (but > > > +not in C++17). Then C++23 P2266 > > > +removed the fallback overload resolution, and changed the implicit move > > > +rules once again. > > > +

> > > +

> > > +The two overload resolutions approach was complicated and quirky, so users > > > +should transition to the newer model. This change means that code that > > > +previously didn't compile in C++17 will now compile, for example:

> > > + > > > +

> > > +   struct S1 { S1(S1 &&); };
> > > +   struct S2 : S1 {};
> > > +
> > > +   S1
> > > +   f (S2 s)
> > > +   {
> > > +     return s; // OK, derived-to-base, use S1::S1(S1&&)
> > > +   }
> > > +
> > > + > > > +

> > > +And conversely, code that used to work in C++17 may not compile anymore: > > > +

> > > + > > > +

> > > +   struct W {
> > > +     W();
> > > +   };
> > > +
> > > +   struct F {
> > > +     F(W&);
> > > +     F(W&&) = delete;
> > > +   };
> > > +
> > > +   F fn ()
> > > +   {
> > > +     W w;
> > > +     return w; // use w as rvalue -> use of deleted function F::F(W&&)
> >
> > Deleted move constructors are an abomination, and should never occur
> > in real code. I'm not sure using one even in an example like this
> > should be encouraged. The example added by P2266 to Annex D is more
> > realistic (and actually broke a libstdc++ test):
> >
> > X& foo(X&& x) { return x; }
>
> Right, but this code still compiles in C++17, it only fails to compile
> in C++23.  The previous example now doesn't compile even in C++17.  So
> how about this improved patch which makes it clear that code with
> deleted move constructors should never occur in practice, and adds a new
> note, specifically about P2266 and the code you showed?

Doh, I've just realised that F(W&&) isn't a move ctor at all. For some
reason I read the example as F(F&&).

I think your original example is fine, and the note would just be
confusing (because it's not a deleted move ctor!)


>
> Thanks for taking a look,
>
> diff --git a/htdocs/gcc-13/porting_to.html b/htdocs/gcc-13/porting_to.html
> index 84a00f21..a9991e8b 100644
> --- a/htdocs/gcc-13/porting_to.html
> +++ b/htdocs/gcc-13/porting_to.html
> @@ -42,5 +42,71 @@ be included explicitly when compiled with GCC 13:
>  
>  
>
> +

Implicit move rules change

> +

> +GCC 13 implements C++23 P2266 which > +simplified the rules for implicit move. As a consequence, valid C++20 > +code that relies on a returned id-expression's being an lvalue > +may change behavior or fail to compile in C++23. For example:

> + > +

> +   decltype(auto) f(int&& x) { return (x); }  // returns int&&; previously returned int&
> +   int& g(int&& x) { return x; }  // ill-formed; previously well-formed
> +
> + > +

Two-stage overload resolution for implicit move removed

> +

GCC 13 removed the two-stage overload resolution when performing > +implicit move, whereby the compiler does two separate overload resolutions: > +one treating the operand as an rvalue, and then (if that resolution fails) > +another one treating the operand as an lvalue. In the standard this was > +introduced in C++11 and implemented in gcc in > + > +r251035. In > + > +r11-2412, the fallback overload resolution was disabled in C++20 (but > +not in C++17). Then C++23 P2266 > +removed the fallback overload resolution, and changed the implicit move > +rules once again.

> + > +

The two overload resolutions approach was complicated and quirky, so users > +should transition to the newer model. This change means that code that > +previously didn't compile in C++17 will now compile, for example:

> + > +

> +   struct S1 { S1(S1 &&); };
> +   struct S2 : S1 {};
> +
> +   S1
> +   f (S2 s)
> +   {
> +     return s; // OK, derived-to-base, use S1::S1(S1&&)
> +   }
> +
> + > +

Conversely, code that used to work in C++17 may not compile anymore. > +For example, the following example used to compile in C++11...17 because > +we performed two separate overload resolutions: one treating the operand > +as an rvalue, and then (if that resolution failed) another one treating > +the operand as an lvalue.
> +NB: this example is contrived because deleted move > +constructors should not occur in real code.

> + > +

> +   struct W {
> +     W();
> +   };
> +
> +   struct F {
> +     F(W&);
> +     F(W&&) = delete;
> +   };
> +
> +   F fn ()
> +   {
> +     W w;
> +     return w; // use w as rvalue -> use of deleted function F::F(W&&)
> +   }
> +
> + > > >