Skip to content

Commit 50ac9ab

Browse files
[Sema] Adding contextual type purpose for where clause expression in constraint system
1 parent e35d2d3 commit 50ac9ab

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

lib/Sema/CSGen.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4019,6 +4019,9 @@ generateForEachStmtConstraints(
40194019
if (cs.generateConstraints(whereTarget, FreeTypeVariableBinding::Disallow))
40204020
return None;
40214021

4022+
cs.setContextualType(forEachStmtInfo.whereExpr,
4023+
TypeLoc::withoutLoc(boolType), CTP_Condition);
4024+
40224025
forEachStmtInfo.whereExpr = whereTarget.getAsExpr();
40234026
}
40244027

test/Sema/diag_type_conversion.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,16 @@ _ = p =*= &o
7272

7373
func rdar25963182(_ bytes: [UInt8] = nil) {}
7474
// expected-error@-1 {{nil default argument value cannot be converted to type}}
75+
76+
// SR-13262
77+
struct SR13262_S {}
78+
79+
func SR13262(_ x: Int) {}
80+
func SR13262_Int(_ x: Int) -> Int { 0 }
81+
func SR13262_SF(_ x: Int) -> SR13262_S { SR13262_S() }
82+
83+
func testSR13262(_ arr: [Int]) {
84+
for x in arr where SR13262(x) {} // expected-error {{cannot convert value of type '()' to expected condition type 'Bool'}}
85+
for x in arr where SR13262_Int(x) {} // expected-error {{type 'Int' cannot be used as a boolean; test for '!= 0' instead}} {{22-22=(}} {{36-36= != 0)}}
86+
for x in arr where SR13262_SF(x) {} // expected-error {{cannot convert value of type 'SR13262_S' to expected condition type 'Bool'}}
87+
}

0 commit comments

Comments
 (0)