Skip to content

Commit cb545e7

Browse files
committed
[ConstraintSystem] Preserve l-valueness of the result after implicit IUO unwrap
Detect that result type of the overload choice is l-value and preserve that information through the forced unwrap operation so it's possible to load the value implicitly during solution application. Resolves: rdar://problem/61337704
1 parent 40d7da4 commit cb545e7

File tree

4 files changed

+27
-3
lines changed

4 files changed

+27
-3
lines changed

lib/Sema/CSApply.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5398,7 +5398,7 @@ Expr *ExprRewriter::coerceOptionalToOptional(Expr *expr, Type toType,
53985398

53995399
Expr *ExprRewriter::coerceImplicitlyUnwrappedOptionalToValue(Expr *expr, Type objTy) {
54005400
auto optTy = cs.getType(expr);
5401-
// Coerce to an r-value.
5401+
// Preserve l-valueness of the result.
54025402
if (optTy->is<LValueType>())
54035403
objTy = LValueType::get(objTy);
54045404

lib/Sema/ConstraintSystem.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3966,8 +3966,13 @@ class ConstraintSystem {
39663966
auto resultTy = fnTy->getResult();
39673967
if (auto *resultFnTy = resultTy->getAs<AnyFunctionType>())
39683968
resultTy = replaceFinalResultTypeWithUnderlying(resultFnTy);
3969-
else
3970-
resultTy = resultTy->getWithoutSpecifierType()->getOptionalObjectType();
3969+
else {
3970+
auto objType =
3971+
resultTy->getWithoutSpecifierType()->getOptionalObjectType();
3972+
// Preserve l-value through force operation.
3973+
resultTy =
3974+
resultTy->is<LValueType>() ? LValueType::get(objType) : objType;
3975+
}
39713976

39723977
assert(resultTy);
39733978

test/Constraints/iuo_objc.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,10 @@ extension X where Self : OptionalRequirements {
5656
let _ = self.name
5757
}
5858
}
59+
60+
func rdar61337704() {
61+
func setColor(v: ColorDescriptor) { }
62+
63+
let descriptor = PaletteDescriptor()
64+
setColor(v: descriptor.colors[0]) // Ok
65+
}

test/Inputs/clang-importer-sdk/usr/include/Foundation.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1181,3 +1181,15 @@ void takeNullableId(_Nullable id);
11811181
@interface IUOProperty
11821182
@property (readonly) id<OptionalRequirements> iuo;
11831183
@end
1184+
1185+
@interface ColorDescriptor : NSObject <NSCopying>
1186+
@end
1187+
1188+
@interface ColorArray : NSObject
1189+
- (ColorDescriptor *)objectAtIndexedSubscript:(NSUInteger)attachmentIndex;
1190+
- (void)setObject:(nullable ColorDescriptor *)attachment atIndexedSubscript:(NSUInteger)attachmentIndex;
1191+
@end
1192+
1193+
@interface PaletteDescriptor : NSObject <NSCopying>
1194+
@property (readonly, nonnull) ColorArray *colors;
1195+
@end

0 commit comments

Comments
 (0)