Skip to content

Commit 17dc3c0

Browse files
authored
Merge pull request #21002 from xedin/rdar-46377919-5.0
[CSDiag] Verify that @autoclosure param has function type before casting
2 parents 4fd7d58 + 36cf21c commit 17dc3c0

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

lib/Sema/CSDiag.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3056,9 +3056,13 @@ typeCheckArgumentChildIndependently(Expr *argExpr, Type argType,
30563056
// Look at each of the arguments assigned to this parameter.
30573057
auto currentParamType = param.getOldType();
30583058

3059-
if (param.isAutoClosure())
3060-
currentParamType =
3061-
currentParamType->castTo<FunctionType>()->getResult();
3059+
// Since this is diagnostics, let's make sure that parameter
3060+
// marked as @autoclosure indeed has a function type, because
3061+
// it can also be an error type and possibly unresolved type.
3062+
if (param.isAutoClosure()) {
3063+
if (auto *funcType = currentParamType->getAs<FunctionType>())
3064+
currentParamType = funcType->getResult();
3065+
}
30623066

30633067
for (auto inArgNo : paramBindings[paramIdx]) {
30643068
// Determine the argument type.

test/Constraints/rdar46377919.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// RUN: %target-typecheck-verify-swift
2+
3+
class Foo {
4+
init(lhs: @autoclosure () -> Int,
5+
rhs: @autoclosure () -> Undefined) {
6+
// expected-error@-1 {{use of undeclared type 'Undefined'}}
7+
}
8+
}
9+
10+
func foo() -> Foo {
11+
return Foo(lhs: 2, rhs: 2)
12+
// expected-error@-1 {{cannot convert value of type 'Int' to expected argument type '<<error type>>'}}
13+
}

0 commit comments

Comments
 (0)