Skip to content

Commit 95acbb9

Browse files
authored
Merge pull request #17849 from rudkx/minor-cleanup-in-optional-conversions
[ConstraintSystem] Small refactor of determining optional conversions.
2 parents c8b61f9 + dc8a71d commit 95acbb9

File tree

1 file changed

+13
-17
lines changed

1 file changed

+13
-17
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
//
33
// This source file is part of the Swift.org open source project
44
//
5-
// Copyright (c) 2014 - 2017 Apple Inc. and the Swift project authors
5+
// Copyright (c) 2014 - 2018 Apple Inc. and the Swift project authors
66
// Licensed under Apache License v2.0 with Runtime Library Exception
77
//
88
// See https://swift.org/LICENSE.txt for license information
@@ -1437,10 +1437,16 @@ static bool isStringCompatiblePointerBaseType(TypeChecker &TC,
14371437
/// Determine whether the first type with the given number of optionals
14381438
/// is potentially more optional than the second type with its number of
14391439
/// optionals.
1440-
static bool isPotentiallyMoreOptionalThan(Type objType1,
1441-
unsigned numOptionals1,
1442-
Type objType2,
1443-
unsigned numOptionals2) {
1440+
static bool isPotentiallyMoreOptionalThan(Type type1, Type type2) {
1441+
1442+
SmallVector<Type, 2> optionals1;
1443+
Type objType1 = type1->lookThroughAllOptionalTypes(optionals1);
1444+
auto numOptionals1 = optionals1.size();
1445+
1446+
SmallVector<Type, 2> optionals2;
1447+
type2->lookThroughAllOptionalTypes(optionals2);
1448+
auto numOptionals2 = optionals2.size();
1449+
14441450
if (numOptionals1 <= numOptionals2 && !objType1->isTypeVariableOrMember())
14451451
return false;
14461452

@@ -1452,22 +1458,12 @@ static void enumerateOptionalConversionRestrictions(
14521458
Type type1, Type type2,
14531459
ConstraintKind kind, ConstraintLocatorBuilder locator,
14541460
llvm::function_ref<void(ConversionRestrictionKind)> fn) {
1455-
SmallVector<Type, 2> optionals1;
1456-
Type objType1 = type1->lookThroughAllOptionalTypes(optionals1);
1457-
1458-
SmallVector<Type, 2> optionals2;
1459-
Type objType2 = type2->lookThroughAllOptionalTypes(optionals2);
1460-
1461-
if (optionals1.empty() && optionals2.empty())
1462-
return;
1463-
14641461
// Optional-to-optional.
1465-
if (!optionals1.empty() && !optionals2.empty())
1462+
if (type1->getOptionalObjectType() && type2->getOptionalObjectType())
14661463
fn(ConversionRestrictionKind::OptionalToOptional);
14671464

14681465
// Inject a value into an optional.
1469-
if (isPotentiallyMoreOptionalThan(objType2, optionals2.size(),
1470-
objType1, optionals1.size())) {
1466+
if (isPotentiallyMoreOptionalThan(type2, type1)) {
14711467
fn(ConversionRestrictionKind::ValueToOptional);
14721468
}
14731469
}

0 commit comments

Comments
 (0)