diff --git a/packages/safe-ds-lang/src/language/typing/safe-ds-type-computer.ts b/packages/safe-ds-lang/src/language/typing/safe-ds-type-computer.ts index 51448ab2b..ab2ccdca0 100644 --- a/packages/safe-ds-lang/src/language/typing/safe-ds-type-computer.ts +++ b/packages/safe-ds-lang/src/language/typing/safe-ds-type-computer.ts @@ -832,15 +832,17 @@ export class SafeDsTypeComputer { // Normalize substitutions for (const [typeParameter, substitution] of state.substitutions) { + let newSubstitution = substitution; + // Replace unknown types by default values or lower bound (Nothing) - if (substitution === UnknownType) { + if (newSubstitution === UnknownType) { const defaultValueType = this.computeType(typeParameter.defaultValue); if (defaultValueType === UnknownType) { state.substitutions.set(typeParameter, this.coreTypes.Nothing); + continue; } else { - state.substitutions.set(typeParameter, defaultValueType); + newSubstitution = defaultValueType; } - continue; } // Clamp to upper bound @@ -848,9 +850,11 @@ export class SafeDsTypeComputer { stopAtTypeParameterType: true, }).substituteTypeParameters(state.substitutions); - if (!this.typeChecker.isSubtypeOf(substitution, upperBound)) { - state.substitutions.set(typeParameter, upperBound); + if (!this.typeChecker.isSubtypeOf(newSubstitution, upperBound)) { + newSubstitution = upperBound; } + + state.substitutions.set(typeParameter, newSubstitution); } return state.substitutions; diff --git a/packages/safe-ds-lang/tests/resources/typing/expressions/calls/type parameter inference/clamp to upper bound/main.sdstest b/packages/safe-ds-lang/tests/resources/typing/expressions/calls/type parameter inference/clamp to upper bound/main.sdstest index a1a7b62e1..8d16db576 100644 --- a/packages/safe-ds-lang/tests/resources/typing/expressions/calls/type parameter inference/clamp to upper bound/main.sdstest +++ b/packages/safe-ds-lang/tests/resources/typing/expressions/calls/type parameter inference/clamp to upper bound/main.sdstest @@ -1,13 +1,19 @@ package tests.typing.expressions.calls.typeParameterInference.clampToUpperBound -class C1(p1: T) +class C1(p1: T) -@Pure fun f1(p1: T) -> r1: T +@Pure fun f1(p1: T) -> r1: T pipeline myPipeline { // $TEST$ serialization C1 »C1("")«; + // $TEST$ serialization C1 + »C1()«; + // $TEST$ serialization Int »f1("")«; + + // $TEST$ serialization Int + »f1()«; }