220 22584 <CADqyrC3_i1tLng=s2jtzmQw6uwOD-=RBQjvM_MOL-05EPA=Ggg@mail.gmail.com> article
Path: news.gmane.org!not-for-mail
From: Jonas Persson <l.j.persson@gmail.com>
Newsgroups: gmane.comp.lang.c++.isocpp.proposals
Subject: Re: Unreachable attribute.
Date: Fri, 13 Nov 2015 08:24:52 +0100
Lines: 193
Approved: news@gmane.org
Message-ID: <CADqyrC3_i1tLng=s2jtzmQw6uwOD-=RBQjvM_MOL-05EPA=Ggg@mail.gmail.com>
References: <b0c0e9f0-444a-4237-9fef-6cacf00127a3@isocpp.org>
	<2573561.klGZ6ZHjC3@lastique-n550jv>
	<CAB+4KHLQeGFTTnwBcG4SCctmHOMNBgen9Hs0PJMUUY3bzb4oXw@mail.gmail.com>
	<4611855.M52eYPluZF@lastique-n550jv>
	<CAB+4KHK4q5ZNGXn7YqFC6r+B9UYZO1U0PfCyUweH5+d8cxqm4g@mail.gmail.com>
	<CAFdMc-1dvPHJc4Jd+OT5dA6WjuXe9MFJ-HaqCf75D1Yw2jGy9A@mail.gmail.com>
	<CAOHCbiv2BtV-NR_wLe73VfqbZ-VT3jhp9JAdnQW5HHx=QnQuSA@mail.gmail.com>
	<CAB+4KHLDOk7FkwKQw4jy37Gm9yWOS5NWAwjJv9xSrdrbMu2Y0w@mail.gmail.com>
	<ee704d43-9fa7-4e5d-8822-71e46738f40b@isocpp.org>
	<CANh8DE=S8WEqMwQ7+kJqn9N4Lz9AjuW_XQ=MgAUFDUaOd9xkNg@mail.gmail.com>
	<c4849a3f-1b45-4306-abfd-41a5aa724bf9@isocpp.org>
	<A7D693EC-9E37-497A-AF8C-5FA607393D1E@gmail.com>
	<d37565ac-e09f-46c0-addb-ba7155988da6@isocpp.org>
	<CADqyrC0AcEhNmgDLQs7mhWWv=rKkp89tDUhiKYnXH1PmzbsB8Q@mail.gmail.com>
	<3705e2b3-76ac-4a55-b4d3-9252c6462c50@isocpp.org>
	<CADqyrC1s7bvqQ0DA6h-D45Bh5s80sXhdmYoYBwty+-8W9vzk0w@mail.gmail.com>
	<CAOHCbiuLHsfbV5s+y2zKRTYh6WwoAJAaH3n9f2VW8QBYg2zpkQ@mail.gmail.com>
	<CADqyrC1uEDARcNrAXbdkYmGZx-aNOb8=HBVsbx1EZZzBWqF2fA@mail.gmail.com>
	<CAOHCbivAm7dWXhyMh71RaJw+TCXcQ8k8M1xBYTA8K6xFye=OYA@mail.gmail.com>
	<CADqyrC2BQk3BqffMPf-ky7Pwjo8O-iu0R0AYqx7LexhKHO6PJw@mail.gmail.com>
	<305866a5-c79d-49f7-ad82-0e359039a5b4@isocpp.org>
	<CADqyrC0DfduguMSFGFE665gtcC1p-cHhYvVr0c7d_EEgVP+Fmg@mail.gmail.com>
	<CAJnLdOaXDL-137kossOK-PtUKodCt7JVX-zVOoVWjKd_9EgE7g@mail.gmail.com>
	<CADqyrC1991G4T0zBzoWg2KHN0X-2kchSXYSrGXX-HbhA+C0wLA@mail.gmail.com>
	<CAJnLdOa8aoJPW_=+NuzNJWcJquKrKHGe-wW2nf5Lwg+_V=9OCQ@mail.gmail.com>
	<CADqyrC1e-8WHWJdtcjjwRE06QZd9gwdRN7DiOeewPjJP0ptz9A@mail.gmail.com>
	<CAJnLdObWcE0acY4ch9P9eFVKWxQ-ST-8g+xhmYbR70B10C6A9A@mail.gmail.com>
Reply-To: std-proposals@isocpp.org
NNTP-Posting-Host: plane.gmane.org
Mime-Version: 1.0
Content-Type: multipart/alternative; boundary=001a1144334696786a052466f431
X-Trace: ger.gmane.org 1447399497 18090 80.91.229.3 (13 Nov 2015 07:24:57 GMT)
X-Complaints-To: usenet@ger.gmane.org
NNTP-Posting-Date: Fri, 13 Nov 2015 07:24:57 +0000 (UTC)
To: std-proposals@isocpp.org
Original-X-From: std-proposals+bncBDLJBMEUU4NBBRNAS2ZAKGQE7J4T24A@isocpp.org Fri Nov 13 08:24:57 2015
Return-path: <std-proposals+bncBDLJBMEUU4NBBRNAS2ZAKGQE7J4T24A@isocpp.org>
Envelope-to: gclcip-std-proposals@m.gmane.org
Original-Received: from mail-lf0-f71.google.com ([209.85.215.71])
	by plane.gmane.org with esmtp (Exim 4.69)
	(envelope-from <std-proposals+bncBDLJBMEUU4NBBRNAS2ZAKGQE7J4T24A@isocpp.org>)
	id 1Zx8j9-0003Ae-4U
	for gclcip-std-proposals@m.gmane.org; Fri, 13 Nov 2015 08:24:55 +0100
Original-Received: by lfdo63 with SMTP id o63sf31569739lfd.2
        for <gclcip-std-proposals@m.gmane.org>; Thu, 12 Nov 2015 23:24:54 -0800 (PST)
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=isocpp_org.20150623.gappssmtp.com; s=20150623;
        h=mime-version:in-reply-to:references:date:message-id:subject:from:to
         :content-type:x-original-sender:x-original-authentication-results
         :reply-to:precedence:mailing-list:list-id:x-spam-checked-in-group
         :list-post:list-help:list-archive:list-subscribe:list-unsubscribe;
        bh=rBx/dinVb47i7aGYq/SDCeZsLNrKxhIlELWhWg+PatA=;
        b=RUp4F84/GTyNkAEYM3Gu4N4v4FwZObkfJZ0Mq2hVIewp3vfxcecLWeePi9QHkymWrS
         irnKqa8EtBcKLM2iloxJVSHJodPpkrQ6Z6gENVJCnO9g8zQLA8BnWDDtSbnxdUFpVh3u
         l9+i9rVN+3VRKw57hMvbtyjViXYU2hY2JX2H0cb74KQ8p5+HmEYX6OlRUkAtdotpcdvi
         dGOenJ61jdjLmmqR9bGGJt9AMtiA2FKrrFyfaDk1m6Qfh0M5+q5ixV2oQXpv0nFAbmzF
         7G+rzDKQBjXA4GF9ELkDVTgK9KDHLORnGJWIo4FWXMXYHqpBgpaoOQoYo04RMQJyFJKW
         bNAA==
X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed;
        d=1e100.net; s=20130820;
        h=x-gm-message-state:mime-version:in-reply-to:references:date
         :message-id:subject:from:to:content-type:x-original-sender
         :x-original-authentication-results:reply-to:precedence:mailing-list
         :list-id:x-spam-checked-in-group:list-post:list-help:list-archive
         :list-subscribe:list-unsubscribe;
        bh=rBx/dinVb47i7aGYq/SDCeZsLNrKxhIlELWhWg+PatA=;
        b=EMrYPQ8N5K0CrA5ubbfYDFujA7d2DdfGoYBbm3oCMFqKc2LFIVJCs996t2bSbGMTfs
         eCZblmijjt7GKtFz5nwoB3hQe/SRP6C60ALPXBdcbwR5iW1+0mY4dWIt+KfYyjL+cJg9
         HE/OqrHfdV7MyuWytqaGXOK4hU2Nksh/MjWjhyGfmcmrGImCWbFy1hf0r+6gNu+ZDImx
         muyby5Q1RvR3u1gF70GI3DhGkO87oRZY3atLSgAYcos1RfGmjQbtWuACYkpFDo0Ztzls
         CvPdrNXBLCheZ5u2JZsYyZyNUCr6qpTbzKDanuXErRoweLJenUA11/1Fy3cdcBxip5yb
         wvyA==
X-Gm-Message-State: ALoCoQlrLylQLHHLORENHQHDBqX5cN3vQWfDytSR1bG2bLPZkklJOXi21LNo1oiUPDM8WkTfzWZP
X-Received: by 10.112.181.194 with SMTP id dy2mr2937899lbc.11.1447399494631;
        Thu, 12 Nov 2015 23:24:54 -0800 (PST)
X-BeenThere: std-proposals@isocpp.org
Original-Received: by 10.28.18.198 with SMTP id 189ls89088wms.11.gmail; Thu, 12 Nov
 2015 23:24:52 -0800 (PST)
X-Received: by 10.28.9.204 with SMTP id 195mr2041065wmj.88.1447399492943;
        Thu, 12 Nov 2015 23:24:52 -0800 (PST)
Original-Received: from mail-wm0-x230.google.com (mail-wm0-x230.google.com. [2a00:1450:400c:c09::230])
        by mx.google.com with ESMTPS id m79si3742120wmg.42.2015.11.12.23.24.52
        for <std-proposals@isocpp.org>
        (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128);
        Thu, 12 Nov 2015 23:24:52 -0800 (PST)
Received-SPF: pass (google.com: domain of l.j.persson@gmail.com designates 2a00:1450:400c:c09::230 as permitted sender) client-ip=2a00:1450:400c:c09::230;
Original-Received: by wmec201 with SMTP id c201so18062472wme.1
        for <std-proposals@isocpp.org>; Thu, 12 Nov 2015 23:24:52 -0800 (PST)
X-Received: by 10.28.170.65 with SMTP id t62mr2078438wme.1.1447399492316; Thu,
 12 Nov 2015 23:24:52 -0800 (PST)
Original-Received: by 10.28.157.148 with HTTP; Thu, 12 Nov 2015 23:24:52 -0800 (PST)
In-Reply-To: <CAJnLdObWcE0acY4ch9P9eFVKWxQ-ST-8g+xhmYbR70B10C6A9A@mail.gmail.com>
X-Original-Sender: l.j.persson@gmail.com
X-Original-Authentication-Results: mx.google.com;       spf=pass (google.com:
 domain of l.j.persson@gmail.com designates 2a00:1450:400c:c09::230 as
 permitted sender) smtp.mailfrom=l.j.persson@gmail.com;       dkim=pass
 header.i=@gmail.com;       dmarc=pass (p=NONE dis=NONE) header.from=gmail.com
Precedence: list
Mailing-list: list std-proposals@isocpp.org; contact std-proposals+owners@isocpp.org
List-ID: <std-proposals.isocpp.org>
X-Spam-Checked-In-Group: std-proposals@isocpp.org
X-Google-Group-Id: 399137483710
List-Post: <http://groups.google.com/a/isocpp.org/group/std-proposals/post>, <mailto:std-proposals@isocpp.org>
List-Help: <http://support.google.com/a/isocpp.org/bin/topic.py?topic=25838>, <mailto:std-proposals+help@isocpp.org>
List-Archive: <http://groups.google.com/a/isocpp.org/group/std-proposals/>
List-Subscribe: <http://groups.google.com/a/isocpp.org/group/std-proposals/subscribe>,
 <mailto:std-proposals+subscribe@isocpp.org>
List-Unsubscribe: <mailto:googlegroups-manage+399137483710+unsubscribe@googlegroups.com>,
 <http://groups.google.com/a/isocpp.org/group/std-proposals/subscribe>
Xref: news.gmane.org gmane.comp.lang.c++.isocpp.proposals:22584
Archived-At: <http://permalink.gmane.org/gmane.comp.lang.c++.isocpp.proposals/22584>

--001a1144334696786a052466f431
Content-Type: text/plain; charset=UTF-8

On Fri, Nov 13, 2015 at 12:45 AM, 'Edward Catmur' via ISO C++ Standard -
Future Proposals <std-proposals@isocpp.org> wrote:

> On Thu, Nov 12, 2015 at 11:20 PM, Jonas Persson <l.j.persson@gmail.com>
> wrote:
>
>>
>>
>> On Thu, Nov 12, 2015 at 11:52 PM, 'Edward Catmur' via ISO C++ Standard -
>> Future Proposals <std-proposals@isocpp.org> wrote:
>>
>>>
>>> On 12 Nov 2015 22:42, "Jonas Persson" <l.j.persson@gmail.com> wrote:
>>> >
>>> >
>>> > In this case UB will not give you any better performance. If you get
>>> UB on the caller side of a precondition, it is becase the code is
>>> incorrect. If that causes the compiler to fail, you can remove the failing
>>> code, giving you the same as UB code pruning would. But with fewer subtle
>>> bugs.
>>>
>>> True, for hand-written code. But invalid or unreachable code paths can
>>> easily crop up in template code, macro expansions, platform-, library- or
>>> configuration-dependent code, generated code, and in edge cases such as
>>> loop or recursion terminating conditions. Why should we have to contort our
>>> code to remove the failing code when the compiler can easily do it for us?
>>>
>> Note that I'm not talking about disallowing UB in general here, only for
>> precondition callers.
>> The reason to make the ill-formed is that
>> 1) the primary reason to use contracts is to catch misuse. Here we are
>> detecting misuse, but we are not telling.
>> 2) it is not evident from reading the code that it will fail or be
>> pruned. Only the compiler knows.
>> 3) We cannot be sure to catch this error by rebuilding in debug mode or
>> with sanitisers, becase any change to the compile process might cause
>> changes in inlining or other things so that the
>> compiler fails to figure out the failed precondition at compile time.
>>
>
> Making precondition failure ill-formed excludes far too much otherwise
> well-formed code:
>
> void print_column(string s, int width) {
>     for (int i = 0; i != width; ++i)
>         cout << ((i < s.size()) ? s[i] : ' ');  // X
> }
> print_column("hello", 10);
>
> The compiler can prove that the program reaches line *X* with *i = 5*.
> But s[5] is a precondition violation! Do you really want this code to be
> ill-formed?
>

How is that precondition violation?  If operator[] has a precodition it
should be i < size(), which is what you are checking.

On the other hand:
void print_column(string s, int width) {
    for (int i = 0; i != width; ++i)
        cout << s[i];  // X
}
print_column("hello", 10);

would be a violation and I would rather have the compile fail than an
access violation or no output at runtime.
Properly fixing that with

void print_column(string s, int width) [expects:width<s.size()] {
    for (int i = 0; i != width; ++i)
        cout << s[i];  // X
}
print_column("hello", 10);

would let the compiler optimize the print_column function and tell you,
hopefully at compile time, that your call to print_column is wrong.

  / Jonas

-- 

--- 
You received this message because you are subscribed to the Google Groups "ISO C++ Standard - Future Proposals" group.
To unsubscribe from this group and stop receiving emails from it, send an email to std-proposals+unsubscribe@isocpp.org.
To post to this group, send email to std-proposals@isocpp.org.
Visit this group at http://groups.google.com/a/isocpp.org/group/std-proposals/.

--001a1144334696786a052466f431
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: quoted-printable

<div dir=3D"ltr"><br><div class=3D"gmail_extra"><br><div class=3D"gmail_quo=
te">On Fri, Nov 13, 2015 at 12:45 AM, &#39;Edward Catmur&#39; via ISO C++ S=
tandard - Future Proposals <span dir=3D"ltr">&lt;<a href=3D"mailto:std-prop=
osals@isocpp.org" target=3D"_blank">std-proposals@isocpp.org</a>&gt;</span>=
 wrote:<br><blockquote class=3D"gmail_quote" style=3D"margin:0px 0px 0px 0.=
8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-st=
yle:solid;padding-left:1ex"><div dir=3D"ltr"><span class=3D"">On Thu, Nov 1=
2, 2015 at 11:20 PM, Jonas Persson <span dir=3D"ltr">&lt;<a href=3D"mailto:=
l.j.persson@gmail.com" target=3D"_blank">l.j.persson@gmail.com</a>&gt;</spa=
n> wrote:<br></span><div class=3D"gmail_extra"><div class=3D"gmail_quote"><=
span class=3D""><blockquote class=3D"gmail_quote" style=3D"margin:0px 0px 0=
px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-le=
ft-style:solid;padding-left:1ex"><div dir=3D"ltr"><br><div class=3D"gmail_e=
xtra"><br><div class=3D"gmail_quote">On Thu, Nov 12, 2015 at 11:52 PM, &#39=
;Edward Catmur&#39; via ISO C++ Standard - Future Proposals <span dir=3D"lt=
r">&lt;<a href=3D"mailto:std-proposals@isocpp.org" target=3D"_blank">std-pr=
oposals@isocpp.org</a>&gt;</span> wrote:<br><blockquote class=3D"gmail_quot=
e" style=3D"margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-colo=
r:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><span><p dir=
=3D"ltr"><br>
On 12 Nov 2015 22:42, &quot;Jonas Persson&quot; &lt;<a href=3D"mailto:l.j.p=
ersson@gmail.com" target=3D"_blank">l.j.persson@gmail.com</a>&gt; wrote:<br=
>
&gt;<br>&gt;<br>
&gt; In this case UB will not give you any better performance. If you get U=
B on the caller side of a precondition, it is becase the code is incorrect.=
 If that causes the compiler to fail, you can remove the failing code, givi=
ng you the same as UB code pruning would. But with fewer subtle bugs.=C2=A0=
</p>
</span><p dir=3D"ltr">True, for hand-written code. But invalid or unreachab=
le code paths can easily crop up in template code, macro expansions, platfo=
rm-, library- or configuration-dependent code, generated code, and in edge =
cases such as loop or recursion terminating conditions. Why should we have =
to contort our code to remove the failing code when the compiler can easily=
 do it for us?</p></blockquote><div>Note that I&#39;m not talking about dis=
allowing UB in general here, only for precondition callers.=C2=A0</div><div=
>The reason to make the ill-formed is that=C2=A0</div><div>1) the primary r=
eason to use contracts is to catch misuse. Here we are detecting misuse, bu=
t we are not telling.=C2=A0</div><div>2) it is not evident from reading the=
 code that it will fail or be pruned. Only the compiler knows.=C2=A0</div><=
div>3) We cannot be sure to catch this error by rebuilding in debug mode or=
 with sanitisers, becase any change to the compile process might cause chan=
ges in inlining or other things so that the</div><div>compiler fails to fig=
ure out the failed precondition at compile time.</div></div></div></div></b=
lockquote><div><br></div></span><div>Making precondition failure ill-formed=
 excludes far too much otherwise well-formed code:</div><div><br></div><div=
><font face=3D"monospace, monospace">void print_column(string s, int width)=
 {</font></div><div><font face=3D"monospace, monospace">=C2=A0 =C2=A0 for (=
int i =3D 0; i !=3D width; ++i)</font></div><div><font face=3D"monospace, m=
onospace">=C2=A0 =C2=A0 =C2=A0 =C2=A0 cout &lt;&lt; ((i &lt; s.size()) ? s[=
i] : &#39; &#39;); =C2=A0// X</font></div><div><font face=3D"monospace, mon=
ospace">}</font></div><div><font face=3D"monospace, monospace">print_column=
(&quot;hello&quot;, 10);</font></div><div><br></div><div>The compiler can p=
rove that the program reaches line <i>X</i> with <i>i =3D 5</i>. But <font =
face=3D"monospace, monospace">s[5]</font> is a precondition violation! Do y=
ou really want this code to be ill-formed?</div></div></div></div></blockqu=
ote><div><br></div><div>How is that precondition violation?=C2=A0 If operat=
or[] has a precodition it should be i &lt; size(), which is what you are ch=
ecking.</div><div><br></div><div>On the other hand:</div><div><font face=3D=
"monospace, monospace">void print_column(string s, int width) {</font></div=
><div><font face=3D"monospace, monospace">=C2=A0 =C2=A0 for (int i =3D 0; i=
 !=3D width; ++i)</font></div><div><font face=3D"monospace, monospace">=C2=
=A0 =C2=A0 =C2=A0 =C2=A0 cout &lt;&lt; s[i]; =C2=A0// X</font></div><div><f=
ont face=3D"monospace, monospace">}</font></div><div><font face=3D"monospac=
e, monospace">print_column(&quot;hello&quot;, 10);</font></div><div><font f=
ace=3D"monospace, monospace"><br></font></div><div><font face=3D"monospace,=
 monospace">would be a violation and I would rather have the compile fail t=
han an access violation or no output at runtime.</font></div><div><font fac=
e=3D"monospace, monospace">Properly fixing that with</font></div><div><font=
 face=3D"monospace, monospace"><br></font></div><div><font face=3D"monospac=
e, monospace">void print_column(string s, int width) [expects:width&lt;s.si=
ze()] {</font></div><div><font face=3D"monospace, monospace">=C2=A0 =C2=A0 =
for (int i =3D 0; i !=3D width; ++i)</font></div><div><font face=3D"monospa=
ce, monospace">=C2=A0 =C2=A0 =C2=A0 =C2=A0 cout &lt;&lt; s[i]; =C2=A0// X</=
font></div><div><font face=3D"monospace, monospace">}</font></div><div><fon=
t face=3D"monospace, monospace">print_column(&quot;hello&quot;, 10);</font>=
</div><div><font face=3D"monospace, monospace"><br></font></div><div>would =
let the compiler optimize the print_column function and tell you, hopefully=
 at compile time, that your call to print_column is wrong.=C2=A0</div></div=
><br></div><div class=3D"gmail_extra">=C2=A0 / Jonas</div></div>

<p></p>

-- <br />
<br />
--- <br />
You received this message because you are subscribed to the Google Groups &=
quot;ISO C++ Standard - Future Proposals&quot; group.<br />
To unsubscribe from this group and stop receiving emails from it, send an e=
mail to <a href=3D"mailto:std-proposals+unsubscribe@isocpp.org">std-proposa=
ls+unsubscribe@isocpp.org</a>.<br />
To post to this group, send email to <a href=3D"mailto:std-proposals@isocpp=
..org">std-proposals@isocpp.org</a>.<br />
Visit this group at <a href=3D"http://groups.google.com/a/isocpp.org/group/=
std-proposals/">http://groups.google.com/a/isocpp.org/group/std-proposals/<=
/a>.<br />

--001a1144334696786a052466f431--

.
