Skip to content

Commit 0ad1ff7

Browse files
committed
Generalize a condition to allow _OptionalNilComparisonType. We now diagnose all of
the cases the hack Mark reverted used to (and more).
1 parent 842d677 commit 0ad1ff7

File tree

3 files changed

+7
-8
lines changed

3 files changed

+7
-8
lines changed

lib/Sema/MiscDiagnostics.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -615,8 +615,7 @@ static void diagSyntacticUseRestrictions(TypeChecker &TC, const Expr *E,
615615
/// (declref_expr implicit decl=Optional.init(nilLiteral:)
616616
static bool isTypeCheckedOptionalNil(Expr *E) {
617617
auto CE = dyn_cast<CallExpr>(E->getSemanticsProvidingExpr());
618-
if (!CE || !CE->isImplicit() ||
619-
!CE->getType()->getAnyOptionalObjectType())
618+
if (!CE || !CE->isImplicit())
620619
return false;
621620

622621
auto CRCE = dyn_cast<ConstructorRefCallExpr>(CE->getSemanticFn());

test/ClangModules/nullability.swift

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@ import CoreCooling
77
func testSomeClass(_ sc: SomeClass, osc: SomeClass?) {
88
let ao1: AnyObject = sc.methodA(osc)
99
_ = ao1
10-
if sc.methodA(osc) == nil { }
10+
if sc.methodA(osc) == nil { } // expected-warning {{comparing non-optional value of type 'AnyObject' to nil always returns false}}
1111

1212
let ao2: AnyObject = sc.methodB(nil)
1313
_ = ao2
14-
if sc.methodA(osc) == nil { }
14+
if sc.methodA(osc) == nil { }// expected-warning {{comparing non-optional value of type 'AnyObject' to nil always returns false}}
1515

1616
let ao3: AnyObject = sc.property // expected-error{{value of optional type 'AnyObject?' not unwrapped; did you mean to use '!' or '?'?}} {{35-35=!}}
1717
_ = ao3
@@ -20,7 +20,7 @@ func testSomeClass(_ sc: SomeClass, osc: SomeClass?) {
2020

2121
let ao4: AnyObject = sc.methodD()
2222
_ = ao4
23-
if sc.methodD() == nil { }
23+
if sc.methodD() == nil { } // expected-warning {{comparing non-optional value of type 'AnyObject' to nil always returns false}}
2424

2525
sc.methodE(sc)
2626
sc.methodE(osc) // expected-error{{value of optional type 'SomeClass?' not unwrapped; did you mean to use '!' or '?'?}} {{17-17=!}}
@@ -37,7 +37,7 @@ func testSomeClass(_ sc: SomeClass, osc: SomeClass?) {
3737
let sc2 = SomeClass(int: ci)
3838
let sc2a: SomeClass = sc2
3939
_ = sc2a
40-
if sc2 == nil { }
40+
if sc2 == nil { } // expected-warning {{comparing non-optional value of type 'SomeClass' to nil always returns false}}
4141

4242
let sc3 = SomeClass(double: 1.5)
4343
if sc3 == nil { } // okay
@@ -47,7 +47,7 @@ func testSomeClass(_ sc: SomeClass, osc: SomeClass?) {
4747
let sc4 = sc.returnMe()
4848
let sc4a: SomeClass = sc4
4949
_ = sc4a
50-
if sc4 == nil { }
50+
if sc4 == nil { } // expected-warning {{comparing non-optional value of type 'SomeClass' to nil always returns false}}
5151
}
5252

5353
// Nullability with CF types.

test/Constraints/diagnostics.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -591,7 +591,7 @@ func r21684487() {
591591
func r18397777(_ d : r21447318?) {
592592
let c = r21447318()
593593

594-
if c != nil {
594+
if c != nil { // expected-warning {{comparing non-optional value of type 'r21447318' to nil always returns true}}
595595
}
596596

597597
if d { // expected-error {{optional type 'r21447318?' cannot be used as a boolean; test for '!= nil' instead}} {{6-6=(}} {{7-7= != nil)}}

0 commit comments

Comments
 (0)