Skip to content

[QoI] Check before trying to emit fix-it to convert from array to dictionary #9839

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

Merged
merged 1 commit into from
May 23, 2017
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions lib/Sema/CSDiag.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7254,11 +7254,12 @@ bool FailureDiagnosis::visitArrayExpr(ArrayExpr *E) {
}
}
}


auto numElements = E->getNumElements();
if (!Conformance) {
// If the contextual type conforms to ExpressibleByDictionaryLiteral and
// this is an empty array, then they meant "[:]".
if (E->getNumElements() == 0 &&
if (numElements == 0 &&
isDictionaryLiteralCompatible(contextualType, CS, E->getLoc())) {
diagnose(E->getStartLoc(), diag::should_use_empty_dictionary_literal)
.fixItInsert(E->getEndLoc(), ":");
Expand All @@ -7271,13 +7272,19 @@ bool FailureDiagnosis::visitArrayExpr(ArrayExpr *E) {

// If the contextual type conforms to ExpressibleByDictionaryLiteral, then
// they wrote "x = [1,2]" but probably meant "x = [1:2]".
if ((E->getElements().size() & 1) == 0 && !E->getElements().empty() &&
if ((numElements & 1) == 0 && numElements > 0 &&
isDictionaryLiteralCompatible(contextualType, CS, E->getLoc())) {
auto diag = diagnose(E->getStartLoc(), diag::meant_dictionary_lit);

// Change every other comma into a colon.
for (unsigned i = 0, e = E->getElements().size()/2; i != e; ++i)
diag.fixItReplace(E->getCommaLocs()[i*2], ":");
// Change every other comma into a colon, only if the number
// of commas present matches the number of elements, because
// otherwise it might a structural problem with the expression
// e.g. ["a""b": 1].
const auto commaLocs = E->getCommaLocs();
if (commaLocs.size() == numElements - 1) {
for (unsigned i = 0, e = numElements / 2; i != e; ++i)
diag.fixItReplace(commaLocs[i*2], ":");
}
}

return true;
Expand Down
14 changes: 14 additions & 0 deletions test/Constraints/dictionary_literal.swift
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,17 @@ func testDefaultExistentials() {
let _ = ["a" : "hello", 17 : "string"]
// expected-error@-1{{heterogeneous collection literal could only be inferred to 'Dictionary<AnyHashable, String>'}}
}

// SR-4952, rdar://problem/32330004 - Assertion failure during swift::ASTVisitor<::FailureDiagnosis,...>::visit
func rdar32330004_1() -> [String: Any] {
return ["a""one": 1, "two": 2, "three": 3] // expected-note {{did you mean to use a dictionary literal instead?}}
// expected-error@-1 2 {{expected ',' separator}}
// expected-error@-2 {{expected expression in container literal}}
// expected-error@-3 {{contextual type '[String : Any]' cannot be used with array literal}}
}

func rdar32330004_2() -> [String: Any] {
return ["a", 0, "one", 1, "two", 2, "three", 3]
// expected-error@-1 {{contextual type '[String : Any]' cannot be used with array literal}}
// expected-note@-2 {{did you mean to use a dictionary literal instead?}} {{14-15=:}} {{24-25=:}} {{34-35=:}} {{46-47=:}}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Newline please!