GNAT 16.1: fully qualified name resolved to the current package when instantiating a generic child — bug?

Building OpenToken-based ArchiCheck with the freshly released GNAT 16.1.0 (via
Alire), I hit a name resolution error that, as far as I can tell, should be
legal Ada:

main.adb:15:04: error: instantiation error at opentoken-production-parser-lalr-parser.ads:4
main.adb:15:04: error: type "Parser.Instance" cannot be used before end of its declaration
main.adb:15:04: error: instantiation error at opentoken-production-parser-lalr-parser.ads:8
main.adb:15:04: error: subprogram "Parse" is not overriding

The structure below is distilled from OpenToken 6.0c; the generic formal
parameters are removed, they are not needed to trigger the error:

-- opentoken.ads
package OpenToken is end OpenToken;

-- opentoken-production.ads
generic
package OpenToken.Production is
end OpenToken.Production;

-- opentoken-production-parser.ads
generic
package OpenToken.Production.Parser is
   type Instance is abstract tagged null record;
   procedure Parse (Parser : in out Instance) is abstract;
end OpenToken.Production.Parser;

-- opentoken-production-parser-lalr.ads
generic
package OpenToken.Production.Parser.LALR is
end OpenToken.Production.Parser.LALR;

-- opentoken-production-parser-lalr-parser.ads
generic
package OpenToken.Production.Parser.LALR.Parser is
   type Instance is new OpenToken.Production.Parser.Instance
     with null record;
   overriding
   procedure Parse (Parser : in out Instance) is null;
end OpenToken.Production.Parser.LALR.Parser;
-- main.adb
with OpenToken.Production;
with OpenToken.Production.Parser;
with OpenToken.Production.Parser.LALR;
with OpenToken.Production.Parser.LALR.Parser;

procedure Main is
   package Production  is new OpenToken.Production;
   package Parser      is new Production.Parser;
   package LALRs       is new Parser.LALR;
   package LALR_Parser is new LALRs.Parser;
begin
   null;
end Main;

Observations, all verified on GNAT 16.1.0 (default switches, Windows):

  1. The template compiles on its own (gcc -c opentoken-production-parser-lalr-parser.ads). The error appears only at the last instantiation, of a generic child through the parent’s instance.
  2. Renaming the grandchild to OpenToken.Production.Parser.LALR.Lalr_Parser — so it no longer shares its simple name with its grandparent OpenToken.Production.Parser — makes everything compile, with the reference OpenToken.Production.Parser.Instance left unchanged. Which suggests a name resolution issue, not a legality rule.
  3. Adding package Grandparent renames OpenToken.Production.Parser; in the grandchild and deriving from Grandparent.Instance does not work around it: the error becomes type "Grandparent.Instance" cannot be used before end of its declaration.

Real-world impact: OpenToken 6.0c (6.0.3 in the Alire index) no longer builds on GNAT 16.1. The same sources have been built fine with earlier GNAT releases for years. But maybe GNAT was wrong before, and is right now!

I searched GCC Bugzilla and this forum and found no report of this; the GNAT 16.1.0-rc1 call-for-testing thread doesn’t mention it either. If the consensus is “compiler bug”, I’ll file it in GCC Bugzilla with the repro.

(And this confirm once more that you don’t want to depend on an unmaintained lib).

It is certainly a bug, at least in the error message: type Parser.Instance is impossible because Parser.Instance is not a defining_identifier purely syntactically.

As for reporting. Well, generics will likely never be fixed. They mend them in one place tearing in another. I would change the name. Instance is an awful choice.
(Using grammars is even worse :grinning_face:)

Where can I find it? I’ve got only 6.0b. Googling only finds very old versions. GitHub null.

No download found.

Long story, Stephen was no more maintaining it, and I had no answer from the maintainer of the Debian Ada port, so I forked it here to publish some fixes I did on the b version in 2018.

Here is the relevant part of the README:

Note that OpenToken is no more maintained

The successor of OpenToken is available on Stephen Leake home : WisiToken

This release was made available for the creation of a specific crates, when porting archicheck on ALire.

The modifications respect to the last official version (the 6.0b) are :

  • [Fixed] Analyzer.Reset missing in Ada_Lexer Set_Input_Feeder procedure
  • [Added] Annotation support added in the Java_Lexer (MetaData starting with @)
  • [Changed] Buffer_Size of the Tokenizer in Java_Lexer increased to 8192
  • [Changed] cosmetic modifications in comments to comply with gnat standards
  • [Changed] some useless “with” removed

NB : Due to Alire using semantic versioning, 6.0c is named 6.0.3 in Alire

Lionel Draghi, april 2024

We are no longer the knights who say, “Ni!”

Monty Python and the Holy Grail (emphasis added)

Oops, that’s my native French showing! “No more maintained” may sound perfectly natural in French, but indeed it makes no sense in English. Thanks for the Monty Python lesson! :grinning_face_with_smiling_eyes:

Thanks for directing me to the sources.

Funny, I put “OpenToken” into the search field of GitHub as claimed by search tips, but found all kind of strings but not this one. What a useless search implementation!

BTW: The Java Lexer and the Ada Lexer were written by me. Also the very basic HTML Lexer.
I even used OpenToken for a professional application. Long long ago.

Are 6.0c and 6.0.3 releases identical, they have different release dates?

The link to WisiToken in ReadMe is dead - the domain is gone.

I can forward the explanation I wrote to Stephen regarding the fixes, if your’re interested.

Yes, 6.0c was not compliant with Alire version scheme, so it’s just a rename.

Oh yes, please…need 20 chars :frowning:

Annotation processing in Java_Lexer (jan 2018)

Package: libopentoken6.1-dev
Version: 6.0b-7
Severity: normal

Dear Maintainer,

Java Annotation, that is identifier starting with @, are not processed by the current Java_Lexer.

Here is an exemple of Java code :

/\*\* 
 \* Test class for {@link PetTypeFormatter} 
 \* 
 \* @author Colin But 
 \*/ 
@RunWith(MockitoJUnitRunner.class) 
public class PetTypeFormatterTests { 

    @Mock 
    private PetRepository pets; 

    private PetTypeFormatter petTypeFormatter; 

    @Before 
    public void setup() { 
        this.petTypeFormatter = new PetTypeFormatter(pets); 
    } 
... 

} 

The Java lexer do not recognize @ character in code.

It’s Ok in comments, and so no problem with Javadoc tags, but it raises an OpenToken.Syntax_Error exception when met in code.
So, annotations like @Override cause the analysis interruption.

I added a new Annotation_T token to the Java lexer, the patch file is joined.

(note that the last part of the patch is my proposal for bug Bug#886507, that is the buffer increase).

Best regards,

--

– Lionel

infinite loop in Tokenizer.Find_Best_Match

Package: libopentoken6.1-dev
Version: 6.0b-7
Severity: normal

Dear Maintainer,

When the size of a comment is close to Max_Buffer_Size (default = 1024),
Find_Best_Match may enter an infinite loop or have an incoherent behavior.

The provided test case contains a comment ending with “123”.

  • ending with “123”, the comment length is 1026 : bug, infinite loop.
    This is the behavior for Max_Buffer_Size + 2 or more longer comment.

  • removing the “3”, length = 1025 : bug, it fails with the message :

    24: 7:60 Unrecognized character ‘’’
    (This character is in the first comment line)

  • removing the “2”, length = 1024 :

    raised OPENTOKEN.PROGRAMMER_ERROR : token larger than buffer size of 1024
    Normal behavior (I suppose that this is the expected behavior for
    Max_Buffer_Size or longer comments).

  • removing the “1”, length = 1023 :
    Normal behavior

I didn’t further investigate the bug (the code is quite complex).

Workarround :
This size is a parameter of the Analyzer initialization (functions
OpenToken.Token.Enumerated.Analyzer.Initialize).
For the Java lexer, as the syntax is exposed in the spec, it’s possible to
declare another Analyzer in user code with different defaults.

But, to avoid users a long investigation, I suggest to change the default size
of the buffer to 4K, as lots of Java comments may be longer thant 1K.

Last line in java_lexer.ads :

   Analyzer : constant Tokenizer.Handle := Tokenizer.Initialize (Syntax, 
Buffer_Size => 4096); 

Lionel

I joined : test main, gpr, Makefile and Java source

NodeEventTarget.java :

public interface NodeEventTarget extends EventTarget { 

    EventSupport getEventSupport(); 
   // Comment size= 1023 
    /\*\* 
     \*  This method allows the dispatch of events into the implementation's 
     \* event model. The event target of the event is the 
     \* <code>EventTarget</code> object on which <code>dispatchEvent</code> 
     \* is called. 
     \* @param evt  The event to be dispatched. 
     \* @return  Indicates whether any of the listeners which handled the 
     \*   event called <code>Event.preventDefault()</code>. If 
     \*   <code>Event.preventDefault()</code> was called the returned value 
     \*   is <code>false</code>, else it is <code>true</code>. 
     \* @exception EventException 
     \*    UNSPECIFIED_EVENT_TYPE_ERR: Raised if the <code>Event.type</code> 
     \*   was not specified by initializing the event before 
     \*   <code>dispatchEvent</code> was called. Specification of the 
     \*   <code>Event.type</code> as <code>null</code> or an empty string 
     \*   will also trigger this exception. 
     \*   <br> DISPATCH_REQUEST_ERR: Raised if the <code>Event</code> object is 
     \*   already being dispatched 
     \*/ 

    EventSupport getEventSupport(); 
   // Comment size= 1024 
    /\*\* 
     \*  This method allows the dispatch of events into the implementation's 
     \* event model. The event target of the event is the 
     \* <code>EventTarget</code> object on which <code>dispatchEvent</code> 
     \* is called. 
     \* @param evt  The event to be dispatched. 
     \* @return  Indicates whether any of the listeners which handled the 
     \*   event called <code>Event.preventDefault()</code>. If 
     \*   <code>Event.preventDefault()</code> was called the returned value 
     \*   is <code>false</code>, else it is <code>true</code>. 
     \* @exception EventException 
     \*    UNSPECIFIED_EVENT_TYPE_ERR: Raised if the <code>Event.type</code> 
     \*   was not specified by initializing the event before 
     \*   <code>dispatchEvent</code> was called. Specification of the 
     \*   <code>Event.type</code> as <code>null</code> or an empty string 
     \*   will also trigger this exception. 
     \*   <br> DISPATCH_REQUEST_ERR: Raised if the <code>Event</code> object is 
     \*   already being dispatched1 
     \*/ 

    EventSupport getEventSupport(); 
    // Comment size= 1025 
    /\*\* 
     \*  This method allows the dispatch of events into the implementation's 
     \* event model. The event target of the event is the 
     \* <code>EventTarget</code> object on which <code>dispatchEvent</code> 
     \* is called. 
     \* @param evt  The event to be dispatched. 
     \* @return  Indicates whether any of the listeners which handled the 
     \*   event called <code>Event.preventDefault()</code>. If 
     \*   <code>Event.preventDefault()</code> was called the returned value 
     \*   is <code>false</code>, else it is <code>true</code>. 
     \* @exception EventException 
     \*    UNSPECIFIED_EVENT_TYPE_ERR: Raised if the <code>Event.type</code> 
     \*   was not specified by initializing the event before 
     \*   <code>dispatchEvent</code> was called. Specification of the 
     \*   <code>Event.type</code> as <code>null</code> or an empty string 
     \*   will also trigger this exception. 
     \*   <br> DISPATCH_REQUEST_ERR: Raised if the <code>Event</code> object is 
     \*   already being dispatched12 
     \*/ 

    EventSupport getEventSupport(); 
    // Comment size= 1026 
    /\*\* 
     \*  This method allows the dispatch of events into the implementation's 
     \* event model. The event target of the event is the 
     \* <code>EventTarget</code> object on which <code>dispatchEvent</code> 
     \* is called. 
     \* @param evt  The event to be dispatched. 
     \* @return  Indicates whether any of the listeners which handled the 
     \*   event called <code>Event.preventDefault()</code>. If 
     \*   <code>Event.preventDefault()</code> was called the returned value 
     \*   is <code>false</code>, else it is <code>true</code>. 
     \* @exception EventException 
     \*    UNSPECIFIED_EVENT_TYPE_ERR: Raised if the <code>Event.type</code> 
     \*   was not specified by initializing the event before 
     \*   <code>dispatchEvent</code> was called. Specification of the 
     \*   <code>Event.type</code> as <code>null</code> or an empty string 
     \*   will also trigger this exception. 
     \*   <br> DISPATCH_REQUEST_ERR: Raised if the <code>Event</code> object is 
     \*   already being dispatched123 
     \*/ 
} 

Makefile :

all: build check 

build: 
        gprbuild -P run_java_lexer.gpr 

check: 
        obj/run_java_lexer NodeEventTarget.java 

run_java_lexer.adb :

with Java_Lexer;       use Java_Lexer; 
with Ada.Text_IO;      use Ada.Text_IO; 
with Ada.Command_Line; use Ada.Command_Line; 
with Ada.Exceptions; 
with OpenToken; 
with OpenToken.Text_Feeder.Text_IO; 

procedure Run_Java_Lexer is 
   File : File_Type; 

begin 
   Ada.Text_IO.Open (File, In_File, Argument (1)); 

   -- Set_Input_Feeder (File); 
   Ada.Text_IO.Set_Input (File); 
   Analyzer.Reset; 
   Analyzer.Set_Text_Feeder (OpenToken.Text_Feeder.Text_IO.Create 
(Ada.Text_IO.Current_Input)); 

   loop 
      exit when End_Of_File (File); 
      Analyzer.Find_Next; 

      Put_Line (Natural'Image (Analyzer.Line) 
                & ": " & Java_Token'Image (Analyzer.ID) 
                & ", " & Analyzer.Lexeme); 

   end loop; 

exception 
   when E : OpenToken.Syntax_Error => 
      Put_Line (Count'Image (Line (File)) & ": " & 
Ada.Exceptions.Exception_Message (E)); 

end Run_Java_Lexer; 

run_java_lexer.gpr :

-- with "../../patched_ot"; 

project run_java_lexer is 

   for Source_Dirs use ("./src"); 
   for Object_Dir use "./obj"; 
   for Main use ("run_java_lexer.adb"); 

   package Ide is 
      for Vcs_Kind use "Git"; 
      -- for Documentation_Dir use "./html"; 
   end Ide; 

   package Builder is 
      for Executable ("run_java_lexer.adb") use "run_java_lexer"; 
      for Default_Switches ("ada") use ("-j2"); 
   end Builder; 

   package Compiler is 
      Common_Switches := ("-gnat2012"); 
      Common_Debug_Switches := Common_Switches & 
      ("-g", "-O0",  "-gnatQ", "-gnatf",  "-gnato", "-fstack-check", "-gnata", 
"-gnateE", "-gnatVa", "-gnatwa", "-gnatyabefhiklnprtx"); 
      -- -gnatQ : Don't quit 
      -- -gnatf : Full errors 
      -- -gnato : Intermediate overflows to be handled in strict mode. 
      -- -fstack-check : stack checking 
      -- -gnata : assertions 
      -- -gnateE : extra infos in exception messages 
      -- -gnatVa : all validity checks 
      -- -gnatwa : activate most optional warning messages 
      -- 
      -- Style checking : 
      -- a : Check attribute casing 
      -- b : Blanks not allowed at statement end 
      -- e : Check end/exit labels 
      -- f : No form feeds or vertical tabs 
      -- h : No horizontal tabs 
      -- i : Check if-then layout 
      -- k : Check keyword casing 
      -- l : Check layout 
      -- n : Check casing of entities in Standard 
      -- p : Check pragma casing 
      -- r : Check references 
      -- t : Check token spacing 
      --removed  M100 : Set maximum line length 
      -- x : Check extra parentheses. 

      for Switches ("ada") use Common_Debug_Switches; 

   end Compiler; 

   package Linker is 
      for Switches ("ada") use ("-g", "-lgcov"); 
   end Linker; 

   package Binder is 
      for Switches ("ada") use ("-Es"); 
   end Binder; 

   package Pretty_Printer is 
      for Switches ("ada") use ("-M100", "-l2", "-A1", "-A2", "-A3", "-A4"); 
   end Pretty_Printer; 

end run_java_lexer; 

– System Information:
Debian Release: buster/sid
APT prefers testing
APT policy: (500, ‘testing’)
Architecture: amd64 (x86_64)

Kernel: Linux 4.14.0-2-amd64 (SMP w/2 CPU cores)
Locale: LANG=fr_FR.utf8, LC_CTYPE=fr_FR.utf8 (charmap=UTF-8), LANGUAGE=fr_FR.utf8 (charmap=UTF-8)
Shell: /bin/sh linked to /bin/dash
Init: systemd (via /run/systemd/system)

Versions of packages libopentoken6.1-dev depends on:
ii gnat 7
ii gnat-7 7.2.0-18
ii libopentoken9.1 6.0b-7

libopentoken6.1-dev recommends no packages.

Versions of packages libopentoken6.1-dev suggests:
ii libopentoken-doc 6.0b-7

– no debconf information

--
– Lionel

Ada lexer not well reset with a call to Set_Text_Feeder

Package: libopentoken6-dev
Version: 6.0b-5+b2
File: libopentoken6
Severity: normal

Dear Maintainer,

while processing a bunch of Ada files, the OpenToken Ada Lexer is sometimes ignoring the call to Set_Input_Feeder, and continue to parse the previously opened Ada file.

I read this in OpenToken.Token.Enumerated.Analyzer :

-------------------------------------------------------------------- 
    --  Reset Analyzer, to start finding tokens. This is appropriate 
    --  when the Feeder text has been changed. 
-------------------------------------------------------------------- 
    procedure Reset (Analyzer : in out Instance); 

It seems appropriate, but it’s not done in the Ada lexer.

For the Java lexer, no problem, as the analyzer instantiation is exposed in the package spec, the user can add a call to Analyzer.Reset just before Analyzer.Set_Text_Feeder.

But, for the Ada lexer, as the analyzer instantiation is hidden in the body, and there no Reset procedure available in the spec, there is no solution on user side.

I suggest to patch Set_Input_Feeder in ada_lexer.adb to do the same thing :

    procedure Set_Input_Feeder (File : in Ada.Text_IO.File_Type) is 
    begin 
       Ada.Text_IO.Set_Input (File); 
       Analyzer.Reset; --> added line 
       Analyzer.Set_Text_Feeder (OpenToken.Text_Feeder.Text_IO.Create (Ada.Text_IO.Current_Input)); 
    end Set_Input_Feeder; 

thanks,

–
– Lionel

PS : unable to send something with reportbug!

– System Information:

Debian Release: 9.2
APT prefers stable-updates
APT policy: (500, ‘stable-updates’), (500, ‘stable’)
Architecture: amd64 (x86_64)

Kernel: Linux 4.9.0-4-amd64 (SMP w/2 CPU cores)
Locale: LANG=fr_FR.utf8, LC_CTYPE=fr_FR.utf8 (charmap=UTF-8), LANGUAGE=fr_FR.utf8 (charmap=UTF-8)
Shell: /bin/sh linked to /bin/dash
Init: systemd (via /run/systemd/system)

Versions of packages libopentoken6-dev depends on:
ii gnat 6.1
ii gnat-6 6.3.0-18
ii libopentoken9 6.0b-5+b2

libopentoken6-dev recommends no packages.

libopentoken6-dev suggests no packages.

– no debconf information

On this one, Stephe answered:

Yes. It’s unlikely there will be another release of OpenToken, at least by me; I’ve moved on to WisiToken, which has no release yet, but is a significant rewrite that simplifies a lot of things, and adds error correction.

It was in dec 2017.

Interesting!

— Comment #6 from Eric Botcazou —
The resolution in the instance has always been incorrect, but it was salvaged
by a kludge present in Sem_Ch8.Premature_Usage until r16-1214:
[gcc r16-1214] ada: Remove dead code

Fixed in 16.3 and later.

Who said fixing that GCC bug was taking forever? :grinning_face: