Skip to content

Commit 53ed83c

Browse files
authored
[CS] Coerce dynamic member strings to their param type (#28512)
[CS] Coerce dynamic member strings to their param type
2 parents f6c22c9 + e599c8b commit 53ed83c

File tree

2 files changed

+23
-32
lines changed

2 files changed

+23
-32
lines changed

lib/Sema/CSApply.cpp

Lines changed: 16 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -267,27 +267,6 @@ static bool buildObjCKeyPathString(KeyPathExpr *E,
267267
return true;
268268
}
269269

270-
/// Form a type checked expression for the index of a @dynamicMemberLookup
271-
/// subscript index parameter.
272-
/// The index expression will have a tuple type of `(dynamicMember: T)`.
273-
static Expr *buildDynamicMemberLookupIndexExpr(StringRef name, SourceLoc loc,
274-
DeclContext *dc,
275-
ConstraintSystem &cs) {
276-
auto &ctx = cs.getASTContext();
277-
278-
auto *stringDecl = ctx.getStringDecl();
279-
auto stringType = stringDecl->getDeclaredType();
280-
281-
// Build and type check the string literal index value to the specific
282-
// string type expected by the subscript.
283-
auto *nameExpr = new (ctx) StringLiteralExpr(name, loc, /*implicit*/true);
284-
nameExpr->setBuiltinInitializer(ctx.getStringBuiltinInitDecl(stringDecl));
285-
nameExpr->setType(stringType);
286-
287-
cs.cacheExprTypes(nameExpr);
288-
return nameExpr;
289-
}
290-
291270
namespace {
292271

293272
/// Rewrites an expression by applying the solution of a constraint
@@ -2717,6 +2696,18 @@ namespace {
27172696
llvm_unreachable("Unhandled OverloadChoiceKind in switch.");
27182697
}
27192698

2699+
/// Form a type checked expression for the index of a @dynamicMemberLookup
2700+
/// subscript index parameter.
2701+
Expr *buildDynamicMemberLookupIndexExpr(StringRef name, SourceLoc loc,
2702+
Type literalTy) {
2703+
// Build and type check the string literal index value to the specific
2704+
// string type expected by the subscript.
2705+
auto &ctx = cs.getASTContext();
2706+
auto *nameExpr = new (ctx) StringLiteralExpr(name, loc, /*implicit*/true);
2707+
cs.setType(nameExpr, literalTy);
2708+
return handleStringLiteralExpr(nameExpr);
2709+
}
2710+
27202711
Expr *buildDynamicMemberLookupRef(Expr *expr, Expr *base, SourceLoc dotLoc,
27212712
SourceLoc nameLoc,
27222713
const SelectedOverload &overload,
@@ -2737,7 +2728,8 @@ namespace {
27372728
// Build and type check the string literal index value to the specific
27382729
// string type expected by the subscript.
27392730
auto fieldName = overload.choice.getName().getBaseIdentifier().str();
2740-
argExpr = buildDynamicMemberLookupIndexExpr(fieldName, nameLoc, dc, cs);
2731+
argExpr = buildDynamicMemberLookupIndexExpr(fieldName, nameLoc,
2732+
paramTy);
27412733
} else {
27422734
argExpr = buildKeyPathDynamicMemberIndexExpr(
27432735
paramTy->castTo<BoundGenericType>(), dotLoc, memberLocator);
@@ -4529,16 +4521,8 @@ namespace {
45294521
auto subscript = cast<SubscriptDecl>(overload.choice.getDecl());
45304522
assert(!subscript->isGetterMutating());
45314523

4532-
auto dc = subscript->getInnermostDeclContext();
4533-
4534-
auto indexType = AnyFunctionType::composeInput(
4535-
cs.getASTContext(),
4536-
subscript->getInterfaceType()->castTo<AnyFunctionType>()->getParams(),
4537-
/*canonicalVararg=*/false);
4538-
45394524
// Compute substitutions to refer to the member.
45404525
auto ref = resolveConcreteDeclRef(subscript, locator);
4541-
indexType = indexType.subst(ref.getSubstitutions());
45424526

45434527
// If this is a @dynamicMemberLookup reference to resolve a property
45444528
// through the subscript(dynamicMember:) member, restore the
@@ -4556,15 +4540,15 @@ namespace {
45564540

45574541
labels = cs.getASTContext().Id_dynamicMember;
45584542

4543+
auto indexType = getTypeOfDynamicMemberIndex(overload);
45594544
if (overload.choice.getKind() ==
45604545
OverloadChoiceKind::KeyPathDynamicMemberLookup) {
4561-
auto indexType = getTypeOfDynamicMemberIndex(overload);
45624546
indexExpr = buildKeyPathDynamicMemberIndexExpr(
45634547
indexType->castTo<BoundGenericType>(), componentLoc, locator);
45644548
} else {
45654549
auto fieldName = overload.choice.getName().getBaseIdentifier().str();
45664550
indexExpr = buildDynamicMemberLookupIndexExpr(fieldName, componentLoc,
4567-
dc, cs);
4551+
indexType);
45684552
}
45694553
}
45704554

test/attr/attr_dynamic_member_lookup.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -746,3 +746,10 @@ struct SR_10557_S1 {
746746
fatalError()
747747
}
748748
}
749+
750+
@dynamicMemberLookup
751+
struct SR11877 {
752+
subscript(dynamicMember member: Substring) -> Int { 0 }
753+
}
754+
755+
_ = \SR11877.okay

0 commit comments

Comments
 (0)