Skip to content

Commit c5b6c29

Browse files
authored
Merge pull request #70209 from xedin/revert-delay-inference-from-OptionalObject
Temporarily revert #69292
2 parents 3a90828 + d9abf18 commit c5b6c29

File tree

5 files changed

+9
-68
lines changed

5 files changed

+9
-68
lines changed

lib/Sema/CSBindings.cpp

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1646,26 +1646,6 @@ PotentialBindings::inferFromRelational(Constraint *constraint) {
16461646
/// those types should be opened.
16471647
void PotentialBindings::infer(Constraint *constraint) {
16481648
switch (constraint->getKind()) {
1649-
case ConstraintKind::OptionalObject: {
1650-
// Inference through optional object is allowed if
1651-
// one of the types is resolved or "optional" type variable
1652-
// cannot be bound to l-value, otherwise there is a
1653-
// risk of binding "optional" to an optional type (inferred from
1654-
// the "object") and discovering an l-value binding for it later.
1655-
auto optionalType = constraint->getFirstType();
1656-
1657-
if (auto *optionalVar = optionalType->getAs<TypeVariableType>()) {
1658-
if (optionalVar->getImpl().canBindToLValue()) {
1659-
auto objectType =
1660-
constraint->getSecondType()->lookThroughAllOptionalTypes();
1661-
if (objectType->isTypeVariableOrMember())
1662-
return;
1663-
}
1664-
}
1665-
1666-
LLVM_FALLTHROUGH;
1667-
}
1668-
16691649
case ConstraintKind::Bind:
16701650
case ConstraintKind::Equal:
16711651
case ConstraintKind::BindParam:
@@ -1675,6 +1655,7 @@ void PotentialBindings::infer(Constraint *constraint) {
16751655
case ConstraintKind::Conversion:
16761656
case ConstraintKind::ArgumentConversion:
16771657
case ConstraintKind::OperatorArgumentConversion:
1658+
case ConstraintKind::OptionalObject:
16781659
case ConstraintKind::UnresolvedMemberChainBase: {
16791660
auto binding = inferFromRelational(constraint);
16801661
if (!binding)

lib/Sema/CSSimplify.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9179,8 +9179,8 @@ ConstraintSystem::simplifyOptionalObjectConstraint(
91799179
}
91809180

91819181
if (optTy->isPlaceholder()) {
9182-
// object type should be simplified because it could be already bound.
9183-
recordAnyTypeVarAsPotentialHole(simplifyType(second));
9182+
if (auto *typeVar = second->getAs<TypeVariableType>())
9183+
recordPotentialHole(typeVar);
91849184
return SolutionKind::Solved;
91859185
}
91869186

test/Constraints/diagnostics.swift

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1121,11 +1121,9 @@ func rdar17170728() {
11211121
var j: Int?
11221122
var k: Int? = 2
11231123

1124-
let _ = [i, j, k].reduce(0 as Int?) { // expected-error {{missing argument label 'into:' in call}}
1125-
// expected-error@-1 {{cannot convert value of type 'Int?' to expected argument type '(inout @escaping (Bool, Bool) -> Bool?, Int?) throws -> ()'}}
1124+
let _ = [i, j, k].reduce(0 as Int?) {
11261125
$0 && $1 ? $0! + $1! : ($0 ? $0! : ($1 ? $1! : nil))
1127-
// expected-error@-1 {{binary operator '+' cannot be applied to two 'Bool' operands}}
1128-
// expected-error@-2 4 {{cannot force unwrap value of non-optional type 'Bool'}}
1126+
// expected-error@-1 4 {{optional type 'Int?' cannot be used as a boolean; test for '!= nil' instead}}
11291127
}
11301128

11311129
let _ = [i, j, k].reduce(0 as Int?) { // expected-error {{missing argument label 'into:' in call}}
@@ -1555,18 +1553,18 @@ func testNilCoalescingOperatorRemoveFix() {
15551553
let _ = "" /* This is a comment */ ?? "" // expected-warning {{left side of nil coalescing operator '??' has non-optional type 'String', so the right side is never used}} {{13-43=}}
15561554

15571555
let _ = "" // This is a comment
1558-
?? "" // expected-warning {{left side of nil coalescing operator '??' has non-optional type 'String', so the right side is never used}} {{1557:13-1558:10=}}
1556+
?? "" // expected-warning {{left side of nil coalescing operator '??' has non-optional type 'String', so the right side is never used}} {{1555:13-1556:10=}}
15591557

15601558
let _ = "" // This is a comment
15611559
/*
15621560
* The blank line below is part of the test case, do not delete it
15631561
*/
15641562

1565-
?? "" // expected-warning {{left side of nil coalescing operator '??' has non-optional type 'String', so the right side is never used}} {{1560:13-1565:10=}}
1563+
?? "" // expected-warning {{left side of nil coalescing operator '??' has non-optional type 'String', so the right side is never used}} {{1558:13-1563:10=}}
15661564

1567-
if ("" ?? // This is a comment // expected-warning {{left side of nil coalescing operator '??' has non-optional type 'String', so the right side is never used}} {{9-1568:9=}}
1565+
if ("" ?? // This is a comment // expected-warning {{left side of nil coalescing operator '??' has non-optional type 'String', so the right side is never used}} {{9-1566:9=}}
15681566
"").isEmpty {}
15691567

15701568
if ("" // This is a comment
1571-
?? "").isEmpty {} // expected-warning {{left side of nil coalescing operator '??' has non-optional type 'String', so the right side is never used}} {{1570:9-1571:12=}}
1569+
?? "").isEmpty {} // expected-warning {{left side of nil coalescing operator '??' has non-optional type 'String', so the right side is never used}} {{1568:9-1569:12=}}
15721570
}

test/Constraints/optional.swift

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -598,19 +598,3 @@ do {
598598
test(x!) // expected-error {{no exact matches in call to local function 'test'}}
599599
// expected-error@-1 {{cannot force unwrap value of non-optional type 'Double'}}
600600
}
601-
602-
// Diagnose cases of invalid chaining when parameter is not optional based on context.
603-
do {
604-
class Test {
605-
var value: Int = 42
606-
}
607-
608-
class Container {
609-
let test: Test = Test()
610-
611-
func loop() {
612-
[test].forEach { $0?.value = 42 }
613-
// expected-error@-1 {{cannot use optional chaining on non-optional value of type 'Test'}}
614-
}
615-
}
616-
}

test/stmt/switch_stmt2.swift

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -150,25 +150,3 @@ func fallthrough_not_last(i: Int) {
150150
break
151151
}
152152
}
153-
154-
// rdar://117871338 - incorrect diagnostic - type of expression is ambiguous when member is missing.
155-
func test_invalid_optional_chaining() {
156-
func test(_: (E) -> Void) {
157-
}
158-
159-
enum E {
160-
case a
161-
case b
162-
}
163-
164-
struct S {
165-
var prop: E
166-
}
167-
168-
test {
169-
switch $0.prop? { // expected-error {{value of type 'E' has no member 'prop'}}
170-
case .a: break
171-
case .b: break
172-
}
173-
}
174-
}

0 commit comments

Comments
 (0)