From 9d342e4d261aab5a0f1739dfaa6f8ec52a4a830a Mon Sep 17 00:00:00 2001 From: Lars Reimann Date: Mon, 30 Oct 2023 19:41:56 +0100 Subject: [PATCH] feat: annotations for the purity of functions (#709) Closes #559 ### Summary of Changes Add annotations to denote the purity of functions to the builtin library. --- .../builtins/safeds/lang/purity.sdsstub | 50 +++++++++++++++++++ ...st.ts => safe-ds-python-generator.test.ts} | 2 +- ...rammar.test.ts => safe-ds-grammar.test.ts} | 2 +- .../grammar/safe-ds-value-converter.test.ts | 2 +- ...ting.test.ts => safe-ds-formatter.test.ts} | 2 +- ....ts => safe-ds-partial-evalutator.test.ts} | 0 .../{testScoping.test.ts => scoping.test.ts} | 2 +- ....test.ts => safe-ds-type-computer.test.ts} | 2 +- ...tion.test.ts => safe-ds-validator.test.ts} | 2 +- 9 files changed, 57 insertions(+), 7 deletions(-) rename packages/safe-ds-lang/tests/language/generation/{testGeneration.test.ts => safe-ds-python-generator.test.ts} (96%) rename packages/safe-ds-lang/tests/language/grammar/{testGrammar.test.ts => safe-ds-grammar.test.ts} (95%) rename packages/safe-ds-lang/tests/language/lsp/formatting/{testFormatting.test.ts => safe-ds-formatter.test.ts} (94%) rename packages/safe-ds-lang/tests/language/partialEvaluation/{testPartialEvaluation.test.ts => safe-ds-partial-evalutator.test.ts} (100%) rename packages/safe-ds-lang/tests/language/scoping/{testScoping.test.ts => scoping.test.ts} (98%) rename packages/safe-ds-lang/tests/language/typing/type computer/{testTyping.test.ts => safe-ds-type-computer.test.ts} (97%) rename packages/safe-ds-lang/tests/language/validation/{testValidation.test.ts => safe-ds-validator.test.ts} (98%) diff --git a/packages/safe-ds-lang/src/resources/builtins/safeds/lang/purity.sdsstub b/packages/safe-ds-lang/src/resources/builtins/safeds/lang/purity.sdsstub index 8353c1610..23a560108 100644 --- a/packages/safe-ds-lang/src/resources/builtins/safeds/lang/purity.sdsstub +++ b/packages/safe-ds-lang/src/resources/builtins/safeds/lang/purity.sdsstub @@ -10,3 +10,53 @@ package safeds.lang @Experimental @Target([AnnotationTarget.Function]) annotation Pure + +/** + * Indicates that the function has side effects and/or does not always return the same results given the same arguments. + * + * @param allReasons + * A list of **all** reasons why the function is impure. If no specific {@link ImpurityReason} applies, include + * `ImpurityReason.Other`. + */ +@Experimental +@Target([AnnotationTarget.Function]) +annotation Impure(allReasons: List) + +/** + * A reason why a function is impure. + */ +enum ImpurityReason { + + /** + * The function reads from a file and the file path is a constant. + * + * @param path The path of the file. + */ + FileReadFromConstantPath(path: String) + + /** + * The function reads from a file and the file path is given by a parameter. + * + * @param parameterName The name of the parameter that specifies the file path. + */ + FileReadFromParameterizedPath(parameterName: String) + + /** + * The function writes to a file and the file path is a constant. + * + * @param path The path of the file. + */ + FileWriteToConstantPath(path: String) + + /** + * The function writes to a file and the file path is given by a parameter. + * + * @param parameterName The name of the parameter that specifies the file path. + */ + FileWriteToParameterizedPath(parameterName: String) + + /** + * The function is impure for some other reason. If possible, use a more specific reason. + */ + Other +} diff --git a/packages/safe-ds-lang/tests/language/generation/testGeneration.test.ts b/packages/safe-ds-lang/tests/language/generation/safe-ds-python-generator.test.ts similarity index 96% rename from packages/safe-ds-lang/tests/language/generation/testGeneration.test.ts rename to packages/safe-ds-lang/tests/language/generation/safe-ds-python-generator.test.ts index b5b700314..4a1114c96 100644 --- a/packages/safe-ds-lang/tests/language/generation/testGeneration.test.ts +++ b/packages/safe-ds-lang/tests/language/generation/safe-ds-python-generator.test.ts @@ -1,4 +1,4 @@ -import { createSafeDsServices } from '../../../src/language/safe-ds-module.js'; +import { createSafeDsServices } from '../../../src/language/index.js'; import { clearDocuments } from 'langium/test'; import { afterEach, beforeEach, describe, expect, it } from 'vitest'; import { NodeFileSystem } from 'langium/node'; diff --git a/packages/safe-ds-lang/tests/language/grammar/testGrammar.test.ts b/packages/safe-ds-lang/tests/language/grammar/safe-ds-grammar.test.ts similarity index 95% rename from packages/safe-ds-lang/tests/language/grammar/testGrammar.test.ts rename to packages/safe-ds-lang/tests/language/grammar/safe-ds-grammar.test.ts index 793e82577..d113671f6 100644 --- a/packages/safe-ds-lang/tests/language/grammar/testGrammar.test.ts +++ b/packages/safe-ds-lang/tests/language/grammar/safe-ds-grammar.test.ts @@ -1,5 +1,5 @@ import { afterEach, describe, it } from 'vitest'; -import { createSafeDsServices } from '../../../src/language/safe-ds-module.js'; +import { createSafeDsServices } from '../../../src/language/index.js'; import { AssertionError } from 'assert'; import { NodeFileSystem } from 'langium/node'; import { createGrammarTests } from './creator.js'; diff --git a/packages/safe-ds-lang/tests/language/grammar/safe-ds-value-converter.test.ts b/packages/safe-ds-lang/tests/language/grammar/safe-ds-value-converter.test.ts index 9cc87e7c5..a6f2de938 100644 --- a/packages/safe-ds-lang/tests/language/grammar/safe-ds-value-converter.test.ts +++ b/packages/safe-ds-lang/tests/language/grammar/safe-ds-value-converter.test.ts @@ -1,5 +1,5 @@ import { describe, expect, it } from 'vitest'; -import { createSafeDsServices } from '../../../src/language/safe-ds-module.js'; +import { createSafeDsServices } from '../../../src/language/index.js'; import { EmptyFileSystem } from 'langium'; import { getNodeOfType } from '../../helpers/nodeFinder.js'; import { diff --git a/packages/safe-ds-lang/tests/language/lsp/formatting/testFormatting.test.ts b/packages/safe-ds-lang/tests/language/lsp/formatting/safe-ds-formatter.test.ts similarity index 94% rename from packages/safe-ds-lang/tests/language/lsp/formatting/testFormatting.test.ts rename to packages/safe-ds-lang/tests/language/lsp/formatting/safe-ds-formatter.test.ts index 1f40ab31b..02119f12e 100644 --- a/packages/safe-ds-lang/tests/language/lsp/formatting/testFormatting.test.ts +++ b/packages/safe-ds-lang/tests/language/lsp/formatting/safe-ds-formatter.test.ts @@ -1,4 +1,4 @@ -import { createSafeDsServices } from '../../../../src/language/safe-ds-module.js'; +import { createSafeDsServices } from '../../../../src/language/index.js'; import { clearDocuments, expectFormatting } from 'langium/test'; import { afterEach, describe, it } from 'vitest'; import { EmptyFileSystem } from 'langium'; diff --git a/packages/safe-ds-lang/tests/language/partialEvaluation/testPartialEvaluation.test.ts b/packages/safe-ds-lang/tests/language/partialEvaluation/safe-ds-partial-evalutator.test.ts similarity index 100% rename from packages/safe-ds-lang/tests/language/partialEvaluation/testPartialEvaluation.test.ts rename to packages/safe-ds-lang/tests/language/partialEvaluation/safe-ds-partial-evalutator.test.ts diff --git a/packages/safe-ds-lang/tests/language/scoping/testScoping.test.ts b/packages/safe-ds-lang/tests/language/scoping/scoping.test.ts similarity index 98% rename from packages/safe-ds-lang/tests/language/scoping/testScoping.test.ts rename to packages/safe-ds-lang/tests/language/scoping/scoping.test.ts index 485ed0caf..761faae23 100644 --- a/packages/safe-ds-lang/tests/language/scoping/testScoping.test.ts +++ b/packages/safe-ds-lang/tests/language/scoping/scoping.test.ts @@ -1,5 +1,5 @@ import { afterEach, beforeEach, describe, it } from 'vitest'; -import { createSafeDsServices } from '../../../src/language/safe-ds-module.js'; +import { createSafeDsServices } from '../../../src/language/index.js'; import { LangiumDocument, Reference, URI } from 'langium'; import { NodeFileSystem } from 'langium/node'; import { clearDocuments, isRangeEqual } from 'langium/test'; diff --git a/packages/safe-ds-lang/tests/language/typing/type computer/testTyping.test.ts b/packages/safe-ds-lang/tests/language/typing/type computer/safe-ds-type-computer.test.ts similarity index 97% rename from packages/safe-ds-lang/tests/language/typing/type computer/testTyping.test.ts rename to packages/safe-ds-lang/tests/language/typing/type computer/safe-ds-type-computer.test.ts index 6f388838a..6d5280f31 100644 --- a/packages/safe-ds-lang/tests/language/typing/type computer/testTyping.test.ts +++ b/packages/safe-ds-lang/tests/language/typing/type computer/safe-ds-type-computer.test.ts @@ -1,5 +1,5 @@ import { afterEach, beforeEach, describe, it } from 'vitest'; -import { createSafeDsServices } from '../../../../src/language/safe-ds-module.js'; +import { createSafeDsServices } from '../../../../src/language/index.js'; import { NodeFileSystem } from 'langium/node'; import { clearDocuments } from 'langium/test'; import { AssertionError } from 'assert'; diff --git a/packages/safe-ds-lang/tests/language/validation/testValidation.test.ts b/packages/safe-ds-lang/tests/language/validation/safe-ds-validator.test.ts similarity index 98% rename from packages/safe-ds-lang/tests/language/validation/testValidation.test.ts rename to packages/safe-ds-lang/tests/language/validation/safe-ds-validator.test.ts index 81d6a7f4a..8d77d0d0d 100644 --- a/packages/safe-ds-lang/tests/language/validation/testValidation.test.ts +++ b/packages/safe-ds-lang/tests/language/validation/safe-ds-validator.test.ts @@ -1,5 +1,5 @@ import { afterEach, beforeEach, describe, it } from 'vitest'; -import { createSafeDsServices } from '../../../src/language/safe-ds-module.js'; +import { createSafeDsServices } from '../../../src/language/index.js'; import { NodeFileSystem } from 'langium/node'; import { createValidationTests, ExpectedIssue } from './creator.js'; import { Diagnostic, DiagnosticSeverity } from 'vscode-languageserver';