Hello,
I am working with access types and I am not sure how to resolve a compiler error:
x86_64-linux-gnu-gcc-10 -c test.adb
test.adb:13:29: prefix of "Access" attribute must be aliased
gnatmake: "test.adb" compilation error
package Things is
type Ref is access all Natural;
type RefRef is access all Ref;
type Rec_A is record
Element : access Natural;
end record;
Member : aliased access Natural;
Instance : aliased Rec_A;
end Things;
with Things;
procedure Test is
Target : Things.RefRef;
begin
-- [Assignment 1] Compiles
Target := Things.Member'Access;
-- [Assignment 2] Compilation Error
Target := Things.Instance.Element'Access;
end Test;
Am I missing an ‘aliased’ somewhere?
Thanks,
N
I should note that changing the ‘Element’ element to ‘aliased’ doesn’t seem to help:
package Things is
type Ref is access all Natural;
type RefRef is access all Ref;
type Rec_A is record
Element : aliased access Natural; -- Add 'aliased'
end record;
Member : aliased access Natural;
Instance : aliased Rec_A;
end Things;
I found the solution!
Changing the ‘access Natural’ to ‘Ref’ (with aliased) works.
This is the final version:
package Things is
type Ref is access all Natural;
type RefRef is access all Ref;
type Rec_A is record
Element : aliased Ref; -- [Fixed here]
end record;
Member : aliased access Natural;
Instance : aliased Rec_A;
end Things;
It seems like the original version (Element : aliased access Natural) should work too. Is this a compiler bug?
Are you transliterating C code?
I ask because the double-access is highly unusual. (There are some cases where this is necessary, but as one of the Ada gurus says: “To a first-order approximation, accesses are never needed.”)
Note that, by definition, the refrant of an access is aliased.
1 Like
I have a binding to a C library which uses pointers to data structures.