Skip to content

Commit 32634b1

Browse files
authored
Merge pull request #5239 from xedin/SR-1255
SR-1255: Improve diagnostic when one of the parameters marked as autoclosure
2 parents 6a61476 + f526a25 commit 32634b1

File tree

4 files changed

+28
-11
lines changed

4 files changed

+28
-11
lines changed

lib/Sema/CSDiag.cpp

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1286,15 +1286,27 @@ CalleeCandidateInfo::evaluateCloseness(DeclContext *dc, Type candArgListType,
12861286
bool mismatchesAreNearMisses = true;
12871287

12881288
CalleeCandidateInfo::FailedArgumentInfo failureInfo;
1289-
1289+
1290+
// Local function which extracts type from the parameter container.
1291+
auto getType = [](const CallArgParam &param) -> Type {
1292+
// If parameter is marked as @autoclosure, we are
1293+
// only interested in it's resulting type.
1294+
if (param.isAutoClosure()) {
1295+
if (auto fnType = param.Ty->getAs<AnyFunctionType>())
1296+
return fnType->getResult();
1297+
}
1298+
1299+
return param.Ty;
1300+
};
1301+
12901302
for (unsigned i = 0, e = paramBindings.size(); i != e; ++i) {
12911303
// Bindings specify the arguments that source the parameter. The only case
12921304
// this returns a non-singular value is when there are varargs in play.
12931305
auto &bindings = paramBindings[i];
1294-
auto paramType = candArgs[i].Ty;
1306+
auto paramType = getType(candArgs[i]);
12951307

12961308
for (auto argNo : bindings) {
1297-
auto argType = actualArgs[argNo].Ty;
1309+
auto argType = getType(actualArgs[argNo]);
12981310
auto rArgType = argType->getRValueType();
12991311

13001312
// If the argument has an unresolved type, then we're not actually
@@ -5264,7 +5276,7 @@ bool FailureDiagnosis::visitApplyExpr(ApplyExpr *callExpr) {
52645276
argType = tupleTy;
52655277
}
52665278
}
5267-
5279+
52685280
// Get the expression result of type checking the arguments to the call
52695281
// independently, so we have some idea of what we're working with.
52705282
//

test/Constraints/bridging.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,7 @@ func rdar19836341(_ ns: NSString?, vns: NSString?) {
288288
// <rdar://problem/20029786> Swift compiler sometimes suggests changing "as!" to "as?!"
289289
func rdar20029786(_ ns: NSString?) {
290290
var s: String = ns ?? "str" as String as String // expected-error{{cannot convert value of type 'NSString?' to expected argument type 'String?'}}
291-
var s2 = ns ?? "str" as String as String // expected-error {{binary operator '??' cannot be applied to operands of type 'NSString?' and 'String'}} expected-note{{}}
291+
var s2 = ns ?? "str" as String as String // expected-error {{cannot convert value of type 'String' to expected argument type 'NSString'}}
292292

293293
let s3: NSString? = "str" as String? // expected-error {{cannot convert value of type 'String?' to specified type 'NSString?'}}
294294

test/Constraints/diagnostics.swift

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -671,8 +671,7 @@ func r24251022() {
671671
func overloadSetResultType(_ a : Int, b : Int) -> Int {
672672
// https://twitter.com/_jlfischer/status/712337382175952896
673673
// TODO: <rdar://problem/27391581> QoI: Nonsensical "binary operator '&&' cannot be applied to two 'Bool' operands"
674-
return a == b && 1 == 2 // expected-error {{binary operator '&&' cannot be applied to two 'Bool' operands}}
675-
// expected-note @-1 {{expected an argument list of type '(Bool, @autoclosure () throws -> Bool)'}}
674+
return a == b && 1 == 2 // expected-error {{cannot convert return expression of type 'Bool' to return type 'Int'}}
676675
}
677676

678677
// <rdar://problem/21523291> compiler error message for mutating immutable field is incorrect
@@ -733,9 +732,8 @@ class Foo23752537 {
733732
extension Foo23752537 {
734733
func isEquivalent(other: Foo23752537) {
735734
// TODO: <rdar://problem/27391581> QoI: Nonsensical "binary operator '&&' cannot be applied to two 'Bool' operands"
736-
// expected-error @+1 {{binary operator '&&' cannot be applied to two 'Bool' operands}}
735+
// expected-error @+1 {{unexpected non-void return value in void function}}
737736
return (self.title != other.title && self.message != other.message)
738-
// expected-note @-1 {{expected an argument list of type '(Bool, @autoclosure () throws -> Bool)'}}
739737
}
740738
}
741739

@@ -815,3 +813,11 @@ r27212391(a: 1, 3, y: 5) // expected-error {{missing argument label 'x' in ca
815813
r27212391(1, x: 3, y: 5) // expected-error {{missing argument label 'a' in call}}
816814
r27212391(a: 1, y: 3, x: 5) // expected-error {{argument 'x' must precede argument 'y'}}
817815
r27212391(a: 1, 3, x: 5) // expected-error {{argument 'x' must precede unnamed argument #2}}
816+
817+
// SR-1255
818+
func foo1255_1() {
819+
return true || false // expected-error {{unexpected non-void return value in void function}}
820+
}
821+
func foo1255_2() -> Int {
822+
return true || false // expected-error {{cannot convert return expression of type 'Bool' to return type 'Int'}}
823+
}

test/expr/expressions.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -735,8 +735,7 @@ func testNilCoalescePrecedence(cond: Bool, a: Int?, r: CountableClosedRange<Int>
735735

736736
// ?? should have lower precedence than range and arithmetic operators.
737737
let r1 = r ?? (0...42) // ok
738-
let r2 = (r ?? 0)...42 // not ok: expected-error {{binary operator '??' cannot be applied to operands of type 'CountableClosedRange<Int>?' and 'Int'}}
739-
// expected-note @-1 {{overloads for '??' exist with these partially matching parameter lists:}}
738+
let r2 = (r ?? 0)...42 // not ok: expected-error {{cannot convert value of type 'Int' to expected argument type 'CountableClosedRange<Int>'}}
740739
let r3 = r ?? 0...42 // parses as the first one, not the second.
741740

742741

0 commit comments

Comments
 (0)