-
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: optional error messages for constraints (#1275)
Closes #1082 ### Summary of Changes Constraints can now have custom error messages. These can be static strings or template strings that references constant parameter, e.g. in ``` @pure fun f( const p1: Int, const p2: Int = -2, ) where { p1 >= 0 else "This parameter must be non-negative.", p2 > p1 else "p2 must be greater than p1, but p2 was {{ p2 }} and p1 was {{ p1 }}.", } ```
- Loading branch information
1 parent
d30edbc
commit fce761c
Showing
29 changed files
with
295 additions
and
66 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 |
---|---|---|
|
@@ -33,6 +33,7 @@ | |
|
||
keywords_generic = ( | ||
"as", | ||
"else", | ||
"const", | ||
"import", | ||
"in", | ||
|
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
31 changes: 31 additions & 0 deletions
31
packages/safe-ds-lang/src/language/validation/other/declarations/constraints.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,31 @@ | ||
import { isSdsParameter, isSdsReference, isSdsString, SdsConstraint } from '../../../generated/ast.js'; | ||
import { AstUtils, ValidationAcceptor } from 'langium'; | ||
import { Parameter } from '../../../helpers/nodeProperties.js'; | ||
|
||
export const CODE_CONSTRAINT_MESSAGE = 'constraint/message'; | ||
|
||
export const messageOfConstraintsMustOnlyReferenceConstantParameters = ( | ||
node: SdsConstraint, | ||
accept: ValidationAcceptor, | ||
) => { | ||
if (!node.message || isSdsString(node.message)) { | ||
return; | ||
} | ||
|
||
for (const expression of node.message.expressions) { | ||
const isInvalid = AstUtils.streamAst(expression) | ||
.filter(isSdsReference) | ||
.map((reference) => reference.target.ref) | ||
.some((target) => { | ||
return target && (!isSdsParameter(target) || !Parameter.isConstant(target)); | ||
}); | ||
|
||
if (isInvalid) { | ||
accept('error', 'The message of a constraint must only reference constant parameters.', { | ||
node: expression, | ||
property: 'target', | ||
code: CODE_CONSTRAINT_MESSAGE, | ||
}); | ||
} | ||
} | ||
}; |
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
7 changes: 7 additions & 0 deletions
7
...resources/formatting/declarations/constraints/greater than constraint with message.sdsdev
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,7 @@ | ||
annotation MyAnnotation where { p > 0 else "" } | ||
|
||
// ----------------------------------------------------------------------------- | ||
|
||
annotation MyAnnotation where { | ||
p > 0 else "" | ||
} |
7 changes: 7 additions & 0 deletions
7
.../formatting/declarations/constraints/greater than or equal constraint with message.sdsdev
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,7 @@ | ||
annotation MyAnnotation where { p >= 0 else "" } | ||
|
||
// ----------------------------------------------------------------------------- | ||
|
||
annotation MyAnnotation where { | ||
p >= 0 else "" | ||
} |
7 changes: 7 additions & 0 deletions
7
...ts/resources/formatting/declarations/constraints/less than constraint with message.sdsdev
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,7 @@ | ||
annotation MyAnnotation where { p < 0 else "" } | ||
|
||
// ----------------------------------------------------------------------------- | ||
|
||
annotation MyAnnotation where { | ||
p < 0 else "" | ||
} |
7 changes: 7 additions & 0 deletions
7
...ces/formatting/declarations/constraints/less than or equal constraint with message.sdsdev
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,7 @@ | ||
annotation MyAnnotation where { p <= 0 else "" } | ||
|
||
// ----------------------------------------------------------------------------- | ||
|
||
annotation MyAnnotation where { | ||
p <= 0 else "" | ||
} |
4 changes: 2 additions & 2 deletions
4
...e-ds-lang/tests/resources/formatting/declarations/constraints/multiple constraints.sdsdev
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 |
---|---|---|
@@ -1,8 +1,8 @@ | ||
annotation MyAnnotation where { p < 0 , q > 1 } | ||
annotation MyAnnotation where { p < 0 , q > 1 else "" } | ||
|
||
// ----------------------------------------------------------------------------- | ||
|
||
annotation MyAnnotation where { | ||
p < 0, | ||
q > 1 | ||
q > 1 else "" | ||
} |
5 changes: 5 additions & 0 deletions
5
...sources/grammar/declarations/constraints/good-greater than constraint with message.sdsdev
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 | ||
|
||
annotation MyAnnotation where { | ||
p > 0 else "p must be positive but was {{ p }}" | ||
} |
5 changes: 5 additions & 0 deletions
5
...ammar/declarations/constraints/good-greater than or equals constraint with message.sdsdev
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 | ||
|
||
annotation MyAnnotation where { | ||
p >= 0 else "p must be non-negative but was {{ p }}" | ||
} |
5 changes: 5 additions & 0 deletions
5
.../resources/grammar/declarations/constraints/good-less than constraint with message.sdsdev
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 | ||
|
||
annotation MyAnnotation where { | ||
p < 0 else "p must be negative" | ||
} |
5 changes: 5 additions & 0 deletions
5
.../grammar/declarations/constraints/good-less than or equals constraint with message.sdsdev
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 | ||
|
||
annotation MyAnnotation where { | ||
p <= 0 else "p must be non-positive" | ||
} |
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 |
---|---|---|
|
@@ -2,5 +2,5 @@ | |
|
||
annotation MyAnnotation where { | ||
p < 0, | ||
q > 0 | ||
q > 0 else "q must be positive" | ||
} |
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
Oops, something went wrong.