Skip to content

Commit e8e2bf6

Browse files
feat: ensure that all assignees get a value (#630)
Closes partially #543 ### Summary of Changes Show an error if an assignee might not get a value. --------- Co-authored-by: megalinter-bot <[email protected]>
1 parent 01933b9 commit e8e2bf6

File tree

7 files changed

+105
-7
lines changed

7 files changed

+105
-7
lines changed

language-configuration.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
["(", ")"],
88
["{", "}"],
99
["[", "]"],
10-
["<", ">"],
1110
["»", "«"]
1211
],
1312
"autoClosingPairs": [

package-lock.json

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/language/typing/safe-ds-type-computer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,9 +88,9 @@ export class SafeDsTypeComputer {
8888
private readonly coreClasses: SafeDsClasses;
8989
private readonly nodeMapper: SafeDsNodeMapper;
9090

91-
readonly typeCache: WorkspaceCache<string, Type>;
91+
private readonly typeCache: WorkspaceCache<string, Type>;
9292

93-
constructor(readonly services: SafeDsServices) {
93+
constructor(services: SafeDsServices) {
9494
this.astNodeLocator = services.workspace.AstNodeLocator;
9595
this.coreClasses = services.builtins.Classes;
9696
this.nodeMapper = services.helpers.NodeMapper;

src/language/validation/other/expressions/references.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ export const referenceMustNotBeFunctionPointer = (node: SdsReference, accept: Va
1919
return;
2020
}
2121

22-
//
2322
let container: AstNode | undefined = node.$container;
2423
if (isSdsMemberAccess(container) && node.$containerProperty === 'member') {
2524
container = container.$container;

src/language/validation/other/statements/assignments.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,24 @@
1-
import { isSdsPipeline, SdsYield } from '../../../generated/ast.js';
1+
import { isSdsPipeline, SdsAssignment, SdsYield } from '../../../generated/ast.js';
22
import { getContainerOfType, ValidationAcceptor } from 'langium';
3+
import { SafeDsServices } from '../../../safe-ds-module.js';
4+
import { assigneesOrEmpty } from '../../../helpers/nodeProperties.js';
35

6+
export const CODE_ASSIGMENT_NOTHING_ASSIGNED = 'assignment/nothing-assigned';
47
export const CODE_ASSIGMENT_YIELD_FORBIDDEN_IN_PIPELINE = 'assignment/yield-forbidden-in-pipeline';
58

9+
export const assignmentAssigneeMustGetValue =
10+
(services: SafeDsServices) =>
11+
(node: SdsAssignment, accept: ValidationAcceptor): void => {
12+
for (const assignee of assigneesOrEmpty(node)) {
13+
if (!services.helpers.NodeMapper.assigneeToAssignedObjectOrUndefined(assignee)) {
14+
accept('error', 'No value is assigned to this assignee.', {
15+
node: assignee,
16+
code: CODE_ASSIGMENT_NOTHING_ASSIGNED,
17+
});
18+
}
19+
}
20+
};
21+
622
export const yieldMustNotBeUsedInPipeline = (node: SdsYield, accept: ValidationAcceptor): void => {
723
const containingPipeline = getContainerOfType(node, isSdsPipeline);
824

src/language/validation/safe-ds-validator.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ import {
3333
unionTypeShouldNotHaveASingularTypeArgument,
3434
} from './style.js';
3535
import { templateStringMustHaveExpressionBetweenTwoStringParts } from './other/expressions/templateStrings.js';
36-
import { yieldMustNotBeUsedInPipeline } from './other/statements/assignments.js';
36+
import { assignmentAssigneeMustGetValue, yieldMustNotBeUsedInPipeline } from './other/statements/assignments.js';
3737
import { attributeMustHaveTypeHint, parameterMustHaveTypeHint, resultMustHaveTypeHint } from './types.js';
3838
import { moduleDeclarationsMustMatchFileKind, moduleWithDeclarationsMustStatePackage } from './other/modules.js';
3939
import { typeParameterConstraintLeftOperandMustBeOwnTypeParameter } from './other/declarations/typeParameterConstraints.js';
@@ -87,7 +87,7 @@ export const registerValidationChecks = function (services: SafeDsServices) {
8787
assigneeAssignedResultShouldNotBeDeprecated(services),
8888
assigneeAssignedResultShouldNotBeExperimental(services),
8989
],
90-
SdsAssignment: [assignmentShouldHaveMoreThanWildcardsAsAssignees],
90+
SdsAssignment: [assignmentAssigneeMustGetValue(services), assignmentShouldHaveMoreThanWildcardsAsAssignees],
9191
SdsAnnotation: [
9292
annotationMustContainUniqueNames,
9393
annotationParameterListShouldNotBeEmpty,
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package tests.validation.other.statements.assignments.nothingAssigned
2+
3+
fun noResults()
4+
fun oneResult() -> first: Int
5+
fun twoResults() -> (first: Int, second: Int)
6+
7+
segment mySegment() -> (
8+
r1: Any?,
9+
r2: Any?,
10+
r3: Any?,
11+
r4: Any?,
12+
r5: Any?,
13+
r6: Any?,
14+
r7: Any?,
15+
r8: Any?,
16+
r9: Any?,
17+
r10: Any?,
18+
r11: Any?,
19+
r12: Any?,
20+
r13: Any?,
21+
r14: Any?,
22+
) {
23+
// $TEST$ error "No value is assigned to this assignee."
24+
»_« = noResults();
25+
// $TEST$ no error "No value is assigned to this assignee."
26+
// $TEST$ error "No value is assigned to this assignee."
27+
»_«, »_« = oneResult();
28+
// $TEST$ no error "No value is assigned to this assignee."
29+
// $TEST$ no error "No value is assigned to this assignee."
30+
// $TEST$ error "No value is assigned to this assignee."
31+
»_«, »_«, »_« = twoResults();
32+
// $TEST$ no error "No value is assigned to this assignee."
33+
// $TEST$ error "No value is assigned to this assignee."
34+
»_«, »_« = 1;
35+
// $TEST$ no error "No value is assigned to this assignee."
36+
// $TEST$ error "No value is assigned to this assignee."
37+
»_«, »_« = unresolved;
38+
// $TEST$ error "No value is assigned to this assignee."
39+
// $TEST$ error "No value is assigned to this assignee."
40+
»_«, »_« = unresolved();
41+
42+
// $TEST$ error "No value is assigned to this assignee."
43+
»val a« = noResults();
44+
// $TEST$ no error "No value is assigned to this assignee."
45+
// $TEST$ error "No value is assigned to this assignee."
46+
»val b«, »val c« = oneResult();
47+
// $TEST$ no error "No value is assigned to this assignee."
48+
// $TEST$ no error "No value is assigned to this assignee."
49+
// $TEST$ error "No value is assigned to this assignee."
50+
»val d«, »val e«, »val f« = twoResults();
51+
// $TEST$ no error "No value is assigned to this assignee."
52+
// $TEST$ error "No value is assigned to this assignee."
53+
»val g«, »val h« = 1;
54+
// $TEST$ no error "No value is assigned to this assignee."
55+
// $TEST$ error "No value is assigned to this assignee."
56+
»val i«, »val j« = unresolved;
57+
// $TEST$ error "No value is assigned to this assignee."
58+
// $TEST$ error "No value is assigned to this assignee."
59+
»val k«, »val l« = unresolved();
60+
61+
// $TEST$ error "No value is assigned to this assignee."
62+
»yield r1« = noResults();
63+
// $TEST$ no error "No value is assigned to this assignee."
64+
// $TEST$ error "No value is assigned to this assignee."
65+
»yield r2«, »yield r3« = oneResult();
66+
// $TEST$ no error "No value is assigned to this assignee."
67+
// $TEST$ no error "No value is assigned to this assignee."
68+
// $TEST$ error "No value is assigned to this assignee."
69+
»yield r4«, »yield r5«, »yield r6« = twoResults();
70+
// $TEST$ no error "No value is assigned to this assignee."
71+
// $TEST$ error "No value is assigned to this assignee."
72+
»yield r7«, »yield r8« = 1;
73+
// $TEST$ no error "No value is assigned to this assignee."
74+
»yield r9« = oneResult();
75+
// $TEST$ no error "No value is assigned to this assignee."
76+
»yield r10« = twoResults();
77+
// $TEST$ no error "No value is assigned to this assignee."
78+
// $TEST$ error "No value is assigned to this assignee."
79+
»yield r11«, »yield r12« = unresolved;
80+
// $TEST$ error "No value is assigned to this assignee."
81+
// $TEST$ error "No value is assigned to this assignee."
82+
»yield r13«, »yield r14« = unresolved();
83+
}

0 commit comments

Comments
 (0)