Skip to content

Commit

Permalink
Merge pull request #31366 from LucianoPAlmeida/SR-12672-diagnostics
Browse files Browse the repository at this point in the history
[SR-12672] Tailored diagnostics for missing member involving array literal default to any element
  • Loading branch information
xedin committed Apr 28, 2020
2 parents 0a38a47 + 723a753 commit a400c74
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 0 deletions.
34 changes: 34 additions & 0 deletions lib/Sema/CSDiagnostics.cpp
Expand Up @@ -3212,6 +3212,9 @@ bool MissingMemberFailure::diagnoseAsError() {

if (diagnoseForDynamicCallable())
return true;

if (diagnoseInLiteralCollectionContext())
return true;

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

Expand Down Expand Up @@ -3399,6 +3402,37 @@ bool MissingMemberFailure::diagnoseForDynamicCallable() const {
return false;
}

bool MissingMemberFailure::diagnoseInLiteralCollectionContext() const {
auto &cs = getConstraintSystem();
auto *expr = castToExpr(getAnchor());
auto *parentExpr = cs.getParentExpr(expr);
auto &solution = getSolution();

if (!(parentExpr && isa<UnresolvedMemberExpr>(expr)))
return false;

auto parentType = getType(parentExpr);

if (!cs.isCollectionType(parentType) && !parentType->is<TupleType>())
return false;

if (isa<TupleExpr>(parentExpr)) {
parentExpr = cs.getParentExpr(parentExpr);
if (!parentExpr)
return false;
}

if (auto *defaultableVar =
cs.getType(parentExpr)->getAs<TypeVariableType>()) {
if (solution.DefaultedConstraints.count(
defaultableVar->getImpl().getLocator()) != 0) {
emitDiagnostic(diag::unresolved_member_no_inference, getName());
return true;
}
}
return false;
}

bool InvalidMemberRefOnExistential::diagnoseAsError() {
auto anchor = getRawAnchor();

Expand Down
4 changes: 4 additions & 0 deletions lib/Sema/CSDiagnostics.h
Expand Up @@ -1059,6 +1059,10 @@ class MissingMemberFailure final : public InvalidMemberRefFailure {
/// overload to be present, but a class marked as `@dynamicCallable`
/// defines only `dynamicallyCall(withArguments:)` variant.
bool diagnoseForDynamicCallable() const;

/// Tailored diagnostics for collection literal with unresolved member expression
/// that defaults the element type. e.g. _ = [.e]
bool diagnoseInLiteralCollectionContext() const;

static DeclName findCorrectEnumCaseName(Type Ty,
TypoCorrectionResults &corrections,
Expand Down
9 changes: 9 additions & 0 deletions test/Constraints/members.swift
Expand Up @@ -664,3 +664,12 @@ func test_34770265(_ dict: [Int: Int]) {
dict.rdar_34770265_val()
// expected-error@-1 {{referencing instance method 'rdar_34770265_val()' on 'Dictionary' requires the types 'Int' and 'String' be equivalent}}
}

// SR-12672
_ = [.e] // expected-error {{reference to member 'e' cannot be resolved without a contextual type}}
let _ : [Any] = [.e] // expected-error {{type 'Any' has no member 'e'}}
_ = [1 :.e] // expected-error {{reference to member 'e' cannot be resolved without a contextual type}}
_ = [.e: 1] // expected-error {{reference to member 'e' cannot be resolved without a contextual type}}
let _ : [Int: Any] = [1 : .e] // expected-error {{type 'Any' has no member 'e'}}
let _ : (Int, Any) = (1, .e) // expected-error {{type 'Any' has no member 'e'}}
_ = (1, .e) // expected-error {{cannot infer contextual base in reference to member 'e'}}

0 comments on commit a400c74

Please sign in to comment.