Incorrect usage of renames? (found in a Rosetta Code task)

Hi;

The following is an excerpt of Ada code (which was written by somebody else) found at

Placeholder : String := " n Phi Is_Prime";
Image_N : String renames Placeholder ( 1 … 3);
Image_Phi : String renames Placeholder ( 6 … 8);
Image_Prime : String renames Placeholder (11 … 17);

My gcc 14.1.0 GNAT compiler complains about the above:

totient.adb:53:43: warning: wrong length for array of subtype of “Standard.String” defined at line 38 [enabled by default]
totient.adb:53:43: warning: Constraint_Error will be raised at run time [enabled by default]
totient.adb:53:59: warning: wrong length for array of subtype of “Standard.String” defined at line 38 [enabled by default]
totient.adb:53:59: warning: Constraint_Error will be raised at run time [enabled by default]

I can change this using Ada string slices without using rename…but I wonder if someone could show me what the author intended with the rename that would also compile. Is this something that would have compiled under Ada 95 or Ada 83?

Thanks,
Retired Build Engineer

The current GNAT Pro has a much better error here:

foo.ads:5:49: warning: static range out of bounds of subtype of "Standard.Integer" defined at line 2 [enabled by default]
foo.ads:5:49: warning: Constraint_Error will be raised at run time [enabled by default]

where line 5 is using “17” when the string only has 15 characters, so the error is legitimate.

That said, it would be much better to use 'First and 'Last for indexes

Thank you for your response.

I wonder how the author of this code was able to obtain the output they posted…

At least I learned something!

I might post a version that works…

Thanks,
Retired Build Engineer

Impossible with the version he posted (in this case it’s wrong from the first version).

I’ve made the fixes and some improvements in Rosseta, and it now prints the same output as posted (case insensitively, since I’ve used True'Image and False'Image. It could probably be improved further, if you want to continue.

Thank you for the code improvement of this task on the Rosetta Code web site. I don’t see the need for additional changes. It is great to have code that works…

Retired Build Engineer

1 Like

One request (if it is ok to request, apologies if not) is that when you post up code snippets to use the code delimiters. You can use triple back ticks: ``` before and after the code to get nicely formatted code (and compiler output as well, see ebriot’s above post). That can really help us out by making it much easier to read. EX:

```
Placeholder : String := " n Phi Is_Prime";
Image_N : String renames Placeholder ( 1 … 3);
Image_Phi : String renames Placeholder ( 6 … 8);
Image_Prime : String renames Placeholder (11 … 17);
```

becomes:

Placeholder : String := " n Phi Is_Prime";
Image_N     : String renames Placeholder ( 1 … 3);
Image_Phi   : String renames Placeholder ( 6 … 8);
Image_Prime : String renames Placeholder (11 … 17);

And if you do the compiler results as well, they come out to be:

totient.adb:53:43: warning: wrong length for array of subtype of “Standard.String” defined at line 38 [enabled by default]
totient.adb:53:43: warning: Constraint_Error will be raised at run time [enabled by default]
totient.adb:53:59: warning: wrong length for array of subtype of “Standard.String” defined at line 38 [enabled by default]
totient.adb:53:59: warning: Constraint_Error will be raised at run time [enabled by default]

It makes seeing the line numbers and such much easier.

The back tick key is usually just left of the 1 key I think.

1 Like

Thank you for the reminder regarding the code formatting, I keep forgetting to use it.

1 Like