Handle empty lambda expressions in UnitsCompiler (fix #248) #280
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #248.
Deriving units for a lambda expression while its child list is still empty
(e.g., immediately after the lambda node is constructed, before
addChild()has been called) could cause an
IndexOutOfBoundsExceptioninUnitsCompiler.lambda(List<ASTNode2>).Previously,
lambda(...)always did:without checking whether values was empty, so an empty child list led to a
call to
values.get(-1).Changes
Updated UnitsCompiler.lambda(List values) to guard against an empty list:
valuesisnullor empty, the method now returnsinvalid(), i.e., anASTNode2ValuewhoseUnitDefinitionis marked asINVALID. This avoids out-of-bounds access while signalling that units for a bare lambda expression (without arguments/body yet) are unknown at that time.For non-empty
values, behaviour is unchanged:All but the last child are treated as arguments and stored in
namesToUnits.The units of the last child (the body) are used as the lambda’s result units.
Tests
Added UnitsCompilerLambdaTest under
core/test/org/sbml/jsbml/math/compiler:testLambdaWithNoChildrenDoesNotThrow():Calls new
UnitsCompiler(3, 1).lambda(Collections.<ASTNode2>emptyList()).Asserts that no
IndexOutOfBoundsExceptionis thrown.Asserts that the returned units are marked as invalid.
Ran:
Both complete with
BUILD SUCCESSon Java 11.