Skip to content

Commit 723a753

Browse files
[CSDiagnostics] Tailor diagnostic for define missing member when involving a collectin type
1 parent 4f7ad54 commit 723a753

File tree

3 files changed

+29
-17
lines changed

3 files changed

+29
-17
lines changed

lib/Sema/CSDiagnostics.cpp

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3213,7 +3213,7 @@ bool MissingMemberFailure::diagnoseAsError() {
32133213
if (diagnoseForDynamicCallable())
32143214
return true;
32153215

3216-
if (diagnoseForDefaultAnyArrayLiteral())
3216+
if (diagnoseInLiteralCollectionContext())
32173217
return true;
32183218

32193219
auto baseType = resolveType(getBaseType())->getWithoutSpecifierType();
@@ -3402,23 +3402,30 @@ bool MissingMemberFailure::diagnoseForDynamicCallable() const {
34023402
return false;
34033403
}
34043404

3405-
bool MissingMemberFailure::diagnoseForDefaultAnyArrayLiteral() const {
3405+
bool MissingMemberFailure::diagnoseInLiteralCollectionContext() const {
34063406
auto &cs = getConstraintSystem();
3407-
auto *expr = getAsExpr(getAnchor());
3407+
auto *expr = castToExpr(getAnchor());
34083408
auto *parentExpr = cs.getParentExpr(expr);
3409-
auto contextualType = getContextualType(parentExpr);
3410-
auto baseType = resolveType(getBaseType())->getWithoutSpecifierType();
3409+
auto &solution = getSolution();
34113410

3412-
if (contextualType)
3411+
if (!(parentExpr && isa<UnresolvedMemberExpr>(expr)))
34133412
return false;
3414-
3415-
if (isa<UnresolvedMemberExpr>(expr) &&
3416-
parentExpr && isa<ArrayExpr>(parentExpr)) {
3417-
if (auto *metatype = baseType->getAs<MetatypeType>()) {
3418-
baseType = metatype->getInstanceType();
3419-
}
3420-
3421-
if (baseType->isAny()) {
3413+
3414+
auto parentType = getType(parentExpr);
3415+
3416+
if (!cs.isCollectionType(parentType) && !parentType->is<TupleType>())
3417+
return false;
3418+
3419+
if (isa<TupleExpr>(parentExpr)) {
3420+
parentExpr = cs.getParentExpr(parentExpr);
3421+
if (!parentExpr)
3422+
return false;
3423+
}
3424+
3425+
if (auto *defaultableVar =
3426+
cs.getType(parentExpr)->getAs<TypeVariableType>()) {
3427+
if (solution.DefaultedConstraints.count(
3428+
defaultableVar->getImpl().getLocator()) != 0) {
34223429
emitDiagnostic(diag::unresolved_member_no_inference, getName());
34233430
return true;
34243431
}

lib/Sema/CSDiagnostics.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1060,9 +1060,9 @@ class MissingMemberFailure final : public InvalidMemberRefFailure {
10601060
/// defines only `dynamicallyCall(withArguments:)` variant.
10611061
bool diagnoseForDynamicCallable() const;
10621062

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;
1063+
/// Tailored diagnostics for collection literal with unresolved member expression
1064+
/// that defaults the element type. e.g. _ = [.e]
1065+
bool diagnoseInLiteralCollectionContext() const;
10661066

10671067
static DeclName findCorrectEnumCaseName(Type Ty,
10681068
TypoCorrectionResults &corrections,

test/Constraints/members.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -668,3 +668,8 @@ func test_34770265(_ dict: [Int: Int]) {
668668
// SR-12672
669669
_ = [.e] // expected-error {{reference to member 'e' cannot be resolved without a contextual type}}
670670
let _ : [Any] = [.e] // expected-error {{type 'Any' has no member 'e'}}
671+
_ = [1 :.e] // expected-error {{reference to member 'e' cannot be resolved without a contextual type}}
672+
_ = [.e: 1] // expected-error {{reference to member 'e' cannot be resolved without a contextual type}}
673+
let _ : [Int: Any] = [1 : .e] // expected-error {{type 'Any' has no member 'e'}}
674+
let _ : (Int, Any) = (1, .e) // expected-error {{type 'Any' has no member 'e'}}
675+
_ = (1, .e) // expected-error {{cannot infer contextual base in reference to member 'e'}}

0 commit comments

Comments
 (0)