Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SR-12672] Tailored diagnostics for missing member involving array literal default to any element #31366

Merged
merged 3 commits into from Apr 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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;
xedin marked this conversation as resolved.
Show resolved Hide resolved

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'}}