https://gcc.gnu.org/bugzilla/show_bug.cgi?id=122159
Bug ID: 122159
Summary: Ada RM example does not compile
Product: gcc
Version: 15.2.1
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: ada
Assignee: unassigned at gcc dot gnu.org
Reporter: [email protected]
CC: dkm at gcc dot gnu.org
Target Milestone: ---
This has already been reported as [TA10-005 public]. It is an
example from the reference manual. One should think that RM
examples are correct syntactically - and more so since there
has no AI been written and there was a discussion in Ada-
Comment about this, which you find attached.
So why does GNAT still fail?
===============================================================
procedure Roman is
type Roman_Digit is ('I', 'V', 'X', 'L', 'C', 'D', 'M');
for Roman_Digit use ('I' => 1, 'V' => 5, 'X' => 10, 'L' => 50, 'C' => 100,
'D' => 500, 'M' => 1000);
subtype Roman_Character is Wide_Wide_Character
with Static_Predicate => Roman_Character in 'I' | 'V' | 'X' | 'L' | 'C' |
'D' | 'M';
Max_Roman_Number : constant := 3_999; -- MMMCMXCIX
type Roman_Number is range 1 .. Max_Roman_Number
with String_Literal => To_Roman_Number;
function To_Roman_Number (S : Wide_Wide_String) return Roman_Number
with Pre => S'Length > 0 and then (for all Char of S => Char in
Roman_Character);
function To_Roman_Number (S : Wide_Wide_String) return Roman_Number is
(declare
R : constant array (Integer range <>) of Roman_Number :=
(for D in S'Range => Roman_Digit'Enum_Rep
(Roman_Digit'Wide_Wide_Value (''' & S(D) & ''')));
-- See 3.5.2 and 13.4
begin
[for I in R'Range =>
(if I < R'Last and then R(I) < R(I + 1) then -1 else 1) *
R(I)]'Reduce("+", 0) -- line 24
-- raised CONSTRAINT_ERROR : roman.adb:24 range check failed (this is previous
line)
);
X: Roman_Number := "III" * "IV" * "XII"; -- 144 (that is, CXLIV)
begin
null;
end Roman;
raised CONSTRAINT_ERROR : roman.adb:24 range check failed
===============================================================
Betreff: Re: [Ada-Comment] Reduction expressions example 4.2.1 Roman numbers
Weitersenden-Von: [email protected]
Datum: Fri, 28 May 2021 12:40:15 +0000
Von: [email protected]
Antwort an: Ada-Comment List <[email protected]>
An: [email protected]
> -----Original Message-----
> From: Christoph & Ursula Grein <[email protected]>
> Sent: Friday, May 28, 2021 9:42 AM
> To: [email protected]
> Subject: Re: [Ada-Comment] Reduction expressions example 4.2.1 Roman
> numbers
>
> It does raise Constraint_Error.
explicitly stating Roman_Number'Base(0) seems to be a workaround
> There is another problem in GNAT:
>
> X: Roman_Number := "III" * "IV" * "XII"; -- 144 (that is, CXLIV)
> roman.adb:51:30: error: there is no applicable operator "*" for a string
> type
>
> Replacing this by
> X: Roman_Number := to_Roman_number("III") * "IV" * "XII";
> it compiles.
That triggered a GNAT bug-box here with the newest GNAT Pro wavefront (22.0w
20210424)
however, both
X: Roman_Number := to_Roman_number("III") * to_Roman_number("IV") *
to_Roman_number("XII");
and
X: Roman_Number := Roman_Number' ("III") * Roman_Number' ("IV") *
Roman_Number'("XII");
works, though
--
~egilhh
===============================================================
Betreff: Re: [Ada-Comment] Reduction expressions example 4.2.1 Roman numbers
Weitersenden-Von: [email protected]
Datum: Fri, 28 May 2021 08:11:41 -0400
Von: Tucker Taft <[email protected]>
Antwort an: Ada-Comment List <[email protected]>
An: Ada-Comment List <[email protected]>
On Fri, May 28, 2021 at 3:44 AM Christoph & Ursula Grein
<[email protected]> wrote:
Am 27.05.2021 um 21:30 schrieb Tucker Taft:
> I agree that the GNAT warning is incorrect in this case. It is a
> somewhat unusual case in that the initial value never becomes the
> final value of the reduction. But in general, the warning does seem
> overly strict, since there are probably a number of situations where
> the initial value never becomes the final value, and it is certainly
> wrong to say "Constraint_Error *will* be raised".
It does raise Constraint_Error.
Interesting. It sounds like it is checking against the wrong subtype,
and the warning comes from that, so I guess suppressing the warning won't
solve the problem. It is presumably using the first subtype of the type
rather than the subtype of the first parameter of the Reducer subprogram
as the subtype for the accumulator.
There is another problem in GNAT:
X: Roman_Number := "III" * "IV" * "XII"; -- 144 (that is, CXLIV)
roman.adb:51:30: error: there is no applicable operator "*" for a string
type
Replacing this by
X: Roman_Number := to_Roman_number("III") * "IV" * "XII";
it compiles.
It looks like overload resolution is not considering fully the possibility
of user-defined string literals.
So, Tuck, you say GNAT is wrong? I'll rereport it to [email protected]
with reference to you.
OK. Since this is an unsupported account, I presume it might not be fixed
immediately, but thank you for documenting the problems! That will
certainly make it more likely a fix can be found sooner.
Christoph
Take care,
-Tuck
________________________________________________________
You have received this message because you subscribed to the Ada-Comment
mailing list. To leave the Ada-Comment list, send an email with
'leave Ada-Comment' in the body to [email protected]. For help
on the other commands available, send 'help Ada-Comment' to the same
address.
Problems? Send mail to [email protected]. This list is operated by the
Ada Resource Association, Inc., PO Box 8685, New York NY 10116-8685.
===============================================================
Betreff: Re: [TA10-005 public] - GNAT CE 2020 - Roman_Numbers
Datum: Fri, 28 May 2021 09:49:50 +0200
Von: Arnaud Charlet <[email protected]>
An: Christoph & Ursula Grein <[email protected]>
Kopie (CC): [email protected]
> I rereport this after a dicussion with Tuck in Ada Comment. He answered:
Thanks for forwarding. Note that we are also following ada-comment, so we
saw your exchange.
Regards,
Arno
===============================================================
Betreff: Fwd: [TA10-005 public] - GNAT CE 2020 - Roman_Numbers
Datum: Fri, 28 May 2021 09:57:15 +0200
Von: Christoph & Ursula Grein <[email protected]>
An: [email protected]
Replacing this by
X: Roman_Number := to_Roman_number("III") * "IV" * "XII";
it compiles.
Excuse me, it produces a bug box:
gprbuild -d
-PC:\Users\Grein\Documents\Christoph\Compiler_Test\GNAT\ausprobieren.gpr
C:\Users\Grein\Documents\Christoph\Compiler_Test\GNAT\ausprobieren.adb
Compile
[Ada] ausprobieren.adb
[Ada] roman.adb
+===========================GNAT BUG DETECTED==============================+
| Community 2021 (20210519-103) (x86_64-w64-mingw32) Program_Error
EXCEPTION_ACCESS_VIOLATION|
| Error detected at roman.adb:51:40 |
| Please submit a bug report by email to [email protected]. |
| GAP members can alternatively use GNAT Tracker: |
| https://www.adacore.com/login?mode=gap section 'Create New Ticket'. |
| See gnatinfo.txt for full info on procedure for submitting bugs. |
| Use a subject line meaningful to you and us to track the bug. |
| Include the entire contents of this bug box in the report. |
| Include the exact command that you entered. |
| Also include sources listed below. |
| Use plain ASCII or MIME attachment(s). |
+==========================================================================+
Please include these source files with error report
Note that list may not be accurate in some cases,
so please double check that the problem can still
be reproduced with the set of files listed.
Consider also -gnatd.n switch (see debug.adb).
C:\Users\Grein\Documents\Christoph\Compiler_Test\GNAT\roman.adb
roman.adb:40:25: warning: value not in range of type "Roman_Number" defined at
line 24 [enabled by default]
roman.adb:40:25: warning: "Constraint_Error" will be raised at run time
[enabled by default]
compilation abandoned
gprbuild: *** compilation phase failed
[2021-05-28 09:37:00] process exited with status 4, 100% (2/2), elapsed time:
10.52s
===============================================================
Betreff: Re: [TA10-005 public] - GNAT CE 2020 - Roman_Numbers
Datum: Fri, 28 May 2021 09:48:31 +0200
Von: Christoph & Ursula Grein <[email protected]>
An: [email protected]
I rereport this after a dicussion with Tuck in Ada Comment. He answered:
I agree that the GNAT warning is incorrect in this case. It is a somewhat
unusual case in that the initial value never becomes the final value of the
reduction. But in general, the warning does seem overly strict, since there
are probably a number of situations where the initial value never becomes the
final value, and it is certainly wrong to say "Constraint_Error *will* be
raised". The nominal subtype of a reduction expression is the Accum_Type, and
that comes from the first parameter to "+", which as you point out is
Roman_Number'Base, and 0 is certainly a legal value of that subtype.
There is another problem:
X: Roman_Number := "III" * "IV" * "XII"; -- 144 (that is, CXLIV)
roman.adb:51:30: error: there is no applicable operator "*" for a string type
Replacing this by
X: Roman_Number := to_Roman_number("III") * "IV" * "XII";
it compiles.
===============================================================
Betreff: Re: [TA10-005 public] - GNAT CE 2020 - Roman_Numbers
Datum: Mon, 26 Oct 2020 12:23:31 -0400
Von: Arnaud Charlet <[email protected]>
An: Christoph & Ursula Grein <[email protected]>
Kopie (CC): [email protected]
> I'm having problems with your implementation of reduction expressions, here
> the Roman_Number example 4.2.1(15/5ff).
Thank you for the report, we'll look at it when we get a chance.
Regards,
Arno
===============================================================
Betreff: GNAT CE 2020 - Roman_Numbers
Datum: Sat, 10 Oct 2020 16:05:30 +0200
Von: Christoph & Ursula Grein <[email protected]>
An: [email protected]
I'm having problems with your implementation of reduction expressions, here the
Roman_Number example 4.2.1(15/5ff).
Referring to 4.5.10(9/5ff), Accum_Type and Value_Type are the same, the Reducer
is
function "+" (Left, Right: Roman_Number'Base) return Roman_Number'Base;
Your implementation uses Roman_Number directly, so it fails to compile the
example. To make it executable, Roman_Number has to include the values -1 and
0.
Now according to the RM, Accum_Type is a subtype of the expected type, here
obviously the *type of Roman_Number*, let's call it T (recursive like it's done
in the RM when talking about a type). Now Roman_Number trivially *is* a subtype
of T , but the RM does not explicitly say which subtype. Starting from the
reducer subprogram, the subtype must obviously be Roman_Number'Base or else
there is no statically matching "+" operation.
The RM is rather obscure in this subclause.
BTW: I know that function To_Roman_Number should paramter Wide_Wide_String
according to not yet finished RM Ada 202x; must be corrected somehow, there is
an Ada Comment.