Discussion:
Raise expressions from AARM.
(too old to reply)
Blady
2024-02-24 09:50:31 UTC
Permalink
Hello,

AARM Ada 2022 section 11.3 presents some uses of raise expressions
including this one:
(http://www.ada-auth.org/standards/22aarm/html/AA-11-3.html)

2.a.10/4 ...

B : Some_Array := (1, 2, 3, others => raise
Not_Valid_Error);

What could be the use cases?

My guess: whatever the size of Some_Array (greater than 3), B is
elaborated but raises Not_Valid_Error when accessing component beyond
position 3:

type Some_Array is array (Positive range 1..10) of Natural;
...
B : Some_Array := (1, 2, 3, others => raise Not_Valid_Error);
...
begin
X := B (2); -- OK
X := B (6); -- raises Not_Valid_Error
end;

Is it correct?
error: "others" choice not allowed here
see: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113862

Thanks, Pascal.
Jeffrey R.Carter
2024-02-24 10:39:08 UTC
Permalink
My guess: whatever the size of Some_Array (greater than 3), B is elaborated but
type Some_Array is array (Positive range 1..10) of Natural;
...
B : Some_Array := (1, 2, 3, others => raise Not_Valid_Error);
...
begin
X := B (2); -- OK
X := B (6); -- raises Not_Valid_Error
end;
Is it correct?
No. This will raise the exception upon the elaboration of B.

The only use of this that I can imagine is if the length of Some_Array is 3.
Then the others choice is null, so the raise expression is never evaluated. But
if someone changes the definition of Some_Array to be longer, then the exception
will be raised.
        >>> error: "others" choice not allowed here
see: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113862
The example in the error report has Some_Array unconstrained, in which case an
others choice is not allowed. With the constrained definition given above, the
aggregate is valid.
--
Jeff Carter
"Gentlemen, you can't fight in here. This is the War Room!"
Dr. Strangelove
30
Blady
2024-02-25 11:09:08 UTC
Permalink
Post by Jeffrey R.Carter
Post by Blady
My guess: whatever the size of Some_Array (greater than 3), B is
elaborated but raises Not_Valid_Error when accessing component beyond
type Some_Array is array (Positive range 1..10) of Natural;
...
B : Some_Array := (1, 2, 3, others => raise Not_Valid_Error);
...
begin
X := B (2); -- OK
X := B (6); -- raises Not_Valid_Error
end;
Is it correct?
No. This will raise the exception upon the elaboration of B.
The only use of this that I can imagine is if the length of Some_Array
is 3. Then the others choice is null, so the raise expression is never
evaluated. But if someone changes the definition of Some_Array to be
longer, then the exception will be raised.
If I understand well, no compiler error nor warning at compilation time
but Not_Valid_Error raised at run time elaboration.
To be compared with:
B1 : Some_Array := (1, 2, 3);
No compiler error, one compiler warning "Constraint_Error will be raised
at run time" and Constraint_Error range check failed raised at run time
elaboration.
Post by Jeffrey R.Carter
Post by Blady
         >>> error: "others" choice not allowed here
see: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113862
The example in the error report has Some_Array unconstrained, in which
case an others choice is not allowed. With the constrained definition
given above, the aggregate is valid.
Thanks to point out, I've corrected the GNAT report.

Pascal.
Niklas Holsti
2024-02-24 10:39:43 UTC
Permalink
Post by Blady
Hello,
AARM Ada 2022 section 11.3 presents some uses of raise expressions
(http://www.ada-auth.org/standards/22aarm/html/AA-11-3.html)
2.a.10/4        ...
                B : Some_Array := (1, 2, 3, others => raise
Not_Valid_Error);
What could be the use cases?
The point of these examples (which are only in the discussion
annotation, not in the normative standard) is to discuss what is
syntactically legal and why. The examples need not make practical sense.
Post by Blady
My guess: whatever the size of Some_Array (greater than 3), B is
elaborated but raises Not_Valid_Error when accessing component beyond
No. A raise-expression is not a value that can be stored in an array or
passed around; its evaluation raises an exception /instead/ of yielding
a value.

In this example, if the evaluation of the array aggregate that
initializes B evaluates the expression supplied for the "others" choice,
this evaluation will raise Not_Valid_Error and disrupt the
initialization of B.

It is not clear to me if the RM requires the evaluation of the "others"
expression if there are no "other" indices. Experimenting with GNAT
(Community 2019) shows that if the Some_Array type has 'Length = 3, the
exception is not raised (so the "others" value is not evaluated), while
if the 'Length is greater than 3 the exception is raised.
Post by Blady
type Some_Array is array (Positive range 1..10) of Natural;
...
B : Some_Array := (1, 2, 3, others => raise Not_Valid_Error);
That should raise Not_Valid_Error during the initialization of B.
Post by Blady
...
begin
X := B (2); -- OK
X := B (6); -- raises Not_Valid_Error
end;
Is it correct?
No.
Post by Blady
        >>> error: "others" choice not allowed here
see: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=113862
Interesting. GNAT Community 2019 accepted it.
Blady
2024-02-25 11:23:48 UTC
Permalink
Post by Niklas Holsti
The point of these examples (which are only in the discussion
annotation, not in the normative standard) is to discuss what is
syntactically legal and why. The examples need not make practical sense.
Well, despite I knew that, I wanted to draw some use cases from them.
For instance:
A : A_Tagged := (Some_Tagged'(raise TBD_Error) with Comp => 'A');
It will raise TBD_Error if Some_Tagged is not a null record, good to
know, isn't it?

Pascal.
Niklas Holsti
2024-02-26 20:01:23 UTC
Permalink
Post by Blady
Post by Niklas Holsti
The point of these examples (which are only in the discussion
annotation, not in the normative standard) is to discuss what is
syntactically legal and why. The examples need not make practical sense.
Well, despite I knew that, I wanted to draw some use cases from them.
  A : A_Tagged   := (Some_Tagged'(raise TBD_Error) with Comp => 'A');
It will raise TBD_Error if Some_Tagged is not a null record, good to
know, isn't it?
Hm, not raising the exception for a null record seems weird to me, and I
cannot deduce it from the RM. Moreover, for a plain qualified expression

Some_Tagged'(raise TBD_Error)

not in an extension aggregate GNAT raises the exception even if the type
is a null record. I suspect that not raising the exception for an
extension aggregate where the ancestor type is a null record is a bug in
GNAT.

Loading...