Skip to content

Commit 589283b

Browse files
committed
[ConstraintSystem] Teach sanitizer about dynamic member lookup expressions
Type-check based diagnostics could introduce keypath dynamic member expressions into AST, which have to be converted back to their original member or subscript reference form before attempting re-typecheck.
1 parent d4bbcc1 commit 589283b

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

lib/Sema/CSGen.cpp

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3274,6 +3274,38 @@ namespace {
32743274
expr = sanitizeArgumentList(expr);
32753275
}
32763276

3277+
// If this expression represents keypath based dynamic member
3278+
// lookup, let's convert it back to the original form of
3279+
// member or subscript reference.
3280+
if (auto *SE = dyn_cast<SubscriptExpr>(expr)) {
3281+
if (auto *TE = dyn_cast<TupleExpr>(SE->getIndex())) {
3282+
auto isImplicitKeyPathExpr = [](Expr *argExpr) -> bool {
3283+
if (auto *KP = dyn_cast<KeyPathExpr>(argExpr))
3284+
return KP->isImplicit();
3285+
return false;
3286+
};
3287+
3288+
if (TE->isImplicit() && TE->getNumElements() == 1 &&
3289+
TE->getElementName(0) == TC.Context.Id_dynamicMember &&
3290+
isImplicitKeyPathExpr(TE->getElement(0))) {
3291+
auto *keyPathExpr = cast<KeyPathExpr>(TE->getElement(0));
3292+
auto *componentExpr = keyPathExpr->getParsedPath();
3293+
3294+
if (auto *UDE = dyn_cast<UnresolvedDotExpr>(componentExpr)) {
3295+
UDE->setBase(SE->getBase());
3296+
return {true, UDE};
3297+
}
3298+
3299+
if (auto *subscript = dyn_cast<SubscriptExpr>(componentExpr)) {
3300+
subscript->setBase(SE->getBase());
3301+
return {true, subscript};
3302+
}
3303+
3304+
llvm_unreachable("unknown keypath component type");
3305+
}
3306+
}
3307+
}
3308+
32773309
// Now, we're ready to walk into sub expressions.
32783310
return {true, expr};
32793311
}
@@ -3898,4 +3930,4 @@ swift::getOriginalArgumentList(Expr *expr) {
38983930
}
38993931

39003932
return result;
3901-
}
3933+
}

0 commit comments

Comments
 (0)