Skip to content

Commit 041051e

Browse files
[CSDiagnostics] Tailor diagnostic for define missing member when involves array literal and unresolved member
1 parent 3039ca4 commit 041051e

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

lib/Sema/CSDiagnostics.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3212,6 +3212,9 @@ bool MissingMemberFailure::diagnoseAsError() {
32123212

32133213
if (diagnoseForDynamicCallable())
32143214
return true;
3215+
3216+
if (diagnoseForDefaultAnyArrayLiteral())
3217+
return true;
32153218

32163219
auto baseType = resolveType(getBaseType())->getWithoutSpecifierType();
32173220

@@ -3399,6 +3402,29 @@ bool MissingMemberFailure::diagnoseForDynamicCallable() const {
33993402
return false;
34003403
}
34013404

3405+
bool MissingMemberFailure::diagnoseForDefaultAnyArrayLiteral() const {
3406+
auto &cs = getConstraintSystem();
3407+
auto *expr = getAsExpr(getAnchor());
3408+
auto *parentExpr = cs.getParentExpr(expr);
3409+
auto contextualType = getContextualType(parentExpr);
3410+
auto baseType = resolveType(getBaseType())->getWithoutSpecifierType();
3411+
3412+
if (contextualType)
3413+
return false;
3414+
3415+
if (isa<UnresolvedMemberExpr>(expr) && isa<ArrayExpr>(parentExpr)) {
3416+
if (auto *metatype = baseType->getAs<MetatypeType>()) {
3417+
baseType = metatype->getInstanceType();
3418+
}
3419+
3420+
if (baseType->isAny()) {
3421+
emitDiagnostic(diag::unresolved_member_no_inference, getName());
3422+
return true;
3423+
}
3424+
}
3425+
return false;
3426+
}
3427+
34023428
bool InvalidMemberRefOnExistential::diagnoseAsError() {
34033429
auto anchor = getRawAnchor();
34043430

lib/Sema/CSDiagnostics.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,6 +1059,10 @@ class MissingMemberFailure final : public InvalidMemberRefFailure {
10591059
/// overload to be present, but a class marked as `@dynamicCallable`
10601060
/// defines only `dynamicallyCall(withArguments:)` variant.
10611061
bool diagnoseForDynamicCallable() const;
1062+
1063+
/// Tailored diagnostics for array literal with unresolved member expression
1064+
/// that defaults the element to element type. e.g. _ = [.e]
1065+
bool diagnoseForDefaultAnyArrayLiteral() const;
10621066

10631067
static DeclName findCorrectEnumCaseName(Type Ty,
10641068
TypoCorrectionResults &corrections,

0 commit comments

Comments
 (0)