comp.lang.ada
 help / color / mirror / Atom feed
From: Adam Beneschan <adambeneschan@gmail.com>
Subject: Re: Compiler checking of String lengths during assignment (Newbie Question)
Date: Fri, 9 Jan 2015 17:48:01 -0800 (PST)
Date: 2015-01-09T17:48:01-08:00	[thread overview]
Message-ID: <8f9a4a2f-24e8-479b-a535-c862c46a272c@googlegroups.com> (raw)
In-Reply-To: <77d434cc-00bc-4a2f-b50e-40736abdd2b2@googlegroups.com>

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.
> 
> I'm using the Libre GNAT GPS 2014. I've a program that looks roughly like this:
> ==============================================
> procedure Main is
>    Head : String (1..5) := "XXX_N";
>    i : Positive;
>    Line : String := "12312312312312312";
>    
> begin
> .
> .
> .
>    i := 2;
>    Head := "123";
>    Head := Line(1 .. 2);
>    Head := Line(i+2 .. i+4);
> .
> .
> .
> end Main;
> ==============================================
> 
> The first 2 cases produces an error during the build, as expected, because Head is known to be length of 5 and I'm assigning something of a different 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_Error when I run it.  If you're not getting a warning, it's probably a compiler bug.  If it's not generating Constraint_Error, it's definitely a compiler bug unless you've done something that turns off checking.

However, it's important to realize that what's clear to us won't necessarily be clear to a compiler.  The range Line(i+2 .. i+4) will always have 'Length 3, no matter what "i" is.  But we know that because we know basic algebra.  It's not easy for a compiler to do the kinds of expression manipulation 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) := "XXX_N"; 
        i : Positive; 
        Line : String := "12312312312312312"; 

        procedure Do_Nothing is null;
    begin 
        i := 2;
        Do_Nothing; 
        Head := Line(i+2 .. i+4); 
    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 call inserted, the compiler assumes that the procedure could change "i", so it can no longer track the value.  The compiler doesn't figure out that Do_Nothing 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


  parent reply	other threads:[~2015-01-10  1:48 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-01-09 22:50 Compiler checking of String lengths during assignment (Newbie Question) isaac1.0
2015-01-09 23:10 ` Simon Wright
2015-01-09 23:20   ` Stefan.Lucks
2015-01-10 12:43     ` Brian Drummond
2015-01-09 23:53 ` Shark8
2015-01-10  0:18 ` Jeffrey Carter
2015-01-10  1:48 ` Adam Beneschan [this message]
2015-01-10 10:03 ` Pascal Obry
2015-01-10 10:54 ` Simon Wright
2015-01-15 20:44 ` isaac1.0
2015-01-15 21:18   ` Simon Wright
2015-01-15 22:30     ` Jeffrey Carter
2015-01-15 22:32     ` Jeffrey Carter
2015-01-16  7:28       ` Simon Wright
2015-01-16 15:44         ` Jeffrey Carter
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox