Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: error if single use annotations are used multiple times #631

Merged
merged 2 commits into from
Oct 11, 2023
Merged
Show file tree
Hide file tree
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
8 changes: 8 additions & 0 deletions src/language/builtins/safe-ds-annotations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,14 @@ export class SafeDsAnnotations extends SafeDsModuleMembers<SdsAnnotation> {
return this.getAnnotation(CORE_ANNOTATIONS_URI, 'Expert');
}

isRepeatable(node: SdsAnnotation | undefined): boolean {
return this.hasAnnotationCallOf(node, this.Repeatable);
}

private get Repeatable(): SdsAnnotation | undefined {
return this.getAnnotation(CORE_ANNOTATIONS_URI, 'Repeatable');
}

private hasAnnotationCallOf(node: SdsAnnotatedObject | undefined, expected: SdsAnnotation | undefined): boolean {
return annotationCallsOrEmpty(node).some((it) => {
const actual = it.annotation?.ref;
Expand Down
22 changes: 22 additions & 0 deletions src/language/validation/builtins/repeatable.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { ValidationAcceptor } from 'langium';
import { SdsAnnotatedObject } from '../../generated/ast.js';
import { SafeDsServices } from '../../safe-ds-module.js';
import { annotationCallsOrEmpty } from '../../helpers/nodeProperties.js';
import { duplicatesBy } from '../../helpers/collectionUtils.js';

export const CODE_ANNOTATION_NOT_REPEATABLE = 'annotation/not-repeatable';

export const singleUseAnnotationsMustNotBeRepeated =
(services: SafeDsServices) => (node: SdsAnnotatedObject, accept: ValidationAcceptor) => {
const callsOfSingleUseAnnotations = annotationCallsOrEmpty(node).filter((it) => {
const annotation = it.annotation?.ref;
return annotation && !services.builtins.Annotations.isRepeatable(annotation);
});

for (const duplicate of duplicatesBy(callsOfSingleUseAnnotations, (it) => it.annotation?.ref)) {
accept('error', `The annotation '${duplicate.annotation.$refText}' is not repeatable.`, {
node: duplicate,
code: CODE_ANNOTATION_NOT_REPEATABLE,
});
}
};
2 changes: 2 additions & 0 deletions src/language/validation/safe-ds-validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ import {
} from './other/declarations/annotationCalls.js';
import { memberAccessMustBeNullSafeIfReceiverIsNullable } from './other/expressions/memberAccesses.js';
import { importPackageMustExist, importPackageShouldNotBeEmpty } from './other/imports.js';
import { singleUseAnnotationsMustNotBeRepeated } from './builtins/repeatable.js';

/**
* Register custom validation checks.
Expand All @@ -98,6 +99,7 @@ export const registerValidationChecks = function (services: SafeDsServices) {
annotationCallAnnotationShouldNotBeExperimental(services),
annotationCallArgumentListShouldBeNeeded,
],
SdsAnnotatedObject: [singleUseAnnotationsMustNotBeRepeated(services)],
SdsArgument: [
argumentCorrespondingParameterShouldNotBeDeprecated(services),
argumentCorrespondingParameterShouldNotBeExperimental(services),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package tests.validation.builtins.annotations.repeatable

annotation SingleUse

@Repeatable
annotation MultiUse

// $TEST$ no error r"The annotation '\w*' is not repeatable\."
»@SingleUse«
// $TEST$ no error r"The annotation '\w*' is not repeatable\."
»@MultiUse«
// $TEST$ no error r"The annotation '\w*' is not repeatable\."
»@MultiUse«
// $TEST$ no error r"The annotation '\w*' is not repeatable\."
»@UnresolvedAnnotation«
// $TEST$ no error r"The annotation '\w*' is not repeatable\."
»@UnresolvedAnnotation«
class CorrectUse

// $TEST$ no error r"The annotation '\w*' is not repeatable\."
»@SingleUse«
// $TEST$ error "The annotation 'SingleUse' is not repeatable."
»@SingleUse«
class IncorrectUse
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ segment mySegment() -> (
// $TEST$ error "No value is assigned to this assignee."
»val k«, »val l« = unresolved();


// $TEST$ error "No value is assigned to this assignee."
»yield r1« = noResults();
// $TEST$ no error "No value is assigned to this assignee."
Expand Down