Skip to content

Resolve negative indices at run time - #877

Merged
mristin merged 1 commit into
mainfrom
mristin/Resolve-negative-indices-at-run-time
Nov 2, 2026
Merged

mristin merged 1 commit into
mainfrom
mristin/Resolve-negative-indices-at-run-time

Conversation

@mristin

@mristin mristin commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

In C++, C#, Java and Go, a negative list index known only at run time was not counted from the back as in Python, and C++ xs[-1] on an empty list was undefined behavior via .back(). We now route all list and JSON-array indices, literals included, through a per-target helper which resolves a negative index and throws when out of range so that the behavior in all targets is equal to that of Python:

  • C++: common::At(xs, i),
  • C#: Common.ListHelpers.At(xs, i) and SetAt(xs, i, v),
  • Java: ListHelpers.at(xs, i) and ListHelpers.setAt(xs, i, v),
  • Go: ourcommon.At(xs, i) and *ourcommon.PointerAt(xs, i) = v.

Go writes go through a pointer since Go can not infer T in SetAt[T]([]T, int64, T) for a *Class written into an []IClass. TypeScript OurCommon.at now throws a RangeError instead of returning undefined. The helpers are generated only if the meta-model indexes a list.

We test reads and writes at negative run-time indices in the common meta-model int_length_interplay.

In C++, C#, Java and Go, a negative list index known only at run time
was not counted from the back as in Python, and C++ ``xs[-1]`` on an
empty list was undefined behavior via ``.back()``. We now route all
list and JSON-array indices, literals included, through a per-target
helper which resolves a negative index and throws when out of range
so that the behavior in all targets is equal to that of Python:

* C++: ``common::At(xs, i)``,
* C#: ``Common.ListHelpers.At(xs, i)`` and ``SetAt(xs, i, v)``,
* Java: ``ListHelpers.at(xs, i)`` and ``ListHelpers.setAt(xs, i, v)``,
* Go: ``ourcommon.At(xs, i)`` and ``*ourcommon.PointerAt(xs, i) = v``.

Go writes go through a pointer since Go can not infer ``T`` in
``SetAt[T]([]T, int64, T)`` for a ``*Class`` written into an
``[]IClass``. TypeScript ``OurCommon.at`` now throws a ``RangeError``
instead of returning ``undefined``. The helpers are generated only if
the meta-model indexes a list.

We test reads and writes at negative run-time indices in the common
meta-model ``int_length_interplay``.
@coveralls

Copy link
Copy Markdown

Coverage Report for CI Build 41426363218

Coverage decreased (-0.008%) to 90.341%

Details

  • Coverage decreased (-0.008%) from the base build.
  • Patch coverage: 11 uncovered changes across 3 files (102 of 113 lines covered, 90.27%).
  • 3 coverage regressions across 2 files.

Uncovered Changes

File Changed Covered %
aas_core_codegen/golang/transpilation.py 25 20 80.0%
aas_core_codegen/csharp/transpilation.py 29 26 89.66%
aas_core_codegen/java/transpilation.py 17 14 82.35%
Total (9 files) 113 102 90.27%

Coverage Regressions

3 previously-covered lines in 2 files lost coverage.

File Lines Losing Coverage Coverage
aas_core_codegen/java/transpilation.py 2 83.19%
aas_core_codegen/cpp/transpilation.py 1 82.45%

Coverage Stats

Coverage Status
Relevant Lines: 52056
Covered Lines: 47028
Line Coverage: 90.34%
Coverage Strength: 2.71 hits per line

💛 - Coveralls

@mristin
mristin merged commit 2aa1eec into main Nov 2, 2026
5 checks passed
@mristin
mristin deleted the mristin/Resolve-negative-indices-at-run-time branch November 2, 2026 10:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants