From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,FREEMAIL_FROM autolearn=unavailable autolearn_force=no version=3.4.4 X-Received: by 10.236.63.8 with SMTP id z8mr13503804yhc.15.1420854482633; Fri, 09 Jan 2015 17:48:02 -0800 (PST) X-Received: by 10.50.118.42 with SMTP id kj10mr101078igb.9.1420854482544; Fri, 09 Jan 2015 17:48:02 -0800 (PST) Path: eternal-september.org!reader01.eternal-september.org!reader02.eternal-september.org!news.eternal-september.org!mx02.eternal-september.org!feeder.eternal-september.org!news.glorb.com!dc16no522031qab.1!news-out.google.com!qk8ni2634igc.0!nntp.google.com!h15no2430328igd.0!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail Newsgroups: comp.lang.ada Date: Fri, 9 Jan 2015 17:48:01 -0800 (PST) In-Reply-To: <77d434cc-00bc-4a2f-b50e-40736abdd2b2@googlegroups.com> Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=66.126.103.122; posting-account=KSa2aQoAAACOxnC0usBJYX8NE3x3a1Xq NNTP-Posting-Host: 66.126.103.122 References: <77d434cc-00bc-4a2f-b50e-40736abdd2b2@googlegroups.com> User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: <8f9a4a2f-24e8-479b-a535-c862c46a272c@googlegroups.com> Subject: Re: Compiler checking of String lengths during assignment (Newbie Question) From: Adam Beneschan Injection-Date: Sat, 10 Jan 2015 01:48:02 +0000 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Xref: news.eternal-september.org comp.lang.ada:24520 Date: 2015-01-09T17:48:01-08:00 List-Id: On Friday, January 9, 2015 at 2:50:18 PM UTC-8, isaa...@gmail.com wrote: > Maybe this is obvious to the experts, but I'm new to Ada. >=20 > I'm using the Libre GNAT GPS 2014. I've a program that looks roughly like= this: > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D > procedure Main is > Head : String (1..5) :=3D "XXX_N"; > i : Positive; > Line : String :=3D "12312312312312312"; > =20 > begin > . > . > . > i :=3D 2; > Head :=3D "123"; > Head :=3D Line(1 .. 2); > Head :=3D Line(i+2 .. i+4); > . > . > . > end Main; > =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D= =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D >=20 > The first 2 cases produces an error during the build, as expected, becaus= e Head is known to be length of 5 and I'm assigning something of a differen= t length. > The 3rd assignment there does NOT produce an error, no warning no nothing= even though this clearly violates the same check. I do get a warning with GCC 4.5.4. And, as expected, I get a Constraint_Er= ror when I run it. If you're not getting a warning, it's probably a compil= er bug. If it's not generating Constraint_Error, it's definitely a compile= r bug unless you've done something that turns off checking. However, it's important to realize that what's clear to us won't necessaril= y be clear to a compiler. The range Line(i+2 .. i+4) will always have 'Len= gth 3, no matter what "i" is. But we know that because we know basic algeb= ra. It's not easy for a compiler to do the kinds of expression manipulatio= n needed to figure something like this out. In fact, I don't get a warning= with this program: procedure Main is Head : String (1..5) :=3D "XXX_N";=20 i : Positive;=20 Line : String :=3D "12312312312312312";=20 procedure Do_Nothing is null; begin=20 i :=3D 2; Do_Nothing;=20 Head :=3D Line(i+2 .. i+4);=20 end Main; Apparently, the reason I get a warning without the Do_Nothing call is that = the compiler keeps track of the values it knows about, so it can compute i+= 2 and i+4 and see that the length will be wrong. But with the procedure ca= ll inserted, the compiler assumes that the procedure could change "i", so i= t can no longer track the value. The compiler doesn't figure out that Do_N= othing doesn't change the value of "i", and it doesn't figure out that i+2 = .. i+4 will always be a range of 3 regardless of the value. So it doesn't = produce a warning. Of course it still gets a Constraint_Error at run time. -- Adam