comp.lang.ada
 help / color / mirror / Atom feed
From: Niklas Holsti <niklas.holsti@tidorum.invalid>
Subject: Re: Did I find a bug here?
Date: Fri, 2 Apr 2021 11:33:11 +0300	[thread overview]
Message-ID: <ico369FihqfU1@mid.individual.net> (raw)
In-Reply-To: <s46h6q$c2m$1@dont-email.me>

On 2021-04-02 10:30, J-P. Rosen wrote:
> Le 02/04/2021 à 08:30, reinert a écrit :
>> Assume this simple program:
>>
>> procedure test0 is
>>    type ABC_Type is (A,B,C);
>>    subtype AB_Type is ABC_Type with Static_Predicate => AB_Type in A | B;
>>    X : ABC_type    := A;
>> begin
>> -- alternative 1:
>> case AB_Type(X) is
>>      when A => null;
>>      when B => null;
>>      when others => null; -- ??? should the compiler complain here?
>>    end case;
>> --alternative 2:
>>    case AB_Type(X) is
>>      when A => null;
>>      when B => null;
>>    end case;
>> end test0;
>>
>> Should the compiler complain about "when others => null" here?
>> My compiler does not (running debian 10, updated, gnat-8).
>> I find it strange that both alternatives goes through.
>>
> A case statement is allowed to have alternatives that cover no value. A 
> friendly compiler can warn you that "this branch covers no value", but 
> what you wrote is not illegal (and sometimes useful, if you have 
> variants of your software that use slightly different definitions of the 
> type).


Recent discussion in ISO SC22 WG9, about the Ada part of the ISO 
"programming language vulnerabilities" document, brought out that if the 
selecting expression (here AB_Type(X)) in a case statement or case 
expression has an invalid representation (for example, is an 
uninitialized variable with an out-of-range value), an Ada compiler is 
required to raise Constraint_Error if there is no "others" alternative, 
but if there is an "others" alternative the compiler can instead let 
execution proceed to that alternative without raising Constraint_Error.

In effect, "others" can cover all values, even those that are outside 
the nominal subtype of the selecting expression. See RM 5.4(12) and 5.4(13).

So if the programmer is worried about such cases (invalid 
representations from uninitialized variables or other causes such as 
Unchecked_Conversion), they can add an apparently unnecessary "others" 
alternative even if the other alternatives already cover all valid 
values. However, note that the compiler may choose to raise 
Constraint_Error even if there is an "others" alternative; RM 5.4 
(10.d). To avoid that uncertainty, the program can perform an explicit 
'Valid check before the case statement.

  reply	other threads:[~2021-04-02  8:33 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-04-02  6:30 Did I find a bug here? reinert
2021-04-02  7:30 ` J-P. Rosen
2021-04-02  8:33   ` Niklas Holsti [this message]
2021-04-03  5:46     ` reinert
2021-04-03  6:41       ` J-P. Rosen
2021-04-03  8:18         ` Niklas Holsti
2021-04-03 12:37           ` J-P. Rosen
  -- strict thread matches above, loose matches on Subject: below --
2021-04-02  6:15 reinert
replies disabled

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