Skip to content

Commit 8fc778e

Browse files
Merge pull request #33355 from LucianoPAlmeida/SR-13262-where-needs-bool
[SR-13262][Sema] Adding contextual type purpose for where clause expression in constraint system
2 parents 85dccde + 50ac9ab commit 8fc778e

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
@@ -3999,6 +3999,9 @@ generateForEachStmtConstraints(
39993999
if (cs.generateConstraints(whereTarget, FreeTypeVariableBinding::Disallow))
40004000
return None;
40014001

4002+
cs.setContextualType(forEachStmtInfo.whereExpr,
4003+
TypeLoc::withoutLoc(boolType), CTP_Condition);
4004+
40024005
forEachStmtInfo.whereExpr = whereTarget.getAsExpr();
40034006
}
40044007

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)