AdaCore / ada_language_server

Server implementing the Microsoft Language Protocol for Ada and SPARK
GNU General Public License v3.0
226 stars 54 forks source link

Use of 202x container aggregates can cause formatting to fail #1133

Closed robdaemon closed 1 year ago

robdaemon commented 1 year ago

Using the VSCode plugin v23.0.14, I get a failure of ALS during formatting code utilizing a container aggregate.

Snippet of the container:

type Token_Types is (NUMBER_TOKEN, DOT, BINARY_SELECTOR, EOF);

type Token is record
  Token_Type : Token_Types;
  Lexeme     : Unbounded_String;
  Line       : Natural;
end record;

package Token_Vectors is new Ada.Containers.Vectors
  (Index_Type => Natural, Element_Type => Token);

package Source_Expected_Map is new Ada.Containers.Indefinite_Ordered_Maps
  (Key_Type => String, Element_Type => Token_Vectors.Vector);

function Create_Token (Token_Type : Token_Types; Lexeme : String; Line : Positive) return Token;
function Create_Token_Str
  (Token_Type : Token_Types; Lexeme : String; Line : Positive) return Token;
function Create_Token_Integer
  (Token_Type : Token_Types; Lexeme : String; Line : Positive; Value : Long_Integer)
  return Token;

And the code that attempts to get formatted and fails:

declare
  Fixtures : constant Source_Expected_Map.Map :=
    ["16rc0ffeef00d.",
      [Create_Token_Integer (NUMBER_TOKEN, "", 1, 828_927_569_933), Create_Token (DOT, ".", 1),
      Create_Token (EOF, "", 1)]];
begin
  null;
end;

This code is somewhat contrived, but it's copied from my source file that is failing to format. The compiler doesn't have issues with this container aggregate with -gnat2022

The error in the VSCode output window is:

[Error - 3:03:56 PM] Request textDocument/formatting failed.
  Message: plaid-st-scanner-test.adb:1:1: Error formatting node (CompilationUnit). Keeping the initial input selection unchanged
s-wchcnv.adb:75 explicit raise
  Code: -32603 

If you use ( and ) instead of [ and ] for the aggregate, ALS formats the file, but the compiler says container aggregate must use [], not ()

reznikmm commented 1 year ago

I'm able to reproduce this with the next code:

with Ada.Containers.Indefinite_Ordered_Maps;

package Main is
   package Source_Expected_Map is new Ada.Containers.Indefinite_Ordered_Maps
     (Key_Type     => String,
      Element_Type => Integer,
      "="          => "=");

  Map :
   Source_Expected_Map.Map :=
    ["d.",
      1];

end Main;

We will fix that. Thank you!

joaopsazevedo commented 1 year ago

@robdaemon this issue is now fixed in the master branch of libadalang-tools which contains the formatter used by ALS. It will be available in the next release of the ALS VSCode extension. Thank you for reporting and for the reproducer.