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.129.124]) by sourceware.org (Postfix) with ESMTPS id A47E53858C56 for ; Wed, 12 Oct 2022 22:44:36 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org A47E53858C56 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=1665614676; 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=MKLQk5TQQ2ZXlxzq5RW+pTSzjI00sAloxDcJzkajgEk=; b=KU1nKuuGXv6YfIFR93m6NqT9mkgDh/AWLiUGD4JATstTl8RKHB4jXaRN6EPkkDnVwf6VUR NXtvB7PC2ygn8scNm7qk2N4z5ivZU6MbG6vVQNPfD3JOxVVpX/n7VR8slGcUhKEcLdFaiF p/nUAtifAoppDdook54JR+s+7imjtDA= Received: from mail-qt1-f200.google.com (mail-qt1-f200.google.com [209.85.160.200]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-595-QUJryYwaPKuCUXEtTOjiQQ-1; Wed, 12 Oct 2022 18:44:34 -0400 X-MC-Unique: QUJryYwaPKuCUXEtTOjiQQ-1 Received: by mail-qt1-f200.google.com with SMTP id s14-20020a05622a1a8e00b00397eacd9c1aso8613417qtc.21 for ; Wed, 12 Oct 2022 15:44:34 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=user-agent:in-reply-to:content-disposition:mime-version:references :message-id:subject:cc:to:from:date:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=MKLQk5TQQ2ZXlxzq5RW+pTSzjI00sAloxDcJzkajgEk=; b=HbLLGk1iKaPu91wKFKz5BjdKv3UuqAAYusUI2o5Ko26JT7SPwDRSVZiqsA6r6Cz6Kj V4Fe95yT8lKaHPHgzSFUvcGixtV2hFnRiTwZWVR2TxGchy1XlefGsaQ6QdzpDRMwUXnY Zp235jc9avIRR0uR62DVo3XDCRdi9l1HEu7KtEiEN6/R76ME4vPnlUN02SHRcyZc4lwb l7Ldmsqe1BccSbWQih6U+u7Zp5B4OJ1GDW1GGVFT9bugEAA1zuOxEJlMShzSOaStha/1 VgYpph93ThmziJclpz55p7TdlO/RfcfoofFa1dotfDl8dHYUwM1QuKuaVTIilO1t4Ve6 k4hw== X-Gm-Message-State: ACrzQf2Arhxajgr8D2PZ6uFycxzYgUvrB4tGTbyawbcaknlKZq0ogOR2 FtlGaI0RinapzqsfGxsx1d9/Yo9RThQcW1GvJtxlPcUE52rb7h3/JWJ5LUPpRGa5/BSG4WE1ge2 CGOOZD0NHW2EJpvByMQ== X-Received: by 2002:a0c:802c:0:b0:4b3:ef3d:9046 with SMTP id 41-20020a0c802c000000b004b3ef3d9046mr17513777qva.109.1665614673869; Wed, 12 Oct 2022 15:44:33 -0700 (PDT) X-Google-Smtp-Source: AMsMyM6d8o5CeU7E5KD+ZbNafmh0Jlxe/At6ByfsO+aPxrUNhGIl0DPdXQekI085TUwLvNdABNG/DA== X-Received: by 2002:a0c:802c:0:b0:4b3:ef3d:9046 with SMTP id 41-20020a0c802c000000b004b3ef3d9046mr17513769qva.109.1665614673647; Wed, 12 Oct 2022 15:44:33 -0700 (PDT) Received: from redhat.com (2603-7000-9500-2e39-0000-0000-0000-1db4.res6.spectrum.com. [2603:7000:9500:2e39::1db4]) by smtp.gmail.com with ESMTPSA id s4-20020a05620a254400b006bbc09af9f5sm16652031qko.101.2022.10.12.15.44.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 12 Oct 2022 15:44:33 -0700 (PDT) Date: Wed, 12 Oct 2022 18:44:31 -0400 From: Marek Polacek To: Jonathan Wakely Cc: GCC Patches , Jason Merrill Subject: Re: [wwwdocs] porting_to: Two-stage overload resolution for implicit move removed Message-ID: References: MIME-Version: 1.0 In-Reply-To: User-Agent: Mutt/2.2.7 (2022-08-07) X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset=us-ascii Content-Disposition: inline X-Spam-Status: No, score=-12.4 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, Oct 12, 2022 at 11:38:01PM +0100, Jonathan Wakely wrote: > 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&&).

And so did I while adding the note :[.

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

I think I'll go ahead with this, then (I've removed the NB).  Thanks!

diff --git a/htdocs/gcc-13/porting_to.html b/htdocs/gcc-13/porting_to.html
index 84a00f21..ccd3f08f 100644
--- a/htdocs/gcc-13/porting_to.html
+++ b/htdocs/gcc-13/porting_to.html
@@ -42,5 +42,69 @@ 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.
+ +


+   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&&)
+   }
+
+