Skip to content

Commit

Permalink
feat: better error if + is used for string concatenation (#1044)
Browse files Browse the repository at this point in the history
### Summary of Changes

If `+` is used for string concatenation, users are now pointed to
template strings.
  • Loading branch information
lars-reimann authored Apr 14, 2024
1 parent 8147480 commit 8be49c5
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
16 changes: 15 additions & 1 deletion packages/safe-ds-lang/src/language/validation/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -176,11 +176,25 @@ export const infixOperationOperandsMustHaveCorrectType = (services: SafeDsServic
});
}
return;
case '+':
if (
typeChecker.isSubtypeOf(leftType, coreTypes.String) ||
typeChecker.isSubtypeOf(rightType, coreTypes.String)
) {
accept('error', `Use template strings for concatenation.`, {
node,
code: CODE_TYPE_MISMATCH,
codeDescription: {
href: 'https://dsl.safeds.com/en/stable/language/pipeline-language/expressions/#template-strings',
},
});
return;
}
// fallthrough
case '<':
case '<=':
case '>=':
case '>':
case '+':
case '-':
case '*':
case '/':
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ pipeline myPipeline {
// $TEST$ no error r"Expected type 'Float' or 'Int' but got .*\."
// $TEST$ no error r"Expected type 'Float' or 'Int' but got .*\."
»0« + »0«;
// $TEST$ error "Expected type 'Float' or 'Int' but got 'literal<"">'."
// $TEST$ error "Expected type 'Float' or 'Int' but got 'literal<"">'."
»""« + »""«;
// $TEST$ error "Expected type 'Float' or 'Int' but got 'literal<true>'."
// $TEST$ error "Expected type 'Float' or 'Int' but got 'literal<false>'."
»true« + »false«;
// $TEST$ error "Expected type 'Float' or 'Int' but got 'unknown'."
// $TEST$ error "Expected type 'Float' or 'Int' but got 'unknown'."
»unresolved« + »unresolved«;
Expand Down Expand Up @@ -166,3 +166,12 @@ class MyClass<T sub Any>(
// $TEST$ error "Expected type 'Float' or 'Int' but got 'T'."
c4: Any? = »p1« > »p1«,
)

pipeline stringConcatenation {
// $TEST$ error "Use template strings for concatenation."
»"" + 0.0«;
// $TEST$ error "Use template strings for concatenation."
»0 + ""«;
// $TEST$ error "Use template strings for concatenation."
»"" + ""«;
}
4 changes: 2 additions & 2 deletions packages/safe-ds-vscode/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,8 @@
},
{
"language": "safe-ds-dev",
"scopeName": "source.safe-ds-stub",
"path": "./syntaxes/safe-ds-stub.tmLanguage.json"
"scopeName": "source.safe-ds-dev",
"path": "./syntaxes/safe-ds-dev.tmLanguage.json"
}
],
"snippets": [
Expand Down

0 comments on commit 8be49c5

Please sign in to comment.