Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 126 additions & 0 deletions features/rfc-spark-constant-in-iterator.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
- Feature ID: spark-constant-in-iterator
- Start Date: 2026-01-09
- Status: Design

Summary
=======

The Iterable aspect is a GNAT specific aspect that provides user defined iteration over containers. Currently, it requires `First`, `Next`, and `Has_Element` functions to iterate over the cursors of the container at minimum. An `Element` function can be added to obtain iteration over the content of the container (`for Item of C loop`, instead of `for Position in Container` loop). In a use-case when the elements of the container are
fetched only for reading the data, this method is unnecessarily slow as it
involves copying the element.
The performance issue can be solved by adding an optional `Constant_Reference` function returning an anonymous access-to-constant view of the element instead of copy.

Motivation
==========

The use of Constant_Reference would avoid copying the element, in particular in quantified expressions where using a while loop is not an alternative (at least not for SPARK).

Guide-level explanation
=======================


As an example, we currently have:

```ada
type List (Capacity : Count_Type) is private
with
Iterable =>
(First => First,
Next => Next,
Has_Element => Has_Element,
Element => Element),

function Element (Container : List; Position : Cursor) return Element_Type;
```

The current proposal adds an alternative prototype:

```ada
type List (Capacity : Count_Type) is private
with
Iterable =>
(First => First,
Next => Next,
Has_Element => Has_Element,
Constant_Reference => Constant_Reference),

function Constant_Reference (Container : List; Position : Cursor) return not null access constant Element_Type;
```

When Constant_Reference is specified for an Iterable then the following loop

```ada
for E of Container loop
P (E);
end loop;
```

will be expanded to code corresponding to

```ada
declare
Position : Cursor := First (Container);
begin
while Has_Element (Container, Position) loop
declare
Ref : constant access constant Element_Type := Constant_Reference (Container, Position);
E : Element_Type renames Ref.all;
begin
P (E);
end;
Position := Next (Container, Position);
end loop;
end;
```

Reference-level explanation
===========================

The main scenario is already explained in the Guide-level explanation section.
Specifying both Element and Constant_Reference shall not be possible.

Rationale and alternatives
==========================

An alternative to this proposal would be [5.5.3 Procedural Iterators](https://ada-rapporteur-group.github.io/ARM/Ada_202Y/AA-5-5-3.html). That would solve the
problem of potentially dangling reference. Reference would be passed as IN parameter and it's lifetime is strictly limited. However, it would create a different kind of performance issue as the compiler optimisation would be
disabled in some cases. The LD group the decided to drop this alternative
because, despite of being safer, wouldn't solve the original problem outlined
of the summary of this RFC.

Another possible enhancement would be allowing private functions in the
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you clarify what you mean by a "private function", perhaps with an example?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Nikokrock, do you have an example for this? You mentioned that this has been discussed earlier.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

type Bytes is private
   with Iterable => (First       => First,
                     Next        => Next,
                     Has_Element => Has_Element,
                     Element     => Unsafe_Get);

If you want to have an efficient implementation, it's better to provide a Get_Element that does not checks bounds has this check is already done by Has_Element. Would be great to make in that case Unsafe_Get private (and so only usable through the iterator)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure I understand what you mean by "bounds". Note that Get_Element might have a precondition (not executed in production) and not necessarily defensive coding.

prototype of Iterable. That is considered a much wider change and shall be
considered separately.

Drawbacks
=========

A small caveat is that, since E is a renaming and not an object, it would not be possible to use it at certain places, for example inside Global and Depends contracts. That is considered to be a minor shortcoming.

As a follow-up, we could consider having a way to supply a Reference function that would allow direct mutation inside the container. It is notably more complicated to do that in a SPARK-compatible way though, because of aliasing restrictions, so it may be better to keep it as a separate RFC.

Compatibility
=============

The change does not change the behaviour of legacy code.

Open questions
==============

None.

Prior art
=========


Unresolved questions
====================

TBD

Future possibilities
====================

Allowing private functions in the prototype of Iterable would remove the issue
of uncontrolled dangling pointers.