Skip to content

Commit 2641554

Browse files
committed
[Diagnostics] Attempt .rawValue fix only if both types are equally optional
This is a follow-up to #35072. Let's wait until both sides are equally optional before attempting `.rawValue` fix, otherwise there is a risk that a valid code would get diagnosed. Extend test coverage of possible `.rawValue` situations to contextual and argument positions to make sure that valid code is accepted. Resolves: SR-13951
1 parent 949b0c0 commit 2641554

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4263,13 +4263,18 @@ bool ConstraintSystem::repairFailures(
42634263
break;
42644264
}
42654265

4266-
if (repairByUsingRawValueOfRawRepresentableType(lhs, rhs))
4267-
break;
4268-
42694266
if (repairViaOptionalUnwrap(*this, lhs, rhs, matchKind, conversionsOrFixes,
42704267
locator))
42714268
break;
42724269

4270+
// Let's wait until both sides are of the same optionality before
4271+
// attempting `.rawValue` fix.
4272+
if (hasConversionOrRestriction(ConversionRestrictionKind::ValueToOptional))
4273+
break;
4274+
4275+
if (repairByUsingRawValueOfRawRepresentableType(lhs, rhs))
4276+
break;
4277+
42734278
// If there are any restrictions here we need to wait and let
42744279
// `simplifyRestrictedConstraintImpl` handle them.
42754280
if (llvm::any_of(conversionsOrFixes,

test/Constraints/sr13951.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,16 @@ func aTransformer(input: Int) -> TheEnum {
2929
func theProblem(input: Int?) {
3030
var enumValue: TheEnum?
3131

32+
func test_arg_position(_: TheEnum?) {}
33+
3234
if let input = input {
3335
enumValue = aTransformer(input: input) // Ok
36+
let _: TheEnum? = enumValue // Ok
37+
let _: TheEnum? = aTransformer(input: input) // Ok
38+
let _: TheEnum?? = enumValue // Ok
39+
let _: TheEnum?? = aTransformer(input: input) // Ok
40+
test_arg_position(aTransformer(input: input)) // Ok
41+
test_arg_position(enumValue) // Ok
3442
}
3543

3644
_ = enumValue // To silence the warning

0 commit comments

Comments
 (0)