-
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: show an error if a pure parameter does not have a callable type (…
…#736) Closes #729 ### Summary of Changes Only allow using the `@Pure` annotation on parameters with a callable type.
- Loading branch information
1 parent
168d098
commit 6c52868
Showing
4 changed files
with
157 additions
and
105 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
27 changes: 27 additions & 0 deletions
27
packages/safe-ds-lang/src/language/validation/builtins/pure.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,27 @@ | ||
import type { ValidationAcceptor } from 'langium'; | ||
import type { SdsParameter } from '../../generated/ast.js'; | ||
import type { SafeDsServices } from '../../safe-ds-module.js'; | ||
import { CallableType } from '../../typing/model.js'; | ||
|
||
export const CODE_PURE_PARAMETER_MUST_HAVE_CALLABLE_TYPE = 'pure/parameter-must-have-callable-type'; | ||
|
||
export const pureParameterMustHaveCallableType = (services: SafeDsServices) => { | ||
const builtinAnnotations = services.builtins.Annotations; | ||
const typeComputer = services.types.TypeComputer; | ||
|
||
return (node: SdsParameter, accept: ValidationAcceptor) => { | ||
// Don't show an error if no type is specified (yet) or if the parameter is not marked as pure | ||
if (!node.type || !builtinAnnotations.isPure(node)) { | ||
return; | ||
} | ||
|
||
const type = typeComputer.computeType(node); | ||
if (!(type instanceof CallableType)) { | ||
accept('error', 'A pure parameter must have a callable type.', { | ||
node, | ||
property: 'name', | ||
code: CODE_PURE_PARAMETER_MUST_HAVE_CALLABLE_TYPE, | ||
}); | ||
} | ||
}; | ||
}; |
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
14 changes: 14 additions & 0 deletions
14
.../validation/builtins/annotations/pure/pure parameter must have callable type/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,14 @@ | ||
package tests.validation.builtins.pure.pureParameterMustHaveCallableType | ||
|
||
// $TEST$ error "A pure parameter must have a callable type." | ||
// $TEST$ error "A pure parameter must have a callable type." | ||
// $TEST$ no error "A pure parameter must have a callable type." | ||
// $TEST$ no error "A pure parameter must have a callable type." | ||
// $TEST$ no error "A pure parameter must have a callable type." | ||
annotation MyAnnotation( | ||
@Pure »a«: Int, | ||
@Pure »b«: Unresolved, | ||
@Pure »c«: () -> (), | ||
@Pure »d«, | ||
»e«: Int, | ||
) |