Skip to content

Commit 5be227a

Browse files
committed
Sema: Fix the same bug with subscripts as well
Fixes rdar://161588385.
1 parent 782d68f commit 5be227a

File tree

3 files changed

+47
-21
lines changed

3 files changed

+47
-21
lines changed

lib/Sema/CSApply.cpp

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2510,8 +2510,27 @@ namespace {
25102510
// Coerce the index argument.
25112511
auto openedFullFnType = simplifyType(selected.adjustedOpenedFullType)
25122512
->castTo<FunctionType>();
2513+
2514+
auto openedFullFnTypeSelf = openedFullFnType;
2515+
2516+
// Now, deal with DynamicSelfType.
2517+
if (selected.adjustedOpenedFullType->hasDynamicSelfType()) {
2518+
openedFullFnTypeSelf = simplifyType(
2519+
selected.adjustedOpenedFullType->eraseDynamicSelfType())
2520+
->castTo<FunctionType>();
2521+
auto replacementTy = getDynamicSelfReplacementType(
2522+
baseTy, subscript, memberLoc);
2523+
openedFullFnType = simplifyType(
2524+
selected.adjustedOpenedFullType
2525+
->replaceDynamicSelfType(replacementTy))
2526+
->castTo<FunctionType>();
2527+
}
2528+
25132529
auto fullSubscriptTy = openedFullFnType->getResult()
25142530
->castTo<FunctionType>();
2531+
auto fullSubscriptTySelf = openedFullFnTypeSelf->getResult()
2532+
->castTo<FunctionType>();
2533+
25152534
auto appliedWrappers =
25162535
solution.getAppliedPropertyWrappers(memberLoc->getAnchor());
25172536
args = coerceCallArguments(
@@ -2565,38 +2584,28 @@ namespace {
25652584
if (!base)
25662585
return nullptr;
25672586

2568-
bool hasDynamicSelf = fullSubscriptTy->hasDynamicSelfType();
2569-
25702587
// Form the subscript expression.
25712588
auto subscriptExpr = SubscriptExpr::create(
25722589
ctx, base, args, subscriptRef, isImplicit, semantics);
25732590
subscriptExpr->setIsSuper(isSuper);
25742591

2575-
if (!hasDynamicSelf) {
2576-
cs.setType(subscriptExpr, fullSubscriptTy->getResult());
2577-
} else {
2578-
cs.setType(subscriptExpr,
2579-
fullSubscriptTy->getResult()
2580-
->replaceDynamicSelfType(containerTy));
2581-
}
2592+
cs.setType(subscriptExpr, fullSubscriptTySelf->getResult());
25822593

25832594
Expr *result = subscriptExpr;
2584-
closeExistentials(result, locator);
25852595

25862596
// If the element is of dynamic 'Self' type, wrap an implicit conversion
25872597
// around the resulting expression, with the destination type having
25882598
// 'Self' swapped for the appropriate replacement type -- usually the
25892599
// base object type.
2590-
if (hasDynamicSelf) {
2591-
const auto conversionTy = simplifyType(
2592-
selected.adjustedOpenedType->castTo<FunctionType>()->getResult());
2593-
2594-
if (!containerTy->isEqual(conversionTy)) {
2595-
result = cs.cacheType(
2596-
new (ctx) CovariantReturnConversionExpr(result, conversionTy));
2597-
}
2600+
if (!fullSubscriptTy->getResult()->isEqual(
2601+
fullSubscriptTySelf->getResult())) {
2602+
result = cs.cacheType(
2603+
new (ctx) CovariantReturnConversionExpr(
2604+
result, fullSubscriptTy->getResult()));
25982605
}
25992606

2607+
closeExistentials(result, locator);
2608+
26002609
return result;
26012610
}
26022611

test/SILGen/dynamic_self_opened_existential.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ func f(_ p: any P & C) {
1919
p.v.g1()
2020
p.v.g2()
2121

22-
// FIXME
23-
// p[].g1()
24-
// p[].g2()
22+
p[].g1()
23+
p[].g2()
2524
}
2625

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// RUN: %target-swift-emit-silgen %s
2+
3+
class AA {
4+
subscript<T>(_: T.Type) -> T? {
5+
get { fatalError() }
6+
set {}
7+
}
8+
}
9+
10+
class C {
11+
typealias A = AA
12+
13+
func f() {
14+
let a = AA()
15+
guard let result = a[Self.A.self] else { return }
16+
_ = result
17+
}
18+
}

0 commit comments

Comments
 (0)