File tree Expand file tree Collapse file tree 2 files changed +31
-1
lines changed Expand file tree Collapse file tree 2 files changed +31
-1
lines changed Original file line number Diff line number Diff line change @@ -5883,6 +5883,24 @@ bool ConstraintSystem::repairFailures(
5883
5883
if (hasConversionOrRestriction(ConversionRestrictionKind::Existential))
5884
5884
break;
5885
5885
5886
+ if (auto *typeVar =
5887
+ lhs->getOptionalObjectType()->getAs<TypeVariableType>()) {
5888
+ auto *argLoc = typeVar->getImpl().getLocator();
5889
+ if (argLoc->directlyAt<OptionalEvaluationExpr>()) {
5890
+ auto OEE = castToExpr<OptionalEvaluationExpr>(argLoc->getAnchor());
5891
+ // If the optional chain in the argument position is invalid
5892
+ // let's unwrap optional and re-introduce the constraint to
5893
+ // be solved later once both sides are sufficiently resolved,
5894
+ // this would allow to diagnose not only the invalid unwrap
5895
+ // but an invalid conversion (if any) as well.
5896
+ if (hasFixFor(getConstraintLocator(OEE->getSubExpr()),
5897
+ FixKind::RemoveUnwrap)) {
5898
+ addConstraint(matchKind, typeVar, rhs, loc);
5899
+ return true;
5900
+ }
5901
+ }
5902
+ }
5903
+
5886
5904
auto result = matchTypes(lhs->getOptionalObjectType(), rhs, matchKind,
5887
5905
TMF_ApplyingFix, locator);
5888
5906
Original file line number Diff line number Diff line change @@ -435,7 +435,7 @@ func test_force_unwrap_not_being_too_eager() {
435
435
// rdar://problem/57097401
436
436
func invalidOptionalChaining( a: Any ) {
437
437
a == " = " ? // expected-error {{cannot use optional chaining on non-optional value of type 'String'}}
438
- // expected-error@-1 {{binary operator '==' cannot be applied to operands of type 'Any' and 'String? '}}
438
+ // expected-error@-1 {{cannot convert value of type 'Any' to expected argument type 'String'}}
439
439
}
440
440
441
441
/// https://github.com/apple/swift/issues/54739
@@ -595,3 +595,15 @@ do {
595
595
test ( x!) // expected-error {{no exact matches in call to local function 'test'}}
596
596
// expected-error@-1 {{cannot force unwrap value of non-optional type 'Double'}}
597
597
}
598
+
599
+ func testExtraQuestionMark( action: ( ) -> Void , v: Int ) {
600
+ struct Test {
601
+ init ( action: ( ) -> Void ) { }
602
+ }
603
+
604
+ Test ( action: action? )
605
+ // expected-error@-1 {{cannot use optional chaining on non-optional value of type '() -> Void'}}
606
+ Test ( action: v? )
607
+ // expected-error@-1 {{cannot convert value of type 'Int' to expected argument type '() -> Void'}}
608
+ // expected-error@-2 {{cannot use optional chaining on non-optional value of type 'Int'}}
609
+ }
You can’t perform that action at this time.
0 commit comments