Weird error: nonoverridable aspect "String_Literal"

Consider this code:
(from AARM 4.2.1 User-Defined Literals)

13.    Max_Roman_Number : constant := 3_999;  -- MMMCMXCIX
14. 
15.    type Roman_Number is range 1 .. Max_Roman_Number
16.      with String_Literal => To_Roman_Number;
17. 
18.    function To_Roman_Number (S : Wide_Wide_String) return Roman_Number;

I got:
error: nonoverridable aspect "String_Literal" of type "Roman_Number" requires "To_Roman_Number" declared at line 18 to be a primitive operation

What could its meaning possibly be?

Are you compiling with Ada 2022 enabled? -gnat2022

IIRC String_Literal is Ada 2022 only. The error may come from an issue in the compiler not detecting correctly the function.

The error is correct but not useful if you’re doing what I think you’re doing. Put your declarations inside of a package and it should work. I don’t remember off the top of my head why a package is required, however I do remember that that RM is very clear about the fact that it is.

It comes from the definition of primitives in 3.2.3, specifically the fact that there’s no equivalent of this for declarative regions:

For a specific type declared immediately within a package_specification, any subprograms (in addition to the enumeration literals) that are explicitly declared immediately within the same package_specification and that operate on the type

Thanks for quoting that!

It’s not very clear to me why operations are not primitive in a procedure declaration part.

Note: declaring a package in the procedure declaration part solve the issue!

procedure Test_SL_decl is
   package Roman is
   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;
   end;
...
 X : Roman.Roman_Number;
...

Both RM and GNAT compiler messages should be clearer on this topic.

@Irvise Yes with -gnat2022, I hadn’t mentioned it.

I encountered something similar when playing with the Put_Image aspect, but I cannot reproduce it with your code.
It would be better if you provided a reproducible example.

The following compiles with $ gnatmake test_sl_decl.adb and GCC 14.3.1 and with 16.1.0 (no -gnat2022 needed).

with Ada.Text_IO;
procedure Test_SL_decl is

   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 is (1);

   X : constant Roman_Number := "I";
begin
   Ada.Text_IO.Put_Line (X'Image);
end Test_SL_decl;