Skip to content

Commit

Permalink
feat: mark entire type cast as wrong if cast is impossible (#991)
Browse files Browse the repository at this point in the history
### Summary of Changes

If a type cast can never succeed, it is now marked completely as
erroneous. Previously, only its left operand was marked, which makes
little sense since both operands determine whether the cast is legal.
  • Loading branch information
lars-reimann authored Apr 4, 2024
1 parent 62fef3f commit 72d4e2e
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ import {
parameterMustHaveTypeHint,
prefixOperationOperandMustHaveCorrectType,
resultMustHaveTypeHint,
typeCastExpressionMustHaveUnknownType,
typeCastMustNotAlwaysFail,
typeParameterDefaultValueMustMatchUpperBound,
yieldTypeMustMatchResultType,
} from './types.js';
Expand Down Expand Up @@ -361,7 +361,7 @@ export const registerValidationChecks = function (services: SafeDsServices) {
],
SdsStatement: [statementMustDoSomething(services)],
SdsTemplateString: [templateStringMustHaveExpressionBetweenTwoStringParts],
SdsTypeCast: [typeCastExpressionMustHaveUnknownType(services)],
SdsTypeCast: [typeCastMustNotAlwaysFail(services)],
SdsTypeParameter: [
typeParameterDefaultValueMustMatchUpperBound(services),
typeParameterMustBeUsedInCorrectPosition(services),
Expand Down
4 changes: 2 additions & 2 deletions packages/safe-ds-lang/src/language/validation/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ export const prefixOperationOperandMustHaveCorrectType = (services: SafeDsServic
};
};

export const typeCastExpressionMustHaveUnknownType = (services: SafeDsServices) => {
export const typeCastMustNotAlwaysFail = (services: SafeDsServices) => {
const typeChecker = services.typing.TypeChecker;
const typeComputer = services.typing.TypeComputer;

Expand All @@ -369,7 +369,7 @@ export const typeCastExpressionMustHaveUnknownType = (services: SafeDsServices)
) {
accept('error', 'This type cast can never succeed.', {
// Using property: "expression" does not work here, probably due to eclipse-langium/langium#1218
node: node.expression,
node,
code: CODE_TYPE_MISMATCH,
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ class E() sub C

pipeline test {
// $TEST$ error "This type cast can never succeed."
»D()« as E;
»D() as E«;

// $TEST$ no error "This type cast can never succeed."
»C()« as C;
»C() as C«;

// $TEST$ no error "This type cast can never succeed."
»C()« as D;
»C() as D«;

// $TEST$ no error "This type cast can never succeed."
»D()« as C;
»D() as C«;

// $TEST$ no error "This type cast can never succeed."
»unresolved« as Int;
»unresolved as Int«;
}

0 comments on commit 72d4e2e

Please sign in to comment.