Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Discriminant problem

88 views
Skip to first unread message

Simon Wright

unread,
Aug 29, 2021, 2:52:01 PM8/29/21
to
I have

Location_Step.Node_Test :=
(Node_Test => Get_Node_Type_Test (T.Node_Type_Part.all),
Name => Null_Unbounded_String);

where

type Location_Steps is record
...
Node_Test : Node_Test_Specification
:= (Node_Test => No_Node_Test,
Name => Null_Unbounded_String);
...
end record;

and

type Node_Test_Specification
(Node_Test : Node_Tests := No_Node_Test) is
record
Name : Unbounded_String;
case Node_Test is
...
end case;
end record;

Because of that function call in

Node_Test => Get_Node_Type_Test (T.Node_Type_Part.all)

the compiler says

value for discriminant "Node_Test" must be static
non-static function call (RM 4.9(6,18))

OK, I get that (tiresome though it is, and I'm amazed I've never come
across it before), but how to approach it? I seem to be OK with

case Get_Node_Type_Test (T.Node_Type_Part.all) is
when Text_Node_Test =>
Location_Step.Node_Test :=
(Node_Test => Text_Node_Test,
Name => Null_Unbounded_String);
when ...
end case;

but this seems very ugly.

I've only just noticed this: I'd been using -gnatX (so that I could use
'Image on records), which meant that the original code was OK (in the
sense that it didn't raise any problems), but of course it's not
portable even within the GNAT family. I think -gnat2020 might solve the
issue too, but there's a bit of a skew between CE 2021 and GCC 11.

Simon Wright

unread,
Aug 30, 2021, 4:13:34 AM8/30/21
to
Simon Wright <si...@pushface.org> writes:

> I've only just noticed this: I'd been using -gnatX (so that I could use
> 'Image on records), which meant that the original code was OK (in the
> sense that it didn't raise any problems), but of course it's not
> portable even within the GNAT family. I think -gnat2020 might solve the
> issue too, but there's a bit of a skew between CE 2021 and GCC 11.

The skew might well be about the semantics of -gnatX - not 100% sure.

The only way I can see that -gnat2020 would help would be if
Get_Node_Type_Test was a static expression function[1], but it's not
even an expression function! It could be, though since it involves tag
tests and can 'return' an exception this seems unlikely.

[1] http://www.ada-auth.org/standards/2xaarm/html/AA-4-9.html#p21.1

Jeffrey R. Carter

unread,
Aug 30, 2021, 6:03:19 PM8/30/21
to
On 8/29/21 8:51 PM, Simon Wright wrote:
>
> the compiler says
>
> value for discriminant "Node_Test" must be static
> non-static function call (RM 4.9(6,18))

This has always been the rule for aggregates.

> OK, I get that (tiresome though it is, and I'm amazed I've never come
> across it before), but how to approach it? I seem to be OK with

That is surprising.

> case Get_Node_Type_Test (T.Node_Type_Part.all) is
> when Text_Node_Test =>
> Location_Step.Node_Test :=
> (Node_Test => Text_Node_Test,
> Name => Null_Unbounded_String);
> when ...
> end case;
>
> but this seems very ugly.

Agreed. What I usually do is

declare
Result : Whatever (D => Non_Static_Value);
-- Discriminant of an object need not be static
begin
Result.F1 := ...;
...
Location_Step.Node_Test := Result;
end;

--
Jeff Carter
"You can never forget too much about C++."
115

Randy Brukardt

unread,
Aug 30, 2021, 9:53:45 PM8/30/21
to
"Jeffrey R. Carter" <spam.jrc...@spam.not.acm.org> wrote in message
news:sgjkj4$kn4$1...@dont-email.me...
> On 8/29/21 8:51 PM, Simon Wright wrote:
>>
>> the compiler says
>>
>> value for discriminant "Node_Test" must be static
>> non-static function call (RM 4.9(6,18))
>
> This has always been the rule for aggregates.

But Ada 202x relaxes it slightly. If the expression has a static nominal
subtype, and every value in the subtype selects the same variant, then a
dynamic discriminant is allowed in a aggregate. I don't know if that is what
is happening here or not; this relaxation doesn't come up that often.

Randy.


Simon Wright

unread,
Sep 9, 2021, 3:51:45 PM9/9/21
to
That's exactly what's happening here.
0 new messages