comp.lang.ada
 help / color / mirror / Atom feed
* Type inference with String_Literal.
@ 2020-12-26  9:12 Blady
  2020-12-27  5:26 ` Randy Brukardt
  0 siblings, 1 reply; 3+ messages in thread
From: Blady @ 2020-12-26  9:12 UTC (permalink / raw)


Hello,

In the following test program, line 16 the string literal is correctly 
inferred to Wide_Wide_String and line 17 the string literal is correctly 
inferred to My_Type1 as aspect String_Literal is defined line 7 for this 
type.
But line 18, GNAT reports a type match error in a "if expression" with 
string literals.
String literal in the "if expression" has to qualified by My_Type1 as in 
line 20:

     1. with Ada.Wide_Wide_Text_IO;
      2. procedure Test_20201225_str_lit is
      3.
      4.    type My_Type1 (Length : Natural) is record
      5.       Value : Wide_Wide_String (1 .. Length);
      6.    end record with
      7.       String_Literal => From_String1;
      8.    function From_String1 (Value : Wide_Wide_String) return 
My_Type1 is ((Length => Value'Length, Value => Value));
      9.    procedure Print1 (Self : My_Type1) is
     10.    begin
     11.       Ada.Wide_Wide_Text_IO.Put_Line (Self.Value);
     12.    end Print1;
     13.
     14.    function Test return Boolean is (True);
     15.
     16.    S : Wide_Wide_String := (if Test then "test0" else "");
     17.    MV0 : My_Type1 := "test0";
     18.    MV1 : My_Type1 := (if Test then "test1" else "");
                               |
         >>> expected type "My_Type1" defined at line 4
         >>> found a string type

     19.    MV2 : My_Type1 := My_Type1'(if Test then "test2" else "");
                                        |
         >>> expected type "My_Type1" defined at line 4
         >>> found a string type

     20.    MV3 : My_Type1 := (if Test then My_Type1'("test3") else "");
     21.
     22. begin
     23.    Print1 ("Test ""string""");
     24.    Print1 ("88");
     25.    Print1 (MV1);
     26.    Print1 (MV2);
     27. end Test_20201225_str_lit;

Shouldn't GNAT set the type My_Type1 for the string literal in line 18 
as it does line 17?

Thanks, Pascal.

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Type inference with String_Literal.
  2020-12-26  9:12 Type inference with String_Literal Blady
@ 2020-12-27  5:26 ` Randy Brukardt
  2021-01-02  9:43   ` Blady
  0 siblings, 1 reply; 3+ messages in thread
From: Randy Brukardt @ 2020-12-27  5:26 UTC (permalink / raw)


Well, the rules for conditional expressions are separate from the rules for 
literals, so it certainly is possible for them to get different results.

However, it appears to me that 4.5.7(13/3) applies to your examples on line 
18 (the expression is "expected" to be of type My_Type1) and on line 19 
(here the expression "shall resolve to" My_Type1). If that rule applies, 
then the dependent expressions should be resolved individually using 
My_Type1 - which obviously should work. So I think this is a bug in GNAT, 
although I'm not 100% certain (if the rule doesn't apply, then there isn't 
enough context to resolve the expression -- but I don't know why the rule 
wouldn't apply).

                              Randy.

"Blady" <p.p11@orange.fr> wrote in message 
news:rs6uql$uj1$1@gioia.aioe.org...
> Hello,
>
> In the following test program, line 16 the string literal is correctly 
> inferred to Wide_Wide_String and line 17 the string literal is correctly 
> inferred to My_Type1 as aspect String_Literal is defined line 7 for this 
> type.
> But line 18, GNAT reports a type match error in a "if expression" with 
> string literals.
> String literal in the "if expression" has to qualified by My_Type1 as in 
> line 20:
>
>     1. with Ada.Wide_Wide_Text_IO;
>      2. procedure Test_20201225_str_lit is
>      3.
>      4.    type My_Type1 (Length : Natural) is record
>      5.       Value : Wide_Wide_String (1 .. Length);
>      6.    end record with
>      7.       String_Literal => From_String1;
>      8.    function From_String1 (Value : Wide_Wide_String) return 
> My_Type1 is ((Length => Value'Length, Value => Value));
>      9.    procedure Print1 (Self : My_Type1) is
>     10.    begin
>     11.       Ada.Wide_Wide_Text_IO.Put_Line (Self.Value);
>     12.    end Print1;
>     13.
>     14.    function Test return Boolean is (True);
>     15.
>     16.    S : Wide_Wide_String := (if Test then "test0" else "");
>     17.    MV0 : My_Type1 := "test0";
>     18.    MV1 : My_Type1 := (if Test then "test1" else "");
>                               |
>         >>> expected type "My_Type1" defined at line 4
>         >>> found a string type
>
>     19.    MV2 : My_Type1 := My_Type1'(if Test then "test2" else "");
>                                        |
>         >>> expected type "My_Type1" defined at line 4
>         >>> found a string type
>
>     20.    MV3 : My_Type1 := (if Test then My_Type1'("test3") else "");
>     21.
>     22. begin
>     23.    Print1 ("Test ""string""");
>     24.    Print1 ("88");
>     25.    Print1 (MV1);
>     26.    Print1 (MV2);
>     27. end Test_20201225_str_lit;
>
> Shouldn't GNAT set the type My_Type1 for the string literal in line 18 as 
> it does line 17?
>
> Thanks, Pascal.
> 


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Type inference with String_Literal.
  2020-12-27  5:26 ` Randy Brukardt
@ 2021-01-02  9:43   ` Blady
  0 siblings, 0 replies; 3+ messages in thread
From: Blady @ 2021-01-02  9:43 UTC (permalink / raw)


Le 27/12/2020 à 06:26, Randy Brukardt a écrit :
> Well, the rules for conditional expressions are separate from the rules for
> literals, so it certainly is possible for them to get different results.
> 
> However, it appears to me that 4.5.7(13/3) applies to your examples on line
> 18 (the expression is "expected" to be of type My_Type1) and on line 19
> (here the expression "shall resolve to" My_Type1). If that rule applies,
> then the dependent expressions should be resolved individually using
> My_Type1 - which obviously should work. So I think this is a bug in GNAT,
> although I'm not 100% certain (if the rule doesn't apply, then there isn't
> enough context to resolve the expression -- but I don't know why the rule
> wouldn't apply).

Thanks Randy for your answer, I've just sent a report to Adacore.

Happy new year to all Ada followers, Pascal.

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2021-01-02  9:43 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-12-26  9:12 Type inference with String_Literal Blady
2020-12-27  5:26 ` Randy Brukardt
2021-01-02  9:43   ` Blady

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