Skip to content

Commit 96900f9

Browse files
authored
Merge pull request #84012 from xedin/rdar-158063151
[CSOptimizer] Don't match `nil` to `_OptionalNilComparisonType`
2 parents 3f96cef + df962a8 commit 96900f9

File tree

4 files changed

+18
-21
lines changed

4 files changed

+18
-21
lines changed

lib/Sema/CSOptimizer.cpp

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -260,13 +260,6 @@ static bool isStandardInfixLogicalOperator(Constraint *disjunction) {
260260
return false;
261261
}
262262

263-
static bool isOperatorNamed(Constraint *disjunction, StringRef name) {
264-
auto *choice = disjunction->getNestedConstraints()[0];
265-
if (auto *decl = getOverloadChoiceDecl(choice))
266-
return decl->isOperator() && decl->getBaseIdentifier().is(name);
267-
return false;
268-
}
269-
270263
static bool isArithmeticOperator(ValueDecl *decl) {
271264
return decl->isOperator() && decl->getBaseIdentifier().isArithmeticOperator();
272265
}
@@ -1022,19 +1015,6 @@ static void determineBestChoicesInContext(
10221015
optionals.size());
10231016
types.push_back({type,
10241017
/*fromLiteral=*/true});
1025-
} else if (literal.first ==
1026-
cs.getASTContext().getProtocol(
1027-
KnownProtocolKind::ExpressibleByNilLiteral) &&
1028-
literal.second.IsDirectRequirement) {
1029-
// `==` and `!=` operators have special overloads that accept `nil`
1030-
// as `_OptionalNilComparisonType` which is preferred over a
1031-
// generic form `(T?, T?)`.
1032-
if (isOperatorNamed(disjunction, "==") ||
1033-
isOperatorNamed(disjunction, "!=")) {
1034-
auto nilComparisonTy =
1035-
cs.getASTContext().get_OptionalNilComparisonTypeType();
1036-
types.push_back({nilComparisonTy, /*fromLiteral=*/true});
1037-
}
10381018
}
10391019
}
10401020

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
// RUN: %target-swift-emit-silgen %s | %FileCheck %s
2+
3+
// Make sure that the type-checker selects an `Equatable.==` instead of one from stdlib that takes _OptionalNilComparisonType
4+
5+
struct Value: Equatable, ExpressibleByNilLiteral {
6+
init(nilLiteral: ()) {
7+
}
8+
}
9+
10+
// CHECK-LABEL: sil hidden [ossa] @$s13rdar1580631514test1vyAA5ValueV_tF : $@convention(thin) (Value) -> ()
11+
// function_ref static Value.__derived_struct_equals(_:_:)
12+
// CHECK: [[EQUALS_REF:%.*]] = function_ref @$s13rdar1580631515ValueV23__derived_struct_equalsySbAC_ACtFZ
13+
// CHECK-NEXT: apply [[EQUALS_REF]](%0, {{.*}})
14+
func test(v: Value) {
15+
_ = v == nil
16+
}

test/SILOptimizer/infinite_recursion.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ public class U {
282282
}
283283

284284
func == (l: S?, r: S?) -> Bool {
285-
if l == nil && r == nil { return true }
285+
if l == nil && r == nil { return true } // expected-warning {{function call causes an infinite recursion}}
286286
guard let l = l, let r = r else { return false }
287287
return l === r
288288
}

validation-test/Sema/type_checker_perf/fast/rdar17170728.swift renamed to validation-test/Sema/type_checker_perf/slow/rdar17170728.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ let i: Int? = 1
55
let j: Int?
66
let k: Int? = 2
77

8+
// expected-error@+1 {{the compiler is unable to type-check this expression in reasonable time}}
89
let _ = [i, j, k].reduce(0 as Int?) {
910
$0 != nil && $1 != nil ? $0! + $1! : ($0 != nil ? $0! : ($1 != nil ? $1! : nil))
1011
}

0 commit comments

Comments
 (0)