Skip to content

[4.2] Sema: Clear the types of exprs changed by collection upcast peepholes. #17685

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
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
27 changes: 24 additions & 3 deletions lib/Sema/CSApply.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6326,6 +6326,11 @@ void ExprRewriter::peepholeArrayUpcast(ArrayExpr *expr, Type toType,
ConstraintLocatorBuilder locator) {
// Update the type of the array literal.
cs.setType(expr, toType);
// FIXME: finish{Array,Dictionary}Expr invoke cacheExprTypes after forming
// the semantic expression for the dictionary literal, which will undo the
// type we set here if this dictionary literal is nested unless we update
// the expr type as well.
expr->setType(toType);

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

ConstraintLocatorBuilder keyLocator =
locator.withPathElement(
Expand Down Expand Up @@ -6378,6 +6388,11 @@ void ExprRewriter::peepholeDictionaryUpcast(DictionaryExpr *expr,
}

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

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

// Update the type of this expression.
cs.setType(paren, ParenType::get(cs.getASTContext(),
cs.getType(paren->getSubExpr())));
auto parenTy = ParenType::get(cs.getASTContext(),
cs.getType(paren->getSubExpr()));
cs.setType(paren, parenTy);
// FIXME: finish{Array,Dictionary}Expr invoke cacheExprTypes after forming
// the semantic expression for the dictionary literal, which will undo the
// type we set here if this dictionary literal is nested unless we update
// the expr type as well.
paren->setType(parenTy);
return true;
}

Expand Down
13 changes: 13 additions & 0 deletions test/expr/cast/objc_coerce_array.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,16 @@ import Foundation
var x = 1

_ = [x] as [NSNumber]

_ = ["x":["y":"z","a":1]] as [String : [String : AnyObject]]
_ = ["x":["z",1]] as [String : [AnyObject]]
_ = [["y":"z","a":1]] as [[String : AnyObject]]
_ = [["z",1]] as [[AnyObject]]

var y: Any = 1

_ = ["x":["y":"z","a":y]] as [String : [String : AnyObject]]
_ = ["x":["z",y]] as [String : [AnyObject]]
_ = [["y":"z","a":y]] as [[String : AnyObject]]
_ = [["z",y]] as [[AnyObject]]