Why can't I use subtypes from limited withed packages in subprogram declarations?
In Ada 2012, a limited with can be used from one file (for example, User) to import types from another package (for example, Provider), even when that package needs to import the current file. This is useful in cases where you have two mutually dependent types, such as the following, but you have them in two different packages, so you can't just use an incomplete type declaration like this:
type Point;
type Line; -- incomplete types
type Point is
record
L, M, N: access Line;
end record;
type Line is
record
P, Q, R: access Point;
end record;
(the above example is from John Barnes' Programming in Ada 2012, section 13.5 Mutually dependent types)
However, my question doesn't even need mutually dependent types. I'm trying to compile some Ada code that was autogenerated using gcc -fdump-ada-spec -C some_c_header.h
, which happens to contain some procedures and/or functions that have parameters being passed as access constant
(const *
in C), and I see no reason this shouldn't work using limited with
, as fdump-ada-spec generates for me.
Why can't I use any subtypes from Provider inside a subprogram declaration? Using regular types is fine, but when trying to use subtypes gcc complains they do not exist in that package.
provider.ads
package Provider is
type Fine_Type is range 0 .. 10_000;
subtype Bad_Type is Fine_Type;
end Provider;
user.ads:
limited with Provider;
package User is
procedure Do_It (A : access constant Provider.Bad_Type);
end User;
For completeness, here are stubs for the rest of the files you would need:
user.adb
package body User is
procedure Do_It (A : access constant Provider.Bad_Type) is
begin
null;
end Do_It;
end User;
limited_with_test.gpr
project limited_with_test is
for Source_Dirs use ("src");
for Main use ("main.adb");
for Exec_Dir use "bin";
for Object_Dir use "obj";
for Create_Missing_Dirs use "True";
end limited_with_test;
If you put the sources into a directory called src, you can call gprbuild in the directory containing that directory and the gpr file, and you get:
$ gprbuild
using project file limited_with_test.gpr
Compile
[Ada] main.adb
user.ads:5:49: error: "Bad_Type" not declared in "Provider"
gprbuild: *** compilation phase failed