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 5883D3858034 for ; Wed, 12 Oct 2022 22:24:27 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 5883D3858034 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=1665613466; 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=5x3CtdU07sX4emoLeJzUwDYqqYrqnWBRipw3UzZBO3Q=; b=EgoUCkTAQDOFrm9xTzOORzAdxaKH1ZVTNvXuZXtXd2ZyTOMBgqBD8AL+UwK7FdxWi4OlFz EMd8K87txGh2iYXENkGFC1xUEaqFa6htEfFYH5ccR72L/YSMpBtDiP+QWmyBIibPJilwxU yv1gUEugLNvRgXy+Q2SFgp5eoQPI21A= Received: from mail-qt1-f199.google.com (mail-qt1-f199.google.com [209.85.160.199]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_128_GCM_SHA256) id us-mta-606-ih5dc0n6MKiGKpWikBiRBg-1; Wed, 12 Oct 2022 18:24:26 -0400 X-MC-Unique: ih5dc0n6MKiGKpWikBiRBg-1 Received: by mail-qt1-f199.google.com with SMTP id f19-20020ac84713000000b00397692bdaecso9269245qtp.22 for ; Wed, 12 Oct 2022 15:24:26 -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=5x3CtdU07sX4emoLeJzUwDYqqYrqnWBRipw3UzZBO3Q=; b=Ne0SVSSE0946Slh3kr6bjEmkv1h4lXYdNf7NDfy8npE650sCv7QsjEc2TKGKbAoViK u19VBUFTnb4JVrbtNLuDIUzQpOq93+6wrER6gCHRbDdd4waDfo5wisUcPj14LHF8DTOc mvo+5g6XN9+e3KISobS5lMzsNR1oK+KL22kNkAR/gitNBd1l8rRYviinNypQSjBN8xMU iPFw0JytoXeLeOf5YoElpHr7YBFpqLPy1eDb7sl49uJgyUFxWwl32bY6JVbgPU774nrH ahcoBSH3qZT5onznziwWxfBu9CFk2UCD6C8Ooc59Yv2Buxtjt8y7vGp9lfIaSUsi6YhJ 8WYA== X-Gm-Message-State: ACrzQf2kWfAP+FMdT9V+qiUtB1rFo7ZU/EHctV29NMcEaN+uV3FNFtV4 /JVTZ/uMk30N0Df26PFRLpSqjEFWZO6/KiyoGFql0bdWOl88jxZM4xGXNPGwEMiHEzUxjyBHyUN /XRa0qWMHLvRCzHUOTQ== X-Received: by 2002:ad4:4eec:0:b0:4b1:9859:b74b with SMTP id dv12-20020ad44eec000000b004b19859b74bmr25117548qvb.102.1665613465580; Wed, 12 Oct 2022 15:24:25 -0700 (PDT) X-Google-Smtp-Source: AMsMyM4R5A+TypLnUtW0HsJBgwEnzhk4rf6WrZwmkaysCc9zmG7h1GdE87iD08H+ZQX1JHLpbN6IQg== X-Received: by 2002:ad4:4eec:0:b0:4b1:9859:b74b with SMTP id dv12-20020ad44eec000000b004b19859b74bmr25117537qvb.102.1665613465261; Wed, 12 Oct 2022 15:24:25 -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 f14-20020a05620a280e00b006be8713f742sm16613001qkp.38.2022.10.12.15.24.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 12 Oct 2022 15:24:24 -0700 (PDT) Date: Wed, 12 Oct 2022 18:24:22 -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 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?

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