From 090fcc3d7fbb8227620a3ecaed45fc12b6de71e7 Mon Sep 17 00:00:00 2001 From: Lars Reimann Date: Mon, 9 Oct 2023 22:22:25 +0200 Subject: [PATCH] feat: warn if experimental language features are used (#624) Closes #108 ### Summary of Changes Warn if experimental language features are used. For the moment, this is only indexed accesses. --------- Co-authored-by: megalinter-bot <129584137+megalinter-bot@users.noreply.github.com> --- .../validation/experimentalLanguageFeature.ts | 11 +++++++++++ src/language/validation/safe-ds-validator.ts | 2 ++ .../builtins/safeds/lang/coreAnnotations.sdsstub | 2 ++ .../builtins/safeds/lang/documentation.sdsstub | 1 + .../indexed access/main.sdstest | 9 +++++++++ 5 files changed, 25 insertions(+) create mode 100644 src/language/validation/experimentalLanguageFeature.ts create mode 100644 tests/resources/validation/experimental language feature/indexed access/main.sdstest diff --git a/src/language/validation/experimentalLanguageFeature.ts b/src/language/validation/experimentalLanguageFeature.ts new file mode 100644 index 000000000..9e8850adb --- /dev/null +++ b/src/language/validation/experimentalLanguageFeature.ts @@ -0,0 +1,11 @@ +import { SdsIndexedAccess } from '../generated/ast.js'; +import { ValidationAcceptor } from 'langium'; + +export const CODE_EXPERIMENTAL_LANGUAGE_FEATURE = 'experimental/language-feature'; + +export const indexedAccessesShouldBeUsedWithCaution = (node: SdsIndexedAccess, accept: ValidationAcceptor): void => { + accept('warning', 'Indexed accesses are experimental and may change without prior notice.', { + node, + code: CODE_EXPERIMENTAL_LANGUAGE_FEATURE, + }); +}; diff --git a/src/language/validation/safe-ds-validator.ts b/src/language/validation/safe-ds-validator.ts index 0ec0351c1..61d5429d7 100644 --- a/src/language/validation/safe-ds-validator.ts +++ b/src/language/validation/safe-ds-validator.ts @@ -63,6 +63,7 @@ import { import { placeholderShouldBeUsed } from './other/declarations/placeholders.js'; import { segmentParameterShouldBeUsed, segmentResultMustBeAssignedExactlyOnce } from './other/declarations/segments.js'; import { lambdaParameterMustNotHaveConstModifier } from './other/expressions/lambdas.js'; +import { indexedAccessesShouldBeUsedWithCaution } from './experimentalLanguageFeature.js'; /** * Register custom validation checks. @@ -107,6 +108,7 @@ export const registerValidationChecks = function (services: SafeDsServices) { SdsEnumVariant: [enumVariantMustContainUniqueNames, enumVariantParameterListShouldNotBeEmpty], SdsExpressionLambda: [expressionLambdaMustContainUniqueNames], SdsFunction: [functionMustContainUniqueNames, functionResultListShouldNotBeEmpty], + SdsIndexedAccess: [indexedAccessesShouldBeUsedWithCaution], SdsLambda: [lambdaParameterMustNotHaveConstModifier], SdsMemberAccess: [memberAccessNullSafetyShouldBeNeeded(services)], SdsModule: [moduleDeclarationsMustMatchFileKind, moduleWithDeclarationsMustStatePackage], diff --git a/src/resources/builtins/safeds/lang/coreAnnotations.sdsstub b/src/resources/builtins/safeds/lang/coreAnnotations.sdsstub index 71602d7ae..330c7cc53 100644 --- a/src/resources/builtins/safeds/lang/coreAnnotations.sdsstub +++ b/src/resources/builtins/safeds/lang/coreAnnotations.sdsstub @@ -90,10 +90,12 @@ annotation Deprecated( ]) annotation Experimental +@Experimental @Description("The function has no side effects and returns the same results for the same arguments.") @Target([AnnotationTarget.Function]) annotation Pure +@Experimental @Description("The function has no side effects.") @Target([AnnotationTarget.Function]) annotation NoSideEffects diff --git a/src/resources/builtins/safeds/lang/documentation.sdsstub b/src/resources/builtins/safeds/lang/documentation.sdsstub index 455dbcbdf..0d714f493 100644 --- a/src/resources/builtins/safeds/lang/documentation.sdsstub +++ b/src/resources/builtins/safeds/lang/documentation.sdsstub @@ -12,6 +12,7 @@ annotation Since( version: String ) +@Experimental @Description("This parameter should only be used by expert users.") @Target([AnnotationTarget.Parameter]) annotation Expert diff --git a/tests/resources/validation/experimental language feature/indexed access/main.sdstest b/tests/resources/validation/experimental language feature/indexed access/main.sdstest new file mode 100644 index 000000000..4c4a858a9 --- /dev/null +++ b/tests/resources/validation/experimental language feature/indexed access/main.sdstest @@ -0,0 +1,9 @@ +package tests.validation.experimentalLanguageFeature.indexedAccess + +pipeline myPipeline { + // $TEST$ warning "Indexed accesses are experimental and may change without prior notice." + »[1, 2][1]«; + + // $TEST$ warning "Indexed accesses are experimental and may change without prior notice." + »{"a": "b"}["a"]«; +}