Skip to content

Commit

Permalink
fix: wrong"assignment/nothing-assigned" error if RHS calls expressi…
Browse files Browse the repository at this point in the history
…on lambda (#781)

### Summary of Changes

If the RHS of an assignment calls an expression lambda, an incorrect
`"assignment/nothing-assigned"` was shown on the first assignee. This PR
fixes this.
  • Loading branch information
lars-reimann authored Nov 19, 2023
1 parent 01a5c03 commit b909cb8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
10 changes: 10 additions & 0 deletions packages/safe-ds-lang/src/language/helpers/safe-ds-node-mapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
isSdsCallable,
isSdsClass,
isSdsEnumVariant,
isSdsExpressionLambda,
isSdsNamedType,
isSdsParameter,
isSdsReference,
Expand Down Expand Up @@ -121,6 +122,15 @@ export class SafeDsNodeMapper {
}
}

// If the RHS calls an expression lambda, the first assignee gets its result
if (isSdsExpressionLambda(callable)) {
if (assigneePosition === 0) {
return callable.result;
} else {
return undefined;
}
}

// Otherwise, the assignee gets the result at the same position
const abstractResults = getAbstractResults(callable);
return abstractResults[assigneePosition];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,18 @@ describe('SafeDsNodeMapper', () => {
`,
expected: ['r1', 'r2'],
},
{
name: 'expression lambda',
code: `
segment mySegment() {
val f = () -> 1;
val a, val b = f();
};
`,
expected: ['1', undefined],
index: 1,
},
{
name: 'function (one result)',
code: `
Expand Down Expand Up @@ -179,10 +191,12 @@ describe('SafeDsNodeMapper', () => {

const abstractResultNameOrNull = (node: SdsAssignee): string | undefined => {
const assignedObject = nodeMapper.assigneeToAssignedObject(node);
if (isSdsAbstractResult(assignedObject)) {
if (!assignedObject) {
return undefined;
} else if (isSdsAbstractResult(assignedObject)) {
return assignedObject.name;
} else {
return undefined;
return assignedObject.$cstNode?.text;
}
};
});
Expand Down

0 comments on commit b909cb8

Please sign in to comment.