Skip to content

Commit 55f8a20

Browse files
authored
Merge pull request #31386 from LucianoPAlmeida/SR-12672-to-5-3
[5.3][Diagnostics] Tailored diagnostics for missing member failure when in…
2 parents 733a7cf + 11eb6a1 commit 55f8a20

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
@@ -3236,6 +3236,9 @@ bool MissingMemberFailure::diagnoseAsError() {
32363236

32373237
if (diagnoseForDynamicCallable())
32383238
return true;
3239+
3240+
if (diagnoseInLiteralCollectionContext())
3241+
return true;
32393242

32403243
auto baseType = resolveType(getBaseType())->getWithoutSpecifierType();
32413244

@@ -3424,6 +3427,37 @@ bool MissingMemberFailure::diagnoseForDynamicCallable() const {
34243427
return false;
34253428
}
34263429

3430+
bool MissingMemberFailure::diagnoseInLiteralCollectionContext() const {
3431+
auto &cs = getConstraintSystem();
3432+
auto *expr = castToExpr<Expr>(getAnchor());
3433+
auto *parentExpr = findParentExpr(expr);
3434+
auto &solution = getSolution();
3435+
3436+
if (!(parentExpr && isa<UnresolvedMemberExpr>(expr)))
3437+
return false;
3438+
3439+
auto parentType = getType(parentExpr);
3440+
3441+
if (!cs.isCollectionType(parentType) && !parentType->is<TupleType>())
3442+
return false;
3443+
3444+
if (isa<TupleExpr>(parentExpr)) {
3445+
parentExpr = findParentExpr(parentExpr);
3446+
if (!parentExpr)
3447+
return false;
3448+
}
3449+
3450+
if (auto *defaultableVar =
3451+
cs.getType(parentExpr)->getAs<TypeVariableType>()) {
3452+
if (solution.DefaultedConstraints.count(
3453+
defaultableVar->getImpl().getLocator()) != 0) {
3454+
emitDiagnostic(diag::unresolved_member_no_inference, getName());
3455+
return true;
3456+
}
3457+
}
3458+
return false;
3459+
}
3460+
34273461
bool InvalidMemberRefOnExistential::diagnoseAsError() {
34283462
auto anchor = getRawAnchor();
34293463

lib/Sema/CSDiagnostics.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1076,6 +1076,10 @@ class MissingMemberFailure final : public InvalidMemberRefFailure {
10761076
/// overload to be present, but a class marked as `@dynamicCallable`
10771077
/// defines only `dynamicallyCall(withArguments:)` variant.
10781078
bool diagnoseForDynamicCallable() const;
1079+
1080+
/// Tailored diagnostics for collection literal with unresolved member expression
1081+
/// that defaults the element type. e.g. _ = [.e]
1082+
bool diagnoseInLiteralCollectionContext() const;
10791083

10801084
static DeclName findCorrectEnumCaseName(Type Ty,
10811085
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)