Skip to content

Commit 13f8098

Browse files
authored
Merge pull request #17685 from jckarter/nested-collection-upcast-peephole-4.2
[4.2] Sema: Clear the types of exprs changed by collection upcast peepholes.
2 parents 78ec3ba + 96eb7aa commit 13f8098

File tree

2 files changed

+37
-3
lines changed

2 files changed

+37
-3
lines changed

lib/Sema/CSApply.cpp

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6326,6 +6326,11 @@ void ExprRewriter::peepholeArrayUpcast(ArrayExpr *expr, Type toType,
63266326
ConstraintLocatorBuilder locator) {
63276327
// Update the type of the array literal.
63286328
cs.setType(expr, toType);
6329+
// FIXME: finish{Array,Dictionary}Expr invoke cacheExprTypes after forming
6330+
// the semantic expression for the dictionary literal, which will undo the
6331+
// type we set here if this dictionary literal is nested unless we update
6332+
// the expr type as well.
6333+
expr->setType(toType);
63296334

63306335
// Convert the elements.
63316336
ConstraintLocatorBuilder innerLocator =
@@ -6350,6 +6355,11 @@ void ExprRewriter::peepholeDictionaryUpcast(DictionaryExpr *expr,
63506355
ConstraintLocatorBuilder locator) {
63516356
// Update the type of the dictionary literal.
63526357
cs.setType(expr, toType);
6358+
// FIXME: finish{Array,Dictionary}Expr invoke cacheExprTypes after forming
6359+
// the semantic expression for the dictionary literal, which will undo the
6360+
// type we set here if this dictionary literal is nested unless we update
6361+
// the expr type as well.
6362+
expr->setType(toType);
63536363

63546364
ConstraintLocatorBuilder keyLocator =
63556365
locator.withPathElement(
@@ -6378,6 +6388,11 @@ void ExprRewriter::peepholeDictionaryUpcast(DictionaryExpr *expr,
63786388
}
63796389

63806390
cs.setType(tuple, tupleType);
6391+
// FIXME: finish{Array,Dictionary}Expr invoke cacheExprTypes after forming
6392+
// the semantic expression for the dictionary literal, which will undo the
6393+
// type we set here if this dictionary literal is nested unless we update
6394+
// the expr type as well.
6395+
tuple->setType(tupleType);
63816396
}
63826397
}
63836398

@@ -6387,16 +6402,22 @@ void ExprRewriter::peepholeDictionaryUpcast(DictionaryExpr *expr,
63876402
bool ExprRewriter::peepholeCollectionUpcast(Expr *expr, Type toType,
63886403
bool bridged,
63896404
ConstraintLocatorBuilder locator) {
6390-
// Recurse into parenthesized expressions.
6405+
// Recur into parenthesized expressions.
63916406
if (auto paren = dyn_cast<ParenExpr>(expr)) {
63926407
// If we can't peephole the subexpression, we're done.
63936408
if (!peepholeCollectionUpcast(paren->getSubExpr(), toType, bridged,
63946409
locator))
63956410
return false;
63966411

63976412
// Update the type of this expression.
6398-
cs.setType(paren, ParenType::get(cs.getASTContext(),
6399-
cs.getType(paren->getSubExpr())));
6413+
auto parenTy = ParenType::get(cs.getASTContext(),
6414+
cs.getType(paren->getSubExpr()));
6415+
cs.setType(paren, parenTy);
6416+
// FIXME: finish{Array,Dictionary}Expr invoke cacheExprTypes after forming
6417+
// the semantic expression for the dictionary literal, which will undo the
6418+
// type we set here if this dictionary literal is nested unless we update
6419+
// the expr type as well.
6420+
paren->setType(parenTy);
64006421
return true;
64016422
}
64026423

test/expr/cast/objc_coerce_array.swift

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,16 @@ import Foundation
55
var x = 1
66

77
_ = [x] as [NSNumber]
8+
9+
_ = ["x":["y":"z","a":1]] as [String : [String : AnyObject]]
10+
_ = ["x":["z",1]] as [String : [AnyObject]]
11+
_ = [["y":"z","a":1]] as [[String : AnyObject]]
12+
_ = [["z",1]] as [[AnyObject]]
13+
14+
var y: Any = 1
15+
16+
_ = ["x":["y":"z","a":y]] as [String : [String : AnyObject]]
17+
_ = ["x":["z",y]] as [String : [AnyObject]]
18+
_ = [["y":"z","a":y]] as [[String : AnyObject]]
19+
_ = [["z",y]] as [[AnyObject]]
20+

0 commit comments

Comments
 (0)