-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat:
unknown
default value of stub parameters (#952)
Closes #951 ### Summary of Changes Add a new literal `unknown` that can be used to mark parameters as optional if the exact default value of a class/enum variant/function parameter is unknown. This literal can be used nowhere else.
- Loading branch information
1 parent
155b1c0
commit 78103e3
Showing
19 changed files
with
132 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,7 @@ | |
"false", | ||
"null", | ||
"true", | ||
"unknown", | ||
) | ||
|
||
keywords_namespace = ( | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
packages/safe-ds-lang/src/language/validation/other/expressions/literals.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { | ||
isSdsCallable, | ||
isSdsCallableType, | ||
isSdsClass, | ||
isSdsEnumVariant, | ||
isSdsFunction, | ||
isSdsParameter, | ||
SdsUnknown, | ||
} from '../../../generated/ast.js'; | ||
import { AstUtils, ValidationAcceptor } from 'langium'; | ||
|
||
export const CODE_LITERALS_UNKNOWN = 'literals/unknown'; | ||
|
||
export const unknownMustOnlyBeUsedAsDefaultValueOfStub = (node: SdsUnknown, accept: ValidationAcceptor): void => { | ||
if (!unknownIsUsedCorrectly(node)) { | ||
accept( | ||
'error', | ||
'unknown is only allowed as the default value of a parameter of a class, enum variant, or function.', | ||
{ | ||
node, | ||
code: CODE_LITERALS_UNKNOWN, | ||
}, | ||
); | ||
} | ||
}; | ||
|
||
const unknownIsUsedCorrectly = (node: SdsUnknown): boolean => { | ||
if (!isSdsParameter(node.$container) || node.$containerProperty !== 'defaultValue') { | ||
return false; | ||
} | ||
|
||
const containingCallable = AstUtils.getContainerOfType(node.$container, isSdsCallable); | ||
return ( | ||
isSdsCallableType(containingCallable) || // Callable types must not have default values in general | ||
isSdsClass(containingCallable) || | ||
isSdsEnumVariant(containingCallable) || | ||
isSdsFunction(containingCallable) | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
packages/safe-ds-lang/tests/resources/formatting/expressions/literals/unknown.sdstest
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
pipeline myPipeline { | ||
unknown; | ||
} | ||
|
||
// ----------------------------------------------------------------------------- | ||
|
||
pipeline myPipeline { | ||
unknown; | ||
} |
5 changes: 5 additions & 0 deletions
5
packages/safe-ds-lang/tests/resources/grammar/expressions/literals/good-unknown.sdstest
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
// $TEST$ no_syntax_error | ||
|
||
pipeline myPipeline { | ||
unknown; | ||
} |
3 changes: 3 additions & 0 deletions
3
...ages/safe-ds-lang/tests/resources/grammar/keywords as names/bad-unescaped unknown.sdstest
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
// $TEST$ syntax_error | ||
|
||
class unknown |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,7 @@ class `segment` | |
class `sub` | ||
class `true` | ||
class `union` | ||
class `unknown` | ||
class `val` | ||
class `where` | ||
class `yield` |
6 changes: 6 additions & 0 deletions
6
.../safe-ds-lang/tests/resources/partial evaluation/base cases/unknown literals/main.sdstest
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
package tests.partialValidation.baseCases.unknownLiterals | ||
|
||
pipeline test { | ||
// $TEST$ serialization ? | ||
»unknown«; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
...ther/expressions/literals/unknown must only be used as default value of stub/main.sdstest
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package tests.validation.other.expressions.literals.unknownMustOnlyBeUsedAsDefaultValueOfStub | ||
|
||
// $TEST$ error "unknown is only allowed as the default value of a parameter of a class, enum variant, or function." | ||
annotation MyAnnotation(p: Int = »unknown«) | ||
|
||
// $TEST$ no error "unknown is only allowed as the default value of a parameter of a class, enum variant, or function." | ||
class MyClass(p: Int = »unknown«) | ||
|
||
enum MyEnum { | ||
// $TEST$ no error "unknown is only allowed as the default value of a parameter of a class, enum variant, or function." | ||
MyVariant(p: Int = »unknown«) | ||
} | ||
|
||
// $TEST$ no error "unknown is only allowed as the default value of a parameter of a class, enum variant, or function." | ||
fun myFunction(p: Int = »unknown«) | ||
|
||
segment mySegment( | ||
// $TEST$ no error "unknown is only allowed as the default value of a parameter of a class, enum variant, or function." | ||
f: (p: Int = »unknown«) -> (), | ||
// $TEST$ error "unknown is only allowed as the default value of a parameter of a class, enum variant, or function." | ||
p: Int = »unknown«, | ||
) { | ||
// $TEST$ error "unknown is only allowed as the default value of a parameter of a class, enum variant, or function." | ||
(p: Int = »unknown«) {}; | ||
// $TEST$ error "unknown is only allowed as the default value of a parameter of a class, enum variant, or function." | ||
(p: Int = »unknown«) -> 1; | ||
|
||
// $TEST$ error "unknown is only allowed as the default value of a parameter of a class, enum variant, or function." | ||
»unknown«; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters