Skip to content

Commit a400c74

Browse files
authored
Merge pull request #31366 from LucianoPAlmeida/SR-12672-diagnostics
[SR-12672] Tailored diagnostics for missing member involving array literal default to any element
2 parents 0a38a47 + 723a753 commit a400c74

File tree

3 files changed

+47
-0
lines changed

3 files changed

+47
-0
lines changed

lib/Sema/CSDiagnostics.cpp

Lines changed: 34 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 (diagnoseInLiteralCollectionContext())
3217+
return true;
32153218

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

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

3405+
bool MissingMemberFailure::diagnoseInLiteralCollectionContext() const {
3406+
auto &cs = getConstraintSystem();
3407+
auto *expr = castToExpr(getAnchor());
3408+
auto *parentExpr = cs.getParentExpr(expr);
3409+
auto &solution = getSolution();
3410+
3411+
if (!(parentExpr && isa<UnresolvedMemberExpr>(expr)))
3412+
return false;
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) {
3429+
emitDiagnostic(diag::unresolved_member_no_inference, getName());
3430+
return true;
3431+
}
3432+
}
3433+
return false;
3434+
}
3435+
34023436
bool InvalidMemberRefOnExistential::diagnoseAsError() {
34033437
auto anchor = getRawAnchor();
34043438

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

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

test/Constraints/members.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -664,3 +664,12 @@ func test_34770265(_ dict: [Int: Int]) {
664664
dict.rdar_34770265_val()
665665
// expected-error@-1 {{referencing instance method 'rdar_34770265_val()' on 'Dictionary' requires the types 'Int' and 'String' be equivalent}}
666666
}
667+
668+
// SR-12672
669+
_ = [.e] // expected-error {{reference to member 'e' cannot be resolved without a contextual type}}
670+
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)