diff --git a/runtime/sema/check_array_expression.go b/runtime/sema/check_array_expression.go index 2da9858d0c..fa1b5c544c 100644 --- a/runtime/sema/check_array_expression.go +++ b/runtime/sema/check_array_expression.go @@ -80,16 +80,8 @@ func (checker *Checker) VisitArrayExpression(arrayExpression *ast.ArrayExpressio if elementType == nil { // Contextually expected type is not available. // Therefore, find the least common supertype of the elements. - elementType = LeastCommonSuperType(argumentTypes...) - + elementType = checker.leastCommonSuperType(arrayExpression, argumentTypes...) if elementType == InvalidType { - checker.report( - &TypeAnnotationRequiredError{ - Cause: "cannot infer type from array literal:", - Pos: arrayExpression.StartPos, - }, - ) - return InvalidType } diff --git a/runtime/sema/check_binary_expression.go b/runtime/sema/check_binary_expression.go index e15d1894a0..2980478306 100644 --- a/runtime/sema/check_binary_expression.go +++ b/runtime/sema/check_binary_expression.go @@ -468,5 +468,5 @@ func (checker *Checker) checkBinaryExpressionNilCoalescing( } } - return LeastCommonSuperType(leftOptional.Type, rightType) + return checker.leastCommonSuperType(expression, leftOptional.Type, rightType) } diff --git a/runtime/sema/check_conditional.go b/runtime/sema/check_conditional.go index 152c489e46..9c4da9cd52 100644 --- a/runtime/sema/check_conditional.go +++ b/runtime/sema/check_conditional.go @@ -128,7 +128,7 @@ func (checker *Checker) VisitConditionalExpression(expression *ast.ConditionalEx return thenType } - return LeastCommonSuperType(thenType, elseType) + return checker.leastCommonSuperType(expression, thenType, elseType) } // checkConditionalBranches checks two conditional branches. diff --git a/runtime/sema/check_dictionary_expression.go b/runtime/sema/check_dictionary_expression.go index 94a4758913..77f2c5fd78 100644 --- a/runtime/sema/check_dictionary_expression.go +++ b/runtime/sema/check_dictionary_expression.go @@ -69,18 +69,13 @@ func (checker *Checker) VisitDictionaryExpression(expression *ast.DictionaryExpr if keyType == nil && valueType == nil { // Contextually expected type is not available. // Therefore, find the least common supertype of the keys and values. - keyType = LeastCommonSuperType(keyTypes...) - valueType = LeastCommonSuperType(valueTypes...) - - if keyType == InvalidType || - valueType == InvalidType { - checker.report( - &TypeAnnotationRequiredError{ - Cause: "cannot infer type from dictionary literal:", - Pos: expression.StartPos, - }, - ) + keyType = checker.leastCommonSuperType(expression, keyTypes...) + if keyType == InvalidType { + return InvalidType + } + valueType = checker.leastCommonSuperType(expression, valueTypes...) + if valueType == InvalidType { return InvalidType } } diff --git a/runtime/sema/checker.go b/runtime/sema/checker.go index e44c8c5603..a03655e0ac 100644 --- a/runtime/sema/checker.go +++ b/runtime/sema/checker.go @@ -2766,3 +2766,18 @@ func (checker *Checker) checkNativeModifier(isNative bool, position ast.HasPosit ) } } + +func (checker *Checker) leastCommonSuperType(pos ast.HasPosition, types ...Type) Type { + elementType := LeastCommonSuperType(types...) + + if elementType == InvalidType { + checker.report( + &TypeAnnotationRequiredError{ + Cause: "cannot infer type:", + Pos: pos.StartPosition(), + }, + ) + } + + return elementType +} diff --git a/runtime/tests/checker/conditional_test.go b/runtime/tests/checker/conditional_test.go index 6078d12fd0..86e918a018 100644 --- a/runtime/tests/checker/conditional_test.go +++ b/runtime/tests/checker/conditional_test.go @@ -66,9 +66,10 @@ func TestCheckInvalidConditionalExpressionElse(t *testing.T) { let x = true ? 2 : y `) - errs := RequireCheckerErrors(t, err, 1) + errs := RequireCheckerErrors(t, err, 2) assert.IsType(t, &sema.NotDeclaredError{}, errs[0]) + assert.IsType(t, &sema.TypeAnnotationRequiredError{}, errs[1]) xType := RequireGlobalValue(t, checker.Elaboration, "x") assert.Equal(t, sema.InvalidType, xType)