Skip to content

Commit dd353b9

Browse files
authored
Merge pull request swiftlang#63794 from xedin/rdar-105348781
[CSSimplify] Make "force optional unwrap" fix less aggressive
2 parents 193cbc9 + 0845435 commit dd353b9

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4600,6 +4600,14 @@ repairViaOptionalUnwrap(ConstraintSystem &cs, Type fromType, Type toType,
46004600
if (type->getOptionalObjectType())
46014601
fromType = type;
46024602

4603+
// Don't attempt the fix until sub-expression is resolved
4604+
// if chain is not using leading-dot syntax. This is better
4605+
// than attempting to propagate type information down optional
4606+
// chain which is hard to diagnose.
4607+
if (type->isTypeVariableOrMember() &&
4608+
!isa<UnresolvedMemberChainResultExpr>(subExpr))
4609+
return false;
4610+
46034611
// If this is a conversion from optional chain to some
46044612
// other type e.g. contextual type or a parameter type,
46054613
// let's use `Bind` to match object types because

test/Constraints/members.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -802,3 +802,16 @@ func test_leading_dot_syntax_unknown_base_ambiguity() {
802802

803803
fn("", value: .member) // expected-error {{cannot infer contextual base in reference to member 'member'}}
804804
}
805+
806+
// rdar://105348781 - failed to produce a diagnostic when passing optional to unrelated type.
807+
func test_mismatch_between_param_and_optional_chain() {
808+
func fn(_: String) {}
809+
810+
struct Test {
811+
var data: [Int]?
812+
813+
func test() {
814+
fn(data?.first) // expected-error {{cannot convert value of type 'Int?' to expected argument type 'String'}}
815+
}
816+
}
817+
}

0 commit comments

Comments
 (0)