Skip to content

Commit 42cf1b7

Browse files
[Sema] Adjusting repairArraLiteralUsedAsDictionary to also ignore OptToOpt promotions
1 parent c6f00fa commit 42cf1b7

File tree

3 files changed

+14
-9
lines changed

3 files changed

+14
-9
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4228,14 +4228,16 @@ static bool repairArrayLiteralUsedAsDictionary(
42284228
if (auto *fix = TreatArrayLiteralAsDictionary::attempt(cs, dictType,
42294229
arrayType, loc)) {
42304230
// Ignore any attempts at promoting the value to an optional as even after
4231-
// stripping off all optionals above the underlying types don't match (array
4231+
// stripping off all optionals above the underlying types won't match (array
42324232
// vs dictionary).
42334233
conversionsOrFixes.erase(
42344234
llvm::remove_if(conversionsOrFixes,
42354235
[&](RestrictionOrFix &E) {
42364236
if (auto restriction = E.getRestriction())
4237-
return *restriction ==
4238-
ConversionRestrictionKind::ValueToOptional;
4237+
return *restriction == ConversionRestrictionKind::
4238+
ValueToOptional ||
4239+
*restriction == ConversionRestrictionKind::
4240+
OptionalToOptional;
42394241
return false;
42404242
}),
42414243
conversionsOrFixes.end());
@@ -4702,6 +4704,12 @@ bool ConstraintSystem::repairFailures(
47024704
return true;
47034705
}
47044706

4707+
// If we are trying to assign e.g. `Array<Int>` to `Array<Float>` let's
4708+
// give solver a chance to determine which generic parameters are
4709+
// mismatched and produce a fix for that.
4710+
if (hasConversionOrRestriction(ConversionRestrictionKind::DeepEquality))
4711+
return false;
4712+
47054713
// An attempt to assign `Int?` to `String?`.
47064714
if (hasConversionOrRestriction(
47074715
ConversionRestrictionKind::OptionalToOptional)) {
@@ -4710,12 +4718,6 @@ bool ConstraintSystem::repairFailures(
47104718
return true;
47114719
}
47124720

4713-
// If we are trying to assign e.g. `Array<Int>` to `Array<Float>` let's
4714-
// give solver a chance to determine which generic parameters are
4715-
// mismatched and produce a fix for that.
4716-
if (hasConversionOrRestriction(ConversionRestrictionKind::DeepEquality))
4717-
return false;
4718-
47194721
// If the situation has to do with protocol composition types and
47204722
// destination doesn't have one of the conformances e.g. source is
47214723
// `X & Y` but destination is only `Y` or vice versa, there is a

test/Constraints/closures.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,7 @@ func rdar21078316() {
371371
var foo : [String : String]?
372372
var bar : [(String, String)]?
373373
bar = foo.map { ($0, $1) } // expected-error {{contextual closure type '([String : String]) throws -> [(String, String)]' expects 1 argument, but 2 were used in closure body}}
374+
// expected-error@-1{{cannot convert value of type '(Dictionary<String, String>, _)' to closure result type '[(String, String)]'}}
374375
}
375376

376377

test/Constraints/dictionary_literal.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,8 @@ class S59215 {
175175
// expected-note@-1{{did you mean to use a dictionary literal instead?}} {{17-18=:}}
176176
m["a"] = [1 , 1] //expected-error{{dictionary of type '[String : String]' cannot be used with array literal}}
177177
// expected-note@-1{{did you mean to use a dictionary literal instead?}} {{17-18=:}}
178+
m["a"] = Optional(["", ""]) //expected-error{{dictionary of type '[String : String]' cannot be used with array literal}}
179+
// expected-note@-1{{did you mean to use a dictionary literal instead?}} {{26-27=:}}
178180
}
179181
}
180182

0 commit comments

Comments
 (0)