@@ -3457,7 +3457,7 @@ bool MissingMemberFailure::diagnoseForSubscriptMemberWithTupleBase() const {
3457
3457
// number only literals.
3458
3458
if (literal && NumericRegex.match (literal->getDigitsText ())) {
3459
3459
unsigned int literalValue = 0 ;
3460
- literal->getDigitsText ().getAsInteger (/* Radix*/ 0 , literalValue);
3460
+ literal->getDigitsText ().getAsInteger (/* Radix= */ 0 , literalValue);
3461
3461
3462
3462
// Verify if the literal value is within the bounds of tuple elements.
3463
3463
if (!literal->isNegative () &&
@@ -3473,6 +3473,27 @@ bool MissingMemberFailure::diagnoseForSubscriptMemberWithTupleBase() const {
3473
3473
return true ;
3474
3474
}
3475
3475
}
3476
+
3477
+ // For subscript access on tuple base types where the subscript index is a
3478
+ // string literal expression which value matches a tuple element label,
3479
+ // let's suggest tuple label access.
3480
+ auto stringLiteral =
3481
+ dyn_cast<StringLiteralExpr>(index->getSemanticsProvidingExpr ());
3482
+ if (stringLiteral && !stringLiteral->getValue ().empty () &&
3483
+ llvm::any_of (tupleType->getElements (), [&](TupleTypeElt element) {
3484
+ return !element.getName ().empty () &&
3485
+ element.getName ().str () == stringLiteral->getValue ();
3486
+ })) {
3487
+ llvm::SmallString<16 > dotAccess;
3488
+ llvm::raw_svector_ostream OS (dotAccess);
3489
+ OS << " ." << stringLiteral->getValue ();
3490
+
3491
+ emitDiagnostic (
3492
+ diag::could_not_find_subscript_member_tuple_did_you_mean_use_dot,
3493
+ baseType, stringLiteral->getValue ())
3494
+ .fixItReplace (index->getSourceRange (), OS.str ());
3495
+ return true ;
3496
+ }
3476
3497
}
3477
3498
3478
3499
emitDiagnostic (diag::could_not_find_subscript_member_tuple, baseType);
0 commit comments