Skip to content

Commit 3a961c8

Browse files
[CSSimplify] Adjusting logic on simplifyOptionalObjectConstraint to attempt InsertCall fix before remove unwrap
1 parent a43e7f8 commit 3a961c8

File tree

4 files changed

+37
-33
lines changed

4 files changed

+37
-33
lines changed

lib/Sema/CSDiagnostics.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -498,7 +498,7 @@ class MemberAccessOnOptionalBaseFailure final : public FailureDiagnostic {
498498
}
499499

500500
SourceLoc getLoc() const override {
501-
// The end location points the dot in the member access.
501+
// The end location points to the dot in the member access.
502502
return getSourceRange().End;
503503
}
504504

lib/Sema/CSSimplify.cpp

Lines changed: 28 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5697,41 +5697,39 @@ ConstraintSystem::simplifyOptionalObjectConstraint(
56975697
// If the base type is not optional, let's attempt a fix (if possible)
56985698
// and assume that `!` is just not there.
56995699
if (!objectTy) {
5700+
if (!shouldAttemptFixes())
5701+
return SolutionKind::Error;
5702+
57005703
// Let's see if we can apply a specific fix here.
5701-
if (shouldAttemptFixes()) {
5702-
if (optTy->isHole())
5703-
return SolutionKind::Solved;
5704+
if (optTy->isHole())
5705+
return SolutionKind::Solved;
5706+
5707+
auto fnType = optTy->getAs<FunctionType>();
5708+
if (fnType && fnType->getNumParams() == 0) {
5709+
// For function types with no parameters, let's try to
5710+
// offer a "make it a call" fix if possible.
5711+
auto optionalResultType = fnType->getResult()->getOptionalObjectType();
5712+
if (optionalResultType) {
5713+
if (matchTypes(optionalResultType, second, ConstraintKind::Bind,
5714+
flags | TMF_ApplyingFix, locator)
5715+
.isSuccess()) {
5716+
auto *fix =
5717+
InsertExplicitCall::create(*this, getConstraintLocator(locator));
5718+
5719+
return recordFix(fix) ? SolutionKind::Error : SolutionKind::Solved;
5720+
}
5721+
}
5722+
}
57045723

5705-
auto *fix =
5706-
RemoveUnwrap::create(*this, optTy, getConstraintLocator(locator));
5724+
auto *fix =
5725+
RemoveUnwrap::create(*this, optTy, getConstraintLocator(locator));
57075726

5708-
if (recordFix(fix))
5709-
return SolutionKind::Error;
5710-
5711-
// If the fix was successful let's record
5712-
// "fixed" object type and continue.
5713-
objectTy = optTy;
5714-
} else {
5715-
// If fixes are not allowed, no choice but to fail.
5727+
if (recordFix(fix))
57165728
return SolutionKind::Error;
5717-
}
5718-
}
57195729

5720-
auto fnType = optTy->getAs<FunctionType>();
5721-
if (shouldAttemptFixes() && fnType && fnType->getNumParams() == 0) {
5722-
// For function types with no parameters, let's try to
5723-
// offer a "make it a call" fix if possible.
5724-
auto optionalResultType = fnType->getResult()->getOptionalObjectType();
5725-
if (optionalResultType) {
5726-
if (matchTypes(optionalResultType, second, ConstraintKind::Bind,
5727-
flags | TMF_ApplyingFix, locator)
5728-
.isSuccess()) {
5729-
auto *fix =
5730-
InsertExplicitCall::create(*this, getConstraintLocator(locator));
5731-
5732-
return recordFix(fix) ? SolutionKind::Error : SolutionKind::Solved;
5733-
}
5734-
}
5730+
// If the fix was successful let's record
5731+
// "fixed" object type and continue.
5732+
objectTy = optTy;
57355733
}
57365734

57375735
// The object type is an lvalue if the optional was.

test/Constraints/diagnostics.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1342,3 +1342,10 @@ func rdar62989214() {
13421342
arr[flag].isTrue // expected-error {{cannot convert value of type 'Flag' to expected argument type 'Int'}}
13431343
}
13441344
}
1345+
1346+
// SR-5688
1347+
func SR5688_1() -> String? { "" }
1348+
SR5688_1!.count // expected-error {{function 'SR5688_1' was used as a property; add () to call it}} {{9-9=()}}
1349+
1350+
func SR5688_2() -> Int? { 0 }
1351+
let _: Int = SR5688_2! // expected-error {{function 'SR5688_2' was used as a property; add () to call it}} {{22-22=()}}

test/Constraints/fixes.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,7 @@ class T {
110110
func m1() {
111111
// <rdar://problem/17741575>
112112
let l = self.m2!.prop1
113-
// expected-error@-1 {{cannot force unwrap value of non-optional type '() -> U?'}} {{22-23=}}
114-
// expected-error@-2 {{method 'm2' was used as a property; add () to call it}} {{22-22=()}}
113+
// expected-error@-1 {{method 'm2' was used as a property; add () to call it}} {{22-22=()}}
115114
}
116115

117116
func m2() -> U! {

0 commit comments

Comments
 (0)