Skip to content

Commit 2759ae2

Browse files
committed
[AST] Add TypeBase::lookThroughSingleOptionalType
1 parent 899f145 commit 2759ae2

File tree

3 files changed

+12
-4
lines changed

3 files changed

+12
-4
lines changed

include/swift/AST/Types.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1067,6 +1067,9 @@ class alignas(1 << TypeAlignInBits) TypeBase {
10671067
// otherwise, return the null type.
10681068
Type getSwiftNewtypeUnderlyingType();
10691069

1070+
/// Return the type T after looking through at most one optional type.
1071+
Type lookThroughSingleOptionalType();
1072+
10701073
/// Return the type T after looking through all of the optional
10711074
/// types.
10721075
Type lookThroughAllOptionalTypes();

lib/AST/Type.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,13 @@ Type TypeBase::getAnyBufferPointerElementType(BufferPointerTypeKind &BPTK) {
670670
return Type();
671671
}
672672

673+
Type TypeBase::lookThroughSingleOptionalType() {
674+
Type type(this);
675+
if (auto objType = type->getOptionalObjectType())
676+
type = objType;
677+
return type;
678+
}
679+
673680
Type TypeBase::lookThroughAllOptionalTypes() {
674681
Type type(this);
675682
while (auto objType = type->getOptionalObjectType())

lib/Sema/CSSimplify.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7241,10 +7241,8 @@ ConstraintSystem::simplifyDynamicCallableApplicableFnConstraint(
72417241
}
72427242

72437243
static Type getBaseTypeForPointer(ConstraintSystem &cs, TypeBase *type) {
7244-
if (Type unwrapped = type->getOptionalObjectType())
7245-
type = unwrapped.getPointer();
7246-
7247-
auto pointeeTy = type->getAnyPointerElementType();
7244+
auto pointeeTy = type->lookThroughSingleOptionalType()
7245+
->getAnyPointerElementType();
72487246
assert(pointeeTy);
72497247
return pointeeTy;
72507248
}

0 commit comments

Comments
 (0)